java – 图形字符串的起源在哪里开始?
作者:互联网
在核心Java书中,它说
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();
当说矩形的原点位于字符串的基线时,作者的意思是什么,而顶部的y坐标是上升?
字符串的边界矩形从哪里开始?
用测试字符串我得到以下内容:
w: 291.0
h: 91.265625
x:0.0
y:-72.38671875
descent: 15.8203125
leading: 3.0585938
这意味着矩形原点位于前导而不是基线,我对此是否正确?
解决方法:
数学计算出来了:
72.38671875上升15.8203125下降3.0585938领先= 91.265625总高度
This tutorial on 2D Text有一个图像说明领先,下降和上升.
在您的具体情况下,72.38671875是上升的高度.这是从基线到最高雕文顶部的测量值.前导是下行器底部到下一行顶部之间的空间.
边界矩形相对于基线. FontMetrics.getStringBounds的API指出“返回的边界是基线相对坐标”,它解释了您的结果. x将始终为0,并且边界框的高度将是上升加上下降加上前导.
标签:java,swing,graphics,awt,java-2d 来源: https://codeday.me/bug/20190715/1470306.html