JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.renderer;
22
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.CodingUtils;
25 import jalview.analysis.Rna;
26 import jalview.analysis.StructureFrequency;
27 import jalview.api.AlignViewportI;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.ProfilesI;
33 import jalview.schemes.ColourSchemeI;
34 import jalview.schemes.NucleotideColourScheme;
35 import jalview.schemes.ResidueProperties;
36 import jalview.schemes.ZappoColourScheme;
37 import jalview.util.Platform;
38
39 import java.awt.BasicStroke;
40 import java.awt.Color;
41 import java.awt.Font;
42 import java.awt.FontMetrics;
43 import java.awt.Graphics;
44 import java.awt.Graphics2D;
45 import java.awt.Image;
46 import java.awt.geom.AffineTransform;
47 import java.awt.image.ImageObserver;
48 import java.util.BitSet;
49 import java.util.Hashtable;
50
51 public class AnnotationRenderer
52 {
53   private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
54
55   private static final int CHAR_A = 'A'; // 65
56
57   private static final int CHAR_Z = 'Z'; // 90
58
59   /**
60    * flag indicating if timing and redraw parameter info should be output
61    */
62   private final boolean debugRedraw;
63
64   private int charWidth, endRes, charHeight;
65
66   private boolean validCharWidth, hasHiddenColumns;
67
68   private FontMetrics fm;
69
70   private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
71
72   boolean av_renderHistogram = true, av_renderProfile = true,
73           av_normaliseProfile = false;
74
75   ResidueShaderI profcolour = null;
76
77   private ColumnSelection columnSelection;
78
79   private HiddenColumns hiddenColumns;
80
81   private ProfilesI hconsensus;
82
83   private Hashtable<String, Object>[] complementConsensus;
84
85   private Hashtable<String, Object>[] hStrucConsensus;
86
87   private boolean av_ignoreGapsConsensus;
88
89   /**
90    * attributes set from AwtRenderPanelI
91    */
92   /**
93    * old image used when data is currently being calculated and cannot be
94    * rendered
95    */
96   private Image fadedImage;
97
98   /**
99    * panel being rendered into
100    */
101   private ImageObserver annotationPanel;
102
103   /**
104    * width of image to render in panel
105    */
106   private int imgWidth;
107
108   /**
109    * offset to beginning of visible area
110    */
111   private int sOffset;
112
113   /**
114    * offset to end of visible area
115    */
116   private int visHeight;
117
118   /**
119    * indicate if the renderer should only render the visible portion of the
120    * annotation given the current view settings
121    */
122   private boolean useClip = true;
123
124   /**
125    * master flag indicating if renderer should ever try to clip. not enabled for
126    * jalview 2.8.1
127    */
128   private boolean canClip = false;
129
130   public AnnotationRenderer()
131   {
132     this(false);
133   }
134
135   /**
136    * Create a new annotation Renderer
137    * 
138    * @param debugRedraw
139    *          flag indicating if timing and redraw parameter info should be
140    *          output
141    */
142   public AnnotationRenderer(boolean debugRedraw)
143   {
144     this.debugRedraw = debugRedraw;
145   }
146
147   /**
148    * Remove any references and resources when this object is no longer required
149    */
150   public void dispose()
151   {
152     hiddenColumns = null;
153     hconsensus = null;
154     complementConsensus = null;
155     hStrucConsensus = null;
156     fadedImage = null;
157     annotationPanel = null;
158   }
159
160   void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
161           int x, int y, int iconOffset, int startRes, int column,
162           boolean validRes, boolean validEnd)
163   {
164     g.setColor(STEM_COLOUR);
165     int sCol = (lastSSX / charWidth)
166             + hiddenColumns.visibleToAbsoluteColumn(startRes);
167     int x1 = lastSSX;
168     int x2 = (x * charWidth);
169
170     char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
171             : row_annotations[column - 1].secondaryStructure;
172
173     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
174             || dc != row_annotations[sCol - 1].secondaryStructure;
175     boolean diffdownstream = !validRes || !validEnd
176             || row_annotations[column] == null
177             || dc != row_annotations[column].secondaryStructure;
178
179     if (column > 0 && Rna.isClosingParenthesis(dc))
180     {
181       if (diffupstream)
182       // if (validRes && column>1 && row_annotations[column-2]!=null &&
183       // dc.equals(row_annotations[column-2].displayCharacter))
184       {
185         /*
186          * if new annotation with a closing base pair half of the stem, 
187          * display a backward arrow
188          */
189         g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
190                 new int[]
191                 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
192                 3);
193         x1 += 5;
194       }
195       if (diffdownstream)
196       {
197         x2 -= 1;
198       }
199     }
200     else
201     {
202       // display a forward arrow
203       if (diffdownstream)
204       {
205         /*
206          * if annotation ending with an opeing base pair half of the stem, 
207          * display a forward arrow
208          */
209         g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
210                 new int[]
211                 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
212                 3);
213         x2 -= 5;
214       }
215       if (diffupstream)
216       {
217         x1 += 1;
218       }
219     }
220     // draw arrow body
221     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
222   }
223
224   void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
225           Annotation[] row_annotations, int lastSSX, int x, int y,
226           int iconOffset, int startRes, int column, boolean validRes,
227           boolean validEnd)
228   {
229     // System.out.println(nonCanColor);
230
231     g.setColor(nonCanColor);
232     int sCol = (lastSSX / charWidth)
233             + hiddenColumns.visibleToAbsoluteColumn(startRes);
234     int x1 = lastSSX;
235     int x2 = (x * charWidth);
236
237     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
238             : row_annotations[column - 1].displayCharacter;
239
240     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
241             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
242     boolean diffdownstream = !validRes || !validEnd
243             || row_annotations[column] == null
244             || !dc.equals(row_annotations[column].displayCharacter);
245     // System.out.println("Column "+column+" diff up: "+diffupstream+"
246     // down:"+diffdownstream);
247     // If a closing base pair half of the stem, display a backward arrow
248     if (column > 0 && Rna.isClosingParenthesis(dc))
249     {
250
251       if (diffupstream)
252       // if (validRes && column>1 && row_annotations[column-2]!=null &&
253       // dc.equals(row_annotations[column-2].displayCharacter))
254       {
255         g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
256                 new int[]
257                 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
258                 3);
259         x1 += 5;
260       }
261       if (diffdownstream)
262       {
263         x2 -= 1;
264       }
265     }
266     else
267     {
268
269       // display a forward arrow
270       if (diffdownstream)
271       {
272         g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
273                 new int[]
274                 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
275                 3);
276         x2 -= 5;
277       }
278       if (diffupstream)
279       {
280         x1 += 1;
281       }
282     }
283     // draw arrow body
284     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
285   }
286
287   // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
288   // av)
289   public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
290           AlignViewportI av)
291   {
292     fm = annotPanel.getFontMetrics();
293     annotationPanel = annotPanel;
294     fadedImage = annotPanel.getFadedImage();
295     imgWidth = annotPanel.getFadedImageWidth();
296     // visible area for rendering
297     int[] bounds = annotPanel.getVisibleVRange();
298     if (bounds != null)
299     {
300       sOffset = bounds[0];
301       visHeight = bounds[1];
302       if (visHeight == 0)
303       {
304         useClip = false;
305       }
306       else
307       {
308         useClip = canClip;
309       }
310     }
311     else
312     {
313       useClip = false;
314     }
315
316     updateFromAlignViewport(av);
317   }
318
319   public void updateFromAlignViewport(AlignViewportI av)
320   {
321     charWidth = av.getCharWidth();
322     endRes = av.getRanges().getEndRes();
323     charHeight = av.getCharHeight();
324     hasHiddenColumns = av.hasHiddenColumns();
325     validCharWidth = av.isValidCharWidth();
326     av_renderHistogram = av.isShowConsensusHistogram();
327     av_renderProfile = av.isShowSequenceLogo();
328     av_normaliseProfile = av.isNormaliseSequenceLogo();
329     profcolour = av.getResidueShading();
330     if (profcolour == null || profcolour.getColourScheme() == null)
331     {
332       /*
333        * Use default colour for sequence logo if 
334        * the alignment has no colourscheme set
335        * (would like to use user preference but n/a for applet)
336        */
337       ColourSchemeI col = av.getAlignment().isNucleotide()
338               ? new NucleotideColourScheme()
339               : new ZappoColourScheme();
340       profcolour = new ResidueShader(col);
341     }
342     columnSelection = av.getColumnSelection();
343     hiddenColumns = av.getAlignment().getHiddenColumns();
344     hconsensus = av.getSequenceConsensusHash();
345     complementConsensus = av.getComplementConsensusHash();
346     hStrucConsensus = av.getRnaStructureConsensusHash();
347     av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
348   }
349
350   /**
351    * Returns profile data; the first element is the profile type, the second is
352    * the number of distinct values, the third the total count, and the remainder
353    * depend on the profile type.
354    * 
355    * @param aa
356    * @param column
357    * @return
358    */
359   int[] getProfileFor(AlignmentAnnotation aa, int column)
360   {
361     // TODO : consider refactoring the global alignment calculation
362     // properties/rendering attributes as a global 'alignment group' which holds
363     // all vis settings for the alignment as a whole rather than a subset
364     //
365     if (aa.autoCalculated && (aa.label.startsWith("Consensus")
366             || aa.label.startsWith("cDNA Consensus")))
367     {
368       boolean forComplement = aa.label.startsWith("cDNA Consensus");
369       if (aa.groupRef != null && aa.groupRef.consensusData != null
370               && aa.groupRef.isShowSequenceLogo())
371       {
372         // TODO? group consensus for cDNA complement
373         return AAFrequency.extractProfile(
374                 aa.groupRef.consensusData.get(column),
375                 aa.groupRef.getIgnoreGapsConsensus());
376       }
377       // TODO extend annotation row to enable dynamic and static profile data to
378       // be stored
379       if (aa.groupRef == null && aa.sequenceRef == null)
380       {
381         if (forComplement)
382         {
383           return AAFrequency.extractCdnaProfile(complementConsensus[column],
384                   av_ignoreGapsConsensus);
385         }
386         else
387         {
388           return AAFrequency.extractProfile(hconsensus.get(column),
389                   av_ignoreGapsConsensus);
390         }
391       }
392     }
393     else
394     {
395       if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
396       {
397         // TODO implement group structure consensus
398         /*
399          * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
400          * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
401          * group selections return StructureFrequency.extractProfile(
402          * aa.groupRef.consensusData[column], aa.groupRef
403          * .getIgnoreGapsConsensus()); }
404          */
405         // TODO extend annotation row to enable dynamic and static profile data
406         // to
407         // be stored
408         if (aa.groupRef == null && aa.sequenceRef == null
409                 && hStrucConsensus != null
410                 && hStrucConsensus.length > column)
411         {
412           return StructureFrequency.extractProfile(hStrucConsensus[column],
413                   av_ignoreGapsConsensus);
414         }
415       }
416     }
417     return null;
418   }
419
420   /**
421    * Render the annotation rows associated with an alignment.
422    * 
423    * @param annotPanel
424    *          container frame
425    * @param av
426    *          data and view settings to render
427    * @param g
428    *          destination for graphics
429    * @param activeRow
430    *          row where a mouse event occured (or -1)
431    * @param startRes
432    *          first column that will be drawn
433    * @param endRes
434    *          last column that will be drawn
435    * @return true if the fadedImage was used for any alignment annotation rows
436    *         currently being calculated
437    */
438   public boolean drawComponent(AwtRenderPanelI annotPanel,
439           AlignViewportI av, Graphics g, int activeRow, int startRes,
440           int endRes)
441   {
442     long stime = System.currentTimeMillis();
443     boolean usedFaded = false;
444     // NOTES:
445     // AnnotationPanel needs to implement: ImageObserver, access to
446     // AlignViewport
447     updateFromAwtRenderPanel(annotPanel, av);
448     fm = g.getFontMetrics();
449     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
450     // int temp = 0;
451     if (aa == null)
452     {
453       return false;
454     }
455     int x = 0, y = 0;
456     int column = 0;
457     char lastSS;
458     int lastSSX;
459     int iconOffset = 0;
460     boolean validRes = false;
461     boolean validEnd = false;
462     boolean labelAllCols = false;
463     // boolean centreColLabels;
464     // boolean centreColLabelsDef = av.isCentreColumnLabels();
465     boolean scaleColLabel = false;
466     final AlignmentAnnotation consensusAnnot = av
467             .getAlignmentConsensusAnnotation();
468     final AlignmentAnnotation structConsensusAnnot = av
469             .getAlignmentStrucConsensusAnnotation();
470     final AlignmentAnnotation complementConsensusAnnot = av
471             .getComplementConsensusAnnotation();
472
473     BitSet graphGroupDrawn = new BitSet();
474     int charOffset = 0; // offset for a label
475     // \u03B2 \u03B1
476     // debug ints
477     int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
478     boolean clipst = false, clipend = false;
479     for (int i = 0; i < aa.length; i++)
480     {
481       AlignmentAnnotation row = aa[i];
482       boolean renderHistogram = true;
483       boolean renderProfile = false;
484       boolean normaliseProfile = false;
485       boolean isRNA = row.isRNA();
486
487       // check if this is a consensus annotation row and set the display
488       // settings appropriately
489       // TODO: generalise this to have render styles for consensus/profile
490       // data
491       if (row.groupRef != null && row == row.groupRef.getConsensus())
492       {
493         renderHistogram = row.groupRef.isShowConsensusHistogram();
494         renderProfile = row.groupRef.isShowSequenceLogo();
495         normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
496       }
497       else if (row == consensusAnnot || row == structConsensusAnnot
498               || row == complementConsensusAnnot)
499       {
500         renderHistogram = av_renderHistogram;
501         renderProfile = av_renderProfile;
502         normaliseProfile = av_normaliseProfile;
503       }
504
505       Annotation[] row_annotations = row.annotations;
506       if (!row.visible)
507       {
508         continue;
509       }
510       // centreColLabels = row.centreColLabels || centreColLabelsDef;
511       labelAllCols = row.showAllColLabels;
512       scaleColLabel = row.scaleColLabel;
513       lastSS = ' ';
514       lastSSX = 0;
515
516       if (!useClip || ((y - charHeight) < visHeight
517               && (y + row.height + charHeight * 2) >= sOffset))
518       {// if_in_visible_region
519         if (!clipst)
520         {
521           clipst = true;
522           yfrom = y;
523           f_i = i;
524         }
525         yto = y;
526         f_to = i;
527         if (row.graph > 0)
528         {
529           if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
530           {
531             continue;
532           }
533
534           // this is so that we draw the characters below the graph
535           y += row.height;
536
537           if (row.hasText)
538           {
539             iconOffset = charHeight - fm.getDescent();
540             y -= charHeight;
541           }
542         }
543         else if (row.hasText)
544         {
545           iconOffset = charHeight - fm.getDescent();
546
547         }
548         else
549         {
550           iconOffset = 0;
551         }
552
553         if (row.autoCalculated && av.isCalculationInProgress(row))
554         {
555           y += charHeight;
556           usedFaded = true;
557           g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
558                   y - row.height, imgWidth, y, annotationPanel);
559           g.setColor(Color.black);
560           // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
561
562           continue;
563         }
564
565         /*
566          * else if (annotationPanel.av.updatingConservation &&
567          * aa[i].label.equals("Conservation")) {
568          * 
569          * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
570          * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
571          * annotationPanel.imgWidth, y, annotationPanel);
572          * 
573          * g.setColor(Color.black); //
574          * g.drawString("Calculating Conservation.....",20, y-row.height/2);
575          * 
576          * continue; } else if (annotationPanel.av.updatingConservation &&
577          * aa[i].label.equals("Quality")) {
578          * 
579          * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
580          * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
581          * annotationPanel.imgWidth, y, annotationPanel);
582          * g.setColor(Color.black); // /
583          * g.drawString("Calculating Quality....",20, y-row.height/2);
584          * 
585          * continue; }
586          */
587         // first pass sets up state for drawing continuation from left-hand
588         // column
589         // of startRes
590         x = (startRes == 0) ? 0 : -1;
591         while (x < endRes - startRes)
592         {
593           if (hasHiddenColumns)
594           {
595             column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
596             if (column > row_annotations.length - 1)
597             {
598               break;
599             }
600           }
601           else
602           {
603             column = startRes + x;
604           }
605
606           if ((row_annotations == null)
607                   || (row_annotations.length <= column)
608                   || (row_annotations[column] == null))
609           {
610             validRes = false;
611           }
612           else
613           {
614             validRes = true;
615           }
616           final String displayChar = validRes
617                   ? row_annotations[column].displayCharacter
618                   : null;
619           if (x > -1)
620           {
621             if (activeRow == i)
622             {
623               g.setColor(Color.red);
624
625               if (columnSelection != null)
626               {
627                 if (columnSelection.contains(column))
628                 {
629                   g.fillRect(x * charWidth, y, charWidth, charHeight);
630                 }
631               }
632             }
633             if (row.getInvalidStrucPos() > x)
634             {
635               g.setColor(Color.orange);
636               g.fillRect(x * charWidth, y, charWidth, charHeight);
637             }
638             else if (row.getInvalidStrucPos() == x)
639             {
640               g.setColor(Color.orange.darker());
641               g.fillRect(x * charWidth, y, charWidth, charHeight);
642             }
643             if (validCharWidth && validRes && displayChar != null
644                     && (displayChar.length() > 0))
645             {
646               Graphics2D gg = ((Graphics2D) g);
647               float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
648                       displayChar.length());
649
650               /*
651                * shrink label width to fit in column, if that is
652                * both configured and necessary
653                */
654               boolean scaledToFit = false;
655               float fmScaling = 1f;
656               if (scaleColLabel && fmWidth > charWidth)
657               {
658                 scaledToFit = true;
659                 fmScaling = charWidth;
660                 fmScaling /= fmWidth;
661                 // and update the label's width to reflect the scaling.
662                 fmWidth = charWidth;
663               }
664
665               charOffset = (int) ((charWidth - fmWidth) / 2f);
666
667               if (row_annotations[column].colour == null)
668               {
669                 gg.setColor(Color.black);
670               }
671               else
672               {
673                 gg.setColor(row_annotations[column].colour);
674               }
675
676               /*
677                * draw the label, unless it is the same secondary structure
678                * symbol (excluding RNA Helix) as the previous column
679                */
680               final int xPos = (x * charWidth) + charOffset;
681               final int yPos = y + iconOffset;
682
683               /*
684                * translate to drawing position _before_ applying any scaling
685                */
686               gg.translate(xPos, yPos);
687               if (scaledToFit)
688               {
689                 /*
690                  * use a scaling transform to make the label narrower
691                  * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
692                  */
693                 gg.transform(
694                         AffineTransform.getScaleInstance(fmScaling, 1.0));
695               }
696               if (column == 0 || row.graph > 0)
697               {
698                 gg.drawString(displayChar, 0, 0);
699               }
700               else if (row_annotations[column - 1] == null || (labelAllCols
701                       || !displayChar.equals(
702                               row_annotations[column - 1].displayCharacter)
703                       || (displayChar.length() < 2
704                               && row_annotations[column].secondaryStructure == ' ')))
705               {
706                 gg.drawString(displayChar, 0, 0);
707               }
708               if (scaledToFit)
709               {
710                 /*
711                  * undo scaling before translating back 
712                  * (restoring saved transform does NOT work in JS PDFGraphics!)
713                  */
714                 gg.transform(AffineTransform
715                         .getScaleInstance(1D / fmScaling, 1.0));
716               }
717               gg.translate(-xPos, -yPos);
718             }
719           }
720           if (row.hasIcons)
721           {
722             char ss = validRes ? row_annotations[column].secondaryStructure
723                     : '-';
724
725             if (ss == '(')
726             {
727               // distinguish between forward/backward base-pairing
728               if (displayChar.indexOf(')') > -1)
729               {
730
731                 ss = ')';
732
733               }
734             }
735             if (ss == '[')
736             {
737               if ((displayChar.indexOf(']') > -1))
738               {
739                 ss = ']';
740
741               }
742             }
743             if (ss == '{')
744             {
745               // distinguish between forward/backward base-pairing
746               if (displayChar.indexOf('}') > -1)
747               {
748                 ss = '}';
749
750               }
751             }
752             if (ss == '<')
753             {
754               // distinguish between forward/backward base-pairing
755               if (displayChar.indexOf('<') > -1)
756               {
757                 ss = '>';
758
759               }
760             }
761             if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
762             {
763               // distinguish between forward/backward base-pairing
764               int ssLowerCase = ss + UPPER_TO_LOWER;
765               // TODO would .equals() be safer here? or charAt(0)?
766               if (displayChar.indexOf(ssLowerCase) > -1)
767               {
768                 ss = (char) ssLowerCase;
769               }
770             }
771
772             if (!validRes || (ss != lastSS))
773             {
774
775               if (x > -1)
776               {
777
778                 // int nb_annot = x - temp;
779                 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
780                 // annot :"+nb_annot);
781                 switch (lastSS)
782                 {
783                 case '(': // Stem case for RNA secondary structure
784                 case ')': // and opposite direction
785                   drawStemAnnot(g, row_annotations, lastSSX, x, y,
786                           iconOffset, startRes, column, validRes, validEnd);
787                   // temp = x;
788                   break;
789
790                 case 'H':
791                   if (!isRNA)
792                   {
793                     drawHelixAnnot(g, row_annotations, lastSSX, x, y,
794                             iconOffset, startRes, column, validRes,
795                             validEnd);
796                     break;
797                   }
798                   // no break if isRNA - falls through to drawNotCanonicalAnnot!
799                 case 'E':
800                   if (!isRNA)
801                   {
802                     drawSheetAnnot(g, row_annotations, lastSSX, x, y,
803                             iconOffset, startRes, column, validRes,
804                             validEnd);
805                     break;
806                   }
807                   // no break if isRNA - fall through to drawNotCanonicalAnnot!
808
809                 case '{':
810                 case '}':
811                 case '[':
812                 case ']':
813                 case '>':
814                 case '<':
815                 case 'A':
816                 case 'a':
817                 case 'B':
818                 case 'b':
819                 case 'C':
820                 case 'c':
821                 case 'D':
822                 case 'd':
823                 case 'e':
824                 case 'F':
825                 case 'f':
826                 case 'G':
827                 case 'g':
828                 case 'h':
829                 case 'I':
830                 case 'i':
831                 case 'J':
832                 case 'j':
833                 case 'K':
834                 case 'k':
835                 case 'L':
836                 case 'l':
837                 case 'M':
838                 case 'm':
839                 case 'N':
840                 case 'n':
841                 case 'O':
842                 case 'o':
843                 case 'P':
844                 case 'p':
845                 case 'Q':
846                 case 'q':
847                 case 'R':
848                 case 'r':
849                 case 'S':
850                 case 's':
851                 case 'T':
852                 case 't':
853                 case 'U':
854                 case 'u':
855                 case 'V':
856                 case 'v':
857                 case 'W':
858                 case 'w':
859                 case 'X':
860                 case 'x':
861                 case 'Y':
862                 case 'y':
863                 case 'Z':
864                 case 'z':
865
866                   Color nonCanColor = getNotCanonicalColor(lastSS);
867                   drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
868                           lastSSX, x, y, iconOffset, startRes, column,
869                           validRes, validEnd);
870                   // temp = x;
871                   break;
872                 default:
873                   g.setColor(Color.gray);
874                   g.fillRect(lastSSX, y + 6 + iconOffset,
875                           (x * charWidth) - lastSSX, 2);
876                   // temp = x;
877                   break;
878                 }
879               }
880               if (validRes)
881               {
882                 lastSS = ss;
883               }
884               else
885               {
886                 lastSS = ' ';
887               }
888               if (x > -1)
889               {
890                 lastSSX = (x * charWidth);
891               }
892             }
893           }
894           column++;
895           x++;
896         }
897         if (column >= row_annotations.length)
898         {
899           column = row_annotations.length - 1;
900           validEnd = false;
901         }
902         else
903         {
904           validEnd = true;
905         }
906         if ((row_annotations == null) || (row_annotations.length <= column)
907                 || (row_annotations[column] == null))
908         {
909           validRes = false;
910         }
911         else
912         {
913           validRes = true;
914         }
915         // x ++;
916
917         if (row.hasIcons)
918         {
919           switch (lastSS)
920           {
921
922           case 'H':
923             if (!isRNA)
924             {
925               drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
926                       startRes, column, validRes, validEnd);
927               break;
928             }
929             // no break if isRNA - fall through to drawNotCanonicalAnnot!
930
931           case 'E':
932             if (!isRNA)
933             {
934               drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
935                       startRes, column, validRes, validEnd);
936               break;
937             }
938             // no break if isRNA - fall through to drawNotCanonicalAnnot!
939
940           case '(':
941           case ')': // Stem case for RNA secondary structure
942
943             drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
944                     startRes, column, validRes, validEnd);
945
946             break;
947           case '{':
948           case '}':
949           case '[':
950           case ']':
951           case '>':
952           case '<':
953           case 'A':
954           case 'a':
955           case 'B':
956           case 'b':
957           case 'C':
958           case 'c':
959           case 'D':
960           case 'd':
961           case 'e':
962           case 'F':
963           case 'f':
964           case 'G':
965           case 'g':
966           case 'h':
967           case 'I':
968           case 'i':
969           case 'J':
970           case 'j':
971           case 'K':
972           case 'k':
973           case 'L':
974           case 'l':
975           case 'M':
976           case 'm':
977           case 'N':
978           case 'n':
979           case 'O':
980           case 'o':
981           case 'P':
982           case 'p':
983           case 'Q':
984           case 'q':
985           case 'R':
986           case 'r':
987           case 'T':
988           case 't':
989           case 'U':
990           case 'u':
991           case 'V':
992           case 'v':
993           case 'W':
994           case 'w':
995           case 'X':
996           case 'x':
997           case 'Y':
998           case 'y':
999           case 'Z':
1000           case 'z':
1001             // System.out.println(lastSS);
1002             Color nonCanColor = getNotCanonicalColor(lastSS);
1003             drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1004                     x, y, iconOffset, startRes, column, validRes, validEnd);
1005             break;
1006           default:
1007             drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1008                     startRes, column, validRes, validEnd);
1009             break;
1010           }
1011         }
1012
1013         if (row.graph > 0 && row.graphHeight > 0)
1014         {
1015           if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1016           {
1017             if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1018             {
1019               // TODO: JAL-1291 revise rendering model so the graphGroup map is
1020               // computed efficiently for all visible labels
1021               float groupmax = -999999, groupmin = 9999999;
1022               for (int gg = 0; gg < aa.length; gg++)
1023               {
1024                 if (aa[gg].graphGroup != row.graphGroup)
1025                 {
1026                   continue;
1027                 }
1028
1029                 if (aa[gg] != row)
1030                 {
1031                   aa[gg].visible = false;
1032                 }
1033                 if (aa[gg].graphMax > groupmax)
1034                 {
1035                   groupmax = aa[gg].graphMax;
1036                 }
1037                 if (aa[gg].graphMin < groupmin)
1038                 {
1039                   groupmin = aa[gg].graphMin;
1040                 }
1041               }
1042
1043               for (int gg = 0; gg < aa.length; gg++)
1044               {
1045                 if (aa[gg].graphGroup == row.graphGroup)
1046                 {
1047                   drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1048                           endRes, y, groupmin, groupmax, row.graphHeight);
1049                 }
1050               }
1051
1052               graphGroupDrawn.set(row.graphGroup);
1053             }
1054             else
1055             {
1056               drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1057                       row.graphMin, row.graphMax, row.graphHeight);
1058             }
1059           }
1060           else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1061           {
1062             drawBarGraph(g, row, row_annotations, startRes, endRes,
1063                     row.graphMin, row.graphMax, y, renderHistogram,
1064                     renderProfile, normaliseProfile);
1065           }
1066         }
1067       }
1068       else
1069       {
1070         if (clipst && !clipend)
1071         {
1072           clipend = true;
1073         }
1074       } // end if_in_visible_region
1075       if (row.graph > 0 && row.hasText)
1076       {
1077         y += charHeight;
1078       }
1079
1080       if (row.graph == 0)
1081       {
1082         y += aa[i].height;
1083       }
1084     }
1085     if (debugRedraw)
1086     {
1087       if (canClip)
1088       {
1089         if (clipst)
1090         {
1091           System.err.println(
1092                   "Start clip at : " + yfrom + " (index " + f_i + ")");
1093         }
1094         if (clipend)
1095         {
1096           System.err.println(
1097                   "End clip at : " + yto + " (index " + f_to + ")");
1098         }
1099       }
1100       ;
1101       System.err.println("Annotation Rendering time:"
1102               + (System.currentTimeMillis() - stime));
1103     }
1104     ;
1105
1106     return !usedFaded;
1107   }
1108
1109   public static final Color GLYPHLINE_COLOR = Color.gray;
1110
1111   public static final Color SHEET_COLOUR = Color.green;
1112
1113   public static final Color HELIX_COLOUR = Color.red;
1114
1115   public static final Color STEM_COLOUR = Color.blue;
1116
1117   // private Color sdNOTCANONICAL_COLOUR;
1118
1119   void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1120           int y, int iconOffset, int startRes, int column, boolean validRes,
1121           boolean validEnd)
1122   {
1123     g.setColor(GLYPHLINE_COLOR);
1124     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1125   }
1126
1127   void drawSheetAnnot(Graphics g, Annotation[] row,
1128
1129           int lastSSX, int x, int y, int iconOffset, int startRes,
1130           int column, boolean validRes, boolean validEnd)
1131   {
1132     g.setColor(SHEET_COLOUR);
1133
1134     if (!validEnd || !validRes || row == null || row[column] == null
1135             || row[column].secondaryStructure != 'E')
1136     {
1137       g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1138               7);
1139       g.fillPolygon(
1140               new int[]
1141               { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1142               new int[]
1143               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1144               3);
1145     }
1146     else
1147     {
1148       g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1149               7);
1150     }
1151
1152   }
1153
1154   void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1155           int y, int iconOffset, int startRes, int column, boolean validRes,
1156           boolean validEnd)
1157   {
1158     g.setColor(HELIX_COLOUR);
1159
1160     int sCol = (lastSSX / charWidth)
1161             + hiddenColumns.visibleToAbsoluteColumn(startRes);
1162     int x1 = lastSSX;
1163     int x2 = (x * charWidth);
1164
1165     if (USE_FILL_ROUND_RECT)
1166     {
1167       int ofs = charWidth / 2;
1168       // Off by 1 offset when drawing rects and ovals
1169       // to offscreen image on the MAC
1170       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1171       if (sCol == 0 || row[sCol - 1] == null
1172               || row[sCol - 1].secondaryStructure != 'H')
1173       {
1174       }
1175       else
1176       {
1177         // g.setColor(Color.orange);
1178         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1179                 0, 0);
1180       }
1181       if (!validRes || row[column] == null
1182               || row[column].secondaryStructure != 'H')
1183       {
1184
1185       }
1186       else
1187       {
1188         // g.setColor(Color.magenta);
1189         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1190                 x2 - x1 - ofs + 1, 8, 0, 0);
1191
1192       }
1193
1194       return;
1195     }
1196
1197     if (sCol == 0 || row[sCol - 1] == null
1198             || row[sCol - 1].secondaryStructure != 'H')
1199     {
1200       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1201       x1 += charWidth / 2;
1202     }
1203
1204     if (!validRes || row[column] == null
1205             || row[column].secondaryStructure != 'H')
1206     {
1207       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1208               8, 270, 180);
1209       x2 -= charWidth / 2;
1210     }
1211
1212     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1213   }
1214
1215   void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1216           Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1217           float max, int graphHeight)
1218   {
1219     if (sRes > aa_annotations.length)
1220     {
1221       return;
1222     }
1223
1224     int x = 0;
1225
1226     // Adjustment for fastpaint to left
1227     if (eRes < endRes)
1228     {
1229       eRes++;
1230     }
1231
1232     eRes = Math.min(eRes, aa_annotations.length);
1233
1234     if (sRes == 0)
1235     {
1236       x++;
1237     }
1238
1239     int y1 = y, y2 = y;
1240     float range = max - min;
1241
1242     // //Draw origin
1243     if (min < 0)
1244     {
1245       y2 = y - (int) ((0 - min / range) * graphHeight);
1246     }
1247
1248     g.setColor(Color.gray);
1249     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1250
1251     eRes = Math.min(eRes, aa_annotations.length);
1252
1253     int column;
1254     int aaMax = aa_annotations.length - 1;
1255
1256     while (x < eRes - sRes)
1257     {
1258       column = sRes + x;
1259       if (hasHiddenColumns)
1260       {
1261         column = hiddenColumns.visibleToAbsoluteColumn(column);
1262       }
1263
1264       if (column > aaMax)
1265       {
1266         break;
1267       }
1268
1269       if (aa_annotations[column] == null
1270               || aa_annotations[column - 1] == null)
1271       {
1272         x++;
1273         continue;
1274       }
1275
1276       if (aa_annotations[column].colour == null)
1277       {
1278         g.setColor(Color.black);
1279       }
1280       else
1281       {
1282         g.setColor(aa_annotations[column].colour);
1283       }
1284
1285       y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1286               * graphHeight);
1287       y2 = y - (int) (((aa_annotations[column].value - min) / range)
1288               * graphHeight);
1289
1290       g.drawLine(x * charWidth - charWidth / 2, y1,
1291               x * charWidth + charWidth / 2, y2);
1292       x++;
1293     }
1294
1295     if (_aa.threshold != null)
1296     {
1297       g.setColor(_aa.threshold.colour);
1298       Graphics2D g2 = (Graphics2D) g;
1299       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1300               BasicStroke.JOIN_ROUND, 3f, new float[]
1301               { 5f, 3f }, 0f));
1302
1303       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1304       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1305       g2.setStroke(new BasicStroke());
1306     }
1307   }
1308
1309   @SuppressWarnings("unused")
1310   void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1311           Annotation[] aa_annotations, int sRes, int eRes, float min,
1312           float max, int y, boolean renderHistogram, boolean renderProfile,
1313           boolean normaliseProfile)
1314   {
1315     if (sRes > aa_annotations.length)
1316     {
1317       return;
1318     }
1319     Font ofont = g.getFont();
1320     eRes = Math.min(eRes, aa_annotations.length);
1321
1322     int x = 0, y1 = y, y2 = y;
1323
1324     float range = max - min;
1325
1326     if (min < 0)
1327     {
1328       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1329     }
1330
1331     g.setColor(Color.gray);
1332
1333     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1334
1335     int column;
1336     int aaMax = aa_annotations.length - 1;
1337     while (x < eRes - sRes)
1338     {
1339       column = sRes + x;
1340       if (hasHiddenColumns)
1341       {
1342         column = hiddenColumns.visibleToAbsoluteColumn(column);
1343       }
1344
1345       if (column > aaMax)
1346       {
1347         break;
1348       }
1349
1350       if (aa_annotations[column] == null)
1351       {
1352         x++;
1353         continue;
1354       }
1355       if (aa_annotations[column].colour == null)
1356       {
1357         g.setColor(Color.black);
1358       }
1359       else
1360       {
1361         g.setColor(aa_annotations[column].colour);
1362       }
1363
1364       y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1365               * _aa.graphHeight);
1366
1367       if (renderHistogram)
1368       {
1369         if (y1 - y2 > 0)
1370         {
1371           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1372         }
1373         else
1374         {
1375           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1376         }
1377       }
1378       // draw profile if available
1379       if (renderProfile)
1380       {
1381
1382         /*
1383          * {profile type, #values, total count, char1, pct1, char2, pct2...}
1384          */
1385         int profl[] = getProfileFor(_aa, column);
1386
1387         // just try to draw the logo if profl is not null
1388         if (profl != null && profl[2] != 0)
1389         {
1390           boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1391           boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1392           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1393           final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1394                   : (y2 - y1);
1395
1396           /**
1397            * Render a single base for a sequence profile, a base pair for
1398            * structure profile, and a triplet for a cdna profile
1399            */
1400           char[] dc = new char[isStructureProfile ? 2
1401                   : (isCdnaProfile ? 3 : 1)];
1402
1403           // lm is not necessary - we can just use fm - could be off by no more
1404           // than 0.5 px
1405           // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1406           // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1407           // + " " + (dec - lm.getDescent()));
1408
1409           double asc = fm.getAscent();
1410           double dec = fm.getDescent();
1411           double fht = fm.getHeight();
1412
1413           double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1414           // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1415
1416           /*
1417            * Traverse the character(s)/percentage data in the array
1418            */
1419
1420           float ht2 = ht;
1421
1422           // profl[1] is the number of values in the profile
1423           for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1424           {
1425
1426             String s;
1427             if (isStructureProfile)
1428             {
1429               // todo can we encode a structure pair as an int, like codons?
1430               dc[0] = (char) profl[c++];
1431               dc[1] = (char) profl[c++];
1432               s = new String(dc);
1433             }
1434             else if (isCdnaProfile)
1435             {
1436               CodingUtils.decodeCodon2(profl[c++], dc);
1437               s = new String(dc);
1438             }
1439             else
1440             {
1441               dc[0] = (char) profl[c++];
1442               s = new String(dc);
1443             }
1444             // next profl[] position is profile % for the character(s)
1445
1446             int percent = profl[c++];
1447             if (percent == 0)
1448             {
1449               // failsafe in case a count rounds down to 0%
1450               continue;
1451             }
1452             double newHeight = normaliseFactor * scale * percent;
1453
1454             /*
1455              * Set character colour as per alignment colour scheme; use the
1456              * codon translation if a cDNA profile
1457              */
1458             Color colour = null;
1459             if (isCdnaProfile)
1460             {
1461               final String codonTranslation = ResidueProperties
1462                       .codonTranslate(s);
1463               colour = profcolour.findColour(codonTranslation.charAt(0),
1464                       column, null);
1465             }
1466             else
1467             {
1468               colour = profcolour.findColour(dc[0], column, null);
1469             }
1470             g.setColor(colour == Color.white ? Color.lightGray : colour);
1471
1472             // Debug - render boxes around characters
1473             // g.setColor(Color.red);
1474             // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1475             // (int)(scl));
1476             // g.setColor(profcolour.findColour(dc[0]).darker());
1477
1478             double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1479             double sy = newHeight / asc;
1480             double newAsc = asc * sy;
1481             double newDec = dec * sy;
1482             // it is not necessary to recalculate lm for the new font.
1483             // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1484             // by definition. Was:
1485             // int hght = (int) (ht + (newAsc - newDec);
1486             // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1487
1488             if (Platform.isJS())
1489             {
1490               /*
1491                * SwingJS does not implement font.deriveFont()
1492                * so use a scaling transform to draw instead,
1493                * this is off by a very small amount
1494                */
1495               final int hght = (int) (ht2 + (newAsc - newDec));
1496               Graphics2D gg = (Graphics2D) g;
1497               int xShift = (int) Math.round(x * charWidth / sx);
1498               int yShift = (int) Math.round(hght / sy);
1499               gg.transform(AffineTransform.getScaleInstance(sx, sy));
1500               gg.drawString(s, xShift, yShift);
1501               gg.transform(
1502                       AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1503               ht2 += newHeight;
1504             }
1505             else
1506             /**
1507              * Java only
1508              * 
1509              * @j2sIgnore
1510              */
1511             {
1512               // Java ('normal') method is to scale the font to fit
1513
1514               final int hght = (int) (ht + (newAsc - newDec));
1515               Font font = ofont
1516                       .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1517               g.setFont(font);
1518               g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1519               g.setFont(ofont);
1520
1521               ht += newHeight;
1522             }
1523           }
1524         }
1525       }
1526       x++;
1527     }
1528     if (_aa.threshold != null)
1529     {
1530       g.setColor(_aa.threshold.colour);
1531       Graphics2D g2 = (Graphics2D) g;
1532       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1533               BasicStroke.JOIN_ROUND, 3f, new float[]
1534               { 5f, 3f }, 0f));
1535
1536       y2 = (int) (y
1537               - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1538       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1539       g2.setStroke(new BasicStroke());
1540     }
1541   }
1542
1543   // used by overview window
1544   public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1545           Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1546   {
1547     eRes = Math.min(eRes, aa_annotations.length);
1548     g.setColor(Color.white);
1549     g.fillRect(0, 0, width, y);
1550     g.setColor(new Color(0, 0, 180));
1551
1552     int x = 0, height;
1553
1554     for (int j = sRes; j < eRes; j++)
1555     {
1556       if (aa_annotations[j] != null)
1557       {
1558         if (aa_annotations[j].colour == null)
1559         {
1560           g.setColor(Color.black);
1561         }
1562         else
1563         {
1564           g.setColor(aa_annotations[j].colour);
1565         }
1566
1567         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1568         if (height > y)
1569         {
1570           height = y;
1571         }
1572
1573         g.fillRect(x, y - height, charWidth, height);
1574       }
1575       x += charWidth;
1576     }
1577   }
1578
1579   Color getNotCanonicalColor(char lastss)
1580   {
1581     switch (lastss)
1582     {
1583     case '{':
1584     case '}':
1585       return new Color(255, 125, 5);
1586
1587     case '[':
1588     case ']':
1589       return new Color(245, 115, 10);
1590
1591     case '>':
1592     case '<':
1593       return new Color(235, 135, 15);
1594
1595     case 'A':
1596     case 'a':
1597       return new Color(225, 105, 20);
1598
1599     case 'B':
1600     case 'b':
1601       return new Color(215, 145, 30);
1602
1603     case 'C':
1604     case 'c':
1605       return new Color(205, 95, 35);
1606
1607     case 'D':
1608     case 'd':
1609       return new Color(195, 155, 45);
1610
1611     case 'E':
1612     case 'e':
1613       return new Color(185, 85, 55);
1614
1615     case 'F':
1616     case 'f':
1617       return new Color(175, 165, 65);
1618
1619     case 'G':
1620     case 'g':
1621       return new Color(170, 75, 75);
1622
1623     case 'H':
1624     case 'h':
1625       return new Color(160, 175, 85);
1626
1627     case 'I':
1628     case 'i':
1629       return new Color(150, 65, 95);
1630
1631     case 'J':
1632     case 'j':
1633       return new Color(140, 185, 105);
1634
1635     case 'K':
1636     case 'k':
1637       return new Color(130, 55, 110);
1638
1639     case 'L':
1640     case 'l':
1641       return new Color(120, 195, 120);
1642
1643     case 'M':
1644     case 'm':
1645       return new Color(110, 45, 130);
1646
1647     case 'N':
1648     case 'n':
1649       return new Color(100, 205, 140);
1650
1651     case 'O':
1652     case 'o':
1653       return new Color(90, 35, 150);
1654
1655     case 'P':
1656     case 'p':
1657       return new Color(85, 215, 160);
1658
1659     case 'Q':
1660     case 'q':
1661       return new Color(75, 25, 170);
1662
1663     case 'R':
1664     case 'r':
1665       return new Color(65, 225, 180);
1666
1667     case 'S':
1668     case 's':
1669       return new Color(55, 15, 185);
1670
1671     case 'T':
1672     case 't':
1673       return new Color(45, 235, 195);
1674
1675     case 'U':
1676     case 'u':
1677       return new Color(35, 5, 205);
1678
1679     case 'V':
1680     case 'v':
1681       return new Color(25, 245, 215);
1682
1683     case 'W':
1684     case 'w':
1685       return new Color(15, 0, 225);
1686
1687     case 'X':
1688     case 'x':
1689       return new Color(10, 255, 235);
1690
1691     case 'Y':
1692     case 'y':
1693       return new Color(5, 150, 245);
1694
1695     case 'Z':
1696     case 'z':
1697       return new Color(0, 80, 255);
1698
1699     default:
1700       System.out.println("This is not a interaction : " + lastss);
1701       return null;
1702
1703     }
1704   }
1705 }