OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #ifndef SkPDFFont_DEFINED | 9 #ifndef SkPDFFont_DEFINED |
10 #define SkPDFFont_DEFINED | 10 #define SkPDFFont_DEFINED |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, | 54 void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, |
55 int numGlyphs); | 55 int numGlyphs); |
56 | 56 |
57 private: | 57 private: |
58 SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font); | 58 SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font); |
59 | 59 |
60 SkTArray<FontGlyphSetPair> fMap; | 60 SkTArray<FontGlyphSetPair> fMap; |
61 }; | 61 }; |
62 | 62 |
| 63 |
63 /** \class SkPDFFont | 64 /** \class SkPDFFont |
64 A PDF Object class representing a font. The font may have resources | 65 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 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 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 This class uses the same pattern as SkPDFGraphicState, a static weak |
68 reference to each instantiated class. | 69 reference to each instantiated class. |
69 */ | 70 */ |
70 class SkPDFFont : public SkPDFDict { | 71 class SkPDFFont : public SkPDFDict { |
71 | 72 |
72 public: | 73 public: |
73 virtual ~SkPDFFont(); | 74 virtual ~SkPDFFont(); |
74 | 75 |
75 /** Returns the typeface represented by this class. Returns nullptr for the | 76 /** Returns the typeface represented by this class. Returns nullptr for the |
76 * default typeface. | 77 * default typeface. |
77 */ | 78 */ |
78 SkTypeface* typeface() const { return fTypeface.get(); } | 79 SkTypeface* typeface() { return fTypeface.get(); } |
79 | 80 |
80 /** Returns the font type represented in this font. For Type0 fonts, | 81 /** Returns the font type represented in this font. For Type0 fonts, |
81 * returns the type of the decendant font. | 82 * returns the type of the decendant font. |
82 */ | 83 */ |
83 SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; } | 84 SkAdvancedTypefaceMetrics::FontType getType() { return fFontType; } |
84 | |
85 static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) { | |
86 return type == SkAdvancedTypefaceMetrics::kType1CID_Font || | |
87 type == SkAdvancedTypefaceMetrics::kTrueType_Font; | |
88 } | |
89 | 85 |
90 /** Returns true if this font encoding supports glyph IDs above 255. | 86 /** Returns true if this font encoding supports glyph IDs above 255. |
91 */ | 87 */ |
92 bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType()
); } | 88 bool multiByteGlyphs() const { return fMultiByteGlyphs; } |
| 89 |
| 90 /** Returns true if the machine readable licensing bits allow embedding. |
| 91 */ |
| 92 bool canEmbed() const; |
| 93 |
| 94 /** Returns true if the machine readable licensing bits allow subsetting. |
| 95 */ |
| 96 bool canSubset() const; |
93 | 97 |
94 /** Return true if this font has an encoding for the passed glyph id. | 98 /** Return true if this font has an encoding for the passed glyph id. |
95 */ | 99 */ |
96 bool hasGlyph(SkGlyphID gid) { | 100 bool hasGlyph(uint16_t glyphID); |
97 return (gid >= fFirstGlyphID && gid <= fLastGlyphID) || gid == 0; | |
98 } | |
99 | 101 |
100 /** Convert (in place) the input glyph IDs into the font encoding. If the | 102 /** Convert (in place) the input glyph IDs into the font encoding. If the |
101 * font has more glyphs than can be encoded (like a type 1 font with more | 103 * font has more glyphs than can be encoded (like a type 1 font with more |
102 * than 255 glyphs) this method only converts up to the first out of range | 104 * than 255 glyphs) this method only converts up to the first out of range |
103 * glyph ID. | 105 * glyph ID. |
104 * @param glyphIDs The input text as glyph IDs. | 106 * @param glyphIDs The input text as glyph IDs. |
105 * @param numGlyphs The number of input glyphs. | 107 * @param numGlyphs The number of input glyphs. |
106 * @return Returns the number of glyphs consumed. | 108 * @return Returns the number of glyphs consumed. |
107 */ | 109 */ |
108 int glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const; | 110 int glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const; |
109 /** | 111 /** |
110 * Like above, but does not modify glyphIDs array. | 112 * Like above, but does not modify glyphIDs array. |
111 */ | 113 */ |
112 int glyphsToPDFFontEncodingCount(const SkGlyphID* glyphIDs, | 114 int glyphsToPDFFontEncodingCount(const SkGlyphID* glyphIDs, |
113 int numGlyphs) const; | 115 int numGlyphs) const; |
114 | 116 |
115 /** Get the font resource for the passed typeface and glyphID. The | 117 /** 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 | 118 * reference count of the object is incremented and it is the caller's |
117 * responsibility to unreference it when done. This is needed to | 119 * responsibility to unreference it when done. This is needed to |
118 * accommodate the weak reference pattern used when the returned object | 120 * accommodate the weak reference pattern used when the returned object |
119 * is new and has no other references. | 121 * is new and has no other references. |
120 * @param typeface The typeface to find. | 122 * @param typeface The typeface to find. |
121 * @param glyphID Specify which section of a large font is of interest. | 123 * @param glyphID Specify which section of a large font is of interest. |
122 */ | 124 */ |
123 static SkPDFFont* GetFontResource(SkPDFCanon* canon, | 125 static SkPDFFont* GetFontResource(SkPDFCanon* canon, |
124 SkTypeface* typeface, | 126 SkTypeface* typeface, |
125 SkGlyphID glyphID); | 127 uint16_t glyphID); |
126 | 128 |
127 // Uses (kGlyphNames_PerGlyphInfo | kToUnicode_PerGlyphInfo). | 129 static sk_sp<const SkAdvancedTypefaceMetrics> GetFontMetricsWithToUnicode( |
128 static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface, | 130 SkTypeface*, uint32_t* glyphs, uint32_t glyphsCount); |
129 SkPDFCanon* canon); | |
130 | 131 |
131 /** Subset the font based on usage set. Returns a SkPDFFont instance with | 132 /** Subset the font based on usage set. Returns a SkPDFFont instance with |
132 * subset. | 133 * subset. |
133 * @param usage Glyph subset requested. | 134 * @param usage Glyph subset requested. |
134 * @return nullptr if font does not support subsetting, a new instanc
e | 135 * @return nullptr if font does not support subsetting, a new instanc
e |
135 * of SkPDFFont otherwise. | 136 * of SkPDFFont otherwise. |
136 */ | 137 */ |
137 virtual sk_sp<SkPDFObject> getFontSubset(SkPDFCanon* canon, | 138 virtual sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage); |
138 const SkPDFGlyphSet* usage); | 139 |
| 140 enum Match { |
| 141 kExact_Match, |
| 142 kRelated_Match, |
| 143 kNot_Match, |
| 144 }; |
| 145 static Match IsMatch(SkPDFFont* existingFont, |
| 146 uint32_t existingFontID, |
| 147 uint16_t existingGlyphID, |
| 148 uint32_t searchFontID, |
| 149 uint16_t searchGlyphID); |
139 | 150 |
140 /** | 151 /** |
141 * Return false iff the typeface has its NotEmbeddable flag set. | 152 * Return false iff the typeface has its NotEmbeddable flag set. |
142 * If typeface is NULL, the default typeface is checked. | 153 * If typeface is NULL, the default typeface is checked. |
143 */ | 154 */ |
144 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*); | 155 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*); |
145 | 156 |
146 protected: | 157 protected: |
147 // Common constructor to handle common members. | 158 // Common constructor to handle common members. |
148 SkPDFFont(sk_sp<SkTypeface> typeface, | 159 SkPDFFont(sk_sp<const SkAdvancedTypefaceMetrics> fontInfo, |
149 SkAdvancedTypefaceMetrics::FontType fontType); | 160 sk_sp<SkTypeface> typeface, |
| 161 sk_sp<SkPDFDict> relatedFontDescriptor, |
| 162 SkAdvancedTypefaceMetrics::FontType fontType, |
| 163 bool multiByteGlyphs); |
150 | 164 |
| 165 // Accessors for subclass. |
| 166 const SkAdvancedTypefaceMetrics* getFontInfo() const { return fFontInfo.get(
); } |
| 167 sk_sp<const SkAdvancedTypefaceMetrics> refFontInfo() const { return fFontInf
o; } |
| 168 |
| 169 void setFontInfo(sk_sp<const SkAdvancedTypefaceMetrics> info); |
151 SkGlyphID firstGlyphID() const { return fFirstGlyphID; } | 170 SkGlyphID firstGlyphID() const { return fFirstGlyphID; } |
152 SkGlyphID lastGlyphID() const { return fLastGlyphID; } | 171 SkGlyphID lastGlyphID() const { return fLastGlyphID; } |
| 172 void setLastGlyphID(uint16_t glyphID); |
| 173 |
| 174 // Accessors for FontDescriptor associated with this object. |
| 175 SkPDFDict* getFontDescriptor() const { return fDescriptor.get(); } |
| 176 sk_sp<SkPDFDict> refFontDescriptor() const { return fDescriptor; } |
| 177 void setFontDescriptor(sk_sp<SkPDFDict> descriptor); |
153 | 178 |
154 sk_sp<SkTypeface> refTypeface() const { return fTypeface; } | 179 sk_sp<SkTypeface> refTypeface() const { return fTypeface; } |
155 | 180 |
156 /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, | 181 /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, |
157 * including the passed glyphID. | 182 * including the passed glyphID. |
158 */ | 183 */ |
159 void adjustGlyphRangeForSingleByteEncoding(SkGlyphID glyphID); | 184 void adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID); |
| 185 |
| 186 // Generate ToUnicode table according to glyph usage subset. |
| 187 // If subset is nullptr, all available glyph ids will be used. |
| 188 void populateToUnicodeTable(const SkPDFGlyphSet* subset); |
| 189 |
| 190 static bool Find(uint32_t fontID, uint16_t glyphID, int* index); |
160 | 191 |
161 void drop() override; | 192 void drop() override; |
162 | 193 |
163 private: | 194 private: |
164 sk_sp<SkTypeface> fTypeface; | 195 sk_sp<SkTypeface> fTypeface; |
| 196 sk_sp<const SkAdvancedTypefaceMetrics> fFontInfo; |
| 197 sk_sp<SkPDFDict> fDescriptor; |
165 | 198 |
166 // The glyph IDs accessible with this font. For Type1 (non CID) fonts, | 199 // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
167 // this will be a subset if the font has more than 255 glyphs. | 200 // this will be a subset if the font has more than 255 glyphs. |
168 SkGlyphID fFirstGlyphID; | 201 SkGlyphID fFirstGlyphID; |
169 SkGlyphID fLastGlyphID; | 202 SkGlyphID fLastGlyphID; |
170 SkAdvancedTypefaceMetrics::FontType fFontType; | 203 SkAdvancedTypefaceMetrics::FontType fFontType; |
| 204 bool fMultiByteGlyphs; |
171 | 205 |
172 typedef SkPDFDict INHERITED; | 206 typedef SkPDFDict INHERITED; |
173 }; | 207 }; |
174 | 208 |
175 #endif | 209 #endif |
OLD | NEW |