blob: e1f549ecb7f7101613c36ce02516c079161bc292 [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.com131d4ee2013-06-26 17:48:12 +000048#include "SkPdfParser.h"
49
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
75// Utilities
76static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
77 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
78
79 bitmap->allocPixels();
80 bitmap->eraseColor(color);
81}
82
83// TODO(edisonn): synonyms? DeviceRGB and RGB ...
84int GetColorSpaceComponents(const std::string& colorSpace) {
85 if (colorSpace == "DeviceCMYK") {
86 return 4;
87 } else if (colorSpace == "DeviceGray" ||
88 colorSpace == "CalGray" ||
89 colorSpace == "Indexed") {
90 return 1;
91 } else if (colorSpace == "DeviceRGB" ||
92 colorSpace == "CalRGB" ||
93 colorSpace == "Lab") {
94 return 3;
95 } else {
96 return 0;
97 }
98}
99
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000100SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000101 SkMatrix matrix;
102 matrix.setAll(SkDoubleToScalar(array[0]),
103 SkDoubleToScalar(array[2]),
104 SkDoubleToScalar(array[4]),
105 SkDoubleToScalar(array[1]),
106 SkDoubleToScalar(array[3]),
107 SkDoubleToScalar(array[5]),
108 SkDoubleToScalar(0),
109 SkDoubleToScalar(0),
110 SkDoubleToScalar(1));
111
112 return matrix;
113}
114
115SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
116 double array[6];
117
118 // TODO(edisonn): security issue, ret if size() != 6
119 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000120 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000121 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000122 return SkMatrix::I(); // TODO(edisonn): report issue
123 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000124 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000125 }
126
127 return SkMatrixFromPdfMatrix(array);
128}
129
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000130SkBitmap* gDumpBitmap = NULL;
131SkCanvas* gDumpCanvas = NULL;
132char gLastKeyword[100] = "";
133int gLastOpKeyword = -1;
134char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
135int gReadOp = 0;
136
137
138
139bool hasVisualEffect(const char* pdfOp) {
140 return true;
141 if (*pdfOp == '\0') return false;
142
143 char markedPdfOp[100] = ",";
144 strcat(markedPdfOp, pdfOp);
145 strcat(markedPdfOp, ",");
146
147 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
148}
149
150// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000151static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000152 bool ret = fTokenizer->readToken(token);
153
154 gReadOp++;
155
156#ifdef PDF_TRACE_DIFF_IN_PNG
157 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
158 // the numbar and name of last operation, so the file name will reflect op that changed.
159 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
160 gDumpCanvas->flush();
161
162 SkBitmap bitmap;
163 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
164
165 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
166
167 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
168 SkCanvas canvas(device);
169
170 // draw context stuff here
171 SkPaint blueBorder;
172 blueBorder.setColor(SK_ColorBLUE);
173 blueBorder.setStyle(SkPaint::kStroke_Style);
174 blueBorder.setTextSize(SkDoubleToScalar(20));
175
176 SkString str;
177
178 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
179 if (clipStack) {
180 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
181 const SkClipStack::Element* elem;
182 double y = 0;
183 int total = 0;
184 while (elem = iter.next()) {
185 total++;
186 y += 30;
187
188 switch (elem->getType()) {
189 case SkClipStack::Element::kRect_Type:
190 canvas.drawRect(elem->getRect(), blueBorder);
191 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
192 break;
193 case SkClipStack::Element::kPath_Type:
194 canvas.drawPath(elem->getPath(), blueBorder);
195 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
196 break;
197 case SkClipStack::Element::kEmpty_Type:
198 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
199 break;
200 default:
201 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
202 break;
203 }
204 }
205
206 y += 30;
207 str.printf("Number of clips in stack: %i", total);
208 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
209 }
210
211 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
212 SkPath clipPath;
213 if (clipRegion.getBoundaryPath(&clipPath)) {
214 SkPaint redBorder;
215 redBorder.setColor(SK_ColorRED);
216 redBorder.setStyle(SkPaint::kStroke_Style);
217 canvas.drawPath(clipPath, redBorder);
218 }
219
220 canvas.flush();
221
222 SkString out;
223
224 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
225 // ... and other properties, to be able to debug th code easily
226
227 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
228 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
229 }
230
231 if (token->fType == kKeyword_TokenType) {
232 strcpy(gLastKeyword, token->fKeyword);
233 gLastOpKeyword = gReadOp;
234 } else {
235 strcpy(gLastKeyword, "");
236 }
237#endif
238
239 return ret;
240}
241
242
243
244typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
245
246map<std::string, PdfOperatorRenderer> gPdfOps;
247
248map<std::string, int> gRenderStats[kCount_PdfResult];
249
edisonn@google.com571c70b2013-07-10 17:09:50 +0000250const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000251 "Success",
252 "Partially implemented",
253 "Not yet implemented",
254 "Ignore Error",
255 "Error",
256 "Unsupported/Unknown"
257};
258
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000259PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000260 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000261 SkCanvas* canvas)
262{
263
264 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
265 if (skfont == NULL) {
266 skfont = SkPdfFont::Default();
267 }
268
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000269
edisonn@google.com571c70b2013-07-10 17:09:50 +0000270 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000271 // TODO(edisonn): report warning
272 return kIgnoreError_PdfResult;
273 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000274 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000275
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000276 SkUnencodedText binary(str);
277
278 SkDecodedText decoded;
279
280 if (skfont->encoding() == NULL) {
281 // TODO(edisonn): report warning
282 return kNYI_PdfResult;
283 }
284
285 skfont->encoding()->decodeText(binary, &decoded);
286
287 SkPaint paint;
288 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
289 // Or maybe just not call setTextSize at all?
290 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
291 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
292 }
293
294// if (fCurFont && fCurFont->GetFontScale() != 0) {
295// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
296// }
297
298 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
299
300 canvas->save();
301
302#if 1
303 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
304
305 SkPoint point1;
306 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
307
308 SkMatrix mirror;
309 mirror.setTranslate(0, -point1.y());
310 // TODO(edisonn): fix rotated text, and skewed too
311 mirror.postScale(SK_Scalar1, -SK_Scalar1);
312 // TODO(edisonn): post rotate, skew
313 mirror.postTranslate(0, point1.y());
314
315 matrix.postConcat(mirror);
316
317 canvas->setMatrix(matrix);
318
319 SkTraceMatrix(matrix, "mirrored");
320#endif
321
edisonn@google.com6e49c342013-06-27 20:03:43 +0000322 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000323 canvas->restore();
324
325 return kPartial_PdfResult;
326}
327
328// TODO(edisonn): create header files with declarations!
329PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
330PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
331PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
332PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
333
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000334// TODO(edisonn): perf!!!
335
336static SkColorTable* getGrayColortable() {
337 static SkColorTable* grayColortable = NULL;
338 if (grayColortable == NULL) {
339 SkPMColor* colors = new SkPMColor[256];
340 for (int i = 0 ; i < 256; i++) {
341 colors[i] = SkPreMultiplyARGB(255, i, i, i);
342 }
343 grayColortable = new SkColorTable(colors, 256);
344 }
345 return grayColortable;
346}
347
edisonn@google.com571c70b2013-07-10 17:09:50 +0000348SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000349 int width, int height, int bytesPerLine,
350 int bpc, const std::string& colorSpace,
351 bool transparencyMask) {
352 SkBitmap bitmap;
353
edisonn@google.com571c70b2013-07-10 17:09:50 +0000354 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000355//#define MAX_COMPONENTS 10
356
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000357 // TODO(edisonn): assume start of lines are aligned at 32 bits?
358 // Is there a faster way to load the uncompressed stream into a bitmap?
359
360 // minimal support for now
361 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
362 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
363
364 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000365 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000366 for (int w = 0 ; w < width; w++) {
367 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
368 uncompressedStream[3 * w + 1],
369 uncompressedStream[3 * w + 2]);
370 i++;
371 }
372 uncompressedStream += bytesPerLine;
373 }
374
375 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
376 bitmap.setPixels(uncompressedStreamArgb);
377 }
378 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
379 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
380
381 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000382 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000383 for (int w = 0 ; w < width; w++) {
384 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
385 uncompressedStream[w];
386 i++;
387 }
388 uncompressedStream += bytesPerLine;
389 }
390
391 bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
392 width, height);
393 bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
394 }
395
396 // TODO(edisonn): Report Warning, NYI, or error
397 return bitmap;
398}
399
edisonn@google.com571c70b2013-07-10 17:09:50 +0000400bool transferImageStreamToARGB(unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000401 int width, int bytesPerLine,
402 int bpc, const std::string& colorSpace,
403 SkColor** uncompressedStreamArgb,
edisonn@google.com571c70b2013-07-10 17:09:50 +0000404 size_t* uncompressedStreamLengthInBytesArgb) {
405 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000406//#define MAX_COMPONENTS 10
407
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000408 // TODO(edisonn): assume start of lines are aligned at 32 bits?
409 int height = uncompressedStreamLength / bytesPerLine;
410
411 // minimal support for now
412 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
413 *uncompressedStreamLengthInBytesArgb = width * height * 4;
414 *uncompressedStreamArgb = (SkColor*)malloc(*uncompressedStreamLengthInBytesArgb);
415
416 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000417 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000418 for (int w = 0 ; w < width; w++) {
419 (*uncompressedStreamArgb)[i] = SkColorSetRGB(uncompressedStream[3 * w],
420 uncompressedStream[3 * w + 1],
421 uncompressedStream[3 * w + 2]);
422 i++;
423 }
424 uncompressedStream += bytesPerLine;
425 }
426 return true;
427 }
428
429 if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
430 *uncompressedStreamLengthInBytesArgb = width * height * 4;
431 *uncompressedStreamArgb = (SkColor*)malloc(*uncompressedStreamLengthInBytesArgb);
432
433 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000434 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000435 for (int w = 0 ; w < width; w++) {
436 (*uncompressedStreamArgb)[i] = SkColorSetRGB(uncompressedStream[w],
437 uncompressedStream[w],
438 uncompressedStream[w]);
439 i++;
440 }
441 uncompressedStream += bytesPerLine;
442 }
443 return true;
444 }
445
446 return false;
447}
448
449// utils
450
451// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
452// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
453// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
454// skia format, through a table
455
456// this functions returns the image, it does not look at the smask.
457
edisonn@google.com571c70b2013-07-10 17:09:50 +0000458SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
459 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000460 // TODO(edisonn): report warning to be used in testing.
461 return SkBitmap();
462 }
463
edisonn@google.com571c70b2013-07-10 17:09:50 +0000464 long bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
465 long width = image->Width(pdfContext->fPdfDoc);
466 long height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000467 std::string colorSpace = "DeviceRGB";
468
469 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000470 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
471 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000472 }
473
474/*
475 bool imageMask = image->imageMask();
476
477 if (imageMask) {
478 if (bpc != 0 && bpc != 1) {
479 // TODO(edisonn): report warning to be used in testing.
480 return SkBitmap();
481 }
482 bpc = 1;
483 }
484*/
485
edisonn@google.com571c70b2013-07-10 17:09:50 +0000486 unsigned char* uncompressedStream = NULL;
487 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000488
edisonn@google.com571c70b2013-07-10 17:09:50 +0000489 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000490
edisonn@google.com571c70b2013-07-10 17:09:50 +0000491 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000492 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000493 // TODO(edisonn): report warning to be used in testing.
494 return SkBitmap();
495 }
496
edisonn@google.com571c70b2013-07-10 17:09:50 +0000497 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
498
499 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
500 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
501 (streamDict->isFilterAArray(NULL) &&
502 streamDict->getFilterAsArray(NULL)->size() > 0 &&
503 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
504 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
505 SkBitmap bitmap;
506 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap);
507 return bitmap;
508 }
509
510
511
512 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
513// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
514// obj.GetDictionary().GetKey(PdfName("Filter")));
515// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
516// value = resolveReferenceObject(pdfContext->fPdfDoc,
517// &value->GetArray()[0]);
518// }
519// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
520// SkStream stream = SkStream::
521// SkImageDecoder::Factory()
522// }
523
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000524 int bytesPerLine = uncompressedStreamLength / height;
525#ifdef PDF_TRACE
526 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000527 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000528 }
529#endif
530
531 SkBitmap bitmap = transferImageStreamToBitmap(
532 (unsigned char*)uncompressedStream, uncompressedStreamLength,
533 width, height, bytesPerLine,
534 bpc, colorSpace,
535 transparencyMask);
536
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000537 return bitmap;
538}
539
edisonn@google.com571c70b2013-07-10 17:09:50 +0000540SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
541 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000542
543 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000544 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000545 }
546
547 // TODO(edisonn): implement GS SMask. Default to empty right now.
548 return pdfContext->fGraphicsState.fSMask;
549}
550
edisonn@google.com571c70b2013-07-10 17:09:50 +0000551PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000552 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000553 return kIgnoreError_PdfResult;
554 }
555
556 SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false);
557 SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage);
558
559 canvas->save();
560 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000561
562#if 1
563 SkScalar z = SkIntToScalar(0);
564 SkScalar one = SkIntToScalar(1);
565
566 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
567 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
568 SkMatrix flip;
569 SkAssertResult(flip.setPolyToPoly(from, to, 4));
570 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fMatrix;
571 solveImageFlip.preConcat(flip);
572 canvas->setMatrix(solveImageFlip);
573#endif
574
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000575 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
576
577 if (sMask.empty()) {
578 canvas->drawBitmapRect(image, dst, NULL);
579 } else {
580 canvas->saveLayer(&dst, NULL);
581 canvas->drawBitmapRect(image, dst, NULL);
582 SkPaint xfer;
583 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
584 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
585 canvas->drawBitmapRect(sMask, dst, &xfer);
586 canvas->restore();
587 }
588
589 canvas->restore();
590
591 return kPartial_PdfResult;
592}
593
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000594
595
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000596
597PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000598 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000599 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000600 }
601
602 PdfOp_q(pdfContext, canvas, NULL);
603 canvas->save();
604
605
edisonn@google.com571c70b2013-07-10 17:09:50 +0000606 if (skobj->Resources(pdfContext->fPdfDoc)) {
607 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000608 }
609
610 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix");
611
edisonn@google.com571c70b2013-07-10 17:09:50 +0000612 if (skobj->has_Matrix()) {
613 pdfContext->fGraphicsState.fMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000614 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
615 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
616 // TODO(edisonn) reset matrixTm and matricTlm also?
617 }
618
619 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
620
621 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
622
edisonn@google.com571c70b2013-07-10 17:09:50 +0000623 if (skobj->has_BBox()) {
624 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000625 }
626
627 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
628 // For this PdfContentsTokenizer needs to be extended.
629
edisonn@google.com571c70b2013-07-10 17:09:50 +0000630 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000631
edisonn@google.com571c70b2013-07-10 17:09:50 +0000632 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000633 if (tokenizer != NULL) {
634 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
635 looper.loop();
636 delete tokenizer;
637 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000638
639 // TODO(edisonn): should we restore the variable stack at the same state?
640 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
641 canvas->restore();
642 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000643 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000644}
645
edisonn@google.com571c70b2013-07-10 17:09:50 +0000646PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000647 return kNYI_PdfResult;
648}
649
edisonn@google.com571c70b2013-07-10 17:09:50 +0000650PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
651 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000652 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000653 }
654
655 PdfOp_q(pdfContext, canvas, NULL);
656 canvas->save();
657
658 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
659 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
660
661 pdfContext->fGraphicsState.fMatrix = pdfContext->fGraphicsState.fMatrixTm;
662 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
663
664 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
665
666 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
667
668 SkRect rm = bBox;
669 pdfContext->fGraphicsState.fMatrix.mapRect(&rm);
670
671 SkTraceRect(rm, "bbox mapped");
672
673 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
674
675 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
676 // For this PdfContentsTokenizer needs to be extended.
677
edisonn@google.com571c70b2013-07-10 17:09:50 +0000678 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000679
edisonn@google.com571c70b2013-07-10 17:09:50 +0000680 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000681 if (tokenizer != NULL) {
682 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
683 looper.loop();
684 delete tokenizer;
685 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000686
687 // TODO(edisonn): should we restore the variable stack at the same state?
688 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
689 canvas->restore();
690 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000691
692 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000693}
694
695
edisonn@google.com571c70b2013-07-10 17:09:50 +0000696// TODO(edisonn): make sure the pointer is unique
697std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000698
699class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000700 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000701public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000702 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000703 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000704 }
705
706 ~CheckRecursiveRendering() {
707 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000708 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000709 }
710
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000711 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000712 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000713 }
714};
715
edisonn@google.com571c70b2013-07-10 17:09:50 +0000716PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
717 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000718 // Oops, corrupt PDF!
719 return kIgnoreError_PdfResult;
720 }
721
edisonn@google.com571c70b2013-07-10 17:09:50 +0000722 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000723
edisonn@google.com571c70b2013-07-10 17:09:50 +0000724 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000725 {
726 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000727 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000728 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000729 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000730 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
731 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000732 default:
733 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000734 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000735}
736
737PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
738 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
739 canvas->save();
740 return kOK_PdfResult;
741}
742
743PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
744 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
745 pdfContext->fStateStack.pop();
746 canvas->restore();
747 return kOK_PdfResult;
748}
749
750PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
751 double array[6];
752 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000753 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000754 pdfContext->fObjectStack.pop();
755 }
756
757 // a b
758 // c d
759 // e f
760
761 // 0 1
762 // 2 3
763 // 4 5
764
765 // sx ky
766 // kx sy
767 // tx ty
768 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
769
770 pdfContext->fGraphicsState.fMatrix.preConcat(matrix);
771
772#ifdef PDF_TRACE
773 printf("cm ");
774 for (int i = 0 ; i < 6 ; i++) {
775 printf("%f ", array[i]);
776 }
777 printf("\n");
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000778 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000779#endif
780
781 return kOK_PdfResult;
782}
783
784//leading TL Set the text leading, Tl
785//, to leading, which is a number expressed in unscaled text
786//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
787PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000788 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000789
790 pdfContext->fGraphicsState.fTextLeading = ty;
791
792 return kOK_PdfResult;
793}
794
795PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000796 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
797 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000798
799 double array[6] = {1, 0, 0, 1, tx, ty};
800 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
801
802 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
803 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
804
805 return kPartial_PdfResult;
806}
807
808PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000809 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
810 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000811
edisonn@google.com571c70b2013-07-10 17:09:50 +0000812 // TODO(edisonn): Create factory methods or constructors so native is hidden
813 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000814 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000815
816 PdfOp_TL(pdfContext, canvas, looper);
817
edisonn@google.com571c70b2013-07-10 17:09:50 +0000818 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000819 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000820
edisonn@google.com571c70b2013-07-10 17:09:50 +0000821 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000822 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000823
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000824 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
825
826 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
827 // and the cleanup can happen while the user looks at the image
828 delete _ty;
829 delete vtx;
830 delete vty;
831
832 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000833}
834
835PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000836 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
837 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
838 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
839 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
840 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
841 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000842
843 double array[6];
844 array[0] = a;
845 array[1] = b;
846 array[2] = c;
847 array[3] = d;
848 array[4] = e;
849 array[5] = f;
850
851 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
852 matrix.postConcat(pdfContext->fGraphicsState.fMatrix);
853
854 // TODO(edisonn): Text positioning.
855 pdfContext->fGraphicsState.fMatrixTm = matrix;
856 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
857
858 return kPartial_PdfResult;
859}
860
861//— T* Move to the start of the next line. This operator has the same effect as the code
862//0 Tl Td
863//where Tl is the current leading parameter in the text state
864PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000865 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
866 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000867
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000868 pdfContext->fObjectStack.push(zero);
869 pdfContext->fObjectStack.push(tl);
870
871 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
872
873 delete zero; // TODO(edisonn): do not alocate and delete constants!
874 delete tl;
875
876 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000877}
878
879PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
880 if (pdfContext->fGraphicsState.fPathClosed) {
881 pdfContext->fGraphicsState.fPath.reset();
882 pdfContext->fGraphicsState.fPathClosed = false;
883 }
884
edisonn@google.com571c70b2013-07-10 17:09:50 +0000885 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
886 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000887
888 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
889 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
890
891 return kOK_PdfResult;
892}
893
894PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
895 if (pdfContext->fGraphicsState.fPathClosed) {
896 pdfContext->fGraphicsState.fPath.reset();
897 pdfContext->fGraphicsState.fPathClosed = false;
898 }
899
edisonn@google.com571c70b2013-07-10 17:09:50 +0000900 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
901 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000902
903 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
904 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
905
906 return kOK_PdfResult;
907}
908
909PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
910 if (pdfContext->fGraphicsState.fPathClosed) {
911 pdfContext->fGraphicsState.fPath.reset();
912 pdfContext->fGraphicsState.fPathClosed = false;
913 }
914
edisonn@google.com571c70b2013-07-10 17:09:50 +0000915 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
916 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
917 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
918 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
919 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
920 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000921
922 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
923 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
924 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
925
926 pdfContext->fGraphicsState.fCurPosX = x3;
927 pdfContext->fGraphicsState.fCurPosY = y3;
928
929 return kOK_PdfResult;
930}
931
932PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
933 if (pdfContext->fGraphicsState.fPathClosed) {
934 pdfContext->fGraphicsState.fPath.reset();
935 pdfContext->fGraphicsState.fPathClosed = false;
936 }
937
edisonn@google.com571c70b2013-07-10 17:09:50 +0000938 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
939 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
940 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
941 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000942 double y1 = pdfContext->fGraphicsState.fCurPosY;
943 double x1 = pdfContext->fGraphicsState.fCurPosX;
944
945 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
946 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
947 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
948
949 pdfContext->fGraphicsState.fCurPosX = x3;
950 pdfContext->fGraphicsState.fCurPosY = y3;
951
952 return kOK_PdfResult;
953}
954
955PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
956 if (pdfContext->fGraphicsState.fPathClosed) {
957 pdfContext->fGraphicsState.fPath.reset();
958 pdfContext->fGraphicsState.fPathClosed = false;
959 }
960
edisonn@google.com571c70b2013-07-10 17:09:50 +0000961 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
962 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000963 double y2 = pdfContext->fGraphicsState.fCurPosY;
964 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000965 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
966 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000967
968 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
969 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
970 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
971
972 pdfContext->fGraphicsState.fCurPosX = x3;
973 pdfContext->fGraphicsState.fCurPosY = y3;
974
975 return kOK_PdfResult;
976}
977
978PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
979 if (pdfContext->fGraphicsState.fPathClosed) {
980 pdfContext->fGraphicsState.fPath.reset();
981 pdfContext->fGraphicsState.fPathClosed = false;
982 }
983
edisonn@google.com571c70b2013-07-10 17:09:50 +0000984 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
985 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
986 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
987 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000988
989 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
990 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
991
992 pdfContext->fGraphicsState.fCurPosX = x;
993 pdfContext->fGraphicsState.fCurPosY = y + height;
994
995 return kOK_PdfResult;
996}
997
998PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
999 pdfContext->fGraphicsState.fPath.close();
1000 return kOK_PdfResult;
1001}
1002
1003PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
1004 SkPath path = pdfContext->fGraphicsState.fPath;
1005
1006 if (close) {
1007 path.close();
1008 }
1009
1010 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1011
1012 SkPaint paint;
1013
1014 SkPoint line[2];
1015 if (fill && !stroke && path.isLine(line)) {
1016 paint.setStyle(SkPaint::kStroke_Style);
1017
1018 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1019 paint.setStrokeWidth(SkDoubleToScalar(0));
1020
1021 canvas->drawPath(path, paint);
1022 } else {
1023 if (fill) {
1024 paint.setStyle(SkPaint::kFill_Style);
1025 if (evenOdd) {
1026 path.setFillType(SkPath::kEvenOdd_FillType);
1027 }
1028
1029 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1030
1031 canvas->drawPath(path, paint);
1032 }
1033
1034 if (stroke) {
1035 paint.setStyle(SkPaint::kStroke_Style);
1036
1037 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1038
1039 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1040 canvas->drawPath(path, paint);
1041 }
1042 }
1043
1044 pdfContext->fGraphicsState.fPath.reset();
1045 // todo zoom ... other stuff ?
1046
1047 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1048#ifndef PDF_DEBUG_NO_CLIPING
1049 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1050#endif
1051 }
1052
1053 //pdfContext->fGraphicsState.fClipPath.reset();
1054 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1055
1056 return kPartial_PdfResult;
1057
1058}
1059
1060PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1061 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1062}
1063
1064PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1065 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1066}
1067
1068PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1069 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1070}
1071
1072PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1073 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1074}
1075
1076PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1077 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1078}
1079
1080PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1081 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1082}
1083
1084PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1085 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1086}
1087
1088PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1089 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1090}
1091
1092PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1093 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1094}
1095
1096PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1097 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1098 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1099#ifndef PDF_DEBUG_NO_CLIPING
1100 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1101#endif
1102 }
1103
1104 //pdfContext->fGraphicsState.fClipPath.reset();
1105 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1106
1107 pdfContext->fGraphicsState.fPathClosed = true;
1108
1109 return kOK_PdfResult;
1110}
1111
1112PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1113 pdfContext->fGraphicsState.fTextBlock = true;
1114 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
1115 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
1116
1117 return kPartial_PdfResult;
1118}
1119
1120PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1121 if (!pdfContext->fGraphicsState.fTextBlock) {
1122 return kIgnoreError_PdfResult;
1123 }
1124 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
1125 return kPartial_PdfResult;
1126}
1127
1128//font size Tf Set the text font, Tf
1129//, to font and the text font size, Tfs, to size. font is the name of a
1130//font resource in the Fontsubdictionary of the current resource dictionary; size is
1131//a number representing a scale factor. There is no initial value for either font or
1132//size; they must be specified explicitly using Tf before any text is shown.
1133PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001134 pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1135 const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001136
1137#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001138 printf("font name: %s\n", fontName);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001139#endif
1140
edisonn@google.com571c70b2013-07-10 17:09:50 +00001141 if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1142 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1143 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1144 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1145 // TODO(edisonn): try to recover and draw it any way?
1146 return kIgnoreError_PdfResult;
1147 }
1148 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1149
1150 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1151
1152 if (skfont) {
1153 pdfContext->fGraphicsState.fSkFont = skfont;
1154 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001155 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001156 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001157}
1158
1159PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1160 if (!pdfContext->fGraphicsState.fTextBlock) {
1161 // TODO(edisonn): try to recover and draw it any way?
1162 return kIgnoreError_PdfResult;
1163 }
1164
1165 PdfResult ret = DrawText(pdfContext,
1166 pdfContext->fObjectStack.top(),
1167 canvas);
1168 pdfContext->fObjectStack.pop();
1169
1170 return ret;
1171}
1172
1173PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1174 if (!pdfContext->fGraphicsState.fTextBlock) {
1175 // TODO(edisonn): try to recover and draw it any way?
1176 return kIgnoreError_PdfResult;
1177 }
1178
1179 PdfOp_T_star(pdfContext, canvas, looper);
1180 // Do not pop, and push, just transfer the param to Tj
1181 return PdfOp_Tj(pdfContext, canvas, looper);
1182}
1183
1184PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1185 if (!pdfContext->fGraphicsState.fTextBlock) {
1186 // TODO(edisonn): try to recover and draw it any way?
1187 return kIgnoreError_PdfResult;
1188 }
1189
1190 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1191 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1192 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1193
1194 pdfContext->fObjectStack.push(aw);
1195 PdfOp_Tw(pdfContext, canvas, looper);
1196
1197 pdfContext->fObjectStack.push(ac);
1198 PdfOp_Tc(pdfContext, canvas, looper);
1199
1200 pdfContext->fObjectStack.push(str);
1201 PdfOp_quote(pdfContext, canvas, looper);
1202
1203 return kPartial_PdfResult;
1204}
1205
1206PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1207 if (!pdfContext->fGraphicsState.fTextBlock) {
1208 // TODO(edisonn): try to recover and draw it any way?
1209 return kIgnoreError_PdfResult;
1210 }
1211
edisonn@google.com571c70b2013-07-10 17:09:50 +00001212 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001213 pdfContext->fObjectStack.pop();
1214
edisonn@google.com571c70b2013-07-10 17:09:50 +00001215 if (!array->isArray()) {
1216 return kIgnoreError_PdfResult;
1217 }
1218
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001219 for( int i=0; i<static_cast<int>(array->size()); i++ )
1220 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001221 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001222 SkPdfObject* obj = (*array)[i];
1223 DrawText(pdfContext,
1224 obj,
1225 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001226 } else if ((*array)[i]->isNumber()) {
1227 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001228 SkMatrix matrix;
1229 matrix.setAll(SkDoubleToScalar(1),
1230 SkDoubleToScalar(0),
1231 // TODO(edisonn): use writing mode, vertical/horizontal.
1232 SkDoubleToScalar(-dx), // amount is substracted!!!
1233 SkDoubleToScalar(0),
1234 SkDoubleToScalar(1),
1235 SkDoubleToScalar(0),
1236 SkDoubleToScalar(0),
1237 SkDoubleToScalar(0),
1238 SkDoubleToScalar(1));
1239
1240 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1241 }
1242 }
1243 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1244}
1245
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001246PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001247 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001248 return kOK_PdfResult;
1249}
1250
1251PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1252 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1253}
1254
1255PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1256 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1257}
1258
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001259PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001260 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001261// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001262
1263 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1264
1265 bool doubles = true;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001266 if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001267 doubles = false;
1268 }
1269
1270#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001271 printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001272#endif
1273
1274 for (int i = n - 1; i >= 0 ; i--) {
1275 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001276 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1277// } else {
1278// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001279 }
1280 }
1281
1282 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001283 // TODO(edisonn): do possible field values to enum at parsing time!
1284 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
1285 if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001286 colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2]));
1287 }
1288 return kPartial_PdfResult;
1289}
1290
1291PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1292 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1293}
1294
1295PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1296 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1297}
1298
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001299PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001300 //SkPdfString* name;
1301 if (pdfContext->fObjectStack.top()->isName()) {
1302 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001303 pdfContext->fObjectStack.pop();
1304 }
1305
1306 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1307 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1308
1309 return kPartial_PdfResult;
1310}
1311
1312PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1313 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1314}
1315
1316PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1317 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1318}
1319
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001320PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001321 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001322 return kNYI_PdfResult;
1323}
1324
1325PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1326 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1327}
1328
1329PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1330 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1331}
1332
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001333PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001334 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1335 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1336 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001337
1338 colorOperator->fColorSpace = "DeviceRGB";
1339 colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b));
1340 return kOK_PdfResult;
1341}
1342
1343PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1344 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1345}
1346
1347PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1348 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1349}
1350
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001351PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001352 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001353 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1354 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1355 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1356 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001357
1358 colorOperator->fColorSpace = "DeviceCMYK";
1359 // TODO(edisonn): Set color.
1360 return kNYI_PdfResult;
1361}
1362
1363PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1364 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1365}
1366
1367PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1368 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1369}
1370
1371PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1372 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1373 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1374
1375 return kOK_PdfResult;
1376}
1377
1378PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1379 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1380
1381#ifdef PDF_TRACE
1382 if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) {
1383 printf("CLIP IS RECT\n");
1384 }
1385#endif
1386
1387 // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd.
1388 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1389 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1390
1391 return kPartial_PdfResult;
1392}
1393
1394PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1395 *looper = new PdfCompatibilitySectionLooper();
1396 return kOK_PdfResult;
1397}
1398
1399PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1400#ifdef ASSERT_BAD_PDF_OPS
1401 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1402 // have the assert when testing good pdfs.
1403#endif
1404 return kIgnoreError_PdfResult;
1405}
1406
1407PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1408 *looper = new PdfInlineImageLooper();
1409 return kOK_PdfResult;
1410}
1411
1412PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1413#ifdef ASSERT_BAD_PDF_OPS
1414 SkASSERT(false); // must be processed in inline image looper, but let's
1415 // have the assert when testing good pdfs.
1416#endif
1417 return kIgnoreError_PdfResult;
1418}
1419
1420PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1421#ifdef ASSERT_BAD_PDF_OPS
1422 SkASSERT(false); // must be processed in inline image looper, but let's
1423 // have the assert when testing good pdfs.
1424#endif
1425 return kIgnoreError_PdfResult;
1426}
1427
1428//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
1429PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001430 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001431 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1432
1433 return kOK_PdfResult;
1434}
1435
1436//lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153).
1437PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1438 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001439 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001440
1441 return kNYI_PdfResult;
1442}
1443
1444//lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153).
1445PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1446 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001447 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001448
1449 return kNYI_PdfResult;
1450}
1451
1452//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
1453PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1454 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001455 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001456
1457 return kNYI_PdfResult;
1458}
1459
1460//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1461//page 155).
1462PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1463 pdfContext->fObjectStack.pop();
1464 pdfContext->fObjectStack.pop();
1465
1466 return kNYI_PdfResult;
1467}
1468
1469//intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197).
1470PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1471 pdfContext->fObjectStack.pop();
1472
1473 return kNYI_PdfResult;
1474}
1475
1476//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1477//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1478//fies the output device’s default flatness tolerance.
1479PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1480 pdfContext->fObjectStack.pop();
1481
1482 return kNYI_PdfResult;
1483}
1484
1485//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1486//the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section).
1487PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001488 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001489
1490#ifdef PDF_TRACE
1491 std::string str;
1492#endif
1493
1494 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001495 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001496
1497 if (extGStateDictionary == NULL) {
1498#ifdef PDF_TRACE
1499 printf("ExtGState is NULL!\n");
1500#endif
1501 return kIgnoreError_PdfResult;
1502 }
1503
edisonn@google.com571c70b2013-07-10 17:09:50 +00001504 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001505
edisonn@google.com571c70b2013-07-10 17:09:50 +00001506 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1507 return kIgnoreError_PdfResult;
1508 }
1509 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001510
1511 // TODO(edisonn): now load all those properties in graphic state.
1512 if (gs == NULL) {
1513 return kIgnoreError_PdfResult;
1514 }
1515
1516 if (gs->has_CA()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001517 pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001518 }
1519
1520 if (gs->has_ca()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001521 pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001522 }
1523
1524 if (gs->has_LW()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001525 pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001526 }
1527
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001528 return kNYI_PdfResult;
1529}
1530
1531//charSpace Tc Set the character spacing, Tc
1532//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1533//Initial value: 0.
1534PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001535 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001536 pdfContext->fGraphicsState.fCharSpace = charSpace;
1537
1538 return kOK_PdfResult;
1539}
1540
1541//wordSpace Tw Set the word spacing, T
1542//w
1543//, to wordSpace, which is a number expressed in unscaled
1544//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1545//value: 0.
1546PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001547 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001548 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1549
1550 return kOK_PdfResult;
1551}
1552
1553//scale Tz Set the horizontal scaling, Th
1554//, to (scale ˜ 100). scale is a number specifying the
1555//percentage of the normal width. Initial value: 100 (normal width).
1556PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001557 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001558
1559 return kNYI_PdfResult;
1560}
1561
1562//render Tr Set the text rendering mode, T
1563//mode, to render, which is an integer. Initial value: 0.
1564PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001565 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001566
1567 return kNYI_PdfResult;
1568}
1569//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1570//units. Initial value: 0.
1571PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001572 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001573
1574 return kNYI_PdfResult;
1575}
1576
1577//wx wy d0
1578PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1579 pdfContext->fObjectStack.pop();
1580 pdfContext->fObjectStack.pop();
1581
1582 return kNYI_PdfResult;
1583}
1584
1585//wx wy llx lly urx ury d1
1586PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1587 pdfContext->fObjectStack.pop();
1588 pdfContext->fObjectStack.pop();
1589 pdfContext->fObjectStack.pop();
1590 pdfContext->fObjectStack.pop();
1591 pdfContext->fObjectStack.pop();
1592 pdfContext->fObjectStack.pop();
1593
1594 return kNYI_PdfResult;
1595}
1596
1597//name sh
1598PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1599 pdfContext->fObjectStack.pop();
1600
1601 return kNYI_PdfResult;
1602}
1603
1604//name Do
1605PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001606 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001607
edisonn@google.com571c70b2013-07-10 17:09:50 +00001608 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001609
1610 if (xObject == NULL) {
1611#ifdef PDF_TRACE
1612 printf("XObject is NULL!\n");
1613#endif
1614 return kIgnoreError_PdfResult;
1615 }
1616
edisonn@google.com571c70b2013-07-10 17:09:50 +00001617 SkPdfObject* value = xObject->get(name);
1618 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001619
1620#ifdef PDF_TRACE
1621// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001622// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001623#endif
1624
edisonn@google.com571c70b2013-07-10 17:09:50 +00001625 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001626}
1627
1628//tag MP Designate a marked-content point. tag is a name object indicating the role or
1629//significance of the point.
1630PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1631 pdfContext->fObjectStack.pop();
1632
1633 return kNYI_PdfResult;
1634}
1635
1636//tag properties DP Designate a marked-content point with an associated property list. tag is a
1637//name object indicating the role or significance of the point; properties is
1638//either an inline dictionary containing the property list or a name object
1639//associated with it in the Properties subdictionary of the current resource
1640//dictionary (see Section 9.5.1, “Property Lists”).
1641PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1642 pdfContext->fObjectStack.pop();
1643 pdfContext->fObjectStack.pop();
1644
1645 return kNYI_PdfResult;
1646}
1647
1648//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1649//tag is a name object indicating the role or significance of the sequence.
1650PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1651 pdfContext->fObjectStack.pop();
1652
1653 return kNYI_PdfResult;
1654}
1655
1656//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1657//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1658//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”).
1659PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1660 pdfContext->fObjectStack.pop();
1661 pdfContext->fObjectStack.pop();
1662
1663 return kNYI_PdfResult;
1664}
1665
1666//— EMC End a marked-content sequence begun by a BMC or BDC operator.
1667PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1668 return kNYI_PdfResult;
1669}
1670
1671void initPdfOperatorRenderes() {
1672 static bool gInitialized = false;
1673 if (gInitialized) {
1674 return;
1675 }
1676
1677 gPdfOps["q"] = PdfOp_q;
1678 gPdfOps["Q"] = PdfOp_Q;
1679 gPdfOps["cm"] = PdfOp_cm;
1680
1681 gPdfOps["TD"] = PdfOp_TD;
1682 gPdfOps["Td"] = PdfOp_Td;
1683 gPdfOps["Tm"] = PdfOp_Tm;
1684 gPdfOps["T*"] = PdfOp_T_star;
1685
1686 gPdfOps["m"] = PdfOp_m;
1687 gPdfOps["l"] = PdfOp_l;
1688 gPdfOps["c"] = PdfOp_c;
1689 gPdfOps["v"] = PdfOp_v;
1690 gPdfOps["y"] = PdfOp_y;
1691 gPdfOps["h"] = PdfOp_h;
1692 gPdfOps["re"] = PdfOp_re;
1693
1694 gPdfOps["S"] = PdfOp_S;
1695 gPdfOps["s"] = PdfOp_s;
1696 gPdfOps["f"] = PdfOp_f;
1697 gPdfOps["F"] = PdfOp_F;
1698 gPdfOps["f*"] = PdfOp_f_star;
1699 gPdfOps["B"] = PdfOp_B;
1700 gPdfOps["B*"] = PdfOp_B_star;
1701 gPdfOps["b"] = PdfOp_b;
1702 gPdfOps["b*"] = PdfOp_b_star;
1703 gPdfOps["n"] = PdfOp_n;
1704
1705 gPdfOps["BT"] = PdfOp_BT;
1706 gPdfOps["ET"] = PdfOp_ET;
1707
1708 gPdfOps["Tj"] = PdfOp_Tj;
1709 gPdfOps["'"] = PdfOp_quote;
1710 gPdfOps["\""] = PdfOp_doublequote;
1711 gPdfOps["TJ"] = PdfOp_TJ;
1712
1713 gPdfOps["CS"] = PdfOp_CS;
1714 gPdfOps["cs"] = PdfOp_cs;
1715 gPdfOps["SC"] = PdfOp_SC;
1716 gPdfOps["SCN"] = PdfOp_SCN;
1717 gPdfOps["sc"] = PdfOp_sc;
1718 gPdfOps["scn"] = PdfOp_scn;
1719 gPdfOps["G"] = PdfOp_G;
1720 gPdfOps["g"] = PdfOp_g;
1721 gPdfOps["RG"] = PdfOp_RG;
1722 gPdfOps["rg"] = PdfOp_rg;
1723 gPdfOps["K"] = PdfOp_K;
1724 gPdfOps["k"] = PdfOp_k;
1725
1726 gPdfOps["W"] = PdfOp_W;
1727 gPdfOps["W*"] = PdfOp_W_star;
1728
1729 gPdfOps["BX"] = PdfOp_BX;
1730 gPdfOps["EX"] = PdfOp_EX;
1731
1732 gPdfOps["BI"] = PdfOp_BI;
1733 gPdfOps["ID"] = PdfOp_ID;
1734 gPdfOps["EI"] = PdfOp_EI;
1735
1736 gPdfOps["w"] = PdfOp_w;
1737 gPdfOps["J"] = PdfOp_J;
1738 gPdfOps["j"] = PdfOp_j;
1739 gPdfOps["M"] = PdfOp_M;
1740 gPdfOps["d"] = PdfOp_d;
1741 gPdfOps["ri"] = PdfOp_ri;
1742 gPdfOps["i"] = PdfOp_i;
1743 gPdfOps["gs"] = PdfOp_gs;
1744
1745 gPdfOps["Tc"] = PdfOp_Tc;
1746 gPdfOps["Tw"] = PdfOp_Tw;
1747 gPdfOps["Tz"] = PdfOp_Tz;
1748 gPdfOps["TL"] = PdfOp_TL;
1749 gPdfOps["Tf"] = PdfOp_Tf;
1750 gPdfOps["Tr"] = PdfOp_Tr;
1751 gPdfOps["Ts"] = PdfOp_Ts;
1752
1753 gPdfOps["d0"] = PdfOp_d0;
1754 gPdfOps["d1"] = PdfOp_d1;
1755
1756 gPdfOps["sh"] = PdfOp_sh;
1757
1758 gPdfOps["Do"] = PdfOp_Do;
1759
1760 gPdfOps["MP"] = PdfOp_MP;
1761 gPdfOps["DP"] = PdfOp_DP;
1762 gPdfOps["BMC"] = PdfOp_BMC;
1763 gPdfOps["BDC"] = PdfOp_BDC;
1764 gPdfOps["EMC"] = PdfOp_EMC;
1765
1766 gInitialized = true;
1767}
1768
1769class InitPdfOps {
1770public:
1771 InitPdfOps() {
1772 initPdfOperatorRenderes();
1773 }
1774};
1775
1776InitPdfOps gInitPdfOps;
1777
1778void reportPdfRenderStats() {
1779 std::map<std::string, int>::iterator iter;
1780
1781 for (int i = 0 ; i < kCount_PdfResult; i++) {
1782 for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) {
1783 printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second);
1784 }
1785 }
1786}
1787
1788PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001789 char keyword[256];
1790
1791 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001792 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001793 strncpy(keyword, token.fKeyword, token.fKeywordLength);
1794 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001795 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com571c70b2013-07-10 17:09:50 +00001796 PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001797 if (pdfOperatorRenderer) {
1798 // caller, main work is done by pdfOperatorRenderer(...)
1799 PdfTokenLooper* childLooper = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001800 gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001801
1802 if (childLooper) {
1803 childLooper->setUp(this);
1804 childLooper->loop();
1805 delete childLooper;
1806 }
1807 } else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001808 gRenderStats[kUnsupported_PdfResult][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001809 }
1810 }
1811 else if (token.fType == kObject_TokenType)
1812 {
1813 fPdfContext->fObjectStack.push( token.fObject );
1814 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001815 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001816 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001817 return kIgnoreError_PdfResult;
1818 }
1819 return kOK_PdfResult;
1820}
1821
1822void PdfMainLooper::loop() {
1823 PdfToken token;
1824 while (readToken(fTokenizer, &token)) {
1825 consumeToken(token);
1826 }
1827}
1828
1829PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
1830 //pdfContext.fInlineImage.fKeyValuePairs[key] = value;
1831 return kNYI_PdfResult;
1832}
1833
1834void PdfInlineImageLooper::loop() {
1835 PdfToken token;
1836 while (readToken(fTokenizer, &token)) {
1837 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1838 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1839 looper->setUp(this);
1840 looper->loop();
1841 } else {
1842 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EI") == 0) {
1843 done();
1844 return;
1845 }
1846
1847 consumeToken(token);
1848 }
1849 }
1850 // TODO(edisonn): report error/warning, EOF without EI.
1851}
1852
1853PdfResult PdfInlineImageLooper::done() {
1854
1855 // TODO(edisonn): long to short names
1856 // TODO(edisonn): set properties in a map
1857 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
1858 // the stream.
1859
1860 SkBitmap bitmap;
1861 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
1862
1863 // TODO(edisonn): matrix use.
1864 // Draw dummy red square, to show the prezence of the inline image.
1865 fCanvas->drawBitmap(bitmap,
1866 SkDoubleToScalar(0),
1867 SkDoubleToScalar(0),
1868 NULL);
1869 return kNYI_PdfResult;
1870}
1871
1872PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
1873 return fParent->consumeToken(token);
1874}
1875
1876void PdfCompatibilitySectionLooper::loop() {
1877 // TODO(edisonn): save stacks position, or create a new stack?
1878 // TODO(edisonn): what happens if we pop out more variables then when we started?
1879 // restore them? fail? We could create a new operands stack for every new BX/EX section,
1880 // pop-ing too much will not affect outside the section.
1881 PdfToken token;
1882 while (readToken(fTokenizer, &token)) {
1883 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1884 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1885 looper->setUp(this);
1886 looper->loop();
1887 delete looper;
1888 } else {
1889 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
1890 fParent->consumeToken(token);
1891 }
1892 }
1893 // TODO(edisonn): restore stack.
1894}
1895
1896// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
1897// TODO(edisonn): Add API for Forms viewing and editing
1898// e.g. SkBitmap getPage(int page);
1899// int formsCount();
1900// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
1901// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
1902// if we load the first page, and we zoom to fit to screen horizontally, then load only those
1903// resources needed, so the preview is fast.
1904// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
1905// references automatically.
1906
1907bool SkPdfViewer::load(const SkString inputFileName, SkPicture* out) {
edisonn@google.com596d2e22013-07-10 17:44:55 +00001908 std::cout << "PDF Loaded: " << inputFileName.c_str() << std::endl;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001909
edisonn@google.com571c70b2013-07-10 17:09:50 +00001910 SkNativeParsedPDF* doc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001911 if (!doc->pages())
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001912 {
edisonn@google.com596d2e22013-07-10 17:44:55 +00001913 std::cout << "ERROR: Empty PDF Document" << inputFileName.c_str() << std::endl;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001914 return false;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001915 } else {
1916
1917 for (int pn = 0; pn < doc->pages(); ++pn) {
1918 // TODO(edisonn): implement inheritance properties as per PDF spec
1919 //SkRect rect = page->MediaBox();
1920 SkRect rect = doc->MediaBox(pn);
1921
1922#ifdef PDF_TRACE
1923 printf("Page Width: %f, Page Height: %f\n", SkScalarToDouble(rect.width()), SkScalarToDouble(rect.height()));
1924#endif
1925
1926 // TODO(edisonn): page->GetCropBox(), page->GetTrimBox() ... how to use?
1927
1928 SkBitmap bitmap;
1929#ifdef PDF_DEBUG_3X
1930 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkScalarToDouble(rect.height()));
1931#else
1932 setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDouble(rect.height()));
1933#endif
1934 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
1935 SkCanvas canvas(device);
1936
1937 gDumpBitmap = &bitmap;
1938
1939 gDumpCanvas = &canvas;
1940 doc->drawPage(pn, &canvas);
1941
1942 SkString out;
1943 if (doc->pages() > 1) {
1944 out.appendf("%s-%i.png", inputFileName.c_str(), pn);
1945 } else {
1946 out = inputFileName;
1947 // .pdf -> .png
1948 out[out.size() - 2] = 'n';
1949 out[out.size() - 1] = 'g';
1950 }
1951 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
1952 }
1953 return true;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001954 }
1955
1956 return true;
1957}