blob: b18167ce9f944fc05502f62dfd2f1e3788a0a017 [file] [log] [blame]
edisonn@google.com131d4ee2013-06-26 17:48:12 +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
8#include "SkCanvas.h"
9#include "SkDevice.h"
10#include "SkForceLinking.h"
11#include "SkGraphics.h"
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
14#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTypeface.h"
18#include "SkTArray.h"
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000019#include "SkTDict.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000020
edisonn@google.com15b11182013-07-11 14:43:15 +000021#include "SkPdfBasics.h"
22#include "SkPdfNativeTokenizer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000023#include <cstdio>
24#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000025#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000026
edisonn@google.com15b11182013-07-11 14:43:15 +000027extern "C" PdfContext* gPdfContext;
28extern "C" SkBitmap* gDumpBitmap;
29extern "C" SkCanvas* gDumpCanvas;
30
edisonn@google.com131d4ee2013-06-26 17:48:12 +000031__SK_FORCE_IMAGE_DECODER_LINKING;
32
33// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
34// keep for each object pos in file
35// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
36
37// TODO(edisonn): security - validate all the user input, all pdf!
38
edisonn@google.com6e49c342013-06-27 20:03:43 +000039// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
40// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000041
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
43/*
44#ifdef PDF_TRACE
45 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000046 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000047 printf("Print Tf Resources: %s\n", str.c_str());
48#endif
49 */
50
edisonn@google.com131d4ee2013-06-26 17:48:12 +000051#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000052#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000053#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000054
55#include "SkPdfBasics.h"
56#include "SkPdfUtils.h"
57
58#include "SkPdfFont.h"
59
edisonn@google.com131d4ee2013-06-26 17:48:12 +000060/*
61 * TODO(edisonn):
62 * - all font types and all ppdf font features
63 * - word spacing
64 * - load font for baidu.pdf
65 * - load font for youtube.pdf
66 * - parser for pdf from the definition already available in pdfspec_autogen.py
67 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000068 * - encapsulate native in the pdf api so the skpdf does not know anything about native ... in progress
edisonn@google.com131d4ee2013-06-26 17:48:12 +000069 * - load gs/ especially smask and already known prop (skp) ... in progress
70 * - wrapper on classes for customizations? e.g.
71 * SkPdfPageObjectVanila - has only the basic loaders/getters
72 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
73 * need to find a nice object model for all this with constructors and factories
74 * - deal with inheritable automatically ?
75 * - deal with specific type in spec directly, add all dictionary types to known types
76*/
77
78using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000079
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000080NotOwnedString strings_DeviceRGB;
81NotOwnedString strings_DeviceCMYK;
edisonn@google.com222382b2013-07-10 22:33:10 +000082
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000083class StringsInit {
84public:
85 StringsInit() {
86 NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB");
87 NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK");
88 }
89};
90
91StringsInit gStringsInit;
edisonn@google.com222382b2013-07-10 22:33:10 +000092
93// TODO(edisonn): Document PdfTokenLooper and subclasses.
94class PdfTokenLooper {
95protected:
96 PdfTokenLooper* fParent;
97 SkPdfNativeTokenizer* fTokenizer;
98 PdfContext* fPdfContext;
99 SkCanvas* fCanvas;
100
101public:
102 PdfTokenLooper(PdfTokenLooper* parent,
103 SkPdfNativeTokenizer* tokenizer,
104 PdfContext* pdfContext,
105 SkCanvas* canvas)
106 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
107
108 virtual ~PdfTokenLooper() {}
109
110 virtual PdfResult consumeToken(PdfToken& token) = 0;
111 virtual void loop() = 0;
112
113 void setUp(PdfTokenLooper* parent) {
114 fParent = parent;
115 fTokenizer = parent->fTokenizer;
116 fPdfContext = parent->fPdfContext;
117 fCanvas = parent->fCanvas;
118 }
edisonn@google.com78b38b12013-07-15 18:20:58 +0000119
120 SkPdfNativeTokenizer* tokenizer() { return fTokenizer; }
edisonn@google.com222382b2013-07-10 22:33:10 +0000121};
122
123class PdfMainLooper : public PdfTokenLooper {
124public:
125 PdfMainLooper(PdfTokenLooper* parent,
126 SkPdfNativeTokenizer* tokenizer,
127 PdfContext* pdfContext,
128 SkCanvas* canvas)
129 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
130
131 virtual PdfResult consumeToken(PdfToken& token);
132 virtual void loop();
133};
134
135class PdfInlineImageLooper : public PdfTokenLooper {
136public:
137 PdfInlineImageLooper()
138 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
139
140 virtual PdfResult consumeToken(PdfToken& token);
141 virtual void loop();
142 PdfResult done();
143};
144
145class PdfCompatibilitySectionLooper : public PdfTokenLooper {
146public:
147 PdfCompatibilitySectionLooper()
148 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
149
150 virtual PdfResult consumeToken(PdfToken& token);
151 virtual void loop();
152};
153
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000154// Utilities
155static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
156 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
157
158 bitmap->allocPixels();
159 bitmap->eraseColor(color);
160}
161
162// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000163static int GetColorSpaceComponents(NotOwnedString& colorSpace) {
164 if (colorSpace.equals("DeviceCMYK")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000165 return 4;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000166 } else if (colorSpace.equals("DeviceGray") ||
167 colorSpace.equals("CalGray") ||
168 colorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000169 return 1;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000170 } else if (colorSpace.equals("DeviceRGB") ||
171 colorSpace.equals("CalRGB") ||
172 colorSpace.equals("Lab")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000173 return 3;
174 } else {
175 return 0;
176 }
177}
178
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000179SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000180 SkMatrix matrix;
181 matrix.setAll(SkDoubleToScalar(array[0]),
182 SkDoubleToScalar(array[2]),
183 SkDoubleToScalar(array[4]),
184 SkDoubleToScalar(array[1]),
185 SkDoubleToScalar(array[3]),
186 SkDoubleToScalar(array[5]),
187 SkDoubleToScalar(0),
188 SkDoubleToScalar(0),
189 SkDoubleToScalar(1));
190
191 return matrix;
192}
193
194SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
195 double array[6];
196
197 // TODO(edisonn): security issue, ret if size() != 6
198 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000199 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000200 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000201 return SkMatrix::I(); // TODO(edisonn): report issue
202 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000203 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000204 }
205
206 return SkMatrixFromPdfMatrix(array);
207}
208
edisonn@google.com222382b2013-07-10 22:33:10 +0000209
210extern "C" SkNativeParsedPDF* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000211SkBitmap* gDumpBitmap = NULL;
212SkCanvas* gDumpCanvas = NULL;
213char gLastKeyword[100] = "";
214int gLastOpKeyword = -1;
215char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
216int gReadOp = 0;
217
218
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000219#ifdef PDF_TRACE_DIFF_IN_PNG
220static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000221 return true;
222 if (*pdfOp == '\0') return false;
223
224 char markedPdfOp[100] = ",";
225 strcat(markedPdfOp, pdfOp);
226 strcat(markedPdfOp, ",");
227
228 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
229}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000230#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000231
edisonn@google.com222382b2013-07-10 22:33:10 +0000232
233
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000234// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000235static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000236 bool ret = fTokenizer->readToken(token);
237
238 gReadOp++;
239
240#ifdef PDF_TRACE_DIFF_IN_PNG
241 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
242 // the numbar and name of last operation, so the file name will reflect op that changed.
243 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
244 gDumpCanvas->flush();
245
246 SkBitmap bitmap;
247 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
248
249 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
250
251 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
252 SkCanvas canvas(device);
253
254 // draw context stuff here
255 SkPaint blueBorder;
256 blueBorder.setColor(SK_ColorBLUE);
257 blueBorder.setStyle(SkPaint::kStroke_Style);
258 blueBorder.setTextSize(SkDoubleToScalar(20));
259
260 SkString str;
261
262 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
263 if (clipStack) {
264 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
265 const SkClipStack::Element* elem;
266 double y = 0;
267 int total = 0;
268 while (elem = iter.next()) {
269 total++;
270 y += 30;
271
272 switch (elem->getType()) {
273 case SkClipStack::Element::kRect_Type:
274 canvas.drawRect(elem->getRect(), blueBorder);
275 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
276 break;
277 case SkClipStack::Element::kPath_Type:
278 canvas.drawPath(elem->getPath(), blueBorder);
279 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
280 break;
281 case SkClipStack::Element::kEmpty_Type:
282 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
283 break;
284 default:
285 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
286 break;
287 }
288 }
289
290 y += 30;
291 str.printf("Number of clips in stack: %i", total);
292 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
293 }
294
295 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
296 SkPath clipPath;
297 if (clipRegion.getBoundaryPath(&clipPath)) {
298 SkPaint redBorder;
299 redBorder.setColor(SK_ColorRED);
300 redBorder.setStyle(SkPaint::kStroke_Style);
301 canvas.drawPath(clipPath, redBorder);
302 }
303
304 canvas.flush();
305
306 SkString out;
307
308 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
309 // ... and other properties, to be able to debug th code easily
310
311 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
312 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
313 }
314
315 if (token->fType == kKeyword_TokenType) {
316 strcpy(gLastKeyword, token->fKeyword);
317 gLastOpKeyword = gReadOp;
318 } else {
319 strcpy(gLastKeyword, "");
320 }
321#endif
322
323 return ret;
324}
325
326
327
328typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
329
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000330SkTDict<PdfOperatorRenderer> gPdfOps(100);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000331
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000332
333template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
334public:
335 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
336};
337
338SkTDictWithDefaultConstructor<int> gRenderStats[kCount_PdfResult];
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000339
edisonn@google.com571c70b2013-07-10 17:09:50 +0000340const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000341 "Success",
342 "Partially implemented",
343 "Not yet implemented",
344 "Ignore Error",
345 "Error",
346 "Unsupported/Unknown"
347};
348
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000349static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000350 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000351 SkCanvas* canvas)
352{
353
354 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
355 if (skfont == NULL) {
356 skfont = SkPdfFont::Default();
357 }
358
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000359
edisonn@google.com571c70b2013-07-10 17:09:50 +0000360 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000361 // TODO(edisonn): report warning
362 return kIgnoreError_PdfResult;
363 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000364 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000365
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000366 SkUnencodedText binary(str);
367
368 SkDecodedText decoded;
369
370 if (skfont->encoding() == NULL) {
371 // TODO(edisonn): report warning
372 return kNYI_PdfResult;
373 }
374
375 skfont->encoding()->decodeText(binary, &decoded);
376
377 SkPaint paint;
378 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
379 // Or maybe just not call setTextSize at all?
380 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
381 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
382 }
383
384// if (fCurFont && fCurFont->GetFontScale() != 0) {
385// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
386// }
387
388 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
389
390 canvas->save();
391
392#if 1
393 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
394
395 SkPoint point1;
396 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
397
398 SkMatrix mirror;
399 mirror.setTranslate(0, -point1.y());
400 // TODO(edisonn): fix rotated text, and skewed too
401 mirror.postScale(SK_Scalar1, -SK_Scalar1);
402 // TODO(edisonn): post rotate, skew
403 mirror.postTranslate(0, point1.y());
404
405 matrix.postConcat(mirror);
406
407 canvas->setMatrix(matrix);
408
409 SkTraceMatrix(matrix, "mirrored");
410#endif
411
edisonn@google.com6e49c342013-06-27 20:03:43 +0000412 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000413 canvas->restore();
414
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000415 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000416}
417
418// TODO(edisonn): create header files with declarations!
419PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
420PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
421PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
422PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
423
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000424// TODO(edisonn): perf!!!
425
426static SkColorTable* getGrayColortable() {
427 static SkColorTable* grayColortable = NULL;
428 if (grayColortable == NULL) {
429 SkPMColor* colors = new SkPMColor[256];
430 for (int i = 0 ; i < 256; i++) {
431 colors[i] = SkPreMultiplyARGB(255, i, i, i);
432 }
433 grayColortable = new SkColorTable(colors, 256);
434 }
435 return grayColortable;
436}
437
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000438static SkBitmap transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000439 int width, int height, int bytesPerLine,
440 int bpc, const std::string& colorSpace,
441 bool transparencyMask) {
442 SkBitmap bitmap;
443
edisonn@google.com571c70b2013-07-10 17:09:50 +0000444 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000445//#define MAX_COMPONENTS 10
446
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000447 // TODO(edisonn): assume start of lines are aligned at 32 bits?
448 // Is there a faster way to load the uncompressed stream into a bitmap?
449
450 // minimal support for now
451 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
452 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
453
454 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000455 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000456 for (int w = 0 ; w < width; w++) {
457 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
458 uncompressedStream[3 * w + 1],
459 uncompressedStream[3 * w + 2]);
460 i++;
461 }
462 uncompressedStream += bytesPerLine;
463 }
464
465 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
466 bitmap.setPixels(uncompressedStreamArgb);
467 }
468 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
469 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
470
471 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000472 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000473 for (int w = 0 ; w < width; w++) {
474 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
475 uncompressedStream[w];
476 i++;
477 }
478 uncompressedStream += bytesPerLine;
479 }
480
481 bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
482 width, height);
483 bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
484 }
485
486 // TODO(edisonn): Report Warning, NYI, or error
487 return bitmap;
488}
489
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000490// utils
491
492// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
493// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
494// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
495// skia format, through a table
496
497// this functions returns the image, it does not look at the smask.
498
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000499static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000500 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000501 // TODO(edisonn): report warning to be used in testing.
502 return SkBitmap();
503 }
504
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000505 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
506 int64_t width = image->Width(pdfContext->fPdfDoc);
507 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000508 std::string colorSpace = "DeviceRGB";
509
510 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000511 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
512 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000513 }
514
515/*
516 bool imageMask = image->imageMask();
517
518 if (imageMask) {
519 if (bpc != 0 && bpc != 1) {
520 // TODO(edisonn): report warning to be used in testing.
521 return SkBitmap();
522 }
523 bpc = 1;
524 }
525*/
526
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000527 const unsigned char* uncompressedStream = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000528 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000529
edisonn@google.com571c70b2013-07-10 17:09:50 +0000530 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000531
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000532 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000533 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000534 // TODO(edisonn): report warning to be used in testing.
535 return SkBitmap();
536 }
537
edisonn@google.com571c70b2013-07-10 17:09:50 +0000538 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
539
540 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
541 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
542 (streamDict->isFilterAArray(NULL) &&
543 streamDict->getFilterAsArray(NULL)->size() > 0 &&
544 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
545 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
546 SkBitmap bitmap;
547 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap);
548 return bitmap;
549 }
550
551
552
553 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
554// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
555// obj.GetDictionary().GetKey(PdfName("Filter")));
556// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
557// value = resolveReferenceObject(pdfContext->fPdfDoc,
558// &value->GetArray()[0]);
559// }
560// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
561// SkStream stream = SkStream::
562// SkImageDecoder::Factory()
563// }
564
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000565 int bytesPerLine = (int)(uncompressedStreamLength / height);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000566#ifdef PDF_TRACE
567 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000568 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000569 }
570#endif
571
572 SkBitmap bitmap = transferImageStreamToBitmap(
573 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000574 (int)width, (int)height, bytesPerLine,
575 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000576 transparencyMask);
577
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000578 return bitmap;
579}
580
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000581static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000582 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000583
584 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000585 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000586 }
587
588 // TODO(edisonn): implement GS SMask. Default to empty right now.
589 return pdfContext->fGraphicsState.fSMask;
590}
591
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000592static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000593 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000594 return kIgnoreError_PdfResult;
595 }
596
597 SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false);
598 SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage);
599
600 canvas->save();
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000601 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000602
603#if 1
604 SkScalar z = SkIntToScalar(0);
605 SkScalar one = SkIntToScalar(1);
606
607 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
608 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
609 SkMatrix flip;
610 SkAssertResult(flip.setPolyToPoly(from, to, 4));
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000611 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000612 solveImageFlip.preConcat(flip);
613 canvas->setMatrix(solveImageFlip);
614#endif
615
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000616 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
617
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000618 // TODO(edisonn): soft mask type? alpha/luminosity.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000619 if (sMask.empty()) {
620 canvas->drawBitmapRect(image, dst, NULL);
621 } else {
622 canvas->saveLayer(&dst, NULL);
623 canvas->drawBitmapRect(image, dst, NULL);
624 SkPaint xfer;
625 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000626 // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf?
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000627 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
628 canvas->drawBitmapRect(sMask, dst, &xfer);
629 canvas->restore();
630 }
631
632 canvas->restore();
633
634 return kPartial_PdfResult;
635}
636
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000637static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000638 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000639 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000640 }
641
642 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000643
644
645
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000646 canvas->save();
647
648
edisonn@google.com571c70b2013-07-10 17:09:50 +0000649 if (skobj->Resources(pdfContext->fPdfDoc)) {
650 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000651 }
652
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000653 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000654
edisonn@google.com571c70b2013-07-10 17:09:50 +0000655 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000656 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
657 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
658 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000659 // TODO(edisonn) reset matrixTm and matricTlm also?
660 }
661
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000662 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000663
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000664 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000665
edisonn@google.com571c70b2013-07-10 17:09:50 +0000666 if (skobj->has_BBox()) {
667 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000668 }
669
670 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
671 // For this PdfContentsTokenizer needs to be extended.
672
edisonn@google.come878e722013-07-29 19:10:58 +0000673 // This is a group?
674 if (skobj->has_Group()) {
675 //TransparencyGroupDictionary* ...
676 }
677
edisonn@google.com571c70b2013-07-10 17:09:50 +0000678 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000679
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000680 SkPdfNativeTokenizer* tokenizer =
681 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000682 if (tokenizer != NULL) {
683 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
684 looper.loop();
685 delete tokenizer;
686 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000687
688 // TODO(edisonn): should we restore the variable stack at the same state?
689 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
690 canvas->restore();
691 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000692 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000693}
694
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000695//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
696// return kNYI_PdfResult;
697//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000698
edisonn@google.com571c70b2013-07-10 17:09:50 +0000699PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
700 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000701 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000702 }
703
704 PdfOp_q(pdfContext, canvas, NULL);
705 canvas->save();
706
707 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
708 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
709
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000710 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
711 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000712
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000713 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000714
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000715 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000716
717 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000718 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000719
720 SkTraceRect(rm, "bbox mapped");
721
722 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
723
724 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
725 // For this PdfContentsTokenizer needs to be extended.
726
edisonn@google.com571c70b2013-07-10 17:09:50 +0000727 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000728
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000729 SkPdfNativeTokenizer* tokenizer =
730 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000731 if (tokenizer != NULL) {
732 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
733 looper.loop();
734 delete tokenizer;
735 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000736
737 // TODO(edisonn): should we restore the variable stack at the same state?
738 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
739 canvas->restore();
740 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000741
742 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000743}
744
745
edisonn@google.com571c70b2013-07-10 17:09:50 +0000746// TODO(edisonn): make sure the pointer is unique
747std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000748
749class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000750 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000751public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000752 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000753 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000754 }
755
756 ~CheckRecursiveRendering() {
757 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000758 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000759 }
760
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000761 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000762 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000763 }
764};
765
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000766static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000767 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000768 // Oops, corrupt PDF!
769 return kIgnoreError_PdfResult;
770 }
771
edisonn@google.com571c70b2013-07-10 17:09:50 +0000772 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000773
edisonn@google.com571c70b2013-07-10 17:09:50 +0000774 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000775 {
776 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000777 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000778 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000779 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000780 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
781 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000782 default:
783 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000784 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000785}
786
787PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
788 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
789 canvas->save();
790 return kOK_PdfResult;
791}
792
793PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
794 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
795 pdfContext->fStateStack.pop();
796 canvas->restore();
797 return kOK_PdfResult;
798}
799
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000800static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000801 double array[6];
802 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000803 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000804 pdfContext->fObjectStack.pop();
805 }
806
807 // a b
808 // c d
809 // e f
810
811 // 0 1
812 // 2 3
813 // 4 5
814
815 // sx ky
816 // kx sy
817 // tx ty
818 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
819
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000820 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000821
822#ifdef PDF_TRACE
823 printf("cm ");
824 for (int i = 0 ; i < 6 ; i++) {
825 printf("%f ", array[i]);
826 }
827 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000828 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000829#endif
830
831 return kOK_PdfResult;
832}
833
834//leading TL Set the text leading, Tl
835//, to leading, which is a number expressed in unscaled text
836//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000837static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000838 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000839
840 pdfContext->fGraphicsState.fTextLeading = ty;
841
842 return kOK_PdfResult;
843}
844
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000845static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000846#ifdef PDF_TRACE
847 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
848#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +0000849 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000850 SkPdfObject* obj = pdfContext->fObjectStack.top();
851 obj = obj;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000852 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000853
854 double array[6] = {1, 0, 0, 1, tx, ty};
855 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
856
857 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
858 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
859
860 return kPartial_PdfResult;
861}
862
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000863static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000864 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
865 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000866
edisonn@google.com571c70b2013-07-10 17:09:50 +0000867 // TODO(edisonn): Create factory methods or constructors so native is hidden
868 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000869 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000870
871 PdfOp_TL(pdfContext, canvas, looper);
872
edisonn@google.com571c70b2013-07-10 17:09:50 +0000873 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000874 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000875
edisonn@google.com571c70b2013-07-10 17:09:50 +0000876 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000877 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000878
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000879 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
880
881 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
882 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000883
884 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000885}
886
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000887static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000888 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
889 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
890 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
891 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
892 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
893 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000894
895 double array[6];
896 array[0] = a;
897 array[1] = b;
898 array[2] = c;
899 array[3] = d;
900 array[4] = e;
901 array[5] = f;
902
903 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000904 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000905
906 // TODO(edisonn): Text positioning.
907 pdfContext->fGraphicsState.fMatrixTm = matrix;
908 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
909
910 return kPartial_PdfResult;
911}
912
913//— T* Move to the start of the next line. This operator has the same effect as the code
914//0 Tl Td
915//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000916static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000917 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
918 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000919
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000920 pdfContext->fObjectStack.push(zero);
921 pdfContext->fObjectStack.push(tl);
922
923 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
924
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000925 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000926}
927
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000928static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000929 if (pdfContext->fGraphicsState.fPathClosed) {
930 pdfContext->fGraphicsState.fPath.reset();
931 pdfContext->fGraphicsState.fPathClosed = false;
932 }
933
edisonn@google.com571c70b2013-07-10 17:09:50 +0000934 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
935 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000936
937 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
938 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
939
940 return kOK_PdfResult;
941}
942
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000943static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000944 if (pdfContext->fGraphicsState.fPathClosed) {
945 pdfContext->fGraphicsState.fPath.reset();
946 pdfContext->fGraphicsState.fPathClosed = false;
947 }
948
edisonn@google.com571c70b2013-07-10 17:09:50 +0000949 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
950 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000951
952 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
953 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
954
955 return kOK_PdfResult;
956}
957
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000958static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000959 if (pdfContext->fGraphicsState.fPathClosed) {
960 pdfContext->fGraphicsState.fPath.reset();
961 pdfContext->fGraphicsState.fPathClosed = false;
962 }
963
edisonn@google.com571c70b2013-07-10 17:09:50 +0000964 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
965 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
966 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
967 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
968 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
969 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000970
971 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
972 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
973 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
974
975 pdfContext->fGraphicsState.fCurPosX = x3;
976 pdfContext->fGraphicsState.fCurPosY = y3;
977
978 return kOK_PdfResult;
979}
980
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000981static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000982 if (pdfContext->fGraphicsState.fPathClosed) {
983 pdfContext->fGraphicsState.fPath.reset();
984 pdfContext->fGraphicsState.fPathClosed = false;
985 }
986
edisonn@google.com571c70b2013-07-10 17:09:50 +0000987 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
988 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
989 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
990 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000991 double y1 = pdfContext->fGraphicsState.fCurPosY;
992 double x1 = pdfContext->fGraphicsState.fCurPosX;
993
994 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
995 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
996 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
997
998 pdfContext->fGraphicsState.fCurPosX = x3;
999 pdfContext->fGraphicsState.fCurPosY = y3;
1000
1001 return kOK_PdfResult;
1002}
1003
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001004static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001005 if (pdfContext->fGraphicsState.fPathClosed) {
1006 pdfContext->fGraphicsState.fPath.reset();
1007 pdfContext->fGraphicsState.fPathClosed = false;
1008 }
1009
edisonn@google.com571c70b2013-07-10 17:09:50 +00001010 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1011 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001012 double y2 = pdfContext->fGraphicsState.fCurPosY;
1013 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001014 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1015 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001016
1017 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1018 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1019 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1020
1021 pdfContext->fGraphicsState.fCurPosX = x3;
1022 pdfContext->fGraphicsState.fCurPosY = y3;
1023
1024 return kOK_PdfResult;
1025}
1026
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001027static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001028 if (pdfContext->fGraphicsState.fPathClosed) {
1029 pdfContext->fGraphicsState.fPath.reset();
1030 pdfContext->fGraphicsState.fPathClosed = false;
1031 }
1032
edisonn@google.com571c70b2013-07-10 17:09:50 +00001033 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1034 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1035 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1036 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001037
1038 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1039 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1040
1041 pdfContext->fGraphicsState.fCurPosX = x;
1042 pdfContext->fGraphicsState.fCurPosY = y + height;
1043
1044 return kOK_PdfResult;
1045}
1046
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001047static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001048 pdfContext->fGraphicsState.fPath.close();
1049 return kOK_PdfResult;
1050}
1051
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001052static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001053 SkPath path = pdfContext->fGraphicsState.fPath;
1054
1055 if (close) {
1056 path.close();
1057 }
1058
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001059 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001060
1061 SkPaint paint;
1062
1063 SkPoint line[2];
1064 if (fill && !stroke && path.isLine(line)) {
1065 paint.setStyle(SkPaint::kStroke_Style);
1066
1067 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1068 paint.setStrokeWidth(SkDoubleToScalar(0));
1069
1070 canvas->drawPath(path, paint);
1071 } else {
1072 if (fill) {
1073 paint.setStyle(SkPaint::kFill_Style);
1074 if (evenOdd) {
1075 path.setFillType(SkPath::kEvenOdd_FillType);
1076 }
1077
1078 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1079
1080 canvas->drawPath(path, paint);
1081 }
1082
1083 if (stroke) {
1084 paint.setStyle(SkPaint::kStroke_Style);
1085
1086 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1087
1088 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1089 canvas->drawPath(path, paint);
1090 }
1091 }
1092
1093 pdfContext->fGraphicsState.fPath.reset();
1094 // todo zoom ... other stuff ?
1095
1096 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1097#ifndef PDF_DEBUG_NO_CLIPING
1098 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1099#endif
1100 }
1101
1102 //pdfContext->fGraphicsState.fClipPath.reset();
1103 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1104
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001105 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001106
1107}
1108
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001109static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001110 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1111}
1112
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001113static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001114 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1115}
1116
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001117static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001118 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1119}
1120
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001121static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001122 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1123}
1124
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001125static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001126 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1127}
1128
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001129static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001130 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1131}
1132
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001133static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001134 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1135}
1136
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001137static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001138 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1139}
1140
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001141static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001142 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1143}
1144
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001145static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001146 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001147 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1148#ifndef PDF_DEBUG_NO_CLIPING
1149 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1150#endif
1151 }
1152
1153 //pdfContext->fGraphicsState.fClipPath.reset();
1154 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1155
1156 pdfContext->fGraphicsState.fPathClosed = true;
1157
1158 return kOK_PdfResult;
1159}
1160
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001161static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001162 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001163 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
1164 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001165
1166 return kPartial_PdfResult;
1167}
1168
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001169static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001170 if (!pdfContext->fGraphicsState.fTextBlock) {
1171 return kIgnoreError_PdfResult;
1172 }
1173 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001174 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001175}
1176
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001177PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001178#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001179 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001180#endif
1181
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001182 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1183 // TODO(edisonn): try to recover and draw it any way?
1184 return kIgnoreError_PdfResult;
1185 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001186
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001187 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1188 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1189 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1190 // TODO(edisonn): try to recover and draw it any way?
1191 return kIgnoreError_PdfResult;
1192 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001193
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001194 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1195
1196 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1197
1198 if (skfont) {
1199 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001200 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001201 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001202 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001203}
1204
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001205//font size Tf Set the text font, Tf
1206//, to font and the text font size, Tfs, to size. font is the name of a
1207//font resource in the Fontsubdictionary of the current resource dictionary; size is
1208//a number representing a scale factor. There is no initial value for either font or
1209//size; they must be specified explicitly using Tf before any text is shown.
1210static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1211 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1212 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1213 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1214}
1215
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001216static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001217 if (!pdfContext->fGraphicsState.fTextBlock) {
1218 // TODO(edisonn): try to recover and draw it any way?
1219 return kIgnoreError_PdfResult;
1220 }
1221
1222 PdfResult ret = DrawText(pdfContext,
1223 pdfContext->fObjectStack.top(),
1224 canvas);
1225 pdfContext->fObjectStack.pop();
1226
1227 return ret;
1228}
1229
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001230static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001231 if (!pdfContext->fGraphicsState.fTextBlock) {
1232 // TODO(edisonn): try to recover and draw it any way?
1233 return kIgnoreError_PdfResult;
1234 }
1235
1236 PdfOp_T_star(pdfContext, canvas, looper);
1237 // Do not pop, and push, just transfer the param to Tj
1238 return PdfOp_Tj(pdfContext, canvas, looper);
1239}
1240
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001241static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001242 if (!pdfContext->fGraphicsState.fTextBlock) {
1243 // TODO(edisonn): try to recover and draw it any way?
1244 return kIgnoreError_PdfResult;
1245 }
1246
1247 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1248 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1249 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1250
1251 pdfContext->fObjectStack.push(aw);
1252 PdfOp_Tw(pdfContext, canvas, looper);
1253
1254 pdfContext->fObjectStack.push(ac);
1255 PdfOp_Tc(pdfContext, canvas, looper);
1256
1257 pdfContext->fObjectStack.push(str);
1258 PdfOp_quote(pdfContext, canvas, looper);
1259
1260 return kPartial_PdfResult;
1261}
1262
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001263static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001264 if (!pdfContext->fGraphicsState.fTextBlock) {
1265 // TODO(edisonn): try to recover and draw it any way?
1266 return kIgnoreError_PdfResult;
1267 }
1268
edisonn@google.com571c70b2013-07-10 17:09:50 +00001269 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001270 pdfContext->fObjectStack.pop();
1271
edisonn@google.com571c70b2013-07-10 17:09:50 +00001272 if (!array->isArray()) {
1273 return kIgnoreError_PdfResult;
1274 }
1275
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001276 for( int i=0; i<static_cast<int>(array->size()); i++ )
1277 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001278 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001279 SkPdfObject* obj = (*array)[i];
1280 DrawText(pdfContext,
1281 obj,
1282 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001283 } else if ((*array)[i]->isNumber()) {
1284 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001285 SkMatrix matrix;
1286 matrix.setAll(SkDoubleToScalar(1),
1287 SkDoubleToScalar(0),
1288 // TODO(edisonn): use writing mode, vertical/horizontal.
1289 SkDoubleToScalar(-dx), // amount is substracted!!!
1290 SkDoubleToScalar(0),
1291 SkDoubleToScalar(1),
1292 SkDoubleToScalar(0),
1293 SkDoubleToScalar(0),
1294 SkDoubleToScalar(0),
1295 SkDoubleToScalar(1));
1296
1297 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1298 }
1299 }
1300 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1301}
1302
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001303static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001304 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001305 return kOK_PdfResult;
1306}
1307
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001308static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001309 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1310}
1311
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001312static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001313 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1314}
1315
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001316static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001317 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001318// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001319
1320 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1321
1322 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001323 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001324 doubles = false;
1325 }
1326
1327#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001328 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001329#endif
1330
1331 for (int i = n - 1; i >= 0 ; i--) {
1332 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001333 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1334// } else {
1335// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001336 }
1337 }
1338
1339 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001340 // TODO(edisonn): do possible field values to enum at parsing time!
1341 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001342 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001343 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001344 }
1345 return kPartial_PdfResult;
1346}
1347
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001348static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001349 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1350}
1351
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001352static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001353 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1354}
1355
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001356static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001357 //SkPdfString* name;
1358 if (pdfContext->fObjectStack.top()->isName()) {
1359 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001360 pdfContext->fObjectStack.pop();
1361 }
1362
1363 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1364 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1365
1366 return kPartial_PdfResult;
1367}
1368
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001369static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001370 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1371}
1372
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001373static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001374 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1375}
1376
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001377static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001378 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001379 return kNYI_PdfResult;
1380}
1381
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001382static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001383 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1384}
1385
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001386static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001387 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1388}
1389
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001390static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001391 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1392 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1393 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001394
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001395 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001396 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001397 return kOK_PdfResult;
1398}
1399
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001400static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001401 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1402}
1403
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001404static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001405 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1406}
1407
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001408static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001409 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001410 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1411 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1412 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1413 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001414
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001415 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001416 // TODO(edisonn): Set color.
1417 return kNYI_PdfResult;
1418}
1419
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001420static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001421 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1422}
1423
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001424static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001425 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1426}
1427
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001428static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001429 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1430 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1431
1432 return kOK_PdfResult;
1433}
1434
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001435static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001436 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1437
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1439 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1440
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001441 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001442}
1443
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001444static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001445 *looper = new PdfCompatibilitySectionLooper();
1446 return kOK_PdfResult;
1447}
1448
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001449static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001450#ifdef ASSERT_BAD_PDF_OPS
1451 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1452 // have the assert when testing good pdfs.
1453#endif
1454 return kIgnoreError_PdfResult;
1455}
1456
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001457static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001458 *looper = new PdfInlineImageLooper();
1459 return kOK_PdfResult;
1460}
1461
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001462static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001463#ifdef ASSERT_BAD_PDF_OPS
1464 SkASSERT(false); // must be processed in inline image looper, but let's
1465 // have the assert when testing good pdfs.
1466#endif
1467 return kIgnoreError_PdfResult;
1468}
1469
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001470static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001471#ifdef ASSERT_BAD_PDF_OPS
1472 SkASSERT(false); // must be processed in inline image looper, but let's
1473 // have the assert when testing good pdfs.
1474#endif
1475 return kIgnoreError_PdfResult;
1476}
1477
1478//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001479static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001480 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001481 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1482
1483 return kOK_PdfResult;
1484}
1485
1486//lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001487static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001488 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001489 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001490
1491 return kNYI_PdfResult;
1492}
1493
1494//lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001495static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001496 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001497 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001498
1499 return kNYI_PdfResult;
1500}
1501
1502//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001503static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001504 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001505 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001506
1507 return kNYI_PdfResult;
1508}
1509
1510//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1511//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001512static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001513 pdfContext->fObjectStack.pop();
1514 pdfContext->fObjectStack.pop();
1515
1516 return kNYI_PdfResult;
1517}
1518
1519//intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001520static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001521 pdfContext->fObjectStack.pop();
1522
1523 return kNYI_PdfResult;
1524}
1525
1526//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1527//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1528//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001529static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001530 pdfContext->fObjectStack.pop();
1531
1532 return kNYI_PdfResult;
1533}
1534
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001535
1536// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1537void skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1538 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1539}
1540
1541void skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1542 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1543}
1544
1545void skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
1546 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1547}
1548
1549void skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1550 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1551}
1552
1553void skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1554 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1555}
1556
1557void skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1558 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1559}
1560
1561void skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1562 // TODO(edisonn): verify input
1563 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1564 // TODO(edisonn): report error/warning
1565 return;
1566 }
1567
1568 SkPdfArray* intervals = (SkPdfArray*)dash->objAtAIndex(0);
1569 int cnt = intervals->size();
1570 if (cnt >= 256) {
1571 // TODO(edisonn): report error/warning, unsuported;
1572 // TODO(edisonn): alloc memory
1573 return;
1574 }
1575 for (int i = 0; i < cnt; i++) {
1576 if (!intervals->objAtAIndex(i)->isNumber()) {
1577 // TODO(edisonn): report error/warning
1578 return;
1579 }
1580 }
1581
1582 pdfContext->fGraphicsState.fDashPhase = dash->objAtAIndex(1)->scalarValue();
1583 pdfContext->fGraphicsState.fDashArrayLength = cnt;
1584 for (int i = 0 ; i < cnt; i++) {
1585 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1586 }
1587}
1588
1589void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1590 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1591 // TODO(edisonn): report error/warning
1592 return;
1593 }
1594 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1595}
1596
edisonn@google.come878e722013-07-29 19:10:58 +00001597SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1598
1599class InitBlendModes {
1600public:
1601 InitBlendModes() {
1602 // TODO(edisonn): use the python code generator?
1603 // TABLE 7.2 Standard separable blend modes
1604 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1605 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1606 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1607 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1608 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1609 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1610 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1611 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1612 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1613 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1614 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1615 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1616
1617 // TABLE 7.3 Standard nonseparable blend modes
1618 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1619 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1620 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1621 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1622 }
1623};
1624
1625InitBlendModes _gDummyInniter;
1626
1627SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1628 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1629 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1630 return mode;
1631 }
1632
1633 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1634}
1635
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001636void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001637 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1638 if (mode <= SkXfermode::kLastMode) {
1639 pdfContext->fGraphicsState.fBlendModesLength = 1;
1640 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1641 } else {
1642 // TODO(edisonn): report unknown blend mode
1643 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001644}
1645
1646void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001647 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
1648 // TODO(edisonn): report error/warning
1649 return;
1650 }
1651 SkXfermode::Mode modes[256];
1652 int cnt = blendModes->size();
1653 for (int i = 0; i < cnt; i++) {
1654 SkPdfObject* name = blendModes->objAtAIndex(i);
1655 if (!name->isName()) {
1656 // TODO(edisonn): report error/warning
1657 return;
1658 }
1659 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
1660 if (mode > SkXfermode::kLastMode) {
1661 // TODO(edisonn): report error/warning
1662 return;
1663 }
1664 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001665
edisonn@google.come878e722013-07-29 19:10:58 +00001666 pdfContext->fGraphicsState.fBlendModesLength = cnt;
1667 for (int i = 0; i < cnt; i++) {
1668 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
1669 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001670}
1671
1672void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
1673 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00001674 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001675 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00001676 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
1677 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
1678 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
1679 } else {
1680 // TODO (edisonn): report error/warning
1681 }
1682}
1683
1684void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001685 if (sMask == "None") {
1686 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
1687 pdfContext->fGraphicsState.fSMask = SkBitmap();
1688 return;
1689 }
1690
edisonn@google.come878e722013-07-29 19:10:58 +00001691 //Next, get the ExtGState Dictionary from the Resource Dictionary:
1692 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
1693
1694 if (extGStateDictionary == NULL) {
1695#ifdef PDF_TRACE
1696 printf("ExtGState is NULL!\n");
1697#endif
1698 // TODO (edisonn): report error/warning
1699 return;
1700 }
1701
1702 SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
1703 if (!obj || !obj->isDictionary()) {
1704 // TODO (edisonn): report error/warning
1705 return;
1706 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001707
1708 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
1709 pdfContext->fGraphicsState.fSMask = SkBitmap();
1710
edisonn@google.come878e722013-07-29 19:10:58 +00001711 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001712}
1713
1714void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
1715 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
1716}
1717
1718
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001719//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1720//the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001721static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001722 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001723
1724#ifdef PDF_TRACE
1725 std::string str;
1726#endif
1727
1728 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001729 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001730
1731 if (extGStateDictionary == NULL) {
1732#ifdef PDF_TRACE
1733 printf("ExtGState is NULL!\n");
1734#endif
1735 return kIgnoreError_PdfResult;
1736 }
1737
edisonn@google.com571c70b2013-07-10 17:09:50 +00001738 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001739
edisonn@google.com571c70b2013-07-10 17:09:50 +00001740 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1741 return kIgnoreError_PdfResult;
1742 }
1743 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001744
1745 // TODO(edisonn): now load all those properties in graphic state.
1746 if (gs == NULL) {
1747 return kIgnoreError_PdfResult;
1748 }
1749
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001750 if (gs->has_LW()) {
1751 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
1752 }
1753
1754 if (gs->has_LC()) {
1755 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
1756 }
1757
1758 if (gs->has_LJ()) {
1759 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
1760 }
1761
1762 if (gs->has_ML()) {
1763 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
1764 }
1765
1766 if (gs->has_D()) {
1767 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
1768 }
1769
1770 if (gs->has_Font()) {
1771 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
1772 }
1773
1774 if (gs->has_BM()) {
1775 if (gs->isBMAName(pdfContext->fPdfDoc)) {
1776 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
1777 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
1778 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
1779 } else {
1780 // TODO(edisonn): report/warn
1781 }
1782 }
1783
1784 if (gs->has_SMask()) {
1785 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
1786 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
1787 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
1788 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
1789 } else {
1790 // TODO(edisonn): report/warn
1791 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001792 }
1793
1794 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001795 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001796 }
1797
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001798 if (gs->has_CA()) {
1799 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
1800 }
1801
1802 if (gs->has_AIS()) {
1803 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001804 }
1805
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001806 return kNYI_PdfResult;
1807}
1808
1809//charSpace Tc Set the character spacing, Tc
1810//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1811//Initial value: 0.
1812PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001813 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001814 pdfContext->fGraphicsState.fCharSpace = charSpace;
1815
1816 return kOK_PdfResult;
1817}
1818
1819//wordSpace Tw Set the word spacing, T
1820//w
1821//, to wordSpace, which is a number expressed in unscaled
1822//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1823//value: 0.
1824PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001825 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001826 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1827
1828 return kOK_PdfResult;
1829}
1830
1831//scale Tz Set the horizontal scaling, Th
1832//, to (scale ˜ 100). scale is a number specifying the
1833//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001834static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001835 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001836
1837 return kNYI_PdfResult;
1838}
1839
1840//render Tr Set the text rendering mode, T
1841//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001842static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001843 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001844
1845 return kNYI_PdfResult;
1846}
1847//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1848//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001849static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001850 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001851
1852 return kNYI_PdfResult;
1853}
1854
1855//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001856static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001857 pdfContext->fObjectStack.pop();
1858 pdfContext->fObjectStack.pop();
1859
1860 return kNYI_PdfResult;
1861}
1862
1863//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001864static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001865 pdfContext->fObjectStack.pop();
1866 pdfContext->fObjectStack.pop();
1867 pdfContext->fObjectStack.pop();
1868 pdfContext->fObjectStack.pop();
1869 pdfContext->fObjectStack.pop();
1870 pdfContext->fObjectStack.pop();
1871
1872 return kNYI_PdfResult;
1873}
1874
1875//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001876static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001877 pdfContext->fObjectStack.pop();
1878
1879 return kNYI_PdfResult;
1880}
1881
1882//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001883static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001884 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001885
edisonn@google.com571c70b2013-07-10 17:09:50 +00001886 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001887
1888 if (xObject == NULL) {
1889#ifdef PDF_TRACE
1890 printf("XObject is NULL!\n");
1891#endif
1892 return kIgnoreError_PdfResult;
1893 }
1894
edisonn@google.com571c70b2013-07-10 17:09:50 +00001895 SkPdfObject* value = xObject->get(name);
1896 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001897
1898#ifdef PDF_TRACE
1899// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001900// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001901#endif
1902
edisonn@google.com571c70b2013-07-10 17:09:50 +00001903 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001904}
1905
1906//tag MP Designate a marked-content point. tag is a name object indicating the role or
1907//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001908static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001909 pdfContext->fObjectStack.pop();
1910
1911 return kNYI_PdfResult;
1912}
1913
1914//tag properties DP Designate a marked-content point with an associated property list. tag is a
1915//name object indicating the role or significance of the point; properties is
1916//either an inline dictionary containing the property list or a name object
1917//associated with it in the Properties subdictionary of the current resource
1918//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001919static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001920 pdfContext->fObjectStack.pop();
1921 pdfContext->fObjectStack.pop();
1922
1923 return kNYI_PdfResult;
1924}
1925
1926//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1927//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001928static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001929 pdfContext->fObjectStack.pop();
1930
1931 return kNYI_PdfResult;
1932}
1933
1934//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1935//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1936//property list or a name object associated with it in the Properties subdictionary of the current resource dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001937static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001938 pdfContext->fObjectStack.pop();
1939 pdfContext->fObjectStack.pop();
1940
1941 return kNYI_PdfResult;
1942}
1943
1944//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001945static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001946 return kNYI_PdfResult;
1947}
1948
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001949static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001950 static bool gInitialized = false;
1951 if (gInitialized) {
1952 return;
1953 }
1954
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001955 gPdfOps.set("q", PdfOp_q);
1956 gPdfOps.set("Q", PdfOp_Q);
1957 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001958
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001959 gPdfOps.set("TD", PdfOp_TD);
1960 gPdfOps.set("Td", PdfOp_Td);
1961 gPdfOps.set("Tm", PdfOp_Tm);
1962 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001963
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001964 gPdfOps.set("m", PdfOp_m);
1965 gPdfOps.set("l", PdfOp_l);
1966 gPdfOps.set("c", PdfOp_c);
1967 gPdfOps.set("v", PdfOp_v);
1968 gPdfOps.set("y", PdfOp_y);
1969 gPdfOps.set("h", PdfOp_h);
1970 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001971
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001972 gPdfOps.set("S", PdfOp_S);
1973 gPdfOps.set("s", PdfOp_s);
1974 gPdfOps.set("f", PdfOp_f);
1975 gPdfOps.set("F", PdfOp_F);
1976 gPdfOps.set("f*", PdfOp_f_star);
1977 gPdfOps.set("B", PdfOp_B);
1978 gPdfOps.set("B*", PdfOp_B_star);
1979 gPdfOps.set("b", PdfOp_b);
1980 gPdfOps.set("b*", PdfOp_b_star);
1981 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001982
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001983 gPdfOps.set("BT", PdfOp_BT);
1984 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001985
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001986 gPdfOps.set("Tj", PdfOp_Tj);
1987 gPdfOps.set("'", PdfOp_quote);
1988 gPdfOps.set("\"", PdfOp_doublequote);
1989 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001990
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001991 gPdfOps.set("CS", PdfOp_CS);
1992 gPdfOps.set("cs", PdfOp_cs);
1993 gPdfOps.set("SC", PdfOp_SC);
1994 gPdfOps.set("SCN", PdfOp_SCN);
1995 gPdfOps.set("sc", PdfOp_sc);
1996 gPdfOps.set("scn", PdfOp_scn);
1997 gPdfOps.set("G", PdfOp_G);
1998 gPdfOps.set("g", PdfOp_g);
1999 gPdfOps.set("RG", PdfOp_RG);
2000 gPdfOps.set("rg", PdfOp_rg);
2001 gPdfOps.set("K", PdfOp_K);
2002 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002003
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002004 gPdfOps.set("W", PdfOp_W);
2005 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002006
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002007 gPdfOps.set("BX", PdfOp_BX);
2008 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002009
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002010 gPdfOps.set("BI", PdfOp_BI);
2011 gPdfOps.set("ID", PdfOp_ID);
2012 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002013
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002014 gPdfOps.set("w", PdfOp_w);
2015 gPdfOps.set("J", PdfOp_J);
2016 gPdfOps.set("j", PdfOp_j);
2017 gPdfOps.set("M", PdfOp_M);
2018 gPdfOps.set("d", PdfOp_d);
2019 gPdfOps.set("ri", PdfOp_ri);
2020 gPdfOps.set("i", PdfOp_i);
2021 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002022
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002023 gPdfOps.set("Tc", PdfOp_Tc);
2024 gPdfOps.set("Tw", PdfOp_Tw);
2025 gPdfOps.set("Tz", PdfOp_Tz);
2026 gPdfOps.set("TL", PdfOp_TL);
2027 gPdfOps.set("Tf", PdfOp_Tf);
2028 gPdfOps.set("Tr", PdfOp_Tr);
2029 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002030
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002031 gPdfOps.set("d0", PdfOp_d0);
2032 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002033
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002034 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002035
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002036 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002037
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002038 gPdfOps.set("MP", PdfOp_MP);
2039 gPdfOps.set("DP", PdfOp_DP);
2040 gPdfOps.set("BMC", PdfOp_BMC);
2041 gPdfOps.set("BDC", PdfOp_BDC);
2042 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002043
2044 gInitialized = true;
2045}
2046
2047class InitPdfOps {
2048public:
2049 InitPdfOps() {
2050 initPdfOperatorRenderes();
2051 }
2052};
2053
2054InitPdfOps gInitPdfOps;
2055
2056void reportPdfRenderStats() {
2057 std::map<std::string, int>::iterator iter;
2058
2059 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002060 SkTDict<int>::Iter iter(gRenderStats[i]);
2061 const char* key;
2062 int value = 0;
2063 while ((key = iter.next(&value)) != NULL) {
2064 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002065 }
2066 }
2067}
2068
2069PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002070 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002071 {
2072 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002073 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002074 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002075 // caller, main work is done by pdfOperatorRenderer(...)
2076 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002077 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
2078
2079 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002080 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2081 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002082
2083 if (childLooper) {
2084 childLooper->setUp(this);
2085 childLooper->loop();
2086 delete childLooper;
2087 }
2088 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002089 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002090 gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2091 gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002092 }
2093 }
2094 else if (token.fType == kObject_TokenType)
2095 {
2096 fPdfContext->fObjectStack.push( token.fObject );
2097 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002098 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002099 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002100 return kIgnoreError_PdfResult;
2101 }
2102 return kOK_PdfResult;
2103}
2104
2105void PdfMainLooper::loop() {
2106 PdfToken token;
2107 while (readToken(fTokenizer, &token)) {
2108 consumeToken(token);
2109 }
2110}
2111
2112PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002113 SkASSERT(false);
2114 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002115}
2116
2117void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002118 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002119}
2120
2121PdfResult PdfInlineImageLooper::done() {
2122
2123 // TODO(edisonn): long to short names
2124 // TODO(edisonn): set properties in a map
2125 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
2126 // the stream.
2127
2128 SkBitmap bitmap;
2129 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
2130
2131 // TODO(edisonn): matrix use.
2132 // Draw dummy red square, to show the prezence of the inline image.
2133 fCanvas->drawBitmap(bitmap,
2134 SkDoubleToScalar(0),
2135 SkDoubleToScalar(0),
2136 NULL);
2137 return kNYI_PdfResult;
2138}
2139
2140PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2141 return fParent->consumeToken(token);
2142}
2143
2144void PdfCompatibilitySectionLooper::loop() {
2145 // TODO(edisonn): save stacks position, or create a new stack?
2146 // TODO(edisonn): what happens if we pop out more variables then when we started?
2147 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2148 // pop-ing too much will not affect outside the section.
2149 PdfToken token;
2150 while (readToken(fTokenizer, &token)) {
2151 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2152 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2153 looper->setUp(this);
2154 looper->loop();
2155 delete looper;
2156 } else {
2157 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2158 fParent->consumeToken(token);
2159 }
2160 }
2161 // TODO(edisonn): restore stack.
2162}
2163
2164// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2165// TODO(edisonn): Add API for Forms viewing and editing
2166// e.g. SkBitmap getPage(int page);
2167// int formsCount();
2168// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2169// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2170// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2171// resources needed, so the preview is fast.
2172// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2173// references automatically.
2174
edisonn@google.com222382b2013-07-10 22:33:10 +00002175PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002176
edisonn@google.com444e25a2013-07-11 15:20:50 +00002177bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002178 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002179 return false;
2180 }
2181
edisonn@google.com222382b2013-07-10 22:33:10 +00002182 if (page < 0 || page >= pages()) {
2183 return false;
2184 }
2185
edisonn@google.com222382b2013-07-10 22:33:10 +00002186 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002187
2188 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page, pdfContext.fTmpPageAllocator);
edisonn@google.com1f080162013-07-23 21:05:49 +00002189 if (!tokenizer) {
2190 // TODO(edisonn): report/warning/debug
2191 return false;
2192 }
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002193
edisonn@google.com222382b2013-07-10 22:33:10 +00002194 pdfContext.fOriginalMatrix = SkMatrix::I();
2195 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2196
2197 gPdfContext = &pdfContext;
2198
2199 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002200 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002201 SkScalar w = dst.width();
2202 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002203
edisonn@google.com444e25a2013-07-11 15:20:50 +00002204 SkScalar wp = fPdfDoc->MediaBox(page).width();
2205 SkScalar hp = fPdfDoc->MediaBox(page).height();
2206
2207 SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(wp, z), SkPoint::Make(wp, hp), SkPoint::Make(z, hp)};
edisonn@google.com222382b2013-07-10 22:33:10 +00002208// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2209
2210 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2211 // TODO(edisonn): add flagg for no clipping.
2212 // Use larger image to make sure we do not draw anything outside of page
2213 // could be used in tests.
2214
2215#ifdef PDF_DEBUG_3X
2216 SkPoint skiaSpace[4] = {SkPoint::Make(w+z, h+h), SkPoint::Make(w+w, h+h), SkPoint::Make(w+w, h+z), SkPoint::Make(w+z, h+z)};
2217#else
2218 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2219#endif
2220 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2221 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2222
2223 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2224 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2225
2226 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2227 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2228
2229 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2230 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2231
2232
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002233 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
2234 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2235 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002236
edisonn@google.com222382b2013-07-10 22:33:10 +00002237#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002238 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002239#endif
2240
edisonn@google.com444e25a2013-07-11 15:20:50 +00002241 canvas->setMatrix(pdfContext.fOriginalMatrix);
2242
edisonn@google.com222382b2013-07-10 22:33:10 +00002243// erase with red before?
2244// SkPaint paint;
2245// paint.setColor(SK_ColorRED);
2246// canvas->drawRect(rect, paint);
2247
2248 PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
2249 looper.loop();
2250
2251 delete tokenizer;
2252
2253 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002254 return true;
2255}
edisonn@google.com222382b2013-07-10 22:33:10 +00002256
2257bool SkPdfRenderer::load(const SkString inputFileName) {
2258 unload();
2259
2260 // TODO(edisonn): create static function that could return NULL if there are errors
2261 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002262 if (fPdfDoc->pages() == 0) {
2263 delete fPdfDoc;
2264 fPdfDoc = NULL;
2265 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002266
2267 return fPdfDoc != NULL;
2268}
2269
edisonn@google.com147adb12013-07-24 15:56:19 +00002270bool SkPdfRenderer::load(SkStream* stream) {
2271 unload();
2272
2273 // TODO(edisonn): create static function that could return NULL if there are errors
2274 fPdfDoc = new SkNativeParsedPDF(stream);
2275 if (fPdfDoc->pages() == 0) {
2276 delete fPdfDoc;
2277 fPdfDoc = NULL;
2278 }
2279
2280 return fPdfDoc != NULL;
2281}
2282
2283
edisonn@google.com222382b2013-07-10 22:33:10 +00002284int SkPdfRenderer::pages() const {
2285 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2286}
2287
2288void SkPdfRenderer::unload() {
2289 delete fPdfDoc;
2290 fPdfDoc = NULL;
2291}
2292
2293SkRect SkPdfRenderer::MediaBox(int page) const {
2294 SkASSERT(fPdfDoc);
2295 return fPdfDoc->MediaBox(page);
2296}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002297
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002298size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002299 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2300}
edisonn@google.com147adb12013-07-24 15:56:19 +00002301
2302bool SkPDFNativeRenderToBitmap(SkStream* stream,
2303 SkBitmap* output,
2304 int page,
2305 SkPdfContent content,
2306 double dpi) {
2307 SkASSERT(page >= 0);
2308 SkPdfRenderer renderer;
2309 renderer.load(stream);
2310 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2311 return false;
2312 }
2313
2314 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2315
2316 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2317 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2318
2319 rect = SkRect::MakeWH(width, height);
2320
2321 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2322
2323 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2324 SkCanvas canvas(device);
2325
2326 return renderer.renderPage(page, &canvas, rect);
2327}