]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Use TextNode implementation for TextElementHandler bounds calculation 13/4513/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 25 Sep 2020 13:44:48 +0000 (16:44 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 25 Sep 2020 13:44:48 +0000 (16:44 +0300)
gitlab #612

Change-Id: I9ff4ea60513fb984e8988681a59f38959e4c4164

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

index a50c190b9b27fdb13a2446bd89e6b36ee5c10214..f7dd3a0fda9f66aa675a6fcc7d31ac0f9f801db2 100644 (file)
  *******************************************************************************/
 package org.simantics.diagram.elements;
 
+import java.awt.Color;
 import java.awt.Font;
-import java.awt.font.FontRenderContext;
-import java.awt.font.TextLayout;
-import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
 
 import org.simantics.g2d.element.ElementHints;
@@ -65,13 +63,14 @@ public class TextElementHandler extends TextElementNoBounds implements InternalS
 
        @Override
     public Rectangle2D getBounds(IElement e, Rectangle2D size) {
-        return calculateBounds(e, size, horizontalAlignment, scale, paddingX, paddingY);
+        return calculateBounds(e, size, horizontalAlignment, verticalAlignment, scale, paddingX, paddingY);
     }
     
     public static Rectangle2D calculateBounds(
             IElement e,
             Rectangle2D size, 
             Alignment horizontalAlignment, 
+            Alignment verticalAlignment,
             double scale,
             double paddingX,
             double paddingY) {
@@ -89,16 +88,16 @@ public class TextElementHandler extends TextElementNoBounds implements InternalS
             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);
+               size.setFrame(0, 0, 0, 0);
             else {
-                FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);
-                TextLayout tl = new TextLayout(text, font, FRC);
-                Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D();   
-                size.setFrame(
-                        getAlignedXCoordinate(bounds, horizontalAlignment) * scale - paddingX,
-                        bounds.getY() * scale -paddingY, 
-                        bounds.getWidth()* scale + paddingX + paddingX, 
-                        bounds.getHeight()* scale + paddingY + paddingY);
+                   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;
             }
         }
         return size;