Java FontMetrics getAllStylesSameWidthsFontsFamillyName()

Here you can find the source of getAllStylesSameWidthsFontsFamillyName()

Description

get All Styles Same Widths Fonts Familly Name

License

Open Source License

Return

String[0] is the SciNotes fonts and String[1] the non-SciNotes

Declaration

public static String[][] getAllStylesSameWidthsFontsFamillyName() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static Graphics2D g2d;
    private static int defaultFontSize = 14;

    /**/* w w  w .ja v a 2  s.  c o  m*/
     * @return String[0] is the SciNotes fonts and String[1] the non-SciNotes
     */
    public static String[][] getAllStylesSameWidthsFontsFamillyName() {
        String[] fontFamilly = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        List<String> all = new ArrayList();
        List<String> notAll = new ArrayList();
        for (int i = 0; i < fontFamilly.length; i++) {
            if (isAllStylesSameWidths(fontFamilly[i])) {
                all.add(fontFamilly[i]);
            } else {
                notAll.add(fontFamilly[i]);
            }
        }

        return new String[][] { all.toArray(new String[0]), notAll.toArray(new String[0]) };
    }

    /**
     * @param fontFamilly the font familly to test
     * @return true if each glyph has the same width in different styles
     */
    public static boolean isAllStylesSameWidths(String fontFamilly) {
        return isAllStylesSameWidths(new Font(fontFamilly, Font.PLAIN, defaultFontSize));
    }

    /**
     * @param font the font to test
     * @return true if each glyph has the same width in different styles
     */
    public static boolean isAllStylesSameWidths(Font font) {
        int[] style = new int[] { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC };
        FontMetrics fm = g2d.getFontMetrics(font.deriveFont(style[0]));
        int[] widths = fm.getWidths();

        for (int i = 1; i < style.length; i++) {
            fm = g2d.getFontMetrics(font.deriveFont(style[i]));
            int[] arr = fm.getWidths();
            // The range 33--126 corresponds to the usual characters in ASCII
            for (int j = 33; j < 126; j++) {
                if (arr[j] != widths[j]) {
                    return false;
                }
            }
        }

        return true;
    }
}

Related

  1. clipString(FontMetrics fm, String text, int availableWidth)
  2. clipString(FontMetrics metrics, int availableWidth, String fullText)
  3. clipString(Graphics g, String t, int width)
  4. cutString(String text, FontMetrics fontMetrics, int maxWidth)
  5. fitString(String s, Graphics2D g2, Rectangle2D rect)
  6. getCenterOffset(Graphics g, Font f, char ch)
  7. getComponentAverageCharacterWidth(Component component)
  8. getConsoleFontMetrics(Font font)
  9. getLineBreakIndex(Graphics g, String text, int index, double maxWidth)