use of org.w3c.dom.svg.SVGTSpanElement in project scout.rt by eclipse.
the class SVGUtility method setTextContent.
/**
* Set the text content of a text element, in case it contains newlines then add tspan elements. Requires the GVT tree
* to be attached to the svg document.
*
* @param textElement
* @param value
* @param rowGap
* in px
*/
@SuppressWarnings("squid:S2583")
public static void setTextContent(Element e, String value, final Float rowGap) {
if (e == null) {
return;
}
SVGTextContentElement textElement = (SVGTextContentElement) e;
// remove inner tspan elements
NodeList nl = textElement.getElementsByTagName(SVGConstants.SVG_TSPAN_TAG);
for (int i = 0; i < nl.getLength(); i++) {
nl.item(i).getParentNode().removeChild(nl.item(i));
}
if (value == null || value.length() == 0) {
setTextContent(textElement, null);
return;
}
if (!value.contains("\n")) {
setTextContent(textElement, value);
return;
}
// get font height
float fontHeight = 0f;
Node tmpNode = textElement;
while (!isSet(fontHeight) && tmpNode != null) {
if (tmpNode instanceof SVGStylable) {
// get font height
String fontSizeText = ((SVGStylable) tmpNode).getStyle().getPropertyValue(SVGConstants.CSS_FONT_SIZE_PROPERTY);
if (fontSizeText != null) {
fontHeight = convertToPx(fontSizeText);
break;
}
}
// next
tmpNode = tmpNode.getParentNode();
}
if (!isSet(fontHeight)) {
fontHeight = DEFAULT_FONT_HEIGHT;
}
Float rGap = rowGap == null ? Float.valueOf(1f) : rowGap;
float rowHeight = fontHeight + rGap;
// create tspan lines
float y = 0;
setTextContent(textElement, null);
for (String line : value.split("[\n\r]")) {
SVGTSpanElement tspanElem = (SVGTSpanElement) textElement.getOwnerDocument().createElementNS(SVG_NS, SVGConstants.SVG_TSPAN_TAG);
textElement.appendChild(tspanElem);
tspanElem.setTextContent(line);
tspanElem.setAttribute("x", "0");
tspanElem.setAttribute("y", String.valueOf(y));
y += rowHeight;
}
}
Aggregations