1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
// TODO(edisonn): this file not commented much on purpose.
// It will probably need heavy refactoring soon anyway to support all encodings, fonts and
// proper text sizing and spacing
#ifndef SkPdfFont_DEFINED
#define SkPdfFont_DEFINED
#include "SkPdfContext.h"
#include "SkPdfHeaders_autogen.h"
#include "SkPdfMapper_autogen.h"
#include "SkPdfUtils.h"
#include "SkTypeface.h"
#include "SkTDict.h"
#include "SkUtils.h"
class SkPdfType0Font;
class SkPdfType1Font;
class SkPdfType3Font;
class SkPdfTrueTypeFont;
class SkPdfMultiMasterFont;
class SkPdfFont;
struct SkPdfStandardFontEntry {
// We don't own this pointer!
const char* fName;
bool fIsBold;
bool fIsItalic;
SkPdfStandardFontEntry()
: fName(NULL),
fIsBold(false),
fIsItalic(false) {}
SkPdfStandardFontEntry(const char* name, bool bold, bool italic)
: fName(name),
fIsBold(bold),
fIsItalic(italic) {}
};
SkTDict<SkPdfStandardFontEntry>& getStandardFonts();
SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic);
SkPdfFont* fontFromName(SkPdfNativeDoc* doc, SkPdfNativeObject* obj, const char* fontName);
struct SkUnencodedText {
void* text;
int len;
public:
SkUnencodedText(const SkPdfString* obj) {
text = (void*)obj->c_str();
len = (int) obj->lenstr();
}
};
struct SkDecodedText {
uint16_t* text;
int len;
public:
unsigned int operator[](int i) const { return text[i]; }
int size() const { return len; }
};
struct SkUnicodeText {
uint16_t* text;
int len;
public:
unsigned int operator[](int i) const { return text[i]; }
int size() const { return len; }
};
class SkPdfEncoding {
public:
virtual ~SkPdfEncoding() {}
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const = 0;
static SkPdfEncoding* fromName(const char* name);
};
SkTDict<SkPdfEncoding*>& getStandardEncodings();
class SkPdfToUnicode {
// TODO(edisonn): hide public members
public:
unsigned short* fCMapEncoding;
unsigned char* fCMapEncodingFlag;
SkPdfToUnicode(SkPdfNativeDoc* parsed, SkPdfStream* stream);
};
class SkPdfIdentityHEncoding : public SkPdfEncoding {
public:
virtual ~SkPdfIdentityHEncoding() {}
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
// TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
uint16_t* text = (uint16_t*)textIn.text;
textOut->text = new uint16_t[textIn.len / 2];
textOut->len = textIn.len / 2;
for (int i = 0; i < textOut->len; i++) {
textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff);
}
return true;
}
static SkPdfIdentityHEncoding* instance() {
static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding();
return inst;
}
};
// TODO(edisonn): using this one when no encoding is specified
class SkPdfDefaultEncoding : public SkPdfEncoding {
public:
virtual ~SkPdfDefaultEncoding() {}
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
// TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
unsigned char* text = (unsigned char*)textIn.text;
textOut->text = new uint16_t[textIn.len];
textOut->len = textIn.len;
for (int i = 0; i < textOut->len; i++) {
textOut->text[i] = text[i];
}
return true;
}
static SkPdfDefaultEncoding* instance() {
static SkPdfDefaultEncoding* inst = new SkPdfDefaultEncoding();
return inst;
}
};
class SkPdfCIDToGIDMapIdentityEncoding : public SkPdfEncoding {
public:
virtual ~SkPdfCIDToGIDMapIdentityEncoding() {}
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
// TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
uint16_t* text = (uint16_t*)textIn.text;
textOut->text = new uint16_t[textIn.len / 2];
textOut->len = textIn.len / 2;
for (int i = 0; i < textOut->len; i++) {
textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff);
}
return true;
}
static SkPdfCIDToGIDMapIdentityEncoding* instance() {
static SkPdfCIDToGIDMapIdentityEncoding* inst = new SkPdfCIDToGIDMapIdentityEncoding();
return inst;
}
};
class SkPdfFont {
public:
SkPdfFont* fBaseFont;
SkPdfEncoding* fEncoding;
SkPdfToUnicode* fToUnicode;
public:
SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()), fToUnicode(NULL) {}
virtual ~SkPdfFont() {
// TODO(edisonn): NYI (will leak for now)
}
const SkPdfEncoding* encoding() const {return fEncoding;}
void drawText(const SkDecodedText& text, SkPaint* paint, SkPdfContext* pdfContext,
SkCanvas* canvas) {
for (int i = 0 ; i < text.size(); i++) {
canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
#ifdef PDF_TRACE
SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(0));
pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1);
printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()),
SkScalarToDouble(point.y()));
#endif // PDF_TRACE
#ifdef PDF_TRACE_DRAWTEXT
SkPaint col;
col.setColor(SK_ColorMAGENTA);
SkRect rect = SkRect::MakeXYWH(SkDoubleToScalar(0.0),
SkDoubleToScalar(0.0),
SkDoubleToScalar(10.0),
SkDoubleToScalar(10.0));
canvas->save();
canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
canvas->drawRect(rect, col);
canvas->restore();
#endif
double width = drawOneChar(text[i], paint, pdfContext, canvas);
pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(width),
SkDoubleToScalar(0.0));
}
}
void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const {
if (fToUnicode) {
textOut->text = new uint16_t[textIn.len];
textOut->len = textIn.len;
for (int i = 0; i < textIn.len; i++) {
textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]];
}
} else {
textOut->text = textIn.text;
textOut->len = textIn.len;
}
};
inline unsigned int ToUnicode(unsigned int ch) const {
if (fToUnicode && fToUnicode->fCMapEncoding) {
return fToUnicode->fCMapEncoding[ch];
} else {
return ch;
}
};
static SkPdfFont* fontFromPdfDictionary(SkPdfNativeDoc* doc, SkPdfFontDictionary* dict);
static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman");}
static SkPdfType0Font* fontFromType0FontDictionary(SkPdfNativeDoc* doc,
SkPdfType0FontDictionary* dict);
static SkPdfType1Font* fontFromType1FontDictionary(SkPdfNativeDoc* doc,
SkPdfType1FontDictionary* dict);
static SkPdfType3Font* fontFromType3FontDictionary(SkPdfNativeDoc* doc,
SkPdfType3FontDictionary* dict);
static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfNativeDoc* doc,
SkPdfTrueTypeFontDictionary* dict);
static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(
SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict);
static SkPdfFont* fontFromFontDescriptor(SkPdfNativeDoc* doc,
SkPdfFontDescriptorDictionary* fd,
bool loadFromName = true);
public:
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
SkCanvas* canvas) = 0;
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0;
private:
static SkPdfFont* fontFromPdfDictionaryOnce(SkPdfNativeDoc* doc, SkPdfFontDictionary* dict);
};
class SkPdfStandardFont : public SkPdfFont {
SkTypeface* fTypeface;
public:
SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {}
public:
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
SkCanvas* canvas) {
paint->setTypeface(fTypeface);
paint->setTextEncoding(SkPaint::kUTF8_TextEncoding);
unsigned long ch4 = ch;
char utf8[10];
size_t len = SkUTF8_FromUnichar((SkUnichar) ch4, utf8);
canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *paint);
SkScalar textWidth = paint->measureText(utf8, len);
return SkScalarToDouble(textWidth);
}
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
};
class SkPdfType0Font : public SkPdfFont {
public:
SkPdfType0Font(SkPdfNativeDoc* doc, SkPdfType0FontDictionary* dict);
public:
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
SkCanvas* canvas) {
return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
}
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
}
};
class SkPdfType1Font : public SkPdfFont {
public:
SkPdfType1Font(SkPdfNativeDoc* doc, SkPdfType1FontDictionary* dict) {
if (dict->has_FontDescriptor()) {
fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescriptor(doc));
} else {
fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str());
}
if (dict->isEncodingAName(doc)) {
fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(doc).c_str());
} else if (dict->isEncodingADictionary(doc)) {
//SkPdfDictionary* dictEnc = dict->getEncodingAsDictionary(doc);
}
dict->FontDescriptor(doc);
}
public:
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
SkCanvas* canvas) {
return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
}
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
}
};
class SkPdfTrueTypeFont : public SkPdfType1Font {
public:
SkPdfTrueTypeFont(SkPdfNativeDoc* doc, SkPdfTrueTypeFontDictionary* dict)
: SkPdfType1Font(doc, dict) {}
};
class SkPdfMultiMasterFont : public SkPdfType1Font {
public:
SkPdfMultiMasterFont(SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict)
: SkPdfType1Font(doc, dict) {}
};
/*
class CIDToGIDMap {
virtual unsigned int map(unsigned int cid) = 0;
static CIDToGIDMap* fromName(const char* name);
};
class CIDToGIDMap_Identity {
virtual unsigned int map(unsigned int cid) { return cid; }
static CIDToGIDMap_Identity* instance() {
static CIDToGIDMap_Identity* inst = new CIDToGIDMap_Identity();
return inst;
}
};
CIDToGIDMap* CIDToGIDMap::fromName(const char* name) {
// The only one supported right now is Identity
if (strcmp(name, "Identity") == 0) {
return CIDToGIDMap_Identity::instance();
}
#ifdef PDF_TRACE
// TODO(edisonn): warning/report
printf("Unknown CIDToGIDMap: %s\n", name);
#endif
return NULL;
}
CIDToGIDMap* fCidToGid;
*/
class SkPdfType3Font : public SkPdfFont {
struct Type3FontChar {
SkPdfNativeObject* fObj;
double fWidth;
};
SkPdfDictionary* fCharProcs;
SkPdfEncodingDictionary* fEncodingDict;
unsigned int fFirstChar;
unsigned int fLastChar;
SkRect fFontBBox;
SkMatrix fFonMatrix;
Type3FontChar* fChars;
public:
SkPdfType3Font(SkPdfNativeDoc* parsed, SkPdfType3FontDictionary* dict) {
fBaseFont = fontFromName(parsed, dict, dict->BaseFont(parsed).c_str());
if (dict->has_Encoding()) {
if (dict->isEncodingAName(parsed)) {
fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(parsed).c_str());
} else if (dict->isEncodingAEncodingdictionary(parsed)) {
// No encoding.
fEncoding = SkPdfDefaultEncoding::instance();
fEncodingDict = dict->getEncodingAsEncodingdictionary(parsed);
}
}
// null?
fCharProcs = dict->CharProcs(parsed);
fToUnicode = NULL;
if (dict->has_ToUnicode()) {
fToUnicode = new SkPdfToUnicode(parsed, dict->ToUnicode(parsed));
}
fFirstChar = (unsigned int)dict->FirstChar(parsed);
fLastChar = (unsigned int)dict->LastChar(parsed);
fFonMatrix = dict->has_FontMatrix() ? dict->FontMatrix(parsed) : SkMatrix::I();
if (dict->has_FontBBox()) {
fFontBBox = dict->FontBBox(parsed);
}
fChars = new Type3FontChar[fLastChar - fFirstChar + 1];
memset(fChars, 0, sizeof(fChars[0]) * (fLastChar - fFirstChar + 1));
const SkPdfArray* widths = dict->Widths(parsed);
for (unsigned int i = 0 ; i < widths->size(); i++) {
if ((fFirstChar + i) >= fFirstChar && (fFirstChar + i) <= fLastChar) {
fChars[i].fWidth = (*widths)[i]->numberValue();
} else {
// TODO(edisonn): report pdf corruption
}
}
const SkPdfArray* diffs = fEncodingDict->Differences(parsed);
unsigned int j = fFirstChar;
for (unsigned int i = 0 ; i < diffs->size(); i++) {
if ((*diffs)[i]->isInteger()) {
j = (unsigned int)(*diffs)[i]->intValue();
} else if ((*diffs)[i]->isName()) {
if (j >= fFirstChar && j <= fLastChar) {
fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]);
} else {
// TODO(edisonn): report pdf corruption
}
j++;
} else {
// TODO(edisonn): report bad pdf
}
}
}
public:
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
SkCanvas* canvas) {
if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) {
return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
}
#ifdef PDF_TRACE
printf("Type 3 char to unicode: %c\n", ToUnicode(ch));
if (ToUnicode(ch) == 'A') {
printf("break;\n");
}
#endif
// TODO(edisonn): is it better to resolve the reference at load time, or now?
doType3Char(pdfContext,
canvas,
pdfContext->fPdfDoc->resolveReference(fChars[ch - fFirstChar].fObj),
fFontBBox,
fFonMatrix,
pdfContext->fGraphicsState.fCurFontSize);
// TODO(edisonn): verify/test translate code, not tested yet
pdfContext->fGraphicsState.fMatrixTm.preTranslate(
SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize *
fChars[ch - fFirstChar].fWidth),
SkDoubleToScalar(0.0));
return fChars[ch - fFirstChar].fWidth;
}
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
};
#endif // SkPdfFont_DEFINED
|