Flutter Engine
The Flutter Engine
SkPDFFont.h
Go to the documentation of this file.
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#ifndef SkPDFFont_DEFINED
8#define SkPDFFont_DEFINED
9
13#include "src/base/SkUTF.h"
16#include "src/pdf/SkPDFTypes.h"
17
18#include <cstdint>
19#include <vector>
20
21class SkGlyph;
22class SkPDFDocument;
23class SkString;
24
25/** \class SkPDFFont
26 A PDF Object class representing a font. The font may have resources
27 attached to it in order to embed the font. SkPDFFonts are canonicalized
28 so that resource deduplication will only include one copy of a font.
29 This class uses the same pattern as SkPDFGraphicState, a static weak
30 reference to each instantiated class.
31*/
32class SkPDFFont {
33public:
37
38 /** Returns the typeface represented by this class. Returns nullptr for the
39 * default typeface.
40 */
41 SkTypeface* typeface() const { return fTypeface.get(); }
42
43 /** Returns the font type represented in this font. For Type0 fonts,
44 * returns the type of the descendant font.
45 */
46 SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; }
47
50 static void GetType1GlyphNames(const SkTypeface&, SkString*);
51
56 }
57
58 /** Returns true if this font encoding supports glyph IDs above 255.
59 */
60 bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType()); }
61
62 /** Return true if this font has an encoding for the passed glyph id.
63 */
64 bool hasGlyph(SkGlyphID gid) {
65 return (gid >= this->firstGlyphID() && gid <= this->lastGlyphID()) || gid == 0;
66 }
67
68 /** Convert the input glyph ID into the font encoding. */
70 if (this->multiByteGlyphs() || gid == 0) {
71 return gid;
72 }
73 SkASSERT(gid >= this->firstGlyphID() && gid <= this->lastGlyphID());
74 SkASSERT(this->firstGlyphID() > 0);
75 return gid - this->firstGlyphID() + 1;
76 }
77
79 SkASSERT(this->hasGlyph(glyph));
80 fGlyphUsage.set(glyph);
81 }
82
83 SkPDFIndirectReference indirectReference() const { return fIndirectReference; }
84
85 /** Get the font resource for the passed typeface and glyphID. The
86 * reference count of the object is incremented and it is the caller's
87 * responsibility to unreference it when done. This is needed to
88 * accommodate the weak reference pattern used when the returned object
89 * is new and has no other references.
90 * @param typeface The typeface to find, not nullptr.
91 * @param glyphID Specify which section of a large font is of interest.
92 */
94 const SkGlyph* glyphs,
96
97 /** Gets SkAdvancedTypefaceMetrics, and caches the result.
98 * @param typeface can not be nullptr.
99 * @return nullptr only when typeface is bad.
100 */
102 SkPDFDocument* canon);
103
104 static const std::vector<SkUnichar>& GetUnicodeMap(const SkTypeface* typeface,
105 SkPDFDocument* canon);
106
107 static void PopulateCommonFontDescriptor(SkPDFDict* descriptor,
109 uint16_t emSize,
110 int16_t defaultWidth);
111
112 void emitSubset(SkPDFDocument*) const;
113
114 /**
115 * Return false iff the typeface has its NotEmbeddable flag set.
116 * typeface is not nullptr
117 */
119
120 SkGlyphID firstGlyphID() const { return fGlyphUsage.firstNonZero(); }
121 SkGlyphID lastGlyphID() const { return fGlyphUsage.lastGlyph(); }
122 const SkPDFGlyphUse& glyphUsage() const { return fGlyphUsage; }
123 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
124
125private:
126 sk_sp<SkTypeface> fTypeface;
127 SkPDFGlyphUse fGlyphUsage;
128 SkPDFIndirectReference fIndirectReference;
130
136 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
137 // this will be a subset if the font has more than 255 glyphs.
138
139 SkPDFFont() = delete;
140 SkPDFFont(const SkPDFFont&) = delete;
141 SkPDFFont& operator=(const SkPDFFont&) = delete;
142};
143
144#endif
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint16_t SkGlyphID
Definition: SkTypes.h:179
GLenum type
SkAdvancedTypefaceMetrics::FontType getType() const
Definition: SkPDFFont.h:46
void noteGlyphUsage(SkGlyphID glyph)
Definition: SkPDFFont.h:78
bool hasGlyph(SkGlyphID gid)
Definition: SkPDFFont.h:64
bool multiByteGlyphs() const
Definition: SkPDFFont.h:60
static const SkAdvancedTypefaceMetrics * GetMetrics(const SkTypeface *typeface, SkPDFDocument *canon)
Definition: SkPDFFont.cpp:118
static void PopulateCommonFontDescriptor(SkPDFDict *descriptor, const SkAdvancedTypefaceMetrics &, uint16_t emSize, int16_t defaultWidth)
Definition: SkPDFFont.cpp:265
static SkPDFFont * GetFontResource(SkPDFDocument *doc, const SkGlyph *glyphs, SkTypeface *typeface)
Definition: SkPDFFont.cpp:208
SkPDFIndirectReference indirectReference() const
Definition: SkPDFFont.h:83
static SkAdvancedTypefaceMetrics::FontType FontType(const SkTypeface &, const SkAdvancedTypefaceMetrics &)
Definition: SkPDFFont.cpp:183
static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type)
Definition: SkPDFFont.h:52
static const std::vector< SkUnichar > & GetUnicodeMap(const SkTypeface *typeface, SkPDFDocument *canon)
Definition: SkPDFFont.cpp:170
SkPDFFont & operator=(SkPDFFont &&)
SkGlyphID lastGlyphID() const
Definition: SkPDFFont.h:121
SkGlyphID firstGlyphID() const
Definition: SkPDFFont.h:120
SkPDFFont(SkPDFFont &&)
void emitSubset(SkPDFDocument *) const
Definition: SkPDFFont.cpp:726
const SkPDFGlyphUse & glyphUsage() const
Definition: SkPDFFont.h:122
static void GetType1GlyphNames(const SkTypeface &, SkString *)
Definition: SkPDFFont.cpp:59
sk_sp< SkTypeface > refTypeface() const
Definition: SkPDFFont.h:123
SkGlyphID glyphToPDFFontEncoding(SkGlyphID gid) const
Definition: SkPDFFont.h:69
SkTypeface * typeface() const
Definition: SkPDFFont.h:41
static bool CanEmbedTypeface(SkTypeface *, SkPDFDocument *)
Definition: SkPDFFont.cpp:743
void set(SkGlyphID gid)
Definition: SkPDFGlyphUse.h:22
SkGlyphID lastGlyph() const
Definition: SkPDFGlyphUse.h:21
SkGlyphID firstNonZero() const
Definition: SkPDFGlyphUse.h:20
T * get() const
Definition: SkRefCnt.h:303