Here you can find the source of isFixedWidthFont(String fontName, Component component)
Parameter | Description |
---|---|
fontName | the font name |
component | the component where the font will be displayed. |
public static boolean isFixedWidthFont(String fontName, Component component)
//package com.java2s; import javax.swing.*; import java.awt.*; public class Main { /**/* w ww . j av a 2 s . c o m*/ * Checks if the font specified by the font name is fixed width font. Fixed width font means all chars have the * exact same width. * * @param fontName the font name * @param component the component where the font will be displayed. * @return true if the font is fixed width. Otherwise false. */ public static boolean isFixedWidthFont(String fontName, Component component) { if (fontName.endsWith(" Bold") || fontName.endsWith(" ITC") || fontName.endsWith(" MT") || fontName.endsWith(" LET") || fontName.endsWith(".bold") || fontName.endsWith(".italic")) return false; try { Font font = new Font(fontName, 0, 12); if (!font.canDisplay('W')) return false; Font boldFont = font.deriveFont(Font.BOLD); FontMetrics fm = component.getFontMetrics(font); FontMetrics fmBold = component.getFontMetrics(boldFont); int l1 = fm.charWidth('l'); int l2 = fmBold.charWidth('l'); if (l1 == l2) { int w1 = fm.charWidth('W'); int w2 = fmBold.charWidth('W'); if (w1 == w2 && l1 == w1) { int s1 = fm.charWidth(' '); int s2 = fmBold.charWidth(' '); if (s1 == s2) { return true; } } } } catch (Throwable throwable) { // ignore it and return false } return false; } /** * Copied from JDK's SwingUtilities2.java * <p/> * Returns the FontMetrics for the current Font of the passed in Graphics. This method is used when a Graphics is * available, typically when painting. If a Graphics is not available the JComponent method of the same name should * be used. * <p/> * Callers should pass in a non-null JComponent, the exception to this is if a JComponent is not readily available * at the time of painting. * <p/> * This does not necessarily return the FontMetrics from the Graphics. * * @param c JComponent requesting FontMetrics, may be null * @param g Graphics Graphics */ public static FontMetrics getFontMetrics(JComponent c, Graphics g) { return getFontMetrics(c, g, g.getFont()); } /** * Copied from JDK's SwingUtilities2.java * <p/> * Returns the FontMetrics for the specified Font. This method is used when a Graphics is available, typically when * painting. If a Graphics is not available the JComponent method of the same name should be used. * <p/> * Callers should pass in a non-null JComponent, the exception to this is if a JComponent is not readily available * at the time of painting. * <p/> * This does not necessarily return the FontMetrics from the Graphics. * * @param c JComponent requesting FontMetrics, may be null * @param c Graphics Graphics * @param font Font to get FontMetrics for */ public static FontMetrics getFontMetrics(JComponent c, Graphics g, Font font) { if (c != null) { // Note: We assume that we're using the FontMetrics // from the widget to layout out text, otherwise we can get // mismatches when printing. return c.getFontMetrics(font); } return Toolkit.getDefaultToolkit().getFontMetrics(font); } }