开发者

How to obtain bounding box of transformed text(java)?

开发者 https://www.devze.com 2023-01-09 07:39 出处:网络
Currently I have a text object that I need to determine its bounds. I used to use the graphics object to obtain the font metrics of the text I am trying to draw, but since I added functionality to rot

Currently I have a text object that I need to determine its bounds. I used to use the graphics object to obtain the font metrics of the text I am trying to draw, but since I added functionality to rotating the object(and possibly more) I need a better way to get the bounds of this object. I have looked multiple places and nothing has really worked for me as of yet. Here is my most current attempt:

//This is the bounding box edges 0: left, 1: right 2: top 3: bottom  
int toReturn[] = new int[4];
//this.transform is the AffineTransform for the text Object(currently only
//rotated)
FontRenderContext frc = new FontRenderContext(this.transform,true,false);
TextLayout tl = new TextLayout(this.typedText,this.font,frc);
Rectangle开发者_如何学运维2D bb = tl.getBounds();
toReturn[0] = (int)(bb.getX());
toReturn[1] = (int)(bb.getX()+bb.getWidth());
toReturn[2] = (int)(bb.getY());
toReturn[3] = (int)(bb.getY()+bb.getHeight());

Is this the proper way to get the bounding box for transformed text?


No, the AffineTransform supplied to FontRenderContext "is used to scale typographical points to pixels in this FontRenderContext." You should be able to use createTransformedShape() on the boundary to get the result you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号