这是Frank的reportSize
函数的另一种实现方式:
public void reportSize(String s, Font myFont) {
Text text = new Text(s);
text.setFont(myFont);
Bounds tb = text.getBoundsInLocal();
Rectangle stencil = new Rectangle(
tb.getMinX(), tb.getMinY(), tb.getWidth(), tb.getHeight()
);
Shape intersection = Shape.intersect(text, stencil);
Bounds ib = intersection.getBoundsInLocal();
System.out.println(
"Text size: " + ib.getWidth() + ", " + ib.getHeight()
);
}
此实现使用形状相交来确定呈现形状的边界框大小,不包含空白区域。该实现不依赖于Java 9+中用户应用程序代码可能无法直接访问的 com.sun
包类。
经过一些尝试,我想出了这个解决方案:
这是生成它的代码:
public void getBoundingBox(String s, Font myFont) {
final FontMetrics fm = Toolkit.getToolkit().getFontLoader().getFontMetrics(myFont);
final Canvas canvas = new Canvas(fm.computeStringWidth(s), fm.getAscent() + fm.getDescent());
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.RED); // Just an abitrary color
gc.setTextBaseline(VPos.TOP); // This saves having to scan the bottom
gc.setFont(myFont);
gc.fillText(s, -fm.getLeading(), 0); // This saves having to scan the left
// Get a snapshot of the canvas
final WritableImage image = canvas.snapshot(null, null);
final PixelReader pr = image.getPixelReader();
final int h = (int) canvas.getHeight();
final int w = (int) canvas.getWidth();
int x;
int y = 0;
// Scan from the top down until we find a red pixel
boolean found = false;
while (y < h && !found) {
x = 0;
while (x < w && !found) {
found = pr.getColor(x, y).equals(Color.RED);
x++;
}
y++;
}
int yPos = y - 2;
// Scan from right to left until we find a red pixel
x = w;
found = false;
while (x > 0 && !found) {
y = 0;
while (y < h && !found) {
found = pr.getColor(x, y).equals(Color.RED);
y++;
}
x--;
}
int xPos = x + 3;
// Here is a visible representation of the bounding box
Rectangle mask = new Rectangle(0, yPos, xPos, h - yPos);
mask.setFill(Color.rgb(0, 0, 255, 0.25));
root.getChildren().addAll(canvas, mask); // root is a global AnchorPane
System.out.println("The width of the bounding box is " + xPos);
System.out.println("The height of the bounding box is " + (h - yPos));
}
FontMetrics需要两个导入:
import com.sun.javafx.tk.FontMetrics;
import com.sun.javafx.tk.Toolkit;
并且如下:
Font myFont = new Font("Arial", 100.0);
getBoundingBox("Testing", myFont);
javafx
,是否正确?据我所知,字体测量数据在 JavaFX API 中没有暴露,因此这个问题似乎与JavaFX没有直接关联。 - jewelseacom.sun.javafx
API,这些API不属于受支持的javafx.
API之列。 - jewelsea