document.writeAttribute("coordsize", coordWidth + "," + coordHeight);
// up to shapetype
document.closeElement();
for (InternalFeature f : tile.getFeatures()) {
InternalFeatureImpl feature = (InternalFeatureImpl) f;
Coordinate pos = geoService.calcDefaultLabelPosition(feature);
if (pos == null) {
continue;
}
com.vividsolutions.jts.geom.Point p = factory.createPoint(pos);
com.vividsolutions.jts.geom.Point labelPos;
try {
labelPos = (com.vividsolutions.jts.geom.Point) transformer.transform(p);
String labelString = feature.getLabel();
// If the attribute has no value, continue with the next:
if (labelString == null || labelString.length() == 0) {
document.closeElement();
continue;
}
// Calculate label width, left and top:
Rectangle2D textBox = textService.getStringBounds(labelString, labelStyle.getFontStyle());
int boxWidth = (int) textBox.getWidth() + 8; // TODO: check why not wide enough !!!
int boxHeight = (int) textBox.getHeight() + 2;
int left = ((int) labelPos.getX()) - boxWidth / 2;
int top = ((int) labelPos.getY()) - boxHeight / 2;
// Group for an individual label (vml:group):
document.writeElement("vml:group", true);
document.writeAttribute("style", "LEFT: " + left + "px; TOP: " + top + "px; WIDTH: " + boxWidth
+ "px; HEIGHT: " + boxHeight + "px; position:absolute;");
document.writeAttribute("coordsize", boxWidth + " " + boxHeight);
// First we draw the rectangle:
document.writeElement("vml:rect", true);
document.writeAttribute("id", feature.getId() + ".label");
document.writeAttribute("style", "WIDTH: " + boxWidth + "px; HEIGHT: " + boxHeight + "px;");
document.writeAttribute("fillcolor", bgStyle.getFillColor());
document.writeAttribute("strokecolor", bgStyle.getStrokeColor());
document.writeAttribute("strokeweight", bgStyle.getStrokeWidth());
// Rect-fill element:
document.writeElement("vml:fill", true);
document.writeAttribute("opacity", Float.toString(bgStyle.getFillOpacity()));
document.closeElement();
// Rect-stroke element:
document.writeElement("vml:stroke", true);
document.writeAttribute("opacity", Float.toString(bgStyle.getStrokeOpacity()));
document.closeElement();
// Then the label-text:
document.writeElement("vml:textbox", true);
document.writeAttribute("id", feature.getId() + ".text");
document.writeAttribute("v-text-anchor", "middle");
document.writeAttribute("inset", "0px, 0px, 0px, 0px");
document.writeAttribute("style", getCssStyle(labelStyle.getFontStyle())
+ "; text-align:center; WIDTH: " + boxWidth + "px; HEIGHT: " + boxHeight + "px;");
document.writeAttribute("fillcolor", labelStyle.getFontStyle().getColor());
if (labelStyle.getFontStyle().getOpacity() > 0) {
document.writeElement("vml:fill", true);
document.writeAttribute("opacity", Float.toString(labelStyle.getFontStyle().getOpacity()));
document.closeElement();
}
// document.writeTextNode(labelString.replaceAll(" ", " "));
document.writeTextNode(labelString);
document.closeElement();
// Close the vml:rect
document.closeElement();
// Close the individual label group (vml:group):
document.closeElement();
} catch (TransformException e) {
log.warn("Label for " + feature.getId() + " could not be written!");
}
}
document.closeElement();
}