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 | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 21 | #include "SkPDFTypes.h" |
| 22 | #include "SkTDArray.h" |
| 23 | #include "SkThread.h" |
| 24 | |
| 25 | class SkPaint; |
| 26 | |
| 27 | /** \class SkPDFFont |
| 28 | A PDF Object class representing a font. The font may have resources |
| 29 | attached to it in order to embed the font. SkPDFFonts are canonicalized |
| 30 | so that resource deduplication will only include one copy of a font. |
| 31 | This class uses the same pattern as SkPDFGraphicState, a static weak |
| 32 | reference to each instantiated class. |
| 33 | */ |
| 34 | class SkPDFFont : public SkPDFDict { |
| 35 | public: |
| 36 | virtual ~SkPDFFont(); |
| 37 | |
| 38 | virtual void getResources(SkTDArray<SkPDFObject*>* resourceList); |
| 39 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 40 | /** Returns the typeface represented by this class. Returns NULL for the |
| 41 | * default typeface. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 42 | */ |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 43 | SkTypeface* typeface(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 44 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 45 | /** Return true if this font has an encoding for the passed glyph id. |
| 46 | */ |
| 47 | bool hasGlyph(uint16_t glyphID); |
| 48 | |
| 49 | /** Returns true if this font encoding supports glyph IDs above 255. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 50 | */ |
| 51 | bool multiByteGlyphs(); |
| 52 | |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 53 | /** Convert (in place) the input glyph IDs into the font encoding. If the |
| 54 | * font has more glyphs than can be encoded (like a type 1 font with more |
| 55 | * 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] | 56 | * glyph ID. |
| 57 | * @param glyphIDs The input text as glyph IDs. |
| 58 | * @param numGlyphs The number of input glyphs. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 59 | * @return Returns the number of glyphs consumed. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 60 | */ |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 61 | size_t glyphsToPDFFontEncoding(uint16_t* glyphIDs, size_t numGlyphs); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 62 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 63 | /** Get the font resource for the passed typeface and glyphID. The |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 64 | * reference count of the object is incremented and it is the caller's |
| 65 | * responsibility to unreference it when done. This is needed to |
| 66 | * accommodate the weak reference pattern used when the returned object |
| 67 | * is new and has no other references. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 68 | * @param typeface The typeface to find. |
| 69 | * @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] | 70 | */ |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 71 | static SkPDFFont* getFontResource(SkTypeface* typeface, uint16_t glyphID); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 72 | |
| 73 | private: |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 74 | SkRefPtr<SkTypeface> fTypeface; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 75 | #ifdef SK_DEBUG |
| 76 | bool fDescendant; |
| 77 | #endif |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 78 | bool fMultiByteGlyphs; |
| 79 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 80 | // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
| 81 | // this will be a subset if the font has more than 255 glyphs. |
| 82 | uint16_t fFirstGlyphID; |
| 83 | uint16_t fLastGlyphID; |
| 84 | // The font info is only kept around after construction for large |
| 85 | // Type1 (non CID) fonts that need multiple "fonts" to access all glyphs. |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 86 | SkRefPtr<SkAdvancedTypefaceMetrics> fFontInfo; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 87 | SkTDArray<SkPDFObject*> fResources; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 88 | SkRefPtr<SkPDFDict> fDescriptor; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 89 | |
| 90 | class FontRec { |
| 91 | public: |
| 92 | SkPDFFont* fFont; |
| 93 | uint32_t fFontID; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 94 | uint16_t fGlyphID; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 95 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 96 | // A fGlyphID of 0 with no fFont always matches. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 97 | bool operator==(const FontRec& b) const; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 98 | FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // This should be made a hash table if performance is a problem. |
| 102 | static SkTDArray<FontRec>& canonicalFonts(); |
| 103 | static SkMutex& canonicalFontsMutex(); |
| 104 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 105 | /** Construct a new font dictionary and support objects. |
| 106 | * @param fontInfo Information about the to create. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 107 | * @param typeface The typeface for the font. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 108 | * @param glyphID The glyph ID the caller is interested in. This |
| 109 | * is important only for Type1 fonts, which have |
| 110 | * more than 255 glyphs. |
| 111 | * @param descendantFont If this is the descendant (true) or root |
| 112 | * (Type 0 font - false) font dictionary. Only True |
| 113 | * Type and CID encoded fonts will use a true value. |
| 114 | * @param fontDescriptor If the font descriptor has already have generated |
| 115 | * for this font, pass it in here, otherwise pass |
| 116 | * NULL. |
| 117 | */ |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 118 | SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface, |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 119 | uint16_t glyphID, bool descendantFont, SkPDFDict* fontDescriptor); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 120 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 121 | void populateType0Font(); |
| 122 | void populateCIDFont(); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame^] | 123 | bool populateType1Font(int16_t glyphID); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 124 | |
| 125 | /** Populate the PDF font dictionary as Type3 font which includes glyph |
| 126 | * descriptions with instructions for painting the glyphs. This function |
| 127 | * doesn't use any fields from SkAdvancedTypefaceMetrics (fFontInfo). Font |
| 128 | * information including glyph paths are queried from the platform |
| 129 | * dependent SkGlyphCache. |
| 130 | */ |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame^] | 131 | void populateType3Font(int16_t glyphID); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 132 | bool addFontDescriptor(int16_t defaultWidth); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 133 | void addWidthInfoFromRange(int16_t defaultWidth, |
| 134 | const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry); |
vandebo@chromium.org | f77e27d | 2011-03-10 22:50:28 +0000 | [diff] [blame^] | 135 | /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs, |
| 136 | * including the passed glyphID. |
| 137 | */ |
| 138 | void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 139 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 140 | static bool find(uint32_t fontID, uint16_t glyphID, int* index); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #endif |