Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: src/pdf/SkPDFFont.h

Issue 2278703002: SkPDF: Glyph validation change (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: major refactor Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #ifndef SkPDFFont_DEFINED 9 #ifndef SkPDFFont_DEFINED
10 #define SkPDFFont_DEFINED 10 #define SkPDFFont_DEFINED
(...skipping 17 matching lines...) Expand all
28 class SkPDFFont : public SkPDFDict { 28 class SkPDFFont : public SkPDFDict {
29 29
30 public: 30 public:
31 virtual ~SkPDFFont(); 31 virtual ~SkPDFFont();
32 32
33 /** Returns the typeface represented by this class. Returns nullptr for the 33 /** Returns the typeface represented by this class. Returns nullptr for the
34 * default typeface. 34 * default typeface.
35 */ 35 */
36 SkTypeface* typeface() const { return fTypeface.get(); } 36 SkTypeface* typeface() const { return fTypeface.get(); }
37 37
38 struct TypefaceInfo {
39 SkGlyphID fMaxGlyphID;
40 bool fGood;
41 };
42 static TypefaceInfo GetTypefaceInfo(SkTypeface* face) {
43 SkASSERT(face);
44 int count = face->countGlyphs();
45 return (count > 0 && count <= 1 + SK_MaxU16)
46 ? TypefaceInfo{SkToU16(count - 1), true}
47 : TypefaceInfo{0, false};
48 }
49
38 /** Returns the font type represented in this font. For Type0 fonts, 50 /** Returns the font type represented in this font. For Type0 fonts,
39 * returns the type of the decendant font. 51 * returns the type of the decendant font.
40 */ 52 */
41 SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; } 53 SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; }
42 54
43 static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) { 55 static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) {
44 return type == SkAdvancedTypefaceMetrics::kType1CID_Font || 56 return type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
45 type == SkAdvancedTypefaceMetrics::kTrueType_Font; 57 type == SkAdvancedTypefaceMetrics::kTrueType_Font;
46 } 58 }
47 59
48 /** Returns true if this font encoding supports glyph IDs above 255. 60 /** Returns true if this font encoding supports glyph IDs above 255.
49 */ 61 */
50 bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType() ); } 62 bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType() ); }
51 63
52 /** Return true if this font has an encoding for the passed glyph id. 64 /** Return true if this font has an encoding for the passed glyph id.
53 */ 65 */
54 bool hasGlyph(SkGlyphID gid) { 66 bool hasGlyph(SkGlyphID gid) {
55 return (gid >= fFirstGlyphID && gid <= fLastGlyphID) || gid == 0; 67 return (gid >= fFirstGlyphID && gid <= fLastGlyphID) || gid == 0;
56 } 68 }
57 69
58 /** Convert (in place) the input glyph IDs into the font encoding. If the 70 /** Convert the input glyph ID into the font encoding. */
59 * font has more glyphs than can be encoded (like a type 1 font with more 71 SkGlyphID glyphToPDFFontEncoding(SkGlyphID gid) const {
60 * than 255 glyphs) this method only converts up to the first out of range 72 if (this->multiByteGlyphs() || gid == 0) {
61 * glyph ID. 73 return gid;
62 * @param glyphIDs The input text as glyph IDs. 74 }
63 * @param numGlyphs The number of input glyphs. 75 SkASSERT(gid >= fFirstGlyphID && gid <= fLastGlyphID);
64 * @return Returns the number of glyphs consumed. 76 SkASSERT(fFirstGlyphID > 0);
65 */ 77 return gid - fFirstGlyphID + 1;
66 int glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const; 78 }
67 /**
68 * Like above, but does not modify glyphIDs array.
69 */
70 int glyphsToPDFFontEncodingCount(const SkGlyphID* glyphIDs,
71 int numGlyphs) const;
72 79
73 void noteGlyphUsage(const SkGlyphID* glyphs, int count) { 80 /** Count the number of glyphIDs that can be encoded with this font.
74 fGlyphUsage.setAll(glyphs, count); 81 * glyphIDs > maxGlyphID are considered okay. */
82 int countStretch(const SkGlyphID* glyphIDs,
83 int numGlyphs,
84 SkGlyphID maxGlyphID) const {
85 SkASSERT(!this->multiByteGlyphs());
bungeman-skia 2016/08/25 19:28:44 This seems like a really odd thing to put here. Wh
hal.canary 2016/08/26 15:27:04 Done.
86 for (int i = 0; i < numGlyphs; i++) {
87 SkGlyphID gid = glyphIDs[i];
88 if (gid != 0 && gid <= maxGlyphID &&
89 (gid < fFirstGlyphID || gid > fLastGlyphID)) {
90 return i;
91 }
92 }
93 return numGlyphs;
94 }
95
96 void noteGlyphUsage(SkGlyphID glyph) {
97 SkASSERT(this->hasGlyph(glyph));
98 fGlyphUsage.set(glyph);
75 } 99 }
76 100
77 /** Get the font resource for the passed typeface and glyphID. The 101 /** Get the font resource for the passed typeface and glyphID. The
78 * reference count of the object is incremented and it is the caller's 102 * reference count of the object is incremented and it is the caller's
79 * responsibility to unreference it when done. This is needed to 103 * responsibility to unreference it when done. This is needed to
80 * accommodate the weak reference pattern used when the returned object 104 * accommodate the weak reference pattern used when the returned object
81 * is new and has no other references. 105 * is new and has no other references.
82 * @param typeface The typeface to find. 106 * @param typeface The typeface to find, non-NULL.
bungeman-skia 2016/08/25 19:28:44 nit: what's NULL? Oh, it's that old name for nullp
hal.canary 2016/08/26 15:27:04 Done.
83 * @param glyphID Specify which section of a large font is of interest. 107 * @param glyphID Specify which section of a large font is of interest.
84 */ 108 */
85 static SkPDFFont* GetFontResource(SkPDFCanon* canon, 109 static SkPDFFont* GetFontResource(SkPDFCanon* canon,
86 SkTypeface* typeface, 110 SkTypeface* typeface,
87 SkGlyphID glyphID); 111 SkGlyphID glyphID);
88 112
89 // Uses (kGlyphNames_PerGlyphInfo | kToUnicode_PerGlyphInfo). 113 // Uses (kGlyphNames_PerGlyphInfo | kToUnicode_PerGlyphInfo).
114 // typeface is non-NULL
90 static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface, 115 static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface,
91 SkPDFCanon* canon); 116 SkPDFCanon* canon);
92 117
93 /** Subset the font based on current usage. 118 /** Subset the font based on current usage.
94 * Must be called before emitObject(). 119 * Must be called before emitObject().
95 */ 120 */
96 virtual void getFontSubset(SkPDFCanon*) = 0; 121 virtual void getFontSubset(SkPDFCanon*) = 0;
97 122
98 /** 123 /**
99 * Return false iff the typeface has its NotEmbeddable flag set. 124 * Return false iff the typeface has its NotEmbeddable flag set.
100 * If typeface is NULL, the default typeface is checked. 125 * typeface is non-NULL
101 */ 126 */
102 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*); 127 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*);
103 128
104 protected: 129 protected:
105 // Common constructor to handle common members. 130 // Common constructor to handle common members.
106 struct Info { 131 struct Info {
107 sk_sp<SkTypeface> fTypeface; 132 sk_sp<SkTypeface> fTypeface;
108 SkGlyphID fFirstGlyphID; 133 SkGlyphID fFirstGlyphID;
109 SkGlyphID fLastGlyphID; 134 SkGlyphID fLastGlyphID;
110 SkAdvancedTypefaceMetrics::FontType fFontType; 135 SkAdvancedTypefaceMetrics::FontType fFontType;
111 }; 136 };
112 SkPDFFont(Info); 137 SkPDFFont(Info);
113 138
114 SkGlyphID firstGlyphID() const { return fFirstGlyphID; } 139 SkGlyphID firstGlyphID() const { return fFirstGlyphID; }
115 SkGlyphID lastGlyphID() const { return fLastGlyphID; } 140 SkGlyphID lastGlyphID() const { return fLastGlyphID; }
116 const SkBitSet& glyphUsage() const { return fGlyphUsage; } 141 const SkBitSet& glyphUsage() const { return fGlyphUsage; }
117 sk_sp<SkTypeface> refTypeface() const { return fTypeface; } 142 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
118 143
119 void drop() override; 144 void drop() override;
120 145
121 private: 146 private:
122 sk_sp<SkTypeface> fTypeface; 147 sk_sp<SkTypeface> fTypeface;
123 SkBitSet fGlyphUsage; 148 SkBitSet fGlyphUsage;
124 149
125 // The glyph IDs accessible with this font. For Type1 (non CID) fonts, 150 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
126 // this will be a subset if the font has more than 255 glyphs. 151 // this will be a subset if the font has more than 255 glyphs.
127 SkGlyphID fFirstGlyphID; 152 const SkGlyphID fFirstGlyphID;
128 SkGlyphID fLastGlyphID; 153 const SkGlyphID fLastGlyphID;
129 SkAdvancedTypefaceMetrics::FontType fFontType; 154 const SkAdvancedTypefaceMetrics::FontType fFontType;
130 155
131 typedef SkPDFDict INHERITED; 156 typedef SkPDFDict INHERITED;
132 }; 157 };
133 158
134 #endif 159 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698