图形字符串的起点从哪里开始?
在核心Java书中它说
getStringBounds方法返回的矩形的宽度是水平方向 字符串的范围。矩形的高度是上升、下降和 领导。矩形的原点位于字符串的基线处。矩形顶部 y 坐标为负。这样就可以得到字符串的宽度、高度和 上升如下:
double stringWidth = bounds.getWidth();
double stringHeight = bounds.getHeight();
double ascent = -bounds.getY();
作者说矩形的原点位于字符串的基线,而顶部 y 坐标是 ascent 是什么意思?
字符串的边界矩形从哪里开始?
使用测试字符串,我得到以下结果:
w: 291.0
h: 91.265625
x:0.0
y:-72.38671875
descent: 15.8203125
leading: 3.0585938
这意味着矩形原点位于前导而不是基线,我对此是否正确?
In core Java book it says
The width of the rectangle that the getStringBounds method returns is the horizontal
extent of the string. The height of the rectangle is the sum of ascent, descent, and
leading. The rectangle has its origin at the baseline of the string. The top y -coordinate of the rectangle is negative. Thus, you can obtain string width, height, and
ascent as follows:
double stringWidth = bounds.getWidth();
double stringHeight = bounds.getHeight();
double ascent = -bounds.getY();
What does the author mean when saying that the rectangle has its origin at the baseline of the string, while top y-coordinate is the ascent?
Where does the bounding rectangle of the string start?
with a test string i got the following:
w: 291.0
h: 91.265625
x:0.0
y:-72.38671875
descent: 15.8203125
leading: 3.0585938
That mean the rectangle origin is at the leading not the baseline, am i correct on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这意味着边界坐标位于零 Y 坐标位于字符串基线且正 Y 坐标向下的空间中。在下图中,黑点对应于零 Y:
因此为负
bounds.getY() (ascent) 对应于最上面的坐标。并且正
bounds.getHeight() +bounds.getY()
(下降 + 前导)将对应于该坐标空间中最底部的坐标。It means that the bounds' coordinates are in a space where zero Y coordinate is at string's baseline and positive Y coordinates go downwards. In the following image the black dot corresponds to zero Y:
Therefore negative
bounds.getY()
(ascent) corresponds to the topmost coordinate. And positivebounds.getHeight() + bounds.getY()
(descent + leading) will correspond to the botmommost coordinate in this coordinate space.计算结果为:
72.38671875 上升 + 15.8203125 下降 + 3.0585938 领先 = 91.265625 总高度
本关于 2D 文本的教程有一张图像说明了前导、下降和上升。
在您的具体情况下,72.38671875 是上升的高度。这是从基线到最高字形顶部的测量值。行距是下降部分底部到下一行顶部之间的空间。
边界矩形是相对于基线的。
FontMetrics.getStringBounds
的 API 声明“返回的边界位于相对基线坐标中”,这解释了您的结果。x
将始终为 0,并且边界框的高度将为上升加上下降加上前导。The math works out:
72.38671875 ascent + 15.8203125 descent + 3.0585938 leading = 91.265625 total height
This tutorial on 2D Text has an image illustrating leading, descent, and ascent.
In your specific case, 72.38671875 is the height of the ascent. That's measured from the baseline to the top of the tallest glyph. The leading is the space between the bottom of the descender to the top of the next line.
The bounding rectangle is relative to the baseline. The API for
FontMetrics.getStringBounds
states "The returned bounds is in baseline-relative coordinates", which explains your results.x
will always be 0, and the height of the bounding box will be the ascent plus the descent plus the leading.Java 图形坐标系的原点位于画布的右上角,Y 坐标从上到下递增。这意味着矩形的顶部边缘(
getY()
的返回值)的 Y 坐标将小于其底部边缘(文本字符串的基线)。getStringBounds()
的结果值与此仅有些一致。虽然尊重坐标系,但边界矩形的原点是相对于基线的,而不是在左上角。这意味着矩形的左上角将具有负 Y 坐标。The Java graphics coordinate system has its origin in the top right of the canvas, with the Y coordinate increasing from top to bottom. This means that a rectangle's top edge (the return value of
getY()
) will have a smaller Y coordinate than its bottom edge (the baseline of a text string).The result value of
getStringBounds()
is only somewhat consistent with this. While the coordinate system is respected, the origin of the bounding rectangle is relative to the baseline, not at the top left. This means that the top left of the rectangle will have a negative Y coordinate.