X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Futils%2FGridUtil.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Futils%2FGridUtil.java;h=5114f1a660656771595a6a5375e1465d96c82e78;hp=8ee6c6a71e9a15a781498c0306ecc48f51e4d951;hb=78d831a19c254d829e45d04c5ec6a3057680b7d7;hpb=3a1169e5b601f99d66cadaf398329fde9cc8e14a diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java index 8ee6c6a71..5114f1a66 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java @@ -213,8 +213,7 @@ public class GridUtil { double x = startY / grid.pixelsPerSegment; return (int) Math.ceil( x ); } - - + /** * Estimate label width, by sampling values from xMin to xMax * using given font and format. @@ -226,22 +225,36 @@ public class GridUtil { */ public static double calcLabelWidth(double xMin, double xMax, Format format, GridSpacing grid) { - double width = 0.0; - double canvasDiff = GridUtils.distanceToNextGridCoord(xMin, grid.segment); - int c = 0; - for (double x=xMin + canvasDiff; x -0.000000000001); - if (nearZero) x = 0.0; - String label = format.format(x); - if (label==null || label.equals("")) continue; - GlyphVector glyphVector; - glyphVector = RULER_FONT.createGlyphVector(frc, label); + double width = 0.0; + double canvasDiff = GridUtils.distanceToNextGridCoord(xMin, grid.segment); + int c = 0; + double canvasStart = xMin + canvasDiff; + for (double x=canvasStart; x -0.000000000001); +// if (nearZero) x = 0.0; + + String label = format.format(x); + if (label==null || label.equals("")) continue; + GlyphVector glyphVector; + glyphVector = RULER_FONT.createGlyphVector(frc, label); double labelWidth = glyphVector.getVisualBounds().getWidth(); + //System.out.format("calcLabelWidth[%d](%s, %f, %f)%n", c, label, labelWidth, width); width = Math.max( width, labelWidth ); - if (c++ > 100) break; - } - return width * 1.05; + if (c++ > 100) break; + + // It is floating-point-wise more precise to calculate x like this than + // accumulating x += grid.segment. + // Also, this is how paintVerticalRuler does it and we want to match that + // in order to calculate the label width from the exact same values that + // paintVerticalRuler will draw. + x = canvasStart + grid.segment * c; + } + return width * 1.05; } - - + }