LOGGER.error("layouting the scalebar failed (unit: " + scaleUnit.toString()
+ ", scale: " + scale.getDenominator() + ")");
return;
}
final ScalebarAttributeValues scalebarParams = settings.getParams();
final DistanceUnit intervalUnit = bestUnit(scaleUnit, intervalLengthInWorldUnits, scalebarParams.lockUnits);
final float intervalLengthInPixels = (float) scaleUnit.convertTo(
intervalLengthInWorldUnits / scale.getDenominator(), DistanceUnit.PX);
//compute the label positions
final List<Label> labels = new ArrayList<Label>(scalebarParams.intervals + 1);
final float leftLabelMargin;
final float rightLabelMargin;
final float topLabelMargin;
final float bottomLabelMargin;
final Font font = new Font(scalebarParams.font, Font.PLAIN, getFontSize(settings));
final FontRenderContext frc = new FontRenderContext(null, true, true);
if (scalebarParams.intervals > 1 || scalebarParams.subIntervals) {
//the label will be centered under each tick mark
for (int i = 0; i <= scalebarParams.intervals; i++) {
String labelText = createLabelText(scaleUnit, intervalLengthInWorldUnits * i, intervalUnit);
if (i == scalebarParams.intervals) {
// only show unit for the last label
labelText += intervalUnit;
}
TextLayout labelLayout = new TextLayout(labelText, font, frc);
labels.add(new Label(intervalLengthInPixels * i, labelLayout, scalebarParams.getOrientation()));
}
leftLabelMargin = labels.get(0).getWidth() / 2.0f;
rightLabelMargin = labels.get(labels.size() - 1).getWidth() / 2.0f;
topLabelMargin = labels.get(0).getHeight() / 2.0f;
bottomLabelMargin = labels.get(labels.size() - 1).getHeight() / 2.0f;
} else {
//if there is only one interval, place the label centered between the two tick marks
String labelText = createLabelText(scaleUnit, intervalLengthInWorldUnits, intervalUnit) + intervalUnit;
TextLayout labelLayout = new TextLayout(labelText, font, frc);
final Label label = new Label(intervalLengthInPixels / 2.0f,
labelLayout, scalebarParams.getOrientation());
labels.add(label);
rightLabelMargin = Math.max(0.0f, label.getWidth() - intervalLengthInPixels) / 2.0f;
leftLabelMargin = rightLabelMargin;
topLabelMargin = Math.max(0.0f, label.getHeight() - intervalLengthInPixels) / 2.0f;
bottomLabelMargin = topLabelMargin;