Skip to content

Commit

Permalink
[781] Adapt the ELK to consider the multiple line
Browse files Browse the repository at this point in the history
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: #781
Signed-off-by: Laurent Fasani <[email protected]>
  • Loading branch information
lfasani committed Mar 2, 2022
1 parent 9dad399 commit c8395c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ private void convertNode(Node node, ElkNode parent, Map<String, ElkConnectableSh
elkNode.setDimensions(node.getSize().getWidth(), node.getSize().getHeight());
elkNode.setLocation(node.getPosition().getX(), node.getPosition().getY());
} else {
double width = Math.max(textBounds.getSize().getWidth(), node.getSize().getWidth());
double height = Math.max(textBounds.getSize().getHeight(), node.getSize().getHeight());
double width = textBounds.getSize().getWidth();
double height = textBounds.getSize().getHeight();
elkNode.setDimensions(width, height);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class TextBoundsProvider {
private static String fontName;

/**
* Computes the text bounds for a label with the given text.
* Computes the text bounds for a label with the given text.<br>
* The text bounds take into account the line return contained in text.
*
* @param labelStyle
* the label style
Expand All @@ -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()) {
Expand All @@ -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);
}
Expand Down

0 comments on commit c8395c4

Please sign in to comment.