Skip to content

Commit

Permalink
[781] Adapt svg export to multiline label
Browse files Browse the repository at this point in the history
Bug: #781
Signed-off-by: Laurent Fasani <[email protected]>
  • Loading branch information
lfasani committed Mar 8, 2022
1 parent 49212bc commit b0a7fcd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public StringBuilder exportLabel(Label label) {
labelExport.append(this.exportImageElement(style.getIconURL(), -20, -12, Optional.empty()));
}

labelExport.append(this.exportTextElement(label.getText(), style));
labelExport.append(this.exportTextElement(label.getText(), label.getType(), style));

return labelExport.append("</g>"); //$NON-NLS-1$
}
Expand Down Expand Up @@ -109,16 +109,32 @@ private StringBuilder addSizeParam(Size size) {
return sizeParam.append("height=\"" + size.getHeight() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
}

private StringBuilder exportTextElement(String text, LabelStyle labelStyle) {
private StringBuilder exportTextElement(String text, String type, LabelStyle labelStyle) {
StringBuilder textExport = new StringBuilder();

textExport.append("<text "); //$NON-NLS-1$
textExport.append("style=\""); //$NON-NLS-1$
textExport.append("fill: " + labelStyle.getColor() + "; "); //$NON-NLS-1$ //$NON-NLS-2$
textExport.append(this.exportFont(labelStyle));
if (type.contains("center")) { //$NON-NLS-1$
textExport.append("text-anchor:middle"); //$NON-NLS-1$
}
textExport.append("\">"); //$NON-NLS-1$

textExport.append(text);
String[] lines = text.split("\\n", -1); //$NON-NLS-1$
if (lines.length == 1) {
textExport.append(text);
} else {
textExport.append("<tspan x=\"0\">" + lines[0] + "</tspan>"); //$NON-NLS-1$//$NON-NLS-2$
double fontSize = labelStyle.getFontSize();
for (int i = 1; i < lines.length; i++) {
if (lines[i].isEmpty()) {
// avoid tspan to be ignored if there is only a line return
lines[i] = " "; //$NON-NLS-1$
}
textExport.append("<tspan x=\"0\" dy=\"" + fontSize + "\">" + lines[i] + "</tspan>"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
}
}

return textExport.append("</text>"); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 THALES GLOBAL SERVICES.
* Copyright (c) 2021, 2022 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -51,14 +51,7 @@ public class TextBoundsProvider {
* @return the text bounds
*/
public TextBounds computeBounds(LabelStyle labelStyle, String text) {
int fontStyle = Font.PLAIN;
if (labelStyle.isBold()) {
fontStyle = fontStyle | Font.BOLD;
}
if (labelStyle.isItalic()) {
fontStyle = fontStyle | Font.ITALIC;
}
Font font = new Font(this.getFontName(), fontStyle, labelStyle.getFontSize());
Font font = this.getFont(labelStyle);

String[] lines = text.split("\\n", -1); //$NON-NLS-1$
Rectangle2D labelBounds = font.getStringBounds(lines[0], FONT_RENDER_CONTEXT);
Expand Down Expand Up @@ -93,6 +86,17 @@ public TextBounds computeBounds(LabelStyle labelStyle, String text) {
return new TextBounds(size, alignment);
}

private Font getFont(LabelStyle labelStyle) {
int fontStyle = Font.PLAIN;
if (labelStyle.isBold()) {
fontStyle = fontStyle | Font.BOLD;
}
if (labelStyle.isItalic()) {
fontStyle = fontStyle | Font.ITALIC;
}
return new Font(this.getFontName(), fontStyle, labelStyle.getFontSize());
}

private String getFontName() {
if (fontName == null) {
if (this.isDefaultFontAvailable()) {
Expand Down

0 comments on commit b0a7fcd

Please sign in to comment.