1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/pdf/SkPDFFont.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,205 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkPDFFont_DEFINED 1.14 +#define SkPDFFont_DEFINED 1.15 + 1.16 +#include "SkAdvancedTypefaceMetrics.h" 1.17 +#include "SkBitSet.h" 1.18 +#include "SkPDFTypes.h" 1.19 +#include "SkTDArray.h" 1.20 +#include "SkThread.h" 1.21 +#include "SkTypeface.h" 1.22 + 1.23 +class SkPaint; 1.24 +class SkPDFCatalog; 1.25 +class SkPDFFont; 1.26 + 1.27 +class SkPDFGlyphSet : public SkNoncopyable { 1.28 +public: 1.29 + SkPDFGlyphSet(); 1.30 + 1.31 + void set(const uint16_t* glyphIDs, int numGlyphs); 1.32 + bool has(uint16_t glyphID) const; 1.33 + void merge(const SkPDFGlyphSet& usage); 1.34 + void exportTo(SkTDArray<uint32_t>* glyphIDs) const; 1.35 + 1.36 +private: 1.37 + SkBitSet fBitSet; 1.38 +}; 1.39 + 1.40 +class SkPDFGlyphSetMap : public SkNoncopyable { 1.41 +public: 1.42 + struct FontGlyphSetPair { 1.43 + FontGlyphSetPair(SkPDFFont* font, SkPDFGlyphSet* glyphSet); 1.44 + 1.45 + SkPDFFont* fFont; 1.46 + SkPDFGlyphSet* fGlyphSet; 1.47 + }; 1.48 + 1.49 + SkPDFGlyphSetMap(); 1.50 + ~SkPDFGlyphSetMap(); 1.51 + 1.52 + class F2BIter { 1.53 + public: 1.54 + explicit F2BIter(const SkPDFGlyphSetMap& map); 1.55 + const FontGlyphSetPair* next() const; 1.56 + void reset(const SkPDFGlyphSetMap& map); 1.57 + 1.58 + private: 1.59 + const SkTDArray<FontGlyphSetPair>* fMap; 1.60 + mutable int fIndex; 1.61 + }; 1.62 + 1.63 + void merge(const SkPDFGlyphSetMap& usage); 1.64 + void reset(); 1.65 + 1.66 + void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, 1.67 + int numGlyphs); 1.68 + 1.69 +private: 1.70 + SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font); 1.71 + 1.72 + SkTDArray<FontGlyphSetPair> fMap; 1.73 +}; 1.74 + 1.75 + 1.76 +/** \class SkPDFFont 1.77 + A PDF Object class representing a font. The font may have resources 1.78 + attached to it in order to embed the font. SkPDFFonts are canonicalized 1.79 + so that resource deduplication will only include one copy of a font. 1.80 + This class uses the same pattern as SkPDFGraphicState, a static weak 1.81 + reference to each instantiated class. 1.82 +*/ 1.83 +class SkPDFFont : public SkPDFDict { 1.84 + SK_DECLARE_INST_COUNT(SkPDFFont) 1.85 +public: 1.86 + virtual ~SkPDFFont(); 1.87 + 1.88 + virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, 1.89 + SkTSet<SkPDFObject*>* newResourceObjects); 1.90 + 1.91 + /** Returns the typeface represented by this class. Returns NULL for the 1.92 + * default typeface. 1.93 + */ 1.94 + SkTypeface* typeface(); 1.95 + 1.96 + /** Returns the font type represented in this font. For Type0 fonts, 1.97 + * returns the type of the decendant font. 1.98 + */ 1.99 + virtual SkAdvancedTypefaceMetrics::FontType getType(); 1.100 + 1.101 + /** Returns true if this font encoding supports glyph IDs above 255. 1.102 + */ 1.103 + virtual bool multiByteGlyphs() const = 0; 1.104 + 1.105 + /** Return true if this font has an encoding for the passed glyph id. 1.106 + */ 1.107 + bool hasGlyph(uint16_t glyphID); 1.108 + 1.109 + /** Convert (in place) the input glyph IDs into the font encoding. If the 1.110 + * font has more glyphs than can be encoded (like a type 1 font with more 1.111 + * than 255 glyphs) this method only converts up to the first out of range 1.112 + * glyph ID. 1.113 + * @param glyphIDs The input text as glyph IDs. 1.114 + * @param numGlyphs The number of input glyphs. 1.115 + * @return Returns the number of glyphs consumed. 1.116 + */ 1.117 + size_t glyphsToPDFFontEncoding(uint16_t* glyphIDs, size_t numGlyphs); 1.118 + 1.119 + /** Get the font resource for the passed typeface and glyphID. The 1.120 + * reference count of the object is incremented and it is the caller's 1.121 + * responsibility to unreference it when done. This is needed to 1.122 + * accommodate the weak reference pattern used when the returned object 1.123 + * is new and has no other references. 1.124 + * @param typeface The typeface to find. 1.125 + * @param glyphID Specify which section of a large font is of interest. 1.126 + */ 1.127 + static SkPDFFont* GetFontResource(SkTypeface* typeface, 1.128 + uint16_t glyphID); 1.129 + 1.130 + /** Subset the font based on usage set. Returns a SkPDFFont instance with 1.131 + * subset. 1.132 + * @param usage Glyph subset requested. 1.133 + * @return NULL if font does not support subsetting, a new instance 1.134 + * of SkPDFFont otherwise. 1.135 + */ 1.136 + virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage); 1.137 + 1.138 +protected: 1.139 + // Common constructor to handle common members. 1.140 + SkPDFFont(SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface, 1.141 + SkPDFDict* relatedFontDescriptor); 1.142 + 1.143 + // Accessors for subclass. 1.144 + SkAdvancedTypefaceMetrics* fontInfo(); 1.145 + void setFontInfo(SkAdvancedTypefaceMetrics* info); 1.146 + uint16_t firstGlyphID() const; 1.147 + uint16_t lastGlyphID() const; 1.148 + void setLastGlyphID(uint16_t glyphID); 1.149 + 1.150 + // Add object to resource list. 1.151 + void addResource(SkPDFObject* object); 1.152 + 1.153 + // Accessors for FontDescriptor associated with this object. 1.154 + SkPDFDict* getFontDescriptor(); 1.155 + void setFontDescriptor(SkPDFDict* descriptor); 1.156 + 1.157 + // Add common entries to FontDescriptor. 1.158 + bool addCommonFontDescriptorEntries(int16_t defaultWidth); 1.159 + 1.160 + /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, 1.161 + * including the passed glyphID. 1.162 + */ 1.163 + void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID); 1.164 + 1.165 + // Generate ToUnicode table according to glyph usage subset. 1.166 + // If subset is NULL, all available glyph ids will be used. 1.167 + void populateToUnicodeTable(const SkPDFGlyphSet* subset); 1.168 + 1.169 + // Create instances of derived types based on fontInfo. 1.170 + static SkPDFFont* Create(SkAdvancedTypefaceMetrics* fontInfo, 1.171 + SkTypeface* typeface, uint16_t glyphID, 1.172 + SkPDFDict* relatedFontDescriptor); 1.173 + 1.174 + static bool Find(uint32_t fontID, uint16_t glyphID, int* index); 1.175 + 1.176 +private: 1.177 + class FontRec { 1.178 + public: 1.179 + SkPDFFont* fFont; 1.180 + uint32_t fFontID; 1.181 + uint16_t fGlyphID; 1.182 + 1.183 + // A fGlyphID of 0 with no fFont always matches. 1.184 + bool operator==(const FontRec& b) const; 1.185 + FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); 1.186 + }; 1.187 + 1.188 + SkAutoTUnref<SkTypeface> fTypeface; 1.189 + 1.190 + // The glyph IDs accessible with this font. For Type1 (non CID) fonts, 1.191 + // this will be a subset if the font has more than 255 glyphs. 1.192 + uint16_t fFirstGlyphID; 1.193 + uint16_t fLastGlyphID; 1.194 + // The font info is only kept around after construction for large 1.195 + // Type1 (non CID) fonts that need multiple "fonts" to access all glyphs. 1.196 + SkAutoTUnref<SkAdvancedTypefaceMetrics> fFontInfo; 1.197 + SkTDArray<SkPDFObject*> fResources; 1.198 + SkAutoTUnref<SkPDFDict> fDescriptor; 1.199 + 1.200 + SkAdvancedTypefaceMetrics::FontType fFontType; 1.201 + 1.202 + // This should be made a hash table if performance is a problem. 1.203 + static SkTDArray<FontRec>& CanonicalFonts(); 1.204 + static SkBaseMutex& CanonicalFontsMutex(); 1.205 + typedef SkPDFDict INHERITED; 1.206 +}; 1.207 + 1.208 +#endif