SkTypeface.h revision ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5e
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
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Typeface objects are immutable, and so they can be shred between threads.
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    To enable this, Typeface inherits from the thread-safe version of SkRefCnt.
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkTypeface : public SkRefCnt {
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Style specifies the intrinsic style attributes of a given typeface
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Style {
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kNormal = 0,
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBold   = 0x01,
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kItalic = 0x02,
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // helpers
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBoldItalic = 0x03
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the typeface's intrinsic style attributes
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style style() const { return fStyle; }
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** DEPRECATED */
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style getStyle() const { return this->style(); }
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if getStyle() has the kBold bit set.
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isBold() const { return (fStyle & kBold) != 0; }
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if getStyle() has the kItalic bit set.
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isItalic() const { return (fStyle & kItalic) != 0; }
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
63ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Return a 32bit value for this typeface, unique for the underlying font
64ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        data. Will never return 0.
65ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t uniqueID() const { return fUniqueID; }
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the uniqueID for the specified typeface. If the face is null,
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        resolve it to the default font and return its uniqueID.
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static uint32_t UniqueID(const SkTypeface* face);
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
73ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Returns true if the two typefaces reference the same underlying font,
74ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        handling either being null (treating null as the default font)
75ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
76ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
77ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested familyName and style. Pass null as the familyName to return
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the default font for the requested style. Will never return null
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param familyName  May be NULL. The name of the font family.
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param style       The style (normal, bold, italic) of the typeface.
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* Create(const char familyName[], Style style = kNormal);
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested typeface and specified Style. Use this call if you want to
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pick a new style from the same family of the existing typeface.
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If family is NULL, this selects from the default font's family.
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param family  May be NULL. The name of the existing type face.
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param s       The style (normal, bold, italic) of the type face.
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a file. If the file does not exist, or is
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null.
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromFile(const char path[]);
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a stream. If the stream is
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null. Ownership of the stream is
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        transferred, so the caller must not reference it again.
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromStream(SkStream* stream);
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // Serialization
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void serialize(SkWStream*) const;
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* Deserialize(SkStream*);
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** uniqueID must be unique (please!) and non-zero
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkTypeface(Style style, uint32_t uniqueID)
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : fUniqueID(uniqueID), fStyle(style) {}
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fUniqueID;
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style       fStyle;
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkRefCnt INHERITED;
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
130