現在、任意のフォントでレンダリングされた文字列の幅を取得しようとしています。
BufferedImage img = new BufferedImage(10, 10, BufferedImage.TRANSLUCENT);
img.createGraphics();
Graphics2D g = (Graphics2D) img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics metrics = g.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
//Dimension dim = new Dimension(adv + 2, hgt + 2);
問題はこの行にあります...
int adv = metrics.stringWidth(text);
ほとんどのフォントではこれでうまくいきますが、一部の洗練されたスクリプト フォントでは、予想よりも幅が狭くなっているように見えます。FontMetrics.stringWidth の javadoc に従って、読むこともできます...
この Font で指定された文字列を表示するための合計送り幅を返します。前進は、弦のベースラインの左端から右端までの距離です。String の進行は、必ずしもその文字の進行の合計ではないことに注意してください。