Typeface.java revision 7f9f99ea11051614a7727dfb9f9578b518e76e3c
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import android.content.res.AssetManager;
20
21import java.io.File;
22
23/**
24 * The Typeface class specifies the typeface and intrinsic style of a font.
25 * This is used in the paint, along with optionally Paint settings like
26 * textSize, textSkewX, textScaleX to specify
27 * how text appears when drawn (and measured).
28 */
29public class Typeface {
30
31    /** The default NORMAL typeface object */
32    public static final Typeface DEFAULT;
33    /**
34     * The default BOLD typeface object. Note: this may be not actually be
35     * bold, depending on what fonts are installed. Call getStyle() to know
36     * for sure.
37     */
38    public static final Typeface DEFAULT_BOLD;
39    /** The NORMAL style of the default sans serif typeface. */
40    public static final Typeface SANS_SERIF;
41    /** The NORMAL style of the default serif typeface. */
42    public static final Typeface SERIF;
43    /** The NORMAL style of the default monospace typeface. */
44    public static final Typeface MONOSPACE;
45
46    /* package */ static Typeface[] sDefaults;
47
48    /* package */ int native_instance;
49
50    // Style
51    public static final int NORMAL = 0;
52    public static final int BOLD = 1;
53    public static final int ITALIC = 2;
54    public static final int BOLD_ITALIC = 3;
55
56    /** Returns the typeface's intrinsic style attributes */
57    public int getStyle() {
58        return nativeGetStyle(native_instance);
59    }
60
61    /** Returns true if getStyle() has the BOLD bit set. */
62    public final boolean isBold() {
63        return (getStyle() & BOLD) != 0;
64    }
65
66    /** Returns true if getStyle() has the ITALIC bit set. */
67    public final boolean isItalic() {
68        return (getStyle() & ITALIC) != 0;
69    }
70
71    /**
72     * Create a typeface object given a family name, and option style information.
73     * If null is passed for the name, then the "default" font will be chosen.
74     * The resulting typeface object can be queried (getStyle()) to discover what
75     * its "real" style characteristics are.
76     *
77     * @param familyName May be null. The name of the font family.
78     * @param style  The style (normal, bold, italic) of the typeface.
79     *               e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
80     * @return The best matching typeface.
81     */
82    public static Typeface create(String familyName, int style) {
83        return new Typeface(nativeCreate(familyName, style));
84    }
85
86    /**
87     * Create a typeface object that best matches the specified existing
88     * typeface and the specified Style. Use this call if you want to pick a new
89     * style from the same family of an existing typeface object. If family is
90     * null, this selects from the default font's family.
91     *
92     * @param family May be null. The name of the existing type face.
93     * @param style  The style (normal, bold, italic) of the typeface.
94     *               e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
95     * @return The best matching typeface.
96     */
97    public static Typeface create(Typeface family, int style) {
98        int ni = 0;
99        if (family != null) {
100            ni = family.native_instance;
101        }
102        return new Typeface(nativeCreateFromTypeface(ni, style));
103    }
104
105    /**
106     * Returns one of the default typeface objects, based on the specified style
107     *
108     * @return the default typeface that corresponds to the style
109     */
110    public static Typeface defaultFromStyle(int style) {
111        return sDefaults[style];
112    }
113
114    /**
115     * Create a new typeface from the specified font data.
116     * @param mgr The application's asset manager
117     * @param path  The file name of the font data in the assets directory
118     * @return The new typeface.
119     */
120    public static Typeface createFromAsset(AssetManager mgr, String path) {
121        return new Typeface(nativeCreateFromAsset(mgr, path));
122    }
123
124    /**
125     * Create a new typeface from the specified font file.
126     *
127     * @param path The path to the font data.
128     * @return The new typeface.
129     */
130    public static Typeface createFromFile(File path) {
131        return new Typeface(nativeCreateFromFile(path.getAbsolutePath()));
132    }
133
134    /**
135     * Create a new typeface from the specified font file.
136     *
137     * @param path The full path to the font data.
138     * @return The new typeface.
139     */
140    public static Typeface createFromFile(String path) {
141        return new Typeface(nativeCreateFromFile(path));
142    }
143
144    // don't allow clients to call this directly
145    private Typeface(int ni) {
146        if (0 == ni) {
147            throw new RuntimeException("native typeface cannot be made");
148        }
149        native_instance = ni;
150    }
151
152    static {
153        DEFAULT         = create((String)null, 0);
154        DEFAULT_BOLD    = create((String)null, Typeface.BOLD);
155        SANS_SERIF      = create("sans-serif", 0);
156        SERIF           = create("serif", 0);
157        MONOSPACE       = create("monospace", 0);
158
159        sDefaults = new Typeface[] {
160            DEFAULT,
161            DEFAULT_BOLD,
162            create((String)null, Typeface.ITALIC),
163            create((String)null, Typeface.BOLD_ITALIC),
164        };
165    }
166
167    protected void finalize() throws Throwable {
168        super.finalize();
169        nativeUnref(native_instance);
170    }
171
172    private static native int  nativeCreate(String familyName, int style);
173    private static native int  nativeCreateFromTypeface(int native_instance, int style);
174    private static native void nativeUnref(int native_instance);
175    private static native int  nativeGetStyle(int native_instance);
176    private static native int  nativeCreateFromAsset(AssetManager mgr, String path);
177    private static native int nativeCreateFromFile(String path);
178
179    /**
180     * Set the global gamma coefficients for black and white text. This call is
181     * usually a no-op in shipping products, and only exists for testing during
182     * development.
183     *
184     * @param blackGamma gamma coefficient for black text
185     * @param whiteGamma gamma coefficient for white text
186     *
187     * @hide - this is just for calibrating devices, not for normal apps
188     */
189    public static native void setGammaForText(float blackGamma, float whiteGamma);
190}
191