JAL-3055 refinements to alternatives to deriveFont
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
index eb998bb..102b3ee 100644 (file)
@@ -651,7 +651,6 @@ public class AnnotationRenderer
                     && (displayChar.length() > 0))
             {
               Graphics2D gg = ((Graphics2D) g);
-              AffineTransform oldTransform = gg.getTransform();
               float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
                       displayChar.length());
 
@@ -687,6 +686,10 @@ public class AnnotationRenderer
                */
               final int xPos = (x * charWidth) + charOffset;
               final int yPos = y + iconOffset;
+
+              /*
+               * translate to drawing position _before_ applying any scaling
+               */
               gg.translate(xPos, yPos);
               if (scaledToFit)
               {
@@ -709,8 +712,16 @@ public class AnnotationRenderer
               {
                 gg.drawString(displayChar, 0, 0);
               }
+              if (scaledToFit)
+              {
+                /*
+                 * undo scaling before translating back 
+                 * (restoring saved transform does NOT work in JS PDFGraphics!)
+                 */
+                gg.transform(AffineTransform
+                        .getScaleInstance(1D / fmScaling, 1.0));
+              }
               gg.translate(-xPos, -yPos);
-              gg.setTransform(oldTransform);
             }
           }
           if (row.hasIcons)
@@ -1386,7 +1397,7 @@ public class AnnotationRenderer
           boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
           boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
-          double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
+          final double normaliseFactor = normaliseProfile ? _aa.graphHeight : (y2 - y1);
 
           /**
            * Render a single base for a sequence profile, a base pair for
@@ -1438,7 +1449,7 @@ public class AnnotationRenderer
             }
             // next profl[] position is profile % for the character(s)
             
-            double newHeight = htn * scale * profl[c++];
+            double newHeight = normaliseFactor * scale * profl[c++];
 
             /*
              * Set character colour as per alignment colour scheme; use the
@@ -1468,12 +1479,12 @@ public class AnnotationRenderer
             double sy = newHeight / asc;
             double newAsc = asc * sy; 
             double newDec = dec * sy;
-            // it is not necessary to recalculated lm for the new font.
+            // it is not necessary to recalculate lm for the new font.
             // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
             // by definition. Was:
-            // int hght = (int) (ht + (newAsc - newDec - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
+            // int hght = (int) (ht + (newAsc - newDec);
+            // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
 
-            final int hght = (int) (ht + (newAsc - newDec)); 
             if (Jalview.isJS())
             {
               /*
@@ -1481,17 +1492,22 @@ public class AnnotationRenderer
                * so use a scaling transform to draw instead,
                * this is off by a very small amount
                */
-              Graphics2D gg = (Graphics2D) g.create();
-              gg.setFont(ofont);
-              gg.translate(x*charWidth, hght);
+              final int hght = (int) (ht2 + (newAsc - newDec));
+              Graphics2D gg = (Graphics2D) g;
+              int xShift = (int) Math.round(x * charWidth / sx);
+              int yShift = (int) Math.round(hght / sy);
               gg.transform(AffineTransform.getScaleInstance(sx, sy));
-              gg.drawString(s, 0, 0); 
-              gg.translate(-x*charWidth, -hght);
-              gg.dispose();
+              gg.drawString(s, xShift, yShift);
+              gg.transform(
+                      AffineTransform.getScaleInstance(1D / sx, 1D / sy));
               ht2 += newHeight;
             }
             else
             {
+              /*
+               * Java ('normal') method is to scale the font to fit
+               */
+              final int hght = (int) (ht + (newAsc - newDec));
               Font font = ofont
                       .deriveFont(AffineTransform.getScaleInstance(sx, sy));
               g.setFont(font);
@@ -1499,7 +1515,6 @@ public class AnnotationRenderer
               ht += newHeight;
             }
           }
-          g.setFont(ofont);
         }
       }
       x++;