| 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 | |
| 20 | #include "SkPDFTypes.h" |
| 21 | #include "SkTDArray.h" |
| 22 | #include "SkThread.h" |
| 23 | |
| 24 | class SkPaint; |
| vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 25 | class SkAdvancedTypefaceMetrics; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 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 | |
| 40 | /* Returns the font ID represented by this class. |
| 41 | */ |
| 42 | uint32_t fontID(); |
| 43 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 44 | /** Return true if this font has an encoding for the passed glyph id. |
| 45 | */ |
| 46 | bool hasGlyph(uint16_t glyphID); |
| 47 | |
| 48 | /** Returns true if this font encoding supports glyph IDs above 255. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 49 | */ |
| 50 | bool multiByteGlyphs(); |
| 51 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 52 | /** Convert the input glyph IDs into the font encoding. If the font has |
| 53 | * more glyphs than can be encoded (like a type 1 font with more than |
| 54 | * 255 glyphs) this method only converts up to the first out of range |
| 55 | * glyph ID. |
| 56 | * @param glyphIDs The input text as glyph IDs. |
| 57 | * @param numGlyphs The number of input glyphs. |
| 58 | * @param encodedValues The method writes its result to this parameter. |
| 59 | * multiByteGlyphs() reveals the output format. |
| 60 | * @param encodedLength The size of encodedValues (in bytes), which is |
| 61 | * overwritten with how much of encodedValues is |
| 62 | * used. |
| 63 | * @return Returns the number of glyphs consumed. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 64 | */ |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 65 | size_t glyphsToPDFFontEncoding(const uint16_t* glyphIDs, size_t numGlyphs, |
| 66 | void* encodedValues, size_t* encodedLength); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 67 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 68 | /** Get the font resource for the passed font ID and glyphID. The |
| 69 | * reference count of the object is incremented and it is the caller's |
| 70 | * responsibility to unreference it when done. This is needed to |
| 71 | * accommodate the weak reference pattern used when the returned object |
| 72 | * is new and has no other references. |
| 73 | * @param fontID The fontId to find. |
| 74 | * @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] | 75 | */ |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 76 | static SkPDFFont* getFontResource(uint32_t fontID, uint16_t glyphID); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | uint32_t fFontID; |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 80 | #ifdef SK_DEBUG |
| 81 | bool fDescendant; |
| 82 | #endif |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 83 | bool fMultiByteGlyphs; |
| 84 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 85 | // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
| 86 | // this will be a subset if the font has more than 255 glyphs. |
| 87 | uint16_t fFirstGlyphID; |
| 88 | uint16_t fLastGlyphID; |
| 89 | // The font info is only kept around after construction for large |
| 90 | // 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] | 91 | SkRefPtr<SkAdvancedTypefaceMetrics> fFontInfo; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 92 | SkTDArray<SkPDFObject*> fResources; |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 93 | SkRefPtr<SkPDFDict> fDescriptor; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 94 | |
| 95 | class FontRec { |
| 96 | public: |
| 97 | SkPDFFont* fFont; |
| 98 | uint32_t fFontID; |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 99 | uint16_t fGlyphID; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 100 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 101 | // A fGlyphID of 0 with no fFont always matches. |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 102 | bool operator==(const FontRec& b) const; |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 103 | FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | // This should be made a hash table if performance is a problem. |
| 107 | static SkTDArray<FontRec>& canonicalFonts(); |
| 108 | static SkMutex& canonicalFontsMutex(); |
| 109 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 110 | /** Construct a new font dictionary and support objects. |
| 111 | * @param fontInfo Information about the to create. |
| 112 | * @param fontID The font ID of the font. |
| 113 | * @param glyphID The glyph ID the caller is interested in. This |
| 114 | * is important only for Type1 fonts, which have |
| 115 | * more than 255 glyphs. |
| 116 | * @param descendantFont If this is the descendant (true) or root |
| 117 | * (Type 0 font - false) font dictionary. Only True |
| 118 | * Type and CID encoded fonts will use a true value. |
| 119 | * @param fontDescriptor If the font descriptor has already have generated |
| 120 | * for this font, pass it in here, otherwise pass |
| 121 | * NULL. |
| 122 | */ |
| vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 123 | SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo, uint32_t fontID, |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 124 | uint16_t glyphID, bool descendantFont, SkPDFDict* fontDescriptor); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 125 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 126 | void populateType0Font(); |
| 127 | void populateCIDFont(); |
| 128 | bool populateType1Font(uint16_t firstGlyphID, uint16_t lastGlyphID); |
| 129 | void populateType3Font(); |
| vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 130 | bool addFontDescriptor(int16_t defaultWidth); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 131 | |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 132 | static bool find(uint32_t fontID, uint16_t glyphID, int* index); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif |