Home | History | Annotate | Download | only in pdf
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      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 SkPDFFont_DEFINED
     11 #define SkPDFFont_DEFINED
     12 
     13 #include "SkAdvancedTypefaceMetrics.h"
     14 #include "SkBitSet.h"
     15 #include "SkPDFTypes.h"
     16 #include "SkTDArray.h"
     17 #include "SkThread.h"
     18 #include "SkTypeface.h"
     19 
     20 class SkPaint;
     21 class SkPDFCatalog;
     22 class SkPDFFont;
     23 
     24 class SkPDFGlyphSet : public SkNoncopyable {
     25 public:
     26     SkPDFGlyphSet();
     27 
     28     void set(const uint16_t* glyphIDs, int numGlyphs);
     29     bool has(uint16_t glyphID) const;
     30     void merge(const SkPDFGlyphSet& usage);
     31     void exportTo(SkTDArray<uint32_t>* glyphIDs) const;
     32 
     33 private:
     34     SkBitSet fBitSet;
     35 };
     36 
     37 class SkPDFGlyphSetMap : public SkNoncopyable {
     38 public:
     39     struct FontGlyphSetPair {
     40         FontGlyphSetPair(SkPDFFont* font, SkPDFGlyphSet* glyphSet);
     41 
     42         SkPDFFont* fFont;
     43         SkPDFGlyphSet* fGlyphSet;
     44     };
     45 
     46     SkPDFGlyphSetMap();
     47     ~SkPDFGlyphSetMap();
     48 
     49     class F2BIter {
     50     public:
     51         explicit F2BIter(const SkPDFGlyphSetMap& map);
     52         const FontGlyphSetPair* next() const;
     53         void reset(const SkPDFGlyphSetMap& map);
     54 
     55     private:
     56         const SkTDArray<FontGlyphSetPair>* fMap;
     57         mutable int fIndex;
     58     };
     59 
     60     void merge(const SkPDFGlyphSetMap& usage);
     61     void reset();
     62 
     63     void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
     64                         int numGlyphs);
     65 
     66 private:
     67     SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font);
     68 
     69     SkTDArray<FontGlyphSetPair> fMap;
     70 };
     71 
     72 
     73 /** \class SkPDFFont
     74     A PDF Object class representing a font.  The font may have resources
     75     attached to it in order to embed the font.  SkPDFFonts are canonicalized
     76     so that resource deduplication will only include one copy of a font.
     77     This class uses the same pattern as SkPDFGraphicState, a static weak
     78     reference to each instantiated class.
     79 */
     80 class SkPDFFont : public SkPDFDict {
     81 public:
     82     virtual ~SkPDFFont();
     83 
     84     virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
     85                               SkTSet<SkPDFObject*>* newResourceObjects);
     86 
     87     /** Returns the typeface represented by this class. Returns NULL for the
     88      *  default typeface.
     89      */
     90     SkTypeface* typeface();
     91 
     92     /** Returns the font type represented in this font.  For Type0 fonts,
     93      *  returns the type of the decendant font.
     94      */
     95     virtual SkAdvancedTypefaceMetrics::FontType getType();
     96 
     97     /** Returns true if this font encoding supports glyph IDs above 255.
     98      */
     99     virtual bool multiByteGlyphs() const = 0;
    100 
    101     /** Return true if this font has an encoding for the passed glyph id.
    102      */
    103     bool hasGlyph(uint16_t glyphID);
    104 
    105     /** Convert (in place) the input glyph IDs into the font encoding.  If the
    106      *  font has more glyphs than can be encoded (like a type 1 font with more
    107      *  than 255 glyphs) this method only converts up to the first out of range
    108      *  glyph ID.
    109      *  @param glyphIDs       The input text as glyph IDs.
    110      *  @param numGlyphs      The number of input glyphs.
    111      *  @return               Returns the number of glyphs consumed.
    112      */
    113     size_t glyphsToPDFFontEncoding(uint16_t* glyphIDs, size_t numGlyphs);
    114 
    115     /** Get the font resource for the passed typeface and glyphID. The
    116      *  reference count of the object is incremented and it is the caller's
    117      *  responsibility to unreference it when done.  This is needed to
    118      *  accommodate the weak reference pattern used when the returned object
    119      *  is new and has no other references.
    120      *  @param typeface  The typeface to find.
    121      *  @param glyphID   Specify which section of a large font is of interest.
    122      */
    123     static SkPDFFont* GetFontResource(SkTypeface* typeface,
    124                                              uint16_t glyphID);
    125 
    126     /** Subset the font based on usage set. Returns a SkPDFFont instance with
    127      *  subset.
    128      *  @param usage  Glyph subset requested.
    129      *  @return       NULL if font does not support subsetting, a new instance
    130      *                of SkPDFFont otherwise.
    131      */
    132     virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage);
    133 
    134 protected:
    135     // Common constructor to handle common members.
    136     SkPDFFont(SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface,
    137               SkPDFDict* relatedFontDescriptor);
    138 
    139     // Accessors for subclass.
    140     SkAdvancedTypefaceMetrics* fontInfo();
    141     void setFontInfo(SkAdvancedTypefaceMetrics* info);
    142     uint16_t firstGlyphID() const;
    143     uint16_t lastGlyphID() const;
    144     void setLastGlyphID(uint16_t glyphID);
    145 
    146     // Add object to resource list.
    147     void addResource(SkPDFObject* object);
    148 
    149     // Accessors for FontDescriptor associated with this object.
    150     SkPDFDict* getFontDescriptor();
    151     void setFontDescriptor(SkPDFDict* descriptor);
    152 
    153     // Add common entries to FontDescriptor.
    154     bool addCommonFontDescriptorEntries(int16_t defaultWidth);
    155 
    156     /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs,
    157      *  including the passed glyphID.
    158      */
    159     void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID);
    160 
    161     // Generate ToUnicode table according to glyph usage subset.
    162     // If subset is NULL, all available glyph ids will be used.
    163     void populateToUnicodeTable(const SkPDFGlyphSet* subset);
    164 
    165     // Create instances of derived types based on fontInfo.
    166     static SkPDFFont* Create(SkAdvancedTypefaceMetrics* fontInfo,
    167                              SkTypeface* typeface, uint16_t glyphID,
    168                              SkPDFDict* relatedFontDescriptor);
    169 
    170     static bool Find(uint32_t fontID, uint16_t glyphID, int* index);
    171 
    172 private:
    173     class FontRec {
    174     public:
    175         SkPDFFont* fFont;
    176         uint32_t fFontID;
    177         uint16_t fGlyphID;
    178 
    179         // A fGlyphID of 0 with no fFont always matches.
    180         bool operator==(const FontRec& b) const;
    181         FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID);
    182     };
    183 
    184     SkAutoTUnref<SkTypeface> fTypeface;
    185 
    186     // The glyph IDs accessible with this font.  For Type1 (non CID) fonts,
    187     // this will be a subset if the font has more than 255 glyphs.
    188     uint16_t fFirstGlyphID;
    189     uint16_t fLastGlyphID;
    190     // The font info is only kept around after construction for large
    191     // Type1 (non CID) fonts that need multiple "fonts" to access all glyphs.
    192     SkAutoTUnref<SkAdvancedTypefaceMetrics> fFontInfo;
    193     SkTDArray<SkPDFObject*> fResources;
    194     SkAutoTUnref<SkPDFDict> fDescriptor;
    195 
    196     SkAdvancedTypefaceMetrics::FontType fFontType;
    197 
    198     // This should be made a hash table if performance is a problem.
    199     static SkTDArray<FontRec>& CanonicalFonts();
    200     static SkBaseMutex& CanonicalFontsMutex();
    201 };
    202 
    203 #endif
    204