vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 9 | #ifndef SkPDFFont_DEFINED |
| 10 | #define SkPDFFont_DEFINED |
| 11 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 12 | #include "SkAdvancedTypefaceMetrics.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 13 | #include "SkBitSet.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 14 | #include "SkPDFTypes.h" |
| 15 | #include "SkTDArray.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 16 | #include "SkTypeface.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 17 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 18 | class SkPDFCanon; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 19 | class SkPDFFont; |
| 20 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 21 | class SkPDFGlyphSet : SkNoncopyable { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 22 | public: |
| 23 | SkPDFGlyphSet(); |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 24 | SkPDFGlyphSet(SkPDFGlyphSet&& o) : fBitSet(std::move(o.fBitSet)) {} |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 25 | |
| 26 | void set(const uint16_t* glyphIDs, int numGlyphs); |
| 27 | bool has(uint16_t glyphID) const; |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 28 | void exportTo(SkTDArray<uint32_t>* glyphIDs) const; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | SkBitSet fBitSet; |
| 32 | }; |
| 33 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 34 | class SkPDFGlyphSetMap : SkNoncopyable { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 35 | public: |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 36 | struct FontGlyphSetPair : SkNoncopyable { |
| 37 | FontGlyphSetPair() : fFont(nullptr) {} |
| 38 | FontGlyphSetPair(FontGlyphSetPair&& o) |
| 39 | : fFont(o.fFont) |
| 40 | , fGlyphSet(std::move(o.fGlyphSet)) { |
| 41 | o.fFont = nullptr; |
| 42 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 43 | SkPDFFont* fFont; |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 44 | SkPDFGlyphSet fGlyphSet; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | SkPDFGlyphSetMap(); |
| 48 | ~SkPDFGlyphSetMap(); |
| 49 | |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 50 | const FontGlyphSetPair* begin() const { return fMap.begin(); } |
| 51 | const FontGlyphSetPair* end() const { return fMap.end(); } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 52 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 53 | void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, |
| 54 | int numGlyphs); |
| 55 | |
| 56 | private: |
| 57 | SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font); |
| 58 | |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 59 | SkTArray<FontGlyphSetPair> fMap; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 62 | |
| 63 | /** \class SkPDFFont |
| 64 | A PDF Object class representing a font. The font may have resources |
| 65 | attached to it in order to embed the font. SkPDFFonts are canonicalized |
| 66 | so that resource deduplication will only include one copy of a font. |
| 67 | This class uses the same pattern as SkPDFGraphicState, a static weak |
| 68 | reference to each instantiated class. |
| 69 | */ |
| 70 | class SkPDFFont : public SkPDFDict { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 71 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 72 | public: |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 73 | virtual ~SkPDFFont(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 74 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | /** Returns the typeface represented by this class. Returns nullptr for the |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 76 | * default typeface. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 77 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 78 | SkTypeface* typeface(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 79 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 80 | /** Returns the font type represented in this font. For Type0 fonts, |
| 81 | * returns the type of the decendant font. |
| 82 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 83 | virtual SkAdvancedTypefaceMetrics::FontType getType(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 84 | |
| 85 | /** Returns true if this font encoding supports glyph IDs above 255. |
| 86 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 87 | virtual bool multiByteGlyphs() const = 0; |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 88 | |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 89 | /** Returns true if the machine readable licensing bits allow embedding. |
| 90 | */ |
| 91 | bool canEmbed() const; |
| 92 | |
| 93 | /** Returns true if the machine readable licensing bits allow subsetting. |
| 94 | */ |
| 95 | bool canSubset() const; |
| 96 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 97 | /** Return true if this font has an encoding for the passed glyph id. |
| 98 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 99 | bool hasGlyph(uint16_t glyphID); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 100 | |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 101 | /** Convert (in place) the input glyph IDs into the font encoding. If the |
| 102 | * font has more glyphs than can be encoded (like a type 1 font with more |
| 103 | * 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] | 104 | * glyph ID. |
| 105 | * @param glyphIDs The input text as glyph IDs. |
| 106 | * @param numGlyphs The number of input glyphs. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 107 | * @return Returns the number of glyphs consumed. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 108 | */ |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 109 | int glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 110 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 111 | /** Get the font resource for the passed typeface and glyphID. The |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 112 | * reference count of the object is incremented and it is the caller's |
| 113 | * responsibility to unreference it when done. This is needed to |
| 114 | * accommodate the weak reference pattern used when the returned object |
| 115 | * is new and has no other references. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 116 | * @param typeface The typeface to find. |
| 117 | * @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] | 118 | */ |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 119 | static SkPDFFont* GetFontResource(SkPDFCanon* canon, |
| 120 | SkTypeface* typeface, |
| 121 | uint16_t glyphID); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 122 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame^] | 123 | static sk_sp<const SkAdvancedTypefaceMetrics> GetFontMetricsWithGlyphNames( |
| 124 | SkTypeface*, uint32_t* glyphs, uint32_t glyphsCount); |
| 125 | |
| 126 | static sk_sp<const SkAdvancedTypefaceMetrics> GetFontMetricsWithToUnicode( |
| 127 | SkTypeface*, uint32_t* glyphs, uint32_t glyphsCount); |
| 128 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 129 | /** Subset the font based on usage set. Returns a SkPDFFont instance with |
| 130 | * subset. |
| 131 | * @param usage Glyph subset requested. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | * @return nullptr if font does not support subsetting, a new instance |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 133 | * of SkPDFFont otherwise. |
| 134 | */ |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 135 | virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 136 | |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 137 | enum Match { |
| 138 | kExact_Match, |
| 139 | kRelated_Match, |
| 140 | kNot_Match, |
| 141 | }; |
| 142 | static Match IsMatch(SkPDFFont* existingFont, |
| 143 | uint32_t existingFontID, |
| 144 | uint16_t existingGlyphID, |
| 145 | uint32_t searchFontID, |
| 146 | uint16_t searchGlyphID); |
| 147 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Return false iff the typeface has its NotEmbeddable flag set. |
| 150 | * If typeface is NULL, the default typeface is checked. |
| 151 | */ |
| 152 | static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*); |
| 153 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 154 | protected: |
| 155 | // Common constructor to handle common members. |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 156 | SkPDFFont(const SkAdvancedTypefaceMetrics* fontInfo, |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 157 | SkTypeface* typeface, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 158 | SkPDFDict* relatedFontDescriptor); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 159 | |
| 160 | // Accessors for subclass. |
halcanary | fb747e2 | 2014-07-11 19:45:23 -0700 | [diff] [blame] | 161 | const SkAdvancedTypefaceMetrics* fontInfo(); |
| 162 | void setFontInfo(const SkAdvancedTypefaceMetrics* info); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 163 | uint16_t firstGlyphID() const; |
| 164 | uint16_t lastGlyphID() const; |
| 165 | void setLastGlyphID(uint16_t glyphID); |
| 166 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 167 | // Accessors for FontDescriptor associated with this object. |
| 168 | SkPDFDict* getFontDescriptor(); |
| 169 | void setFontDescriptor(SkPDFDict* descriptor); |
| 170 | |
| 171 | // Add common entries to FontDescriptor. |
| 172 | bool addCommonFontDescriptorEntries(int16_t defaultWidth); |
| 173 | |
| 174 | /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, |
| 175 | * including the passed glyphID. |
| 176 | */ |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 177 | void adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 178 | |
| 179 | // Generate ToUnicode table according to glyph usage subset. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 180 | // If subset is nullptr, all available glyph ids will be used. |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 181 | void populateToUnicodeTable(const SkPDFGlyphSet* subset); |
| 182 | |
| 183 | // Create instances of derived types based on fontInfo. |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 184 | static SkPDFFont* Create(SkPDFCanon* canon, |
| 185 | const SkAdvancedTypefaceMetrics* fontInfo, |
| 186 | SkTypeface* typeface, |
| 187 | uint16_t glyphID, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 188 | SkPDFDict* relatedFontDescriptor); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 189 | |
| 190 | static bool Find(uint32_t fontID, uint16_t glyphID, int* index); |
| 191 | |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 192 | void drop() override; |
| 193 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 194 | private: |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 195 | sk_sp<SkTypeface> fTypeface; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 196 | |
| 197 | // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
| 198 | // this will be a subset if the font has more than 255 glyphs. |
| 199 | uint16_t fFirstGlyphID; |
| 200 | uint16_t fLastGlyphID; |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 201 | sk_sp<const SkAdvancedTypefaceMetrics> fFontInfo; |
| 202 | sk_sp<SkPDFDict> fDescriptor; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 203 | |
| 204 | SkAdvancedTypefaceMetrics::FontType fFontType; |
| 205 | |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame] | 206 | typedef SkPDFDict INHERITED; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | #endif |