下面列出了javax.swing.text.StyleContext#getFont ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Font loadByStr(String fontDesc) {
String[] parts = fontDesc.split("-");
if (parts.length != 3) {
throw new JadxRuntimeException("Unsupported font description format: " + fontDesc);
}
String name = parts[0];
int style = parseFontStyle(parts[1]);
int size = Integer.parseInt(parts[2]);
StyleContext sc = StyleContext.getDefaultStyleContext();
Font font = sc.getFont(name, style, size);
if (font == null) {
throw new JadxRuntimeException("Font not found: " + fontDesc);
}
return font;
}
/**
* Returns the font for the values in the passed in AttributeSet.
* It is assumed the keys will be CSS.Attribute keys.
* <code>sc</code> is the StyleContext that will be messaged to get
* the font once the size, name and style have been determined.
*/
Font getFont(StyleContext sc, AttributeSet a, int defaultSize, StyleSheet ss) {
ss = getStyleSheet(ss);
int size = getFontSize(a, defaultSize, ss);
/*
* If the vertical alignment is set to either superscript or
* subscript we reduce the font size by 2 points.
*/
StringValue vAlignV = (StringValue)a.getAttribute
(CSS.Attribute.VERTICAL_ALIGN);
if ((vAlignV != null)) {
String vAlign = vAlignV.toString();
if ((vAlign.indexOf("sup") >= 0) ||
(vAlign.indexOf("sub") >= 0)) {
size -= 2;
}
}
FontFamily familyValue = (FontFamily)a.getAttribute
(CSS.Attribute.FONT_FAMILY);
String family = (familyValue != null) ? familyValue.getValue() :
Font.SANS_SERIF;
int style = Font.PLAIN;
FontWeight weightValue = (FontWeight) a.getAttribute
(CSS.Attribute.FONT_WEIGHT);
if ((weightValue != null) && (weightValue.getValue() > 400)) {
style |= Font.BOLD;
}
Object fs = a.getAttribute(CSS.Attribute.FONT_STYLE);
if ((fs != null) && (fs.toString().indexOf("italic") >= 0)) {
style |= Font.ITALIC;
}
if (family.equalsIgnoreCase("monospace")) {
family = Font.MONOSPACED;
}
Font f = sc.getFont(family, style, size);
if (f == null
|| (f.getFamily().equals(Font.DIALOG)
&& ! family.equalsIgnoreCase(Font.DIALOG))) {
family = Font.SANS_SERIF;
f = sc.getFont(family, style, size);
}
return f;
}