blob: cc490553d1a22b55ea0ce93c49e60d45eb686878 [file] [log] [blame]
edisonn@google.comcf2cfa12013-08-21 16:31:37 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
edisonn@google.comd03c2c72013-10-11 18:26:45 +00008// TODO(edisonn): this file not commented much on purpose.
9// It will probably need heavy refactoring soon anyway to support all encodings, fonts and
10// proper text sizing and spacing
11
edisonn@google.comcf2cfa12013-08-21 16:31:37 +000012#ifndef SkPdfFont_DEFINED
13#define SkPdfFont_DEFINED
edisonn@google.com1be794f2013-06-21 21:43:09 +000014
scroggo@google.comd9067022013-11-20 22:33:39 +000015#include "SkPdfContext.h"
edisonn@google.com1be794f2013-06-21 21:43:09 +000016#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000017#include "SkPdfMapper_autogen.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000018#include "SkPdfUtils.h"
edisonn@google.come50d9a12013-10-10 20:58:22 +000019#include "SkTypeface.h"
edisonn@google.com063d7072013-08-16 15:05:08 +000020#include "SkTDict.h"
edisonn@google.come50d9a12013-10-10 20:58:22 +000021#include "SkUtils.h"
edisonn@google.com1be794f2013-06-21 21:43:09 +000022
23class SkPdfType0Font;
24class SkPdfType1Font;
25class SkPdfType3Font;
26class SkPdfTrueTypeFont;
edisonn@google.com1be794f2013-06-21 21:43:09 +000027class SkPdfMultiMasterFont;
28class SkPdfFont;
29
edisonn@google.com1be794f2013-06-21 21:43:09 +000030struct SkPdfStandardFontEntry {
edisonn@google.com596d2e22013-07-10 17:44:55 +000031 // We don't own this pointer!
edisonn@google.com1be794f2013-06-21 21:43:09 +000032 const char* fName;
33 bool fIsBold;
34 bool fIsItalic;
edisonn@google.com596d2e22013-07-10 17:44:55 +000035 SkPdfStandardFontEntry()
36 : fName(NULL),
37 fIsBold(false),
38 fIsItalic(false) {}
39
40 SkPdfStandardFontEntry(const char* name, bool bold, bool italic)
41 : fName(name),
42 fIsBold(bold),
43 fIsItalic(italic) {}
edisonn@google.com1be794f2013-06-21 21:43:09 +000044};
45
edisonn@google.com063d7072013-08-16 15:05:08 +000046SkTDict<SkPdfStandardFontEntry>& getStandardFonts();
edisonn@google.com1be794f2013-06-21 21:43:09 +000047SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic);
edisonn@google.com3aa35552013-08-14 18:26:20 +000048SkPdfFont* fontFromName(SkPdfNativeDoc* doc, SkPdfNativeObject* obj, const char* fontName);
edisonn@google.com1be794f2013-06-21 21:43:09 +000049
50struct SkUnencodedText {
51 void* text;
52 int len;
53
54public:
edisonn@google.com3aac1f92013-07-02 22:42:53 +000055 SkUnencodedText(const SkPdfString* obj) {
56 text = (void*)obj->c_str();
scroggo@google.com5f008652013-12-02 20:58:12 +000057 len = (int) obj->lenstr();
edisonn@google.com1be794f2013-06-21 21:43:09 +000058 }
59};
60
61struct SkDecodedText {
62 uint16_t* text;
63 int len;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000064public:
65 unsigned int operator[](int i) const { return text[i]; }
66 int size() const { return len; }
edisonn@google.com1be794f2013-06-21 21:43:09 +000067};
68
69struct SkUnicodeText {
70 uint16_t* text;
71 int len;
72
73public:
74 unsigned int operator[](int i) const { return text[i]; }
75 int size() const { return len; }
76};
77
78class SkPdfEncoding {
79public:
mtklein@google.com9615f8d2013-11-20 14:29:51 +000080 virtual ~SkPdfEncoding() {}
edisonn@google.com1be794f2013-06-21 21:43:09 +000081 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const = 0;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000082 static SkPdfEncoding* fromName(const char* name);
edisonn@google.com1be794f2013-06-21 21:43:09 +000083};
84
edisonn@google.com063d7072013-08-16 15:05:08 +000085SkTDict<SkPdfEncoding*>& getStandardEncodings();
edisonn@google.comb857a0c2013-06-25 20:45:40 +000086
87class SkPdfToUnicode {
88 // TODO(edisonn): hide public members
89public:
90 unsigned short* fCMapEncoding;
91 unsigned char* fCMapEncodingFlag;
92
edisonn@google.com3aa35552013-08-14 18:26:20 +000093 SkPdfToUnicode(SkPdfNativeDoc* parsed, SkPdfStream* stream);
edisonn@google.comb857a0c2013-06-25 20:45:40 +000094};
95
96
edisonn@google.com1be794f2013-06-21 21:43:09 +000097class SkPdfIdentityHEncoding : public SkPdfEncoding {
98public:
mtklein@google.com9615f8d2013-11-20 14:29:51 +000099 virtual ~SkPdfIdentityHEncoding() {}
edisonn@google.com1be794f2013-06-21 21:43:09 +0000100 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
101 // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
102
103 uint16_t* text = (uint16_t*)textIn.text;
104 textOut->text = new uint16_t[textIn.len / 2];
105 textOut->len = textIn.len / 2;
106
107 for (int i = 0; i < textOut->len; i++) {
108 textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff);
109 }
110
111 return true;
112 }
113
114 static SkPdfIdentityHEncoding* instance() {
115 static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding();
116 return inst;
117 }
118};
119
edisonn@google.com6e49c342013-06-27 20:03:43 +0000120// TODO(edisonn): using this one when no encoding is specified
121class SkPdfDefaultEncoding : public SkPdfEncoding {
122public:
mtklein@google.com9615f8d2013-11-20 14:29:51 +0000123 virtual ~SkPdfDefaultEncoding() {}
edisonn@google.com6e49c342013-06-27 20:03:43 +0000124 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
125 // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
126
127 unsigned char* text = (unsigned char*)textIn.text;
128 textOut->text = new uint16_t[textIn.len];
129 textOut->len = textIn.len;
130
131 for (int i = 0; i < textOut->len; i++) {
132 textOut->text[i] = text[i];
133 }
134
135 return true;
136 }
137
138 static SkPdfDefaultEncoding* instance() {
139 static SkPdfDefaultEncoding* inst = new SkPdfDefaultEncoding();
140 return inst;
141 }
142};
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000143
144class SkPdfCIDToGIDMapIdentityEncoding : public SkPdfEncoding {
145public:
mtklein@google.com9615f8d2013-11-20 14:29:51 +0000146 virtual ~SkPdfCIDToGIDMapIdentityEncoding() {}
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000147 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
148 // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
149
edisonn@google.com571c70b2013-07-10 17:09:50 +0000150 uint16_t* text = (uint16_t*)textIn.text;
151 textOut->text = new uint16_t[textIn.len / 2];
152 textOut->len = textIn.len / 2;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000153
154 for (int i = 0; i < textOut->len; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000155 textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000156 }
157
158 return true;
159 }
160
161 static SkPdfCIDToGIDMapIdentityEncoding* instance() {
162 static SkPdfCIDToGIDMapIdentityEncoding* inst = new SkPdfCIDToGIDMapIdentityEncoding();
163 return inst;
164 }
165};
166
edisonn@google.com1be794f2013-06-21 21:43:09 +0000167class SkPdfFont {
168public:
169 SkPdfFont* fBaseFont;
170 SkPdfEncoding* fEncoding;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000171 SkPdfToUnicode* fToUnicode;
172
edisonn@google.com1be794f2013-06-21 21:43:09 +0000173
174public:
edisonn@google.com6e49c342013-06-27 20:03:43 +0000175 SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()), fToUnicode(NULL) {}
edisonn@google.com1be794f2013-06-21 21:43:09 +0000176
edisonn@google.com7ee12ca2013-08-05 18:18:06 +0000177 virtual ~SkPdfFont() {
178 // TODO(edisonn): NYI (will leak for now)
179 }
180
edisonn@google.com1be794f2013-06-21 21:43:09 +0000181 const SkPdfEncoding* encoding() const {return fEncoding;}
182
edisonn@google.come50d9a12013-10-10 20:58:22 +0000183 void drawText(const SkDecodedText& text, SkPaint* paint, SkPdfContext* pdfContext,
184 SkCanvas* canvas) {
edisonn@google.com1be794f2013-06-21 21:43:09 +0000185 for (int i = 0 ; i < text.size(); i++) {
edisonn@google.come57c62d2013-08-07 18:04:15 +0000186 canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
187#ifdef PDF_TRACE
188 SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(0));
189 pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1);
edisonn@google.come50d9a12013-10-10 20:58:22 +0000190 printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()),
191 SkScalarToDouble(point.y()));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000192#endif // PDF_TRACE
edisonn@google.com3aa35552013-08-14 18:26:20 +0000193
194#ifdef PDF_TRACE_DRAWTEXT
195 SkPaint col;
196 col.setColor(SK_ColorMAGENTA);
edisonn@google.come50d9a12013-10-10 20:58:22 +0000197 SkRect rect = SkRect::MakeXYWH(SkDoubleToScalar(0.0),
198 SkDoubleToScalar(0.0),
199 SkDoubleToScalar(10.0),
200 SkDoubleToScalar(10.0));
edisonn@google.com3aa35552013-08-14 18:26:20 +0000201 canvas->save();
202 canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
203 canvas->drawRect(rect, col);
204 canvas->restore();
205#endif
edisonn@google.com6e49c342013-06-27 20:03:43 +0000206 double width = drawOneChar(text[i], paint, pdfContext, canvas);
edisonn@google.come50d9a12013-10-10 20:58:22 +0000207 pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(width),
208 SkDoubleToScalar(0.0));
edisonn@google.com1be794f2013-06-21 21:43:09 +0000209 }
210 }
211
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000212 void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const {
213 if (fToUnicode) {
214 textOut->text = new uint16_t[textIn.len];
215 textOut->len = textIn.len;
216 for (int i = 0; i < textIn.len; i++) {
217 textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]];
218 }
219 } else {
220 textOut->text = textIn.text;
221 textOut->len = textIn.len;
222 }
223 };
224
225 inline unsigned int ToUnicode(unsigned int ch) const {
edisonn@google.comf68aed32013-08-22 15:37:21 +0000226 if (fToUnicode && fToUnicode->fCMapEncoding) {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000227 return fToUnicode->fCMapEncoding[ch];
228 } else {
229 return ch;
230 }
edisonn@google.com1be794f2013-06-21 21:43:09 +0000231 };
232
edisonn@google.com3aa35552013-08-14 18:26:20 +0000233 static SkPdfFont* fontFromPdfDictionary(SkPdfNativeDoc* doc, SkPdfFontDictionary* dict);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000234 static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman");}
edisonn@google.com1be794f2013-06-21 21:43:09 +0000235
edisonn@google.come50d9a12013-10-10 20:58:22 +0000236 static SkPdfType0Font* fontFromType0FontDictionary(SkPdfNativeDoc* doc,
237 SkPdfType0FontDictionary* dict);
238 static SkPdfType1Font* fontFromType1FontDictionary(SkPdfNativeDoc* doc,
239 SkPdfType1FontDictionary* dict);
240 static SkPdfType3Font* fontFromType3FontDictionary(SkPdfNativeDoc* doc,
241 SkPdfType3FontDictionary* dict);
242 static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfNativeDoc* doc,
243 SkPdfTrueTypeFontDictionary* dict);
244 static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(
245 SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000246
edisonn@google.come50d9a12013-10-10 20:58:22 +0000247 static SkPdfFont* fontFromFontDescriptor(SkPdfNativeDoc* doc,
248 SkPdfFontDescriptorDictionary* fd,
249 bool loadFromName = true);
edisonn@google.com6e49c342013-06-27 20:03:43 +0000250
edisonn@google.com1be794f2013-06-21 21:43:09 +0000251public:
edisonn@google.come50d9a12013-10-10 20:58:22 +0000252 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
253 SkCanvas* canvas) = 0;
edisonn@google.com1be794f2013-06-21 21:43:09 +0000254 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000255
256private:
edisonn@google.com3aa35552013-08-14 18:26:20 +0000257 static SkPdfFont* fontFromPdfDictionaryOnce(SkPdfNativeDoc* doc, SkPdfFontDictionary* dict);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000258};
259
260class SkPdfStandardFont : public SkPdfFont {
261 SkTypeface* fTypeface;
262
263public:
264 SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {}
265
266public:
edisonn@google.come50d9a12013-10-10 20:58:22 +0000267 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
268 SkCanvas* canvas) {
edisonn@google.com1be794f2013-06-21 21:43:09 +0000269 paint->setTypeface(fTypeface);
270 paint->setTextEncoding(SkPaint::kUTF8_TextEncoding);
271
272 unsigned long ch4 = ch;
273 char utf8[10];
scroggo@google.com5f008652013-12-02 20:58:12 +0000274 size_t len = SkUTF8_FromUnichar((SkUnichar) ch4, utf8);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000275
276 canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *paint);
277
278 SkScalar textWidth = paint->measureText(utf8, len);
edisonn@google.com6e49c342013-06-27 20:03:43 +0000279 return SkScalarToDouble(textWidth);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000280 }
281
edisonn@google.com1be794f2013-06-21 21:43:09 +0000282 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
283};
284
edisonn@google.com1be794f2013-06-21 21:43:09 +0000285class SkPdfType0Font : public SkPdfFont {
edisonn@google.com1be794f2013-06-21 21:43:09 +0000286public:
edisonn@google.com3aa35552013-08-14 18:26:20 +0000287 SkPdfType0Font(SkPdfNativeDoc* doc, SkPdfType0FontDictionary* dict);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000288
289public:
edisonn@google.com1be794f2013-06-21 21:43:09 +0000290
edisonn@google.come50d9a12013-10-10 20:58:22 +0000291 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
292 SkCanvas* canvas) {
edisonn@google.com6e49c342013-06-27 20:03:43 +0000293 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000294 }
295
296 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
edisonn@google.com1be794f2013-06-21 21:43:09 +0000297 }
298};
299
edisonn@google.com1be794f2013-06-21 21:43:09 +0000300class SkPdfType1Font : public SkPdfFont {
301public:
edisonn@google.com3aa35552013-08-14 18:26:20 +0000302 SkPdfType1Font(SkPdfNativeDoc* doc, SkPdfType1FontDictionary* dict) {
edisonn@google.com6e49c342013-06-27 20:03:43 +0000303 if (dict->has_FontDescriptor()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000304 fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescriptor(doc));
edisonn@google.com6e49c342013-06-27 20:03:43 +0000305 } else {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000306 fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str());
edisonn@google.com6e49c342013-06-27 20:03:43 +0000307 }
edisonn@google.com3aa35552013-08-14 18:26:20 +0000308
309 if (dict->isEncodingAName(doc)) {
310 fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(doc).c_str());
311 } else if (dict->isEncodingADictionary(doc)) {
312 //SkPdfDictionary* dictEnc = dict->getEncodingAsDictionary(doc);
313 }
314 dict->FontDescriptor(doc);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000315 }
316
edisonn@google.com1be794f2013-06-21 21:43:09 +0000317public:
edisonn@google.come50d9a12013-10-10 20:58:22 +0000318 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
319 SkCanvas* canvas) {
edisonn@google.com6e49c342013-06-27 20:03:43 +0000320 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000321 }
322
323 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
324
325 }
edisonn@google.com6e49c342013-06-27 20:03:43 +0000326};
edisonn@google.com1be794f2013-06-21 21:43:09 +0000327
edisonn@google.com6e49c342013-06-27 20:03:43 +0000328class SkPdfTrueTypeFont : public SkPdfType1Font {
edisonn@google.com1be794f2013-06-21 21:43:09 +0000329public:
edisonn@google.come50d9a12013-10-10 20:58:22 +0000330 SkPdfTrueTypeFont(SkPdfNativeDoc* doc, SkPdfTrueTypeFontDictionary* dict)
331 : SkPdfType1Font(doc, dict) {}
edisonn@google.com1be794f2013-06-21 21:43:09 +0000332};
333
edisonn@google.com6e49c342013-06-27 20:03:43 +0000334class SkPdfMultiMasterFont : public SkPdfType1Font {
edisonn@google.com1be794f2013-06-21 21:43:09 +0000335public:
edisonn@google.come50d9a12013-10-10 20:58:22 +0000336 SkPdfMultiMasterFont(SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict)
337 : SkPdfType1Font(doc, dict) {}
edisonn@google.com1be794f2013-06-21 21:43:09 +0000338};
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000339/*
340class CIDToGIDMap {
341 virtual unsigned int map(unsigned int cid) = 0;
342 static CIDToGIDMap* fromName(const char* name);
343};
344
345class CIDToGIDMap_Identity {
346 virtual unsigned int map(unsigned int cid) { return cid; }
347
348 static CIDToGIDMap_Identity* instance() {
349 static CIDToGIDMap_Identity* inst = new CIDToGIDMap_Identity();
350 return inst;
351 }
352};
353
354CIDToGIDMap* CIDToGIDMap::fromName(const char* name) {
355 // The only one supported right now is Identity
356 if (strcmp(name, "Identity") == 0) {
357 return CIDToGIDMap_Identity::instance();
358 }
359
360#ifdef PDF_TRACE
361 // TODO(edisonn): warning/report
362 printf("Unknown CIDToGIDMap: %s\n", name);
363#endif
364 return NULL;
365}
366CIDToGIDMap* fCidToGid;
367*/
edisonn@google.com1be794f2013-06-21 21:43:09 +0000368
369class SkPdfType3Font : public SkPdfFont {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000370 struct Type3FontChar {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000371 SkPdfNativeObject* fObj;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000372 double fWidth;
373 };
374
375 SkPdfDictionary* fCharProcs;
376 SkPdfEncodingDictionary* fEncodingDict;
377 unsigned int fFirstChar;
378 unsigned int fLastChar;
379
380 SkRect fFontBBox;
381 SkMatrix fFonMatrix;
382
383 Type3FontChar* fChars;
384
edisonn@google.com1be794f2013-06-21 21:43:09 +0000385public:
edisonn@google.com3aa35552013-08-14 18:26:20 +0000386 SkPdfType3Font(SkPdfNativeDoc* parsed, SkPdfType3FontDictionary* dict) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000387 fBaseFont = fontFromName(parsed, dict, dict->BaseFont(parsed).c_str());
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000388
389 if (dict->has_Encoding()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000390 if (dict->isEncodingAName(parsed)) {
391 fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(parsed).c_str());
392 } else if (dict->isEncodingAEncodingdictionary(parsed)) {
edisonn@google.com2fd5d362013-07-23 19:43:48 +0000393 // No encoding.
394 fEncoding = SkPdfDefaultEncoding::instance();
edisonn@google.com571c70b2013-07-10 17:09:50 +0000395 fEncodingDict = dict->getEncodingAsEncodingdictionary(parsed);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000396 }
397 }
398
399 // null?
edisonn@google.com571c70b2013-07-10 17:09:50 +0000400 fCharProcs = dict->CharProcs(parsed);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000401
402 fToUnicode = NULL;
403 if (dict->has_ToUnicode()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000404 fToUnicode = new SkPdfToUnicode(parsed, dict->ToUnicode(parsed));
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000405 }
406
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000407 fFirstChar = (unsigned int)dict->FirstChar(parsed);
408 fLastChar = (unsigned int)dict->LastChar(parsed);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000409 fFonMatrix = dict->has_FontMatrix() ? dict->FontMatrix(parsed) : SkMatrix::I();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000410
edisonn@google.com571c70b2013-07-10 17:09:50 +0000411 if (dict->has_FontBBox()) {
412 fFontBBox = dict->FontBBox(parsed);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000413 }
414
415 fChars = new Type3FontChar[fLastChar - fFirstChar + 1];
416
417 memset(fChars, 0, sizeof(fChars[0]) * (fLastChar - fFirstChar + 1));
418
edisonn@google.com571c70b2013-07-10 17:09:50 +0000419 const SkPdfArray* widths = dict->Widths(parsed);
420 for (unsigned int i = 0 ; i < widths->size(); i++) {
edisonn@google.comac4bedc2013-07-25 21:40:23 +0000421 if ((fFirstChar + i) >= fFirstChar && (fFirstChar + i) <= fLastChar) {
422 fChars[i].fWidth = (*widths)[i]->numberValue();
423 } else {
424 // TODO(edisonn): report pdf corruption
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000425 }
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000426 }
427
edisonn@google.com571c70b2013-07-10 17:09:50 +0000428 const SkPdfArray* diffs = fEncodingDict->Differences(parsed);
429 unsigned int j = fFirstChar;
430 for (unsigned int i = 0 ; i < diffs->size(); i++) {
431 if ((*diffs)[i]->isInteger()) {
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000432 j = (unsigned int)(*diffs)[i]->intValue();
edisonn@google.com571c70b2013-07-10 17:09:50 +0000433 } else if ((*diffs)[i]->isName()) {
edisonn@google.comac4bedc2013-07-25 21:40:23 +0000434 if (j >= fFirstChar && j <= fLastChar) {
435 fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]);
436 } else {
437 // TODO(edisonn): report pdf corruption
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000438 }
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000439 j++;
440 } else {
edisonn@google.comac4bedc2013-07-25 21:40:23 +0000441 // TODO(edisonn): report bad pdf
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000442 }
443 }
edisonn@google.com1be794f2013-06-21 21:43:09 +0000444 }
445
446public:
edisonn@google.come50d9a12013-10-10 20:58:22 +0000447 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
448 SkCanvas* canvas) {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000449 if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) {
edisonn@google.com6e49c342013-06-27 20:03:43 +0000450 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000451 }
edisonn@google.com1be794f2013-06-21 21:43:09 +0000452
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000453#ifdef PDF_TRACE
454 printf("Type 3 char to unicode: %c\n", ToUnicode(ch));
455 if (ToUnicode(ch) == 'A') {
456 printf("break;\n");
457 }
458#endif
459
edisonn@google.com571c70b2013-07-10 17:09:50 +0000460 // TODO(edisonn): is it better to resolve the reference at load time, or now?
edisonn@google.come50d9a12013-10-10 20:58:22 +0000461 doType3Char(pdfContext,
462 canvas,
463 pdfContext->fPdfDoc->resolveReference(fChars[ch - fFirstChar].fObj),
464 fFontBBox,
465 fFonMatrix,
466 pdfContext->fGraphicsState.fCurFontSize);
edisonn@google.com1be794f2013-06-21 21:43:09 +0000467
edisonn@google.com6e49c342013-06-27 20:03:43 +0000468 // TODO(edisonn): verify/test translate code, not tested yet
edisonn@google.come50d9a12013-10-10 20:58:22 +0000469 pdfContext->fGraphicsState.fMatrixTm.preTranslate(
470 SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize *
471 fChars[ch - fFirstChar].fWidth),
472 SkDoubleToScalar(0.0));
edisonn@google.com571c70b2013-07-10 17:09:50 +0000473 return fChars[ch - fFirstChar].fWidth;
edisonn@google.com1be794f2013-06-21 21:43:09 +0000474 }
475
edisonn@google.come50d9a12013-10-10 20:58:22 +0000476 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
edisonn@google.com1be794f2013-06-21 21:43:09 +0000477};
478
edisonn@google.comcf2cfa12013-08-21 16:31:37 +0000479#endif // SkPdfFont_DEFINED