blob: 7c4b47465d962ff2fc323d02bbfc20fff0865e72 [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"
19#include "picture_utils.h"
20
21#include <iostream>
22#include <cstdio>
23#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000024#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000025
26__SK_FORCE_IMAGE_DECODER_LINKING;
27
28// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
29// keep for each object pos in file
30// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
31
32// TODO(edisonn): security - validate all the user input, all pdf!
33
edisonn@google.com6e49c342013-06-27 20:03:43 +000034// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
35// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000036
edisonn@google.com3aac1f92013-07-02 22:42:53 +000037// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
38/*
39#ifdef PDF_TRACE
40 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000041 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042 printf("Print Tf Resources: %s\n", str.c_str());
43#endif
44 */
45
edisonn@google.com131d4ee2013-06-26 17:48:12 +000046#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000047#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000048#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000049
50#include "SkPdfBasics.h"
51#include "SkPdfUtils.h"
52
53#include "SkPdfFont.h"
54
edisonn@google.com131d4ee2013-06-26 17:48:12 +000055/*
56 * TODO(edisonn):
57 * - all font types and all ppdf font features
58 * - word spacing
59 * - load font for baidu.pdf
60 * - load font for youtube.pdf
61 * - parser for pdf from the definition already available in pdfspec_autogen.py
62 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000063 * - 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 +000064 * - load gs/ especially smask and already known prop (skp) ... in progress
65 * - wrapper on classes for customizations? e.g.
66 * SkPdfPageObjectVanila - has only the basic loaders/getters
67 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
68 * need to find a nice object model for all this with constructors and factories
69 * - deal with inheritable automatically ?
70 * - deal with specific type in spec directly, add all dictionary types to known types
71*/
72
73using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000074
edisonn@google.com222382b2013-07-10 22:33:10 +000075
76
77// TODO(edisonn): Document PdfTokenLooper and subclasses.
78class PdfTokenLooper {
79protected:
80 PdfTokenLooper* fParent;
81 SkPdfNativeTokenizer* fTokenizer;
82 PdfContext* fPdfContext;
83 SkCanvas* fCanvas;
84
85public:
86 PdfTokenLooper(PdfTokenLooper* parent,
87 SkPdfNativeTokenizer* tokenizer,
88 PdfContext* pdfContext,
89 SkCanvas* canvas)
90 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
91
92 virtual ~PdfTokenLooper() {}
93
94 virtual PdfResult consumeToken(PdfToken& token) = 0;
95 virtual void loop() = 0;
96
97 void setUp(PdfTokenLooper* parent) {
98 fParent = parent;
99 fTokenizer = parent->fTokenizer;
100 fPdfContext = parent->fPdfContext;
101 fCanvas = parent->fCanvas;
102 }
103};
104
105class PdfMainLooper : public PdfTokenLooper {
106public:
107 PdfMainLooper(PdfTokenLooper* parent,
108 SkPdfNativeTokenizer* tokenizer,
109 PdfContext* pdfContext,
110 SkCanvas* canvas)
111 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
112
113 virtual PdfResult consumeToken(PdfToken& token);
114 virtual void loop();
115};
116
117class PdfInlineImageLooper : public PdfTokenLooper {
118public:
119 PdfInlineImageLooper()
120 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
121
122 virtual PdfResult consumeToken(PdfToken& token);
123 virtual void loop();
124 PdfResult done();
125};
126
127class PdfCompatibilitySectionLooper : public PdfTokenLooper {
128public:
129 PdfCompatibilitySectionLooper()
130 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
131
132 virtual PdfResult consumeToken(PdfToken& token);
133 virtual void loop();
134};
135
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000136// Utilities
137static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
138 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
139
140 bitmap->allocPixels();
141 bitmap->eraseColor(color);
142}
143
144// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000145static int GetColorSpaceComponents(const std::string& colorSpace) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000146 if (colorSpace == "DeviceCMYK") {
147 return 4;
148 } else if (colorSpace == "DeviceGray" ||
149 colorSpace == "CalGray" ||
150 colorSpace == "Indexed") {
151 return 1;
152 } else if (colorSpace == "DeviceRGB" ||
153 colorSpace == "CalRGB" ||
154 colorSpace == "Lab") {
155 return 3;
156 } else {
157 return 0;
158 }
159}
160
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000161SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000162 SkMatrix matrix;
163 matrix.setAll(SkDoubleToScalar(array[0]),
164 SkDoubleToScalar(array[2]),
165 SkDoubleToScalar(array[4]),
166 SkDoubleToScalar(array[1]),
167 SkDoubleToScalar(array[3]),
168 SkDoubleToScalar(array[5]),
169 SkDoubleToScalar(0),
170 SkDoubleToScalar(0),
171 SkDoubleToScalar(1));
172
173 return matrix;
174}
175
176SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
177 double array[6];
178
179 // TODO(edisonn): security issue, ret if size() != 6
180 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000181 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000182 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000183 return SkMatrix::I(); // TODO(edisonn): report issue
184 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000185 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000186 }
187
188 return SkMatrixFromPdfMatrix(array);
189}
190
edisonn@google.com222382b2013-07-10 22:33:10 +0000191
192extern "C" SkNativeParsedPDF* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000193SkBitmap* gDumpBitmap = NULL;
194SkCanvas* gDumpCanvas = NULL;
195char gLastKeyword[100] = "";
196int gLastOpKeyword = -1;
197char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
198int gReadOp = 0;
199
200
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000201#ifdef PDF_TRACE_DIFF_IN_PNG
202static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000203 return true;
204 if (*pdfOp == '\0') return false;
205
206 char markedPdfOp[100] = ",";
207 strcat(markedPdfOp, pdfOp);
208 strcat(markedPdfOp, ",");
209
210 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
211}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000212#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000213
edisonn@google.com222382b2013-07-10 22:33:10 +0000214
215
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000216// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000217static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000218 bool ret = fTokenizer->readToken(token);
219
220 gReadOp++;
221
222#ifdef PDF_TRACE_DIFF_IN_PNG
223 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
224 // the numbar and name of last operation, so the file name will reflect op that changed.
225 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
226 gDumpCanvas->flush();
227
228 SkBitmap bitmap;
229 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
230
231 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
232
233 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
234 SkCanvas canvas(device);
235
236 // draw context stuff here
237 SkPaint blueBorder;
238 blueBorder.setColor(SK_ColorBLUE);
239 blueBorder.setStyle(SkPaint::kStroke_Style);
240 blueBorder.setTextSize(SkDoubleToScalar(20));
241
242 SkString str;
243
244 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
245 if (clipStack) {
246 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
247 const SkClipStack::Element* elem;
248 double y = 0;
249 int total = 0;
250 while (elem = iter.next()) {
251 total++;
252 y += 30;
253
254 switch (elem->getType()) {
255 case SkClipStack::Element::kRect_Type:
256 canvas.drawRect(elem->getRect(), blueBorder);
257 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
258 break;
259 case SkClipStack::Element::kPath_Type:
260 canvas.drawPath(elem->getPath(), blueBorder);
261 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
262 break;
263 case SkClipStack::Element::kEmpty_Type:
264 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
265 break;
266 default:
267 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
268 break;
269 }
270 }
271
272 y += 30;
273 str.printf("Number of clips in stack: %i", total);
274 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
275 }
276
277 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
278 SkPath clipPath;
279 if (clipRegion.getBoundaryPath(&clipPath)) {
280 SkPaint redBorder;
281 redBorder.setColor(SK_ColorRED);
282 redBorder.setStyle(SkPaint::kStroke_Style);
283 canvas.drawPath(clipPath, redBorder);
284 }
285
286 canvas.flush();
287
288 SkString out;
289
290 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
291 // ... and other properties, to be able to debug th code easily
292
293 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
294 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
295 }
296
297 if (token->fType == kKeyword_TokenType) {
298 strcpy(gLastKeyword, token->fKeyword);
299 gLastOpKeyword = gReadOp;
300 } else {
301 strcpy(gLastKeyword, "");
302 }
303#endif
304
305 return ret;
306}
307
308
309
310typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
311
312map<std::string, PdfOperatorRenderer> gPdfOps;
313
314map<std::string, int> gRenderStats[kCount_PdfResult];
315
edisonn@google.com571c70b2013-07-10 17:09:50 +0000316const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000317 "Success",
318 "Partially implemented",
319 "Not yet implemented",
320 "Ignore Error",
321 "Error",
322 "Unsupported/Unknown"
323};
324
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000325static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000326 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000327 SkCanvas* canvas)
328{
329
330 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
331 if (skfont == NULL) {
332 skfont = SkPdfFont::Default();
333 }
334
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000335
edisonn@google.com571c70b2013-07-10 17:09:50 +0000336 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000337 // TODO(edisonn): report warning
338 return kIgnoreError_PdfResult;
339 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000340 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000341
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000342 SkUnencodedText binary(str);
343
344 SkDecodedText decoded;
345
346 if (skfont->encoding() == NULL) {
347 // TODO(edisonn): report warning
348 return kNYI_PdfResult;
349 }
350
351 skfont->encoding()->decodeText(binary, &decoded);
352
353 SkPaint paint;
354 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
355 // Or maybe just not call setTextSize at all?
356 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
357 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
358 }
359
360// if (fCurFont && fCurFont->GetFontScale() != 0) {
361// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
362// }
363
364 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
365
366 canvas->save();
367
368#if 1
369 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
370
371 SkPoint point1;
372 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
373
374 SkMatrix mirror;
375 mirror.setTranslate(0, -point1.y());
376 // TODO(edisonn): fix rotated text, and skewed too
377 mirror.postScale(SK_Scalar1, -SK_Scalar1);
378 // TODO(edisonn): post rotate, skew
379 mirror.postTranslate(0, point1.y());
380
381 matrix.postConcat(mirror);
382
383 canvas->setMatrix(matrix);
384
385 SkTraceMatrix(matrix, "mirrored");
386#endif
387
edisonn@google.com6e49c342013-06-27 20:03:43 +0000388 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000389 canvas->restore();
390
391 return kPartial_PdfResult;
392}
393
394// TODO(edisonn): create header files with declarations!
395PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
396PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
397PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
398PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
399
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000400// TODO(edisonn): perf!!!
401
402static SkColorTable* getGrayColortable() {
403 static SkColorTable* grayColortable = NULL;
404 if (grayColortable == NULL) {
405 SkPMColor* colors = new SkPMColor[256];
406 for (int i = 0 ; i < 256; i++) {
407 colors[i] = SkPreMultiplyARGB(255, i, i, i);
408 }
409 grayColortable = new SkColorTable(colors, 256);
410 }
411 return grayColortable;
412}
413
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000414static SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000415 int width, int height, int bytesPerLine,
416 int bpc, const std::string& colorSpace,
417 bool transparencyMask) {
418 SkBitmap bitmap;
419
edisonn@google.com571c70b2013-07-10 17:09:50 +0000420 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000421//#define MAX_COMPONENTS 10
422
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000423 // TODO(edisonn): assume start of lines are aligned at 32 bits?
424 // Is there a faster way to load the uncompressed stream into a bitmap?
425
426 // minimal support for now
427 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
428 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
429
430 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000431 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000432 for (int w = 0 ; w < width; w++) {
433 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
434 uncompressedStream[3 * w + 1],
435 uncompressedStream[3 * w + 2]);
436 i++;
437 }
438 uncompressedStream += bytesPerLine;
439 }
440
441 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
442 bitmap.setPixels(uncompressedStreamArgb);
443 }
444 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
445 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
446
447 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000448 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000449 for (int w = 0 ; w < width; w++) {
450 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
451 uncompressedStream[w];
452 i++;
453 }
454 uncompressedStream += bytesPerLine;
455 }
456
457 bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
458 width, height);
459 bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
460 }
461
462 // TODO(edisonn): Report Warning, NYI, or error
463 return bitmap;
464}
465
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000466// utils
467
468// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
469// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
470// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
471// skia format, through a table
472
473// this functions returns the image, it does not look at the smask.
474
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000475static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000476 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000477 // TODO(edisonn): report warning to be used in testing.
478 return SkBitmap();
479 }
480
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000481 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
482 int64_t width = image->Width(pdfContext->fPdfDoc);
483 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000484 std::string colorSpace = "DeviceRGB";
485
486 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000487 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
488 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000489 }
490
491/*
492 bool imageMask = image->imageMask();
493
494 if (imageMask) {
495 if (bpc != 0 && bpc != 1) {
496 // TODO(edisonn): report warning to be used in testing.
497 return SkBitmap();
498 }
499 bpc = 1;
500 }
501*/
502
edisonn@google.com571c70b2013-07-10 17:09:50 +0000503 unsigned char* uncompressedStream = NULL;
504 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000505
edisonn@google.com571c70b2013-07-10 17:09:50 +0000506 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000507
edisonn@google.com571c70b2013-07-10 17:09:50 +0000508 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000509 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000510 // TODO(edisonn): report warning to be used in testing.
511 return SkBitmap();
512 }
513
edisonn@google.com571c70b2013-07-10 17:09:50 +0000514 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
515
516 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
517 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
518 (streamDict->isFilterAArray(NULL) &&
519 streamDict->getFilterAsArray(NULL)->size() > 0 &&
520 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
521 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
522 SkBitmap bitmap;
523 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap);
524 return bitmap;
525 }
526
527
528
529 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
530// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
531// obj.GetDictionary().GetKey(PdfName("Filter")));
532// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
533// value = resolveReferenceObject(pdfContext->fPdfDoc,
534// &value->GetArray()[0]);
535// }
536// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
537// SkStream stream = SkStream::
538// SkImageDecoder::Factory()
539// }
540
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000541 int bytesPerLine = uncompressedStreamLength / height;
542#ifdef PDF_TRACE
543 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000544 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000545 }
546#endif
547
548 SkBitmap bitmap = transferImageStreamToBitmap(
549 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000550 (int)width, (int)height, bytesPerLine,
551 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000552 transparencyMask);
553
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000554 return bitmap;
555}
556
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000557static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000558 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000559
560 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000561 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000562 }
563
564 // TODO(edisonn): implement GS SMask. Default to empty right now.
565 return pdfContext->fGraphicsState.fSMask;
566}
567
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000568static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000569 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000570 return kIgnoreError_PdfResult;
571 }
572
573 SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false);
574 SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage);
575
576 canvas->save();
577 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000578
579#if 1
580 SkScalar z = SkIntToScalar(0);
581 SkScalar one = SkIntToScalar(1);
582
583 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
584 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
585 SkMatrix flip;
586 SkAssertResult(flip.setPolyToPoly(from, to, 4));
587 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fMatrix;
588 solveImageFlip.preConcat(flip);
589 canvas->setMatrix(solveImageFlip);
590#endif
591
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000592 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
593
594 if (sMask.empty()) {
595 canvas->drawBitmapRect(image, dst, NULL);
596 } else {
597 canvas->saveLayer(&dst, NULL);
598 canvas->drawBitmapRect(image, dst, NULL);
599 SkPaint xfer;
600 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
601 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
602 canvas->drawBitmapRect(sMask, dst, &xfer);
603 canvas->restore();
604 }
605
606 canvas->restore();
607
608 return kPartial_PdfResult;
609}
610
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000611
612
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000613
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000614static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000615 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000616 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000617 }
618
619 PdfOp_q(pdfContext, canvas, NULL);
620 canvas->save();
621
622
edisonn@google.com571c70b2013-07-10 17:09:50 +0000623 if (skobj->Resources(pdfContext->fPdfDoc)) {
624 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000625 }
626
627 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix");
628
edisonn@google.com571c70b2013-07-10 17:09:50 +0000629 if (skobj->has_Matrix()) {
630 pdfContext->fGraphicsState.fMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000631 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
632 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
633 // TODO(edisonn) reset matrixTm and matricTlm also?
634 }
635
636 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
637
638 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
639
edisonn@google.com571c70b2013-07-10 17:09:50 +0000640 if (skobj->has_BBox()) {
641 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000642 }
643
644 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
645 // For this PdfContentsTokenizer needs to be extended.
646
edisonn@google.com571c70b2013-07-10 17:09:50 +0000647 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000648
edisonn@google.com571c70b2013-07-10 17:09:50 +0000649 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000650 if (tokenizer != NULL) {
651 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
652 looper.loop();
653 delete tokenizer;
654 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000655
656 // TODO(edisonn): should we restore the variable stack at the same state?
657 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
658 canvas->restore();
659 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000660 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000661}
662
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000663//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
664// return kNYI_PdfResult;
665//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000666
edisonn@google.com571c70b2013-07-10 17:09:50 +0000667PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
668 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000669 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000670 }
671
672 PdfOp_q(pdfContext, canvas, NULL);
673 canvas->save();
674
675 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
676 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
677
678 pdfContext->fGraphicsState.fMatrix = pdfContext->fGraphicsState.fMatrixTm;
679 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
680
681 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
682
683 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
684
685 SkRect rm = bBox;
686 pdfContext->fGraphicsState.fMatrix.mapRect(&rm);
687
688 SkTraceRect(rm, "bbox mapped");
689
690 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
691
692 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
693 // For this PdfContentsTokenizer needs to be extended.
694
edisonn@google.com571c70b2013-07-10 17:09:50 +0000695 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000696
edisonn@google.com571c70b2013-07-10 17:09:50 +0000697 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000698 if (tokenizer != NULL) {
699 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
700 looper.loop();
701 delete tokenizer;
702 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000703
704 // TODO(edisonn): should we restore the variable stack at the same state?
705 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
706 canvas->restore();
707 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000708
709 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000710}
711
712
edisonn@google.com571c70b2013-07-10 17:09:50 +0000713// TODO(edisonn): make sure the pointer is unique
714std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000715
716class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000717 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000718public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000719 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000720 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000721 }
722
723 ~CheckRecursiveRendering() {
724 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000725 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000726 }
727
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000728 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000729 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000730 }
731};
732
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000733static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000734 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000735 // Oops, corrupt PDF!
736 return kIgnoreError_PdfResult;
737 }
738
edisonn@google.com571c70b2013-07-10 17:09:50 +0000739 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000740
edisonn@google.com571c70b2013-07-10 17:09:50 +0000741 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000742 {
743 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000744 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000745 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000746 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000747 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
748 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000749 default:
750 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000751 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000752}
753
754PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
755 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
756 canvas->save();
757 return kOK_PdfResult;
758}
759
760PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
761 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
762 pdfContext->fStateStack.pop();
763 canvas->restore();
764 return kOK_PdfResult;
765}
766
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000767static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000768 double array[6];
769 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000770 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000771 pdfContext->fObjectStack.pop();
772 }
773
774 // a b
775 // c d
776 // e f
777
778 // 0 1
779 // 2 3
780 // 4 5
781
782 // sx ky
783 // kx sy
784 // tx ty
785 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
786
787 pdfContext->fGraphicsState.fMatrix.preConcat(matrix);
788
789#ifdef PDF_TRACE
790 printf("cm ");
791 for (int i = 0 ; i < 6 ; i++) {
792 printf("%f ", array[i]);
793 }
794 printf("\n");
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000795 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000796#endif
797
798 return kOK_PdfResult;
799}
800
801//leading TL Set the text leading, Tl
802//, to leading, which is a number expressed in unscaled text
803//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000804static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000805 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000806
807 pdfContext->fGraphicsState.fTextLeading = ty;
808
809 return kOK_PdfResult;
810}
811
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000812static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000813 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
814 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000815
816 double array[6] = {1, 0, 0, 1, tx, ty};
817 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
818
819 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
820 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
821
822 return kPartial_PdfResult;
823}
824
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000825static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000826 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
827 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000828
edisonn@google.com571c70b2013-07-10 17:09:50 +0000829 // TODO(edisonn): Create factory methods or constructors so native is hidden
830 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000831 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000832
833 PdfOp_TL(pdfContext, canvas, looper);
834
edisonn@google.com571c70b2013-07-10 17:09:50 +0000835 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000836 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000837
edisonn@google.com571c70b2013-07-10 17:09:50 +0000838 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000839 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000840
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000841 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
842
843 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
844 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000845
846 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000847}
848
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000849static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000850 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
851 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
852 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
853 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
854 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
855 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000856
857 double array[6];
858 array[0] = a;
859 array[1] = b;
860 array[2] = c;
861 array[3] = d;
862 array[4] = e;
863 array[5] = f;
864
865 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
866 matrix.postConcat(pdfContext->fGraphicsState.fMatrix);
867
868 // TODO(edisonn): Text positioning.
869 pdfContext->fGraphicsState.fMatrixTm = matrix;
870 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
871
872 return kPartial_PdfResult;
873}
874
875//— T* Move to the start of the next line. This operator has the same effect as the code
876//0 Tl Td
877//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000878static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000879 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
880 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000881
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000882 pdfContext->fObjectStack.push(zero);
883 pdfContext->fObjectStack.push(tl);
884
885 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
886
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000887 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000888}
889
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000890static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000891 if (pdfContext->fGraphicsState.fPathClosed) {
892 pdfContext->fGraphicsState.fPath.reset();
893 pdfContext->fGraphicsState.fPathClosed = false;
894 }
895
edisonn@google.com571c70b2013-07-10 17:09:50 +0000896 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
897 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000898
899 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
900 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
901
902 return kOK_PdfResult;
903}
904
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000905static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000906 if (pdfContext->fGraphicsState.fPathClosed) {
907 pdfContext->fGraphicsState.fPath.reset();
908 pdfContext->fGraphicsState.fPathClosed = false;
909 }
910
edisonn@google.com571c70b2013-07-10 17:09:50 +0000911 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
912 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000913
914 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
915 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
916
917 return kOK_PdfResult;
918}
919
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000920static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000921 if (pdfContext->fGraphicsState.fPathClosed) {
922 pdfContext->fGraphicsState.fPath.reset();
923 pdfContext->fGraphicsState.fPathClosed = false;
924 }
925
edisonn@google.com571c70b2013-07-10 17:09:50 +0000926 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
927 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
928 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
929 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
930 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
931 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000932
933 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
934 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
935 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
936
937 pdfContext->fGraphicsState.fCurPosX = x3;
938 pdfContext->fGraphicsState.fCurPosY = y3;
939
940 return kOK_PdfResult;
941}
942
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000943static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000944 if (pdfContext->fGraphicsState.fPathClosed) {
945 pdfContext->fGraphicsState.fPath.reset();
946 pdfContext->fGraphicsState.fPathClosed = false;
947 }
948
edisonn@google.com571c70b2013-07-10 17:09:50 +0000949 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
950 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
951 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
952 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000953 double y1 = pdfContext->fGraphicsState.fCurPosY;
954 double x1 = pdfContext->fGraphicsState.fCurPosX;
955
956 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
957 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
958 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
959
960 pdfContext->fGraphicsState.fCurPosX = x3;
961 pdfContext->fGraphicsState.fCurPosY = y3;
962
963 return kOK_PdfResult;
964}
965
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000966static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000967 if (pdfContext->fGraphicsState.fPathClosed) {
968 pdfContext->fGraphicsState.fPath.reset();
969 pdfContext->fGraphicsState.fPathClosed = false;
970 }
971
edisonn@google.com571c70b2013-07-10 17:09:50 +0000972 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
973 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000974 double y2 = pdfContext->fGraphicsState.fCurPosY;
975 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000976 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
977 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000978
979 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
980 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
981 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
982
983 pdfContext->fGraphicsState.fCurPosX = x3;
984 pdfContext->fGraphicsState.fCurPosY = y3;
985
986 return kOK_PdfResult;
987}
988
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000989static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000990 if (pdfContext->fGraphicsState.fPathClosed) {
991 pdfContext->fGraphicsState.fPath.reset();
992 pdfContext->fGraphicsState.fPathClosed = false;
993 }
994
edisonn@google.com571c70b2013-07-10 17:09:50 +0000995 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
996 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
997 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
998 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000999
1000 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1001 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1002
1003 pdfContext->fGraphicsState.fCurPosX = x;
1004 pdfContext->fGraphicsState.fCurPosY = y + height;
1005
1006 return kOK_PdfResult;
1007}
1008
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001009static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001010 pdfContext->fGraphicsState.fPath.close();
1011 return kOK_PdfResult;
1012}
1013
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001014static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001015 SkPath path = pdfContext->fGraphicsState.fPath;
1016
1017 if (close) {
1018 path.close();
1019 }
1020
1021 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1022
1023 SkPaint paint;
1024
1025 SkPoint line[2];
1026 if (fill && !stroke && path.isLine(line)) {
1027 paint.setStyle(SkPaint::kStroke_Style);
1028
1029 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1030 paint.setStrokeWidth(SkDoubleToScalar(0));
1031
1032 canvas->drawPath(path, paint);
1033 } else {
1034 if (fill) {
1035 paint.setStyle(SkPaint::kFill_Style);
1036 if (evenOdd) {
1037 path.setFillType(SkPath::kEvenOdd_FillType);
1038 }
1039
1040 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1041
1042 canvas->drawPath(path, paint);
1043 }
1044
1045 if (stroke) {
1046 paint.setStyle(SkPaint::kStroke_Style);
1047
1048 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1049
1050 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1051 canvas->drawPath(path, paint);
1052 }
1053 }
1054
1055 pdfContext->fGraphicsState.fPath.reset();
1056 // todo zoom ... other stuff ?
1057
1058 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1059#ifndef PDF_DEBUG_NO_CLIPING
1060 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1061#endif
1062 }
1063
1064 //pdfContext->fGraphicsState.fClipPath.reset();
1065 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1066
1067 return kPartial_PdfResult;
1068
1069}
1070
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001071static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001072 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1073}
1074
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001075static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001076 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1077}
1078
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001079static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001080 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1081}
1082
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001083static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001084 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1085}
1086
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001087static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001088 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1089}
1090
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001091static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001092 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1093}
1094
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001095static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001096 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1097}
1098
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001099static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001100 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1101}
1102
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001103static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001104 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1105}
1106
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001107static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001108 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1109 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1110#ifndef PDF_DEBUG_NO_CLIPING
1111 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1112#endif
1113 }
1114
1115 //pdfContext->fGraphicsState.fClipPath.reset();
1116 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1117
1118 pdfContext->fGraphicsState.fPathClosed = true;
1119
1120 return kOK_PdfResult;
1121}
1122
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001123static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001124 pdfContext->fGraphicsState.fTextBlock = true;
1125 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
1126 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
1127
1128 return kPartial_PdfResult;
1129}
1130
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001131static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001132 if (!pdfContext->fGraphicsState.fTextBlock) {
1133 return kIgnoreError_PdfResult;
1134 }
1135 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
1136 return kPartial_PdfResult;
1137}
1138
1139//font size Tf Set the text font, Tf
1140//, to font and the text font size, Tfs, to size. font is the name of a
1141//font resource in the Fontsubdictionary of the current resource dictionary; size is
1142//a number representing a scale factor. There is no initial value for either font or
1143//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001144static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001145 pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1146 const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001147
1148#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001149 printf("font name: %s\n", fontName);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001150#endif
1151
edisonn@google.com571c70b2013-07-10 17:09:50 +00001152 if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1153 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1154 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1155 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1156 // TODO(edisonn): try to recover and draw it any way?
1157 return kIgnoreError_PdfResult;
1158 }
1159 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1160
1161 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1162
1163 if (skfont) {
1164 pdfContext->fGraphicsState.fSkFont = skfont;
1165 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001166 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001167 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001168}
1169
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001170static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001171 if (!pdfContext->fGraphicsState.fTextBlock) {
1172 // TODO(edisonn): try to recover and draw it any way?
1173 return kIgnoreError_PdfResult;
1174 }
1175
1176 PdfResult ret = DrawText(pdfContext,
1177 pdfContext->fObjectStack.top(),
1178 canvas);
1179 pdfContext->fObjectStack.pop();
1180
1181 return ret;
1182}
1183
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001184static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001185 if (!pdfContext->fGraphicsState.fTextBlock) {
1186 // TODO(edisonn): try to recover and draw it any way?
1187 return kIgnoreError_PdfResult;
1188 }
1189
1190 PdfOp_T_star(pdfContext, canvas, looper);
1191 // Do not pop, and push, just transfer the param to Tj
1192 return PdfOp_Tj(pdfContext, canvas, looper);
1193}
1194
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001195static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001196 if (!pdfContext->fGraphicsState.fTextBlock) {
1197 // TODO(edisonn): try to recover and draw it any way?
1198 return kIgnoreError_PdfResult;
1199 }
1200
1201 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1202 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1203 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1204
1205 pdfContext->fObjectStack.push(aw);
1206 PdfOp_Tw(pdfContext, canvas, looper);
1207
1208 pdfContext->fObjectStack.push(ac);
1209 PdfOp_Tc(pdfContext, canvas, looper);
1210
1211 pdfContext->fObjectStack.push(str);
1212 PdfOp_quote(pdfContext, canvas, looper);
1213
1214 return kPartial_PdfResult;
1215}
1216
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001217static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001218 if (!pdfContext->fGraphicsState.fTextBlock) {
1219 // TODO(edisonn): try to recover and draw it any way?
1220 return kIgnoreError_PdfResult;
1221 }
1222
edisonn@google.com571c70b2013-07-10 17:09:50 +00001223 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001224 pdfContext->fObjectStack.pop();
1225
edisonn@google.com571c70b2013-07-10 17:09:50 +00001226 if (!array->isArray()) {
1227 return kIgnoreError_PdfResult;
1228 }
1229
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001230 for( int i=0; i<static_cast<int>(array->size()); i++ )
1231 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001232 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001233 SkPdfObject* obj = (*array)[i];
1234 DrawText(pdfContext,
1235 obj,
1236 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001237 } else if ((*array)[i]->isNumber()) {
1238 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001239 SkMatrix matrix;
1240 matrix.setAll(SkDoubleToScalar(1),
1241 SkDoubleToScalar(0),
1242 // TODO(edisonn): use writing mode, vertical/horizontal.
1243 SkDoubleToScalar(-dx), // amount is substracted!!!
1244 SkDoubleToScalar(0),
1245 SkDoubleToScalar(1),
1246 SkDoubleToScalar(0),
1247 SkDoubleToScalar(0),
1248 SkDoubleToScalar(0),
1249 SkDoubleToScalar(1));
1250
1251 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1252 }
1253 }
1254 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1255}
1256
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001257static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001258 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001259 return kOK_PdfResult;
1260}
1261
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001262static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001263 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1264}
1265
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001266static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001267 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1268}
1269
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001270static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001271 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001272// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001273
1274 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1275
1276 bool doubles = true;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001277 if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001278 doubles = false;
1279 }
1280
1281#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001282 printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001283#endif
1284
1285 for (int i = n - 1; i >= 0 ; i--) {
1286 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001287 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1288// } else {
1289// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001290 }
1291 }
1292
1293 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001294 // TODO(edisonn): do possible field values to enum at parsing time!
1295 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
1296 if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001297 colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2]));
1298 }
1299 return kPartial_PdfResult;
1300}
1301
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001302static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001303 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1304}
1305
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001306static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001307 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1308}
1309
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001310static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001311 //SkPdfString* name;
1312 if (pdfContext->fObjectStack.top()->isName()) {
1313 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001314 pdfContext->fObjectStack.pop();
1315 }
1316
1317 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1318 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1319
1320 return kPartial_PdfResult;
1321}
1322
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001323static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001324 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1325}
1326
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001327static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001328 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1329}
1330
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001331static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001332 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001333 return kNYI_PdfResult;
1334}
1335
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001336static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001337 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1338}
1339
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001340static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001341 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1342}
1343
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001344static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001345 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1346 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1347 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001348
1349 colorOperator->fColorSpace = "DeviceRGB";
1350 colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b));
1351 return kOK_PdfResult;
1352}
1353
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001354static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001355 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1356}
1357
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001358static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001359 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1360}
1361
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001362static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001363 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001364 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1365 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1366 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1367 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001368
1369 colorOperator->fColorSpace = "DeviceCMYK";
1370 // TODO(edisonn): Set color.
1371 return kNYI_PdfResult;
1372}
1373
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001374static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001375 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1376}
1377
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001378static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001379 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1380}
1381
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001382static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001383 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1384 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1385
1386 return kOK_PdfResult;
1387}
1388
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001389static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001390 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1391
1392#ifdef PDF_TRACE
1393 if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) {
1394 printf("CLIP IS RECT\n");
1395 }
1396#endif
1397
1398 // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd.
1399 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1400 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1401
1402 return kPartial_PdfResult;
1403}
1404
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001405static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001406 *looper = new PdfCompatibilitySectionLooper();
1407 return kOK_PdfResult;
1408}
1409
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001410static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001411#ifdef ASSERT_BAD_PDF_OPS
1412 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1413 // have the assert when testing good pdfs.
1414#endif
1415 return kIgnoreError_PdfResult;
1416}
1417
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001418static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001419 *looper = new PdfInlineImageLooper();
1420 return kOK_PdfResult;
1421}
1422
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001423static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001424#ifdef ASSERT_BAD_PDF_OPS
1425 SkASSERT(false); // must be processed in inline image looper, but let's
1426 // have the assert when testing good pdfs.
1427#endif
1428 return kIgnoreError_PdfResult;
1429}
1430
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001431static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001432#ifdef ASSERT_BAD_PDF_OPS
1433 SkASSERT(false); // must be processed in inline image looper, but let's
1434 // have the assert when testing good pdfs.
1435#endif
1436 return kIgnoreError_PdfResult;
1437}
1438
1439//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001440static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001441 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001442 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1443
1444 return kOK_PdfResult;
1445}
1446
1447//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 +00001448static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001449 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001450 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001451
1452 return kNYI_PdfResult;
1453}
1454
1455//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 +00001456static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001457 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001458 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001459
1460 return kNYI_PdfResult;
1461}
1462
1463//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001464static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001465 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001466 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001467
1468 return kNYI_PdfResult;
1469}
1470
1471//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1472//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001473static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001474 pdfContext->fObjectStack.pop();
1475 pdfContext->fObjectStack.pop();
1476
1477 return kNYI_PdfResult;
1478}
1479
1480//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 +00001481static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001482 pdfContext->fObjectStack.pop();
1483
1484 return kNYI_PdfResult;
1485}
1486
1487//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1488//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1489//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001490static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001491 pdfContext->fObjectStack.pop();
1492
1493 return kNYI_PdfResult;
1494}
1495
1496//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1497//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 +00001498static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001499 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001500
1501#ifdef PDF_TRACE
1502 std::string str;
1503#endif
1504
1505 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001506 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001507
1508 if (extGStateDictionary == NULL) {
1509#ifdef PDF_TRACE
1510 printf("ExtGState is NULL!\n");
1511#endif
1512 return kIgnoreError_PdfResult;
1513 }
1514
edisonn@google.com571c70b2013-07-10 17:09:50 +00001515 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001516
edisonn@google.com571c70b2013-07-10 17:09:50 +00001517 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1518 return kIgnoreError_PdfResult;
1519 }
1520 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001521
1522 // TODO(edisonn): now load all those properties in graphic state.
1523 if (gs == NULL) {
1524 return kIgnoreError_PdfResult;
1525 }
1526
1527 if (gs->has_CA()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001528 pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001529 }
1530
1531 if (gs->has_ca()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001532 pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001533 }
1534
1535 if (gs->has_LW()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001536 pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001537 }
1538
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001539 return kNYI_PdfResult;
1540}
1541
1542//charSpace Tc Set the character spacing, Tc
1543//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1544//Initial value: 0.
1545PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001546 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001547 pdfContext->fGraphicsState.fCharSpace = charSpace;
1548
1549 return kOK_PdfResult;
1550}
1551
1552//wordSpace Tw Set the word spacing, T
1553//w
1554//, to wordSpace, which is a number expressed in unscaled
1555//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1556//value: 0.
1557PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001558 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001559 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1560
1561 return kOK_PdfResult;
1562}
1563
1564//scale Tz Set the horizontal scaling, Th
1565//, to (scale ˜ 100). scale is a number specifying the
1566//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001567static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001568 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001569
1570 return kNYI_PdfResult;
1571}
1572
1573//render Tr Set the text rendering mode, T
1574//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001575static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001576 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001577
1578 return kNYI_PdfResult;
1579}
1580//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1581//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001582static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001583 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001584
1585 return kNYI_PdfResult;
1586}
1587
1588//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001589static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001590 pdfContext->fObjectStack.pop();
1591 pdfContext->fObjectStack.pop();
1592
1593 return kNYI_PdfResult;
1594}
1595
1596//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001597static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001598 pdfContext->fObjectStack.pop();
1599 pdfContext->fObjectStack.pop();
1600 pdfContext->fObjectStack.pop();
1601 pdfContext->fObjectStack.pop();
1602 pdfContext->fObjectStack.pop();
1603 pdfContext->fObjectStack.pop();
1604
1605 return kNYI_PdfResult;
1606}
1607
1608//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001609static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001610 pdfContext->fObjectStack.pop();
1611
1612 return kNYI_PdfResult;
1613}
1614
1615//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001616static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001617 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001618
edisonn@google.com571c70b2013-07-10 17:09:50 +00001619 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001620
1621 if (xObject == NULL) {
1622#ifdef PDF_TRACE
1623 printf("XObject is NULL!\n");
1624#endif
1625 return kIgnoreError_PdfResult;
1626 }
1627
edisonn@google.com571c70b2013-07-10 17:09:50 +00001628 SkPdfObject* value = xObject->get(name);
1629 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001630
1631#ifdef PDF_TRACE
1632// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001633// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001634#endif
1635
edisonn@google.com571c70b2013-07-10 17:09:50 +00001636 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001637}
1638
1639//tag MP Designate a marked-content point. tag is a name object indicating the role or
1640//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001641static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001642 pdfContext->fObjectStack.pop();
1643
1644 return kNYI_PdfResult;
1645}
1646
1647//tag properties DP Designate a marked-content point with an associated property list. tag is a
1648//name object indicating the role or significance of the point; properties is
1649//either an inline dictionary containing the property list or a name object
1650//associated with it in the Properties subdictionary of the current resource
1651//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001652static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001653 pdfContext->fObjectStack.pop();
1654 pdfContext->fObjectStack.pop();
1655
1656 return kNYI_PdfResult;
1657}
1658
1659//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1660//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001661static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001662 pdfContext->fObjectStack.pop();
1663
1664 return kNYI_PdfResult;
1665}
1666
1667//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1668//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1669//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 +00001670static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001671 pdfContext->fObjectStack.pop();
1672 pdfContext->fObjectStack.pop();
1673
1674 return kNYI_PdfResult;
1675}
1676
1677//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001678static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001679 return kNYI_PdfResult;
1680}
1681
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001682static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001683 static bool gInitialized = false;
1684 if (gInitialized) {
1685 return;
1686 }
1687
1688 gPdfOps["q"] = PdfOp_q;
1689 gPdfOps["Q"] = PdfOp_Q;
1690 gPdfOps["cm"] = PdfOp_cm;
1691
1692 gPdfOps["TD"] = PdfOp_TD;
1693 gPdfOps["Td"] = PdfOp_Td;
1694 gPdfOps["Tm"] = PdfOp_Tm;
1695 gPdfOps["T*"] = PdfOp_T_star;
1696
1697 gPdfOps["m"] = PdfOp_m;
1698 gPdfOps["l"] = PdfOp_l;
1699 gPdfOps["c"] = PdfOp_c;
1700 gPdfOps["v"] = PdfOp_v;
1701 gPdfOps["y"] = PdfOp_y;
1702 gPdfOps["h"] = PdfOp_h;
1703 gPdfOps["re"] = PdfOp_re;
1704
1705 gPdfOps["S"] = PdfOp_S;
1706 gPdfOps["s"] = PdfOp_s;
1707 gPdfOps["f"] = PdfOp_f;
1708 gPdfOps["F"] = PdfOp_F;
1709 gPdfOps["f*"] = PdfOp_f_star;
1710 gPdfOps["B"] = PdfOp_B;
1711 gPdfOps["B*"] = PdfOp_B_star;
1712 gPdfOps["b"] = PdfOp_b;
1713 gPdfOps["b*"] = PdfOp_b_star;
1714 gPdfOps["n"] = PdfOp_n;
1715
1716 gPdfOps["BT"] = PdfOp_BT;
1717 gPdfOps["ET"] = PdfOp_ET;
1718
1719 gPdfOps["Tj"] = PdfOp_Tj;
1720 gPdfOps["'"] = PdfOp_quote;
1721 gPdfOps["\""] = PdfOp_doublequote;
1722 gPdfOps["TJ"] = PdfOp_TJ;
1723
1724 gPdfOps["CS"] = PdfOp_CS;
1725 gPdfOps["cs"] = PdfOp_cs;
1726 gPdfOps["SC"] = PdfOp_SC;
1727 gPdfOps["SCN"] = PdfOp_SCN;
1728 gPdfOps["sc"] = PdfOp_sc;
1729 gPdfOps["scn"] = PdfOp_scn;
1730 gPdfOps["G"] = PdfOp_G;
1731 gPdfOps["g"] = PdfOp_g;
1732 gPdfOps["RG"] = PdfOp_RG;
1733 gPdfOps["rg"] = PdfOp_rg;
1734 gPdfOps["K"] = PdfOp_K;
1735 gPdfOps["k"] = PdfOp_k;
1736
1737 gPdfOps["W"] = PdfOp_W;
1738 gPdfOps["W*"] = PdfOp_W_star;
1739
1740 gPdfOps["BX"] = PdfOp_BX;
1741 gPdfOps["EX"] = PdfOp_EX;
1742
1743 gPdfOps["BI"] = PdfOp_BI;
1744 gPdfOps["ID"] = PdfOp_ID;
1745 gPdfOps["EI"] = PdfOp_EI;
1746
1747 gPdfOps["w"] = PdfOp_w;
1748 gPdfOps["J"] = PdfOp_J;
1749 gPdfOps["j"] = PdfOp_j;
1750 gPdfOps["M"] = PdfOp_M;
1751 gPdfOps["d"] = PdfOp_d;
1752 gPdfOps["ri"] = PdfOp_ri;
1753 gPdfOps["i"] = PdfOp_i;
1754 gPdfOps["gs"] = PdfOp_gs;
1755
1756 gPdfOps["Tc"] = PdfOp_Tc;
1757 gPdfOps["Tw"] = PdfOp_Tw;
1758 gPdfOps["Tz"] = PdfOp_Tz;
1759 gPdfOps["TL"] = PdfOp_TL;
1760 gPdfOps["Tf"] = PdfOp_Tf;
1761 gPdfOps["Tr"] = PdfOp_Tr;
1762 gPdfOps["Ts"] = PdfOp_Ts;
1763
1764 gPdfOps["d0"] = PdfOp_d0;
1765 gPdfOps["d1"] = PdfOp_d1;
1766
1767 gPdfOps["sh"] = PdfOp_sh;
1768
1769 gPdfOps["Do"] = PdfOp_Do;
1770
1771 gPdfOps["MP"] = PdfOp_MP;
1772 gPdfOps["DP"] = PdfOp_DP;
1773 gPdfOps["BMC"] = PdfOp_BMC;
1774 gPdfOps["BDC"] = PdfOp_BDC;
1775 gPdfOps["EMC"] = PdfOp_EMC;
1776
1777 gInitialized = true;
1778}
1779
1780class InitPdfOps {
1781public:
1782 InitPdfOps() {
1783 initPdfOperatorRenderes();
1784 }
1785};
1786
1787InitPdfOps gInitPdfOps;
1788
1789void reportPdfRenderStats() {
1790 std::map<std::string, int>::iterator iter;
1791
1792 for (int i = 0 ; i < kCount_PdfResult; i++) {
1793 for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) {
1794 printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second);
1795 }
1796 }
1797}
1798
1799PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001800 char keyword[256];
1801
1802 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001803 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001804 strncpy(keyword, token.fKeyword, token.fKeywordLength);
1805 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001806 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com571c70b2013-07-10 17:09:50 +00001807 PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001808 if (pdfOperatorRenderer) {
1809 // caller, main work is done by pdfOperatorRenderer(...)
1810 PdfTokenLooper* childLooper = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001811 gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001812
1813 if (childLooper) {
1814 childLooper->setUp(this);
1815 childLooper->loop();
1816 delete childLooper;
1817 }
1818 } else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001819 gRenderStats[kUnsupported_PdfResult][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001820 }
1821 }
1822 else if (token.fType == kObject_TokenType)
1823 {
1824 fPdfContext->fObjectStack.push( token.fObject );
1825 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001826 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001827 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001828 return kIgnoreError_PdfResult;
1829 }
1830 return kOK_PdfResult;
1831}
1832
1833void PdfMainLooper::loop() {
1834 PdfToken token;
1835 while (readToken(fTokenizer, &token)) {
1836 consumeToken(token);
1837 }
1838}
1839
1840PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
1841 //pdfContext.fInlineImage.fKeyValuePairs[key] = value;
1842 return kNYI_PdfResult;
1843}
1844
1845void PdfInlineImageLooper::loop() {
1846 PdfToken token;
1847 while (readToken(fTokenizer, &token)) {
1848 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1849 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1850 looper->setUp(this);
1851 looper->loop();
1852 } else {
1853 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EI") == 0) {
1854 done();
1855 return;
1856 }
1857
1858 consumeToken(token);
1859 }
1860 }
1861 // TODO(edisonn): report error/warning, EOF without EI.
1862}
1863
1864PdfResult PdfInlineImageLooper::done() {
1865
1866 // TODO(edisonn): long to short names
1867 // TODO(edisonn): set properties in a map
1868 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
1869 // the stream.
1870
1871 SkBitmap bitmap;
1872 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
1873
1874 // TODO(edisonn): matrix use.
1875 // Draw dummy red square, to show the prezence of the inline image.
1876 fCanvas->drawBitmap(bitmap,
1877 SkDoubleToScalar(0),
1878 SkDoubleToScalar(0),
1879 NULL);
1880 return kNYI_PdfResult;
1881}
1882
1883PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
1884 return fParent->consumeToken(token);
1885}
1886
1887void PdfCompatibilitySectionLooper::loop() {
1888 // TODO(edisonn): save stacks position, or create a new stack?
1889 // TODO(edisonn): what happens if we pop out more variables then when we started?
1890 // restore them? fail? We could create a new operands stack for every new BX/EX section,
1891 // pop-ing too much will not affect outside the section.
1892 PdfToken token;
1893 while (readToken(fTokenizer, &token)) {
1894 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1895 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1896 looper->setUp(this);
1897 looper->loop();
1898 delete looper;
1899 } else {
1900 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
1901 fParent->consumeToken(token);
1902 }
1903 }
1904 // TODO(edisonn): restore stack.
1905}
1906
1907// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
1908// TODO(edisonn): Add API for Forms viewing and editing
1909// e.g. SkBitmap getPage(int page);
1910// int formsCount();
1911// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
1912// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
1913// if we load the first page, and we zoom to fit to screen horizontally, then load only those
1914// resources needed, so the preview is fast.
1915// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
1916// references automatically.
1917
edisonn@google.com222382b2013-07-10 22:33:10 +00001918PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001919
edisonn@google.com222382b2013-07-10 22:33:10 +00001920bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas) const {
1921 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001922 return false;
1923 }
1924
edisonn@google.com222382b2013-07-10 22:33:10 +00001925 if (page < 0 || page >= pages()) {
1926 return false;
1927 }
1928
1929 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page);
1930
1931 PdfContext pdfContext(fPdfDoc);
1932 pdfContext.fOriginalMatrix = SkMatrix::I();
1933 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
1934
1935 gPdfContext = &pdfContext;
1936
1937 // TODO(edisonn): get matrix stuff right.
1938 // TODO(edisonn): add DPI/scale/zoom.
1939 SkScalar z = SkIntToScalar(0);
1940 SkRect rect = fPdfDoc->MediaBox(page);
1941 SkScalar w = rect.width();
1942 SkScalar h = rect.height();
1943
1944 SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(w, z), SkPoint::Make(w, h), SkPoint::Make(z, h)};
1945// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
1946
1947 // TODO(edisonn): add flag for this app to create sourunding buffer zone
1948 // TODO(edisonn): add flagg for no clipping.
1949 // Use larger image to make sure we do not draw anything outside of page
1950 // could be used in tests.
1951
1952#ifdef PDF_DEBUG_3X
1953 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)};
1954#else
1955 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
1956#endif
1957 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
1958 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
1959
1960 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
1961 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
1962
1963 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
1964 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
1965
1966 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
1967 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
1968
1969
1970 pdfContext.fGraphicsState.fMatrix = pdfContext.fOriginalMatrix;
1971 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fMatrix;
1972 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fMatrix;
1973
1974 canvas->setMatrix(pdfContext.fOriginalMatrix);
1975
1976#ifndef PDF_DEBUG_NO_PAGE_CLIPING
1977 canvas->clipRect(SkRect::MakeXYWH(z, z, w, h), SkRegion::kIntersect_Op, true);
1978#endif
1979
1980// erase with red before?
1981// SkPaint paint;
1982// paint.setColor(SK_ColorRED);
1983// canvas->drawRect(rect, paint);
1984
1985 PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
1986 looper.loop();
1987
1988 delete tokenizer;
1989
1990 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001991 return true;
1992}
edisonn@google.com222382b2013-07-10 22:33:10 +00001993
1994bool SkPdfRenderer::load(const SkString inputFileName) {
1995 unload();
1996
1997 // TODO(edisonn): create static function that could return NULL if there are errors
1998 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
1999
2000 return fPdfDoc != NULL;
2001}
2002
2003int SkPdfRenderer::pages() const {
2004 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2005}
2006
2007void SkPdfRenderer::unload() {
2008 delete fPdfDoc;
2009 fPdfDoc = NULL;
2010}
2011
2012SkRect SkPdfRenderer::MediaBox(int page) const {
2013 SkASSERT(fPdfDoc);
2014 return fPdfDoc->MediaBox(page);
2015}