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
16dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.orgclass SkDescriptor;
17dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.orgclass SkFontDescriptor;
18dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.orgclass SkScalerContext;
19dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.orgstruct SkScalerContextRec;
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkStream;
21c48b2b330f8bf0e01381e27ca36f48ef5c7d8d5fvandebo@chromium.orgclass SkAdvancedTypefaceMetrics;
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkWStream;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
242f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.comtypedef uint32_t SkFontID;
25ddc218e508c4cdd16ff3461498cbbc2b5189b2bebungeman@google.com/** Machine endian. */
26f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.comtypedef uint32_t SkFontTableTag;
272f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkTypeface
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The SkTypeface class specifies the typeface and intrinsic style of a font.
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    This is used in the paint, along with optionally algorithmic settings like
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    how text appears when drawn (and measured).
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
35069b827f8e4f8ca212548719db64983a69b8dc25reed@android.com    Typeface objects are immutable, and so they can be shared between threads.
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
37a02bc1519cf49afa31fb38bed097dd5014880d04bungeman@google.comclass SK_API SkTypeface : public SkWeakRefCnt {
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
39fcb57b5570ec8b3176d4025d4c44d145591f819creed@google.com    SK_DECLARE_INST_COUNT(SkTypeface)
40fcb57b5570ec8b3176d4025d4c44d145591f819creed@google.com
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Style specifies the intrinsic style attributes of a given typeface
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Style {
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kNormal = 0,
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBold   = 0x01,
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kItalic = 0x02,
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // helpers
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBoldItalic = 0x03
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the typeface's intrinsic style attributes
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style style() const { return fStyle; }
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if getStyle() has the kBold bit set.
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isBold() const { return (fStyle & kBold) != 0; }
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if getStyle() has the kItalic bit set.
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isItalic() const { return (fStyle & kItalic) != 0; }
635b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
64fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com    /** Returns true if the typeface claims to be fixed-pitch.
65fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com     *  This is a style bit, advance widths may vary even if this returns true.
66875b4ecef79fd61f7d23260928638bcca4133751reed@google.com     */
67fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com    bool isFixedPitch() const { return fIsFixedPitch; }
68875b4ecef79fd61f7d23260928638bcca4133751reed@google.com
69ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Return a 32bit value for this typeface, unique for the underlying font
70ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        data. Will never return 0.
71ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
722f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    SkFontID uniqueID() const { return fUniqueID; }
73fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the uniqueID for the specified typeface. If the face is null,
75b1d9d2ef2803bd55fdc886d13033b48f8450dd14reed@android.com        resolve it to the default font and return its uniqueID. Will never
76b1d9d2ef2803bd55fdc886d13033b48f8450dd14reed@android.com        return 0.
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
782f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    static SkFontID UniqueID(const SkTypeface* face);
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
80ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    /** Returns true if the two typefaces reference the same underlying font,
81ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com        handling either being null (treating null as the default font)
82ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com     */
83ef772ddb86e9ab5e7e2811c67ed75a24f3ce0e5ereed@android.com    static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
845b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
85fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com    /**
86fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com     *  Returns a ref() to the default typeface. The caller must call unref()
87fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com     *  when they are done referencing the object. Never returns NULL.
88fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com     */
894fa748d5801df66e46e6f4e98e07523d44d261a2djsollen@google.com    static SkTypeface* RefDefault(Style style = SkTypeface::kNormal);
90fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested familyName and style. Pass null as the familyName to return
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the default font for the requested style. Will never return null
945b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param familyName  May be NULL. The name of the font family.
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param style       The style (normal, bold, italic) of the typeface.
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
100069b827f8e4f8ca212548719db64983a69b8dc25reed@android.com    static SkTypeface* CreateFromName(const char familyName[], Style style);
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new reference to the typeface that most closely matches the
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        requested typeface and specified Style. Use this call if you want to
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pick a new style from the same family of the existing typeface.
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If family is NULL, this selects from the default font's family.
1065b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param family  May be NULL. The name of the existing type face.
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param s       The style (normal, bold, italic) of the type face.
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return reference to the closest-matching typeface. Call must call
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unref() when they are done.
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a file. If the file does not exist, or is
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null.
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromFile(const char path[]);
1185b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return a new typeface given a stream. If the stream is
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        not a valid font file, returns null. Ownership of the stream is
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        transferred, so the caller must not reference it again.
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* CreateFromStream(SkStream* stream);
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12562533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com    /** Write a unique signature to a stream, sufficient to reconstruct a
12662533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        typeface referencing the same font when Deserialize is called.
12762533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com     */
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void serialize(SkWStream*) const;
1295b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
13062533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com    /** Given the data previously written by serialize(), return a new instance
13162533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        to a typeface referring to the same font. If that font is not available,
13262533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        return null. If an instance is returned, the caller is responsible for
13362533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com        calling unref() when they are done with it.
13462533ed6bb490e9abf5d02686d897a93c5e85d51reed@android.com     */
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkTypeface* Deserialize(SkStream*);
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
137bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    enum Encoding {
138bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com        kUTF8_Encoding,
139bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com        kUTF16_Encoding,
140bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com        kUTF32_Encoding
141bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    };
142bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com
143bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    /**
144bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  Given an array of character codes, of the specified encoding,
145bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  optionally return their corresponding glyph IDs (if glyphs is not NULL).
146bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *
147bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  @param chars pointer to the array of character codes
1483c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com     *  @param encoding how the characters are encoded
149bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  @param glyphs (optional) returns the corresponding glyph IDs for each
150bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *          character code, up to glyphCount values. If a character code is
151bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *          not found in the typeface, the corresponding glyph ID will be 0.
152bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  @param glyphCount number of code points in 'chars' to process. If glyphs
153bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *          is not NULL, then it must point sufficient memory to write
154bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *          glyphCount values into it.
155bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  @return the number of number of continuous non-zero glyph IDs computed
156bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *          from the beginning of chars. This value is valid, even if the
157bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *          glyphs parameter is NULL.
158bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     */
159bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    int charsToGlyphs(const void* chars, Encoding encoding, uint16_t glyphs[],
160bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com                      int glyphCount) const;
161bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com
162bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    /**
163bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     *  Return the number of glyphs in the typeface.
164bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com     */
165bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    int countGlyphs() const;
166bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com
167f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    // Table getters -- may fail if the underlying font format is not organized
168f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    // as 4-byte tables.
169f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com
170f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Return the number of tables in the font. */
171f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    int countTables() const;
172fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
173f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Copy into tags[] (allocated by the caller) the list of table tags in
174f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  the font, and return the number. This will be the same as CountTables()
175f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  or 0 if an error occured. If tags == NULL, this only returns the count
176f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  (the same as calling countTables()).
177f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     */
178f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    int getTableTags(SkFontTableTag tags[]) const;
179fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
180f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Given a table tag, return the size of its contents, or 0 if not present
181f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     */
182f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    size_t getTableSize(SkFontTableTag) const;
183fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
184f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    /** Copy the contents of a table into data (allocated by the caller). Note
185f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  that the contents of the table will be in their native endian order
186f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  (which for most truetype tables is big endian). If the table tag is
187f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  not found, or there is an error copying the data, then 0 is returned.
188f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  If this happens, it is possible that some or all of the memory pointed
189f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  to by data may have been written to, even though an error has occured.
190fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     *
191f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param fontID the font to copy the table from
192f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param tag  The table tag whose contents are to be copied
193f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param offset The offset in bytes into the table's contents where the
194f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  copy should start from.
195f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param length The number of bytes, starting at offset, of table data
196f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  to copy.
197f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @param data storage address where the table contents are copied to
198f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  @return the number of bytes actually copied into data. If offset+length
199f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  exceeds the table's size, then only the bytes up to the table's
200f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  size are actually copied, and this is the value returned. If
201f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  offset > the table's size, or tag is not a valid table,
202f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     *  then 0 is returned.
203f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com     */
204f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com    size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
205f11508d7f4aae52739b9c4f3bb2da2fd78fa23d9reed@google.com                        void* data) const;
2064b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com
2074b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com    /**
2084b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com     *  Return the units-per-em value for this typeface, or zero if there is an
2094b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com     *  error.
2104b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com     */
2114b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com    int getUnitsPerEm() const;
2124b2af9c91d39c2176a32e7ba42a0276dca68034areed@google.com
21335fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com    /**
21435fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  Given a run of glyphs, return the associated horizontal adjustments.
21535fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  Adjustments are in "design units", which are integers relative to the
21635fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  typeface's units per em (see getUnitsPerEm).
21735fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *
21835fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  Some typefaces are known to never support kerning. Calling this method
21935fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  with all zeros (e.g. getKerningPairAdustments(NULL, 0, NULL)) returns
22035fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  a boolean indicating if the typeface might support kerning. If it
22135fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  returns false, then it will always return false (no kerning) for all
22235fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  possible glyph runs. If it returns true, then it *may* return true for
22335fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  somne glyph runs.
22435fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *
22535fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  If count is non-zero, then the glyphs parameter must point to at least
22635fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  [count] valid glyph IDs, and the adjustments parameter must be
22735fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  sized to at least [count - 1] entries. If the method returns true, then
22835fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  [count-1] entries in the adjustments array will be set. If the method
22935fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  returns false, then no kerning should be applied, and the adjustments
23035fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  array will be in an undefined state (possibly some values may have been
23135fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     *  written, but none of them should be interpreted as valid values).
23235fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com     */
23335fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com    bool getKerningPairAdjustments(const uint16_t glyphs[], int count,
23435fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com                                   int32_t adjustments[]) const;
23535fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com
236a980269c2498836101146adc729ef780fb89824ebungeman@google.com    struct LocalizedString {
237a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkString fString;
238a980269c2498836101146adc729ef780fb89824ebungeman@google.com        SkString fLanguage;
239a980269c2498836101146adc729ef780fb89824ebungeman@google.com    };
240a980269c2498836101146adc729ef780fb89824ebungeman@google.com    class LocalizedStrings : ::SkNoncopyable {
241a980269c2498836101146adc729ef780fb89824ebungeman@google.com    public:
242a980269c2498836101146adc729ef780fb89824ebungeman@google.com        virtual ~LocalizedStrings() { }
243a980269c2498836101146adc729ef780fb89824ebungeman@google.com        virtual bool next(LocalizedString* localizedString) = 0;
244839702b61934914118ec557dd641be322eba3b5fbungeman@google.com        void unref() { SkDELETE(this); }
245a980269c2498836101146adc729ef780fb89824ebungeman@google.com    };
246a980269c2498836101146adc729ef780fb89824ebungeman@google.com    /**
247a980269c2498836101146adc729ef780fb89824ebungeman@google.com     *  Returns an iterator which will attempt to enumerate all of the
248a980269c2498836101146adc729ef780fb89824ebungeman@google.com     *  family names specified by the font.
249839702b61934914118ec557dd641be322eba3b5fbungeman@google.com     *  It is the caller's responsibility to unref() the returned pointer.
250a980269c2498836101146adc729ef780fb89824ebungeman@google.com     */
251839702b61934914118ec557dd641be322eba3b5fbungeman@google.com    LocalizedStrings* createFamilyNameIterator() const;
252a980269c2498836101146adc729ef780fb89824ebungeman@google.com
2532cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com    /**
25408df48d2411b33a9078f1621894d630f0b11cd60reed@google.com     *  Return the family name for this typeface. It will always be returned
25508df48d2411b33a9078f1621894d630f0b11cd60reed@google.com     *  encoded as UTF8, but the language of the name is whatever the host
25608df48d2411b33a9078f1621894d630f0b11cd60reed@google.com     *  platform chooses.
25708df48d2411b33a9078f1621894d630f0b11cd60reed@google.com     */
25808df48d2411b33a9078f1621894d630f0b11cd60reed@google.com    void getFamilyName(SkString* name) const;
25908df48d2411b33a9078f1621894d630f0b11cd60reed@google.com
26008df48d2411b33a9078f1621894d630f0b11cd60reed@google.com    /**
2612cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com     *  Return a stream for the contents of the font data, or NULL on failure.
2622cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com     *  If ttcIndex is not null, it is set to the TrueTypeCollection index
2632cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com     *  of this typeface within the stream, or 0 if the stream is not a
2642cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com     *  collection.
2652cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com     */
266fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com    SkStream* openStream(int* ttcIndex) const;
26784e22d847fc84727bc220947162363ee1fe068fcreed@google.com
26884e22d847fc84727bc220947162363ee1fe068fcreed@google.com    /**
26984e22d847fc84727bc220947162363ee1fe068fcreed@google.com     *  Return a scalercontext for the given descriptor. If this fails, then
27084e22d847fc84727bc220947162363ee1fe068fcreed@google.com     *  if allowFailure is true, this returns NULL, else it returns a
27184e22d847fc84727bc220947162363ee1fe068fcreed@google.com     *  dummy scalercontext that will not crash, but will draw nothing.
27284e22d847fc84727bc220947162363ee1fe068fcreed@google.com     */
27384e22d847fc84727bc220947162363ee1fe068fcreed@google.com    SkScalerContext* createScalerContext(const SkDescriptor*,
27484e22d847fc84727bc220947162363ee1fe068fcreed@google.com                                         bool allowFailure = false) const;
275fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com
2767edec140391ce06933cc1506d96bb70093b63c7breed@google.com    // PRIVATE / EXPERIMENTAL -- do not call
2777edec140391ce06933cc1506d96bb70093b63c7breed@google.com    void filterRec(SkScalerContextRec* rec) const {
2787edec140391ce06933cc1506d96bb70093b63c7breed@google.com        this->onFilterRec(rec);
2797edec140391ce06933cc1506d96bb70093b63c7breed@google.com    }
2807edec140391ce06933cc1506d96bb70093b63c7breed@google.com    // PRIVATE / EXPERIMENTAL -- do not call
2817edec140391ce06933cc1506d96bb70093b63c7breed@google.com    void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
2827edec140391ce06933cc1506d96bb70093b63c7breed@google.com        this->onGetFontDescriptor(desc, isLocal);
2837edec140391ce06933cc1506d96bb70093b63c7breed@google.com    }
2847edec140391ce06933cc1506d96bb70093b63c7breed@google.com
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
286dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.org    /** uniqueID must be unique and non-zero
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
288fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com    SkTypeface(Style style, SkFontID uniqueID, bool isFixedPitch = false);
2892f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    virtual ~SkTypeface();
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
291fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com    /** Sets the fixedPitch bit. If used, must be called in the constructor. */
292fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com    void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; }
293fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com
294cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com    friend class SkScalerContext;
2954fa748d5801df66e46e6f4e98e07523d44d261a2djsollen@google.com    static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
296cb1bbb375aa4fdd099dc60302ca1712f04607782bungeman@google.com
2970da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com    virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const = 0;
2980da48618a758ef46c2174bdc1eaeb6dd8a693a2ereed@google.com    virtual void onFilterRec(SkScalerContextRec*) const = 0;
2992689f615e364dc48ad73826564f5b13d2329179dreed@google.com    virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
3002689f615e364dc48ad73826564f5b13d2329179dreed@google.com                        SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
3012689f615e364dc48ad73826564f5b13d2329179dreed@google.com                        const uint32_t* glyphIDs,
3022689f615e364dc48ad73826564f5b13d2329179dreed@google.com                        uint32_t glyphIDsCount) const = 0;
30335fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com
304292b1d4903a770a77282508054917b48fb989d49reed@google.com    virtual SkStream* onOpenStream(int* ttcIndex) const = 0;
3055526ede94a2fc58bcf6b578b12a29f6addad776dreed@google.com    virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0;
306c1641fc92259a1ca5cfc32cd5c8c55ea316b2bd1skia.committer@gmail.com
307bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com    virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[],
3083c996f8a15e5d8fada9550d978e9b5344b81d276bungeman@google.com                                int glyphCount) const = 0;
3097bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    virtual int onCountGlyphs() const = 0;
310bcb42aecf1bdb9ae80d766d203b4f636b954cf03reed@google.com
3117bdd614a1940935d3badeb54f7aae75d76ea830dbungeman@google.com    virtual int onGetUPEM() const = 0;
31235fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com    virtual bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
31335fe7372b1b897a77578a220c334e1fb36d144e9reed@google.com                                             int32_t adjustments[]) const;
3142cdc6713fb04c46ecbc73a724029a8b266004ddfreed@google.com
315839702b61934914118ec557dd641be322eba3b5fbungeman@google.com    virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0;
316a980269c2498836101146adc729ef780fb89824ebungeman@google.com
317ddc218e508c4cdd16ff3461498cbbc2b5189b2bebungeman@google.com    virtual int onGetTableTags(SkFontTableTag tags[]) const = 0;
318dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.org    virtual size_t onGetTableData(SkFontTableTag, size_t offset,
319ddc218e508c4cdd16ff3461498cbbc2b5189b2bebungeman@google.com                                  size_t length, void* data) const = 0;
320dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.org
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
32299f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com    friend class SkGTypeface;
32399f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com    friend class SkPDFFont;
32499f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com    friend class SkPDFCIDFont;
32599f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com
32699f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com    /** Retrieve detailed typeface metrics.  Used by the PDF backend.
32799f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     @param perGlyphInfo Indicate what glyph specific information (advances,
32899f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     names, etc.) should be populated.
32999f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     @param glyphIDs  For per-glyph info, specify subset of the font by
33099f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     giving glyph ids.  Each integer represents a glyph
33199f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     id.  Passing NULL means all glyphs in the font.
33299f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
33399f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     glyphIDs is NULL.
33499f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     @return The returned object has already been referenced.
33599f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com     */
33699f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com    SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics(
33799f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com                          SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
33899f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com                          const uint32_t* glyphIDs = NULL,
33999f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com                          uint32_t glyphIDsCount = 0) const;
34099f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.com
34199f381ad769cc13b0f23c99c47b65c3f236e15ccreed@google.comprivate:
34278358bf624c7e7c09ffccf638c50870808d884d6mtklein    static SkTypeface* CreateDefault(int style);  // SkLazyPtr requires an int, not a Style.
34378358bf624c7e7c09ffccf638c50870808d884d6mtklein    static void        DeleteDefault(SkTypeface*);
34418b75e54ab64763615e09761fcaeb3f439835f59bungeman@google.com
3452f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com    SkFontID    fUniqueID;
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Style       fStyle;
347fe74765f0d302669ae49e68074492bdfe0ce6e6fbungeman@google.com    bool        fIsFixedPitch;
3485b31b0f1eaca0cdc3aa5697e8efa1baf68b0774ereed@google.com
349fed86bdb8b9f037439bbfa7cdbd53a581dbc5985reed@google.com    friend class SkPaint;
35090808e87c21e93b8e670360655e0b0eb12cb2f87reed@google.com    friend class SkGlyphCache;  // GetDefaultTypeface
351dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.org    // just so deprecated fonthost can call protected methods
352dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.org    friend class SkFontHost;
353dc09f07671145b21c83de7153c9c9b950c871f1amike@reedtribe.org
354a02bc1519cf49afa31fb38bed097dd5014880d04bungeman@google.com    typedef SkWeakRefCnt INHERITED;
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
358