blob: 93a72f173b572d10c29ac7c5be31951a065675de [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00003 *
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.org9db86bb2011-03-04 21:43:27 +000020#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000021#include "SkPDFTypes.h"
22#include "SkTDArray.h"
23#include "SkThread.h"
24
25class 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*/
34class SkPDFFont : public SkPDFDict {
35public:
vandebo@chromium.org3509f052011-05-30 20:52:33 +000036 SK_API virtual ~SkPDFFont();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000037
vandebo@chromium.org3509f052011-05-30 20:52:33 +000038 SK_API virtual void getResources(SkTDArray<SkPDFObject*>* resourceList);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000039
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000040 /** Returns the typeface represented by this class. Returns NULL for the
41 * default typeface.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000042 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +000043 SK_API SkTypeface* typeface();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000044
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000045 /** Returns the font type represented in this font. For Type0 fonts,
46 * returns the type of the decendant font.
47 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +000048 SK_API SkAdvancedTypefaceMetrics::FontType getType();
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000049
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000050 /** Return true if this font has an encoding for the passed glyph id.
51 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +000052 SK_API bool hasGlyph(uint16_t glyphID);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000053
54 /** Returns true if this font encoding supports glyph IDs above 255.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000055 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +000056 SK_API bool multiByteGlyphs();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000057
vandebo@chromium.org01294102011-02-28 19:52:18 +000058 /** Convert (in place) the input glyph IDs into the font encoding. If the
59 * font has more glyphs than can be encoded (like a type 1 font with more
60 * than 255 glyphs) this method only converts up to the first out of range
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000061 * glyph ID.
62 * @param glyphIDs The input text as glyph IDs.
63 * @param numGlyphs The number of input glyphs.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000064 * @return Returns the number of glyphs consumed.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000065 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +000066 SK_API size_t glyphsToPDFFontEncoding(uint16_t* glyphIDs, size_t numGlyphs);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000067
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000068 /** Get the font resource for the passed typeface and glyphID. The
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000069 * 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.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000073 * @param typeface The typeface to find.
74 * @param glyphID Specify which section of a large font is of interest.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000075 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +000076 SK_API static SkPDFFont* getFontResource(SkTypeface* typeface,
77 uint16_t glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000078
79private:
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000080 SkRefPtr<SkTypeface> fTypeface;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000081 SkAdvancedTypefaceMetrics::FontType fType;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000082#ifdef SK_DEBUG
83 bool fDescendant;
84#endif
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000085 bool fMultiByteGlyphs;
86
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000087 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
88 // this will be a subset if the font has more than 255 glyphs.
89 uint16_t fFirstGlyphID;
90 uint16_t fLastGlyphID;
91 // The font info is only kept around after construction for large
92 // Type1 (non CID) fonts that need multiple "fonts" to access all glyphs.
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000093 SkRefPtr<SkAdvancedTypefaceMetrics> fFontInfo;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000094 SkTDArray<SkPDFObject*> fResources;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000095 SkRefPtr<SkPDFDict> fDescriptor;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000096
97 class FontRec {
98 public:
99 SkPDFFont* fFont;
100 uint32_t fFontID;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000101 uint16_t fGlyphID;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000102
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000103 // A fGlyphID of 0 with no fFont always matches.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000104 bool operator==(const FontRec& b) const;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000105 FontRec(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000106 };
107
108 // This should be made a hash table if performance is a problem.
109 static SkTDArray<FontRec>& canonicalFonts();
110 static SkMutex& canonicalFontsMutex();
111
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000112 /** Construct a new font dictionary and support objects.
113 * @param fontInfo Information about the to create.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000114 * @param typeface The typeface for the font.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000115 * @param glyphID The glyph ID the caller is interested in. This
116 * is important only for Type1 fonts, which have
117 * more than 255 glyphs.
118 * @param descendantFont If this is the descendant (true) or root
119 * (Type 0 font - false) font dictionary. Only True
120 * Type and CID encoded fonts will use a true value.
121 * @param fontDescriptor If the font descriptor has already have generated
122 * for this font, pass it in here, otherwise pass
123 * NULL.
124 */
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000125 SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo, SkTypeface* typeface,
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000126 uint16_t glyphID, bool descendantFont, SkPDFDict* fontDescriptor);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000127
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000128 void populateType0Font();
129 void populateCIDFont();
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000130 bool populateType1Font(int16_t glyphID);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000131
132 /** Populate the PDF font dictionary as Type3 font which includes glyph
133 * descriptions with instructions for painting the glyphs. This function
134 * doesn't use any fields from SkAdvancedTypefaceMetrics (fFontInfo). Font
135 * information including glyph paths are queried from the platform
136 * dependent SkGlyphCache.
137 */
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000138 void populateType3Font(int16_t glyphID);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000139 bool addFontDescriptor(int16_t defaultWidth);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000140 void populateToUnicodeTable();
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000141 void addWidthInfoFromRange(int16_t defaultWidth,
142 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry);
vandebo@chromium.orgf77e27d2011-03-10 22:50:28 +0000143 /** Set fFirstGlyphID and fLastGlyphID to span at most 255 glyphs,
144 * including the passed glyphID.
145 */
146 void adjustGlyphRangeForSingleByteEncoding(int16_t glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000147
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000148 static bool find(uint32_t fontID, uint16_t glyphID, int* index);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000149};
150
151#endif