JAL-3055 refinements to alternatives to deriveFont
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 30 Aug 2018 15:28:01 +0000 (16:28 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 30 Aug 2018 15:28:01 +0000 (16:28 +0100)
src/jalview/renderer/AnnotationRenderer.java

index 0aeb45a..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
@@ -1482,17 +1493,20 @@ public class AnnotationRenderer
                * this is off by a very small amount
                */
               final int hght = (int) (ht2 + (newAsc - newDec));
-              Graphics2D gg = (Graphics2D) g.create();
-              gg.setFont(ofont);
-              int xShift = (int) (x * charWidth / sx);
-              int yShift = (int) (hght / sy);
+              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, xShift, yShift);
-              gg.dispose();
+              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));
@@ -1501,7 +1515,6 @@ public class AnnotationRenderer
               ht += newHeight;
             }
           }
-          g.setFont(ofont);
         }
       }
       x++;