📄 font.java
字号:
/* * @(#)Font.java 1.28 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.awt;import java.security.AccessController;import sun.security.action.GetPropertyAction;import java.util.StringTokenizer;import java.util.Locale;import java.util.Map;import java.util.Hashtable;import java.awt.font.TextAttribute;import java.text.AttributedCharacterIterator.Attribute;/** * A class that produces font objects. * * @version 1.2, 10/18/01 * @author Nicholas Allen * @since JDK1.0 */public class Font implements java.io.Serializable { /* * Constants to be used for styles. Can be combined to mix * styles. */ /** * The plain style constant. This style can be combined with * the other style constants for mixed styles. * @since JDK1.0 */ public static final int PLAIN = 0; /** * The bold style constant. This style can be combined with the * other style constants for mixed styles. * @since JDK1.0 */ public static final int BOLD = 1; /** * The italicized style constant. This style can be combined * with the other style constants for mixed styles. * @since JDK1.0 */ public static final int ITALIC = 2; /** * The platform specific family name of this font. */ transient private String family; /** * The logical name of this font. * @since JDK1.0 */ protected String name; /** * The style of the font. This is the sum of the * constants <code>PLAIN</code>, <code>BOLD</code>, * or <code>ITALIC</code>. */ protected int style; /** * The point size of this font. * @since JDK1.0 */ protected int size; /* * JDK 1.1 serialVersionUID */ private static final long serialVersionUID = -4206021311591459213L; /** Cached font metrics for this font. This should be initialised the first time the font metrics is obtained for this font. */ transient FontMetrics metrics; /** * A map of font attributes available in this font. * Attributes include things like ligatures and glyph substitution. * * @serial * @see #getAttributes() */ private Hashtable fRequestedAttributes; private static final Map EMPTY_MAP = new Hashtable(5, (float)0.9); private void initializeFont(Hashtable attributes) { if (attributes == null) { fRequestedAttributes = new Hashtable(5, (float)0.9); fRequestedAttributes.put(TextAttribute.FAMILY, name); fRequestedAttributes.put(TextAttribute.SIZE, new Float(size)); fRequestedAttributes.put(TextAttribute.WEIGHT, (style & BOLD) != 0 ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR); fRequestedAttributes.put(TextAttribute.POSTURE, (style & ITALIC) != 0 ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR); } } /** * Creates a new font with the specified name, style and point size. * @param name the font name * @param style the constant style used * @param size the point size of the font * @see Toolkit#getFontList * @since JDK1.0 */ public Font(String name, int style, int size) { this.name = name; if (this.name == null) { this.name = "Default"; } this.style = PLAIN; if (style == BOLD || style == ITALIC || style == (BOLD | ITALIC)) { this.style = style; } this.size = size; setFamily(); initializeFont(fRequestedAttributes); } /** * Creates a new <code>Font</code> with the specified attributes. * This <code>Font</code> only recognizes keys defined in * {@link TextAttribute} as attributes. If <code>attributes</code> * is <code>null</code>, a new <code>Font</code> is initialized * with default attributes. * @param attributes the attributes to assign to the new * <code>Font</code>, or <code>null</code> */ public Font(Map attributes) { this.name = "Default"; this.style = PLAIN; this.size = 12; if((attributes != null) && (!attributes.equals(EMPTY_MAP))) { Object obj; fRequestedAttributes = new Hashtable(attributes); if ((obj = attributes.get(TextAttribute.FAMILY)) != null) { this.name = (String)obj; } if ((obj = attributes.get(TextAttribute.WEIGHT)) != null) { if(obj.equals(TextAttribute.WEIGHT_BOLD)) { this.style |= BOLD; } } if ((obj = attributes.get(TextAttribute.POSTURE)) != null) { if(obj.equals(TextAttribute.POSTURE_OBLIQUE)) { this.style |= ITALIC; } } if ((obj = attributes.get(TextAttribute.SIZE)) != null) { float pointSize = ((Float)obj).floatValue(); this.size = (int)(pointSize + 0.5); } } setFamily(); initializeFont(fRequestedAttributes); } /** * Returns a <code>Font</code> appropriate to this attribute set. * * @param attributes the attributes to assign to the new * <code>Font</code> * @return a new <code>Font</code> created with the specified * attributes * @since 1.2 * @see java.awt.font.TextAttribute */ public static Font getFont(Map attributes) { Font font = (Font)attributes.get(TextAttribute.FONT); if (font != null) { return font; } return new Font(attributes); } /** * Returns a map of font attributes available in this * <code>Font</code>. Attributes include things like ligatures and * glyph substitution. * @return the attributes map of this <code>Font</code>. */ public Map getAttributes() { return (Map)fRequestedAttributes.clone(); } /** * Returns the keys of all the attributes supported by this * <code>Font</code>. These attributes can be used to derive other * fonts. * @return an array containing the keys of all the attributes * supported by this <code>Font</code>. * @since 1.2 */ public Attribute[] getAvailableAttributes(){ Attribute attributes[] = { TextAttribute.FAMILY, TextAttribute.WEIGHT, TextAttribute.POSTURE, TextAttribute.SIZE, }; return attributes; } /** * Gets the platform specific family name of the font. Use the * <code>getName</code> method to get the logical name of the font. * @return a string, the platform specific family name. * @see java.awt.Font#getName * @since JDK1.0 */ public String getFamily() { return family; } /** * Gets the logical name of the font. * @return a string, the logical name of the font. * @see #getFamily * @since JDK1.0 */ public String getName() { return name; } /** * Gets the style of the font. * @return the style of this font. * @see #isPlain * @see #isBold * @see #isItalic * @since JDK1.0 */ public int getStyle() { return style; } /** * Gets the point size of the font. * @return the point size of this font. * @since JDK1.0 */ public int getSize() { return size; } /** * Indicates whether the font's style is plain. * @return <code>true</code> if the font is neither * bold nor italic; <code>false</code> otherwise. * @see java.awt.Font#getStyle * @since JDK1.0 */ public boolean isPlain() { return style == 0; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -