下面列出了java.awt.Font#getFont ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(
Map<? extends Attribute, ?> attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
static Font getFontAtCurrentPos(AttributedCharacterIterator aci) {
Object value = aci.getAttribute(TextAttribute.FONT);
if (value != null) {
return (Font) value;
}
if (aci.getAttribute(TextAttribute.FAMILY) != null) {
return Font.getFont(aci.getAttributes());
}
int ch = CodePointIterator.create(aci).next();
if (ch != CodePointIterator.DONE) {
FontResolver resolver = FontResolver.getInstance();
return resolver.getFont(resolver.getFontIndex(ch), aci.getAttributes());
}
return null;
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(
Map<? extends Attribute, ?> attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(
Map<? extends Attribute, ?> attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(
Map<? extends Attribute, ?> attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
static Font getFontAtCurrentPos(AttributedCharacterIterator aci) {
Object value = aci.getAttribute(TextAttribute.FONT);
if (value != null) {
return (Font) value;
}
if (aci.getAttribute(TextAttribute.FAMILY) != null) {
return Font.getFont(aci.getAttributes());
}
int ch = CodePointIterator.create(aci).next();
if (ch != CodePointIterator.DONE) {
FontResolver resolver = FontResolver.getInstance();
return resolver.getFont(resolver.getFontIndex(ch), aci.getAttributes());
}
return null;
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(
Map<? extends Attribute, ?> attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
static Font getFontAtCurrentPos(AttributedCharacterIterator aci) {
Object value = aci.getAttribute(TextAttribute.FONT);
if (value != null) {
return (Font) value;
}
if (aci.getAttribute(TextAttribute.FAMILY) != null) {
return Font.getFont(aci.getAttributes());
}
int ch = CodePointIterator.create(aci).next();
if (ch != CodePointIterator.DONE) {
FontResolver resolver = FontResolver.getInstance();
return resolver.getFont(resolver.getFontIndex(ch), aci.getAttributes());
}
return null;
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
static Font getFontAtCurrentPos(AttributedCharacterIterator aci) {
Object value = aci.getAttribute(TextAttribute.FONT);
if (value != null) {
return (Font) value;
}
if (aci.getAttribute(TextAttribute.FAMILY) != null) {
return Font.getFont(aci.getAttributes());
}
int ch = CodePointIterator.create(aci).next();
if (ch != CodePointIterator.DONE) {
FontResolver resolver = FontResolver.getInstance();
return resolver.getFont(resolver.getFontIndex(ch), aci.getAttributes());
}
return null;
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(Map attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
/**
* Extract a GraphicAttribute or Font from the given attributes.
* If attributes does not contain a GraphicAttribute, Font, or
* Font family entry this method returns null.
*/
private static Object getGraphicOrFont(
Map<? extends Attribute, ?> attributes) {
Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
if (value != null) {
return value;
}
value = attributes.get(TextAttribute.FONT);
if (value != null) {
return value;
}
if (attributes.get(TextAttribute.FAMILY) != null) {
return Font.getFont(attributes);
}
else {
return null;
}
}
private static Font getFontFromFile(String relativePatch)
{
Font f = null;
try{
f = Font.createFont(Font.TRUETYPE_FONT
, FontObjectFactory.class.getResourceAsStream(relativePatch));
}
catch (Exception e){
f = Font.getFont("Arial");
}
return f;
}
/**
* The fontNames can contain a comma separated list of font names. This function
* will parse that list trying to match the first font name that can be found in
* the list against the list of available font families. Spaces around font
* names are stripped. For example:
*
* <pre>
* 'Input Mono, Lucinda Sans Mono, Courier New, Courier, monofont'
* </pre>
* <p>
* A special case is when a font name starts with a $, in that case the name is
* looked up in the System properties. .
* <p>
* If none of the names can be found, the last font is returned as a desperate
* last attempt.
*
* @param fontNames comma separated list of font names
**/
public static synchronized String matchBestFontName(String fontNames) {
String[] names = fontNames.trim().split("\\s*,\\s*");
if (availableFontNames.isEmpty()) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] availableFontFamilyNames = ge.getAvailableFontFamilyNames();
for (String availableName : availableFontFamilyNames) {
availableFontNames.put(toSoundex(availableName), availableName);
}
}
for (String name : names) {
if (name.startsWith("$")) {
name = name.substring(1);
Font font = Font.getFont(name);
if (font != null)
return font.getFontName();
} else {
String soundex = toSoundex(name);
String fontName = availableFontNames.get(soundex);
if (fontName != null)
return name;
}
}
return names[names.length - 1];
}
protected Font loadFont(Map<Attribute, Object> textAttributes)
{
// check bundled fonts
FontUtil fontUtil = FontUtil.getInstance(context.getJasperReportsContext());
Font font = fontUtil.getAwtFontFromBundles(fontKey.fontAttribute, fontKey.style, fontKey.size, fontKey.locale, false);
if (font == null)
{
// checking AWT font
fontUtil.checkAwtFont(fontKey.fontAttribute.getFamily(), context.isIgnoreMissingFont());
// creating AWT font
// FIXME using the current text attributes might be slightly dangerous as we are sharing font metrics
font = Font.getFont(textAttributes);
}
return font;
}
protected Expression instantiate(Object oldInstance, Encoder out) {
Font font = (Font) oldInstance;
int count = 0;
String family = null;
int style = Font.PLAIN;
int size = 12;
Map<TextAttribute, ?> basic = font.getAttributes();
Map<TextAttribute, Object> clone = new HashMap<>(basic.size());
for (TextAttribute key : basic.keySet()) {
Object value = basic.get(key);
if (value != null) {
clone.put(key, value);
}
if (key == TextAttribute.FAMILY) {
if (value instanceof String) {
count++;
family = (String) value;
}
}
else if (key == TextAttribute.WEIGHT) {
if (TextAttribute.WEIGHT_REGULAR.equals(value)) {
count++;
} else if (TextAttribute.WEIGHT_BOLD.equals(value)) {
count++;
style |= Font.BOLD;
}
}
else if (key == TextAttribute.POSTURE) {
if (TextAttribute.POSTURE_REGULAR.equals(value)) {
count++;
} else if (TextAttribute.POSTURE_OBLIQUE.equals(value)) {
count++;
style |= Font.ITALIC;
}
} else if (key == TextAttribute.SIZE) {
if (value instanceof Number) {
Number number = (Number) value;
size = number.intValue();
if (size == number.floatValue()) {
count++;
}
}
}
}
Class<?> type = font.getClass();
if (count == clone.size()) {
return new Expression(font, type, "new", new Object[]{family, style, size});
}
if (type == Font.class) {
return new Expression(font, type, "getFont", new Object[]{clone});
}
return new Expression(font, type, "new", new Object[]{Font.getFont(clone)});
}