?? font.java
字號:
/* Font.java -- Font object Copyright (C) 1999, 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import gnu.java.awt.ClasspathToolkit;import gnu.java.awt.peer.ClasspathFontPeer;import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.font.LineMetrics;import java.awt.font.TextLayout;import java.awt.geom.AffineTransform;import java.awt.geom.Rectangle2D;import java.awt.peer.FontPeer;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.Serializable;import java.text.AttributedCharacterIterator;import java.text.CharacterIterator;import java.text.StringCharacterIterator;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.StringTokenizer;/** * This class represents a windowing system font. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Warren Levy (warrenl@cygnus.com) * @author Graydon Hoare (graydon@redhat.com) */public class Font implements Serializable{ /** * Constant indicating a "plain" font. */ public static final int PLAIN = 0; /** * Constant indicating a "bold" font. */ public static final int BOLD = 1; /** * Constant indicating an "italic" font. */ public static final int ITALIC = 2; /** * Constant indicating the baseline mode characteristic of Roman. */ public static final int ROMAN_BASELINE = 0; /** * Constant indicating the baseline mode characteristic of Chinese. */ public static final int CENTER_BASELINE = 1; /** * Constant indicating the baseline mode characteristic of Devanigri. */ public static final int HANGING_BASELINE = 2; /** * Indicates to <code>createFont</code> that the supplied font data * is in TrueType format. * * <p><em>Specification Note:</em> The Sun JavaDoc for J2SE 1.4 does * not indicate whether this value also subsumes OpenType. OpenType * is essentially the same format as TrueType, but allows to define * glyph shapes in the same way as PostScript, using cubic bezier * curves. * * @since 1.3 */ public static final int TRUETYPE_FONT = 0; /** * A flag for <code>layoutGlyphVector</code>, indicating that the * orientation of a text run is from left to right. * * @since 1.4 */ public static final int LAYOUT_LEFT_TO_RIGHT = 0; /** * A flag for <code>layoutGlyphVector</code>, indicating that the * orientation of a text run is from right to left. * * @since 1.4 */ public static final int LAYOUT_RIGHT_TO_LEFT = 1; /** * A flag for <code>layoutGlyphVector</code>, indicating that the * text does not contain valid characters before the * <code>start</code> position. If this flag is set, * <code>layoutGlyphVector</code> does not examine the text before * <code>start</code>, even if this would be necessary to select the * correct glyphs (e.g., for Arabic text). * * @since 1.4 */ public static final int LAYOUT_NO_START_CONTEXT = 2; /** * A flag for <code>layoutGlyphVector</code>, indicating that the * text does not contain valid characters after the * <code>limit</code> position. If this flag is set, * <code>layoutGlyphVector</code> does not examine the text after * <code>limit</code>, even if this would be necessary to select the * correct glyphs (e.g., for Arabic text). * * @since 1.4 */ public static final int LAYOUT_NO_LIMIT_CONTEXT = 4; /** * The logical name of this font. * * @since 1.0 */ protected String name; /** * The size of this font in points, rounded. * * @since 1.0 */ protected int size; /** * The size of this font in points. * * @since 1.0 */ protected float pointSize; /** * The style of this font -- PLAIN, BOLD, ITALIC or BOLD+ITALIC. * * @since 1.0 */ protected int style;//Serialization constant private static final long serialVersionUID = -4206021311591459213L; // The ClasspathToolkit-provided peer which implements this font private transient ClasspathFontPeer peer; /** * Creates a <code>Font</code> object from the specified string, which * is in one of the following formats: * <p> * <ul> * <li>fontname-style-pointsize * <li>fontname-style * <li>fontname-pointsize * <li>fontname * </ul> * <p> * The style should be one of BOLD, ITALIC, or BOLDITALIC. The default * style if none is specified is PLAIN. The default size if none * is specified is 12. * * @param fontspec a string specifying the required font (<code>null</code> * permitted, interpreted as 'Dialog-PLAIN-12'). * * @return A font. */ public static Font decode(String fontspec) { if (fontspec == null) fontspec = "Dialog-PLAIN-12"; String name = null; int style = PLAIN; int size = 12; StringTokenizer st = new StringTokenizer(fontspec, "- "); while (st.hasMoreTokens()) { String token = st.nextToken(); if (name == null) { name = token; continue; } if (token.toUpperCase().equals("BOLD")) { style = BOLD; continue; } if (token.toUpperCase().equals("ITALIC")) { style = ITALIC; continue; } if (token.toUpperCase().equals("BOLDITALIC")) { style = BOLD | ITALIC; continue; } int tokenval = 0; try { tokenval = Integer.parseInt(token); } catch (NumberFormatException e) { // Ignored. } if (tokenval != 0) size = tokenval; } HashMap attrs = new HashMap(); ClasspathFontPeer.copyStyleToAttrs(style, attrs); ClasspathFontPeer.copySizeToAttrs(size, attrs); return getFontFromToolkit(name, attrs); } /* These methods delegate to the toolkit. */ static ClasspathToolkit tk() { return (ClasspathToolkit) Toolkit.getDefaultToolkit(); } /* Every factory method in Font should eventually call this. */ static Font getFontFromToolkit(String name, Map attribs) { return tk().getFont(name, attribs); } /* Every Font constructor should eventually call this. */ static ClasspathFontPeer getPeerFromToolkit(String name, Map attrs) { return tk().getClasspathFontPeer(name, attrs); } /** * Returns a <code>Font</code> object from the passed property name. * * @param propname The name of the system property. * @param defval Value to use if the property is not found. * * @return The requested font, or <code>default</code> if the property * not exist or is malformed. */ public static Font getFont(String propname, Font defval) { String propval = System.getProperty(propname); if (propval != null) return decode(propval); return defval; } /** * Returns a <code>Font</code> object from the passed property name. * * @param propname The name of the system property. * * @return The requested font, or <code>null</code> if the property * not exist or is malformed. */ public static Font getFont(String propname) { return getFont(propname, (Font) null); } /** * Initializes a new instance of <code>Font</code> with the specified * attributes. * * @param name The name of the font. * @param style The font style. * @param size The font point size. */ public Font(String name, int style, int size) { HashMap attrs = new HashMap(); ClasspathFontPeer.copyStyleToAttrs(style, attrs); ClasspathFontPeer.copySizeToAttrs(size, attrs); this.peer = getPeerFromToolkit(name, attrs); this.size = size; this.pointSize = (float) size; if (name != null) this.name = name; else this.name = peer.getName(this); } public Font(Map attrs) { this(null, attrs); } /* This extra constructor is here to permit ClasspathToolkit and to build a font with a "logical name" as well as attrs. ClasspathToolkit.getFont(String,Map) uses reflection to call this package-private constructor. */ Font(String name, Map attrs) { // If attrs is null, setting it to an empty HashMap will give this // Font default attributes. if (attrs == null) attrs = new HashMap(); peer = getPeerFromToolkit(name, attrs); size = (int) peer.getSize(this); pointSize = peer.getSize(this); if (name != null) this.name = name; else this.name = peer.getName(this); } /** * Returns the logical name of the font. A logical name is the name the * font was constructed with. It may be the name of a logical font (one * of 6 required names in all java environments) or it may be a face * name. * * @return The logical name of the font. * * @see #getFamily() * @see #getFontName() */ public String getName () { return peer.getName(this); } /** * Returns the size of the font, in typographics points (1/72 of an inch), * rounded to an integer. * * @return The font size */ public int getSize() { return size; } /** * Returns the size of the font, in typographics points (1/72 of an inch). * * @return The font size */ public float getSize2D() { return pointSize; } /** * Tests whether or not this is a plain font. This will be true if * and only if neither the bold nor the italics style is set. * * @return <code>true</code> if this is a plain font, <code>false</code> * otherwise. */ public boolean isPlain() { return peer.isPlain(this); } /** * Tests whether or not this font is bold. * * @return <code>true</code> if this font is bold, <code>false</code> * otherwise. */ public boolean isBold() { return peer.isBold(this); } /** * Tests whether or not this font is italic. * * @return <code>true</code> if this font is italic, <code>false</code> * otherwise. */ public boolean isItalic() { return peer.isItalic(this); } /** * Returns the family name of this font. A family name describes a design * or "brand name" (such as Helvetica or Palatino). It is less specific * than a font face name (such as Helvetica Bold). * * @return A string containing the font family name. * * @since 1.2 *
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -