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 7, 2022
1 parent 3d617ca commit 69bc5f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.sirius.components.diagrams.Node;
import org.eclipse.sirius.components.diagrams.Position;
import org.eclipse.sirius.components.diagrams.Size;
import org.eclipse.sirius.components.diagrams.TextBoundsProvider;
import org.springframework.stereotype.Service;

/**
Expand All @@ -34,6 +35,8 @@
public class DiagramElementExportService {
private final ImageRegistry imageRegistry;

private TextBoundsProvider textBoundsProvider = new TextBoundsProvider();

public DiagramElementExportService(ImageRegistry imageRegistry) {
this.imageRegistry = Objects.requireNonNull(imageRegistry);
}
Expand All @@ -54,7 +57,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 +112,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 lineHeight = this.textBoundsProvider.getLineHeight(labelStyle, text);
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=\"" + lineHeight + "\">" + 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,23 @@ public TextBounds computeBounds(LabelStyle labelStyle, String text) {
return new TextBounds(size, alignment);
}

public double getLineHeight(LabelStyle labelStyle, String text) {
Font font = this.getFont(labelStyle);
Rectangle2D labelBounds = font.getStringBounds(text, FONT_RENDER_CONTEXT);
return labelBounds.getHeight();
}

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 69bc5f4

Please sign in to comment.