From: Marko Luukkainen Date: Fri, 25 Sep 2020 13:44:48 +0000 (+0300) Subject: Use TextNode implementation for TextElementHandler bounds calculation X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=271b16d9670ad5f20a2ca12d0c67f15390cddb54;p=simantics%2Fplatform.git Use TextNode implementation for TextElementHandler bounds calculation gitlab #612 Change-Id: I9ff4ea60513fb984e8988681a59f38959e4c4164 --- 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 a50c190b9..f7dd3a0fd 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 @@ -11,10 +11,8 @@ *******************************************************************************/ 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;