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 #include "SkData.h" | 8 #include "SkData.h" |
9 #include "SkGlyphCache.h" | 9 #include "SkGlyphCache.h" |
10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
32 #endif | 32 #endif |
33 | 33 |
34 namespace { | 34 namespace { |
35 // PDF's notion of symbolic vs non-symbolic is related to the character set, not | 35 // PDF's notion of symbolic vs non-symbolic is related to the character set, not |
36 // symbols vs. characters. Rarely is a font the right character set to call it | 36 // symbols vs. characters. Rarely is a font the right character set to call it |
37 // non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) | 37 // non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) |
38 static const int kPdfSymbolic = 4; | 38 static const int kPdfSymbolic = 4; |
39 | 39 |
40 class SkPDFType0Font final : public SkPDFFont { | 40 class SkPDFType0Font final : public SkPDFFont { |
41 public: | 41 public: |
42 SkPDFType0Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 42 SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
43 sk_sp<SkTypeface> typeface, | 43 sk_sp<SkTypeface> typeface, |
44 SkAdvancedTypefaceMetrics::FontType type); | 44 SkAdvancedTypefaceMetrics::FontType type); |
45 virtual ~SkPDFType0Font(); | 45 virtual ~SkPDFType0Font(); |
46 sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; | 46 sk_sp<SkPDFObject> getFontSubset(SkPDFCanon*, const SkPDFGlyphSet*) override ; |
47 #ifdef SK_DEBUG | 47 #ifdef SK_DEBUG |
48 void emitObject(SkWStream*, | 48 void emitObject(SkWStream*, |
49 const SkPDFObjNumMap&, | 49 const SkPDFObjNumMap&, |
50 const SkPDFSubstituteMap&) const override; | 50 const SkPDFSubstituteMap&) const override; |
51 #endif | 51 #endif |
52 | 52 |
53 private: | 53 private: |
54 #ifdef SK_DEBUG | 54 #ifdef SK_DEBUG |
55 bool fPopulated; | 55 bool fPopulated; |
56 #endif | 56 #endif |
57 bool populate(const SkPDFGlyphSet* subset); | 57 bool populate(const SkPDFGlyphSet* subset, |
58 const SkAdvancedTypefaceMetrics& metrics); | |
58 typedef SkPDFDict INHERITED; | 59 typedef SkPDFDict INHERITED; |
59 }; | 60 }; |
60 | 61 |
61 class SkPDFType1Font final : public SkPDFFont { | 62 class SkPDFType1Font final : public SkPDFFont { |
62 public: | 63 public: |
63 SkPDFType1Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 64 SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, |
64 sk_sp<SkTypeface> typeface, | 65 sk_sp<SkTypeface> typeface, |
65 uint16_t glyphID, | 66 uint16_t glyphID, |
66 sk_sp<SkPDFDict> relatedFontDescriptor); | 67 SkPDFCanon* canon); |
67 virtual ~SkPDFType1Font(); | 68 virtual ~SkPDFType1Font() {} |
68 | |
69 private: | |
70 bool populate(int16_t glyphID); | |
71 bool addFontDescriptor(int16_t defaultWidth); | |
72 }; | 69 }; |
73 | 70 |
74 class SkPDFType3Font final : public SkPDFFont { | 71 class SkPDFType3Font final : public SkPDFFont { |
75 public: | 72 public: |
76 SkPDFType3Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 73 SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
77 sk_sp<SkTypeface> typeface, | 74 sk_sp<SkTypeface> typeface, |
78 SkAdvancedTypefaceMetrics::FontType fontType, | 75 SkAdvancedTypefaceMetrics::FontType fontType, |
79 uint16_t glyphID); | 76 uint16_t glyphID); |
80 virtual ~SkPDFType3Font() {} | 77 virtual ~SkPDFType3Font() {} |
81 void emitObject(SkWStream*, | 78 void emitObject(SkWStream*, |
82 const SkPDFObjNumMap&, | 79 const SkPDFObjNumMap&, |
83 const SkPDFSubstituteMap&) const override { | 80 const SkPDFSubstituteMap&) const override { |
84 SkDEBUGFAIL("should call getFontSubset!"); | 81 SkDEBUGFAIL("should call getFontSubset!"); |
85 } | 82 } |
86 sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; | 83 sk_sp<SkPDFObject> getFontSubset(SkPDFCanon*, const SkPDFGlyphSet*) override ; |
87 }; | 84 }; |
88 | 85 |
89 /////////////////////////////////////////////////////////////////////////////// | 86 /////////////////////////////////////////////////////////////////////////////// |
90 // File-Local Functions | 87 // File-Local Functions |
91 /////////////////////////////////////////////////////////////////////////////// | 88 /////////////////////////////////////////////////////////////////////////////// |
92 | 89 |
93 static SkAutoGlyphCache vector_cache(SkTypeface* face, SkScalar size = 0) { | 90 static SkAutoGlyphCache vector_cache(SkTypeface* face, SkScalar size = 0) { |
94 SkPaint tmpPaint; | 91 SkPaint tmpPaint; |
95 tmpPaint.setHinting(SkPaint::kNo_Hinting); | 92 tmpPaint.setHinting(SkPaint::kNo_Hinting); |
96 tmpPaint.setTypeface(sk_ref_sp(face)); | 93 tmpPaint.setTypeface(sk_ref_sp(face)); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 * some additional state indicating which subset of the font is used. It | 206 * some additional state indicating which subset of the font is used. It |
210 * must be maintained at the page granularity and then combined at the document | 207 * must be maintained at the page granularity and then combined at the document |
211 * granularity. a) change SkPDFFont to fill in its state on demand, kind of | 208 * granularity. a) change SkPDFFont to fill in its state on demand, kind of |
212 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each | 209 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each |
213 * page/pdf device. c) in the document, retrieve the per font glyph usage | 210 * page/pdf device. c) in the document, retrieve the per font glyph usage |
214 * from each page and combine it and ask for a resource with that subset. | 211 * from each page and combine it and ask for a resource with that subset. |
215 */ | 212 */ |
216 | 213 |
217 SkPDFFont::~SkPDFFont() {} | 214 SkPDFFont::~SkPDFFont() {} |
218 | 215 |
219 bool SkPDFFont::canEmbed() const { | 216 static bool can_embed(const SkAdvancedTypefaceMetrics* metrics) { |
220 if (!fFontInfo.get()) { | 217 return !metrics || |
221 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); | 218 !SkToBool(metrics->fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable _FontFlag); |
222 return true; | |
223 } | |
224 return (fFontInfo->fFlags & | |
225 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) == 0; | |
226 } | 219 } |
227 | 220 |
228 bool SkPDFFont::canSubset() const { | 221 static bool can_subset(const SkAdvancedTypefaceMetrics* metrics) { |
229 if (!fFontInfo.get()) { | 222 return !metrics || |
230 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); | 223 !SkToBool(metrics->fFlags & SkAdvancedTypefaceMetrics::kNotSubsettabl e_FontFlag); |
231 return true; | |
232 } | |
233 return (fFontInfo->fFlags & | |
234 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag) == 0; | |
235 } | |
236 | |
237 bool SkPDFFont::hasGlyph(uint16_t id) { | |
238 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; | |
239 } | 224 } |
240 | 225 |
241 int SkPDFFont::glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const { | 226 int SkPDFFont::glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const { |
242 // A font with multibyte glyphs will support all glyph IDs in a single font. | 227 // A font with multibyte glyphs will support all glyph IDs in a single font. |
243 if (this->multiByteGlyphs()) { | 228 if (this->multiByteGlyphs()) { |
244 return numGlyphs; | 229 return numGlyphs; |
245 } | 230 } |
246 | 231 |
247 for (int i = 0; i < numGlyphs; i++) { | 232 for (int i = 0; i < numGlyphs; i++) { |
248 if (glyphIDs[i] == 0) { | 233 if (glyphIDs[i] == 0) { |
(...skipping 15 matching lines...) Expand all Loading... | |
264 } | 249 } |
265 for (int i = 0; i < numGlyphs; i++) { | 250 for (int i = 0; i < numGlyphs; i++) { |
266 if (glyphIDs[i] != 0 && | 251 if (glyphIDs[i] != 0 && |
267 (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID)) { | 252 (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID)) { |
268 return i; | 253 return i; |
269 } | 254 } |
270 } | 255 } |
271 return numGlyphs; | 256 return numGlyphs; |
272 } | 257 } |
273 | 258 |
274 // static | 259 |
260 const SkAdvancedTypefaceMetrics* SkPDFFont::GetMetrics(SkTypeface* typeface, | |
261 SkPDFCanon* canon) { | |
262 SkFontID id = SkTypeface::UniqueID(typeface); | |
263 if (SkAdvancedTypefaceMetrics** ptr = canon->fTypefaceMetrics.find(id)) { | |
264 return *ptr; | |
265 } | |
266 sk_sp<SkTypeface> defaultFace; | |
267 if (!typeface) { | |
268 defaultFace = SkTypeface::MakeDefault(); | |
269 typeface = defaultFace.get(); | |
270 } | |
271 sk_sp<SkAdvancedTypefaceMetrics> metrics( | |
272 typeface->getAdvancedTypefaceMetrics( | |
273 SkTypeface::kGlyphNames_PerGlyphInfo | | |
274 SkTypeface::kToUnicode_PerGlyphInfo, nullptr, 0)); | |
bungeman-skia
2016/08/17 19:53:51
It's really strange formatting to break a single p
hal.canary
2016/08/17 20:14:07
Done.
| |
275 return *canon->fTypefaceMetrics.set(id, metrics.release()); | |
276 } | |
277 | |
278 SkAdvancedTypefaceMetrics::FontType font_type(const SkAdvancedTypefaceMetrics* m etrics) { | |
279 if (!metrics || SkToBool(metrics->fFlags & | |
280 SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { | |
281 // force Type3 fallback. | |
282 return SkAdvancedTypefaceMetrics::kOther_Font; | |
283 } | |
284 return metrics->fType; | |
285 } | |
286 | |
287 static SkGlyphID first_glyph_for_single_byte_encoding(SkGlyphID gid) { | |
288 return gid != 0 ? gid - (gid - 1) % 255 : 1; | |
289 } | |
290 | |
275 SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, | 291 SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, |
276 SkTypeface* face, | 292 SkTypeface* face, |
277 uint16_t glyphID) { | 293 SkGlyphID glyphID) { |
278 SkASSERT(canon); | 294 SkASSERT(canon); |
279 const uint32_t fontID = SkTypeface::UniqueID(face); | 295 const SkAdvancedTypefaceMetrics* fontMetrics = SkPDFFont::GetMetrics(face, c anon); |
280 SkPDFFont* relatedFont; | 296 SkAdvancedTypefaceMetrics::FontType type = font_type(fontMetrics); |
281 if (SkPDFFont* pdfFont = canon->findFont(fontID, glyphID, &relatedFont)) { | 297 bool multibyte = SkPDFFont::IsMultiByte(type); |
282 return SkRef(pdfFont); | 298 SkGlyphID firstGlyph = multibyte ? 0 : first_glyph_for_single_byte_encoding( glyphID); |
299 uint64_t fontID = (SkTypeface::UniqueID(face) << 16) | firstGlyph; | |
300 | |
301 if (SkPDFFont** found = canon->fFontMap.find(fontID)) { | |
302 SkASSERT(multibyte == (*found)->multiByteGlyphs()); | |
303 return SkRef(*found); | |
283 } | 304 } |
305 | |
284 sk_sp<SkTypeface> typeface(face ? sk_ref_sp(face) : SkTypeface::MakeDefault( )); | 306 sk_sp<SkTypeface> typeface(face ? sk_ref_sp(face) : SkTypeface::MakeDefault( )); |
285 SkASSERT(typeface); | 307 SkASSERT(typeface); |
286 int glyphCount = typeface->countGlyphs(); | 308 int glyphCount = typeface->countGlyphs(); |
309 // Validate typeface + glyph; | |
287 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. | 310 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. |
288 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount | 311 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount |
289 glyphID >= glyphCount) { // invalid glyph | 312 glyphID >= glyphCount) { // invalid glyph |
290 return nullptr; | 313 return nullptr; |
291 } | 314 } |
292 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; | |
293 sk_sp<SkPDFDict> relatedFontDescriptor; | |
294 if (relatedFont) { | |
295 fontMetrics = relatedFont->refFontInfo(); | |
296 relatedFontDescriptor = relatedFont->refFontDescriptor(); | |
297 | |
298 // This only is to catch callers who pass invalid glyph ids. | |
299 // If glyph id is invalid, then we will create duplicate entries | |
300 // for TrueType fonts. | |
301 SkDEBUGCODE(SkAdvancedTypefaceMetrics::FontType fontType = relatedFont-> getType()); | |
302 SkASSERT(fontType != SkAdvancedTypefaceMetrics::kType1CID_Font); | |
303 SkASSERT(fontType != SkAdvancedTypefaceMetrics::kTrueType_Font); | |
304 } else { | |
305 SkTypeface::PerGlyphInfo info = SkTypeface::kGlyphNames_PerGlyphInfo | | |
306 SkTypeface::kToUnicode_PerGlyphInfo; | |
307 fontMetrics.reset( | |
308 typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); | |
309 } | |
310 | |
311 SkAdvancedTypefaceMetrics::FontType type = | |
312 fontMetrics ? fontMetrics->fType : SkAdvancedTypefaceMetrics::kOther_Fon t; | |
313 if (fontMetrics && | |
314 SkToBool(fontMetrics->fFlags & | |
315 SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { | |
316 // force Type3 fallback. | |
317 type = SkAdvancedTypefaceMetrics::kOther_Font; | |
318 } | |
319 | 315 |
320 sk_sp<SkPDFFont> font; | 316 sk_sp<SkPDFFont> font; |
321 switch (type) { | 317 switch (type) { |
322 case SkAdvancedTypefaceMetrics::kType1CID_Font: | 318 case SkAdvancedTypefaceMetrics::kType1CID_Font: |
323 case SkAdvancedTypefaceMetrics::kTrueType_Font: | 319 case SkAdvancedTypefaceMetrics::kTrueType_Font: |
324 SkASSERT(relatedFontDescriptor == nullptr); | 320 SkASSERT(multibyte); |
325 SkASSERT(fontMetrics != nullptr); | 321 SkASSERT(fontMetrics != nullptr); |
326 font = sk_make_sp<SkPDFType0Font>(std::move(fontMetrics), | 322 font = sk_make_sp<SkPDFType0Font>(fontMetrics, |
327 std::move(typeface), | 323 std::move(typeface), |
328 type); | 324 type); |
329 break; | 325 break; |
330 case SkAdvancedTypefaceMetrics::kType1_Font: | 326 case SkAdvancedTypefaceMetrics::kType1_Font: |
327 SkASSERT(!multibyte); | |
331 SkASSERT(fontMetrics != nullptr); | 328 SkASSERT(fontMetrics != nullptr); |
332 font = sk_make_sp<SkPDFType1Font>(std::move(fontMetrics), | 329 font = sk_make_sp<SkPDFType1Font>(fontMetrics, |
333 std::move(typeface), | 330 std::move(typeface), |
334 glyphID, | 331 glyphID, |
335 std::move(relatedFontDescriptor)); | 332 canon); |
336 break; | 333 break; |
337 case SkAdvancedTypefaceMetrics::kCFF_Font: | 334 default: |
338 SkASSERT(fontMetrics != nullptr); | 335 SkASSERT(!multibyte); |
339 // fallthrough | 336 font = sk_make_sp<SkPDFType3Font>(fontMetrics, |
340 case SkAdvancedTypefaceMetrics::kOther_Font: | |
341 font = sk_make_sp<SkPDFType3Font>(std::move(fontMetrics), | |
342 std::move(typeface), | 337 std::move(typeface), |
343 type, | 338 type, |
344 glyphID); | 339 glyphID); |
345 break; | 340 break; |
346 default: | |
347 SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); | |
348 return nullptr; | |
349 } | 341 } |
350 // When firstGlyphID==0, SkFont::IsMatch() matches all glyphs in font. | 342 canon->fFontMap.set(fontID, SkRef(font.get())); |
351 SkGlyphID firstGlyphID = font->multiByteGlyphs() ? 0 : font->fFirstGlyphID; | |
352 // TODO(halcanary) Make SkCanon::addFont take sk_sp<SkPDFFont>. | |
353 canon->addFont(font.get(), fontID, firstGlyphID); | |
354 return font.release(); // TODO(halcanary) return sk_sp<SkPDFFont>. | 343 return font.release(); // TODO(halcanary) return sk_sp<SkPDFFont>. |
355 } | 344 } |
356 | 345 |
357 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { | 346 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(SkPDFCanon*, const SkPDFGlyphSet*) { |
358 return nullptr; // Default: no support. | 347 return nullptr; // Default: no support. |
359 } | 348 } |
360 | 349 |
361 // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> | 350 SkPDFFont::SkPDFFont(sk_sp<SkTypeface> typeface, |
362 SkPDFFont::SkPDFFont(sk_sp<const SkAdvancedTypefaceMetrics> info, | 351 SkAdvancedTypefaceMetrics::FontType fontType) |
363 sk_sp<SkTypeface> typeface, | |
364 sk_sp<SkPDFDict> relatedFontDescriptor, | |
365 SkAdvancedTypefaceMetrics::FontType fontType, | |
366 bool multiByteGlyphs) | |
367 : SkPDFDict("Font") | 352 : SkPDFDict("Font") |
368 , fTypeface(std::move(typeface)) | 353 , fTypeface(std::move(typeface)) |
369 , fFontInfo(std::move(info)) | |
370 , fDescriptor(std::move(relatedFontDescriptor)) | |
371 , fFirstGlyphID(1) | 354 , fFirstGlyphID(1) |
372 , fFontType(fontType) | 355 , fFontType(fontType) { |
373 , fMultiByteGlyphs(multiByteGlyphs) { | |
374 SkASSERT(fTypeface); | 356 SkASSERT(fTypeface); |
375 fLastGlyphID = fFontInfo ? fFontInfo->fLastGlyphID : 0; | 357 fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); |
376 if (0 == fLastGlyphID) { | |
377 fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); | |
378 } | |
379 } | |
380 | |
381 void SkPDFFont::setFontInfo(sk_sp<const SkAdvancedTypefaceMetrics> info) { | |
382 if (info) { | |
383 fFontInfo = std::move(info); | |
384 } | |
385 } | |
386 | |
387 void SkPDFFont::setLastGlyphID(uint16_t glyphID) { | |
388 fLastGlyphID = glyphID; | |
389 } | |
390 | |
391 void SkPDFFont::setFontDescriptor(sk_sp<SkPDFDict> descriptor) { | |
392 fDescriptor = std::move(descriptor); | |
393 } | 358 } |
394 | 359 |
395 static void add_common_font_descriptor_entries(SkPDFDict* descriptor, | 360 static void add_common_font_descriptor_entries(SkPDFDict* descriptor, |
396 const SkAdvancedTypefaceMetrics& metrics, | 361 const SkAdvancedTypefaceMetrics& metrics, |
397 int16_t defaultWidth) { | 362 int16_t defaultWidth) { |
398 const uint16_t emSize = metrics.fEmSize; | 363 const uint16_t emSize = metrics.fEmSize; |
399 descriptor->insertName("FontName", metrics.fFontName); | 364 descriptor->insertName("FontName", metrics.fFontName); |
400 descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic)); | 365 descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic)); |
401 descriptor->insertScalar("Ascent", | 366 descriptor->insertScalar("Ascent", |
402 scaleFromFontUnits(metrics.fAscent, emSize)); | 367 scaleFromFontUnits(metrics.fAscent, emSize)); |
403 descriptor->insertScalar("Descent", | 368 descriptor->insertScalar("Descent", |
404 scaleFromFontUnits(metrics.fDescent, emSize)); | 369 scaleFromFontUnits(metrics.fDescent, emSize)); |
405 descriptor->insertScalar("StemV", | 370 descriptor->insertScalar("StemV", |
406 scaleFromFontUnits(metrics.fStemV, emSize)); | 371 scaleFromFontUnits(metrics.fStemV, emSize)); |
407 descriptor->insertScalar("CapHeight", | 372 descriptor->insertScalar("CapHeight", |
408 scaleFromFontUnits(metrics.fCapHeight, emSize)); | 373 scaleFromFontUnits(metrics.fCapHeight, emSize)); |
409 descriptor->insertInt("ItalicAngle", metrics.fItalicAngle); | 374 descriptor->insertInt("ItalicAngle", metrics.fItalicAngle); |
410 descriptor->insertObject( | 375 descriptor->insertObject( |
411 "FontBBox", makeFontBBox(metrics.fBBox, metrics.fEmSize)); | 376 "FontBBox", makeFontBBox(metrics.fBBox, metrics.fEmSize)); |
412 if (defaultWidth > 0) { | 377 if (defaultWidth > 0) { |
413 descriptor->insertScalar("MissingWidth", | 378 descriptor->insertScalar("MissingWidth", |
414 scaleFromFontUnits(defaultWidth, emSize)); | 379 scaleFromFontUnits(defaultWidth, emSize)); |
415 } | 380 } |
416 } | 381 } |
417 | 382 |
418 void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID) { | 383 void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(SkGlyphID glyphID) { |
419 // Single byte glyph encoding supports a max of 255 glyphs. | 384 // Single byte glyph encoding supports a max of 255 glyphs. |
420 fFirstGlyphID = glyphID - (glyphID - 1) % 255; | 385 fFirstGlyphID = first_glyph_for_single_byte_encoding(glyphID); |
bungeman-skia
2016/08/17 19:53:51
nit: two spaces after '='
hal.canary
2016/08/17 20:14:07
Done.
| |
421 if (fLastGlyphID > fFirstGlyphID + 255 - 1) { | 386 if (fLastGlyphID > fFirstGlyphID + 255 - 1) { |
422 fLastGlyphID = fFirstGlyphID + 255 - 1; | 387 fLastGlyphID = fFirstGlyphID + 255 - 1; |
423 } | 388 } |
424 } | 389 } |
425 | 390 |
426 void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) { | |
427 if (fFontInfo == nullptr || fFontInfo->fGlyphToUnicode.begin() == nullptr) { | |
428 return; | |
429 } | |
430 this->insertObjRef("ToUnicode", | |
431 SkPDFMakeToUnicodeCmap(fFontInfo->fGlyphToUnicode, | |
432 subset, | |
433 multiByteGlyphs(), | |
434 firstGlyphID(), | |
435 lastGlyphID())); | |
436 } | |
437 | |
438 /////////////////////////////////////////////////////////////////////////////// | 391 /////////////////////////////////////////////////////////////////////////////// |
439 // class SkPDFType0Font | 392 // class SkPDFType0Font |
440 /////////////////////////////////////////////////////////////////////////////// | 393 /////////////////////////////////////////////////////////////////////////////// |
441 | 394 |
442 SkPDFType0Font::SkPDFType0Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 395 SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
443 sk_sp<SkTypeface> typeface, | 396 sk_sp<SkTypeface> typeface, |
444 SkAdvancedTypefaceMetrics::FontType fontType) | 397 SkAdvancedTypefaceMetrics::FontType fontType) |
445 : SkPDFFont(std::move(info), std::move(typeface), nullptr, fontType, true) { | 398 : SkPDFFont(std::move(typeface), fontType) { |
446 SkDEBUGCODE(fPopulated = false); | 399 SkDEBUGCODE(fPopulated = false); |
447 if (!canSubset()) { | 400 if (!can_subset(info)) { |
448 this->populate(nullptr); | 401 this->populate(nullptr, *info); |
449 } | 402 } |
450 } | 403 } |
451 | 404 |
452 SkPDFType0Font::~SkPDFType0Font() {} | 405 SkPDFType0Font::~SkPDFType0Font() {} |
453 | 406 |
454 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { | 407 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(SkPDFCanon* canon, |
455 if (!canSubset()) { | 408 const SkPDFGlyphSet* subset) { |
409 const SkAdvancedTypefaceMetrics* metrics = | |
410 SkPDFFont::GetMetrics(this->typeface(), canon); | |
411 SkASSERT(metrics); | |
412 if (!can_subset(metrics)) { | |
456 return nullptr; | 413 return nullptr; |
457 } | 414 } |
458 auto newSubset = sk_make_sp<SkPDFType0Font>(refFontInfo(), refTypeface(), ge tType()); | 415 auto newSubset = sk_make_sp<SkPDFType0Font>( |
459 newSubset->populate(subset); | 416 metrics, this->refTypeface(), this->getType()); |
417 newSubset->populate(subset, *metrics); | |
460 return newSubset; | 418 return newSubset; |
461 } | 419 } |
462 | 420 |
463 #ifdef SK_DEBUG | 421 #ifdef SK_DEBUG |
464 void SkPDFType0Font::emitObject(SkWStream* stream, | 422 void SkPDFType0Font::emitObject(SkWStream* stream, |
465 const SkPDFObjNumMap& objNumMap, | 423 const SkPDFObjNumMap& objNumMap, |
466 const SkPDFSubstituteMap& substitutes) const { | 424 const SkPDFSubstituteMap& substitutes) const { |
467 SkASSERT(fPopulated); | 425 SkASSERT(fPopulated); |
468 return INHERITED::emitObject(stream, objNumMap, substitutes); | 426 return INHERITED::emitObject(stream, objNumMap, substitutes); |
469 } | 427 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 auto subsetStream = sk_make_sp<SkPDFStream>( | 473 auto subsetStream = sk_make_sp<SkPDFStream>( |
516 SkData::MakeWithProc( | 474 SkData::MakeWithProc( |
517 subsetFont, subsetFontSize, | 475 subsetFont, subsetFontSize, |
518 [](const void* p, void*) { delete[] (unsigned char*)p; }, | 476 [](const void* p, void*) { delete[] (unsigned char*)p; }, |
519 nullptr)); | 477 nullptr)); |
520 subsetStream->dict()->insertInt("Length1", subsetFontSize); | 478 subsetStream->dict()->insertInt("Length1", subsetFontSize); |
521 return subsetStream; | 479 return subsetStream; |
522 } | 480 } |
523 #endif // SK_SFNTLY_SUBSETTER | 481 #endif // SK_SFNTLY_SUBSETTER |
524 | 482 |
525 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { | 483 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset, |
526 SkASSERT(this->canEmbed()); | 484 const SkAdvancedTypefaceMetrics& metrics) { |
527 SkASSERT(this->getFontInfo()); | 485 SkASSERT(can_embed(&metrics)); |
528 const SkAdvancedTypefaceMetrics& metrics = *(this->getFontInfo()); | |
529 SkAdvancedTypefaceMetrics::FontType type = this->getType(); | 486 SkAdvancedTypefaceMetrics::FontType type = this->getType(); |
530 SkTypeface* face = this->typeface(); | 487 SkTypeface* face = this->typeface(); |
531 SkASSERT(face); | 488 SkASSERT(face); |
532 const SkString& name = metrics.fFontName; | 489 const SkString& name = metrics.fFontName; |
533 | 490 |
534 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 491 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
535 add_common_font_descriptor_entries(descriptor.get(), metrics, 0); | 492 add_common_font_descriptor_entries(descriptor.get(), metrics, 0); |
536 switch (type) { | 493 switch (type) { |
537 case SkAdvancedTypefaceMetrics::kTrueType_Font: { | 494 case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
538 int ttcIndex; | 495 int ttcIndex; |
539 std::unique_ptr<SkStreamAsset> fontAsset(face->openStream(&ttcIndex) ); | 496 std::unique_ptr<SkStreamAsset> fontAsset(face->openStream(&ttcIndex) ); |
540 SkASSERT(fontAsset); | 497 SkASSERT(fontAsset); |
541 if (!fontAsset) { | 498 if (!fontAsset) { |
542 return false; | 499 return false; |
543 } | 500 } |
544 size_t fontSize = fontAsset->getLength(); | 501 size_t fontSize = fontAsset->getLength(); |
545 SkASSERT(fontSize > 0); | 502 SkASSERT(fontSize > 0); |
546 if (fontSize == 0) { | 503 if (fontSize == 0) { |
547 return false; | 504 return false; |
548 } | 505 } |
549 | 506 |
550 #ifdef SK_SFNTLY_SUBSETTER | 507 #ifdef SK_SFNTLY_SUBSETTER |
551 if (this->canSubset() && subset) { | 508 if (can_subset(&metrics) && subset) { |
552 // Generate glyph id array. in format needed by sfntly | 509 // Generate glyph id array. in format needed by sfntly |
553 SkTDArray<uint32_t> glyphIDs; | 510 SkTDArray<uint32_t> glyphIDs; |
554 if (subset) { | 511 if (subset) { |
555 if (!subset->has(0)) { | 512 if (!subset->has(0)) { |
556 glyphIDs.push(0); // Always include glyph 0. | 513 glyphIDs.push(0); // Always include glyph 0. |
557 } | 514 } |
558 subset->exportTo(&glyphIDs); | 515 subset->exportTo(&glyphIDs); |
559 } | 516 } |
560 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( | 517 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( |
561 std::move(fontAsset), glyphIDs, name.c_str()); | 518 std::move(fontAsset), glyphIDs, name.c_str()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 } else { | 557 } else { |
601 SkASSERT(false); | 558 SkASSERT(false); |
602 } | 559 } |
603 | 560 |
604 auto sysInfo = sk_make_sp<SkPDFDict>(); | 561 auto sysInfo = sk_make_sp<SkPDFDict>(); |
605 sysInfo->insertString("Registry", "Adobe"); | 562 sysInfo->insertString("Registry", "Adobe"); |
606 sysInfo->insertString("Ordering", "Identity"); | 563 sysInfo->insertString("Ordering", "Identity"); |
607 sysInfo->insertInt("Supplement", 0); | 564 sysInfo->insertInt("Supplement", 0); |
608 newCIDFont->insertObject("CIDSystemInfo", std::move(sysInfo)); | 565 newCIDFont->insertObject("CIDSystemInfo", std::move(sysInfo)); |
609 | 566 |
610 uint16_t emSize = this->getFontInfo()->fEmSize; | 567 uint16_t emSize = metrics.fEmSize; |
611 int16_t defaultWidth = 0; | 568 int16_t defaultWidth = 0; |
612 const SkBitSet* bitSet = subset ? &subset->bitSet() : nullptr; | 569 const SkBitSet* bitSet = subset ? &subset->bitSet() : nullptr; |
613 { | 570 { |
614 SkAutoGlyphCache glyphCache = vector_cache(face); | 571 SkAutoGlyphCache glyphCache = vector_cache(face); |
615 sk_sp<SkPDFArray> widths = SkPDFMakeCIDGlyphWidthsArray( | 572 sk_sp<SkPDFArray> widths = SkPDFMakeCIDGlyphWidthsArray( |
616 glyphCache.get(), bitSet, emSize, &defaultWidth); | 573 glyphCache.get(), bitSet, emSize, &defaultWidth); |
617 if (widths && widths->size() > 0) { | 574 if (widths && widths->size() > 0) { |
618 newCIDFont->insertObject("W", std::move(widths)); | 575 newCIDFont->insertObject("W", std::move(widths)); |
619 } | 576 } |
620 newCIDFont->insertScalar( | 577 newCIDFont->insertScalar( |
621 "DW", scaleFromFontUnits(defaultWidth, emSize)); | 578 "DW", scaleFromFontUnits(defaultWidth, emSize)); |
622 } | 579 } |
623 | 580 |
624 //////////////////////////////////////////////////////////////////////////// | 581 //////////////////////////////////////////////////////////////////////////// |
625 | 582 |
626 this->insertName("Subtype", "Type0"); | 583 this->insertName("Subtype", "Type0"); |
627 this->insertName("BaseFont", metrics.fFontName); | 584 this->insertName("BaseFont", metrics.fFontName); |
628 this->insertName("Encoding", "Identity-H"); | 585 this->insertName("Encoding", "Identity-H"); |
629 auto descendantFonts = sk_make_sp<SkPDFArray>(); | 586 auto descendantFonts = sk_make_sp<SkPDFArray>(); |
630 descendantFonts->appendObjRef(std::move(newCIDFont)); | 587 descendantFonts->appendObjRef(std::move(newCIDFont)); |
631 this->insertObject("DescendantFonts", std::move(descendantFonts)); | 588 this->insertObject("DescendantFonts", std::move(descendantFonts)); |
632 this->populateToUnicodeTable(subset); | 589 |
590 if (metrics.fGlyphToUnicode.count() > 0) { | |
591 this->insertObjRef("ToUnicode", | |
592 SkPDFMakeToUnicodeCmap(metrics.fGlyphToUnicode, | |
593 subset, | |
594 multiByteGlyphs(), | |
595 firstGlyphID(), | |
596 lastGlyphID())); | |
597 } | |
633 SkDEBUGCODE(fPopulated = true); | 598 SkDEBUGCODE(fPopulated = true); |
634 return true; | 599 return true; |
635 } | 600 } |
636 | 601 |
637 sk_sp<const SkAdvancedTypefaceMetrics> SkPDFFont::GetFontMetricsWithToUnicode( | |
638 SkTypeface* typeface, uint32_t* glyphs, uint32_t glyphsCount) { | |
639 return sk_sp<const SkAdvancedTypefaceMetrics>( | |
640 typeface->getAdvancedTypefaceMetrics( | |
641 SkTypeface::kToUnicode_PerGlyphInfo, glyphs, glyphsCount)); | |
642 } | |
643 | |
644 | |
645 /////////////////////////////////////////////////////////////////////////////// | 602 /////////////////////////////////////////////////////////////////////////////// |
646 // class SkPDFType1Font | 603 // class SkPDFType1Font |
647 /////////////////////////////////////////////////////////////////////////////// | 604 /////////////////////////////////////////////////////////////////////////////// |
648 | 605 |
649 SkPDFType1Font::SkPDFType1Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 606 static sk_sp<SkPDFDict> make_type1_font_descriptor( |
650 sk_sp<SkTypeface> typeface, | 607 SkTypeface* typeface, |
651 uint16_t glyphID, | 608 const SkAdvancedTypefaceMetrics* info) { |
652 sk_sp<SkPDFDict> relatedFontDescriptor) | |
653 : SkPDFFont(std::move(info), | |
654 std::move(typeface), | |
655 std::move(relatedFontDescriptor), | |
656 SkAdvancedTypefaceMetrics::kType1_Font, | |
657 /* multiByteGlyphs = */ false) { | |
658 this->populate(glyphID); // TODO(halcanary): subset this. | |
659 } | |
660 | |
661 SkPDFType1Font::~SkPDFType1Font() {} | |
662 | |
663 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { | |
664 if (sk_sp<SkPDFDict> descriptor = this->refFontDescriptor()) { | |
665 this->insertObjRef("FontDescriptor", std::move(descriptor)); | |
666 return true; | |
667 } | |
668 | |
669 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 609 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
670 setFontDescriptor(descriptor); | 610 SkASSERT(info); |
611 add_common_font_descriptor_entries(descriptor.get(), *info, 0); | |
671 | 612 |
672 int ttcIndex; | 613 int ttcIndex; |
673 size_t header SK_INIT_TO_AVOID_WARNING; | 614 size_t header SK_INIT_TO_AVOID_WARNING; |
674 size_t data SK_INIT_TO_AVOID_WARNING; | 615 size_t data SK_INIT_TO_AVOID_WARNING; |
675 size_t trailer SK_INIT_TO_AVOID_WARNING; | 616 size_t trailer SK_INIT_TO_AVOID_WARNING; |
676 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex) ); | 617 std::unique_ptr<SkStreamAsset> rawFontData(typeface->openStream(&ttcIndex)); |
677 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), | 618 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), |
678 &header, &data, &traile r); | 619 &header, &data, &traile r); |
679 if (!fontData) { | 620 if (fontData && can_embed(info)) { |
680 return false; | 621 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); |
622 fontStream->dict()->insertInt("Length1", header); | |
623 fontStream->dict()->insertInt("Length2", data); | |
624 fontStream->dict()->insertInt("Length3", trailer); | |
625 descriptor->insertObjRef("FontFile", std::move(fontStream)); | |
681 } | 626 } |
682 SkASSERT(this->canEmbed()); | 627 return descriptor; |
683 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); | |
684 fontStream->dict()->insertInt("Length1", header); | |
685 fontStream->dict()->insertInt("Length2", data); | |
686 fontStream->dict()->insertInt("Length3", trailer); | |
687 descriptor->insertObjRef("FontFile", std::move(fontStream)); | |
688 | |
689 SkASSERT(this->getFontInfo()); | |
690 add_common_font_descriptor_entries(descriptor.get(), | |
691 *this->getFontInfo(), | |
692 defaultWidth); | |
693 this->insertObjRef("FontDescriptor", std::move(descriptor)); | |
694 return true; | |
695 } | 628 } |
696 | 629 |
697 bool SkPDFType1Font::populate(int16_t glyphID) { | 630 static void populate_type_1_font(SkPDFDict* font, |
698 this->insertName("Subtype", "Type1"); | 631 const SkAdvancedTypefaceMetrics* info, |
699 this->insertName("BaseFont", this->getFontInfo()->fFontName); | 632 SkTypeface* typeface, |
700 adjustGlyphRangeForSingleByteEncoding(glyphID); | 633 SkGlyphID firstGlyphID, |
701 SkGlyphID firstGlyphID = this->firstGlyphID(); | 634 SkGlyphID lastGlyphID) { |
702 SkGlyphID lastGlyphID = this->lastGlyphID(); | 635 SkASSERT(info); |
636 font->insertName("Subtype", "Type1"); | |
637 font->insertName("BaseFont", info->fFontName); | |
703 | 638 |
704 // glyphCount not including glyph 0 | 639 // glyphCount not including glyph 0 |
705 unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; | 640 unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; |
706 SkASSERT(glyphCount > 0 && glyphCount <= 255); | 641 SkASSERT(glyphCount > 0 && glyphCount <= 255); |
707 this->insertInt("FirstChar", (size_t)0); | 642 font->insertInt("FirstChar", (size_t)0); |
708 this->insertInt("LastChar", (size_t)glyphCount); | 643 font->insertInt("LastChar", (size_t)glyphCount); |
709 { | 644 { |
710 SkAutoGlyphCache glyphCache = vector_cache(this->typeface()); | 645 SkAutoGlyphCache glyphCache = vector_cache(typeface); |
711 auto widths = sk_make_sp<SkPDFArray>(); | 646 auto widths = sk_make_sp<SkPDFArray>(); |
712 SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; | 647 SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; |
713 const uint16_t emSize = this->getFontInfo()->fEmSize; | 648 const uint16_t emSize = info->fEmSize; |
714 widths->appendScalar(from_font_units(advance, emSize)); | 649 widths->appendScalar(from_font_units(advance, emSize)); |
715 for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { | 650 for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
716 advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; | 651 advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; |
717 widths->appendScalar(from_font_units(advance, emSize)); | 652 widths->appendScalar(from_font_units(advance, emSize)); |
718 } | 653 } |
719 this->insertObject("Widths", std::move(widths)); | 654 font->insertObject("Widths", std::move(widths)); |
720 } | |
721 if (!addFontDescriptor(0)) { | |
722 return false; | |
723 } | 655 } |
724 auto encDiffs = sk_make_sp<SkPDFArray>(); | 656 auto encDiffs = sk_make_sp<SkPDFArray>(); |
725 encDiffs->reserve(lastGlyphID - firstGlyphID + 3); | 657 encDiffs->reserve(lastGlyphID - firstGlyphID + 3); |
726 encDiffs->appendInt(0); | 658 encDiffs->appendInt(0); |
727 const SkTArray<SkString>& glyphNames = this->getFontInfo()->fGlyphNames; | 659 const SkTArray<SkString>& glyphNames = info->fGlyphNames; |
728 SkASSERT(glyphNames.count() > lastGlyphID); | 660 SkASSERT(glyphNames.count() > lastGlyphID); |
729 encDiffs->appendName(glyphNames[0].c_str()); | 661 encDiffs->appendName(glyphNames[0].c_str()); |
730 const SkString unknown("UNKNOWN"); | 662 const SkString unknown("UNKNOWN"); |
731 for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { | 663 for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
732 const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty( ); | 664 const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty( ); |
733 const SkString& name = valid ? glyphNames[gID] : unknown; | 665 const SkString& name = valid ? glyphNames[gID] : unknown; |
734 encDiffs->appendName(name); | 666 encDiffs->appendName(name); |
735 } | 667 } |
736 | 668 |
737 auto encoding = sk_make_sp<SkPDFDict>("Encoding"); | 669 auto encoding = sk_make_sp<SkPDFDict>("Encoding"); |
738 encoding->insertObject("Differences", std::move(encDiffs)); | 670 encoding->insertObject("Differences", std::move(encDiffs)); |
739 this->insertObject("Encoding", std::move(encoding)); | 671 font->insertObject("Encoding", std::move(encoding)); |
740 return true; | 672 } |
673 | |
674 SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, | |
675 sk_sp<SkTypeface> typeface, | |
676 uint16_t glyphID, | |
677 SkPDFCanon* canon) | |
678 : SkPDFFont(std::move(typeface),SkAdvancedTypefaceMetrics::kType1_Font) { | |
bungeman-skia
2016/08/17 19:53:51
nit: space after comma
also, there's a lot of for
hal.canary
2016/08/17 20:14:07
Done.
| |
679 SkFontID fontID = this->typeface()->uniqueID(); | |
680 sk_sp<SkPDFDict> fontDescriptor; | |
681 if (SkPDFDict** ptr = canon->fFontDescriptors.find(fontID)) { | |
682 fontDescriptor = sk_ref_sp(*ptr); | |
683 } else { | |
684 fontDescriptor = make_type1_font_descriptor(this->typeface(), info); | |
685 canon->fFontDescriptors.set(fontID, SkRef(fontDescriptor.get())); | |
686 } | |
687 this->insertObjRef("FontDescriptor", std::move(fontDescriptor)); | |
688 this->adjustGlyphRangeForSingleByteEncoding(glyphID); | |
689 // TODO(halcanary): subset this (advances and names). | |
690 populate_type_1_font(this, info, this->typeface(), | |
691 this->firstGlyphID(), this->lastGlyphID()); | |
741 } | 692 } |
742 | 693 |
743 /////////////////////////////////////////////////////////////////////////////// | 694 /////////////////////////////////////////////////////////////////////////////// |
744 // class SkPDFType3Font | 695 // class SkPDFType3Font |
745 /////////////////////////////////////////////////////////////////////////////// | 696 /////////////////////////////////////////////////////////////////////////////// |
746 | 697 |
747 namespace { | 698 namespace { |
748 // returns [0, first, first+1, ... last-1, last] | 699 // returns [0, first, first+1, ... last-1, last] |
749 struct SingleByteGlyphIdIterator { | 700 struct SingleByteGlyphIdIterator { |
750 SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) | 701 SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) |
(...skipping 16 matching lines...) Expand all Loading... | |
767 int fCurrent; // must be int to make fLast+1 to fit | 718 int fCurrent; // must be int to make fLast+1 to fit |
768 }; | 719 }; |
769 Iter begin() const { return Iter(fFirst, 0); } | 720 Iter begin() const { return Iter(fFirst, 0); } |
770 Iter end() const { return Iter(fFirst, (int)fLast + 1); } | 721 Iter end() const { return Iter(fFirst, (int)fLast + 1); } |
771 private: | 722 private: |
772 const SkGlyphID fFirst; | 723 const SkGlyphID fFirst; |
773 const SkGlyphID fLast; | 724 const SkGlyphID fLast; |
774 }; | 725 }; |
775 } | 726 } |
776 | 727 |
777 static void add_type3_font_info(SkPDFDict* font, | 728 static void add_type3_font_info(SkPDFCanon* canon, |
729 SkPDFDict* font, | |
778 SkTypeface* typeface, | 730 SkTypeface* typeface, |
779 SkScalar emSize, | 731 SkScalar emSize, |
780 const SkPDFGlyphSet* subset, | 732 const SkPDFGlyphSet* subset, |
781 SkGlyphID firstGlyphID, | 733 SkGlyphID firstGlyphID, |
782 SkGlyphID lastGlyphID) { | 734 SkGlyphID lastGlyphID) { |
783 SkASSERT(lastGlyphID >= firstGlyphID); | 735 SkASSERT(lastGlyphID >= firstGlyphID); |
784 SkASSERT(emSize > 0.0f); | 736 SkASSERT(emSize > 0.0f); |
785 SkAutoGlyphCache cache = vector_cache(typeface, emSize); | 737 SkAutoGlyphCache cache = vector_cache(typeface, emSize); |
786 font->insertName("Subtype", "Type3"); | 738 font->insertName("Subtype", "Type3"); |
787 // Flip about the x-axis and scale by 1/emSize. | 739 // Flip about the x-axis and scale by 1/emSize. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
855 glyphs of the font were placed with their origins coincident and | 807 glyphs of the font were placed with their origins coincident and |
856 then filled." */ | 808 then filled." */ |
857 auto fontBBox = sk_make_sp<SkPDFArray>(); | 809 auto fontBBox = sk_make_sp<SkPDFArray>(); |
858 fontBBox->reserve(4); | 810 fontBBox->reserve(4); |
859 fontBBox->appendInt(bbox.left()); | 811 fontBBox->appendInt(bbox.left()); |
860 fontBBox->appendInt(bbox.bottom()); | 812 fontBBox->appendInt(bbox.bottom()); |
861 fontBBox->appendInt(bbox.right()); | 813 fontBBox->appendInt(bbox.right()); |
862 fontBBox->appendInt(bbox.top()); | 814 fontBBox->appendInt(bbox.top()); |
863 font->insertObject("FontBBox", std::move(fontBBox)); | 815 font->insertObject("FontBBox", std::move(fontBBox)); |
864 font->insertName("CIDToGIDMap", "Identity"); | 816 font->insertName("CIDToGIDMap", "Identity"); |
865 sk_sp<const SkAdvancedTypefaceMetrics> metrics; | 817 const SkAdvancedTypefaceMetrics* metrics = |
866 if (subset) { | 818 SkPDFFont::GetMetrics(typeface, canon); |
bungeman-skia
2016/08/17 19:53:51
nit: any reason not to have this on one line?
hal.canary
2016/08/17 20:14:07
Done.
| |
867 SkTDArray<uint32_t> subsetList; | 819 if (metrics /* && metrics->fGlyphToUnicode.count() > 0 */) { |
868 for (SkGlyphID gID : SingleByteGlyphIdIterator(firstGlyphID, lastGlyphID )) { | |
869 if (gID == 0 || subset->has(gID)) { // Always include glyph 0. | |
870 subsetList.push(0); | |
871 } | |
872 } | |
873 subset->exportTo(&subsetList); | |
874 metrics = SkPDFFont::GetFontMetricsWithToUnicode(typeface, subsetList.be gin(), | |
875 subsetList.count()); | |
876 } else { | |
877 metrics = SkPDFFont::GetFontMetricsWithToUnicode(typeface, nullptr, 0); | |
878 } | |
879 if (metrics) { | |
880 font->insertObjRef("ToUnicode", | 820 font->insertObjRef("ToUnicode", |
881 SkPDFMakeToUnicodeCmap(metrics->fGlyphToUnicode, | 821 SkPDFMakeToUnicodeCmap(metrics->fGlyphToUnicode, |
882 subset, | 822 subset, |
883 false, | 823 false, |
884 firstGlyphID, | 824 firstGlyphID, |
885 lastGlyphID)); | 825 lastGlyphID)); |
886 } | 826 } |
887 font->insertObject("Widths", std::move(widthArray)); | 827 font->insertObject("Widths", std::move(widthArray)); |
888 font->insertObject("Encoding", std::move(encoding)); | 828 font->insertObject("Encoding", std::move(encoding)); |
889 font->insertObject("CharProcs", std::move(charProcs)); | 829 font->insertObject("CharProcs", std::move(charProcs)); |
890 } | 830 } |
891 | 831 |
892 SkPDFType3Font::SkPDFType3Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 832 SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
893 sk_sp<SkTypeface> typeface, | 833 sk_sp<SkTypeface> typeface, |
894 SkAdvancedTypefaceMetrics::FontType fontType, | 834 SkAdvancedTypefaceMetrics::FontType fontType, |
895 uint16_t glyphID) | 835 uint16_t glyphID) |
896 : SkPDFFont(std::move(info), std::move(typeface), nullptr, | 836 : SkPDFFont(std::move(typeface), fontType) { |
897 fontType, /* multiByteGlyphs = */ false) { | |
898 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. | |
899 this->setLastGlyphID(SkToU16(this->typeface()->countGlyphs() - 1)); | |
900 this->adjustGlyphRangeForSingleByteEncoding(glyphID); | 837 this->adjustGlyphRangeForSingleByteEncoding(glyphID); |
901 } | 838 } |
902 | 839 |
903 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { | 840 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(SkPDFCanon* canon, |
841 const SkPDFGlyphSet* usage) { | |
904 // All fonts are subset before serialization. | 842 // All fonts are subset before serialization. |
905 // TODO(halcanary): all fonts should follow this pattern. | 843 // TODO(halcanary): all fonts should follow this pattern. |
844 const SkAdvancedTypefaceMetrics* info = | |
845 SkPDFFont::GetMetrics(this->typeface(), canon); | |
846 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; | |
906 auto font = sk_make_sp<SkPDFDict>("Font"); | 847 auto font = sk_make_sp<SkPDFDict>("Font"); |
907 const SkAdvancedTypefaceMetrics* info = this->getFontInfo(); | 848 add_type3_font_info(canon, font.get(), this->typeface(), (SkScalar)emSize, u sage, |
908 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; | |
909 add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, | |
910 this->firstGlyphID(), this->lastGlyphID()); | 849 this->firstGlyphID(), this->lastGlyphID()); |
911 return font; | 850 return font; |
912 } | 851 } |
913 | 852 |
914 | 853 |
915 //////////////////////////////////////////////////////////////////////////////// | 854 //////////////////////////////////////////////////////////////////////////////// |
916 | 855 |
917 SkPDFFont::Match SkPDFFont::IsMatch(SkPDFFont* existingFont, | |
918 uint32_t existingFontID, | |
919 uint16_t existingGlyphID, | |
920 uint32_t searchFontID, | |
921 uint16_t searchGlyphID) { | |
922 if (existingFontID != searchFontID) { | |
923 return SkPDFFont::kNot_Match; | |
924 } | |
925 if (existingGlyphID == 0 || searchGlyphID == 0) { | |
926 return SkPDFFont::kExact_Match; | |
927 } | |
928 if (existingFont != nullptr) { | |
929 return (existingFont->fFirstGlyphID <= searchGlyphID && | |
930 searchGlyphID <= existingFont->fLastGlyphID) | |
931 ? SkPDFFont::kExact_Match | |
932 : SkPDFFont::kRelated_Match; | |
933 } | |
934 return (existingGlyphID == searchGlyphID) ? SkPDFFont::kExact_Match | |
935 : SkPDFFont::kRelated_Match; | |
936 } | |
937 | |
938 // Since getAdvancedTypefaceMetrics is expensive, cache the result. | |
939 bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFCanon* canon) { | 856 bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFCanon* canon) { |
940 SkFontID id = SkTypeface::UniqueID(typeface); | 857 return can_embed(SkPDFFont::GetMetrics(typeface, canon)); |
941 if (bool* value = canon->fCanEmbedTypeface.find(id)) { | |
942 return *value; | |
943 } | |
944 SkAutoResolveDefaultTypeface face(typeface); | |
945 bool canEmbed = true; | |
946 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics( | |
947 face->getAdvancedTypefaceMetrics( | |
948 SkTypeface::kNo_PerGlyphInfo, nullptr, 0)); | |
949 if (fontMetrics) { | |
950 canEmbed = !SkToBool( | |
951 fontMetrics->fFlags & | |
952 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); | |
953 } | |
954 return *canon->fCanEmbedTypeface.set(id, canEmbed); | |
955 } | 858 } |
956 | 859 |
957 void SkPDFFont::drop() { | 860 void SkPDFFont::drop() { |
958 fTypeface = nullptr; | 861 fTypeface = nullptr; |
959 fFontInfo = nullptr; | |
960 fDescriptor = nullptr; | |
961 this->SkPDFDict::drop(); | 862 this->SkPDFDict::drop(); |
962 } | 863 } |
OLD | NEW |