]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Use paddings when calculating Text bounds. 66/4666/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 5 Mar 2021 13:12:24 +0000 (15:12 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 5 Mar 2021 13:12:24 +0000 (15:12 +0200)
Allow external implementations to use bounds calculation without
re-using cached data.

gitlab #676

Change-Id: I9277f45189c45c5e5845a194633e2becd0f2abdb

bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java

index f7dd3a0fda9f66aa675a6fcc7d31ac0f9f801db2..39ca504aabd071d6fc5279405ba9122023dc41ec 100644 (file)
@@ -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;
         
     }