• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkTypeface.h"
12 #include "include/core/SkTypes.h"
13 #include "src/core/SkAdvancedTypefaceMetrics.h"
14 #include "src/core/SkStrikeCache.h"
15 #include "src/pdf/SkPDFGlyphUse.h"
16 #include "src/pdf/SkPDFTypes.h"
17 
18 #include <vector>
19 
20 class SkPDFDocument;
21 class SkStrike;
22 class SkString;
23 
24 /** \class SkPDFFont
25     A PDF Object class representing a font.  The font may have resources
26     attached to it in order to embed the font.  SkPDFFonts are canonicalized
27     so that resource deduplication will only include one copy of a font.
28     This class uses the same pattern as SkPDFGraphicState, a static weak
29     reference to each instantiated class.
30 */
31 class SkPDFFont {
32 public:
SkPDFFont()33     SkPDFFont() {}
34     ~SkPDFFont();
35     SkPDFFont(SkPDFFont&&);
36     SkPDFFont& operator=(SkPDFFont&&);
37 
38     /** Returns the typeface represented by this class. Returns nullptr for the
39      *  default typeface.
40      */
typeface()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      */
getType()46     SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; }
47 
48     static SkAdvancedTypefaceMetrics::FontType FontType(const SkAdvancedTypefaceMetrics&);
49     static void GetType1GlyphNames(const SkTypeface&, SkString*);
50 
IsMultiByte(SkAdvancedTypefaceMetrics::FontType type)51     static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) {
52         return type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
53                type == SkAdvancedTypefaceMetrics::kTrueType_Font;
54     }
55 
56     /** Returns true if this font encoding supports glyph IDs above 255.
57      */
multiByteGlyphs()58     bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType()); }
59 
60     /** Return true if this font has an encoding for the passed glyph id.
61      */
hasGlyph(SkGlyphID gid)62     bool hasGlyph(SkGlyphID gid) {
63         return (gid >= this->firstGlyphID() && gid <= this->lastGlyphID()) || gid == 0;
64     }
65 
66     /** Convert the input glyph ID into the font encoding.  */
glyphToPDFFontEncoding(SkGlyphID gid)67     SkGlyphID glyphToPDFFontEncoding(SkGlyphID gid) const {
68         if (this->multiByteGlyphs() || gid == 0) {
69             return gid;
70         }
71         SkASSERT(gid >= this->firstGlyphID() && gid <= this->lastGlyphID());
72         SkASSERT(this->firstGlyphID() > 0);
73         return gid - this->firstGlyphID() + 1;
74     }
75 
noteGlyphUsage(SkGlyphID glyph)76     void noteGlyphUsage(SkGlyphID glyph) {
77         SkASSERT(this->hasGlyph(glyph));
78         fGlyphUsage.set(glyph);
79     }
80 
indirectReference()81     SkPDFIndirectReference indirectReference() const { return fIndirectReference; }
82 
83     /** Get the font resource for the passed typeface and glyphID. The
84      *  reference count of the object is incremented and it is the caller's
85      *  responsibility to unreference it when done.  This is needed to
86      *  accommodate the weak reference pattern used when the returned object
87      *  is new and has no other references.
88      *  @param typeface  The typeface to find, not nullptr.
89      *  @param glyphID   Specify which section of a large font is of interest.
90      */
91     static SkPDFFont* GetFontResource(SkPDFDocument* doc,
92                                       const SkGlyph* glyphs,
93                                       SkTypeface* typeface);
94 
95     /** Gets SkAdvancedTypefaceMetrics, and caches the result.
96      *  @param typeface can not be nullptr.
97      *  @return nullptr only when typeface is bad.
98      */
99     static const SkAdvancedTypefaceMetrics* GetMetrics(const SkTypeface* typeface,
100                                                        SkPDFDocument* canon);
101 
102     static const std::vector<SkUnichar>& GetUnicodeMap(const SkTypeface* typeface,
103                                                        SkPDFDocument* canon);
104 
105     static void PopulateCommonFontDescriptor(SkPDFDict* descriptor,
106                                              const SkAdvancedTypefaceMetrics&,
107                                              uint16_t emSize,
108                                              int16_t defaultWidth);
109 
110     void emitSubset(SkPDFDocument*) const;
111 
112     /**
113      *  Return false iff the typeface has its NotEmbeddable flag set.
114      *  typeface is not nullptr
115      */
116     static bool CanEmbedTypeface(SkTypeface*, SkPDFDocument*);
117 
firstGlyphID()118     SkGlyphID firstGlyphID() const { return fGlyphUsage.firstNonZero(); }
lastGlyphID()119     SkGlyphID lastGlyphID() const { return fGlyphUsage.lastGlyph(); }
glyphUsage()120     const SkPDFGlyphUse& glyphUsage() const { return fGlyphUsage; }
refTypeface()121     sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
122 
123 private:
124     sk_sp<SkTypeface> fTypeface;
125     SkPDFGlyphUse fGlyphUsage;
126     SkPDFIndirectReference fIndirectReference;
127     SkAdvancedTypefaceMetrics::FontType fFontType = (SkAdvancedTypefaceMetrics::FontType)(-1);
128 
129     SkPDFFont(sk_sp<SkTypeface>,
130               SkGlyphID firstGlyphID,
131               SkGlyphID lastGlyphID,
132               SkAdvancedTypefaceMetrics::FontType fontType,
133               SkPDFIndirectReference indirectReference);
134     // The glyph IDs accessible with this font.  For Type1 (non CID) fonts,
135     // this will be a subset if the font has more than 255 glyphs.
136 
137     SkPDFFont(const SkPDFFont&) = delete;
138     SkPDFFont& operator=(const SkPDFFont&) = delete;
139 };
140 
141 #endif
142