Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
SkPDFFont Class Reference

#include <SkPDFFont.h>

Public Member Functions

 ~SkPDFFont ()
 
 SkPDFFont (SkPDFFont &&)
 
SkPDFFontoperator= (SkPDFFont &&)
 
SkTypefacetypeface () const
 
SkAdvancedTypefaceMetrics::FontType getType () const
 
bool multiByteGlyphs () const
 
bool hasGlyph (SkGlyphID gid)
 
SkGlyphID glyphToPDFFontEncoding (SkGlyphID gid) const
 
void noteGlyphUsage (SkGlyphID glyph)
 
SkPDFIndirectReference indirectReference () const
 
void emitSubset (SkPDFDocument *) const
 
SkGlyphID firstGlyphID () const
 
SkGlyphID lastGlyphID () const
 
const SkPDFGlyphUseglyphUsage () const
 
sk_sp< SkTypefacerefTypeface () const
 

Static Public Member Functions

static SkAdvancedTypefaceMetrics::FontType FontType (const SkTypeface &, const SkAdvancedTypefaceMetrics &)
 
static void GetType1GlyphNames (const SkTypeface &, SkString *)
 
static bool IsMultiByte (SkAdvancedTypefaceMetrics::FontType type)
 
static SkPDFFontGetFontResource (SkPDFDocument *doc, const SkGlyph *glyphs, SkTypeface *typeface)
 
static const SkAdvancedTypefaceMetricsGetMetrics (const SkTypeface *typeface, SkPDFDocument *canon)
 
static const std::vector< SkUnichar > & GetUnicodeMap (const SkTypeface *typeface, SkPDFDocument *canon)
 
static void PopulateCommonFontDescriptor (SkPDFDict *descriptor, const SkAdvancedTypefaceMetrics &, uint16_t emSize, int16_t defaultWidth)
 
static bool CanEmbedTypeface (SkTypeface *, SkPDFDocument *)
 

Detailed Description

A PDF Object class representing a font. The font may have resources attached to it in order to embed the font. SkPDFFonts are canonicalized so that resource deduplication will only include one copy of a font. This class uses the same pattern as SkPDFGraphicState, a static weak reference to each instantiated class.

Definition at line 32 of file SkPDFFont.h.

Constructor & Destructor Documentation

◆ ~SkPDFFont()

SkPDFFont::~SkPDFFont ( )
default

◆ SkPDFFont()

SkPDFFont::SkPDFFont ( SkPDFFont &&  )
default

Member Function Documentation

◆ CanEmbedTypeface()

bool SkPDFFont::CanEmbedTypeface ( SkTypeface typeface,
SkPDFDocument doc 
)
static

Return false iff the typeface has its NotEmbeddable flag set. typeface is not nullptr

Definition at line 743 of file SkPDFFont.cpp.

743 {
745 return metrics && can_embed(*metrics);
746}
static bool can_embed(const SkAdvancedTypefaceMetrics &metrics)
Definition: SkPDFFont.cpp:110
static const SkAdvancedTypefaceMetrics * GetMetrics(const SkTypeface *typeface, SkPDFDocument *canon)
Definition: SkPDFFont.cpp:118
SkTypeface * typeface() const
Definition: SkPDFFont.h:41

◆ emitSubset()

void SkPDFFont::emitSubset ( SkPDFDocument doc) const

Definition at line 726 of file SkPDFFont.cpp.

726 {
727 switch (fFontType) {
731 return emit_subset_type0(*this, doc);
732#ifndef SK_PDF_DO_NOT_SUPPORT_TYPE_1_FONTS
734 return SkPDFEmitType1Font(*this, doc);
735#endif
736 default:
737 return emit_subset_type3(*this, doc);
738 }
739}
static void emit_subset_type3(const SkPDFFont &pdfFont, SkPDFDocument *doc)
Definition: SkPDFFont.cpp:568
static void emit_subset_type0(const SkPDFFont &font, SkPDFDocument *doc)
Definition: SkPDFFont.cpp:295
void SkPDFEmitType1Font(const SkPDFFont &pdfFont, SkPDFDocument *doc)

◆ firstGlyphID()

SkGlyphID SkPDFFont::firstGlyphID ( ) const
inline

Definition at line 120 of file SkPDFFont.h.

120{ return fGlyphUsage.firstNonZero(); }
SkGlyphID firstNonZero() const
Definition: SkPDFGlyphUse.h:20

◆ FontType()

SkAdvancedTypefaceMetrics::FontType SkPDFFont::FontType ( const SkTypeface typeface,
const SkAdvancedTypefaceMetrics metrics 
)
static

Definition at line 183 of file SkPDFFont.cpp.

184 {
186 // PDF is actually interested in the encoding of the data, not just the logical format.
187 // If the TrueType is actually wOFF or wOF2 then it should not be directly embedded in PDF.
188 // For now export these as Type3 until the subsetter can handle table based fonts.
189 // See https://github.com/harfbuzz/harfbuzz/issues/3609 and
190 // https://skia-review.googlesource.com/c/skia/+/543485
193 // force Type3 fallback.
195 }
196 if (typeface.getTableSize(kCOLRTableTag)) {
197 // https://bugs.chromium.org/p/skia/issues/detail?id=12650
198 // Don't embed COLRv0 / COLRv1 fonts, fall back to bitmaps.
200 }
201 return metrics.fType;
202}
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
size_t getTableSize(SkFontTableTag) const
Definition: SkTypeface.cpp:309
@ kNotEmbeddable_FontFlag
May not be embedded.
@ kAltDataFormat_FontFlag
Data compressed. Table access may still work.
@ kVariable_FontFlag
May be true for Type1, CFF, or TrueType fonts.

◆ GetFontResource()

SkPDFFont * SkPDFFont::GetFontResource ( SkPDFDocument doc,
const SkGlyph glyphs,
SkTypeface typeface 
)
static

Get the font resource for the passed typeface and glyphID. The reference count of the object is incremented and it is the caller's responsibility to unreference it when done. This is needed to accommodate the weak reference pattern used when the returned object is new and has no other references.

Parameters
typefaceThe typeface to find, not nullptr.
glyphIDSpecify which section of a large font is of interest.

Definition at line 208 of file SkPDFFont.cpp.

210 {
211 SkASSERT(doc);
212 SkASSERT(face); // All SkPDFDevice::internalDrawText ensures this.
213 const SkAdvancedTypefaceMetrics* fontMetrics = SkPDFFont::GetMetrics(face, doc);
214 SkASSERT(fontMetrics); // SkPDFDevice::internalDrawText ensures the typeface is good.
215 // GetMetrics only returns null to signify a bad typeface.
216 const SkAdvancedTypefaceMetrics& metrics = *fontMetrics;
218 if (!(glyph->isEmpty() || glyph->path())) {
220 }
221 bool multibyte = SkPDFFont::IsMultiByte(type);
222 SkGlyphID subsetCode =
223 multibyte ? 0 : first_nonzero_glyph_for_single_byte_encoding(glyph->getGlyphID());
224 uint64_t typefaceID = (static_cast<uint64_t>(face->uniqueID()) << 16) | subsetCode;
225
226 if (SkPDFFont* found = doc->fFontMap.find(typefaceID)) {
227 SkASSERT(multibyte == found->multiByteGlyphs());
228 return found;
229 }
230
233
234 SkGlyphID lastGlyph = SkToU16(typeface->countGlyphs() - 1);
235
236 // should be caught by SkPDFDevice::internalDrawText
237 SkASSERT(glyph->getGlyphID() <= lastGlyph);
238
239 SkGlyphID firstNonZeroGlyph;
240 if (multibyte) {
241 firstNonZeroGlyph = 1;
242 } else {
243 firstNonZeroGlyph = subsetCode;
244 lastGlyph = SkToU16(std::min<int>((int)lastGlyph, 254 + (int)subsetCode));
245 }
246 auto ref = doc->reserveRef();
247 return doc->fFontMap.set(
248 typefaceID, SkPDFFont(std::move(typeface), firstNonZeroGlyph, lastGlyph, type, ref));
249}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkGlyphID first_nonzero_glyph_for_single_byte_encoding(SkGlyphID gid)
Definition: SkPDFFont.cpp:204
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
uint16_t SkGlyphID
Definition: SkTypes.h:179
GLenum type
skia_private::THashMap< uint64_t, SkPDFFont > fFontMap
SkPDFIndirectReference reserveRef()
static SkAdvancedTypefaceMetrics::FontType FontType(const SkTypeface &, const SkAdvancedTypefaceMetrics &)
Definition: SkPDFFont.cpp:183
static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type)
Definition: SkPDFFont.h:52
int countGlyphs() const
Definition: SkTypeface.cpp:432
V * find(const K &key) const
Definition: SkTHash.h:494
V * set(K key, V val)
Definition: SkTHash.h:487

◆ GetMetrics()

const SkAdvancedTypefaceMetrics * SkPDFFont::GetMetrics ( const SkTypeface typeface,
SkPDFDocument canon 
)
static

Gets SkAdvancedTypefaceMetrics, and caches the result.

Parameters
typefacecan not be nullptr.
Returns
nullptr only when typeface is bad.

Definition at line 118 of file SkPDFFont.cpp.

119 {
122 if (std::unique_ptr<SkAdvancedTypefaceMetrics>* ptr = canon->fTypefaceMetrics.find(id)) {
123 return ptr->get(); // canon retains ownership.
124 }
125 int count = typeface->countGlyphs();
126 if (count <= 0 || count > 1 + SkTo<int>(UINT16_MAX)) {
127 // Cache nullptr to skip this check. Use SkSafeUnref().
128 canon->fTypefaceMetrics.set(id, nullptr);
129 return nullptr;
130 }
131 std::unique_ptr<SkAdvancedTypefaceMetrics> metrics = typeface->getAdvancedMetrics();
132 if (!metrics) {
133 metrics = std::make_unique<SkAdvancedTypefaceMetrics>();
134 }
135
136 if (0 == metrics->fStemV || 0 == metrics->fCapHeight) {
137 SkFont font;
138 font.setHinting(SkFontHinting::kNone);
139 font.setTypeface(sk_ref_sp(typeface));
140 font.setSize(1000); // glyph coordinate system
141 if (0 == metrics->fStemV) {
142 // Figure out a good guess for StemV - Min width of i, I, !, 1.
143 // This probably isn't very good with an italic font.
144 int16_t stemV = SHRT_MAX;
145 for (char c : {'i', 'I', '!', '1'}) {
146 uint16_t g = font.unicharToGlyph(c);
148 font.getBounds(&g, 1, &bounds, nullptr);
149 stemV = std::min(stemV, SkToS16(SkScalarRoundToInt(bounds.width())));
150 }
151 metrics->fStemV = stemV;
152 }
153 if (0 == metrics->fCapHeight) {
154 // Figure out a good guess for CapHeight: average the height of M and X.
155 SkScalar capHeight = 0;
156 for (char c : {'M', 'X'}) {
157 uint16_t g = font.unicharToGlyph(c);
159 font.getBounds(&g, 1, &bounds, nullptr);
160 capHeight += bounds.height();
161 }
162 metrics->fCapHeight = SkToS16(SkScalarRoundToInt(capHeight / 2));
163 }
164 }
165 // Fonts are always subset, so always prepend the subset tag.
166 metrics->fPostScriptName.prepend(canon->nextFontSubsetTag());
167 return canon->fTypefaceMetrics.set(id, std::move(metrics))->get();
168}
int count
Definition: FontMgrTest.cpp:50
@ kNone
glyph outlines unchanged
#define SkScalarRoundToInt(x)
Definition: SkScalar.h:37
constexpr int16_t SkToS16(S x)
Definition: SkTo.h:23
uint32_t SkTypefaceID
Definition: SkTypeface.h:38
Definition: SkFont.h:35
SkString nextFontSubsetTag()
skia_private::THashMap< uint32_t, std::unique_ptr< SkAdvancedTypefaceMetrics > > fTypefaceMetrics
SkTypefaceID uniqueID() const
Definition: SkTypeface.h:101
float SkScalar
Definition: extension.cpp:12
static float min(float r, float g, float b)
Definition: hsl.cpp:48
Optional< SkRect > bounds
Definition: SkRecords.h:189
font
Font Metadata and Metrics.

◆ getType()

SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType ( ) const
inline

Returns the font type represented in this font. For Type0 fonts, returns the type of the descendant font.

Definition at line 46 of file SkPDFFont.h.

46{ return fFontType; }

◆ GetType1GlyphNames()

void SkPDFFont::GetType1GlyphNames ( const SkTypeface face,
SkString dst 
)
static

Definition at line 59 of file SkPDFFont.cpp.

59 {
61}
virtual void getPostScriptGlyphNames(SkString *) const =0
dst
Definition: cp.py:12

◆ GetUnicodeMap()

const std::vector< SkUnichar > & SkPDFFont::GetUnicodeMap ( const SkTypeface typeface,
SkPDFDocument canon 
)
static

Definition at line 170 of file SkPDFFont.cpp.

171 {
173 SkASSERT(canon);
175 if (std::vector<SkUnichar>* ptr = canon->fToUnicodeMap.find(id)) {
176 return *ptr;
177 }
178 std::vector<SkUnichar> buffer(typeface->countGlyphs());
180 return *canon->fToUnicodeMap.set(id, std::move(buffer));
181}
skia_private::THashMap< uint32_t, std::vector< SkUnichar > > fToUnicodeMap
virtual void getGlyphToUnicodeMap(SkUnichar *dstArray) const =0
Definition: SkTypeface.cpp:468
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ glyphToPDFFontEncoding()

SkGlyphID SkPDFFont::glyphToPDFFontEncoding ( SkGlyphID  gid) const
inline

Convert the input glyph ID into the font encoding.

Definition at line 69 of file SkPDFFont.h.

69 {
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 }
bool multiByteGlyphs() const
Definition: SkPDFFont.h:60
SkGlyphID lastGlyphID() const
Definition: SkPDFFont.h:121
SkGlyphID firstGlyphID() const
Definition: SkPDFFont.h:120

◆ glyphUsage()

const SkPDFGlyphUse & SkPDFFont::glyphUsage ( ) const
inline

Definition at line 122 of file SkPDFFont.h.

122{ return fGlyphUsage; }

◆ hasGlyph()

bool SkPDFFont::hasGlyph ( SkGlyphID  gid)
inline

Return true if this font has an encoding for the passed glyph id.

Definition at line 64 of file SkPDFFont.h.

64 {
65 return (gid >= this->firstGlyphID() && gid <= this->lastGlyphID()) || gid == 0;
66 }

◆ indirectReference()

SkPDFIndirectReference SkPDFFont::indirectReference ( ) const
inline

Definition at line 83 of file SkPDFFont.h.

83{ return fIndirectReference; }

◆ IsMultiByte()

static bool SkPDFFont::IsMultiByte ( SkAdvancedTypefaceMetrics::FontType  type)
inlinestatic

◆ lastGlyphID()

SkGlyphID SkPDFFont::lastGlyphID ( ) const
inline

Definition at line 121 of file SkPDFFont.h.

121{ return fGlyphUsage.lastGlyph(); }
SkGlyphID lastGlyph() const
Definition: SkPDFGlyphUse.h:21

◆ multiByteGlyphs()

bool SkPDFFont::multiByteGlyphs ( ) const
inline

Returns true if this font encoding supports glyph IDs above 255.

Definition at line 60 of file SkPDFFont.h.

60{ return SkPDFFont::IsMultiByte(this->getType()); }
SkAdvancedTypefaceMetrics::FontType getType() const
Definition: SkPDFFont.h:46

◆ noteGlyphUsage()

void SkPDFFont::noteGlyphUsage ( SkGlyphID  glyph)
inline

Definition at line 78 of file SkPDFFont.h.

78 {
79 SkASSERT(this->hasGlyph(glyph));
80 fGlyphUsage.set(glyph);
81 }
bool hasGlyph(SkGlyphID gid)
Definition: SkPDFFont.h:64
void set(SkGlyphID gid)
Definition: SkPDFGlyphUse.h:22

◆ operator=()

SkPDFFont & SkPDFFont::operator= ( SkPDFFont &&  )
default

◆ PopulateCommonFontDescriptor()

void SkPDFFont::PopulateCommonFontDescriptor ( SkPDFDict descriptor,
const SkAdvancedTypefaceMetrics metrics,
uint16_t  emSize,
int16_t  defaultWidth 
)
static

Definition at line 265 of file SkPDFFont.cpp.

268 {
269 descriptor->insertName("FontName", metrics.fPostScriptName);
270 descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic));
271 descriptor->insertScalar("Ascent",
272 scaleFromFontUnits(metrics.fAscent, emSize));
273 descriptor->insertScalar("Descent",
274 scaleFromFontUnits(metrics.fDescent, emSize));
275 descriptor->insertScalar("StemV",
276 scaleFromFontUnits(metrics.fStemV, emSize));
277 descriptor->insertScalar("CapHeight",
278 scaleFromFontUnits(metrics.fCapHeight, emSize));
279 descriptor->insertInt("ItalicAngle", metrics.fItalicAngle);
280 descriptor->insertObject("FontBBox",
281 SkPDFMakeArray(scaleFromFontUnits(metrics.fBBox.left(), emSize),
282 scaleFromFontUnits(metrics.fBBox.bottom(), emSize),
283 scaleFromFontUnits(metrics.fBBox.right(), emSize),
284 scaleFromFontUnits(metrics.fBBox.top(), emSize)));
285 if (defaultWidth > 0) {
286 descriptor->insertScalar("MissingWidth",
287 scaleFromFontUnits(defaultWidth, emSize));
288 }
289}
static std::unique_ptr< SkPDFArray > SkPDFMakeArray(Args... args)
Definition: SkPDFTypes.h:125
void insertName(const char key[], const char nameValue[])
Definition: SkPDFTypes.cpp:512
void insertObject(const char key[], std::unique_ptr< SkPDFObject > &&)
Definition: SkPDFTypes.cpp:484
void insertInt(const char key[], int32_t value)
Definition: SkPDFTypes.cpp:496
void insertScalar(const char key[], SkScalar value)
Definition: SkPDFTypes.cpp:508
constexpr int32_t top() const
Definition: SkRect.h:120
constexpr int32_t bottom() const
Definition: SkRect.h:134
constexpr int32_t right() const
Definition: SkRect.h:127
constexpr int32_t left() const
Definition: SkRect.h:113

◆ refTypeface()

sk_sp< SkTypeface > SkPDFFont::refTypeface ( ) const
inline

Definition at line 123 of file SkPDFFont.h.

123{ return fTypeface; }

◆ typeface()

SkTypeface * SkPDFFont::typeface ( ) const
inline

Returns the typeface represented by this class. Returns nullptr for the default typeface.

Definition at line 41 of file SkPDFFont.h.

41{ return fTypeface.get(); }
T * get() const
Definition: SkRefCnt.h:303

The documentation for this class was generated from the following files: