From: gmungoc Date: Tue, 28 Aug 2018 11:32:01 +0000 (+0100) Subject: JAL-3055 corrections to use of AffineTransform to scale characters X-Git-Tag: Release_2_11_4_0~45^2~18^2~454 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a81e4ad01b8eaecac4445f85b48cd455aa089016;p=jalview.git JAL-3055 corrections to use of AffineTransform to scale characters --- diff --git a/src/jalview/renderer/AnnotationRenderer.java b/src/jalview/renderer/AnnotationRenderer.java index 8480a34..eb998bb 100644 --- a/src/jalview/renderer/AnnotationRenderer.java +++ b/src/jalview/renderer/AnnotationRenderer.java @@ -476,9 +476,6 @@ public class AnnotationRenderer BitSet graphGroupDrawn = new BitSet(); int charOffset = 0; // offset for a label - float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a - // column. - Font ofont = g.getFont(); // \u03B2 \u03B1 // debug ints int yfrom = 0, f_i = 0, yto = 0, f_to = 0; @@ -654,43 +651,25 @@ public class AnnotationRenderer && (displayChar.length() > 0)) { Graphics2D gg = ((Graphics2D) g); - AffineTransform t = gg.getTransform(); - fmWidth = fm.charsWidth(displayChar.toCharArray(), 0, + AffineTransform oldTransform = gg.getTransform(); + float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0, displayChar.length()); - if (/* centreColLabels || */scaleColLabel) + + /* + * shrink label width to fit in column, if that is + * both configured and necessary + */ + boolean scaledToFit = false; + float fmScaling = 1f; + if (scaleColLabel && fmWidth > charWidth) { - // fmWidth = fm.charsWidth(displayChar.toCharArray(), 0, - // displayChar.length()); - // - // if (scaleColLabel) - // { - // justify the label and scale to fit in column - if (fmWidth > charWidth) - { - // scale only if the current font isn't already small enough - fmScaling = charWidth; - fmScaling /= fmWidth; - if (Jalview.isJS()) - { - gg.setFont(ofont); - gg.transform(AffineTransform.getScaleInstance(fmScaling, - 1.0)); - } - else - { - g.setFont(ofont.deriveFont(AffineTransform - .getScaleInstance(fmScaling, 1.0))); - } - // and update the label's width to reflect the scaling. - fmWidth = charWidth; - } - // } + scaledToFit = true; + fmScaling = charWidth; + fmScaling /= fmWidth; + // and update the label's width to reflect the scaling. + fmWidth = charWidth; } - // TODO is it ok to use width of / show all characters here? - // else - // { - // fmWidth = fm.charWidth(displayChar.charAt(0)); - // } + charOffset = (int) ((charWidth - fmWidth) / 2f); if (row_annotations[column].colour == null) @@ -702,10 +681,25 @@ public class AnnotationRenderer gg.setColor(row_annotations[column].colour); } + /* + * draw the label, unless it is the same secondary structure + * symbol (excluding RNA Helix) as the previous column + */ + final int xPos = (x * charWidth) + charOffset; + final int yPos = y + iconOffset; + gg.translate(xPos, yPos); + if (scaledToFit) + { + /* + * use a scaling transform to make the label narrower + * (JalviewJS doesn't have Font.deriveFont(AffineTransform)) + */ + gg.transform( + AffineTransform.getScaleInstance(fmScaling, 1.0)); + } if (column == 0 || row.graph > 0) { - gg.drawString(displayChar, (x * charWidth) + charOffset, - y + iconOffset); + gg.drawString(displayChar, 0, 0); } else if (row_annotations[column - 1] == null || (labelAllCols || !displayChar.equals( @@ -713,11 +707,10 @@ public class AnnotationRenderer || (displayChar.length() < 2 && row_annotations[column].secondaryStructure == ' '))) { - gg.drawString(displayChar, x * charWidth + charOffset, - y + iconOffset); + gg.drawString(displayChar, 0, 0); } - g.setFont(ofont); - gg.setTransform(t); + gg.translate(-xPos, -yPos); + gg.setTransform(oldTransform); } } if (row.hasIcons) @@ -1480,28 +1473,31 @@ public class AnnotationRenderer // by definition. Was: // int hght = (int) (ht + (newAsc - newDec - lm.getBaselineOffsets()[lm.getBaselineIndex()])); - // original: - - if (/** @j2sNative false && */ true) { - int hght = (int) (ht + (newAsc - newDec)); // Q: why " - newDec " ? (0,0) is on the font baseline, I think - Font font = ofont.deriveFont(AffineTransform.getScaleInstance(sx, sy)); - g.setFont(font); - g.drawChars(dc, 0, dc.length, x * charWidth, hght); - ht += newHeight; - } else { - // SwingJS does not implement font.deriveFont() - // this is off by a very small amount. - int hght2 = (int) (ht2 + newAsc); + final int hght = (int) (ht + (newAsc - newDec)); + if (Jalview.isJS()) + { + /* + * SwingJS does not implement font.deriveFont() + * 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); gg.transform(AffineTransform.getScaleInstance(sx, sy)); - //System.out.println("sx " + sx + " sy " + sy + " " + hght + " " + lm.getDescent() + " " + dec + " " + newDec + " " + lm.getAscent() + " " + asc + " " + newAsc); - gg.drawString(s, (int) (x * charWidth / sx), - (int) (hght2 / sy)); + gg.drawString(s, 0, 0); + gg.translate(-x*charWidth, -hght); gg.dispose(); ht2 += newHeight; } - + else + { + Font font = ofont + .deriveFont(AffineTransform.getScaleInstance(sx, sy)); + g.setFont(font); + g.drawChars(dc, 0, dc.length, x * charWidth, hght); + ht += newHeight; + } } g.setFont(ofont); }