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 || !SkToBool(metrics->fFlags & |
221 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); | 218 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontF lag); |
bungeman-skia
2016/08/17 18:56:04
If the '!SkToBool(...)' part will fit if formatted
hal.canary
2016/08/17 19:29:49
Done.
| |
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 || !SkToBool(metrics->fFlags & |
230 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); | 223 SkAdvancedTypefaceMetrics::kNotSubsettable_Font Flag); |
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 (sk_sp<SkAdvancedTypefaceMetrics>* ptr = canon->fTypefaceMetrics.find(id) ) { | |
264 return ptr->get(); | |
265 } | |
266 sk_sp<SkTypeface> defaultFace; | |
267 if (!typeface) { | |
bungeman-skia
2016/08/17 18:56:04
Note that 'SkTypeface::UniqueID is already doing t
hal.canary
2016/08/17 19:29:49
Why keep churning reference counts?
I intend to u
| |
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)); | |
275 return canon->fTypefaceMetrics.set(id, std::move(metrics))->get(); | |
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 > 1 ? gid - (gid - 1) % 255 : 1; | |
bungeman-skia
2016/08/17 18:56:04
1 - 0 % 255 == 1
gid > 0?
hal.canary
2016/08/17 19:29:49
Done.
| |
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 (sk_sp<SkPDFFont>* found = canon->fFontMap.find(fontID)) { | |
302 SkASSERT(multibyte == (*found)->multiByteGlyphs()); | |
303 sk_sp<SkPDFFont> font = *found; | |
304 return font.release(); | |
283 } | 305 } |
306 | |
284 sk_sp<SkTypeface> typeface(face ? sk_ref_sp(face) : SkTypeface::MakeDefault( )); | 307 sk_sp<SkTypeface> typeface(face ? sk_ref_sp(face) : SkTypeface::MakeDefault( )); |
285 SkASSERT(typeface); | 308 SkASSERT(typeface); |
286 int glyphCount = typeface->countGlyphs(); | 309 int glyphCount = typeface->countGlyphs(); |
310 // Validate typeface + glyph; | |
287 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. | 311 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. |
288 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount | 312 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount |
289 glyphID >= glyphCount) { // invalid glyph | 313 glyphID >= glyphCount) { // invalid glyph |
290 return nullptr; | 314 return nullptr; |
291 } | 315 } |
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 | 316 |
320 sk_sp<SkPDFFont> font; | 317 sk_sp<SkPDFFont> font; |
321 switch (type) { | 318 switch (type) { |
322 case SkAdvancedTypefaceMetrics::kType1CID_Font: | 319 case SkAdvancedTypefaceMetrics::kType1CID_Font: |
323 case SkAdvancedTypefaceMetrics::kTrueType_Font: | 320 case SkAdvancedTypefaceMetrics::kTrueType_Font: |
324 SkASSERT(relatedFontDescriptor == nullptr); | 321 SkASSERT(multibyte); |
325 SkASSERT(fontMetrics != nullptr); | 322 SkASSERT(fontMetrics != nullptr); |
326 font = sk_make_sp<SkPDFType0Font>(std::move(fontMetrics), | 323 font = sk_make_sp<SkPDFType0Font>(fontMetrics, |
327 std::move(typeface), | 324 std::move(typeface), |
328 type); | 325 type); |
329 break; | 326 break; |
330 case SkAdvancedTypefaceMetrics::kType1_Font: | 327 case SkAdvancedTypefaceMetrics::kType1_Font: |
328 SkASSERT(!multibyte); | |
331 SkASSERT(fontMetrics != nullptr); | 329 SkASSERT(fontMetrics != nullptr); |
332 font = sk_make_sp<SkPDFType1Font>(std::move(fontMetrics), | 330 font = sk_make_sp<SkPDFType1Font>(fontMetrics, |
333 std::move(typeface), | 331 std::move(typeface), |
334 glyphID, | 332 glyphID, |
335 std::move(relatedFontDescriptor)); | 333 canon); |
336 break; | 334 break; |
337 case SkAdvancedTypefaceMetrics::kCFF_Font: | 335 default: |
338 SkASSERT(fontMetrics != nullptr); | 336 SkASSERT(!multibyte); |
339 // fallthrough | 337 font = sk_make_sp<SkPDFType3Font>(fontMetrics, |
340 case SkAdvancedTypefaceMetrics::kOther_Font: | |
341 font = sk_make_sp<SkPDFType3Font>(std::move(fontMetrics), | |
342 std::move(typeface), | 338 std::move(typeface), |
343 type, | 339 type, |
344 glyphID); | 340 glyphID); |
345 break; | 341 break; |
346 default: | |
347 SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); | |
348 return nullptr; | |
349 } | 342 } |
350 // When firstGlyphID==0, SkFont::IsMatch() matches all glyphs in font. | 343 canon->fFontMap.set(fontID, font); |
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>. | 344 return font.release(); // TODO(halcanary) return sk_sp<SkPDFFont>. |
355 } | 345 } |
356 | 346 |
357 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { | 347 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(SkPDFCanon*, const SkPDFGlyphSet*) { |
358 return nullptr; // Default: no support. | 348 return nullptr; // Default: no support. |
359 } | 349 } |
360 | 350 |
361 // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> | 351 SkPDFFont::SkPDFFont(sk_sp<SkTypeface> typeface, |
362 SkPDFFont::SkPDFFont(sk_sp<const SkAdvancedTypefaceMetrics> info, | 352 SkAdvancedTypefaceMetrics::FontType fontType) |
363 sk_sp<SkTypeface> typeface, | |
364 sk_sp<SkPDFDict> relatedFontDescriptor, | |
365 SkAdvancedTypefaceMetrics::FontType fontType, | |
366 bool multiByteGlyphs) | |
367 : SkPDFDict("Font") | 353 : SkPDFDict("Font") |
368 , fTypeface(std::move(typeface)) | 354 , fTypeface(std::move(typeface)) |
369 , fFontInfo(std::move(info)) | |
370 , fDescriptor(std::move(relatedFontDescriptor)) | |
371 , fFirstGlyphID(1) | 355 , fFirstGlyphID(1) |
372 , fFontType(fontType) | 356 , fFontType(fontType) { |
373 , fMultiByteGlyphs(multiByteGlyphs) { | |
374 SkASSERT(fTypeface); | 357 SkASSERT(fTypeface); |
375 fLastGlyphID = fFontInfo ? fFontInfo->fLastGlyphID : 0; | 358 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 } | 359 } |
394 | 360 |
395 static void add_common_font_descriptor_entries(SkPDFDict* descriptor, | 361 static void add_common_font_descriptor_entries(SkPDFDict* descriptor, |
396 const SkAdvancedTypefaceMetrics& metrics, | 362 const SkAdvancedTypefaceMetrics& metrics, |
397 int16_t defaultWidth) { | 363 int16_t defaultWidth) { |
398 const uint16_t emSize = metrics.fEmSize; | 364 const uint16_t emSize = metrics.fEmSize; |
399 descriptor->insertName("FontName", metrics.fFontName); | 365 descriptor->insertName("FontName", metrics.fFontName); |
400 descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic)); | 366 descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic)); |
401 descriptor->insertScalar("Ascent", | 367 descriptor->insertScalar("Ascent", |
402 scaleFromFontUnits(metrics.fAscent, emSize)); | 368 scaleFromFontUnits(metrics.fAscent, emSize)); |
403 descriptor->insertScalar("Descent", | 369 descriptor->insertScalar("Descent", |
404 scaleFromFontUnits(metrics.fDescent, emSize)); | 370 scaleFromFontUnits(metrics.fDescent, emSize)); |
405 descriptor->insertScalar("StemV", | 371 descriptor->insertScalar("StemV", |
406 scaleFromFontUnits(metrics.fStemV, emSize)); | 372 scaleFromFontUnits(metrics.fStemV, emSize)); |
407 descriptor->insertScalar("CapHeight", | 373 descriptor->insertScalar("CapHeight", |
408 scaleFromFontUnits(metrics.fCapHeight, emSize)); | 374 scaleFromFontUnits(metrics.fCapHeight, emSize)); |
409 descriptor->insertInt("ItalicAngle", metrics.fItalicAngle); | 375 descriptor->insertInt("ItalicAngle", metrics.fItalicAngle); |
410 descriptor->insertObject( | 376 descriptor->insertObject( |
411 "FontBBox", makeFontBBox(metrics.fBBox, metrics.fEmSize)); | 377 "FontBBox", makeFontBBox(metrics.fBBox, metrics.fEmSize)); |
412 if (defaultWidth > 0) { | 378 if (defaultWidth > 0) { |
413 descriptor->insertScalar("MissingWidth", | 379 descriptor->insertScalar("MissingWidth", |
414 scaleFromFontUnits(defaultWidth, emSize)); | 380 scaleFromFontUnits(defaultWidth, emSize)); |
415 } | 381 } |
416 } | 382 } |
417 | 383 |
418 void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID) { | 384 void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(SkGlyphID glyphID) { |
419 // Single byte glyph encoding supports a max of 255 glyphs. | 385 // Single byte glyph encoding supports a max of 255 glyphs. |
420 fFirstGlyphID = glyphID - (glyphID - 1) % 255; | 386 fFirstGlyphID = first_glyph_for_single_byte_encoding(glyphID); |
421 if (fLastGlyphID > fFirstGlyphID + 255 - 1) { | 387 if (fLastGlyphID > fFirstGlyphID + 255 - 1) { |
422 fLastGlyphID = fFirstGlyphID + 255 - 1; | 388 fLastGlyphID = fFirstGlyphID + 255 - 1; |
423 } | 389 } |
424 } | 390 } |
425 | 391 |
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 /////////////////////////////////////////////////////////////////////////////// | 392 /////////////////////////////////////////////////////////////////////////////// |
439 // class SkPDFType0Font | 393 // class SkPDFType0Font |
440 /////////////////////////////////////////////////////////////////////////////// | 394 /////////////////////////////////////////////////////////////////////////////// |
441 | 395 |
442 SkPDFType0Font::SkPDFType0Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 396 SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
443 sk_sp<SkTypeface> typeface, | 397 sk_sp<SkTypeface> typeface, |
444 SkAdvancedTypefaceMetrics::FontType fontType) | 398 SkAdvancedTypefaceMetrics::FontType fontType) |
445 : SkPDFFont(std::move(info), std::move(typeface), nullptr, fontType, true) { | 399 : SkPDFFont(std::move(typeface), fontType) { |
446 SkDEBUGCODE(fPopulated = false); | 400 SkDEBUGCODE(fPopulated = false); |
447 if (!canSubset()) { | 401 if (!can_subset(info)) { |
448 this->populate(nullptr); | 402 this->populate(nullptr, *info); |
449 } | 403 } |
450 } | 404 } |
451 | 405 |
452 SkPDFType0Font::~SkPDFType0Font() {} | 406 SkPDFType0Font::~SkPDFType0Font() {} |
453 | 407 |
454 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { | 408 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(SkPDFCanon* canon, |
455 if (!canSubset()) { | 409 const SkPDFGlyphSet* subset) { |
410 const SkAdvancedTypefaceMetrics* metrics = | |
411 SkPDFFont::GetMetrics(this->typeface(), canon); | |
412 SkASSERT(metrics); | |
413 if (!can_subset(metrics)) { | |
456 return nullptr; | 414 return nullptr; |
457 } | 415 } |
458 auto newSubset = sk_make_sp<SkPDFType0Font>(refFontInfo(), refTypeface(), ge tType()); | 416 auto newSubset = sk_make_sp<SkPDFType0Font>( |
459 newSubset->populate(subset); | 417 metrics, this->refTypeface(), this->getType()); |
418 newSubset->populate(subset, *metrics); | |
460 return newSubset; | 419 return newSubset; |
461 } | 420 } |
462 | 421 |
463 #ifdef SK_DEBUG | 422 #ifdef SK_DEBUG |
464 void SkPDFType0Font::emitObject(SkWStream* stream, | 423 void SkPDFType0Font::emitObject(SkWStream* stream, |
465 const SkPDFObjNumMap& objNumMap, | 424 const SkPDFObjNumMap& objNumMap, |
466 const SkPDFSubstituteMap& substitutes) const { | 425 const SkPDFSubstituteMap& substitutes) const { |
467 SkASSERT(fPopulated); | 426 SkASSERT(fPopulated); |
468 return INHERITED::emitObject(stream, objNumMap, substitutes); | 427 return INHERITED::emitObject(stream, objNumMap, substitutes); |
469 } | 428 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 auto subsetStream = sk_make_sp<SkPDFStream>( | 474 auto subsetStream = sk_make_sp<SkPDFStream>( |
516 SkData::MakeWithProc( | 475 SkData::MakeWithProc( |
517 subsetFont, subsetFontSize, | 476 subsetFont, subsetFontSize, |
518 [](const void* p, void*) { delete[] (unsigned char*)p; }, | 477 [](const void* p, void*) { delete[] (unsigned char*)p; }, |
519 nullptr)); | 478 nullptr)); |
520 subsetStream->dict()->insertInt("Length1", subsetFontSize); | 479 subsetStream->dict()->insertInt("Length1", subsetFontSize); |
521 return subsetStream; | 480 return subsetStream; |
522 } | 481 } |
523 #endif // SK_SFNTLY_SUBSETTER | 482 #endif // SK_SFNTLY_SUBSETTER |
524 | 483 |
525 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { | 484 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset, |
526 SkASSERT(this->canEmbed()); | 485 const SkAdvancedTypefaceMetrics& metrics) { |
527 SkASSERT(this->getFontInfo()); | 486 SkASSERT(can_embed(&metrics)); |
528 const SkAdvancedTypefaceMetrics& metrics = *(this->getFontInfo()); | |
529 SkAdvancedTypefaceMetrics::FontType type = this->getType(); | 487 SkAdvancedTypefaceMetrics::FontType type = this->getType(); |
530 SkTypeface* face = this->typeface(); | 488 SkTypeface* face = this->typeface(); |
531 SkASSERT(face); | 489 SkASSERT(face); |
532 const SkString& name = metrics.fFontName; | 490 const SkString& name = metrics.fFontName; |
533 | 491 |
534 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 492 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
535 add_common_font_descriptor_entries(descriptor.get(), metrics, 0); | 493 add_common_font_descriptor_entries(descriptor.get(), metrics, 0); |
536 switch (type) { | 494 switch (type) { |
537 case SkAdvancedTypefaceMetrics::kTrueType_Font: { | 495 case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
538 int ttcIndex; | 496 int ttcIndex; |
539 std::unique_ptr<SkStreamAsset> fontAsset(face->openStream(&ttcIndex) ); | 497 std::unique_ptr<SkStreamAsset> fontAsset(face->openStream(&ttcIndex) ); |
540 SkASSERT(fontAsset); | 498 SkASSERT(fontAsset); |
541 if (!fontAsset) { | 499 if (!fontAsset) { |
542 return false; | 500 return false; |
543 } | 501 } |
544 size_t fontSize = fontAsset->getLength(); | 502 size_t fontSize = fontAsset->getLength(); |
545 SkASSERT(fontSize > 0); | 503 SkASSERT(fontSize > 0); |
546 if (fontSize == 0) { | 504 if (fontSize == 0) { |
547 return false; | 505 return false; |
548 } | 506 } |
549 | 507 |
550 #ifdef SK_SFNTLY_SUBSETTER | 508 #ifdef SK_SFNTLY_SUBSETTER |
551 if (this->canSubset() && subset) { | 509 if (can_subset(&metrics) && subset) { |
552 // Generate glyph id array. in format needed by sfntly | 510 // Generate glyph id array. in format needed by sfntly |
553 SkTDArray<uint32_t> glyphIDs; | 511 SkTDArray<uint32_t> glyphIDs; |
554 if (subset) { | 512 if (subset) { |
555 if (!subset->has(0)) { | 513 if (!subset->has(0)) { |
556 glyphIDs.push(0); // Always include glyph 0. | 514 glyphIDs.push(0); // Always include glyph 0. |
557 } | 515 } |
558 subset->exportTo(&glyphIDs); | 516 subset->exportTo(&glyphIDs); |
559 } | 517 } |
560 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( | 518 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( |
561 std::move(fontAsset), glyphIDs, name.c_str()); | 519 std::move(fontAsset), glyphIDs, name.c_str()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 } else { | 558 } else { |
601 SkASSERT(false); | 559 SkASSERT(false); |
602 } | 560 } |
603 | 561 |
604 auto sysInfo = sk_make_sp<SkPDFDict>(); | 562 auto sysInfo = sk_make_sp<SkPDFDict>(); |
605 sysInfo->insertString("Registry", "Adobe"); | 563 sysInfo->insertString("Registry", "Adobe"); |
606 sysInfo->insertString("Ordering", "Identity"); | 564 sysInfo->insertString("Ordering", "Identity"); |
607 sysInfo->insertInt("Supplement", 0); | 565 sysInfo->insertInt("Supplement", 0); |
608 newCIDFont->insertObject("CIDSystemInfo", std::move(sysInfo)); | 566 newCIDFont->insertObject("CIDSystemInfo", std::move(sysInfo)); |
609 | 567 |
610 uint16_t emSize = this->getFontInfo()->fEmSize; | 568 uint16_t emSize = (uint16_t)metrics.fEmSize; |
bungeman-skia
2016/08/17 18:56:04
It's ugly that we cast this to uint16_t here for n
hal.canary
2016/08/17 19:29:49
Done.
that was some sort of cut-and-paste error.
| |
611 int16_t defaultWidth = 0; | 569 int16_t defaultWidth = 0; |
612 const SkBitSet* bitSet = subset ? &subset->bitSet() : nullptr; | 570 const SkBitSet* bitSet = subset ? &subset->bitSet() : nullptr; |
613 { | 571 { |
614 SkAutoGlyphCache glyphCache = vector_cache(face); | 572 SkAutoGlyphCache glyphCache = vector_cache(face); |
615 sk_sp<SkPDFArray> widths = SkPDFMakeCIDGlyphWidthsArray( | 573 sk_sp<SkPDFArray> widths = SkPDFMakeCIDGlyphWidthsArray( |
616 glyphCache.get(), bitSet, emSize, &defaultWidth); | 574 glyphCache.get(), bitSet, emSize, &defaultWidth); |
617 if (widths && widths->size() > 0) { | 575 if (widths && widths->size() > 0) { |
618 newCIDFont->insertObject("W", std::move(widths)); | 576 newCIDFont->insertObject("W", std::move(widths)); |
619 } | 577 } |
620 newCIDFont->insertScalar( | 578 newCIDFont->insertScalar( |
621 "DW", scaleFromFontUnits(defaultWidth, emSize)); | 579 "DW", scaleFromFontUnits(defaultWidth, emSize)); |
622 } | 580 } |
623 | 581 |
624 //////////////////////////////////////////////////////////////////////////// | 582 //////////////////////////////////////////////////////////////////////////// |
625 | 583 |
626 this->insertName("Subtype", "Type0"); | 584 this->insertName("Subtype", "Type0"); |
627 this->insertName("BaseFont", metrics.fFontName); | 585 this->insertName("BaseFont", metrics.fFontName); |
628 this->insertName("Encoding", "Identity-H"); | 586 this->insertName("Encoding", "Identity-H"); |
629 auto descendantFonts = sk_make_sp<SkPDFArray>(); | 587 auto descendantFonts = sk_make_sp<SkPDFArray>(); |
630 descendantFonts->appendObjRef(std::move(newCIDFont)); | 588 descendantFonts->appendObjRef(std::move(newCIDFont)); |
631 this->insertObject("DescendantFonts", std::move(descendantFonts)); | 589 this->insertObject("DescendantFonts", std::move(descendantFonts)); |
632 this->populateToUnicodeTable(subset); | 590 |
591 if (metrics.fGlyphToUnicode.count() > 0) { | |
592 this->insertObjRef("ToUnicode", | |
593 SkPDFMakeToUnicodeCmap(metrics.fGlyphToUnicode, | |
594 subset, | |
595 multiByteGlyphs(), | |
596 firstGlyphID(), | |
597 lastGlyphID())); | |
598 } | |
633 SkDEBUGCODE(fPopulated = true); | 599 SkDEBUGCODE(fPopulated = true); |
634 return true; | 600 return true; |
635 } | 601 } |
636 | 602 |
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 /////////////////////////////////////////////////////////////////////////////// | 603 /////////////////////////////////////////////////////////////////////////////// |
646 // class SkPDFType1Font | 604 // class SkPDFType1Font |
647 /////////////////////////////////////////////////////////////////////////////// | 605 /////////////////////////////////////////////////////////////////////////////// |
648 | 606 |
649 SkPDFType1Font::SkPDFType1Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 607 static sk_sp<SkPDFDict> make_type1_font_descriptor( |
650 sk_sp<SkTypeface> typeface, | 608 SkTypeface* typeface, |
651 uint16_t glyphID, | 609 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"); | 610 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
670 setFontDescriptor(descriptor); | 611 SkASSERT(info); |
612 add_common_font_descriptor_entries(descriptor.get(), *info, 0); | |
671 | 613 |
672 int ttcIndex; | 614 int ttcIndex; |
673 size_t header SK_INIT_TO_AVOID_WARNING; | 615 size_t header SK_INIT_TO_AVOID_WARNING; |
674 size_t data SK_INIT_TO_AVOID_WARNING; | 616 size_t data SK_INIT_TO_AVOID_WARNING; |
675 size_t trailer SK_INIT_TO_AVOID_WARNING; | 617 size_t trailer SK_INIT_TO_AVOID_WARNING; |
676 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex) ); | 618 std::unique_ptr<SkStreamAsset> rawFontData(typeface->openStream(&ttcIndex)); |
677 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), | 619 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), |
678 &header, &data, &traile r); | 620 &header, &data, &traile r); |
679 if (!fontData) { | 621 if (fontData && can_embed(info)) { |
680 return false; | 622 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); |
623 fontStream->dict()->insertInt("Length1", header); | |
624 fontStream->dict()->insertInt("Length2", data); | |
625 fontStream->dict()->insertInt("Length3", trailer); | |
626 descriptor->insertObjRef("FontFile", std::move(fontStream)); | |
681 } | 627 } |
682 SkASSERT(this->canEmbed()); | 628 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 } | 629 } |
696 | 630 |
697 bool SkPDFType1Font::populate(int16_t glyphID) { | 631 static void populate_type_1_font(SkPDFDict* font, |
698 this->insertName("Subtype", "Type1"); | 632 const SkAdvancedTypefaceMetrics* info, |
699 this->insertName("BaseFont", this->getFontInfo()->fFontName); | 633 SkTypeface* typeface, |
700 adjustGlyphRangeForSingleByteEncoding(glyphID); | 634 SkGlyphID firstGlyphID, |
701 SkGlyphID firstGlyphID = this->firstGlyphID(); | 635 SkGlyphID lastGlyphID) { |
702 SkGlyphID lastGlyphID = this->lastGlyphID(); | 636 SkASSERT(info); |
637 font->insertName("Subtype", "Type1"); | |
638 font->insertName("BaseFont", info->fFontName); | |
703 | 639 |
704 // glyphCount not including glyph 0 | 640 // glyphCount not including glyph 0 |
705 unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; | 641 unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; |
706 SkASSERT(glyphCount > 0 && glyphCount <= 255); | 642 SkASSERT(glyphCount > 0 && glyphCount <= 255); |
707 this->insertInt("FirstChar", (size_t)0); | 643 font->insertInt("FirstChar", (size_t)0); |
708 this->insertInt("LastChar", (size_t)glyphCount); | 644 font->insertInt("LastChar", (size_t)glyphCount); |
709 { | 645 { |
710 SkAutoGlyphCache glyphCache = vector_cache(this->typeface()); | 646 SkAutoGlyphCache glyphCache = vector_cache(typeface); |
711 auto widths = sk_make_sp<SkPDFArray>(); | 647 auto widths = sk_make_sp<SkPDFArray>(); |
712 SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; | 648 SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; |
713 const uint16_t emSize = this->getFontInfo()->fEmSize; | 649 const uint16_t emSize = info->fEmSize; |
714 widths->appendScalar(from_font_units(advance, emSize)); | 650 widths->appendScalar(from_font_units(advance, emSize)); |
715 for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { | 651 for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
716 advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; | 652 advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; |
717 widths->appendScalar(from_font_units(advance, emSize)); | 653 widths->appendScalar(from_font_units(advance, emSize)); |
718 } | 654 } |
719 this->insertObject("Widths", std::move(widths)); | 655 font->insertObject("Widths", std::move(widths)); |
720 } | |
721 if (!addFontDescriptor(0)) { | |
722 return false; | |
723 } | 656 } |
724 auto encDiffs = sk_make_sp<SkPDFArray>(); | 657 auto encDiffs = sk_make_sp<SkPDFArray>(); |
725 encDiffs->reserve(lastGlyphID - firstGlyphID + 3); | 658 encDiffs->reserve(lastGlyphID - firstGlyphID + 3); |
726 encDiffs->appendInt(0); | 659 encDiffs->appendInt(0); |
727 const SkTArray<SkString>& glyphNames = this->getFontInfo()->fGlyphNames; | 660 const SkTArray<SkString>& glyphNames = info->fGlyphNames; |
728 SkASSERT(glyphNames.count() > lastGlyphID); | 661 SkASSERT(glyphNames.count() > lastGlyphID); |
729 encDiffs->appendName(glyphNames[0].c_str()); | 662 encDiffs->appendName(glyphNames[0].c_str()); |
730 const SkString unknown("UNKNOWN"); | 663 const SkString unknown("UNKNOWN"); |
731 for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { | 664 for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
732 const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty( ); | 665 const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty( ); |
733 const SkString& name = valid ? glyphNames[gID] : unknown; | 666 const SkString& name = valid ? glyphNames[gID] : unknown; |
734 encDiffs->appendName(name); | 667 encDiffs->appendName(name); |
735 } | 668 } |
736 | 669 |
737 auto encoding = sk_make_sp<SkPDFDict>("Encoding"); | 670 auto encoding = sk_make_sp<SkPDFDict>("Encoding"); |
738 encoding->insertObject("Differences", std::move(encDiffs)); | 671 encoding->insertObject("Differences", std::move(encDiffs)); |
739 this->insertObject("Encoding", std::move(encoding)); | 672 font->insertObject("Encoding", std::move(encoding)); |
740 return true; | 673 } |
674 | |
675 SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, | |
676 sk_sp<SkTypeface> typeface, | |
677 uint16_t glyphID, | |
678 SkPDFCanon* canon) | |
679 : SkPDFFont(std::move(typeface), | |
680 SkAdvancedTypefaceMetrics::kType1_Font) { | |
bungeman-skia
2016/08/17 18:56:04
I would much prefer the brace on the next line at
hal.canary
2016/08/17 19:29:49
Acknowledged.
| |
681 SkFontID fontID = this->typeface()->uniqueID() ; | |
682 sk_sp<SkPDFDict> fontDescriptor; | |
683 if (sk_sp<SkPDFDict>* ptr = canon->fFontDescriptors.find(fontID)) { | |
684 fontDescriptor = *ptr; | |
685 } else { | |
686 fontDescriptor = make_type1_font_descriptor(this->typeface(), info); | |
687 canon->fFontDescriptors.set(fontID, fontDescriptor); | |
688 } | |
689 this->insertObjRef("FontDescriptor", std::move(fontDescriptor)); | |
690 this->adjustGlyphRangeForSingleByteEncoding(glyphID); | |
691 // TODO(halcanary): subset this (advances and names). | |
692 populate_type_1_font(this, info, this->typeface(), | |
693 this->firstGlyphID(), this->lastGlyphID()); | |
741 } | 694 } |
742 | 695 |
743 /////////////////////////////////////////////////////////////////////////////// | 696 /////////////////////////////////////////////////////////////////////////////// |
744 // class SkPDFType3Font | 697 // class SkPDFType3Font |
745 /////////////////////////////////////////////////////////////////////////////// | 698 /////////////////////////////////////////////////////////////////////////////// |
746 | 699 |
747 namespace { | 700 namespace { |
748 // returns [0, first, first+1, ... last-1, last] | 701 // returns [0, first, first+1, ... last-1, last] |
749 struct SingleByteGlyphIdIterator { | 702 struct SingleByteGlyphIdIterator { |
750 SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) | 703 SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) |
(...skipping 16 matching lines...) Expand all Loading... | |
767 int fCurrent; // must be int to make fLast+1 to fit | 720 int fCurrent; // must be int to make fLast+1 to fit |
768 }; | 721 }; |
769 Iter begin() const { return Iter(fFirst, 0); } | 722 Iter begin() const { return Iter(fFirst, 0); } |
770 Iter end() const { return Iter(fFirst, (int)fLast + 1); } | 723 Iter end() const { return Iter(fFirst, (int)fLast + 1); } |
771 private: | 724 private: |
772 const SkGlyphID fFirst; | 725 const SkGlyphID fFirst; |
773 const SkGlyphID fLast; | 726 const SkGlyphID fLast; |
774 }; | 727 }; |
775 } | 728 } |
776 | 729 |
777 static void add_type3_font_info(SkPDFDict* font, | 730 static void add_type3_font_info(SkPDFCanon* canon, |
731 SkPDFDict* font, | |
778 SkTypeface* typeface, | 732 SkTypeface* typeface, |
779 SkScalar emSize, | 733 SkScalar emSize, |
780 const SkPDFGlyphSet* subset, | 734 const SkPDFGlyphSet* subset, |
781 SkGlyphID firstGlyphID, | 735 SkGlyphID firstGlyphID, |
782 SkGlyphID lastGlyphID) { | 736 SkGlyphID lastGlyphID) { |
783 SkASSERT(lastGlyphID >= firstGlyphID); | 737 SkASSERT(lastGlyphID >= firstGlyphID); |
784 SkASSERT(emSize > 0.0f); | 738 SkASSERT(emSize > 0.0f); |
785 SkAutoGlyphCache cache = vector_cache(typeface, emSize); | 739 SkAutoGlyphCache cache = vector_cache(typeface, emSize); |
786 font->insertName("Subtype", "Type3"); | 740 font->insertName("Subtype", "Type3"); |
787 // Flip about the x-axis and scale by 1/emSize. | 741 // 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 | 809 glyphs of the font were placed with their origins coincident and |
856 then filled." */ | 810 then filled." */ |
857 auto fontBBox = sk_make_sp<SkPDFArray>(); | 811 auto fontBBox = sk_make_sp<SkPDFArray>(); |
858 fontBBox->reserve(4); | 812 fontBBox->reserve(4); |
859 fontBBox->appendInt(bbox.left()); | 813 fontBBox->appendInt(bbox.left()); |
860 fontBBox->appendInt(bbox.bottom()); | 814 fontBBox->appendInt(bbox.bottom()); |
861 fontBBox->appendInt(bbox.right()); | 815 fontBBox->appendInt(bbox.right()); |
862 fontBBox->appendInt(bbox.top()); | 816 fontBBox->appendInt(bbox.top()); |
863 font->insertObject("FontBBox", std::move(fontBBox)); | 817 font->insertObject("FontBBox", std::move(fontBBox)); |
864 font->insertName("CIDToGIDMap", "Identity"); | 818 font->insertName("CIDToGIDMap", "Identity"); |
865 sk_sp<const SkAdvancedTypefaceMetrics> metrics; | 819 const SkAdvancedTypefaceMetrics* metrics = |
866 if (subset) { | 820 SkPDFFont::GetMetrics(typeface, canon); |
867 SkTDArray<uint32_t> subsetList; | 821 if (metrics/* && metrics->fGlyphToUnicode.count() > 0 */) { |
bungeman-skia
2016/08/17 18:56:04
commented out?
hal.canary
2016/08/17 19:29:49
Apparently we sometimes have fGlyphToUnicode.count
| |
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", | 822 font->insertObjRef("ToUnicode", |
881 SkPDFMakeToUnicodeCmap(metrics->fGlyphToUnicode, | 823 SkPDFMakeToUnicodeCmap(metrics->fGlyphToUnicode, |
882 subset, | 824 subset, |
883 false, | 825 false, |
884 firstGlyphID, | 826 firstGlyphID, |
885 lastGlyphID)); | 827 lastGlyphID)); |
886 } | 828 } |
887 font->insertObject("Widths", std::move(widthArray)); | 829 font->insertObject("Widths", std::move(widthArray)); |
888 font->insertObject("Encoding", std::move(encoding)); | 830 font->insertObject("Encoding", std::move(encoding)); |
889 font->insertObject("CharProcs", std::move(charProcs)); | 831 font->insertObject("CharProcs", std::move(charProcs)); |
890 } | 832 } |
891 | 833 |
892 SkPDFType3Font::SkPDFType3Font(sk_sp<const SkAdvancedTypefaceMetrics> info, | 834 SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
893 sk_sp<SkTypeface> typeface, | 835 sk_sp<SkTypeface> typeface, |
894 SkAdvancedTypefaceMetrics::FontType fontType, | 836 SkAdvancedTypefaceMetrics::FontType fontType, |
895 uint16_t glyphID) | 837 uint16_t glyphID) |
896 : SkPDFFont(std::move(info), std::move(typeface), nullptr, | 838 : 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); | 839 this->adjustGlyphRangeForSingleByteEncoding(glyphID); |
901 } | 840 } |
902 | 841 |
903 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { | 842 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(SkPDFCanon* canon, |
843 const SkPDFGlyphSet* usage) { | |
904 // All fonts are subset before serialization. | 844 // All fonts are subset before serialization. |
905 // TODO(halcanary): all fonts should follow this pattern. | 845 // TODO(halcanary): all fonts should follow this pattern. |
846 const SkAdvancedTypefaceMetrics* info = | |
847 SkPDFFont::GetMetrics(this->typeface(), canon); | |
848 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; | |
906 auto font = sk_make_sp<SkPDFDict>("Font"); | 849 auto font = sk_make_sp<SkPDFDict>("Font"); |
907 const SkAdvancedTypefaceMetrics* info = this->getFontInfo(); | 850 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()); | 851 this->firstGlyphID(), this->lastGlyphID()); |
911 return font; | 852 return font; |
912 } | 853 } |
913 | 854 |
914 | 855 |
915 //////////////////////////////////////////////////////////////////////////////// | 856 //////////////////////////////////////////////////////////////////////////////// |
916 | 857 |
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) { | 858 bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFCanon* canon) { |
940 SkFontID id = SkTypeface::UniqueID(typeface); | 859 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 } | 860 } |
956 | 861 |
957 void SkPDFFont::drop() { | 862 void SkPDFFont::drop() { |
958 fTypeface = nullptr; | 863 fTypeface = nullptr; |
959 fFontInfo = nullptr; | |
960 fDescriptor = nullptr; | |
961 this->SkPDFDict::drop(); | 864 this->SkPDFDict::drop(); |
962 } | 865 } |
OLD | NEW |