📄 fontmanager.java
字号:
package ergo.ui;
// $Id: FontManager.java,v 1.2 1999/08/13 01:20:07 sigue Exp $
/*
* Copyright (C) 1999 Carl L. Gay and Antranig M. Basman.
* See the file copyright.txt, distributed with this software,
* for further information.
*/
import ergo.util.*;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Toolkit;
/* Font Manager class for options. Maintains a default font, which should
* precede all other fonts in the INI file, which all subsequent fonts are
* resolved against. Also maintains a "hyper-default" font, in the case
* not even the default font is available.
*/
class FontManager {
private static Font defaultFont = new Font("SansSerif", Font.PLAIN, 12);
private static FontMetrics metrics
= Toolkit.getDefaultToolkit().getFontMetrics(defaultFont);
public static Font getDefaultFont () {
return defaultFont;
}
public static void setDefaultFont (Font f) {
if (f != null)
defaultFont = f;
}
public static FontMetrics getFontMetrics () {
return metrics;
}
public static boolean isValidFontName (String name) {
String[] fontnames = Toolkit.getDefaultToolkit().getFontList();
for (int i = 0; i < fontnames.length; i++) {
//Debug.println("fontnames[" + i + "] = " + fontnames[i]);
if (name.equalsIgnoreCase(fontnames[i]))
return true;
}
return false;
}
// A font spec is a string specifying the name, style and size of
// a font, with spaces characters separating the components.
// e.g., "courier plain 12"
public static Font resolve (FontSpec f) {
try {
String name = defaultFont.getName();
int style = defaultFont.getStyle();
int size = defaultFont.getSize();
// Parse the name
if (! "*".equalsIgnoreCase(f.getName())) {
if (isValidFontName(f.getName()))
name = f.getName();
else {
// IE 4.0 doesn't recognize the newly sanctioned font names.
// Replace the new ones with the old ones, if they're valid.
if (f.getName().equalsIgnoreCase("Monospaced")
&& isValidFontName("Courier"))
name = "Courier";
else if (f.getName().equalsIgnoreCase("SansSerif")
&& isValidFontName("Helvetica"))
name = "Helvetica";
else if (f.getName().equalsIgnoreCase("Serif")
&& isValidFontName("TimesRoman"))
name = "TimesRoman";
else
Debug.println("Unrecognized font name (" + f.getName()
+ ") in FontSpec. Using default.");
}
}
// Parse and verify the style
if ("PLAIN".equalsIgnoreCase(f.getStyle()))
style = Font.PLAIN;
else if ("BOLD".equalsIgnoreCase(f.getStyle()))
style = Font.BOLD;
else if ("ITALIC".equalsIgnoreCase(f.getStyle()))
style = Font.ITALIC;
else if ("BOLDITALIC".equalsIgnoreCase(f.getStyle()))
style = Font.BOLD + Font.ITALIC;
else if ("*".equalsIgnoreCase(f.getStyle()))
{} // style already initialized to defaultFont.getStyle() above.
else {
Debug.println("Unrecognized font style (" + f.getStyle()
+ ") in FontSpec. Using default.");
}
// Parse and merge the size
if (! "*".equalsIgnoreCase(f.getSize())) {
try {
int sz = Integer.valueOf(f.getSize()).intValue();
char relativeSize = f.getSize().charAt(0);
if (relativeSize == '-')
size -= sz;
else if (relativeSize == '+')
size += sz;
else if (sz < 3 || sz > 32)
Debug.println("Invalid font size (" + f.getSize()
+ ") in FontSpec. Using default.");
else
size = sz;
}
catch (NumberFormatException nfe) {
Debug.println("Illegal font size (" + f.getSize()
+ ") specified in FontSpec. Using default.");
}
}
return new Font(name, style, size);
}
catch (java.util.NoSuchElementException e) { }
return defaultFont;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -