double minPixels)
{
if (controlWidth==0) controlWidth = 1;
- if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize);
+ // This prevents clients from getting the proper GridSpacing information.
+ //if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize);
double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
return GridUtils.snapToGrid(pos, segment);
}
+ @Override
+ public String toString() {
+ return String.format("%s [minPixels=%.15f, segment=%.15f, segmentExp=%.15f, pixelsPerUnit=%.15f, unitsPerPixel=%.15f, pixelsPerSegment=%.15f]",
+ getClass().getName(),
+ minPixels,
+ segment,
+ segmentExp,
+ pixelsPerUnit,
+ unitsPerPixel,
+ pixelsPerSegment);
+ }
+
}
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.
*/
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<xMax; x+=grid.segment) {
- boolean nearZero = (x < 0.000000000001) && (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<xMax; ) {
+ // This nearZero-logic causes a discrepancy between how the labels
+ // are rendered in paintVerticalRuler() and calculated in this method.
+ // Removing it doesn't seem to be causing any immediate adverse effects.
+ // However, this was introduced ages ago to "fix a glitch" so some kind
+ // of regressions might happen if rmeoved.
+// boolean nearZero = (x < 0.000000000001) && (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;
}
-
-
+
}