blob: 6f8a9232c2683d62ddf9691b64251b1e37828308 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000010#ifndef SkPDFFont_DEFINED
11#define SkPDFFont_DEFINED
12
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000013#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000014#include "SkBitSet.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkPDFTypes.h"
16#include "SkTDArray.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000017#include "SkTypeface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000018
19class SkPaint;
halcanary792c80f2015-02-20 07:21:05 -080020class SkPDFCanon;
halcanary37c46ca2015-03-31 12:30:20 -070021class SkPDFObjNumMap;
vandebo@chromium.org98594282011-07-25 22:34:12 +000022class SkPDFFont;
23
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000024class SkPDFGlyphSet : SkNoncopyable {
vandebo@chromium.org98594282011-07-25 22:34:12 +000025public:
26 SkPDFGlyphSet();
27
28 void set(const uint16_t* glyphIDs, int numGlyphs);
29 bool has(uint16_t glyphID) const;
30 void merge(const SkPDFGlyphSet& usage);
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000031 void exportTo(SkTDArray<uint32_t>* glyphIDs) const;
vandebo@chromium.org98594282011-07-25 22:34:12 +000032
33private:
34 SkBitSet fBitSet;
35};
36
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000037class SkPDFGlyphSetMap : SkNoncopyable {
vandebo@chromium.org98594282011-07-25 22:34:12 +000038public:
39 struct FontGlyphSetPair {
40 FontGlyphSetPair(SkPDFFont* font, SkPDFGlyphSet* glyphSet);
41
42 SkPDFFont* fFont;
43 SkPDFGlyphSet* fGlyphSet;
44 };
45
46 SkPDFGlyphSetMap();
47 ~SkPDFGlyphSetMap();
48
49 class F2BIter {
50 public:
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000051 explicit F2BIter(const SkPDFGlyphSetMap& map);
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000052 const FontGlyphSetPair* next() const;
vandebo@chromium.org98594282011-07-25 22:34:12 +000053 void reset(const SkPDFGlyphSetMap& map);
54
55 private:
56 const SkTDArray<FontGlyphSetPair>* fMap;
57 mutable int fIndex;
58 };
59
60 void merge(const SkPDFGlyphSetMap& usage);
61 void reset();
62
63 void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
64 int numGlyphs);
65
66private:
67 SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font);
68
69 SkTDArray<FontGlyphSetPair> fMap;
70};
71
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000072
73/** \class SkPDFFont
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.
79*/
80class SkPDFFont : public SkPDFDict {
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +000081 SK_DECLARE_INST_COUNT(SkPDFFont)
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000082public:
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000083 virtual ~SkPDFFont();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000084
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000085 /** Returns the typeface represented by this class. Returns NULL for the
86 * default typeface.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000087 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000088 SkTypeface* typeface();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000089
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000090 /** Returns the font type represented in this font. For Type0 fonts,
91 * returns the type of the decendant font.
92 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000093 virtual SkAdvancedTypefaceMetrics::FontType getType();
vandebo@chromium.org98594282011-07-25 22:34:12 +000094
95 /** Returns true if this font encoding supports glyph IDs above 255.
96 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000097 virtual bool multiByteGlyphs() const = 0;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000098
vandebo0f9bad02014-06-19 11:05:39 -070099 /** Returns true if the machine readable licensing bits allow embedding.
100 */
101 bool canEmbed() const;
102
103 /** Returns true if the machine readable licensing bits allow subsetting.
104 */
105 bool canSubset() const;
106
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000107 /** Return true if this font has an encoding for the passed glyph id.
108 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +0000109 bool hasGlyph(uint16_t glyphID);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000110
vandebo@chromium.org01294102011-02-28 19:52:18 +0000111 /** Convert (in place) the input glyph IDs into the font encoding. If the
112 * font has more glyphs than can be encoded (like a type 1 font with more
113 * than 255 glyphs) this method only converts up to the first out of range
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000114 * glyph ID.
115 * @param glyphIDs The input text as glyph IDs.
116 * @param numGlyphs The number of input glyphs.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000117 * @return Returns the number of glyphs consumed.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000118 */
reed@google.comaec40662014-04-18 19:29:07 +0000119 int glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000120
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000121 /** Get the font resource for the passed typeface and glyphID. The
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000122 * reference count of the object is incremented and it is the caller's
123 * responsibility to unreference it when done. This is needed to
124 * accommodate the weak reference pattern used when the returned object
125 * is new and has no other references.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000126 * @param typeface The typeface to find.
127 * @param glyphID Specify which section of a large font is of interest.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000128 */
halcanary792c80f2015-02-20 07:21:05 -0800129 static SkPDFFont* GetFontResource(SkPDFCanon* canon,
130 SkTypeface* typeface,
131 uint16_t glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000132
vandebo@chromium.org98594282011-07-25 22:34:12 +0000133 /** Subset the font based on usage set. Returns a SkPDFFont instance with
134 * subset.
135 * @param usage Glyph subset requested.
136 * @return NULL if font does not support subsetting, a new instance
137 * of SkPDFFont otherwise.
138 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +0000139 virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000140
halcanaryfb62b3d2015-01-21 09:59:14 -0800141 enum Match {
142 kExact_Match,
143 kRelated_Match,
144 kNot_Match,
145 };
146 static Match IsMatch(SkPDFFont* existingFont,
147 uint32_t existingFontID,
148 uint16_t existingGlyphID,
149 uint32_t searchFontID,
150 uint16_t searchGlyphID);
151
vandebo@chromium.org98594282011-07-25 22:34:12 +0000152protected:
153 // Common constructor to handle common members.
halcanary2e3f9d82015-02-27 12:41:03 -0800154 SkPDFFont(const SkAdvancedTypefaceMetrics* fontInfo,
halcanary792c80f2015-02-20 07:21:05 -0800155 SkTypeface* typeface,
sugoi@google.come2e81132013-03-05 18:35:55 +0000156 SkPDFDict* relatedFontDescriptor);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000157
158 // Accessors for subclass.
halcanaryfb747e22014-07-11 19:45:23 -0700159 const SkAdvancedTypefaceMetrics* fontInfo();
160 void setFontInfo(const SkAdvancedTypefaceMetrics* info);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000161 uint16_t firstGlyphID() const;
162 uint16_t lastGlyphID() const;
163 void setLastGlyphID(uint16_t glyphID);
164
vandebo@chromium.org98594282011-07-25 22:34:12 +0000165 // Accessors for FontDescriptor associated with this object.
166 SkPDFDict* getFontDescriptor();
167 void setFontDescriptor(SkPDFDict* descriptor);
168
169 // Add common entries to FontDescriptor.
170 bool addCommonFontDescriptorEntries(int16_t defaultWidth);
171
172 /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs,
173 * including the passed glyphID.
174 */
bungeman22edc832014-10-03 07:55:58 -0700175 void adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000176
177 // Generate ToUnicode table according to glyph usage subset.
178 // If subset is NULL, all available glyph ids will be used.
179 void populateToUnicodeTable(const SkPDFGlyphSet* subset);
180
181 // Create instances of derived types based on fontInfo.
halcanary792c80f2015-02-20 07:21:05 -0800182 static SkPDFFont* Create(SkPDFCanon* canon,
183 const SkAdvancedTypefaceMetrics* fontInfo,
184 SkTypeface* typeface,
185 uint16_t glyphID,
sugoi@google.come2e81132013-03-05 18:35:55 +0000186 SkPDFDict* relatedFontDescriptor);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000187
188 static bool Find(uint32_t fontID, uint16_t glyphID, int* index);
189
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000190private:
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000191 SkAutoTUnref<SkTypeface> fTypeface;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000192
193 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
194 // this will be a subset if the font has more than 255 glyphs.
195 uint16_t fFirstGlyphID;
196 uint16_t fLastGlyphID;
halcanaryfb747e22014-07-11 19:45:23 -0700197 SkAutoTUnref<const SkAdvancedTypefaceMetrics> fFontInfo;
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000198 SkAutoTUnref<SkPDFDict> fDescriptor;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000199
200 SkAdvancedTypefaceMetrics::FontType fFontType;
201
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +0000202 typedef SkPDFDict INHERITED;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000203};
204
205#endif