package ij.util;
import java.text.*;
import java.awt.*;
public class FontUtil {
public static Font getFont(String fontFamilyName, int style, float size) {
Font font = new Font(fontFamilyName, style, (int)size);
if (!font.getFamily().startsWith(fontFamilyName)) {
String[] similarFonts = getSimilarFontsList(fontFamilyName);
font = getFont(similarFonts, style, (int)size);
}
if (size != (int)size)
font = font.deriveFont(size);
return font;
}
private static Font getFont(String[] fontNames, int style, int size) {
int iSize = (int)size;
Font font = null;
for (String fontName : fontNames) {
font = new Font(fontName, style, iSize);
if (font.getFamily().startsWith(fontName))
break;
}
return font;
}
public static String[] getSimilarFontsList(String fontFamily) {
if (fontFamily.indexOf("Times")>=0 || fontFamily.indexOf("Serif")>=0)
return new String[]{"Times New Roman", "Times", "Liberation Serif", "Serif"};
else if (fontFamily.indexOf("Arial")>=0 || fontFamily.indexOf("Helvetica")>=0 || fontFamily.indexOf("Sans")>=0)
return new String[]{"Arial", "Helvetica", "Helvetica Neue", "Liberation Sans", "SansSerif"};
else if (fontFamily.indexOf("Courier")>=0 || fontFamily.indexOf("Mono")>=0)
return new String[]{"Courier New", "Courier", "Liberation Mono", "Monospaced"};
else
return new String[]{fontFamily};
}
}