diff options
Diffstat (limited to 'include/pdf/SkPDFFont.h')
-rw-r--r-- | include/pdf/SkPDFFont.h | 197 |
1 files changed, 124 insertions, 73 deletions
diff --git a/include/pdf/SkPDFFont.h b/include/pdf/SkPDFFont.h index 93a72f1..b884017 100644 --- a/include/pdf/SkPDFFont.h +++ b/include/pdf/SkPDFFont.h @@ -1,28 +1,74 @@ + /* - * Copyright (C) 2011 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 + * Copyright 2011 Google Inc. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. */ + #ifndef SkPDFFont_DEFINED #define SkPDFFont_DEFINED #include "SkAdvancedTypefaceMetrics.h" +#include "SkBitSet.h" #include "SkPDFTypes.h" #include "SkTDArray.h" #include "SkThread.h" +#include "SkTypeface.h" class SkPaint; +class SkPDFCatalog; +class SkPDFFont; + +class SkPDFGlyphSet : public SkNoncopyable { +public: + SkPDFGlyphSet(); + + void set(const uint16_t* glyphIDs, int numGlyphs); + bool has(uint16_t glyphID) const; + void merge(const SkPDFGlyphSet& usage); + void exportTo(SkTDArray<uint32_t>* glyphIDs) const; + +private: + SkBitSet fBitSet; +}; + +class SkPDFGlyphSetMap : public SkNoncopyable { +public: + struct FontGlyphSetPair { + FontGlyphSetPair(SkPDFFont* font, SkPDFGlyphSet* glyphSet); + + SkPDFFont* fFont; + SkPDFGlyphSet* fGlyphSet; + }; + + SkPDFGlyphSetMap(); + ~SkPDFGlyphSetMap(); + + class F2BIter { + public: + explicit F2BIter(const SkPDFGlyphSetMap& map); + FontGlyphSetPair* next() const; + void reset(const SkPDFGlyphSetMap& map); + + private: + const SkTDArray<FontGlyphSetPair>* fMap; + mutable int fIndex; + }; + + void merge(const SkPDFGlyphSetMap& usage); + void reset(); + + void noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs, + int numGlyphs); + +private: + SkPDFGlyphSet* getGlyphSetForFont(SkPDFFont* font); + + SkTDArray<FontGlyphSetPair> fMap; +}; + /** \class SkPDFFont A PDF Object class representing a font. The font may have resources @@ -45,15 +91,15 @@ public: /** Returns the font type represented in this font. For Type0 fonts, * returns the type of the decendant font. */ - SK_API SkAdvancedTypefaceMetrics::FontType getType(); + SK_API virtual SkAdvancedTypefaceMetrics::FontType getType(); - /** Return true if this font has an encoding for the passed glyph id. + /** Returns true if this font encoding supports glyph IDs above 255. */ - SK_API bool hasGlyph(uint16_t glyphID); + SK_API virtual bool multiByteGlyphs() const = 0; - /** Returns true if this font encoding supports glyph IDs above 255. + /** Return true if this font has an encoding for the passed glyph id. */ - SK_API bool multiByteGlyphs(); + SK_API bool hasGlyph(uint16_t glyphID); /** Convert (in place) the input glyph IDs into the font encoding. If the * font has more glyphs than can be encoded (like a type 1 font with more @@ -73,16 +119,68 @@ public: * @param typeface The typeface to find. * @param glyphID Specify which section of a large font is of interest. */ - SK_API static SkPDFFont* getFontResource(SkTypeface* typeface, + SK_API static SkPDFFont* GetFontResource(SkTypeface* typeface, uint16_t glyphID); + /** Subset the font based on usage set. Returns a SkPDFFont instance with + * subset. + * @param usage Glyph subset requested. + * @return NULL if font does not support subsetting, a new instance + * of SkPDFFont otherwise. + */ + SK_API virtual SkPDFFont* getFontSubset(const SkPDFGlyphSet* usage); + +protected: + // Common constructor to handle common members. + SkPDFFont(SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface, + uint16_t glyphID, bool descendantFont); + + // Accessors for subclass. + SkAdvancedTypefaceMetrics* fontInfo(); + void setFontInfo(SkAdvancedTypefaceMetrics* info); + uint16_t firstGlyphID() const; + uint16_t lastGlyphID() const; + void setLastGlyphID(uint16_t glyphID); + + // Add object to resource list. + void addResource(SkPDFObject* object); + + // Accessors for FontDescriptor associated with this object. + SkPDFDict* getFontDescriptor(); + void setFontDescriptor(SkPDFDict* descriptor); + + // Add common entries to FontDescriptor. + bool addCommonFontDescriptorEntries(int16_t defaultWidth); + + /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, + * including the passed glyphID. + */ + void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID); + + // Generate ToUnicode table according to glyph usage subset. + // If subset is NULL, all available glyph ids will be used. + void populateToUnicodeTable(const SkPDFGlyphSet* subset); + + // Create instances of derived types based on fontInfo. + static SkPDFFont* Create(SkAdvancedTypefaceMetrics* fontInfo, + SkTypeface* typeface, uint16_t glyphID, + SkPDFDict* fontDescriptor); + + static bool Find(uint32_t fontID, uint16_t glyphID, int* index); + private: + class FontRec { + public: + SkPDFFont* fFont; + uint32_t fFontID; + uint16_t fGlyphID; + + // A fGlyphID of 0 with no fFont always matches. + bool operator==(const FontRec& b) const; + FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); + }; + SkRefPtr<SkTypeface> fTypeface; - SkAdvancedTypefaceMetrics::FontType fType; -#ifdef SK_DEBUG - bool fDescendant; -#endif - bool fMultiByteGlyphs; // The glyph IDs accessible with this font. For Type1 (non CID) fonts, // this will be a subset if the font has more than 255 glyphs. @@ -94,58 +192,11 @@ private: SkTDArray<SkPDFObject*> fResources; SkRefPtr<SkPDFDict> fDescriptor; - class FontRec { - public: - SkPDFFont* fFont; - uint32_t fFontID; - uint16_t fGlyphID; - - // A fGlyphID of 0 with no fFont always matches. - bool operator==(const FontRec& b) const; - FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); - }; + SkAdvancedTypefaceMetrics::FontType fFontType; // This should be made a hash table if performance is a problem. - static SkTDArray<FontRec>& canonicalFonts(); - static SkMutex& canonicalFontsMutex(); - - /** Construct a new font dictionary and support objects. - * @param fontInfo Information about the to create. - * @param typeface The typeface for the font. - * @param glyphID The glyph ID the caller is interested in. This - * is important only for Type1 fonts, which have - * more than 255 glyphs. - * @param descendantFont If this is the descendant (true) or root - * (Type 0 font - false) font dictionary. Only True - * Type and CID encoded fonts will use a true value. - * @param fontDescriptor If the font descriptor has already have generated - * for this font, pass it in here, otherwise pass - * NULL. - */ - SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface, - uint16_t glyphID, bool descendantFont, SkPDFDict* fontDescriptor); - - void populateType0Font(); - void populateCIDFont(); - bool populateType1Font(int16_t glyphID); - - /** Populate the PDF font dictionary as Type3 font which includes glyph - * descriptions with instructions for painting the glyphs. This function - * doesn't use any fields from SkAdvancedTypefaceMetrics (fFontInfo). Font - * information including glyph paths are queried from the platform - * dependent SkGlyphCache. - */ - void populateType3Font(int16_t glyphID); - bool addFontDescriptor(int16_t defaultWidth); - void populateToUnicodeTable(); - void addWidthInfoFromRange(int16_t defaultWidth, - const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry); - /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, - * including the passed glyphID. - */ - void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID); - - static bool find(uint32_t fontID, uint16_t glyphID, int* index); + static SkTDArray<FontRec>& CanonicalFonts(); + static SkMutex& CanonicalFontsMutex(); }; #endif |