blob: c55f6507698601b6da86ac1d1e2cd4aaf6f41d4f [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00009#ifndef SkPDFFont_DEFINED
10#define SkPDFFont_DEFINED
11
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000012#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000013#include "SkBitSet.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000014#include "SkPDFTypes.h"
15#include "SkTDArray.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000016#include "SkTypeface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000017
halcanary792c80f2015-02-20 07:21:05 -080018class SkPDFCanon;
vandebo@chromium.org98594282011-07-25 22:34:12 +000019class SkPDFFont;
20
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000021class SkPDFGlyphSet : SkNoncopyable {
vandebo@chromium.org98594282011-07-25 22:34:12 +000022public:
23 SkPDFGlyphSet();
halcanary3c35fb32016-06-30 11:55:07 -070024 SkPDFGlyphSet(SkPDFGlyphSet&& o) : fBitSet(std::move(o.fBitSet)) {}
vandebo@chromium.org98594282011-07-25 22:34:12 +000025
26 void set(const uint16_t* glyphIDs, int numGlyphs);
27 bool has(uint16_t glyphID) const;
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000028 void exportTo(SkTDArray<uint32_t>* glyphIDs) const;
vandebo@chromium.org98594282011-07-25 22:34:12 +000029
30private:
31 SkBitSet fBitSet;
32};
33
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000034class SkPDFGlyphSetMap : SkNoncopyable {
vandebo@chromium.org98594282011-07-25 22:34:12 +000035public:
halcanary3c35fb32016-06-30 11:55:07 -070036 struct FontGlyphSetPair : SkNoncopyable {
37 FontGlyphSetPair() : fFont(nullptr) {}
38 FontGlyphSetPair(FontGlyphSetPair&& o)
39 : fFont(o.fFont)
40 , fGlyphSet(std::move(o.fGlyphSet)) {
41 o.fFont = nullptr;
42 }
vandebo@chromium.org98594282011-07-25 22:34:12 +000043 SkPDFFont* fFont;
halcanary3c35fb32016-06-30 11:55:07 -070044 SkPDFGlyphSet fGlyphSet;
vandebo@chromium.org98594282011-07-25 22:34:12 +000045 };
46
47 SkPDFGlyphSetMap();
48 ~SkPDFGlyphSetMap();
49
halcanarycc77c122016-03-23 06:26:31 -070050 const FontGlyphSetPair* begin() const { return fMap.begin(); }
51 const FontGlyphSetPair* end() const { return fMap.end(); }
vandebo@chromium.org98594282011-07-25 22:34:12 +000052
vandebo@chromium.org98594282011-07-25 22:34:12 +000053 void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
54 int numGlyphs);
55
56private:
57 SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font);
58
halcanary3c35fb32016-06-30 11:55:07 -070059 SkTArray<FontGlyphSetPair> fMap;
vandebo@chromium.org98594282011-07-25 22:34:12 +000060};
61
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000062
63/** \class SkPDFFont
64 A PDF Object class representing a font. The font may have resources
65 attached to it in order to embed the font. SkPDFFonts are canonicalized
66 so that resource deduplication will only include one copy of a font.
67 This class uses the same pattern as SkPDFGraphicState, a static weak
68 reference to each instantiated class.
69*/
70class SkPDFFont : public SkPDFDict {
halcanary9d524f22016-03-29 09:03:52 -070071
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000072public:
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000073 virtual ~SkPDFFont();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000074
halcanary96fcdcc2015-08-27 07:41:13 -070075 /** Returns the typeface represented by this class. Returns nullptr for the
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000076 * default typeface.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000077 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000078 SkTypeface* typeface();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000079
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000080 /** Returns the font type represented in this font. For Type0 fonts,
81 * returns the type of the decendant font.
82 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000083 virtual SkAdvancedTypefaceMetrics::FontType getType();
vandebo@chromium.org98594282011-07-25 22:34:12 +000084
85 /** Returns true if this font encoding supports glyph IDs above 255.
86 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000087 virtual bool multiByteGlyphs() const = 0;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000088
vandebo0f9bad02014-06-19 11:05:39 -070089 /** Returns true if the machine readable licensing bits allow embedding.
90 */
91 bool canEmbed() const;
92
93 /** Returns true if the machine readable licensing bits allow subsetting.
94 */
95 bool canSubset() const;
96
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000097 /** Return true if this font has an encoding for the passed glyph id.
98 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000099 bool hasGlyph(uint16_t glyphID);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000100
vandebo@chromium.org01294102011-02-28 19:52:18 +0000101 /** Convert (in place) the input glyph IDs into the font encoding. If the
102 * font has more glyphs than can be encoded (like a type 1 font with more
103 * than 255 glyphs) this method only converts up to the first out of range
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000104 * glyph ID.
105 * @param glyphIDs The input text as glyph IDs.
106 * @param numGlyphs The number of input glyphs.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000107 * @return Returns the number of glyphs consumed.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000108 */
reed@google.comaec40662014-04-18 19:29:07 +0000109 int glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000110
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000111 /** Get the font resource for the passed typeface and glyphID. The
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000112 * reference count of the object is incremented and it is the caller's
113 * responsibility to unreference it when done. This is needed to
114 * accommodate the weak reference pattern used when the returned object
115 * is new and has no other references.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000116 * @param typeface The typeface to find.
117 * @param glyphID Specify which section of a large font is of interest.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000118 */
halcanary792c80f2015-02-20 07:21:05 -0800119 static SkPDFFont* GetFontResource(SkPDFCanon* canon,
120 SkTypeface* typeface,
121 uint16_t glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000122
halcanary8eccc302016-08-09 13:04:34 -0700123 static sk_sp<const SkAdvancedTypefaceMetrics> GetFontMetricsWithGlyphNames(
124 SkTypeface*, uint32_t* glyphs, uint32_t glyphsCount);
125
126 static sk_sp<const SkAdvancedTypefaceMetrics> GetFontMetricsWithToUnicode(
127 SkTypeface*, uint32_t* glyphs, uint32_t glyphsCount);
128
vandebo@chromium.org98594282011-07-25 22:34:12 +0000129 /** Subset the font based on usage set. Returns a SkPDFFont instance with
130 * subset.
131 * @param usage Glyph subset requested.
halcanary96fcdcc2015-08-27 07:41:13 -0700132 * @return nullptr if font does not support subsetting, a new instance
vandebo@chromium.org98594282011-07-25 22:34:12 +0000133 * of SkPDFFont otherwise.
134 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +0000135 virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000136
halcanaryfb62b3d2015-01-21 09:59:14 -0800137 enum Match {
138 kExact_Match,
139 kRelated_Match,
140 kNot_Match,
141 };
142 static Match IsMatch(SkPDFFont* existingFont,
143 uint32_t existingFontID,
144 uint16_t existingGlyphID,
145 uint32_t searchFontID,
146 uint16_t searchGlyphID);
147
halcanary66a82f32015-10-12 13:05:04 -0700148 /**
149 * Return false iff the typeface has its NotEmbeddable flag set.
150 * If typeface is NULL, the default typeface is checked.
151 */
152 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*);
153
vandebo@chromium.org98594282011-07-25 22:34:12 +0000154protected:
155 // Common constructor to handle common members.
halcanary2e3f9d82015-02-27 12:41:03 -0800156 SkPDFFont(const SkAdvancedTypefaceMetrics* fontInfo,
halcanary792c80f2015-02-20 07:21:05 -0800157 SkTypeface* typeface,
sugoi@google.come2e81132013-03-05 18:35:55 +0000158 SkPDFDict* relatedFontDescriptor);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000159
160 // Accessors for subclass.
halcanaryfb747e22014-07-11 19:45:23 -0700161 const SkAdvancedTypefaceMetrics* fontInfo();
162 void setFontInfo(const SkAdvancedTypefaceMetrics* info);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000163 uint16_t firstGlyphID() const;
164 uint16_t lastGlyphID() const;
165 void setLastGlyphID(uint16_t glyphID);
166
vandebo@chromium.org98594282011-07-25 22:34:12 +0000167 // Accessors for FontDescriptor associated with this object.
168 SkPDFDict* getFontDescriptor();
169 void setFontDescriptor(SkPDFDict* descriptor);
170
171 // Add common entries to FontDescriptor.
172 bool addCommonFontDescriptorEntries(int16_t defaultWidth);
173
174 /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs,
175 * including the passed glyphID.
176 */
bungeman22edc832014-10-03 07:55:58 -0700177 void adjustGlyphRangeForSingleByteEncoding(uint16_t glyphID);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000178
179 // Generate ToUnicode table according to glyph usage subset.
halcanary96fcdcc2015-08-27 07:41:13 -0700180 // If subset is nullptr, all available glyph ids will be used.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000181 void populateToUnicodeTable(const SkPDFGlyphSet* subset);
182
183 // Create instances of derived types based on fontInfo.
halcanary792c80f2015-02-20 07:21:05 -0800184 static SkPDFFont* Create(SkPDFCanon* canon,
185 const SkAdvancedTypefaceMetrics* fontInfo,
186 SkTypeface* typeface,
187 uint16_t glyphID,
sugoi@google.come2e81132013-03-05 18:35:55 +0000188 SkPDFDict* relatedFontDescriptor);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000189
190 static bool Find(uint32_t fontID, uint16_t glyphID, int* index);
191
halcanarybae235e2016-03-21 10:05:23 -0700192 void drop() override;
193
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000194private:
halcanary48810a02016-03-07 14:57:50 -0800195 sk_sp<SkTypeface> fTypeface;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000196
197 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
198 // this will be a subset if the font has more than 255 glyphs.
199 uint16_t fFirstGlyphID;
200 uint16_t fLastGlyphID;
halcanary48810a02016-03-07 14:57:50 -0800201 sk_sp<const SkAdvancedTypefaceMetrics> fFontInfo;
202 sk_sp<SkPDFDict> fDescriptor;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000203
204 SkAdvancedTypefaceMetrics::FontType fFontType;
205
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +0000206 typedef SkPDFDict INHERITED;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000207};
208
209#endif