blob: 39cae9c7d6f4d4926deef87e92087eab856bbd35 [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
845 delete _ty;
846 delete vtx;
847 delete vty;
848
849 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000850}
851
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000852static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000853 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
854 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
855 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
856 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
857 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
858 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000859
860 double array[6];
861 array[0] = a;
862 array[1] = b;
863 array[2] = c;
864 array[3] = d;
865 array[4] = e;
866 array[5] = f;
867
868 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
869 matrix.postConcat(pdfContext->fGraphicsState.fMatrix);
870
871 // TODO(edisonn): Text positioning.
872 pdfContext->fGraphicsState.fMatrixTm = matrix;
873 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
874
875 return kPartial_PdfResult;
876}
877
878//— T* Move to the start of the next line. This operator has the same effect as the code
879//0 Tl Td
880//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000881static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000882 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
883 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000884
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000885 pdfContext->fObjectStack.push(zero);
886 pdfContext->fObjectStack.push(tl);
887
888 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
889
890 delete zero; // TODO(edisonn): do not alocate and delete constants!
891 delete tl;
892
893 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000894}
895
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000896static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000897 if (pdfContext->fGraphicsState.fPathClosed) {
898 pdfContext->fGraphicsState.fPath.reset();
899 pdfContext->fGraphicsState.fPathClosed = false;
900 }
901
edisonn@google.com571c70b2013-07-10 17:09:50 +0000902 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
903 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000904
905 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
906 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
907
908 return kOK_PdfResult;
909}
910
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000911static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000912 if (pdfContext->fGraphicsState.fPathClosed) {
913 pdfContext->fGraphicsState.fPath.reset();
914 pdfContext->fGraphicsState.fPathClosed = false;
915 }
916
edisonn@google.com571c70b2013-07-10 17:09:50 +0000917 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
918 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000919
920 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
921 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
922
923 return kOK_PdfResult;
924}
925
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000926static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000927 if (pdfContext->fGraphicsState.fPathClosed) {
928 pdfContext->fGraphicsState.fPath.reset();
929 pdfContext->fGraphicsState.fPathClosed = false;
930 }
931
edisonn@google.com571c70b2013-07-10 17:09:50 +0000932 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
933 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
934 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
935 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
936 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
937 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000938
939 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
940 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
941 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
942
943 pdfContext->fGraphicsState.fCurPosX = x3;
944 pdfContext->fGraphicsState.fCurPosY = y3;
945
946 return kOK_PdfResult;
947}
948
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000949static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000950 if (pdfContext->fGraphicsState.fPathClosed) {
951 pdfContext->fGraphicsState.fPath.reset();
952 pdfContext->fGraphicsState.fPathClosed = false;
953 }
954
edisonn@google.com571c70b2013-07-10 17:09:50 +0000955 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
956 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
957 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
958 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000959 double y1 = pdfContext->fGraphicsState.fCurPosY;
960 double x1 = pdfContext->fGraphicsState.fCurPosX;
961
962 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
963 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
964 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
965
966 pdfContext->fGraphicsState.fCurPosX = x3;
967 pdfContext->fGraphicsState.fCurPosY = y3;
968
969 return kOK_PdfResult;
970}
971
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000972static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000973 if (pdfContext->fGraphicsState.fPathClosed) {
974 pdfContext->fGraphicsState.fPath.reset();
975 pdfContext->fGraphicsState.fPathClosed = false;
976 }
977
edisonn@google.com571c70b2013-07-10 17:09:50 +0000978 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
979 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000980 double y2 = pdfContext->fGraphicsState.fCurPosY;
981 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000982 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
983 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000984
985 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
986 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
987 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
988
989 pdfContext->fGraphicsState.fCurPosX = x3;
990 pdfContext->fGraphicsState.fCurPosY = y3;
991
992 return kOK_PdfResult;
993}
994
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000995static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000996 if (pdfContext->fGraphicsState.fPathClosed) {
997 pdfContext->fGraphicsState.fPath.reset();
998 pdfContext->fGraphicsState.fPathClosed = false;
999 }
1000
edisonn@google.com571c70b2013-07-10 17:09:50 +00001001 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1002 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1003 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1004 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001005
1006 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1007 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1008
1009 pdfContext->fGraphicsState.fCurPosX = x;
1010 pdfContext->fGraphicsState.fCurPosY = y + height;
1011
1012 return kOK_PdfResult;
1013}
1014
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001015static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001016 pdfContext->fGraphicsState.fPath.close();
1017 return kOK_PdfResult;
1018}
1019
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001020static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001021 SkPath path = pdfContext->fGraphicsState.fPath;
1022
1023 if (close) {
1024 path.close();
1025 }
1026
1027 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1028
1029 SkPaint paint;
1030
1031 SkPoint line[2];
1032 if (fill && !stroke && path.isLine(line)) {
1033 paint.setStyle(SkPaint::kStroke_Style);
1034
1035 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1036 paint.setStrokeWidth(SkDoubleToScalar(0));
1037
1038 canvas->drawPath(path, paint);
1039 } else {
1040 if (fill) {
1041 paint.setStyle(SkPaint::kFill_Style);
1042 if (evenOdd) {
1043 path.setFillType(SkPath::kEvenOdd_FillType);
1044 }
1045
1046 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1047
1048 canvas->drawPath(path, paint);
1049 }
1050
1051 if (stroke) {
1052 paint.setStyle(SkPaint::kStroke_Style);
1053
1054 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1055
1056 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1057 canvas->drawPath(path, paint);
1058 }
1059 }
1060
1061 pdfContext->fGraphicsState.fPath.reset();
1062 // todo zoom ... other stuff ?
1063
1064 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1065#ifndef PDF_DEBUG_NO_CLIPING
1066 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1067#endif
1068 }
1069
1070 //pdfContext->fGraphicsState.fClipPath.reset();
1071 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1072
1073 return kPartial_PdfResult;
1074
1075}
1076
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001077static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001078 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1079}
1080
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001081static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001082 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1083}
1084
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001085static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001086 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1087}
1088
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001089static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001090 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1091}
1092
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001093static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001094 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1095}
1096
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001097static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001098 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1099}
1100
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001101static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001102 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1103}
1104
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001105static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001106 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1107}
1108
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001109static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001110 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1111}
1112
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001113static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001114 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1115 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1116#ifndef PDF_DEBUG_NO_CLIPING
1117 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1118#endif
1119 }
1120
1121 //pdfContext->fGraphicsState.fClipPath.reset();
1122 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1123
1124 pdfContext->fGraphicsState.fPathClosed = true;
1125
1126 return kOK_PdfResult;
1127}
1128
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001129static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001130 pdfContext->fGraphicsState.fTextBlock = true;
1131 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
1132 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
1133
1134 return kPartial_PdfResult;
1135}
1136
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001137static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001138 if (!pdfContext->fGraphicsState.fTextBlock) {
1139 return kIgnoreError_PdfResult;
1140 }
1141 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
1142 return kPartial_PdfResult;
1143}
1144
1145//font size Tf Set the text font, Tf
1146//, to font and the text font size, Tfs, to size. font is the name of a
1147//font resource in the Fontsubdictionary of the current resource dictionary; size is
1148//a number representing a scale factor. There is no initial value for either font or
1149//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001150static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001151 pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1152 const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001153
1154#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001155 printf("font name: %s\n", fontName);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001156#endif
1157
edisonn@google.com571c70b2013-07-10 17:09:50 +00001158 if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1159 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1160 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1161 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1162 // TODO(edisonn): try to recover and draw it any way?
1163 return kIgnoreError_PdfResult;
1164 }
1165 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1166
1167 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1168
1169 if (skfont) {
1170 pdfContext->fGraphicsState.fSkFont = skfont;
1171 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001172 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001173 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001174}
1175
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001176static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001177 if (!pdfContext->fGraphicsState.fTextBlock) {
1178 // TODO(edisonn): try to recover and draw it any way?
1179 return kIgnoreError_PdfResult;
1180 }
1181
1182 PdfResult ret = DrawText(pdfContext,
1183 pdfContext->fObjectStack.top(),
1184 canvas);
1185 pdfContext->fObjectStack.pop();
1186
1187 return ret;
1188}
1189
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001190static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001191 if (!pdfContext->fGraphicsState.fTextBlock) {
1192 // TODO(edisonn): try to recover and draw it any way?
1193 return kIgnoreError_PdfResult;
1194 }
1195
1196 PdfOp_T_star(pdfContext, canvas, looper);
1197 // Do not pop, and push, just transfer the param to Tj
1198 return PdfOp_Tj(pdfContext, canvas, looper);
1199}
1200
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001201static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001202 if (!pdfContext->fGraphicsState.fTextBlock) {
1203 // TODO(edisonn): try to recover and draw it any way?
1204 return kIgnoreError_PdfResult;
1205 }
1206
1207 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1208 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1209 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1210
1211 pdfContext->fObjectStack.push(aw);
1212 PdfOp_Tw(pdfContext, canvas, looper);
1213
1214 pdfContext->fObjectStack.push(ac);
1215 PdfOp_Tc(pdfContext, canvas, looper);
1216
1217 pdfContext->fObjectStack.push(str);
1218 PdfOp_quote(pdfContext, canvas, looper);
1219
1220 return kPartial_PdfResult;
1221}
1222
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001223static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001224 if (!pdfContext->fGraphicsState.fTextBlock) {
1225 // TODO(edisonn): try to recover and draw it any way?
1226 return kIgnoreError_PdfResult;
1227 }
1228
edisonn@google.com571c70b2013-07-10 17:09:50 +00001229 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001230 pdfContext->fObjectStack.pop();
1231
edisonn@google.com571c70b2013-07-10 17:09:50 +00001232 if (!array->isArray()) {
1233 return kIgnoreError_PdfResult;
1234 }
1235
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001236 for( int i=0; i<static_cast<int>(array->size()); i++ )
1237 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001238 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001239 SkPdfObject* obj = (*array)[i];
1240 DrawText(pdfContext,
1241 obj,
1242 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001243 } else if ((*array)[i]->isNumber()) {
1244 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001245 SkMatrix matrix;
1246 matrix.setAll(SkDoubleToScalar(1),
1247 SkDoubleToScalar(0),
1248 // TODO(edisonn): use writing mode, vertical/horizontal.
1249 SkDoubleToScalar(-dx), // amount is substracted!!!
1250 SkDoubleToScalar(0),
1251 SkDoubleToScalar(1),
1252 SkDoubleToScalar(0),
1253 SkDoubleToScalar(0),
1254 SkDoubleToScalar(0),
1255 SkDoubleToScalar(1));
1256
1257 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1258 }
1259 }
1260 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1261}
1262
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001263static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001264 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001265 return kOK_PdfResult;
1266}
1267
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001268static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001269 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1270}
1271
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001272static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001273 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1274}
1275
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001276static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001277 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001278// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001279
1280 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1281
1282 bool doubles = true;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001283 if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001284 doubles = false;
1285 }
1286
1287#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001288 printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001289#endif
1290
1291 for (int i = n - 1; i >= 0 ; i--) {
1292 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001293 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1294// } else {
1295// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001296 }
1297 }
1298
1299 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001300 // TODO(edisonn): do possible field values to enum at parsing time!
1301 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
1302 if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001303 colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2]));
1304 }
1305 return kPartial_PdfResult;
1306}
1307
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001308static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001309 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1310}
1311
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001312static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001313 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1314}
1315
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001316static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001317 //SkPdfString* name;
1318 if (pdfContext->fObjectStack.top()->isName()) {
1319 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001320 pdfContext->fObjectStack.pop();
1321 }
1322
1323 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1324 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1325
1326 return kPartial_PdfResult;
1327}
1328
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001329static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001330 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1331}
1332
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001333static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001334 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1335}
1336
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001337static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001338 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001339 return kNYI_PdfResult;
1340}
1341
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001342static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001343 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1344}
1345
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001346static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001347 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1348}
1349
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001350static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001351 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1352 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1353 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001354
1355 colorOperator->fColorSpace = "DeviceRGB";
1356 colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b));
1357 return kOK_PdfResult;
1358}
1359
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001360static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001361 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1362}
1363
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001364static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001365 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1366}
1367
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001368static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001369 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001370 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1371 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1372 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1373 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001374
1375 colorOperator->fColorSpace = "DeviceCMYK";
1376 // TODO(edisonn): Set color.
1377 return kNYI_PdfResult;
1378}
1379
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001380static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001381 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1382}
1383
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001384static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001385 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1386}
1387
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001388static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001389 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1390 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1391
1392 return kOK_PdfResult;
1393}
1394
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001395static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001396 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1397
1398#ifdef PDF_TRACE
1399 if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) {
1400 printf("CLIP IS RECT\n");
1401 }
1402#endif
1403
1404 // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd.
1405 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1406 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1407
1408 return kPartial_PdfResult;
1409}
1410
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001411static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001412 *looper = new PdfCompatibilitySectionLooper();
1413 return kOK_PdfResult;
1414}
1415
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001416static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001417#ifdef ASSERT_BAD_PDF_OPS
1418 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1419 // have the assert when testing good pdfs.
1420#endif
1421 return kIgnoreError_PdfResult;
1422}
1423
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001424static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001425 *looper = new PdfInlineImageLooper();
1426 return kOK_PdfResult;
1427}
1428
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001429static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001430#ifdef ASSERT_BAD_PDF_OPS
1431 SkASSERT(false); // must be processed in inline image looper, but let's
1432 // have the assert when testing good pdfs.
1433#endif
1434 return kIgnoreError_PdfResult;
1435}
1436
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001437static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438#ifdef ASSERT_BAD_PDF_OPS
1439 SkASSERT(false); // must be processed in inline image looper, but let's
1440 // have the assert when testing good pdfs.
1441#endif
1442 return kIgnoreError_PdfResult;
1443}
1444
1445//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001446static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001447 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001448 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1449
1450 return kOK_PdfResult;
1451}
1452
1453//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 +00001454static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001455 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001456 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001457
1458 return kNYI_PdfResult;
1459}
1460
1461//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 +00001462static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001463 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001464 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001465
1466 return kNYI_PdfResult;
1467}
1468
1469//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001470static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001471 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001472 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001473
1474 return kNYI_PdfResult;
1475}
1476
1477//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1478//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001479static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001480 pdfContext->fObjectStack.pop();
1481 pdfContext->fObjectStack.pop();
1482
1483 return kNYI_PdfResult;
1484}
1485
1486//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 +00001487static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001488 pdfContext->fObjectStack.pop();
1489
1490 return kNYI_PdfResult;
1491}
1492
1493//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1494//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1495//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001496static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001497 pdfContext->fObjectStack.pop();
1498
1499 return kNYI_PdfResult;
1500}
1501
1502//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1503//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 +00001504static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001505 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001506
1507#ifdef PDF_TRACE
1508 std::string str;
1509#endif
1510
1511 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001512 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001513
1514 if (extGStateDictionary == NULL) {
1515#ifdef PDF_TRACE
1516 printf("ExtGState is NULL!\n");
1517#endif
1518 return kIgnoreError_PdfResult;
1519 }
1520
edisonn@google.com571c70b2013-07-10 17:09:50 +00001521 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001522
edisonn@google.com571c70b2013-07-10 17:09:50 +00001523 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1524 return kIgnoreError_PdfResult;
1525 }
1526 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001527
1528 // TODO(edisonn): now load all those properties in graphic state.
1529 if (gs == NULL) {
1530 return kIgnoreError_PdfResult;
1531 }
1532
1533 if (gs->has_CA()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001534 pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001535 }
1536
1537 if (gs->has_ca()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001538 pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001539 }
1540
1541 if (gs->has_LW()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001542 pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001543 }
1544
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001545 return kNYI_PdfResult;
1546}
1547
1548//charSpace Tc Set the character spacing, Tc
1549//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1550//Initial value: 0.
1551PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001552 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001553 pdfContext->fGraphicsState.fCharSpace = charSpace;
1554
1555 return kOK_PdfResult;
1556}
1557
1558//wordSpace Tw Set the word spacing, T
1559//w
1560//, to wordSpace, which is a number expressed in unscaled
1561//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1562//value: 0.
1563PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001564 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001565 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1566
1567 return kOK_PdfResult;
1568}
1569
1570//scale Tz Set the horizontal scaling, Th
1571//, to (scale ˜ 100). scale is a number specifying the
1572//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001573static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001574 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001575
1576 return kNYI_PdfResult;
1577}
1578
1579//render Tr Set the text rendering mode, T
1580//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001581static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001582 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001583
1584 return kNYI_PdfResult;
1585}
1586//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1587//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001588static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001589 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001590
1591 return kNYI_PdfResult;
1592}
1593
1594//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001595static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001596 pdfContext->fObjectStack.pop();
1597 pdfContext->fObjectStack.pop();
1598
1599 return kNYI_PdfResult;
1600}
1601
1602//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001603static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001604 pdfContext->fObjectStack.pop();
1605 pdfContext->fObjectStack.pop();
1606 pdfContext->fObjectStack.pop();
1607 pdfContext->fObjectStack.pop();
1608 pdfContext->fObjectStack.pop();
1609 pdfContext->fObjectStack.pop();
1610
1611 return kNYI_PdfResult;
1612}
1613
1614//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001615static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001616 pdfContext->fObjectStack.pop();
1617
1618 return kNYI_PdfResult;
1619}
1620
1621//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001622static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001623 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001624
edisonn@google.com571c70b2013-07-10 17:09:50 +00001625 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001626
1627 if (xObject == NULL) {
1628#ifdef PDF_TRACE
1629 printf("XObject is NULL!\n");
1630#endif
1631 return kIgnoreError_PdfResult;
1632 }
1633
edisonn@google.com571c70b2013-07-10 17:09:50 +00001634 SkPdfObject* value = xObject->get(name);
1635 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001636
1637#ifdef PDF_TRACE
1638// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001639// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001640#endif
1641
edisonn@google.com571c70b2013-07-10 17:09:50 +00001642 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001643}
1644
1645//tag MP Designate a marked-content point. tag is a name object indicating the role or
1646//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001647static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001648 pdfContext->fObjectStack.pop();
1649
1650 return kNYI_PdfResult;
1651}
1652
1653//tag properties DP Designate a marked-content point with an associated property list. tag is a
1654//name object indicating the role or significance of the point; properties is
1655//either an inline dictionary containing the property list or a name object
1656//associated with it in the Properties subdictionary of the current resource
1657//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001658static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001659 pdfContext->fObjectStack.pop();
1660 pdfContext->fObjectStack.pop();
1661
1662 return kNYI_PdfResult;
1663}
1664
1665//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1666//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001667static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001668 pdfContext->fObjectStack.pop();
1669
1670 return kNYI_PdfResult;
1671}
1672
1673//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1674//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1675//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 +00001676static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001677 pdfContext->fObjectStack.pop();
1678 pdfContext->fObjectStack.pop();
1679
1680 return kNYI_PdfResult;
1681}
1682
1683//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001684static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001685 return kNYI_PdfResult;
1686}
1687
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001688static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001689 static bool gInitialized = false;
1690 if (gInitialized) {
1691 return;
1692 }
1693
1694 gPdfOps["q"] = PdfOp_q;
1695 gPdfOps["Q"] = PdfOp_Q;
1696 gPdfOps["cm"] = PdfOp_cm;
1697
1698 gPdfOps["TD"] = PdfOp_TD;
1699 gPdfOps["Td"] = PdfOp_Td;
1700 gPdfOps["Tm"] = PdfOp_Tm;
1701 gPdfOps["T*"] = PdfOp_T_star;
1702
1703 gPdfOps["m"] = PdfOp_m;
1704 gPdfOps["l"] = PdfOp_l;
1705 gPdfOps["c"] = PdfOp_c;
1706 gPdfOps["v"] = PdfOp_v;
1707 gPdfOps["y"] = PdfOp_y;
1708 gPdfOps["h"] = PdfOp_h;
1709 gPdfOps["re"] = PdfOp_re;
1710
1711 gPdfOps["S"] = PdfOp_S;
1712 gPdfOps["s"] = PdfOp_s;
1713 gPdfOps["f"] = PdfOp_f;
1714 gPdfOps["F"] = PdfOp_F;
1715 gPdfOps["f*"] = PdfOp_f_star;
1716 gPdfOps["B"] = PdfOp_B;
1717 gPdfOps["B*"] = PdfOp_B_star;
1718 gPdfOps["b"] = PdfOp_b;
1719 gPdfOps["b*"] = PdfOp_b_star;
1720 gPdfOps["n"] = PdfOp_n;
1721
1722 gPdfOps["BT"] = PdfOp_BT;
1723 gPdfOps["ET"] = PdfOp_ET;
1724
1725 gPdfOps["Tj"] = PdfOp_Tj;
1726 gPdfOps["'"] = PdfOp_quote;
1727 gPdfOps["\""] = PdfOp_doublequote;
1728 gPdfOps["TJ"] = PdfOp_TJ;
1729
1730 gPdfOps["CS"] = PdfOp_CS;
1731 gPdfOps["cs"] = PdfOp_cs;
1732 gPdfOps["SC"] = PdfOp_SC;
1733 gPdfOps["SCN"] = PdfOp_SCN;
1734 gPdfOps["sc"] = PdfOp_sc;
1735 gPdfOps["scn"] = PdfOp_scn;
1736 gPdfOps["G"] = PdfOp_G;
1737 gPdfOps["g"] = PdfOp_g;
1738 gPdfOps["RG"] = PdfOp_RG;
1739 gPdfOps["rg"] = PdfOp_rg;
1740 gPdfOps["K"] = PdfOp_K;
1741 gPdfOps["k"] = PdfOp_k;
1742
1743 gPdfOps["W"] = PdfOp_W;
1744 gPdfOps["W*"] = PdfOp_W_star;
1745
1746 gPdfOps["BX"] = PdfOp_BX;
1747 gPdfOps["EX"] = PdfOp_EX;
1748
1749 gPdfOps["BI"] = PdfOp_BI;
1750 gPdfOps["ID"] = PdfOp_ID;
1751 gPdfOps["EI"] = PdfOp_EI;
1752
1753 gPdfOps["w"] = PdfOp_w;
1754 gPdfOps["J"] = PdfOp_J;
1755 gPdfOps["j"] = PdfOp_j;
1756 gPdfOps["M"] = PdfOp_M;
1757 gPdfOps["d"] = PdfOp_d;
1758 gPdfOps["ri"] = PdfOp_ri;
1759 gPdfOps["i"] = PdfOp_i;
1760 gPdfOps["gs"] = PdfOp_gs;
1761
1762 gPdfOps["Tc"] = PdfOp_Tc;
1763 gPdfOps["Tw"] = PdfOp_Tw;
1764 gPdfOps["Tz"] = PdfOp_Tz;
1765 gPdfOps["TL"] = PdfOp_TL;
1766 gPdfOps["Tf"] = PdfOp_Tf;
1767 gPdfOps["Tr"] = PdfOp_Tr;
1768 gPdfOps["Ts"] = PdfOp_Ts;
1769
1770 gPdfOps["d0"] = PdfOp_d0;
1771 gPdfOps["d1"] = PdfOp_d1;
1772
1773 gPdfOps["sh"] = PdfOp_sh;
1774
1775 gPdfOps["Do"] = PdfOp_Do;
1776
1777 gPdfOps["MP"] = PdfOp_MP;
1778 gPdfOps["DP"] = PdfOp_DP;
1779 gPdfOps["BMC"] = PdfOp_BMC;
1780 gPdfOps["BDC"] = PdfOp_BDC;
1781 gPdfOps["EMC"] = PdfOp_EMC;
1782
1783 gInitialized = true;
1784}
1785
1786class InitPdfOps {
1787public:
1788 InitPdfOps() {
1789 initPdfOperatorRenderes();
1790 }
1791};
1792
1793InitPdfOps gInitPdfOps;
1794
1795void reportPdfRenderStats() {
1796 std::map<std::string, int>::iterator iter;
1797
1798 for (int i = 0 ; i < kCount_PdfResult; i++) {
1799 for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) {
1800 printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second);
1801 }
1802 }
1803}
1804
1805PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001806 char keyword[256];
1807
1808 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001809 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001810 strncpy(keyword, token.fKeyword, token.fKeywordLength);
1811 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001812 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com571c70b2013-07-10 17:09:50 +00001813 PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001814 if (pdfOperatorRenderer) {
1815 // caller, main work is done by pdfOperatorRenderer(...)
1816 PdfTokenLooper* childLooper = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001817 gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001818
1819 if (childLooper) {
1820 childLooper->setUp(this);
1821 childLooper->loop();
1822 delete childLooper;
1823 }
1824 } else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001825 gRenderStats[kUnsupported_PdfResult][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001826 }
1827 }
1828 else if (token.fType == kObject_TokenType)
1829 {
1830 fPdfContext->fObjectStack.push( token.fObject );
1831 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001832 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001833 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001834 return kIgnoreError_PdfResult;
1835 }
1836 return kOK_PdfResult;
1837}
1838
1839void PdfMainLooper::loop() {
1840 PdfToken token;
1841 while (readToken(fTokenizer, &token)) {
1842 consumeToken(token);
1843 }
1844}
1845
1846PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
1847 //pdfContext.fInlineImage.fKeyValuePairs[key] = value;
1848 return kNYI_PdfResult;
1849}
1850
1851void PdfInlineImageLooper::loop() {
1852 PdfToken token;
1853 while (readToken(fTokenizer, &token)) {
1854 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1855 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1856 looper->setUp(this);
1857 looper->loop();
1858 } else {
1859 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EI") == 0) {
1860 done();
1861 return;
1862 }
1863
1864 consumeToken(token);
1865 }
1866 }
1867 // TODO(edisonn): report error/warning, EOF without EI.
1868}
1869
1870PdfResult PdfInlineImageLooper::done() {
1871
1872 // TODO(edisonn): long to short names
1873 // TODO(edisonn): set properties in a map
1874 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
1875 // the stream.
1876
1877 SkBitmap bitmap;
1878 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
1879
1880 // TODO(edisonn): matrix use.
1881 // Draw dummy red square, to show the prezence of the inline image.
1882 fCanvas->drawBitmap(bitmap,
1883 SkDoubleToScalar(0),
1884 SkDoubleToScalar(0),
1885 NULL);
1886 return kNYI_PdfResult;
1887}
1888
1889PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
1890 return fParent->consumeToken(token);
1891}
1892
1893void PdfCompatibilitySectionLooper::loop() {
1894 // TODO(edisonn): save stacks position, or create a new stack?
1895 // TODO(edisonn): what happens if we pop out more variables then when we started?
1896 // restore them? fail? We could create a new operands stack for every new BX/EX section,
1897 // pop-ing too much will not affect outside the section.
1898 PdfToken token;
1899 while (readToken(fTokenizer, &token)) {
1900 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1901 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1902 looper->setUp(this);
1903 looper->loop();
1904 delete looper;
1905 } else {
1906 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
1907 fParent->consumeToken(token);
1908 }
1909 }
1910 // TODO(edisonn): restore stack.
1911}
1912
1913// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
1914// TODO(edisonn): Add API for Forms viewing and editing
1915// e.g. SkBitmap getPage(int page);
1916// int formsCount();
1917// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
1918// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
1919// if we load the first page, and we zoom to fit to screen horizontally, then load only those
1920// resources needed, so the preview is fast.
1921// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
1922// references automatically.
1923
edisonn@google.com222382b2013-07-10 22:33:10 +00001924PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001925
edisonn@google.com222382b2013-07-10 22:33:10 +00001926bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas) const {
1927 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001928 return false;
1929 }
1930
edisonn@google.com222382b2013-07-10 22:33:10 +00001931 if (page < 0 || page >= pages()) {
1932 return false;
1933 }
1934
1935 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page);
1936
1937 PdfContext pdfContext(fPdfDoc);
1938 pdfContext.fOriginalMatrix = SkMatrix::I();
1939 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
1940
1941 gPdfContext = &pdfContext;
1942
1943 // TODO(edisonn): get matrix stuff right.
1944 // TODO(edisonn): add DPI/scale/zoom.
1945 SkScalar z = SkIntToScalar(0);
1946 SkRect rect = fPdfDoc->MediaBox(page);
1947 SkScalar w = rect.width();
1948 SkScalar h = rect.height();
1949
1950 SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(w, z), SkPoint::Make(w, h), SkPoint::Make(z, h)};
1951// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
1952
1953 // TODO(edisonn): add flag for this app to create sourunding buffer zone
1954 // TODO(edisonn): add flagg for no clipping.
1955 // Use larger image to make sure we do not draw anything outside of page
1956 // could be used in tests.
1957
1958#ifdef PDF_DEBUG_3X
1959 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)};
1960#else
1961 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
1962#endif
1963 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
1964 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
1965
1966 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
1967 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
1968
1969 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
1970 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
1971
1972 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
1973 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
1974
1975
1976 pdfContext.fGraphicsState.fMatrix = pdfContext.fOriginalMatrix;
1977 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fMatrix;
1978 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fMatrix;
1979
1980 canvas->setMatrix(pdfContext.fOriginalMatrix);
1981
1982#ifndef PDF_DEBUG_NO_PAGE_CLIPING
1983 canvas->clipRect(SkRect::MakeXYWH(z, z, w, h), SkRegion::kIntersect_Op, true);
1984#endif
1985
1986// erase with red before?
1987// SkPaint paint;
1988// paint.setColor(SK_ColorRED);
1989// canvas->drawRect(rect, paint);
1990
1991 PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
1992 looper.loop();
1993
1994 delete tokenizer;
1995
1996 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001997 return true;
1998}
edisonn@google.com222382b2013-07-10 22:33:10 +00001999
2000bool SkPdfRenderer::load(const SkString inputFileName) {
2001 unload();
2002
2003 // TODO(edisonn): create static function that could return NULL if there are errors
2004 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
2005
2006 return fPdfDoc != NULL;
2007}
2008
2009int SkPdfRenderer::pages() const {
2010 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2011}
2012
2013void SkPdfRenderer::unload() {
2014 delete fPdfDoc;
2015 fPdfDoc = NULL;
2016}
2017
2018SkRect SkPdfRenderer::MediaBox(int page) const {
2019 SkASSERT(fPdfDoc);
2020 return fPdfDoc->MediaBox(page);
2021}