SkTypeface.h revision fbfcd5602128ec010c82cb733c9cdc0a3254f9f3
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypeface_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypeface_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org#include "SkAdvancedTypefaceMetrics.h"
14a02bc1519cf49afa31fb38bed097dd5014880d04bungeman@google.com#include "SkWeakRefCnt.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkStream;
17c48b2b330f8bf0e01381e27ca36f48ef5c7d8d5fvandebo@chromium.orgclass SkAdvancedTypefaceMetrics;
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkWStream;
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
202f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.comtypedef uint32_t SkFontID;
21f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.comtypedef uint32_t SkFontTableTag;
222f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkTypeface
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The SkTypeface class specifies the typeface and intrinsic style of a font.
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    This is used in the paint, along with optionally algorithmic settings like
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    how text appears when drawn (and measured).
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
30069b827f8e4f8ca212548719db64983a69b8dc25reed@android.com    Typeface objects are immutable, and so they can be shared between threads.
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
32a02bc1519cf49afa31fb38bed097dd5014880d04bungeman@google.comclass SK_API SkTypeface : public SkWeakRefCnt {
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
34fcb57b5570ec8b3176d4025d4c44d145591f819creed@google.com    SK_DECLARE_INST_COUNT(SkTypeface)
35fcb57b5570ec8b3176d4025d4c44d145591f819creed@google.com
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; }
585b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
59875b4ecef79fd61f7d23260928638bcca4133751reed@google.com    /** Returns true if the typeface is fixed-width
60875b4ecef79fd61f7d23260928638bcca4133751reed@google.com     */
61875b4ecef79fd61f7d23260928638bcca4133751reed@google.com    bool isFixedWidth() const { return fIsFixedWidth; }
62875b4ecef79fd61f7d23260928638bcca4133751reed@google.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     */
662f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    SkFontID uniqueID() const { return fUniqueID; }
67fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the uniqueID for the specified typeface. If the face is null,
69b1d9d2ef2803bd55fdc886d13033b48f8450dd14reed@android.com        resolve it to the default font and return its uniqueID. Will never
70b1d9d2ef2803bd55fdc886d13033b48f8450dd14reed@android.com        return 0.
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
722f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    static SkFontID UniqueID(const SkTypeface* face);
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
74ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Returns true if the two typefaces reference the same underlying font,
75ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        handling either being null (treating null as the default font)
76ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
77ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
785b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested familyName and style. Pass null as the familyName to return
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the default font for the requested style. Will never return null
825b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param familyName  May be NULL. The name of the font family.
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param style       The style (normal, bold, italic) of the typeface.
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
88069b827f8e4f8ca212548719db64983a69b8dc25reed@android.com    static SkTypeface* CreateFromName(const char familyName[], Style style);
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested typeface and specified Style. Use this call if you want to
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pick a new style from the same family of the existing typeface.
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If family is NULL, this selects from the default font's family.
945b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param family  May be NULL. The name of the existing type face.
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param s       The style (normal, bold, italic) of the type face.
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a file. If the file does not exist, or is
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null.
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromFile(const char path[]);
1065b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a stream. If the stream is
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null. Ownership of the stream is
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        transferred, so the caller must not reference it again.
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromStream(SkStream* stream);
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11362533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com    /** Write a unique signature to a stream, sufficient to reconstruct a
11462533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        typeface referencing the same font when Deserialize is called.
11562533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com     */
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void serialize(SkWStream*) const;
1175b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
11862533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com    /** Given the data previously written by serialize(), return a new instance
11962533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        to a typeface referring to the same font. If that font is not available,
12062533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        return null. If an instance is returned, the caller is responsible for
12162533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        calling unref() when they are done with it.
12262533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com     */
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* Deserialize(SkStream*);
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
125c48b2b330f8bf0e01381e27ca36f48ef5c7d8d5fvandebo@chromium.org    /** Retrieve detailed typeface metrics.  Used by the PDF backend.
126325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org        @param perGlyphInfo Indicate what glyph specific information (advances,
127325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org                            names, etc.) should be populated.
12837ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org        @param glyphIDs  For per-glyph info, specify subset of the font by
12937ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                         giving glyph ids.  Each integer represents a glyph
13037ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                         id.  Passing NULL means all glyphs in the font.
13137ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org        @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
13237ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org                             glyphIDs is NULL.
133c48b2b330f8bf0e01381e27ca36f48ef5c7d8d5fvandebo@chromium.org        @return The returned object has already been referenced.
1342a22e10ab2946c5590cd2a258427ce3ccfca9bfavandebo@chromium.org     */
135c48b2b330f8bf0e01381e27ca36f48ef5c7d8d5fvandebo@chromium.org    SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics(
13637ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org            SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
13737ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org            const uint32_t* glyphIDs = NULL,
13837ad8fb72ff1b3faac93b01ead2c79e1a06fc172vandebo@chromium.org            uint32_t glyphIDsCount = 0) const;
139325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org
140f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    // Table getters -- may fail if the underlying font format is not organized
141f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    // as 4-byte tables.
142f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com
143f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Return the number of tables in the font. */
144f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    int countTables() const;
145fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
146f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Copy into tags[] (allocated by the caller) the list of table tags in
147f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  the font, and return the number. This will be the same as CountTables()
148f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  or 0 if an error occured. If tags == NULL, this only returns the count
149f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  (the same as calling countTables()).
150f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     */
151f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    int getTableTags(SkFontTableTag tags[]) const;
152fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
153f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Given a table tag, return the size of its contents, or 0 if not present
154f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     */
155f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    size_t getTableSize(SkFontTableTag) const;
156fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
157f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Copy the contents of a table into data (allocated by the caller). Note
158f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  that the contents of the table will be in their native endian order
159f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  (which for most truetype tables is big endian). If the table tag is
160f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  not found, or there is an error copying the data, then 0 is returned.
161f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  If this happens, it is possible that some or all of the memory pointed
162f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  to by data may have been written to, even though an error has occured.
163fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     *
164f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param fontID the font to copy the table from
165f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param tag  The table tag whose contents are to be copied
166f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param offset The offset in bytes into the table's contents where the
167f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  copy should start from.
168f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param length The number of bytes, starting at offset, of table data
169f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  to copy.
170f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param data storage address where the table contents are copied to
171f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @return the number of bytes actually copied into data. If offset+length
172f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  exceeds the table's size, then only the bytes up to the table's
173f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  size are actually copied, and this is the value returned. If
174f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  offset > the table's size, or tag is not a valid table,
175f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  then 0 is returned.
176f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     */
177f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
178f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com                        void* data) const;
1794b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com
1804b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com    /**
1814b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com     *  Return the units-per-em value for this typeface, or zero if there is an
1824b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com     *  error.
1834b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com     */
1844b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com    int getUnitsPerEm() const;
1854b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** uniqueID must be unique (please!) and non-zero
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1892f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    SkTypeface(Style style, SkFontID uniqueID, bool isFixedWidth = false);
1902f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    virtual ~SkTypeface();
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1932f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    SkFontID    fUniqueID;
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style       fStyle;
1955b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com    bool        fIsFixedWidth;
1965b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
197a02bc1519cf49afa31fb38bed097dd5014880d04bungeman@google.com    typedef SkWeakRefCnt INHERITED;
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
201