factory = new GeometryFactory();
}
public void writeObject(Object object, GraphicsDocument document, boolean asChild) throws RenderException {
InternalTileImpl tile = (InternalTileImpl) object;
FeatureStyleInfo bgStyle = labelStyle.getBackgroundStyle();
document.writeElement("vml:group", asChild);
document.writeId("labels." + tile.getCode().toString());
document.writeAttribute("coordsize", coordWidth + "," + coordHeight);
document.writeAttribute("style", "WIDTH: " + coordWidth + "; HEIGHT: " + coordHeight);
// the shapetype
document.writeElement("vml:shapetype", true);
document.writeAttribute("id", "labels." + tile.getCode().toString() + ".style");
document.writeAttribute("style", "WIDTH: 100%; HEIGHT: 100%");
document.writeAttribute("style", "VISIBILITY: hidden");
document.writeAttribute("filled", "f");
document.writeAttribute("stroked", "f");
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");