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

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: fix compile 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 if (this->multiByteGlyphs()) {
86 return numGlyphs;
87 }
88 for (int i = 0; i < numGlyphs; i++) {
89 SkGlyphID gid = glyphIDs[i];
90 if (gid != 0 && gid <= maxGlyphID &&
91 (gid < fFirstGlyphID || gid > fLastGlyphID)) {
92 return i;
93 }
94 }
95 return numGlyphs;
96 }
97
98 void noteGlyphUsage(SkGlyphID glyph) {
99 SkASSERT(this->hasGlyph(glyph));
100 fGlyphUsage.set(glyph);
75 } 101 }
76 102
77 /** Get the font resource for the passed typeface and glyphID. The 103 /** 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 104 * reference count of the object is incremented and it is the caller's
79 * responsibility to unreference it when done. This is needed to 105 * responsibility to unreference it when done. This is needed to
80 * accommodate the weak reference pattern used when the returned object 106 * accommodate the weak reference pattern used when the returned object
81 * is new and has no other references. 107 * is new and has no other references.
82 * @param typeface The typeface to find. 108 * @param typeface The typeface to find, not nullptr.
83 * @param glyphID Specify which section of a large font is of interest. 109 * @param glyphID Specify which section of a large font is of interest.
84 */ 110 */
85 static SkPDFFont* GetFontResource(SkPDFCanon* canon, 111 static SkPDFFont* GetFontResource(SkPDFCanon* canon,
86 SkTypeface* typeface, 112 SkTypeface* typeface,
87 SkGlyphID glyphID); 113 SkGlyphID glyphID);
88 114
89 // Uses (kGlyphNames_PerGlyphInfo | kToUnicode_PerGlyphInfo). 115 /** Uses (kGlyphNames_PerGlyphInfo | kToUnicode_PerGlyphInfo) to get
116 * SkAdvancedTypefaceMetrics, and caches the result.
117 * @param typeface can not be nullptr.
118 * @return nullptr only when typeface is bad.
119 */
90 static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface, 120 static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface,
91 SkPDFCanon* canon); 121 SkPDFCanon* canon);
92 122
93 /** Subset the font based on current usage. 123 /** Subset the font based on current usage.
94 * Must be called before emitObject(). 124 * Must be called before emitObject().
95 */ 125 */
96 virtual void getFontSubset(SkPDFCanon*) = 0; 126 virtual void getFontSubset(SkPDFCanon*) = 0;
97 127
98 /** 128 /**
99 * Return false iff the typeface has its NotEmbeddable flag set. 129 * Return false iff the typeface has its NotEmbeddable flag set.
100 * If typeface is NULL, the default typeface is checked. 130 * typeface is not nullptr
101 */ 131 */
102 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*); 132 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*);
103 133
104 protected: 134 protected:
105 // Common constructor to handle common members. 135 // Common constructor to handle common members.
106 struct Info { 136 struct Info {
107 sk_sp<SkTypeface> fTypeface; 137 sk_sp<SkTypeface> fTypeface;
108 SkGlyphID fFirstGlyphID; 138 SkGlyphID fFirstGlyphID;
109 SkGlyphID fLastGlyphID; 139 SkGlyphID fLastGlyphID;
110 SkAdvancedTypefaceMetrics::FontType fFontType; 140 SkAdvancedTypefaceMetrics::FontType fFontType;
111 }; 141 };
112 SkPDFFont(Info); 142 SkPDFFont(Info);
113 143
114 SkGlyphID firstGlyphID() const { return fFirstGlyphID; } 144 SkGlyphID firstGlyphID() const { return fFirstGlyphID; }
115 SkGlyphID lastGlyphID() const { return fLastGlyphID; } 145 SkGlyphID lastGlyphID() const { return fLastGlyphID; }
116 const SkBitSet& glyphUsage() const { return fGlyphUsage; } 146 const SkBitSet& glyphUsage() const { return fGlyphUsage; }
117 sk_sp<SkTypeface> refTypeface() const { return fTypeface; } 147 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
118 148
119 void drop() override; 149 void drop() override;
120 150
121 private: 151 private:
122 sk_sp<SkTypeface> fTypeface; 152 sk_sp<SkTypeface> fTypeface;
123 SkBitSet fGlyphUsage; 153 SkBitSet fGlyphUsage;
124 154
125 // The glyph IDs accessible with this font. For Type1 (non CID) fonts, 155 // 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. 156 // this will be a subset if the font has more than 255 glyphs.
127 SkGlyphID fFirstGlyphID; 157 const SkGlyphID fFirstGlyphID;
128 SkGlyphID fLastGlyphID; 158 const SkGlyphID fLastGlyphID;
129 SkAdvancedTypefaceMetrics::FontType fFontType; 159 const SkAdvancedTypefaceMetrics::FontType fFontType;
130 160
131 typedef SkPDFDict INHERITED; 161 typedef SkPDFDict INHERITED;
132 }; 162 };
133 163
134 #endif 164 #endif
OLDNEW
« src/pdf/SkPDFDevice.cpp ('K') | « src/pdf/SkPDFDevice.cpp ('k') | src/pdf/SkPDFFont.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698