下面列出了java.awt.Font#SANS_SERIF 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private BufferedImage createImage(String label) {
Font font = new Font(Font.SANS_SERIF, Font.BOLD, SIZE);
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = new TextLayout(label, font, frc);
Rectangle r = layout.getPixelBounds(null, 0, 0);
//System.out.println(r);
BufferedImage bi = new BufferedImage(
r.width + 1, r.height + 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(getBackground());
g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());
g2d.setColor(getForeground());
layout.draw(g2d, 0, -r.y);
g2d.dispose();
return bi;
}
/**
* Creates a font based on OCR font information.
*
* @param info OCR-based font information
*/
public TextFont (FontInfo info)
{
this(
info.isSerif ? Font.SERIF
: (info.isMonospace ? Font.MONOSPACED : Font.SANS_SERIF),
(info.isBold ? Font.BOLD : 0) | (info.isItalic ? Font.ITALIC : 0),
info.pointsize);
}
public CollagePanel(ChatPanel chat)
{
this.chat = chat;
this.font = new Font(Font.SANS_SERIF, Font.PLAIN, 14);
this.collageChooser = new JFileChooser();
FileFilter pngFileFilter = new FileNameExtensionFilter("PNG Image (*.png)", "png");
this.collageChooser.setFileFilter(pngFileFilter);
images = new ArrayList<BufferedImage>();
build();
}
protected void doTest() {
Font f = new Font(Font.SANS_SERIF, Font.ITALIC, 24);
f.getNumGlyphs();
}
/**
* Get a list of installed fonts in the requested {@link Locale}.
* The list contains the fonts Family Names.
* If Locale is null, the default locale is used.
*
* @param requestedLocale, if null the default locale is used.
* @return list of installed fonts in the system.
*/
public String[] getInstalledFontFamilyNames(Locale requestedLocale) {
if (requestedLocale == null) {
requestedLocale = Locale.getDefault();
}
if (allFamilies != null && lastDefaultLocale != null &&
requestedLocale.equals(lastDefaultLocale)) {
String[] copyFamilies = new String[allFamilies.length];
System.arraycopy(allFamilies, 0, copyFamilies,
0, allFamilies.length);
return copyFamilies;
}
TreeMap<String,String> familyNames = new TreeMap<String,String>();
// these names are always there and aren't localised
String str;
str = Font.SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.SANS_SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.MONOSPACED; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG_INPUT; familyNames.put(str.toLowerCase(), str);
/* Platform APIs may be used to get the set of available family
* names for the current default locale so long as it is the same
* as the start-up system locale, rather than loading all fonts.
*/
if (requestedLocale.equals(getSystemStartupLocale()) &&
getFamilyNamesFromPlatform(familyNames, requestedLocale)) {
/* Augment platform names with JRE font family names */
getJREFontFamilyNames(familyNames, requestedLocale);
} else {
loadFontFiles();
Font2D[] physicalfonts = getPhysicalFonts();
for (int i=0; i < physicalfonts.length; i++) {
if (!(physicalfonts[i] instanceof NativeFont)) {
String name =
physicalfonts[i].getFamilyName(requestedLocale);
familyNames.put(name.toLowerCase(requestedLocale), name);
}
}
}
// Add any native font family names here
addNativeFontFamilyNames(familyNames, requestedLocale);
String[] retval = new String[familyNames.size()];
Object [] keyNames = familyNames.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)familyNames.get(keyNames[i]);
}
if (requestedLocale.equals(Locale.getDefault())) {
lastDefaultLocale = requestedLocale;
allFamilies = new String[retval.length];
System.arraycopy(retval, 0, allFamilies, 0, allFamilies.length);
}
return retval;
}
Object parseCssValue(String value) {
int cIndex = value.indexOf(',');
FontFamily ff = new FontFamily();
ff.svalue = value;
ff.family = null;
if (cIndex == -1) {
setFontName(ff, value);
}
else {
boolean done = false;
int lastIndex;
int length = value.length();
cIndex = 0;
while (!done) {
// skip ws.
while (cIndex < length &&
Character.isWhitespace(value.charAt(cIndex)))
cIndex++;
// Find next ','
lastIndex = cIndex;
cIndex = value.indexOf(',', cIndex);
if (cIndex == -1) {
cIndex = length;
}
if (lastIndex < length) {
if (lastIndex != cIndex) {
int lastCharIndex = cIndex;
if (cIndex > 0 && value.charAt(cIndex - 1) == ' '){
lastCharIndex--;
}
setFontName(ff, value.substring
(lastIndex, lastCharIndex));
done = (ff.family != null);
}
cIndex++;
}
else {
done = true;
}
}
}
if (ff.family == null) {
ff.family = Font.SANS_SERIF;
}
return ff;
}
public static PDType1Font getPDFont(Font javaFont) {
switch (javaFont.getFamily()) {
case Font.SERIF:
if (javaFont.isPlain()) {
return PDType1Font.TIMES_ROMAN;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.TIMES_BOLD_ITALIC;
} else {
return PDType1Font.TIMES_BOLD;
}
} else {
return PDType1Font.TIMES_ITALIC;
}
case Font.SANS_SERIF:
if (javaFont.isPlain()) {
return PDType1Font.HELVETICA;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.HELVETICA_BOLD_OBLIQUE;
} else {
return PDType1Font.HELVETICA_BOLD;
}
} else {
return PDType1Font.HELVETICA_OBLIQUE;
}
case Font.MONOSPACED:
if (javaFont.isPlain()) {
return PDType1Font.COURIER;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.COURIER_BOLD_OBLIQUE;
} else {
return PDType1Font.COURIER_BOLD;
}
} else {
return PDType1Font.COURIER_OBLIQUE;
}
case Font.DIALOG:
case Font.DIALOG_INPUT:
return PDType1Font.SYMBOL;
default:
throw new DSSException("The font is not supported! Please use DSSFileFont implementation for custom fonts.");
}
}
protected void doTest() {
Font f = new Font(Font.SANS_SERIF, Font.ITALIC, 24);
f.getNumGlyphs();
}
/**
* 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;
}
/**
* Get a list of installed fonts in the requested {@link Locale}.
* The list contains the fonts Family Names.
* If Locale is null, the default locale is used.
*
* @param requestedLocale, if null the default locale is used.
* @return list of installed fonts in the system.
*/
public String[] getInstalledFontFamilyNames(Locale requestedLocale) {
if (requestedLocale == null) {
requestedLocale = Locale.getDefault();
}
if (allFamilies != null && lastDefaultLocale != null &&
requestedLocale.equals(lastDefaultLocale)) {
String[] copyFamilies = new String[allFamilies.length];
System.arraycopy(allFamilies, 0, copyFamilies,
0, allFamilies.length);
return copyFamilies;
}
TreeMap<String,String> familyNames = new TreeMap<String,String>();
// these names are always there and aren't localised
String str;
str = Font.SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.SANS_SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.MONOSPACED; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG_INPUT; familyNames.put(str.toLowerCase(), str);
/* Platform APIs may be used to get the set of available family
* names for the current default locale so long as it is the same
* as the start-up system locale, rather than loading all fonts.
*/
if (requestedLocale.equals(getSystemStartupLocale()) &&
getFamilyNamesFromPlatform(familyNames, requestedLocale)) {
/* Augment platform names with JRE font family names */
getJREFontFamilyNames(familyNames, requestedLocale);
} else {
loadFontFiles();
Font2D[] physicalfonts = getPhysicalFonts();
for (int i=0; i < physicalfonts.length; i++) {
if (!(physicalfonts[i] instanceof NativeFont)) {
String name =
physicalfonts[i].getFamilyName(requestedLocale);
familyNames.put(name.toLowerCase(requestedLocale), name);
}
}
}
// Add any native font family names here
addNativeFontFamilyNames(familyNames, requestedLocale);
String[] retval = new String[familyNames.size()];
Object [] keyNames = familyNames.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)familyNames.get(keyNames[i]);
}
if (requestedLocale.equals(Locale.getDefault())) {
lastDefaultLocale = requestedLocale;
allFamilies = new String[retval.length];
System.arraycopy(retval, 0, allFamilies, 0, allFamilies.length);
}
return retval;
}
protected void doTest() {
Font f = new Font(Font.SANS_SERIF, Font.ITALIC, 24);
f.getNumGlyphs();
}
/**
* Get a list of installed fonts in the requested {@link Locale}.
* The list contains the fonts Family Names.
* If Locale is null, the default locale is used.
*
* @param requestedLocale, if null the default locale is used.
* @return list of installed fonts in the system.
*/
public String[] getInstalledFontFamilyNames(Locale requestedLocale) {
if (requestedLocale == null) {
requestedLocale = Locale.getDefault();
}
if (allFamilies != null && lastDefaultLocale != null &&
requestedLocale.equals(lastDefaultLocale)) {
String[] copyFamilies = new String[allFamilies.length];
System.arraycopy(allFamilies, 0, copyFamilies,
0, allFamilies.length);
return copyFamilies;
}
TreeMap<String,String> familyNames = new TreeMap<String,String>();
// these names are always there and aren't localised
String str;
str = Font.SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.SANS_SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.MONOSPACED; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG_INPUT; familyNames.put(str.toLowerCase(), str);
/* Platform APIs may be used to get the set of available family
* names for the current default locale so long as it is the same
* as the start-up system locale, rather than loading all fonts.
*/
if (requestedLocale.equals(getSystemStartupLocale()) &&
getFamilyNamesFromPlatform(familyNames, requestedLocale)) {
/* Augment platform names with JRE font family names */
getJREFontFamilyNames(familyNames, requestedLocale);
} else {
loadFontFiles();
Font2D[] physicalfonts = getPhysicalFonts();
for (int i=0; i < physicalfonts.length; i++) {
if (!(physicalfonts[i] instanceof NativeFont)) {
String name =
physicalfonts[i].getFamilyName(requestedLocale);
familyNames.put(name.toLowerCase(requestedLocale), name);
}
}
}
// Add any native font family names here
addNativeFontFamilyNames(familyNames, requestedLocale);
String[] retval = new String[familyNames.size()];
Object [] keyNames = familyNames.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)familyNames.get(keyNames[i]);
}
if (requestedLocale.equals(Locale.getDefault())) {
lastDefaultLocale = requestedLocale;
allFamilies = new String[retval.length];
System.arraycopy(retval, 0, allFamilies, 0, allFamilies.length);
}
return retval;
}
private static void makeText(Graphics2D graphics, ImageSize maxSize, Random rand) {
switchColor(graphics, rand);
String fontName = Font.SANS_SERIF;
switch (rand.nextInt(4)) {
case 0:
fontName = Font.SANS_SERIF;
break;
case 1:
fontName = Font.MONOSPACED;
break;
case 2:
fontName = Font.SERIF;
break;
case 3:
fontName = Font.DIALOG;
break;
default:
fontName = Font.SANS_SERIF;
break;
}
int fontStyle = Font.PLAIN;
switch (rand.nextInt(3)) {
case 0:
fontStyle = Font.PLAIN;
break;
case 1:
fontStyle = Font.BOLD;
break;
case 2:
fontStyle = Font.ITALIC;
break;
default:
fontStyle = Font.PLAIN;
break;
}
int fontSize = rand.nextInt(MAX_FONT_SIZE + 1);
graphics.setFont(new Font(fontName, fontStyle, fontSize));
int textLength = rand.nextInt(MAX_TEXT_LENGTH + 1);
String str = Stream.generate(() -> rand.nextInt(MAX_CHAR_SIZE)).limit(textLength)
.map(i -> (char) i.intValue())
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
graphics.drawString(str, rand.nextInt(maxSize.getWidth()), rand.nextInt(maxSize.getHeight()));
}
/**
* Get a list of installed fonts in the requested {@link Locale}.
* The list contains the fonts Family Names.
* If Locale is null, the default locale is used.
*
* @param requestedLocale, if null the default locale is used.
* @return list of installed fonts in the system.
*/
public String[] getInstalledFontFamilyNames(Locale requestedLocale) {
if (requestedLocale == null) {
requestedLocale = Locale.getDefault();
}
if (allFamilies != null && lastDefaultLocale != null &&
requestedLocale.equals(lastDefaultLocale)) {
String[] copyFamilies = new String[allFamilies.length];
System.arraycopy(allFamilies, 0, copyFamilies,
0, allFamilies.length);
return copyFamilies;
}
TreeMap<String,String> familyNames = new TreeMap<String,String>();
// these names are always there and aren't localised
String str;
str = Font.SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.SANS_SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.MONOSPACED; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG_INPUT; familyNames.put(str.toLowerCase(), str);
/* Platform APIs may be used to get the set of available family
* names for the current default locale so long as it is the same
* as the start-up system locale, rather than loading all fonts.
*/
if (requestedLocale.equals(getSystemStartupLocale()) &&
getFamilyNamesFromPlatform(familyNames, requestedLocale)) {
/* Augment platform names with JRE font family names */
getJREFontFamilyNames(familyNames, requestedLocale);
} else {
loadFontFiles();
Font2D[] physicalfonts = getPhysicalFonts();
for (int i=0; i < physicalfonts.length; i++) {
if (!(physicalfonts[i] instanceof NativeFont)) {
String name =
physicalfonts[i].getFamilyName(requestedLocale);
familyNames.put(name.toLowerCase(requestedLocale), name);
}
}
}
// Add any native font family names here
addNativeFontFamilyNames(familyNames, requestedLocale);
String[] retval = new String[familyNames.size()];
Object [] keyNames = familyNames.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)familyNames.get(keyNames[i]);
}
if (requestedLocale.equals(Locale.getDefault())) {
lastDefaultLocale = requestedLocale;
allFamilies = new String[retval.length];
System.arraycopy(retval, 0, allFamilies, 0, allFamilies.length);
}
return retval;
}
/**
* 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 superscirpt 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;
}
/**
* Get a list of installed fonts in the requested {@link Locale}.
* The list contains the fonts Family Names.
* If Locale is null, the default locale is used.
*
* @param requestedLocale, if null the default locale is used.
* @return list of installed fonts in the system.
*/
public String[] getInstalledFontFamilyNames(Locale requestedLocale) {
if (requestedLocale == null) {
requestedLocale = Locale.getDefault();
}
if (allFamilies != null && lastDefaultLocale != null &&
requestedLocale.equals(lastDefaultLocale)) {
String[] copyFamilies = new String[allFamilies.length];
System.arraycopy(allFamilies, 0, copyFamilies,
0, allFamilies.length);
return copyFamilies;
}
TreeMap<String,String> familyNames = new TreeMap<String,String>();
// these names are always there and aren't localised
String str;
str = Font.SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.SANS_SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.MONOSPACED; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG_INPUT; familyNames.put(str.toLowerCase(), str);
/* Platform APIs may be used to get the set of available family
* names for the current default locale so long as it is the same
* as the start-up system locale, rather than loading all fonts.
*/
if (requestedLocale.equals(getSystemStartupLocale()) &&
getFamilyNamesFromPlatform(familyNames, requestedLocale)) {
/* Augment platform names with JRE font family names */
getJREFontFamilyNames(familyNames, requestedLocale);
} else {
loadFontFiles();
Font2D[] physicalfonts = getPhysicalFonts();
for (int i=0; i < physicalfonts.length; i++) {
if (!(physicalfonts[i] instanceof NativeFont)) {
String name =
physicalfonts[i].getFamilyName(requestedLocale);
familyNames.put(name.toLowerCase(requestedLocale), name);
}
}
}
// Add any native font family names here
addNativeFontFamilyNames(familyNames, requestedLocale);
String[] retval = new String[familyNames.size()];
Object [] keyNames = familyNames.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)familyNames.get(keyNames[i]);
}
if (requestedLocale.equals(Locale.getDefault())) {
lastDefaultLocale = requestedLocale;
allFamilies = new String[retval.length];
System.arraycopy(retval, 0, allFamilies, 0, allFamilies.length);
}
return retval;
}
/**
* 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 superscirpt 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;
}
/**
* 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 superscirpt 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;
}
Object parseCssValue(String value) {
int cIndex = value.indexOf(',');
FontFamily ff = new FontFamily();
ff.svalue = value;
ff.family = null;
if (cIndex == -1) {
setFontName(ff, value);
}
else {
boolean done = false;
int lastIndex;
int length = value.length();
cIndex = 0;
while (!done) {
// skip ws.
while (cIndex < length &&
Character.isWhitespace(value.charAt(cIndex)))
cIndex++;
// Find next ','
lastIndex = cIndex;
cIndex = value.indexOf(',', cIndex);
if (cIndex == -1) {
cIndex = length;
}
if (lastIndex < length) {
if (lastIndex != cIndex) {
int lastCharIndex = cIndex;
if (cIndex > 0 && value.charAt(cIndex - 1) == ' '){
lastCharIndex--;
}
setFontName(ff, value.substring
(lastIndex, lastCharIndex));
done = (ff.family != null);
}
cIndex++;
}
else {
done = true;
}
}
}
if (ff.family == null) {
ff.family = Font.SANS_SERIF;
}
return ff;
}
/**
* Get a list of installed fonts in the requested {@link Locale}.
* The list contains the fonts Family Names.
* If Locale is null, the default locale is used.
*
* @param requestedLocale, if null the default locale is used.
* @return list of installed fonts in the system.
*/
public String[] getInstalledFontFamilyNames(Locale requestedLocale) {
if (requestedLocale == null) {
requestedLocale = Locale.getDefault();
}
if (allFamilies != null && lastDefaultLocale != null &&
requestedLocale.equals(lastDefaultLocale)) {
String[] copyFamilies = new String[allFamilies.length];
System.arraycopy(allFamilies, 0, copyFamilies,
0, allFamilies.length);
return copyFamilies;
}
TreeMap<String,String> familyNames = new TreeMap<String,String>();
// these names are always there and aren't localised
String str;
str = Font.SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.SANS_SERIF; familyNames.put(str.toLowerCase(), str);
str = Font.MONOSPACED; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG; familyNames.put(str.toLowerCase(), str);
str = Font.DIALOG_INPUT; familyNames.put(str.toLowerCase(), str);
/* Platform APIs may be used to get the set of available family
* names for the current default locale so long as it is the same
* as the start-up system locale, rather than loading all fonts.
*/
if (requestedLocale.equals(getSystemStartupLocale()) &&
getFamilyNamesFromPlatform(familyNames, requestedLocale)) {
/* Augment platform names with JRE font family names */
getJREFontFamilyNames(familyNames, requestedLocale);
} else {
loadFontFiles();
Font2D[] physicalfonts = getPhysicalFonts();
for (int i=0; i < physicalfonts.length; i++) {
if (!(physicalfonts[i] instanceof NativeFont)) {
String name =
physicalfonts[i].getFamilyName(requestedLocale);
familyNames.put(name.toLowerCase(requestedLocale), name);
}
}
}
// Add any native font family names here
addNativeFontFamilyNames(familyNames, requestedLocale);
String[] retval = new String[familyNames.size()];
Object [] keyNames = familyNames.keySet().toArray();
for (int i=0; i < keyNames.length; i++) {
retval[i] = (String)familyNames.get(keyNames[i]);
}
if (requestedLocale.equals(Locale.getDefault())) {
lastDefaultLocale = requestedLocale;
allFamilies = new String[retval.length];
System.arraycopy(retval, 0, allFamilies, 0, allFamilies.length);
}
return retval;
}