*******************************************************************************/
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;
@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) {
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;