epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 10 | #ifndef SkPDFFont_DEFINED |
| 11 | #define SkPDFFont_DEFINED |
| 12 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 13 | #include "SkAdvancedTypefaceMetrics.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 14 | #include "SkBitSet.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 15 | #include "SkPDFTypes.h" |
| 16 | #include "SkTDArray.h" |
| 17 | #include "SkThread.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 18 | #include "SkTypeface.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 19 | |
| 20 | class SkPaint; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 21 | class SkPDFCatalog; |
| 22 | class SkPDFFont; |
| 23 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 24 | class SkPDFGlyphSet : SkNoncopyable { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 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); |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 31 | void exportTo(SkTDArray<uint32_t>* glyphIDs) const; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | SkBitSet fBitSet; |
| 35 | }; |
| 36 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 37 | class SkPDFGlyphSetMap : SkNoncopyable { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 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: |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 51 | explicit F2BIter(const SkPDFGlyphSetMap& map); |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 52 | const FontGlyphSetPair* next() const; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 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 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 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 { |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame] | 81 | SK_DECLARE_INST_COUNT(SkPDFFont) |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 82 | public: |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 83 | virtual ~SkPDFFont(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 84 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 85 | virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
| 86 | SkTSet<SkPDFObject*>* newResourceObjects); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 87 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 88 | /** Returns the typeface represented by this class. Returns NULL for the |
| 89 | * default typeface. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 90 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 91 | SkTypeface* typeface(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 92 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 93 | /** Returns the font type represented in this font. For Type0 fonts, |
| 94 | * returns the type of the decendant font. |
| 95 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 96 | virtual SkAdvancedTypefaceMetrics::FontType getType(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 97 | |
| 98 | /** Returns true if this font encoding supports glyph IDs above 255. |
| 99 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 100 | virtual bool multiByteGlyphs() const = 0; |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 101 | |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 102 | /** Returns true if the machine readable licensing bits allow embedding. |
| 103 | */ |
| 104 | bool canEmbed() const; |
| 105 | |
| 106 | /** Returns true if the machine readable licensing bits allow subsetting. |
| 107 | */ |
| 108 | bool canSubset() const; |
| 109 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 110 | /** Return true if this font has an encoding for the passed glyph id. |
| 111 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 112 | bool hasGlyph(uint16_t glyphID); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 113 | |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 114 | /** Convert (in place) the input glyph IDs into the font encoding. If the |
| 115 | * font has more glyphs than can be encoded (like a type 1 font with more |
| 116 | * than 255 glyphs) this method only converts up to the first out of range |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 117 | * glyph ID. |
| 118 | * @param glyphIDs The input text as glyph IDs. |
| 119 | * @param numGlyphs The number of input glyphs. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 120 | * @return Returns the number of glyphs consumed. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 121 | */ |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 122 | int glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 123 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 124 | /** Get the font resource for the passed typeface and glyphID. The |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 125 | * reference count of the object is incremented and it is the caller's |
| 126 | * responsibility to unreference it when done. This is needed to |
| 127 | * accommodate the weak reference pattern used when the returned object |
| 128 | * is new and has no other references. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 129 | * @param typeface The typeface to find. |
| 130 | * @param glyphID Specify which section of a large font is of interest. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 131 | */ |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 132 | static SkPDFFont* GetFontResource(SkTypeface* typeface, uint16_t glyphID); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 133 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 134 | /** Subset the font based on usage set. Returns a SkPDFFont instance with |
| 135 | * subset. |
| 136 | * @param usage Glyph subset requested. |
| 137 | * @return NULL if font does not support subsetting, a new instance |
| 138 | * of SkPDFFont otherwise. |
| 139 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 140 | virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 141 | |
| 142 | protected: |
| 143 | // Common constructor to handle common members. |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 144 | SkPDFFont(const SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 145 | SkPDFDict* relatedFontDescriptor); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 146 | |
| 147 | // Accessors for subclass. |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 148 | const SkAdvancedTypefaceMetrics* fontInfo(); |
| 149 | void setFontInfo(const SkAdvancedTypefaceMetrics* info); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 150 | uint16_t firstGlyphID() const; |
| 151 | uint16_t lastGlyphID() const; |
| 152 | void setLastGlyphID(uint16_t glyphID); |
| 153 | |
| 154 | // Add object to resource list. |
| 155 | void addResource(SkPDFObject* object); |
| 156 | |
| 157 | // Accessors for FontDescriptor associated with this object. |
| 158 | SkPDFDict* getFontDescriptor(); |
| 159 | void setFontDescriptor(SkPDFDict* descriptor); |
| 160 | |
| 161 | // Add common entries to FontDescriptor. |
| 162 | bool addCommonFontDescriptorEntries(int16_t defaultWidth); |
| 163 | |
| 164 | /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, |
| 165 | * including the passed glyphID. |
| 166 | */ |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 167 | void adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 168 | |
| 169 | // Generate ToUnicode table according to glyph usage subset. |
| 170 | // If subset is NULL, all available glyph ids will be used. |
| 171 | void populateToUnicodeTable(const SkPDFGlyphSet* subset); |
| 172 | |
| 173 | // Create instances of derived types based on fontInfo. |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 174 | static SkPDFFont* Create(const SkAdvancedTypefaceMetrics* fontInfo, |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 175 | SkTypeface* typeface, uint16_t glyphID, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 176 | SkPDFDict* relatedFontDescriptor); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 177 | |
| 178 | static bool Find(uint32_t fontID, uint16_t glyphID, int* index); |
| 179 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 180 | private: |
vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 181 | class FontRec { |
| 182 | public: |
| 183 | SkPDFFont* fFont; |
| 184 | uint32_t fFontID; |
| 185 | uint16_t fGlyphID; |
| 186 | |
| 187 | // A fGlyphID of 0 with no fFont always matches. |
| 188 | bool operator==(const FontRec& b) const; |
| 189 | FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); |
| 190 | }; |
vandebo@chromium.org | 6504cfd | 2011-07-23 20:22:53 +0000 | [diff] [blame] | 191 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 192 | SkAutoTUnref<SkTypeface> fTypeface; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 193 | |
| 194 | // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
| 195 | // this will be a subset if the font has more than 255 glyphs. |
| 196 | uint16_t fFirstGlyphID; |
| 197 | uint16_t fLastGlyphID; |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 198 | SkAutoTUnref<const SkAdvancedTypefaceMetrics> fFontInfo; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 199 | SkTDArray<SkPDFObject*> fResources; |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 200 | SkAutoTUnref<SkPDFDict> fDescriptor; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 201 | |
| 202 | SkAdvancedTypefaceMetrics::FontType fFontType; |
| 203 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 204 | // This should be made a hash table if performance is a problem. |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 205 | static SkTDArray<FontRec>& CanonicalFonts(); |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 206 | static SkBaseMutex& CanonicalFontsMutex(); |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame] | 207 | typedef SkPDFDict INHERITED; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | #endif |