| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1 | /* |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 Google Inc. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkPDFFont_DEFINED |
| 18 | #define SkPDFFont_DEFINED |
| 19 | |
| ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 20 | #include "SkAdvancedTypefaceMetrics.h" |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 21 | #include "SkBitSet.h" |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 22 | #include "SkPDFTypes.h" |
| 23 | #include "SkTDArray.h" |
| 24 | #include "SkThread.h" |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 25 | #include "SkTypeface.h" |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 26 | |
| 27 | class SkPaint; |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 28 | class SkPDFCatalog; |
| 29 | class SkPDFFont; |
| 30 | |
| 31 | class SkPDFGlyphSet : public SkNoncopyable { |
| 32 | public: |
| 33 | SkPDFGlyphSet(); |
| 34 | |
| 35 | void set(const uint16_t* glyphIDs, int numGlyphs); |
| 36 | bool has(uint16_t glyphID) const; |
| 37 | void merge(const SkPDFGlyphSet& usage); |
| vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 38 | void exportTo(SkTDArray<uint32_t>* glyphIDs) const; |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | SkBitSet fBitSet; |
| 42 | }; |
| 43 | |
| 44 | class SkPDFGlyphSetMap : public SkNoncopyable { |
| 45 | public: |
| 46 | struct FontGlyphSetPair { |
| 47 | FontGlyphSetPair(SkPDFFont* font, SkPDFGlyphSet* glyphSet); |
| 48 | |
| 49 | SkPDFFont* fFont; |
| 50 | SkPDFGlyphSet* fGlyphSet; |
| 51 | }; |
| 52 | |
| 53 | SkPDFGlyphSetMap(); |
| 54 | ~SkPDFGlyphSetMap(); |
| 55 | |
| 56 | class F2BIter { |
| 57 | public: |
| 58 | F2BIter(const SkPDFGlyphSetMap& map); |
| 59 | FontGlyphSetPair* next() const; |
| 60 | void reset(const SkPDFGlyphSetMap& map); |
| 61 | |
| 62 | private: |
| 63 | const SkTDArray<FontGlyphSetPair>* fMap; |
| 64 | mutable int fIndex; |
| 65 | }; |
| 66 | |
| 67 | void merge(const SkPDFGlyphSetMap& usage); |
| 68 | void reset(); |
| 69 | |
| 70 | void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, |
| 71 | int numGlyphs); |
| 72 | |
| 73 | private: |
| 74 | SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font); |
| 75 | |
| 76 | SkTDArray<FontGlyphSetPair> fMap; |
| 77 | }; |
| 78 | |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 79 | |
| 80 | /** \class SkPDFFont |
| 81 | A PDF Object class representing a font. The font may have resources |
| 82 | attached to it in order to embed the font. SkPDFFonts are canonicalized |
| 83 | so that resource deduplication will only include one copy of a font. |
| 84 | This class uses the same pattern as SkPDFGraphicState, a static weak |
| 85 | reference to each instantiated class. |
| 86 | */ |
| 87 | class SkPDFFont : public SkPDFDict { |
| 88 | public: |
| vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 89 | SK_API virtual ~SkPDFFont(); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 90 | |
| vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 91 | SK_API virtual void getResources(SkTDArray<SkPDFObject*>* resourceList); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 92 | |
| ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 93 | /** Returns the typeface represented by this class. Returns NULL for the |
| 94 | * default typeface. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 95 | */ |
| vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 96 | SK_API SkTypeface* typeface(); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 97 | |
| vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 98 | /** Returns the font type represented in this font. For Type0 fonts, |
| 99 | * returns the type of the decendant font. |
| 100 | */ |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 101 | SK_API virtual SkAdvancedTypefaceMetrics::FontType getType(); |
| 102 | |
| 103 | /** Returns true if this font encoding supports glyph IDs above 255. |
| 104 | */ |
| 105 | SK_API virtual bool multiByteGlyphs() const = 0; |
| vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 106 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 107 | /** Return true if this font has an encoding for the passed glyph id. |
| 108 | */ |
| vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 109 | SK_API bool hasGlyph(uint16_t glyphID); |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 110 | |
| vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 111 | /** 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 114 | * glyph ID. |
| 115 | * @param glyphIDs The input text as glyph IDs. |
| 116 | * @param numGlyphs The number of input glyphs. |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 117 | * @return Returns the number of glyphs consumed. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 118 | */ |
| vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 119 | SK_API size_t glyphsToPDFFontEncoding(uint16_t* glyphIDs, size_t numGlyphs); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 120 | |
| ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 121 | /** Get the font resource for the passed typeface and glyphID. The |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 122 | * 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.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 126 | * @param typeface The typeface to find. |
| 127 | * @param glyphID Specify which section of a large font is of interest. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 128 | */ |
| reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 129 | SK_API static SkPDFFont* GetFontResource(SkTypeface* typeface, |
| vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 130 | uint16_t glyphID); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 131 | |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 132 | /** Subset the font based on usage set. Returns a SkPDFFont instance with |
| 133 | * subset. |
| 134 | * @param usage Glyph subset requested. |
| 135 | * @return NULL if font does not support subsetting, a new instance |
| 136 | * of SkPDFFont otherwise. |
| 137 | */ |
| 138 | SK_API virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage); |
| 139 | |
| 140 | protected: |
| 141 | // Common constructor to handle common members. |
| 142 | SkPDFFont(SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface, |
| 143 | uint16_t glyphID, bool descendantFont); |
| 144 | |
| 145 | // Accessors for subclass. |
| 146 | SkAdvancedTypefaceMetrics* fontInfo(); |
| 147 | uint16_t firstGlyphID() const; |
| 148 | uint16_t lastGlyphID() const; |
| 149 | void setLastGlyphID(uint16_t glyphID); |
| 150 | |
| 151 | // Add object to resource list. |
| 152 | void addResource(SkPDFObject* object); |
| 153 | |
| 154 | // Accessors for FontDescriptor associated with this object. |
| 155 | SkPDFDict* getFontDescriptor(); |
| 156 | void setFontDescriptor(SkPDFDict* descriptor); |
| 157 | |
| 158 | // Add common entries to FontDescriptor. |
| 159 | bool addCommonFontDescriptorEntries(int16_t defaultWidth); |
| 160 | |
| 161 | /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, |
| 162 | * including the passed glyphID. |
| 163 | */ |
| 164 | void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID); |
| 165 | |
| 166 | // Generate ToUnicode table according to glyph usage subset. |
| 167 | // If subset is NULL, all available glyph ids will be used. |
| 168 | void populateToUnicodeTable(const SkPDFGlyphSet* subset); |
| 169 | |
| 170 | // Create instances of derived types based on fontInfo. |
| 171 | static SkPDFFont* Create(SkAdvancedTypefaceMetrics* fontInfo, |
| 172 | SkTypeface* typeface, uint16_t glyphID, |
| 173 | SkPDFDict* fontDescriptor); |
| 174 | |
| 175 | static bool Find(uint32_t fontID, uint16_t glyphID, int* index); |
| 176 | |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 177 | private: |
| vandebo@chromium.org | 31dcee7 | 2011-07-23 21:13:30 +0000 | [diff] [blame] | 178 | class FontRec { |
| 179 | public: |
| 180 | SkPDFFont* fFont; |
| 181 | uint32_t fFontID; |
| 182 | uint16_t fGlyphID; |
| 183 | |
| 184 | // A fGlyphID of 0 with no fFont always matches. |
| 185 | bool operator==(const FontRec& b) const; |
| 186 | FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); |
| 187 | }; |
| vandebo@chromium.org | 6504cfd | 2011-07-23 20:22:53 +0000 | [diff] [blame] | 188 | |
| vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 189 | SkRefPtr<SkTypeface> fTypeface; |
| 190 | |
| 191 | // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
| 192 | // this will be a subset if the font has more than 255 glyphs. |
| 193 | uint16_t fFirstGlyphID; |
| 194 | uint16_t fLastGlyphID; |
| 195 | // The font info is only kept around after construction for large |
| 196 | // Type1 (non CID) fonts that need multiple "fonts" to access all glyphs. |
| 197 | SkRefPtr<SkAdvancedTypefaceMetrics> fFontInfo; |
| 198 | SkTDArray<SkPDFObject*> fResources; |
| 199 | SkRefPtr<SkPDFDict> fDescriptor; |
| 200 | |
| 201 | SkAdvancedTypefaceMetrics::FontType fFontType; |
| 202 | |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 203 | // This should be made a hash table if performance is a problem. |
| reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 204 | static SkTDArray<FontRec>& CanonicalFonts(); |
| 205 | static SkMutex& CanonicalFontsMutex(); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | #endif |