From c8395c4802385737519c31fdd55243af3873c32d Mon Sep 17 00:00:00 2001 From: Laurent Fasani Date: Mon, 28 Feb 2022 10:58:58 +0100 Subject: [PATCH] [781] Adapt the ELK to consider the multiple line The ELK label TextBound is set according the line returns contained in the label text. This will allow to have the right width of the node and to avoid the overlap of the label text with the contained nodes. Bug: https://github.com/eclipse-sirius/sirius-components/issues/781 Signed-off-by: Laurent Fasani --- .../diagrams/layout/ELKDiagramConverter.java | 4 ++-- .../diagrams/TextBoundsProvider.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/backend/sirius-components-diagrams-layout/src/main/java/org/eclipse/sirius/components/diagrams/layout/ELKDiagramConverter.java b/backend/sirius-components-diagrams-layout/src/main/java/org/eclipse/sirius/components/diagrams/layout/ELKDiagramConverter.java index b2da0218613..ba6d6f83641 100644 --- a/backend/sirius-components-diagrams-layout/src/main/java/org/eclipse/sirius/components/diagrams/layout/ELKDiagramConverter.java +++ b/backend/sirius-components-diagrams-layout/src/main/java/org/eclipse/sirius/components/diagrams/layout/ELKDiagramConverter.java @@ -320,8 +320,8 @@ private void convertNode(Node node, ElkNode parent, Map + * The text bounds take into account the line return contained in text. * * @param labelStyle * the label style @@ -58,10 +59,23 @@ public TextBounds computeBounds(LabelStyle labelStyle, String text) { fontStyle = fontStyle | Font.ITALIC; } Font font = new Font(this.getFontName(), fontStyle, labelStyle.getFontSize()); - Rectangle2D stringBounds = font.getStringBounds(text, FONT_RENDER_CONTEXT); - double width = stringBounds.getWidth(); - double height = stringBounds.getHeight(); + String[] lines = text.split("\\n", -1); //$NON-NLS-1$ + Rectangle2D labelBounds = font.getStringBounds(lines[0], FONT_RENDER_CONTEXT); + if (lines.length > 1) { + for (int i = 1; i < lines.length; i++) { + String line = lines[i]; + + Rectangle2D lineBounds = font.getStringBounds(line, FONT_RENDER_CONTEXT); + // shift the rectangle under the previous line + lineBounds.setFrame(lineBounds.getX(), lineBounds.getY() + labelBounds.getHeight(), lineBounds.getWidth(), lineBounds.getHeight()); + + labelBounds = labelBounds.createUnion(lineBounds); + } + } + + double height = labelBounds.getHeight(); + double width = labelBounds.getWidth(); double iconWidth = 0; double iconHeight = 0; if (!labelStyle.getIconURL().isEmpty()) { @@ -74,7 +88,7 @@ public TextBounds computeBounds(LabelStyle labelStyle, String text) { Size size = Size.of(width + iconWidth, height + iconHeight); // Sprotty needs the inverse of the x and y for the alignment, so it's "0 - x" and "0 - y" on purpose - Position alignment = Position.at(0 - stringBounds.getX() + iconWidth, 0 - stringBounds.getY()); + Position alignment = Position.at(0 - labelBounds.getX() + iconWidth, 0 - labelBounds.getY()); return new TextBounds(size, alignment); }