Java FontMetrics getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size)

Here you can find the source of getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size)

Description

Calculates the largest size of the given font for which the given string will fit into the given size.

License

Open Source License

Declaration

public static int getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size) 

Method Source Code

//package com.java2s;
/**/*from w ww.jav  a 2 s.  c om*/
 * The utillib library.
 * More information is available at http://www.jinchess.com/.
 * Copyright (C) 2002 Alexander Maryanovsky.
 * All rights reserved.
 * <p>
 * The utillib library is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * <p>
 * The utillib library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Lesser General Public License
 * along with utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.*;

public class Main {
    /**
     * Calculates the largest size of the given font for which the given string
     * will fit into the given size. This method uses the default toolkit.
     */
    public static int getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size) {
        return getMaxFittingFontSize(g, font, string, size.width, size.height);
    }

    /**
     * Calculates the largest size of the given font for which the given string
     * will fit into the given size.
     */
    public static int getMaxFittingFontSize(Graphics g, Font font, String string, int width, int height) {
        int minSize = 0;
        int maxSize = 288;
        int curSize = font.getSize();

        while (maxSize - minSize > 2) {
            FontMetrics fm = g.getFontMetrics(new Font(font.getName(), font.getStyle(), curSize));
            int fontWidth = fm.stringWidth(string);
            int fontHeight = fm.getLeading() + fm.getMaxAscent() + fm.getMaxDescent();

            if ((fontWidth > width) || (fontHeight > height)) {
                maxSize = curSize;
                curSize = (maxSize + minSize) / 2;
            } else {
                minSize = curSize;
                curSize = (minSize + maxSize) / 2;
            }
        }

        return curSize;
    }

    /**
     * Returns the <code>FontMetrics</code> for the specified <code>Font</code>.
     */
    public static FontMetrics getFontMetrics(Font font) {
        return Toolkit.getDefaultToolkit().getFontMetrics(font); // DEPRECATED
    }
}

Related

  1. getLineHeight(Component c, int defaultHeight)
  2. getLineHeight(Component c, int defaultHeight)
  3. getLineHeight(Component component, int count)
  4. getLineHeight(FontMetrics fm)
  5. getLongestStringWidth(FontMetrics fm, String[] theStrings)
  6. getMaxFontHeight(final java.awt.Font font)
  7. getMonospacedFontNames()
  8. getMonospacedFontsFamillyName()
  9. getPrefSize(FontMetrics fm, String keyTip)