blob: f9ced8807d2ef20cd24977b8edac706af0779f12 [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.comf111a4b2013-07-31 18:22:36 +0000637//TODO(edisonn): options for implementing isolation and knockout
638// 1) emulate them (current solution)
639// PRO: simple
640// CON: will need to use readPixels, which means serious perf issues
641// 2) Compile a plan for an array of matrixes, compose the result at the end
642// PRO: might be faster then 1, no need to readPixels
643// CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan
644// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
645// 3) support them natively in SkCanvas
646// PRO: simple
647// CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1)
648// 4) compile a plan using pathops, and render once without any fancy rules with backdrop
649// PRO: simple, fast
650// CON: pathops must be bug free first + time to compute new paths
651// pay a price at loading pdf to compute a pdf draw plan
652// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
653
654
655// TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree!
656// TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed
657
658
659
660static void doGroup_before(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) {
661 SkRect bboxOrig = bbox;
662 SkBitmap backdrop;
663 bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc);
664// bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc);
665 bool hasPixels = false;
666 if (!isolatedGroup) {
667 // TODO(edisonn): if the rect is not mapable, the operation could be expensive, e.g.
668 // a diagonal long but small rect would require to save all the page
669 SkMatrix inverse;
670 if (pdfContext->fGraphicsState.fCTM.mapRect(&bbox) &&
671 canvas->getTotalMatrix().invert(&inverse) &&
672 inverse.mapRect(&bbox)) {
673 SkIRect area = SkIRect::MakeLTRB(SkScalarTruncToInt(bbox.left()),
674 SkScalarTruncToInt(bbox.top()),
675 SkScalarTruncToInt(bbox.right()) + 2,
676 SkScalarTruncToInt(bbox.bottom()) + 2);
677 SkBitmap dummy;
678 if (canvas->readPixels(area, &dummy)) {
679 hasPixels = true;
680 }
681 }
682 }
683 SkPaint paint;
684 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
685 canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL);
686
687 if (hasPixels) {
688 canvas->drawBitmapRect(backdrop, bboxOrig, NULL);
689 }
690}
691
692//static void doGroup_after(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup) {
693//}
694
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000695static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000696 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000697 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000698 }
699
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000700 if (!skobj->has_BBox()) {
701 return kIgnoreError_PdfResult;
702 }
703
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000704 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000705
706
707
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000708 canvas->save();
709
710
edisonn@google.com571c70b2013-07-10 17:09:50 +0000711 if (skobj->Resources(pdfContext->fPdfDoc)) {
712 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000713 }
714
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000715 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000716
edisonn@google.com571c70b2013-07-10 17:09:50 +0000717 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000718 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
719 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
720 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000721 // TODO(edisonn) reset matrixTm and matricTlm also?
722 }
723
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000724 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000725
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000726 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000727
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000728 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
729 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000730
731 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
732 // For this PdfContentsTokenizer needs to be extended.
733
edisonn@google.come878e722013-07-29 19:10:58 +0000734 // This is a group?
735 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000736 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
737 doGroup_before(pdfContext, canvas, bbox, tgroup, false);
edisonn@google.come878e722013-07-29 19:10:58 +0000738 }
739
edisonn@google.com571c70b2013-07-10 17:09:50 +0000740 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000741
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000742 SkPdfNativeTokenizer* tokenizer =
743 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000744 if (tokenizer != NULL) {
745 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
746 looper.loop();
747 delete tokenizer;
748 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000749
750 // TODO(edisonn): should we restore the variable stack at the same state?
751 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000752
753 if (skobj->has_Group()) {
754 canvas->restore();
755 }
756
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000757 canvas->restore();
758 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000759 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000760}
761
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000762//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
763// return kNYI_PdfResult;
764//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000765
edisonn@google.com571c70b2013-07-10 17:09:50 +0000766PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
767 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000768 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000769 }
770
771 PdfOp_q(pdfContext, canvas, NULL);
772 canvas->save();
773
774 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
775 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
776
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000777 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
778 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000779
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000780 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000781
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000782 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000783
784 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000785 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000786
787 SkTraceRect(rm, "bbox mapped");
788
789 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
790
791 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
792 // For this PdfContentsTokenizer needs to be extended.
793
edisonn@google.com571c70b2013-07-10 17:09:50 +0000794 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000795
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000796 SkPdfNativeTokenizer* tokenizer =
797 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000798 if (tokenizer != NULL) {
799 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
800 looper.loop();
801 delete tokenizer;
802 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000803
804 // TODO(edisonn): should we restore the variable stack at the same state?
805 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
806 canvas->restore();
807 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000808
809 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000810}
811
812
edisonn@google.com571c70b2013-07-10 17:09:50 +0000813// TODO(edisonn): make sure the pointer is unique
814std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000815
816class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000817 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000818public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000819 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000820 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000821 }
822
823 ~CheckRecursiveRendering() {
824 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000825 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000826 }
827
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000828 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000829 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000830 }
831};
832
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000833static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000834 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000835 // Oops, corrupt PDF!
836 return kIgnoreError_PdfResult;
837 }
838
edisonn@google.com571c70b2013-07-10 17:09:50 +0000839 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000840
edisonn@google.com571c70b2013-07-10 17:09:50 +0000841 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000842 {
843 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000844 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000845 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000846 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000847 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
848 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000849 default:
850 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000851 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000852}
853
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000854static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
855 if (!skobj) {
856 return kIgnoreError_PdfResult;
857 }
858
859 if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
860 return kNYI_PdfResult;
861 }
862
863 SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
864
865 if (!stream) {
866 return kIgnoreError_PdfResult;
867 }
868
869 if (CheckRecursiveRendering::IsInRendering(skobj)) {
870 // Oops, corrupt PDF!
871 return kIgnoreError_PdfResult;
872 }
873 CheckRecursiveRendering checkRecursion(skobj);
874
875
876 PdfOp_q(pdfContext, canvas, NULL);
877
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000878
879 if (skobj->Resources(pdfContext->fPdfDoc)) {
880 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
881 }
882
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000883 // TODO(edisonn): MediaBox can be inherited!!!!
884 SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc);
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000885 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000886 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
887 doGroup_before(pdfContext, canvas, bbox, tgroup, true);
888 } else {
889 canvas->save();
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000890 }
891
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000892
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000893 SkPdfNativeTokenizer* tokenizer =
894 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
895 if (tokenizer != NULL) {
896 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
897 looper.loop();
898 delete tokenizer;
899 }
900
901 // TODO(edisonn): should we restore the variable stack at the same state?
902 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
903 canvas->restore();
904 PdfOp_Q(pdfContext, canvas, NULL);
905 return kPartial_PdfResult;
906}
907
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000908PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
909 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
910 canvas->save();
911 return kOK_PdfResult;
912}
913
914PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
915 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
916 pdfContext->fStateStack.pop();
917 canvas->restore();
918 return kOK_PdfResult;
919}
920
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000921static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000922 double array[6];
923 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000924 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000925 pdfContext->fObjectStack.pop();
926 }
927
928 // a b
929 // c d
930 // e f
931
932 // 0 1
933 // 2 3
934 // 4 5
935
936 // sx ky
937 // kx sy
938 // tx ty
939 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
940
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000941 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000942
943#ifdef PDF_TRACE
944 printf("cm ");
945 for (int i = 0 ; i < 6 ; i++) {
946 printf("%f ", array[i]);
947 }
948 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000949 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000950#endif
951
952 return kOK_PdfResult;
953}
954
955//leading TL Set the text leading, Tl
956//, to leading, which is a number expressed in unscaled text
957//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000958static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000959 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000960
961 pdfContext->fGraphicsState.fTextLeading = ty;
962
963 return kOK_PdfResult;
964}
965
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000966static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000967#ifdef PDF_TRACE
968 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
969#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +0000970 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000971 SkPdfObject* obj = pdfContext->fObjectStack.top();
972 obj = obj;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000973 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000974
975 double array[6] = {1, 0, 0, 1, tx, ty};
976 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
977
978 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
979 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
980
981 return kPartial_PdfResult;
982}
983
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000984static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000985 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
986 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000987
edisonn@google.com571c70b2013-07-10 17:09:50 +0000988 // TODO(edisonn): Create factory methods or constructors so native is hidden
989 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000990 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000991
992 PdfOp_TL(pdfContext, canvas, looper);
993
edisonn@google.com571c70b2013-07-10 17:09:50 +0000994 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000995 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000996
edisonn@google.com571c70b2013-07-10 17:09:50 +0000997 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000998 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000999
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001000 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
1001
1002 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
1003 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001004
1005 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001006}
1007
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001008static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001009 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1010 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1011 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1012 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1013 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1014 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001015
1016 double array[6];
1017 array[0] = a;
1018 array[1] = b;
1019 array[2] = c;
1020 array[3] = d;
1021 array[4] = e;
1022 array[5] = f;
1023
1024 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001025 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001026
1027 // TODO(edisonn): Text positioning.
1028 pdfContext->fGraphicsState.fMatrixTm = matrix;
1029 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
1030
1031 return kPartial_PdfResult;
1032}
1033
1034//— T* Move to the start of the next line. This operator has the same effect as the code
1035//0 Tl Td
1036//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001037static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001038 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
1039 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001040
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001041 pdfContext->fObjectStack.push(zero);
1042 pdfContext->fObjectStack.push(tl);
1043
1044 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
1045
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001046 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001047}
1048
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001049static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001050 if (pdfContext->fGraphicsState.fPathClosed) {
1051 pdfContext->fGraphicsState.fPath.reset();
1052 pdfContext->fGraphicsState.fPathClosed = false;
1053 }
1054
edisonn@google.com571c70b2013-07-10 17:09:50 +00001055 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1056 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001057
1058 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1059 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1060
1061 return kOK_PdfResult;
1062}
1063
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001064static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001065 if (pdfContext->fGraphicsState.fPathClosed) {
1066 pdfContext->fGraphicsState.fPath.reset();
1067 pdfContext->fGraphicsState.fPathClosed = false;
1068 }
1069
edisonn@google.com571c70b2013-07-10 17:09:50 +00001070 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1071 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001072
1073 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1074 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1075
1076 return kOK_PdfResult;
1077}
1078
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001079static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001080 if (pdfContext->fGraphicsState.fPathClosed) {
1081 pdfContext->fGraphicsState.fPath.reset();
1082 pdfContext->fGraphicsState.fPathClosed = false;
1083 }
1084
edisonn@google.com571c70b2013-07-10 17:09:50 +00001085 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1086 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1087 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1088 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1089 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1090 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001091
1092 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1093 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1094 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1095
1096 pdfContext->fGraphicsState.fCurPosX = x3;
1097 pdfContext->fGraphicsState.fCurPosY = y3;
1098
1099 return kOK_PdfResult;
1100}
1101
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001102static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001103 if (pdfContext->fGraphicsState.fPathClosed) {
1104 pdfContext->fGraphicsState.fPath.reset();
1105 pdfContext->fGraphicsState.fPathClosed = false;
1106 }
1107
edisonn@google.com571c70b2013-07-10 17:09:50 +00001108 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1109 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1110 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1111 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001112 double y1 = pdfContext->fGraphicsState.fCurPosY;
1113 double x1 = pdfContext->fGraphicsState.fCurPosX;
1114
1115 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1116 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1117 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1118
1119 pdfContext->fGraphicsState.fCurPosX = x3;
1120 pdfContext->fGraphicsState.fCurPosY = y3;
1121
1122 return kOK_PdfResult;
1123}
1124
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001125static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001126 if (pdfContext->fGraphicsState.fPathClosed) {
1127 pdfContext->fGraphicsState.fPath.reset();
1128 pdfContext->fGraphicsState.fPathClosed = false;
1129 }
1130
edisonn@google.com571c70b2013-07-10 17:09:50 +00001131 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1132 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001133 double y2 = pdfContext->fGraphicsState.fCurPosY;
1134 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001135 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1136 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001137
1138 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1139 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1140 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1141
1142 pdfContext->fGraphicsState.fCurPosX = x3;
1143 pdfContext->fGraphicsState.fCurPosY = y3;
1144
1145 return kOK_PdfResult;
1146}
1147
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001148static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001149 if (pdfContext->fGraphicsState.fPathClosed) {
1150 pdfContext->fGraphicsState.fPath.reset();
1151 pdfContext->fGraphicsState.fPathClosed = false;
1152 }
1153
edisonn@google.com571c70b2013-07-10 17:09:50 +00001154 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1155 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1156 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1157 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001158
1159 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1160 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1161
1162 pdfContext->fGraphicsState.fCurPosX = x;
1163 pdfContext->fGraphicsState.fCurPosY = y + height;
1164
1165 return kOK_PdfResult;
1166}
1167
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001168static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001169 pdfContext->fGraphicsState.fPath.close();
1170 return kOK_PdfResult;
1171}
1172
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001173static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001174 SkPath path = pdfContext->fGraphicsState.fPath;
1175
1176 if (close) {
1177 path.close();
1178 }
1179
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001180 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001181
1182 SkPaint paint;
1183
1184 SkPoint line[2];
1185 if (fill && !stroke && path.isLine(line)) {
1186 paint.setStyle(SkPaint::kStroke_Style);
1187
1188 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1189 paint.setStrokeWidth(SkDoubleToScalar(0));
1190
1191 canvas->drawPath(path, paint);
1192 } else {
1193 if (fill) {
1194 paint.setStyle(SkPaint::kFill_Style);
1195 if (evenOdd) {
1196 path.setFillType(SkPath::kEvenOdd_FillType);
1197 }
1198
1199 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1200
1201 canvas->drawPath(path, paint);
1202 }
1203
1204 if (stroke) {
1205 paint.setStyle(SkPaint::kStroke_Style);
1206
1207 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1208
1209 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1210 canvas->drawPath(path, paint);
1211 }
1212 }
1213
1214 pdfContext->fGraphicsState.fPath.reset();
1215 // todo zoom ... other stuff ?
1216
1217 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1218#ifndef PDF_DEBUG_NO_CLIPING
1219 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1220#endif
1221 }
1222
1223 //pdfContext->fGraphicsState.fClipPath.reset();
1224 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1225
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001226 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001227
1228}
1229
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001230static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001231 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1232}
1233
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001234static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001235 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1236}
1237
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001238static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001239 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1240}
1241
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001242static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001243 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1244}
1245
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001246static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001247 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1248}
1249
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001250static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001251 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1252}
1253
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001254static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001255 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1256}
1257
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001258static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001259 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1260}
1261
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001262static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001263 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1264}
1265
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001266static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001267 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001268 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1269#ifndef PDF_DEBUG_NO_CLIPING
1270 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1271#endif
1272 }
1273
1274 //pdfContext->fGraphicsState.fClipPath.reset();
1275 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1276
1277 pdfContext->fGraphicsState.fPathClosed = true;
1278
1279 return kOK_PdfResult;
1280}
1281
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001282static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001283 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001284 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
1285 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001286
1287 return kPartial_PdfResult;
1288}
1289
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001290static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001291 if (!pdfContext->fGraphicsState.fTextBlock) {
1292 return kIgnoreError_PdfResult;
1293 }
1294 // 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 +00001295 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001296}
1297
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001298PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001299#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001300 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001301#endif
1302
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001303 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1304 // TODO(edisonn): try to recover and draw it any way?
1305 return kIgnoreError_PdfResult;
1306 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001307
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001308 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1309 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1310 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1311 // TODO(edisonn): try to recover and draw it any way?
1312 return kIgnoreError_PdfResult;
1313 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001314
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001315 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1316
1317 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1318
1319 if (skfont) {
1320 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001321 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001322 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001323 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001324}
1325
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001326//font size Tf Set the text font, Tf
1327//, to font and the text font size, Tfs, to size. font is the name of a
1328//font resource in the Fontsubdictionary of the current resource dictionary; size is
1329//a number representing a scale factor. There is no initial value for either font or
1330//size; they must be specified explicitly using Tf before any text is shown.
1331static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1332 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1333 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1334 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1335}
1336
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001337static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001338 if (!pdfContext->fGraphicsState.fTextBlock) {
1339 // TODO(edisonn): try to recover and draw it any way?
1340 return kIgnoreError_PdfResult;
1341 }
1342
1343 PdfResult ret = DrawText(pdfContext,
1344 pdfContext->fObjectStack.top(),
1345 canvas);
1346 pdfContext->fObjectStack.pop();
1347
1348 return ret;
1349}
1350
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001351static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001352 if (!pdfContext->fGraphicsState.fTextBlock) {
1353 // TODO(edisonn): try to recover and draw it any way?
1354 return kIgnoreError_PdfResult;
1355 }
1356
1357 PdfOp_T_star(pdfContext, canvas, looper);
1358 // Do not pop, and push, just transfer the param to Tj
1359 return PdfOp_Tj(pdfContext, canvas, looper);
1360}
1361
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001362static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001363 if (!pdfContext->fGraphicsState.fTextBlock) {
1364 // TODO(edisonn): try to recover and draw it any way?
1365 return kIgnoreError_PdfResult;
1366 }
1367
1368 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1369 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1370 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1371
1372 pdfContext->fObjectStack.push(aw);
1373 PdfOp_Tw(pdfContext, canvas, looper);
1374
1375 pdfContext->fObjectStack.push(ac);
1376 PdfOp_Tc(pdfContext, canvas, looper);
1377
1378 pdfContext->fObjectStack.push(str);
1379 PdfOp_quote(pdfContext, canvas, looper);
1380
1381 return kPartial_PdfResult;
1382}
1383
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001384static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001385 if (!pdfContext->fGraphicsState.fTextBlock) {
1386 // TODO(edisonn): try to recover and draw it any way?
1387 return kIgnoreError_PdfResult;
1388 }
1389
edisonn@google.com571c70b2013-07-10 17:09:50 +00001390 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001391 pdfContext->fObjectStack.pop();
1392
edisonn@google.com571c70b2013-07-10 17:09:50 +00001393 if (!array->isArray()) {
1394 return kIgnoreError_PdfResult;
1395 }
1396
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001397 for( int i=0; i<static_cast<int>(array->size()); i++ )
1398 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001399 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001400 SkPdfObject* obj = (*array)[i];
1401 DrawText(pdfContext,
1402 obj,
1403 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001404 } else if ((*array)[i]->isNumber()) {
1405 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001406 SkMatrix matrix;
1407 matrix.setAll(SkDoubleToScalar(1),
1408 SkDoubleToScalar(0),
1409 // TODO(edisonn): use writing mode, vertical/horizontal.
1410 SkDoubleToScalar(-dx), // amount is substracted!!!
1411 SkDoubleToScalar(0),
1412 SkDoubleToScalar(1),
1413 SkDoubleToScalar(0),
1414 SkDoubleToScalar(0),
1415 SkDoubleToScalar(0),
1416 SkDoubleToScalar(1));
1417
1418 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1419 }
1420 }
1421 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1422}
1423
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001424static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001425 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001426 return kOK_PdfResult;
1427}
1428
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001429static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001430 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1431}
1432
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001433static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001434 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1435}
1436
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001437static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001439// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001440
1441 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1442
1443 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001444 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001445 doubles = false;
1446 }
1447
1448#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001449 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001450#endif
1451
1452 for (int i = n - 1; i >= 0 ; i--) {
1453 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001454 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1455// } else {
1456// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001457 }
1458 }
1459
1460 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001461 // TODO(edisonn): do possible field values to enum at parsing time!
1462 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001463 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001464 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001465 }
1466 return kPartial_PdfResult;
1467}
1468
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001469static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001470 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1471}
1472
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001473static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001474 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1475}
1476
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001477static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001478 //SkPdfString* name;
1479 if (pdfContext->fObjectStack.top()->isName()) {
1480 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001481 pdfContext->fObjectStack.pop();
1482 }
1483
1484 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1485 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1486
1487 return kPartial_PdfResult;
1488}
1489
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001490static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001491 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1492}
1493
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001494static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001495 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1496}
1497
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001498static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001499 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001500 return kNYI_PdfResult;
1501}
1502
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001503static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001504 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1505}
1506
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001507static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001508 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1509}
1510
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001511static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001512 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1513 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1514 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001515
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001516 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001517 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001518 return kOK_PdfResult;
1519}
1520
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001521static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001522 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1523}
1524
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001525static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001526 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1527}
1528
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001529static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001530 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001531 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1532 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1533 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1534 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001535
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001536 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001537 // TODO(edisonn): Set color.
1538 return kNYI_PdfResult;
1539}
1540
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001541static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001542 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1543}
1544
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001545static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001546 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1547}
1548
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001549static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001550 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1551 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1552
1553 return kOK_PdfResult;
1554}
1555
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001556static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001557 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1558
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001559 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1560 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1561
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001562 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001563}
1564
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001565static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001566 *looper = new PdfCompatibilitySectionLooper();
1567 return kOK_PdfResult;
1568}
1569
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001570static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001571#ifdef ASSERT_BAD_PDF_OPS
1572 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1573 // have the assert when testing good pdfs.
1574#endif
1575 return kIgnoreError_PdfResult;
1576}
1577
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001578static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001579 *looper = new PdfInlineImageLooper();
1580 return kOK_PdfResult;
1581}
1582
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001583static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001584#ifdef ASSERT_BAD_PDF_OPS
1585 SkASSERT(false); // must be processed in inline image looper, but let's
1586 // have the assert when testing good pdfs.
1587#endif
1588 return kIgnoreError_PdfResult;
1589}
1590
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001591static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001592#ifdef ASSERT_BAD_PDF_OPS
1593 SkASSERT(false); // must be processed in inline image looper, but let's
1594 // have the assert when testing good pdfs.
1595#endif
1596 return kIgnoreError_PdfResult;
1597}
1598
edisonn@google.com24cdf132013-07-30 16:06:12 +00001599
1600// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1601PdfResult skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1602 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1603 return kOK_PdfResult;
1604}
1605
1606PdfResult skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1607 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1608 return kOK_PdfResult;
1609}
1610
1611PdfResult skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001612 pdfContext->fGraphicsState.fLineWidth = lineWidth;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001613 return kOK_PdfResult;
1614}
1615
1616PdfResult skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1617 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1618 return kOK_PdfResult;
1619}
1620
1621PdfResult skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1622 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1623 return kOK_PdfResult;
1624}
1625
1626PdfResult skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1627 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1628 return kOK_PdfResult;
1629}
1630
1631// TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure
1632/*
16331) [ ] 0 No dash; solid, unbroken lines
16342) [3] 0 3 units on, 3 units off, …
16353) [2] 1 1 on, 2 off, 2 on, 2 off, …
16364) [2 1] 0 2 on, 1 off, 2 on, 1 off, …
16375) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, …
16386) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, …
1639 */
1640
1641PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* intervals, SkPdfObject* phase) {
1642 int cnt = intervals->size();
1643 if (cnt >= 256) {
1644 // TODO(edisonn): report error/warning, unsuported;
1645 // TODO(edisonn): alloc memory
1646 return kIgnoreError_PdfResult;
1647 }
1648 for (int i = 0; i < cnt; i++) {
1649 if (!intervals->objAtAIndex(i)->isNumber()) {
1650 // TODO(edisonn): report error/warning
1651 return kIgnoreError_PdfResult;
1652 }
1653 }
1654
edisonn@google.com24cdf132013-07-30 16:06:12 +00001655 double total = 0;
1656 for (int i = 0 ; i < cnt; i++) {
1657 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1658 total += pdfContext->fGraphicsState.fDashArray[i];
1659 }
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001660 if (cnt & 1) {
1661 if (cnt == 1) {
1662 pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0];
1663 cnt++;
1664 } else {
1665 // TODO(edisonn): report error/warning
1666 return kNYI_PdfResult;
1667 }
1668 }
1669 pdfContext->fGraphicsState.fDashArrayLength = cnt;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001670 pdfContext->fGraphicsState.fDashPhase = phase->scalarValue();
1671 if (pdfContext->fGraphicsState.fDashPhase == 0) {
1672 // other rules, changes?
1673 pdfContext->fGraphicsState.fDashPhase = total;
1674 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001675
1676 return kOK_PdfResult;
1677}
1678
edisonn@google.com24cdf132013-07-30 16:06:12 +00001679PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1680 // TODO(edisonn): verify input
1681 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1682 // TODO(edisonn): report error/warning
1683 return kIgnoreError_PdfResult;
1684 }
1685 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1));
1686}
1687
1688void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1689 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1690 // TODO(edisonn): report error/warning
1691 return;
1692 }
1693 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1694}
1695
1696
1697//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
1698static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1699 double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1700 return skpdfGraphicsStateApplyLW(pdfContext, lw);
1701}
1702
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001703//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 +00001704static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001705 int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1706 return skpdfGraphicsStateApplyLC(pdfContext, lc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001707}
1708
1709//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 +00001710static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001711 double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1712 return skpdfGraphicsStateApplyLJ(pdfContext, lj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001713}
1714
1715//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001716static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001717 double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1718 return skpdfGraphicsStateApplyML(pdfContext, ml);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001719}
1720
1721//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1722//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001723static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001724 SkPdfObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1725 SkPdfObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001726
edisonn@google.com24cdf132013-07-30 16:06:12 +00001727 if (!array->isArray()) {
1728 return kIgnoreError_PdfResult;
1729 }
1730
1731 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001732}
1733
1734//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 +00001735static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001736 pdfContext->fObjectStack.pop();
1737
1738 return kNYI_PdfResult;
1739}
1740
1741//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1742//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1743//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001744static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001745 pdfContext->fObjectStack.pop();
1746
1747 return kNYI_PdfResult;
1748}
1749
edisonn@google.come878e722013-07-29 19:10:58 +00001750SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1751
1752class InitBlendModes {
1753public:
1754 InitBlendModes() {
1755 // TODO(edisonn): use the python code generator?
1756 // TABLE 7.2 Standard separable blend modes
1757 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1758 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1759 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1760 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1761 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1762 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1763 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1764 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1765 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1766 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1767 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1768 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1769
1770 // TABLE 7.3 Standard nonseparable blend modes
1771 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1772 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1773 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1774 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1775 }
1776};
1777
1778InitBlendModes _gDummyInniter;
1779
1780SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1781 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1782 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1783 return mode;
1784 }
1785
1786 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1787}
1788
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001789void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001790 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1791 if (mode <= SkXfermode::kLastMode) {
1792 pdfContext->fGraphicsState.fBlendModesLength = 1;
1793 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1794 } else {
1795 // TODO(edisonn): report unknown blend mode
1796 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001797}
1798
1799void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001800 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
1801 // TODO(edisonn): report error/warning
1802 return;
1803 }
1804 SkXfermode::Mode modes[256];
1805 int cnt = blendModes->size();
1806 for (int i = 0; i < cnt; i++) {
1807 SkPdfObject* name = blendModes->objAtAIndex(i);
1808 if (!name->isName()) {
1809 // TODO(edisonn): report error/warning
1810 return;
1811 }
1812 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
1813 if (mode > SkXfermode::kLastMode) {
1814 // TODO(edisonn): report error/warning
1815 return;
1816 }
1817 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001818
edisonn@google.come878e722013-07-29 19:10:58 +00001819 pdfContext->fGraphicsState.fBlendModesLength = cnt;
1820 for (int i = 0; i < cnt; i++) {
1821 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
1822 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001823}
1824
1825void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
1826 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00001827 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001828 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00001829 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
1830 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
1831 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
1832 } else {
1833 // TODO (edisonn): report error/warning
1834 }
1835}
1836
1837void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001838 if (sMask == "None") {
1839 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
1840 pdfContext->fGraphicsState.fSMask = SkBitmap();
1841 return;
1842 }
1843
edisonn@google.come878e722013-07-29 19:10:58 +00001844 //Next, get the ExtGState Dictionary from the Resource Dictionary:
1845 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
1846
1847 if (extGStateDictionary == NULL) {
1848#ifdef PDF_TRACE
1849 printf("ExtGState is NULL!\n");
1850#endif
1851 // TODO (edisonn): report error/warning
1852 return;
1853 }
1854
1855 SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
1856 if (!obj || !obj->isDictionary()) {
1857 // TODO (edisonn): report error/warning
1858 return;
1859 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001860
1861 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
1862 pdfContext->fGraphicsState.fSMask = SkBitmap();
1863
edisonn@google.come878e722013-07-29 19:10:58 +00001864 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001865}
1866
1867void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
1868 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
1869}
1870
1871
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001872//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1873//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 +00001874static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001875 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001876
1877#ifdef PDF_TRACE
1878 std::string str;
1879#endif
1880
1881 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001882 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001883
1884 if (extGStateDictionary == NULL) {
1885#ifdef PDF_TRACE
1886 printf("ExtGState is NULL!\n");
1887#endif
1888 return kIgnoreError_PdfResult;
1889 }
1890
edisonn@google.com571c70b2013-07-10 17:09:50 +00001891 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001892
edisonn@google.com571c70b2013-07-10 17:09:50 +00001893 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1894 return kIgnoreError_PdfResult;
1895 }
1896 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001897
1898 // TODO(edisonn): now load all those properties in graphic state.
1899 if (gs == NULL) {
1900 return kIgnoreError_PdfResult;
1901 }
1902
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001903 if (gs->has_LW()) {
1904 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
1905 }
1906
1907 if (gs->has_LC()) {
1908 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
1909 }
1910
1911 if (gs->has_LJ()) {
1912 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
1913 }
1914
1915 if (gs->has_ML()) {
1916 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
1917 }
1918
1919 if (gs->has_D()) {
1920 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
1921 }
1922
1923 if (gs->has_Font()) {
1924 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
1925 }
1926
1927 if (gs->has_BM()) {
1928 if (gs->isBMAName(pdfContext->fPdfDoc)) {
1929 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
1930 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
1931 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
1932 } else {
1933 // TODO(edisonn): report/warn
1934 }
1935 }
1936
1937 if (gs->has_SMask()) {
1938 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
1939 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
1940 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
1941 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
1942 } else {
1943 // TODO(edisonn): report/warn
1944 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001945 }
1946
1947 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001948 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001949 }
1950
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001951 if (gs->has_CA()) {
1952 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
1953 }
1954
1955 if (gs->has_AIS()) {
1956 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001957 }
1958
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001959 return kNYI_PdfResult;
1960}
1961
1962//charSpace Tc Set the character spacing, Tc
1963//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1964//Initial value: 0.
1965PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001966 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001967 pdfContext->fGraphicsState.fCharSpace = charSpace;
1968
1969 return kOK_PdfResult;
1970}
1971
1972//wordSpace Tw Set the word spacing, T
1973//w
1974//, to wordSpace, which is a number expressed in unscaled
1975//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1976//value: 0.
1977PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001978 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001979 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1980
1981 return kOK_PdfResult;
1982}
1983
1984//scale Tz Set the horizontal scaling, Th
1985//, to (scale ˜ 100). scale is a number specifying the
1986//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001987static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001988 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001989
1990 return kNYI_PdfResult;
1991}
1992
1993//render Tr Set the text rendering mode, T
1994//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001995static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001996 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001997
1998 return kNYI_PdfResult;
1999}
2000//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
2001//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002002static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002003 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002004
2005 return kNYI_PdfResult;
2006}
2007
2008//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002009static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002010 pdfContext->fObjectStack.pop();
2011 pdfContext->fObjectStack.pop();
2012
2013 return kNYI_PdfResult;
2014}
2015
2016//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002017static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002018 pdfContext->fObjectStack.pop();
2019 pdfContext->fObjectStack.pop();
2020 pdfContext->fObjectStack.pop();
2021 pdfContext->fObjectStack.pop();
2022 pdfContext->fObjectStack.pop();
2023 pdfContext->fObjectStack.pop();
2024
2025 return kNYI_PdfResult;
2026}
2027
2028//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002029static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002030 pdfContext->fObjectStack.pop();
2031
2032 return kNYI_PdfResult;
2033}
2034
2035//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002036static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002037 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002038
edisonn@google.com571c70b2013-07-10 17:09:50 +00002039 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002040
2041 if (xObject == NULL) {
2042#ifdef PDF_TRACE
2043 printf("XObject is NULL!\n");
2044#endif
2045 return kIgnoreError_PdfResult;
2046 }
2047
edisonn@google.com571c70b2013-07-10 17:09:50 +00002048 SkPdfObject* value = xObject->get(name);
2049 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002050
2051#ifdef PDF_TRACE
2052// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002053// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002054#endif
2055
edisonn@google.com571c70b2013-07-10 17:09:50 +00002056 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002057}
2058
2059//tag MP Designate a marked-content point. tag is a name object indicating the role or
2060//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002061static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002062 pdfContext->fObjectStack.pop();
2063
2064 return kNYI_PdfResult;
2065}
2066
2067//tag properties DP Designate a marked-content point with an associated property list. tag is a
2068//name object indicating the role or significance of the point; properties is
2069//either an inline dictionary containing the property list or a name object
2070//associated with it in the Properties subdictionary of the current resource
2071//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002072static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002073 pdfContext->fObjectStack.pop();
2074 pdfContext->fObjectStack.pop();
2075
2076 return kNYI_PdfResult;
2077}
2078
2079//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
2080//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002081static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002082 pdfContext->fObjectStack.pop();
2083
2084 return kNYI_PdfResult;
2085}
2086
2087//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
2088//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
2089//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 +00002090static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002091 pdfContext->fObjectStack.pop();
2092 pdfContext->fObjectStack.pop();
2093
2094 return kNYI_PdfResult;
2095}
2096
2097//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002098static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002099 return kNYI_PdfResult;
2100}
2101
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002102static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002103 static bool gInitialized = false;
2104 if (gInitialized) {
2105 return;
2106 }
2107
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002108 gPdfOps.set("q", PdfOp_q);
2109 gPdfOps.set("Q", PdfOp_Q);
2110 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002111
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002112 gPdfOps.set("TD", PdfOp_TD);
2113 gPdfOps.set("Td", PdfOp_Td);
2114 gPdfOps.set("Tm", PdfOp_Tm);
2115 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002116
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002117 gPdfOps.set("m", PdfOp_m);
2118 gPdfOps.set("l", PdfOp_l);
2119 gPdfOps.set("c", PdfOp_c);
2120 gPdfOps.set("v", PdfOp_v);
2121 gPdfOps.set("y", PdfOp_y);
2122 gPdfOps.set("h", PdfOp_h);
2123 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002124
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002125 gPdfOps.set("S", PdfOp_S);
2126 gPdfOps.set("s", PdfOp_s);
2127 gPdfOps.set("f", PdfOp_f);
2128 gPdfOps.set("F", PdfOp_F);
2129 gPdfOps.set("f*", PdfOp_f_star);
2130 gPdfOps.set("B", PdfOp_B);
2131 gPdfOps.set("B*", PdfOp_B_star);
2132 gPdfOps.set("b", PdfOp_b);
2133 gPdfOps.set("b*", PdfOp_b_star);
2134 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002135
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002136 gPdfOps.set("BT", PdfOp_BT);
2137 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002138
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002139 gPdfOps.set("Tj", PdfOp_Tj);
2140 gPdfOps.set("'", PdfOp_quote);
2141 gPdfOps.set("\"", PdfOp_doublequote);
2142 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002143
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002144 gPdfOps.set("CS", PdfOp_CS);
2145 gPdfOps.set("cs", PdfOp_cs);
2146 gPdfOps.set("SC", PdfOp_SC);
2147 gPdfOps.set("SCN", PdfOp_SCN);
2148 gPdfOps.set("sc", PdfOp_sc);
2149 gPdfOps.set("scn", PdfOp_scn);
2150 gPdfOps.set("G", PdfOp_G);
2151 gPdfOps.set("g", PdfOp_g);
2152 gPdfOps.set("RG", PdfOp_RG);
2153 gPdfOps.set("rg", PdfOp_rg);
2154 gPdfOps.set("K", PdfOp_K);
2155 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002156
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002157 gPdfOps.set("W", PdfOp_W);
2158 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002159
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002160 gPdfOps.set("BX", PdfOp_BX);
2161 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002162
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002163 gPdfOps.set("BI", PdfOp_BI);
2164 gPdfOps.set("ID", PdfOp_ID);
2165 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002166
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002167 gPdfOps.set("w", PdfOp_w);
2168 gPdfOps.set("J", PdfOp_J);
2169 gPdfOps.set("j", PdfOp_j);
2170 gPdfOps.set("M", PdfOp_M);
2171 gPdfOps.set("d", PdfOp_d);
2172 gPdfOps.set("ri", PdfOp_ri);
2173 gPdfOps.set("i", PdfOp_i);
2174 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002175
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002176 gPdfOps.set("Tc", PdfOp_Tc);
2177 gPdfOps.set("Tw", PdfOp_Tw);
2178 gPdfOps.set("Tz", PdfOp_Tz);
2179 gPdfOps.set("TL", PdfOp_TL);
2180 gPdfOps.set("Tf", PdfOp_Tf);
2181 gPdfOps.set("Tr", PdfOp_Tr);
2182 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002183
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002184 gPdfOps.set("d0", PdfOp_d0);
2185 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002186
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002187 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002188
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002189 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002190
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002191 gPdfOps.set("MP", PdfOp_MP);
2192 gPdfOps.set("DP", PdfOp_DP);
2193 gPdfOps.set("BMC", PdfOp_BMC);
2194 gPdfOps.set("BDC", PdfOp_BDC);
2195 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002196
2197 gInitialized = true;
2198}
2199
2200class InitPdfOps {
2201public:
2202 InitPdfOps() {
2203 initPdfOperatorRenderes();
2204 }
2205};
2206
2207InitPdfOps gInitPdfOps;
2208
2209void reportPdfRenderStats() {
2210 std::map<std::string, int>::iterator iter;
2211
2212 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002213 SkTDict<int>::Iter iter(gRenderStats[i]);
2214 const char* key;
2215 int value = 0;
2216 while ((key = iter.next(&value)) != NULL) {
2217 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002218 }
2219 }
2220}
2221
2222PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002223 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002224 {
2225 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002226 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002227 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002228 // caller, main work is done by pdfOperatorRenderer(...)
2229 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002230 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
2231
2232 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002233 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2234 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002235
2236 if (childLooper) {
2237 childLooper->setUp(this);
2238 childLooper->loop();
2239 delete childLooper;
2240 }
2241 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002242 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002243 gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2244 gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002245 }
2246 }
2247 else if (token.fType == kObject_TokenType)
2248 {
2249 fPdfContext->fObjectStack.push( token.fObject );
2250 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002251 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002252 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002253 return kIgnoreError_PdfResult;
2254 }
2255 return kOK_PdfResult;
2256}
2257
2258void PdfMainLooper::loop() {
2259 PdfToken token;
2260 while (readToken(fTokenizer, &token)) {
2261 consumeToken(token);
2262 }
2263}
2264
2265PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002266 SkASSERT(false);
2267 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002268}
2269
2270void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002271 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002272}
2273
2274PdfResult PdfInlineImageLooper::done() {
2275
2276 // TODO(edisonn): long to short names
2277 // TODO(edisonn): set properties in a map
2278 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
2279 // the stream.
2280
2281 SkBitmap bitmap;
2282 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
2283
2284 // TODO(edisonn): matrix use.
2285 // Draw dummy red square, to show the prezence of the inline image.
2286 fCanvas->drawBitmap(bitmap,
2287 SkDoubleToScalar(0),
2288 SkDoubleToScalar(0),
2289 NULL);
2290 return kNYI_PdfResult;
2291}
2292
2293PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2294 return fParent->consumeToken(token);
2295}
2296
2297void PdfCompatibilitySectionLooper::loop() {
2298 // TODO(edisonn): save stacks position, or create a new stack?
2299 // TODO(edisonn): what happens if we pop out more variables then when we started?
2300 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2301 // pop-ing too much will not affect outside the section.
2302 PdfToken token;
2303 while (readToken(fTokenizer, &token)) {
2304 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2305 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2306 looper->setUp(this);
2307 looper->loop();
2308 delete looper;
2309 } else {
2310 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2311 fParent->consumeToken(token);
2312 }
2313 }
2314 // TODO(edisonn): restore stack.
2315}
2316
2317// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2318// TODO(edisonn): Add API for Forms viewing and editing
2319// e.g. SkBitmap getPage(int page);
2320// int formsCount();
2321// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2322// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2323// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2324// resources needed, so the preview is fast.
2325// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2326// references automatically.
2327
edisonn@google.com222382b2013-07-10 22:33:10 +00002328PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002329
edisonn@google.com444e25a2013-07-11 15:20:50 +00002330bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002331 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002332 return false;
2333 }
2334
edisonn@google.com222382b2013-07-10 22:33:10 +00002335 if (page < 0 || page >= pages()) {
2336 return false;
2337 }
2338
edisonn@google.com222382b2013-07-10 22:33:10 +00002339 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002340
edisonn@google.com222382b2013-07-10 22:33:10 +00002341 pdfContext.fOriginalMatrix = SkMatrix::I();
2342 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2343
2344 gPdfContext = &pdfContext;
2345
2346 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002347 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002348 SkScalar w = dst.width();
2349 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002350
edisonn@google.com444e25a2013-07-11 15:20:50 +00002351 SkScalar wp = fPdfDoc->MediaBox(page).width();
2352 SkScalar hp = fPdfDoc->MediaBox(page).height();
2353
2354 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 +00002355// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2356
2357 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2358 // TODO(edisonn): add flagg for no clipping.
2359 // Use larger image to make sure we do not draw anything outside of page
2360 // could be used in tests.
2361
2362#ifdef PDF_DEBUG_3X
2363 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)};
2364#else
2365 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2366#endif
2367 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2368 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2369
2370 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2371 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2372
2373 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2374 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2375
2376 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2377 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2378
2379
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002380 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
2381 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2382 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002383
edisonn@google.com222382b2013-07-10 22:33:10 +00002384#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002385 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002386#endif
2387
edisonn@google.com444e25a2013-07-11 15:20:50 +00002388 canvas->setMatrix(pdfContext.fOriginalMatrix);
2389
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002390 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2391
2392 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002393// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002394// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002395// canvas->drawRect(rect, paint);
2396
edisonn@google.com222382b2013-07-10 22:33:10 +00002397
2398 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002399 return true;
2400}
edisonn@google.com222382b2013-07-10 22:33:10 +00002401
2402bool SkPdfRenderer::load(const SkString inputFileName) {
2403 unload();
2404
2405 // TODO(edisonn): create static function that could return NULL if there are errors
2406 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002407 if (fPdfDoc->pages() == 0) {
2408 delete fPdfDoc;
2409 fPdfDoc = NULL;
2410 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002411
2412 return fPdfDoc != NULL;
2413}
2414
edisonn@google.com147adb12013-07-24 15:56:19 +00002415bool SkPdfRenderer::load(SkStream* stream) {
2416 unload();
2417
2418 // TODO(edisonn): create static function that could return NULL if there are errors
2419 fPdfDoc = new SkNativeParsedPDF(stream);
2420 if (fPdfDoc->pages() == 0) {
2421 delete fPdfDoc;
2422 fPdfDoc = NULL;
2423 }
2424
2425 return fPdfDoc != NULL;
2426}
2427
2428
edisonn@google.com222382b2013-07-10 22:33:10 +00002429int SkPdfRenderer::pages() const {
2430 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2431}
2432
2433void SkPdfRenderer::unload() {
2434 delete fPdfDoc;
2435 fPdfDoc = NULL;
2436}
2437
2438SkRect SkPdfRenderer::MediaBox(int page) const {
2439 SkASSERT(fPdfDoc);
2440 return fPdfDoc->MediaBox(page);
2441}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002442
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002443size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002444 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2445}
edisonn@google.com147adb12013-07-24 15:56:19 +00002446
2447bool SkPDFNativeRenderToBitmap(SkStream* stream,
2448 SkBitmap* output,
2449 int page,
2450 SkPdfContent content,
2451 double dpi) {
2452 SkASSERT(page >= 0);
2453 SkPdfRenderer renderer;
2454 renderer.load(stream);
2455 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2456 return false;
2457 }
2458
2459 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2460
2461 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2462 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2463
2464 rect = SkRect::MakeWH(width, height);
2465
2466 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2467
2468 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2469 SkCanvas canvas(device);
2470
2471 return renderer.renderPage(page, &canvas, rect);
2472}