3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkPDFFont_DEFINED
11 #define SkPDFFont_DEFINED
13 #include "SkAdvancedTypefaceMetrics.h"
15 #include "SkPDFTypes.h"
16 #include "SkTDArray.h"
18 #include "SkTypeface.h"
24 class SkPDFGlyphSet : SkNoncopyable {
28 void set(const uint16_t* glyphIDs, int numGlyphs);
29 bool has(uint16_t glyphID) const;
30 void merge(const SkPDFGlyphSet& usage);
31 void exportTo(SkTDArray<uint32_t>* glyphIDs) const;
37 class SkPDFGlyphSetMap : SkNoncopyable {
39 struct FontGlyphSetPair {
40 FontGlyphSetPair(SkPDFFont* font, SkPDFGlyphSet* glyphSet);
43 SkPDFGlyphSet* fGlyphSet;
51 explicit F2BIter(const SkPDFGlyphSetMap& map);
52 const FontGlyphSetPair* next() const;
53 void reset(const SkPDFGlyphSetMap& map);
56 const SkTDArray<FontGlyphSetPair>* fMap;
60 void merge(const SkPDFGlyphSetMap& usage);
63 void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
67 SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font);
69 SkTDArray<FontGlyphSetPair> fMap;
74 A PDF Object class representing a font. The font may have resources
75 attached to it in order to embed the font. SkPDFFonts are canonicalized
76 so that resource deduplication will only include one copy of a font.
77 This class uses the same pattern as SkPDFGraphicState, a static weak
78 reference to each instantiated class.
80 class SkPDFFont : public SkPDFDict {
81 SK_DECLARE_INST_COUNT(SkPDFFont)
85 virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
86 SkTSet<SkPDFObject*>* newResourceObjects);
88 /** Returns the typeface represented by this class. Returns NULL for the
91 SkTypeface* typeface();
93 /** Returns the font type represented in this font. For Type0 fonts,
94 * returns the type of the decendant font.
96 virtual SkAdvancedTypefaceMetrics::FontType getType();
98 /** Returns true if this font encoding supports glyph IDs above 255.
100 virtual bool multiByteGlyphs() const = 0;
102 /** Returns true if the machine readable licensing bits allow embedding.
104 bool canEmbed() const;
106 /** Returns true if the machine readable licensing bits allow subsetting.
108 bool canSubset() const;
110 /** Return true if this font has an encoding for the passed glyph id.
112 bool hasGlyph(uint16_t glyphID);
114 /** Convert (in place) the input glyph IDs into the font encoding. If the
115 * font has more glyphs than can be encoded (like a type 1 font with more
116 * than 255 glyphs) this method only converts up to the first out of range
118 * @param glyphIDs The input text as glyph IDs.
119 * @param numGlyphs The number of input glyphs.
120 * @return Returns the number of glyphs consumed.
122 int glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs);
124 /** Get the font resource for the passed typeface and glyphID. The
125 * reference count of the object is incremented and it is the caller's
126 * responsibility to unreference it when done. This is needed to
127 * accommodate the weak reference pattern used when the returned object
128 * is new and has no other references.
129 * @param typeface The typeface to find.
130 * @param glyphID Specify which section of a large font is of interest.
132 static SkPDFFont* GetFontResource(SkTypeface* typeface, uint16_t glyphID);
134 /** Subset the font based on usage set. Returns a SkPDFFont instance with
136 * @param usage Glyph subset requested.
137 * @return NULL if font does not support subsetting, a new instance
138 * of SkPDFFont otherwise.
140 virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage);
143 // Common constructor to handle common members.
144 SkPDFFont(const SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface,
145 SkPDFDict* relatedFontDescriptor);
147 // Accessors for subclass.
148 const SkAdvancedTypefaceMetrics* fontInfo();
149 void setFontInfo(const SkAdvancedTypefaceMetrics* info);
150 uint16_t firstGlyphID() const;
151 uint16_t lastGlyphID() const;
152 void setLastGlyphID(uint16_t glyphID);
154 // Add object to resource list.
155 void addResource(SkPDFObject* object);
157 // Accessors for FontDescriptor associated with this object.
158 SkPDFDict* getFontDescriptor();
159 void setFontDescriptor(SkPDFDict* descriptor);
161 // Add common entries to FontDescriptor.
162 bool addCommonFontDescriptorEntries(int16_t defaultWidth);
164 /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs,
165 * including the passed glyphID.
167 void adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID);
169 // Generate ToUnicode table according to glyph usage subset.
170 // If subset is NULL, all available glyph ids will be used.
171 void populateToUnicodeTable(const SkPDFGlyphSet* subset);
173 // Create instances of derived types based on fontInfo.
174 static SkPDFFont* Create(const SkAdvancedTypefaceMetrics* fontInfo,
175 SkTypeface* typeface, uint16_t glyphID,
176 SkPDFDict* relatedFontDescriptor);
178 static bool Find(uint32_t fontID, uint16_t glyphID, int* index);
187 // A fGlyphID of 0 with no fFont always matches.
188 bool operator==(const FontRec& b) const;
189 FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID);
192 SkAutoTUnref<SkTypeface> fTypeface;
194 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
195 // this will be a subset if the font has more than 255 glyphs.
196 uint16_t fFirstGlyphID;
197 uint16_t fLastGlyphID;
198 SkAutoTUnref<const SkAdvancedTypefaceMetrics> fFontInfo;
199 SkTDArray<SkPDFObject*> fResources;
200 SkAutoTUnref<SkPDFDict> fDescriptor;
202 SkAdvancedTypefaceMetrics::FontType fFontType;
204 // This should be made a hash table if performance is a problem.
205 static SkTDArray<FontRec>& CanonicalFonts();
206 static SkBaseMutex& CanonicalFontsMutex();
207 typedef SkPDFDict INHERITED;