use of org.w3c.dom.svg.SVGTextContentElement in project megameklab by MegaMek.
the class PrintEntity method writeTextFields.
protected void writeTextFields() {
setTextField("title", getRecordSheetTitle().toUpperCase());
setTextField("type", getEntity().getShortNameRaw());
setTextField("mpWalk", formatWalk());
setTextField("mpRun", formatRun());
setTextField("mpJump", formatJump());
setTextField("tonnage", Integer.toString((int) getEntity().getWeight()));
setTextField("techBase", formatTechBase());
setTextField("rulesLevel", formatRulesLevel());
setTextField("era", formatEra(getEntity().getYear()));
setTextField("cost", formatCost());
setTextField("bv", Integer.toString(getEntity().calculateBattleValue()));
UnitRole role = UnitRoleHandler.getRoleFor(getEntity());
if (role == UnitRole.UNDETERMINED) {
hideElement("lblRole", true);
hideElement("role", true);
} else {
setTextField("role", role.toString());
}
// This will require building the graphics tree so we measure the elements.
if (getEntity().getCrew().getCrewType() != CrewType.SINGLE) {
build();
}
for (int i = 0; i < getEntity().getCrew().getSlotCount(); i++) {
// If we have multiple named crew for the unit, change the "Name:" label to
// the label of the slot. This will usually require adjusting the position of the
// name or the length of the blank.
double nameOffset = 0;
if (getEntity().getCrew().getSlotCount() > 1) {
Element element = getSVGDocument().getElementById("crewName" + i);
if (null != element) {
float oldWidth = ((SVGTextContentElement) element).getComputedTextLength();
element.setTextContent(getEntity().getCrew().getCrewType().getRoleName(i) + ":");
nameOffset = SVGLocatableSupport.getBBox(element).getWidth() - oldWidth;
}
}
if (!getEntity().getCrew().getName().equalsIgnoreCase("unnamed")) {
Element element = getSVGDocument().getElementById("blanksCrew" + i);
if (null != element) {
hideElement(element);
}
if (nameOffset != 0) {
element = getSVGDocument().getElementById("pilotName" + i);
if (null != element) {
double offset = nameOffset;
String prev = element.getAttribute(SVGConstants.SVG_X_ATTRIBUTE);
if (null != prev) {
offset += Double.parseDouble(prev);
} else {
offset += ((SVGTextContentElement) element).getStartPositionOfChar(0).getX();
}
element.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, Double.toString(offset));
}
}
setTextField("pilotName" + i, getEntity().getCrew().getName(i), true);
setTextField("gunnerySkill" + i, Integer.toString(getEntity().getCrew().getGunnery(i)), true);
setTextField("pilotingSkill" + i, Integer.toString(getEntity().getCrew().getPiloting(i)), true);
StringJoiner spaList = new StringJoiner(", ");
PilotOptions spas = getEntity().getCrew().getOptions();
for (Enumeration<IOptionGroup> optionGroups = spas.getGroups(); optionGroups.hasMoreElements(); ) {
IOptionGroup optiongroup = optionGroups.nextElement();
if (spas.count(optiongroup.getKey()) > 0) {
for (Enumeration<IOption> options = optiongroup.getOptions(); options.hasMoreElements(); ) {
IOption option = options.nextElement();
if (option != null && option.booleanValue()) {
spaList.add(option.getDisplayableNameWithValue().replaceAll(" \\(.*?\\)", ""));
}
}
}
}
if (spaList.length() > 0) {
Element rect = getSVGDocument().getElementById("spas" + getEntity().getCrew().getSlotCount());
if ((null != rect) && (rect instanceof SVGRectElement)) {
Rectangle2D bbox = getRectBBox((SVGRectElement) rect);
Element canvas = (Element) ((Node) rect).getParentNode();
String spaText = "Abilities: " + spaList.toString();
float fontSize = FONT_SIZE_MEDIUM;
if (getTextLength(spaText, fontSize) > bbox.getWidth()) {
fontSize = (float) bbox.getHeight() / 2.4f;
}
double lineHeight = fontSize * 1.2;
addMultilineTextElement(canvas, bbox.getX(), bbox.getY() + lineHeight, bbox.getWidth(), lineHeight, spaText, fontSize, "start", "normal", "black", ' ');
}
}
} else {
setTextField("pilotName" + i, null);
setTextField("gunnerySkill" + i, null);
setTextField("pilotingSkill" + i, null);
if (nameOffset != 0) {
Element element = getSVGDocument().getElementById("blankCrewName" + i);
if (null != element) {
float w = ((SVGGraphicsElement) element).getBBox().getWidth();
element.setAttributeNS(null, SVGConstants.SVG_D_ATTRIBUTE, String.format("M %f,0 %f,0", nameOffset, w - nameOffset));
}
}
}
}
}
use of org.w3c.dom.svg.SVGTextContentElement 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