SkTypeface.h revision 62533ed6bb490e9abf5d02686d897a93c5e85d51
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypeface_DEFINED
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypeface_DEFINED
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkStream;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkWStream;
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkTypeface
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The SkTypeface class specifies the typeface and intrinsic style of a font.
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    This is used in the paint, along with optionally algorithmic settings like
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    how text appears when drawn (and measured).
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
32069b827f8e4f8ca212548719db64983a69b8dc25reed@android.com    Typeface objects are immutable, and so they can be shared between threads.
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTypeface : public SkRefCnt {
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Style specifies the intrinsic style attributes of a given typeface
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Style {
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kNormal = 0,
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBold   = 0x01,
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kItalic = 0x02,
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // helpers
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBoldItalic = 0x03
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the typeface's intrinsic style attributes
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style style() const { return fStyle; }
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if getStyle() has the kBold bit set.
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isBold() const { return (fStyle & kBold) != 0; }
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if getStyle() has the kItalic bit set.
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isItalic() const { return (fStyle & kItalic) != 0; }
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
59ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Return a 32bit value for this typeface, unique for the underlying font
60ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        data. Will never return 0.
61ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t uniqueID() const { return fUniqueID; }
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the uniqueID for the specified typeface. If the face is null,
65b1d9d2ef2803bd55fdc886d13033b48f8450dd14reed@android.com        resolve it to the default font and return its uniqueID. Will never
66b1d9d2ef2803bd55fdc886d13033b48f8450dd14reed@android.com        return 0.
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static uint32_t UniqueID(const SkTypeface* face);
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
70ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Returns true if the two typefaces reference the same underlying font,
71ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        handling either being null (treating null as the default font)
72ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
73ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
74ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested familyName and style. Pass null as the familyName to return
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the default font for the requested style. Will never return null
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param familyName  May be NULL. The name of the font family.
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param style       The style (normal, bold, italic) of the typeface.
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
84069b827f8e4f8ca212548719db64983a69b8dc25reed@android.com    static SkTypeface* CreateFromName(const char familyName[], Style style);
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested typeface and specified Style. Use this call if you want to
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pick a new style from the same family of the existing typeface.
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If family is NULL, this selects from the default font's family.
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param family  May be NULL. The name of the existing type face.
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param s       The style (normal, bold, italic) of the type face.
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a file. If the file does not exist, or is
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null.
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromFile(const char path[]);
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a stream. If the stream is
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null. Ownership of the stream is
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        transferred, so the caller must not reference it again.
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromStream(SkStream* stream);
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10962533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com    /** Write a unique signature to a stream, sufficient to reconstruct a
11062533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        typeface referencing the same font when Deserialize is called.
11162533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com     */
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void serialize(SkWStream*) const;
11362533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com
11462533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com    /** Given the data previously written by serialize(), return a new instance
11562533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        to a typeface referring to the same font. If that font is not available,
11662533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        return null. If an instance is returned, the caller is responsible for
11762533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        calling unref() when they are done with it.
11862533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com     */
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* Deserialize(SkStream*);
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** uniqueID must be unique (please!) and non-zero
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTypeface(Style style, uint32_t uniqueID)
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fUniqueID(uniqueID), fStyle(style) {}
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fUniqueID;
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style       fStyle;
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkRefCnt INHERITED;
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
135