| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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; |
| 25 | |
| 26 | /** \class SkPDFFont |
| 27 | A PDF Object class representing a font. The font may have resources |
| 28 | attached to it in order to embed the font. SkPDFFonts are canonicalized |
| 29 | so that resource deduplication will only include one copy of a font. |
| 30 | This class uses the same pattern as SkPDFGraphicState, a static weak |
| 31 | reference to each instantiated class. |
| 32 | */ |
| 33 | class SkPDFFont : public SkPDFDict { |
| 34 | public: |
| 35 | virtual ~SkPDFFont(); |
| 36 | |
| 37 | virtual void getResources(SkTDArray<SkPDFObject*>* resourceList); |
| 38 | |
| 39 | /* Returns the font ID represented by this class. |
| 40 | */ |
| 41 | uint32_t fontID(); |
| 42 | |
| 43 | /** Returns true if this font supports glyph IDs above 255. |
| 44 | */ |
| 45 | bool multiByteGlyphs(); |
| 46 | |
| 47 | /** Covert the specified text to glyph IDs, taking into consideration |
| 48 | * PDF encodings and return the number of glyphs IDs written. |
| 49 | */ |
| 50 | int textToPDFGlyphs(const void* text, size_t byteLength, |
| 51 | const SkPaint& paint, uint16_t* glyphs, |
| 52 | size_t glyphsLength) const; |
| 53 | |
| 54 | /** Get the font resource for the passed font ID. The reference count of |
| 55 | * the object is incremented and it is the caller's responsibility to |
| 56 | * unreference it when done. This is needed to accommodate the weak |
| 57 | * reference pattern used when the returned object is new and has no |
| 58 | * other references. |
| 59 | * @param paint The SkPaint to emulate. |
| 60 | */ |
| 61 | static SkPDFFont* getFontResouceByID(uint32_t fontID); |
| 62 | |
| 63 | private: |
| 64 | uint32_t fFontID; |
| 65 | bool fMultiByteGlyphs; |
| 66 | |
| 67 | SkTDArray<SkPDFObject*> fResources; |
| 68 | |
| 69 | class FontRec { |
| 70 | public: |
| 71 | SkPDFFont* fFont; |
| 72 | uint32_t fFontID; |
| 73 | |
| 74 | bool operator==(const FontRec& b) const; |
| 75 | FontRec(SkPDFFont* font, uint32_t fontID); |
| 76 | }; |
| 77 | |
| 78 | // This should be made a hash table if performance is a problem. |
| 79 | static SkTDArray<FontRec>& canonicalFonts(); |
| 80 | static SkMutex& canonicalFontsMutex(); |
| 81 | |
| 82 | SkPDFFont(uint32_t fontID, bool multiByteGlyphs); |
| 83 | |
| 84 | void populateBuiltinFont(const char fontName[]); |
| 85 | void populateFont(const char subType[], const char fontName[], |
| 86 | int firstChar, int lastChar, int widths[], |
| 87 | SkPDFObject* fontDescriptor); |
| 88 | |
| 89 | static int find(uint32_t fontID); |
| 90 | }; |
| 91 | |
| 92 | #endif |