From: Marko Luukkainen Date: Fri, 5 Mar 2021 13:12:24 +0000 (+0200) Subject: Use paddings when calculating Text bounds. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F66%2F4666%2F1;p=simantics%2Fplatform.git Use paddings when calculating Text bounds. Allow external implementations to use bounds calculation without re-using cached data. gitlab #676 Change-Id: I9277f45189c45c5e5845a194633e2becd0f2abdb --- diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java index f7dd3a0fd..39ca504aa 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java @@ -63,10 +63,10 @@ public class TextElementHandler extends TextElementNoBounds implements InternalS @Override public Rectangle2D getBounds(IElement e, Rectangle2D size) { - return calculateBounds(e, size, horizontalAlignment, verticalAlignment, scale, paddingX, paddingY); + return getBounds(e, size, horizontalAlignment, verticalAlignment, scale, paddingX, paddingY); } - - public static Rectangle2D calculateBounds( + + public static Rectangle2D getBounds( IElement e, Rectangle2D size, Alignment horizontalAlignment, @@ -74,32 +74,46 @@ public class TextElementHandler extends TextElementNoBounds implements InternalS double scale, double paddingX, double paddingY) { - TextNode node = (TextNode) e.getHint(SG_NODE); Rectangle2D b = e.getHint(ElementHints.KEY_BOUNDS); - + if (size == null) size = new Rectangle2D.Double(); if (node != null) size.setRect(node.getBoundsInLocal()); else if(b != null && ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false)) size.setRect(b); - else { - String text = e.getHint(ElementHints.KEY_TEXT); - Font font = e.getHint(ElementHints.KEY_FONT); - if(text == null || font == null) - size.setFrame(0, 0, 0, 0); - else { - TextNode n= new TextNode(); - n.init(text, font, Color.black, 0.0, 0.0, scale); - n.setHorizontalAlignment((byte)horizontalAlignment.ordinal()); - n.setVerticalAlignment((byte)verticalAlignment.ordinal()); - - Rectangle2D r = n.getBoundsInLocal(); - size.setFrame(r); - return r; - } - } + else calculateBounds(e, size, horizontalAlignment, verticalAlignment, scale, paddingX, paddingY); + + return size; + } + + public static Rectangle2D calculateBounds( + IElement e, + Rectangle2D size, + Alignment horizontalAlignment, + Alignment verticalAlignment, + double scale, + double paddingX, + double paddingY) { + + if (size == null) + size = new Rectangle2D.Double(); + + String text = e.getHint(ElementHints.KEY_TEXT); + Font font = e.getHint(ElementHints.KEY_FONT); + if(text == null || font == null) { + size.setFrame(0, 0, 0, 0); + } else { + TextNode n= new TextNode(); + n.init(text, font, Color.black, 0.0, 0.0, scale); + n.setHorizontalAlignment((byte)horizontalAlignment.ordinal()); + n.setVerticalAlignment((byte)verticalAlignment.ordinal()); + n.setPadding(paddingX, paddingY); + Rectangle2D r = n.getBoundsInLocal(); + size.setFrame(r); + return r; + } return size; }