From 69bc5f4a2b4f05ac4ce4ababbdbe68d91be0fed6 Mon Sep 17 00:00:00 2001 From: Laurent Fasani Date: Mon, 7 Mar 2022 10:05:42 +0100 Subject: [PATCH] [781] Adapt svg export to multiline label Bug: https://github.com/eclipse-sirius/sirius-components/issues/781 Signed-off-by: Laurent Fasani --- .../svg/DiagramElementExportService.java | 25 +++++++++++++++-- .../diagrams/TextBoundsProvider.java | 28 +++++++++++++------ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/backend/sirius-components-collaborative-diagrams/src/main/java/org/eclipse/sirius/components/collaborative/diagrams/export/svg/DiagramElementExportService.java b/backend/sirius-components-collaborative-diagrams/src/main/java/org/eclipse/sirius/components/collaborative/diagrams/export/svg/DiagramElementExportService.java index 56532edaa3..78642fa83b 100644 --- a/backend/sirius-components-collaborative-diagrams/src/main/java/org/eclipse/sirius/components/collaborative/diagrams/export/svg/DiagramElementExportService.java +++ b/backend/sirius-components-collaborative-diagrams/src/main/java/org/eclipse/sirius/components/collaborative/diagrams/export/svg/DiagramElementExportService.java @@ -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; /** @@ -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); } @@ -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(""); //$NON-NLS-1$ } @@ -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(""); //$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("" + lines[0] + ""); //$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("" + lines[i] + ""); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + } + } return textExport.append(""); //$NON-NLS-1$ } diff --git a/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/TextBoundsProvider.java b/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/TextBoundsProvider.java index d2e09b4bd1..55c87fc280 100644 --- a/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/TextBoundsProvider.java +++ b/backend/sirius-components-diagrams/src/main/java/org/eclipse/sirius/components/diagrams/TextBoundsProvider.java @@ -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 @@ -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); @@ -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()) {