Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2006 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkTypeface_DEFINED
     11 #define SkTypeface_DEFINED
     12 
     13 #include "SkAdvancedTypefaceMetrics.h"
     14 #include "SkWeakRefCnt.h"
     15 
     16 class SkStream;
     17 class SkAdvancedTypefaceMetrics;
     18 class SkWStream;
     19 
     20 typedef uint32_t SkFontID;
     21 typedef uint32_t SkFontTableTag;
     22 
     23 /** \class SkTypeface
     24 
     25     The SkTypeface class specifies the typeface and intrinsic style of a font.
     26     This is used in the paint, along with optionally algorithmic settings like
     27     textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify
     28     how text appears when drawn (and measured).
     29 
     30     Typeface objects are immutable, and so they can be shared between threads.
     31 */
     32 class SK_API SkTypeface : public SkWeakRefCnt {
     33 public:
     34     SK_DECLARE_INST_COUNT(SkTypeface)
     35 
     36     /** Style specifies the intrinsic style attributes of a given typeface
     37     */
     38     enum Style {
     39         kNormal = 0,
     40         kBold   = 0x01,
     41         kItalic = 0x02,
     42 
     43         // helpers
     44         kBoldItalic = 0x03
     45     };
     46 
     47     /** Returns the typeface's intrinsic style attributes
     48     */
     49     Style style() const { return fStyle; }
     50 
     51     /** Returns true if getStyle() has the kBold bit set.
     52     */
     53     bool isBold() const { return (fStyle & kBold) != 0; }
     54 
     55     /** Returns true if getStyle() has the kItalic bit set.
     56     */
     57     bool isItalic() const { return (fStyle & kItalic) != 0; }
     58 
     59     /** Returns true if the typeface is fixed-width
     60      */
     61     bool isFixedWidth() const { return fIsFixedWidth; }
     62 
     63     /** Return a 32bit value for this typeface, unique for the underlying font
     64         data. Will never return 0.
     65      */
     66     SkFontID uniqueID() const { return fUniqueID; }
     67 
     68     /** Return the uniqueID for the specified typeface. If the face is null,
     69         resolve it to the default font and return its uniqueID. Will never
     70         return 0.
     71     */
     72     static SkFontID UniqueID(const SkTypeface* face);
     73 
     74     /** Returns true if the two typefaces reference the same underlying font,
     75         handling either being null (treating null as the default font)
     76      */
     77     static bool Equal(const SkTypeface* facea, const SkTypeface* faceb);
     78 
     79     /** Return a new reference to the typeface that most closely matches the
     80         requested familyName and style. Pass null as the familyName to return
     81         the default font for the requested style. Will never return null
     82 
     83         @param familyName  May be NULL. The name of the font family.
     84         @param style       The style (normal, bold, italic) of the typeface.
     85         @return reference to the closest-matching typeface. Call must call
     86                 unref() when they are done.
     87     */
     88     static SkTypeface* CreateFromName(const char familyName[], Style style);
     89 
     90     /** Return a new reference to the typeface that most closely matches the
     91         requested typeface and specified Style. Use this call if you want to
     92         pick a new style from the same family of the existing typeface.
     93         If family is NULL, this selects from the default font's family.
     94 
     95         @param family  May be NULL. The name of the existing type face.
     96         @param s       The style (normal, bold, italic) of the type face.
     97         @return reference to the closest-matching typeface. Call must call
     98                 unref() when they are done.
     99     */
    100     static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s);
    101 
    102     /** Return a new typeface given a file. If the file does not exist, or is
    103         not a valid font file, returns null.
    104     */
    105     static SkTypeface* CreateFromFile(const char path[]);
    106 
    107     /** Return a new typeface given a stream. If the stream is
    108         not a valid font file, returns null. Ownership of the stream is
    109         transferred, so the caller must not reference it again.
    110     */
    111     static SkTypeface* CreateFromStream(SkStream* stream);
    112 
    113     /** Write a unique signature to a stream, sufficient to reconstruct a
    114         typeface referencing the same font when Deserialize is called.
    115      */
    116     void serialize(SkWStream*) const;
    117 
    118     /** Given the data previously written by serialize(), return a new instance
    119         to a typeface referring to the same font. If that font is not available,
    120         return null. If an instance is returned, the caller is responsible for
    121         calling unref() when they are done with it.
    122      */
    123     static SkTypeface* Deserialize(SkStream*);
    124 
    125     /** Retrieve detailed typeface metrics.  Used by the PDF backend.
    126         @param perGlyphInfo Indicate what glyph specific information (advances,
    127                             names, etc.) should be populated.
    128         @param glyphIDs  For per-glyph info, specify subset of the font by
    129                          giving glyph ids.  Each integer represents a glyph
    130                          id.  Passing NULL means all glyphs in the font.
    131         @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
    132                              glyphIDs is NULL.
    133         @return The returned object has already been referenced.
    134      */
    135     SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics(
    136             SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
    137             const uint32_t* glyphIDs = NULL,
    138             uint32_t glyphIDsCount = 0) const;
    139 
    140     // Table getters -- may fail if the underlying font format is not organized
    141     // as 4-byte tables.
    142 
    143     /** Return the number of tables in the font. */
    144     int countTables() const;
    145 
    146     /** Copy into tags[] (allocated by the caller) the list of table tags in
    147      *  the font, and return the number. This will be the same as CountTables()
    148      *  or 0 if an error occured. If tags == NULL, this only returns the count
    149      *  (the same as calling countTables()).
    150      */
    151     int getTableTags(SkFontTableTag tags[]) const;
    152 
    153     /** Given a table tag, return the size of its contents, or 0 if not present
    154      */
    155     size_t getTableSize(SkFontTableTag) const;
    156 
    157     /** Copy the contents of a table into data (allocated by the caller). Note
    158      *  that the contents of the table will be in their native endian order
    159      *  (which for most truetype tables is big endian). If the table tag is
    160      *  not found, or there is an error copying the data, then 0 is returned.
    161      *  If this happens, it is possible that some or all of the memory pointed
    162      *  to by data may have been written to, even though an error has occured.
    163      *
    164      *  @param fontID the font to copy the table from
    165      *  @param tag  The table tag whose contents are to be copied
    166      *  @param offset The offset in bytes into the table's contents where the
    167      *  copy should start from.
    168      *  @param length The number of bytes, starting at offset, of table data
    169      *  to copy.
    170      *  @param data storage address where the table contents are copied to
    171      *  @return the number of bytes actually copied into data. If offset+length
    172      *  exceeds the table's size, then only the bytes up to the table's
    173      *  size are actually copied, and this is the value returned. If
    174      *  offset > the table's size, or tag is not a valid table,
    175      *  then 0 is returned.
    176      */
    177     size_t getTableData(SkFontTableTag tag, size_t offset, size_t length,
    178                         void* data) const;
    179 
    180     /**
    181      *  Return the units-per-em value for this typeface, or zero if there is an
    182      *  error.
    183      */
    184     int getUnitsPerEm() const;
    185 
    186 protected:
    187     /** uniqueID must be unique (please!) and non-zero
    188     */
    189     SkTypeface(Style style, SkFontID uniqueID, bool isFixedWidth = false);
    190     virtual ~SkTypeface();
    191 
    192     friend class SkScalerContext;
    193     static SkTypeface* GetDefaultTypeface();
    194 
    195 private:
    196     SkFontID    fUniqueID;
    197     Style       fStyle;
    198     bool        fIsFixedWidth;
    199 
    200     typedef SkWeakRefCnt INHERITED;
    201 };
    202 
    203 #endif
    204