9

を使用してフォントのレンダリングされた高さを決定するのは簡単ですFontMetricsが、その逆はどうでしょうか。ピクセル単位の特定の高さに収まるフォントを取得するにはどうすればよいですか?

Verdana「アセンダーからディセンダーまで30ピクセルの高さのサイズでください。」

これをJavaに要求するにはどうすればよいですか?

4

4 に答える 4

12

私はこれが非常に古い質問であることを知っていますが、誰かがそれを見つけるかもしれません:

Java(および他の多くの場所)のフォントの高さは、「活版印刷のポイント」で示されます。これは、およそ1/72インチとして定義されます。

特定のピクセルの高さに必要なポイントを計算するには、次を使用できる必要があります。

double fontSize= 72.0 * pixelSize / Toolkit.getDefaultToolkit().getScreenResolution();

私はまだこれを広範囲にテストしていませんが、私が使用したモニターでは機能するようです。うまくいかない場合があれば報告します。

これを使用した標準のシステムフォントの場合、大文字の高さ(つまり、上昇)を指定されたピクセルサイズに設定します。ascent + descentをピクセルサイズに設定する必要がある場合は、FontMetricsを使用して値を修正できます。

FontMetrics m= g.getFontMetrics(font); // g is your current Graphics object
double totalSize= fontSize * (m.getAscent() + m.getDescent()) / m.getAscent();

もちろん、特定の文字の実際のピクセルの高さは、使用する文字とフォントによって異なります。したがって、「H」の高さが正確なピクセル数であることを確認したい場合は、トライアルを使用することをお勧めします。 -他の回答で言及されているエラーメソッド。これらのメソッドを使用して、表示する特定のテキストごとにサイズを取得すると(@Bobが提案したように)、画面上で「ace」のようなテキストがランダムにフォントサイズが乱れる可能性があることに注意してください。 「は「タグ」よりもはるかに大きな文字になります。これを回避するために、特定の文字または文字シーケンス("T"または"Tg"など)を1つ選択し、それをピクセルの高さに一度固定してから、そこから取得したフォントサイズを使用します。

于 2014-10-25T17:01:54.473 に答える
7

高さでフォントを見つける「直接的な」方法はないと思います。間接的な方法のみ...サイズをループし、それぞれの高さをテストすることにより、<=必要な高さです。

これを一度実行している場合は、それらをループするだけです...「オンザフライ」で実行している場合は、バイナリ検索を実行すると、より高速になります。

于 2011-04-29T08:24:54.543 に答える
5

ピクセル単位の実際の高さでフォントを取得する方法を知りません。使用されているコンテキストによって異なるため、最適な一致をサンプリングするよりも短い方法はおそらくありません。設計された高さから上下のサイズを探すのはかなり速いはずです。これを行うメソッドの例を次に示します。

public Font getFont(String name, int style, int height) {
    int size = height;
    Boolean up = null;
    while (true) {
        Font font = new Font(name, style, size);
        int testHeight = getFontMetrics(font).getHeight();
        if (testHeight < height && up != Boolean.FALSE) {
            size++;
            up = Boolean.TRUE;
        } else if (testHeight > height && up != Boolean.TRUE) {
            size--;
            up = Boolean.FALSE;
        } else {
            return font;
        }
    }
}
于 2011-04-29T08:48:53.160 に答える
1

WhiteFang34のコードは、特定の文字列の実際の高さを返す次のメソッドと組み合わせて使用​​すると便利です。リアルタイムレンダリング、特に大きなフォント/文字列の場合は少し遅いかもしれません。さらに最適化できると確信していますが、今のところ、それは私自身のニーズを満たし、バックエンドプロセスで実行するのに十分な速度です。

/*
 * getFontRenderedHeight
 * *************************************************************************
 * Summary: Font metrics do not give an accurate measurement of the rendered
 * font height for certain strings because the space between the ascender
 * limit and baseline is not always fully used and descenders may not be
 * present. for example the strings '0' 'a' 'f' and 'j' are all different
 * heights from top to bottom but the metrics returned are always the same.
 * If you want to place text that exactly fills a specific height, you need
 * to work out what the exact height is for the specific string. This method
 * achieves that by rendering the text and then scanning the top and bottom
 * rows until the real height of the string is found.
 */
/**
 * Calculate the actual height of rendered text for a specific string more
 * accurately than metrics when ascenders and descenders may not be present
 * <p>
 * Note: this method is probably not very efficient for repeated measurement
 * of large strings and large font sizes but it works quite effectively for
 * short strings. Consider measuring a subset of your string value. Also
 * beware of measuring symbols such as '-' and '.' the results may be
 * unexpected!
 * 
 * @param string
 *            The text to measure. You might be able to speed this process
 *            up by only measuring a single character or subset of your
 *            string i.e if you know your string ONLY contains numbers and
 *            all the numbers in the font are the same height, just pass in
 *            a single digit rather than the whole numeric string.
 * @param font
 *            The font being used. Obviously the size of the font affects
 *            the result
 * @param targetGraphicsContext
 *            The graphics context the text will actually be rendered in.
 *            This is passed in so the rendering options for anti-aliasing
 *            can be matched.
 * @return Integer - the exact actual height of the text.
 * @author Robert Heritage [mrheritage@gmail.com]
 */
public Integer getFontRenderedHeight(String string, Font font, Graphics2D targetGraphicsContext) {
    BufferedImage image;
    Graphics2D g;
    Color textColour = Color.white;

    // In the first instance; use a temporary BufferedImage object to render
    // the text and get the font metrics.
    image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    g = image.createGraphics();
    FontMetrics metrics = g.getFontMetrics(font);
    Rectangle2D rect = metrics.getStringBounds(string, g);

    // now set up the buffered Image with a canvas size slightly larger than
    // the font metrics - this guarantees that there is at least one row of
    // black pixels at the top and the bottom
    image = new BufferedImage((int) rect.getWidth() + 1, (int) metrics.getHeight() + 2, BufferedImage.TYPE_INT_RGB);
    g = image.createGraphics();

    // take the rendering hints from the target graphics context to ensure
    // the results are accurate.
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, targetGraphicsContext.getRenderingHint(RenderingHints.KEY_ANTIALIASING));
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, targetGraphicsContext.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING));

    g.setColor(textColour);
    g.setFont(font);
    g.drawString(string, 0, image.getHeight());

    // scan the bottom row - descenders will be cropped initially, so the
    // text will need to be moved up (down in the co-ordinates system) to
    // fit it in the canvas if it contains any. This may need to be done a
    // few times until there is a row of black pixels at the bottom.
    boolean foundBottom, foundTop = false;
    int offset = 0;
    do {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setColor(textColour);
        g.drawString(string, 0, image.getHeight() - offset);

        foundBottom = true;
        for (int x = 0; x < image.getWidth(); x++) {
            if (image.getRGB(x, image.getHeight() - 1) != Color.BLACK.getRGB()) {
                foundBottom = false;
            }
        }
        offset++;
    } while (!foundBottom);

    System.out.println(image.getHeight());

    // Scan the top of the image downwards one line at a time until it
    // contains a non-black pixel. This loop uses the break statement to
    // stop the while loop as soon as a non-black pixel is found, this
    // avoids the need to scan the rest of the line
    int y = 0;
    do {
        for (int x = 0; x < image.getWidth(); x++) {
            if (image.getRGB(x, y) != Color.BLACK.getRGB()) {
                foundTop = true;
                break;
            }
        }
        y++;
    } while (!foundTop);

    return image.getHeight() - y;
}
于 2011-10-12T09:21:10.660 に答える