blob: bb2049e17f056bdd43847f581d327b5aff8d2ef7 [file] [log] [blame]
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCanvas.h"
9#include "SkDevice.h"
10#include "SkForceLinking.h"
11#include "SkGraphics.h"
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
14#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTypeface.h"
18#include "SkTArray.h"
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000019#include "SkTDict.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000020
edisonn@google.com15b11182013-07-11 14:43:15 +000021#include "SkPdfBasics.h"
22#include "SkPdfNativeTokenizer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000023#include <cstdio>
24#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000025#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000026
edisonn@google.com15b11182013-07-11 14:43:15 +000027extern "C" PdfContext* gPdfContext;
28extern "C" SkBitmap* gDumpBitmap;
29extern "C" SkCanvas* gDumpCanvas;
30
edisonn@google.com131d4ee2013-06-26 17:48:12 +000031__SK_FORCE_IMAGE_DECODER_LINKING;
32
33// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
34// keep for each object pos in file
35// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
36
37// TODO(edisonn): security - validate all the user input, all pdf!
38
edisonn@google.com6e49c342013-06-27 20:03:43 +000039// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
40// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000041
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
43/*
44#ifdef PDF_TRACE
45 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000046 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000047 printf("Print Tf Resources: %s\n", str.c_str());
48#endif
49 */
50
edisonn@google.com131d4ee2013-06-26 17:48:12 +000051#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000052#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000053#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000054
55#include "SkPdfBasics.h"
56#include "SkPdfUtils.h"
57
58#include "SkPdfFont.h"
59
edisonn@google.com131d4ee2013-06-26 17:48:12 +000060/*
61 * TODO(edisonn):
62 * - all font types and all ppdf font features
63 * - word spacing
64 * - load font for baidu.pdf
65 * - load font for youtube.pdf
66 * - parser for pdf from the definition already available in pdfspec_autogen.py
67 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000068 * - encapsulate native in the pdf api so the skpdf does not know anything about native ... in progress
edisonn@google.com131d4ee2013-06-26 17:48:12 +000069 * - load gs/ especially smask and already known prop (skp) ... in progress
70 * - wrapper on classes for customizations? e.g.
71 * SkPdfPageObjectVanila - has only the basic loaders/getters
72 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
73 * need to find a nice object model for all this with constructors and factories
74 * - deal with inheritable automatically ?
75 * - deal with specific type in spec directly, add all dictionary types to known types
76*/
77
78using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000079
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000080NotOwnedString strings_DeviceRGB;
81NotOwnedString strings_DeviceCMYK;
edisonn@google.com222382b2013-07-10 22:33:10 +000082
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000083class StringsInit {
84public:
85 StringsInit() {
86 NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB");
87 NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK");
88 }
89};
90
91StringsInit gStringsInit;
edisonn@google.com222382b2013-07-10 22:33:10 +000092
93// TODO(edisonn): Document PdfTokenLooper and subclasses.
94class PdfTokenLooper {
95protected:
96 PdfTokenLooper* fParent;
97 SkPdfNativeTokenizer* fTokenizer;
98 PdfContext* fPdfContext;
99 SkCanvas* fCanvas;
100
101public:
102 PdfTokenLooper(PdfTokenLooper* parent,
103 SkPdfNativeTokenizer* tokenizer,
104 PdfContext* pdfContext,
105 SkCanvas* canvas)
106 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
107
108 virtual ~PdfTokenLooper() {}
109
110 virtual PdfResult consumeToken(PdfToken& token) = 0;
111 virtual void loop() = 0;
112
113 void setUp(PdfTokenLooper* parent) {
114 fParent = parent;
115 fTokenizer = parent->fTokenizer;
116 fPdfContext = parent->fPdfContext;
117 fCanvas = parent->fCanvas;
118 }
edisonn@google.com78b38b12013-07-15 18:20:58 +0000119
120 SkPdfNativeTokenizer* tokenizer() { return fTokenizer; }
edisonn@google.com222382b2013-07-10 22:33:10 +0000121};
122
123class PdfMainLooper : public PdfTokenLooper {
124public:
125 PdfMainLooper(PdfTokenLooper* parent,
126 SkPdfNativeTokenizer* tokenizer,
127 PdfContext* pdfContext,
128 SkCanvas* canvas)
129 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
130
131 virtual PdfResult consumeToken(PdfToken& token);
132 virtual void loop();
133};
134
135class PdfInlineImageLooper : public PdfTokenLooper {
136public:
137 PdfInlineImageLooper()
138 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
139
140 virtual PdfResult consumeToken(PdfToken& token);
141 virtual void loop();
142 PdfResult done();
143};
144
145class PdfCompatibilitySectionLooper : public PdfTokenLooper {
146public:
147 PdfCompatibilitySectionLooper()
148 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
149
150 virtual PdfResult consumeToken(PdfToken& token);
151 virtual void loop();
152};
153
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000154// Utilities
155static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
156 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
157
158 bitmap->allocPixels();
159 bitmap->eraseColor(color);
160}
161
162// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000163static int GetColorSpaceComponents(NotOwnedString& colorSpace) {
164 if (colorSpace.equals("DeviceCMYK")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000165 return 4;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000166 } else if (colorSpace.equals("DeviceGray") ||
167 colorSpace.equals("CalGray") ||
168 colorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000169 return 1;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000170 } else if (colorSpace.equals("DeviceRGB") ||
171 colorSpace.equals("CalRGB") ||
172 colorSpace.equals("Lab")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000173 return 3;
174 } else {
175 return 0;
176 }
177}
178
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000179SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000180 SkMatrix matrix;
181 matrix.setAll(SkDoubleToScalar(array[0]),
182 SkDoubleToScalar(array[2]),
183 SkDoubleToScalar(array[4]),
184 SkDoubleToScalar(array[1]),
185 SkDoubleToScalar(array[3]),
186 SkDoubleToScalar(array[5]),
187 SkDoubleToScalar(0),
188 SkDoubleToScalar(0),
189 SkDoubleToScalar(1));
190
191 return matrix;
192}
193
194SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
195 double array[6];
196
197 // TODO(edisonn): security issue, ret if size() != 6
198 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000199 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000200 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000201 return SkMatrix::I(); // TODO(edisonn): report issue
202 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000203 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000204 }
205
206 return SkMatrixFromPdfMatrix(array);
207}
208
edisonn@google.com222382b2013-07-10 22:33:10 +0000209
210extern "C" SkNativeParsedPDF* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000211SkBitmap* gDumpBitmap = NULL;
212SkCanvas* gDumpCanvas = NULL;
213char gLastKeyword[100] = "";
214int gLastOpKeyword = -1;
215char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
216int gReadOp = 0;
217
218
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000219#ifdef PDF_TRACE_DIFF_IN_PNG
220static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000221 return true;
222 if (*pdfOp == '\0') return false;
223
224 char markedPdfOp[100] = ",";
225 strcat(markedPdfOp, pdfOp);
226 strcat(markedPdfOp, ",");
227
228 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
229}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000230#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000231
edisonn@google.com222382b2013-07-10 22:33:10 +0000232
233
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000234// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000235static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000236 bool ret = fTokenizer->readToken(token);
237
238 gReadOp++;
239
240#ifdef PDF_TRACE_DIFF_IN_PNG
241 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
242 // the numbar and name of last operation, so the file name will reflect op that changed.
243 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
244 gDumpCanvas->flush();
245
246 SkBitmap bitmap;
247 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
248
249 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
250
251 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
252 SkCanvas canvas(device);
253
254 // draw context stuff here
255 SkPaint blueBorder;
256 blueBorder.setColor(SK_ColorBLUE);
257 blueBorder.setStyle(SkPaint::kStroke_Style);
258 blueBorder.setTextSize(SkDoubleToScalar(20));
259
260 SkString str;
261
262 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
263 if (clipStack) {
264 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
265 const SkClipStack::Element* elem;
266 double y = 0;
267 int total = 0;
edisonn@google.com91ce6982013-08-05 20:45:40 +0000268 while ((elem = iter.next()) != NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000269 total++;
270 y += 30;
271
272 switch (elem->getType()) {
273 case SkClipStack::Element::kRect_Type:
274 canvas.drawRect(elem->getRect(), blueBorder);
275 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
276 break;
277 case SkClipStack::Element::kPath_Type:
278 canvas.drawPath(elem->getPath(), blueBorder);
279 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
280 break;
281 case SkClipStack::Element::kEmpty_Type:
282 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
283 break;
284 default:
285 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
286 break;
287 }
288 }
289
290 y += 30;
291 str.printf("Number of clips in stack: %i", total);
292 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
293 }
294
295 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
296 SkPath clipPath;
297 if (clipRegion.getBoundaryPath(&clipPath)) {
298 SkPaint redBorder;
299 redBorder.setColor(SK_ColorRED);
300 redBorder.setStyle(SkPaint::kStroke_Style);
301 canvas.drawPath(clipPath, redBorder);
302 }
303
304 canvas.flush();
305
306 SkString out;
307
308 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
309 // ... and other properties, to be able to debug th code easily
310
311 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
312 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
313 }
314
315 if (token->fType == kKeyword_TokenType) {
316 strcpy(gLastKeyword, token->fKeyword);
317 gLastOpKeyword = gReadOp;
318 } else {
319 strcpy(gLastKeyword, "");
320 }
321#endif
322
323 return ret;
324}
325
326
327
328typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
329
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000330SkTDict<PdfOperatorRenderer> gPdfOps(100);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000331
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000332
333template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
334public:
335 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
336};
337
338SkTDictWithDefaultConstructor<int> gRenderStats[kCount_PdfResult];
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000339
edisonn@google.com571c70b2013-07-10 17:09:50 +0000340const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000341 "Success",
342 "Partially implemented",
343 "Not yet implemented",
344 "Ignore Error",
345 "Error",
346 "Unsupported/Unknown"
347};
348
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000349static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000350 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000351 SkCanvas* canvas)
352{
353
354 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
355 if (skfont == NULL) {
356 skfont = SkPdfFont::Default();
357 }
358
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000359
edisonn@google.com571c70b2013-07-10 17:09:50 +0000360 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000361 // TODO(edisonn): report warning
362 return kIgnoreError_PdfResult;
363 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000364 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000365
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000366 SkUnencodedText binary(str);
367
368 SkDecodedText decoded;
369
370 if (skfont->encoding() == NULL) {
371 // TODO(edisonn): report warning
372 return kNYI_PdfResult;
373 }
374
375 skfont->encoding()->decodeText(binary, &decoded);
376
377 SkPaint paint;
378 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
379 // Or maybe just not call setTextSize at all?
380 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
381 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
382 }
383
384// if (fCurFont && fCurFont->GetFontScale() != 0) {
385// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
386// }
387
388 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
389
390 canvas->save();
391
392#if 1
393 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
394
395 SkPoint point1;
396 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
397
398 SkMatrix mirror;
399 mirror.setTranslate(0, -point1.y());
400 // TODO(edisonn): fix rotated text, and skewed too
401 mirror.postScale(SK_Scalar1, -SK_Scalar1);
402 // TODO(edisonn): post rotate, skew
403 mirror.postTranslate(0, point1.y());
404
405 matrix.postConcat(mirror);
406
407 canvas->setMatrix(matrix);
408
409 SkTraceMatrix(matrix, "mirrored");
410#endif
411
edisonn@google.com6e49c342013-06-27 20:03:43 +0000412 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000413 canvas->restore();
414
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000415 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000416}
417
418// TODO(edisonn): create header files with declarations!
419PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
420PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
421PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
422PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
423
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000424// TODO(edisonn): perf!!!
425
426static SkColorTable* getGrayColortable() {
427 static SkColorTable* grayColortable = NULL;
428 if (grayColortable == NULL) {
429 SkPMColor* colors = new SkPMColor[256];
430 for (int i = 0 ; i < 256; i++) {
431 colors[i] = SkPreMultiplyARGB(255, i, i, i);
432 }
433 grayColortable = new SkColorTable(colors, 256);
434 }
435 return grayColortable;
436}
437
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000438static SkBitmap* transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000439 int width, int height, int bytesPerLine,
440 int bpc, const std::string& colorSpace,
441 bool transparencyMask) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000442 SkBitmap* bitmap = new SkBitmap();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000443
edisonn@google.com571c70b2013-07-10 17:09:50 +0000444 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000445//#define MAX_COMPONENTS 10
446
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000447 // TODO(edisonn): assume start of lines are aligned at 32 bits?
448 // Is there a faster way to load the uncompressed stream into a bitmap?
449
450 // minimal support for now
451 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
452 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
453
454 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000455 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000456 for (int w = 0 ; w < width; w++) {
457 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
458 uncompressedStream[3 * w + 1],
459 uncompressedStream[3 * w + 2]);
460 i++;
461 }
462 uncompressedStream += bytesPerLine;
463 }
464
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000465 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
466 bitmap->setPixels(uncompressedStreamArgb);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000467 }
468 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
469 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
470
471 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000472 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000473 for (int w = 0 ; w < width; w++) {
474 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
475 uncompressedStream[w];
476 i++;
477 }
478 uncompressedStream += bytesPerLine;
479 }
480
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000481 bitmap->setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000482 width, height);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000483 bitmap->setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000484 }
485
486 // TODO(edisonn): Report Warning, NYI, or error
487 return bitmap;
488}
489
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000490// utils
491
492// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
493// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
494// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
495// skia format, through a table
496
497// this functions returns the image, it does not look at the smask.
498
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000499static SkBitmap* getImageFromObjectCore(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000500 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000501 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000502 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000503 }
504
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000505 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
506 int64_t width = image->Width(pdfContext->fPdfDoc);
507 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000508 std::string colorSpace = "DeviceRGB";
509
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000510 bool indexed = false;
511 SkPMColor colors[256];
512 int cnt = 0;
513
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000514 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000515 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
516 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000517 } else if (image->isColorSpaceAArray(pdfContext->fPdfDoc)) {
518 SkPdfArray* array = image->getColorSpaceAsArray(pdfContext->fPdfDoc);
519 if (array && array->size() == 4 && array->objAtAIndex(0)->isName("Indexed") &&
520 (array->objAtAIndex(1)->isName("DeviceRGB") || array->objAtAIndex(1)->isName("RGB")) &&
521 array->objAtAIndex(2)->isInteger() &&
522 array->objAtAIndex(3)->isHexString()
523 ) {
524 // TODO(edisonn): suport only DeviceRGB for now.
525 indexed = true;
526 cnt = array->objAtAIndex(2)->intValue() + 1;
527 if (cnt > 256) {
528 // TODO(edionn): report NYIs
529 return NULL;
530 }
531 SkColorTable colorTable(cnt);
532 NotOwnedString data = array->objAtAIndex(3)->strRef();
533 if (data.fBytes != (unsigned int)cnt * 3) {
534 // TODO(edionn): report error/warning
535 return NULL;
536 }
537 for (int i = 0 ; i < cnt; i++) {
538 colors[i] = SkPreMultiplyARGB(0xff, data.fBuffer[3 * i], data.fBuffer[3 * i + 1], data.fBuffer[3 * i + 2]);
539 }
540 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000541 }
542
543/*
544 bool imageMask = image->imageMask();
545
546 if (imageMask) {
547 if (bpc != 0 && bpc != 1) {
548 // TODO(edisonn): report warning to be used in testing.
549 return SkBitmap();
550 }
551 bpc = 1;
552 }
553*/
554
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000555 const unsigned char* uncompressedStream = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000556 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000557
edisonn@google.com571c70b2013-07-10 17:09:50 +0000558 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000559
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000560 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000561 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000562 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000563 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000564 }
565
edisonn@google.com571c70b2013-07-10 17:09:50 +0000566 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
567
568 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
569 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
570 (streamDict->isFilterAArray(NULL) &&
571 streamDict->getFilterAsArray(NULL)->size() > 0 &&
572 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
573 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000574 SkBitmap* bitmap = new SkBitmap();
575 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, bitmap);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000576 return bitmap;
577 }
578
579
580
581 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
582// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
583// obj.GetDictionary().GetKey(PdfName("Filter")));
584// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
585// value = resolveReferenceObject(pdfContext->fPdfDoc,
586// &value->GetArray()[0]);
587// }
588// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
589// SkStream stream = SkStream::
590// SkImageDecoder::Factory()
591// }
592
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000593 // TODO(edisonn): assumes RGB for now, since it is the only onwe implemented
594 if (indexed) {
595 SkBitmap* bitmap = new SkBitmap();
596 bitmap->setConfig(SkBitmap::kIndex8_Config, width, height);
597 SkColorTable* colorTable = new SkColorTable(colors, cnt);
598 bitmap->setPixels((void*)uncompressedStream, colorTable);
599 return bitmap;
600 }
601
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000602 int bytesPerLine = (int)(uncompressedStreamLength / height);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000603#ifdef PDF_TRACE
604 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000605 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000606 }
607#endif
608
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000609 SkBitmap* bitmap = transferImageStreamToBitmap(
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000610 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000611 (int)width, (int)height, bytesPerLine,
612 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000613 transparencyMask);
614
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000615 return bitmap;
616}
617
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000618static SkBitmap* getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
619 if (!transparencyMask) {
620 if (!image->hasData(SkPdfObject::kBitmap_Data)) {
621 SkBitmap* bitmap = getImageFromObjectCore(pdfContext, image, transparencyMask);
622 image->setData(bitmap, SkPdfObject::kBitmap_Data);
623 }
624 return (SkBitmap*) image->data(SkPdfObject::kBitmap_Data);
625 } else {
626 return getImageFromObjectCore(pdfContext, image, transparencyMask);
627 }
628}
629
630static SkBitmap* getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000631 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000632
633 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000634 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000635 }
636
637 // TODO(edisonn): implement GS SMask. Default to empty right now.
638 return pdfContext->fGraphicsState.fSMask;
639}
640
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000641static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000642 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000643 return kIgnoreError_PdfResult;
644 }
645
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000646 SkBitmap* image = getImageFromObject(pdfContext, skpdfimage, false);
647 SkBitmap* sMask = getSmaskFromObject(pdfContext, skpdfimage);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000648
649 canvas->save();
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000650 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000651
652#if 1
653 SkScalar z = SkIntToScalar(0);
654 SkScalar one = SkIntToScalar(1);
655
656 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
657 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
658 SkMatrix flip;
659 SkAssertResult(flip.setPolyToPoly(from, to, 4));
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000660 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000661 solveImageFlip.preConcat(flip);
662 canvas->setMatrix(solveImageFlip);
663#endif
664
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000665 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
666
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000667 // TODO(edisonn): soft mask type? alpha/luminosity.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000668 if (!sMask || sMask->empty()) {
669 canvas->drawBitmapRect(*image, dst, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000670 } else {
671 canvas->saveLayer(&dst, NULL);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000672 canvas->drawBitmapRect(*image, dst, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000673 SkPaint xfer;
674 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000675 // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf?
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000676 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000677 canvas->drawBitmapRect(*sMask, dst, &xfer);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000678 canvas->restore();
679 }
680
681 canvas->restore();
682
683 return kPartial_PdfResult;
684}
685
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000686//TODO(edisonn): options for implementing isolation and knockout
687// 1) emulate them (current solution)
688// PRO: simple
689// CON: will need to use readPixels, which means serious perf issues
690// 2) Compile a plan for an array of matrixes, compose the result at the end
691// PRO: might be faster then 1, no need to readPixels
692// CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan
693// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
694// 3) support them natively in SkCanvas
695// PRO: simple
696// CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1)
697// 4) compile a plan using pathops, and render once without any fancy rules with backdrop
698// PRO: simple, fast
699// CON: pathops must be bug free first + time to compute new paths
700// pay a price at loading pdf to compute a pdf draw plan
701// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
edisonn@google.com251176e2013-08-01 13:24:00 +0000702// 5) for knockout, render the objects in reverse order, and add every object to the clip, and any new draw will be cliped
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000703
704
705// TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree!
706// TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed
707
708
709
710static void doGroup_before(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) {
711 SkRect bboxOrig = bbox;
712 SkBitmap backdrop;
713 bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc);
714// bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000715 SkPaint paint;
716 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
717 canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000718}
719
edisonn@google.com251176e2013-08-01 13:24:00 +0000720// TODO(edisonn): non isolation implemented in skia
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000721//static void doGroup_after(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup) {
edisonn@google.com251176e2013-08-01 13:24:00 +0000722// if not isolated
723// canvas->drawBitmapRect(backdrop, bboxOrig, NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000724//}
725
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000726static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000727 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000728 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000729 }
730
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000731 if (!skobj->has_BBox()) {
732 return kIgnoreError_PdfResult;
733 }
734
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000735 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000736
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000737 canvas->save();
738
739
edisonn@google.com571c70b2013-07-10 17:09:50 +0000740 if (skobj->Resources(pdfContext->fPdfDoc)) {
741 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000742 }
743
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000744 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000745
edisonn@google.com571c70b2013-07-10 17:09:50 +0000746 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000747 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
748 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
749 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000750 // TODO(edisonn) reset matrixTm and matricTlm also?
751 }
752
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000753 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000754
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000755 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000756
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000757 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
758 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000759
760 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
761 // For this PdfContentsTokenizer needs to be extended.
762
edisonn@google.come878e722013-07-29 19:10:58 +0000763 // This is a group?
764 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000765 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
766 doGroup_before(pdfContext, canvas, bbox, tgroup, false);
edisonn@google.come878e722013-07-29 19:10:58 +0000767 }
768
edisonn@google.com571c70b2013-07-10 17:09:50 +0000769 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000770
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000771 SkPdfNativeTokenizer* tokenizer =
772 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000773 if (tokenizer != NULL) {
774 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
775 looper.loop();
776 delete tokenizer;
777 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000778
779 // TODO(edisonn): should we restore the variable stack at the same state?
780 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000781
782 if (skobj->has_Group()) {
783 canvas->restore();
784 }
785
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000786 canvas->restore();
787 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000788 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000789}
790
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000791
792// TODO(edisonn): Extract a class like ObjWithStream
793static PdfResult doXObject_Pattern(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1PatternDictionary* skobj) {
794 if (!skobj || !skobj->hasStream()) {
795 return kIgnoreError_PdfResult;
796 }
797
798 if (!skobj->has_BBox()) {
799 return kIgnoreError_PdfResult;
800 }
801
802 PdfOp_q(pdfContext, canvas, NULL);
803
804 canvas->save();
805
806
807 if (skobj->Resources(pdfContext->fPdfDoc)) {
808 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
809 }
810
811 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
812
813 if (skobj->has_Matrix()) {
814 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
815 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
816 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
817 // TODO(edisonn) reset matrixTm and matricTlm also?
818 }
819
820 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
821
822 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
823
824 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
825 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
826
827 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
828 // For this PdfContentsTokenizer needs to be extended.
829
830 SkPdfStream* stream = (SkPdfStream*)skobj;
831
832 SkPdfNativeTokenizer* tokenizer =
833 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
834 if (tokenizer != NULL) {
835 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
836 looper.loop();
837 delete tokenizer;
838 }
839
840 // TODO(edisonn): should we restore the variable stack at the same state?
841 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
842
843 canvas->restore();
844 PdfOp_Q(pdfContext, canvas, NULL);
845 return kPartial_PdfResult;
846}
847
848
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000849//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
850// return kNYI_PdfResult;
851//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000852
edisonn@google.com571c70b2013-07-10 17:09:50 +0000853PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
854 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000855 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000856 }
857
858 PdfOp_q(pdfContext, canvas, NULL);
859 canvas->save();
860
861 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
862 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
863
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000864 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
865 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000866
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000867 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000868
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000869 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000870
871 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000872 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000873
874 SkTraceRect(rm, "bbox mapped");
875
876 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
877
878 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
879 // For this PdfContentsTokenizer needs to be extended.
880
edisonn@google.com571c70b2013-07-10 17:09:50 +0000881 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000882
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000883 SkPdfNativeTokenizer* tokenizer =
884 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000885 if (tokenizer != NULL) {
886 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
887 looper.loop();
888 delete tokenizer;
889 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000890
891 // TODO(edisonn): should we restore the variable stack at the same state?
892 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
893 canvas->restore();
894 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000895
896 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000897}
898
899
edisonn@google.com571c70b2013-07-10 17:09:50 +0000900// TODO(edisonn): make sure the pointer is unique
901std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000902
903class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000904 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000905public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000906 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000907 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000908 }
909
910 ~CheckRecursiveRendering() {
911 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000912 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000913 }
914
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000915 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000916 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000917 }
918};
919
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000920static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000921 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000922 // Oops, corrupt PDF!
923 return kIgnoreError_PdfResult;
924 }
925
edisonn@google.com571c70b2013-07-10 17:09:50 +0000926 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000927
edisonn@google.com571c70b2013-07-10 17:09:50 +0000928 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000929 {
930 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000931 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000932 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000933 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000934 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
935 //return doXObject_PS(skxobj.asPS());
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000936 default: {
937 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(obj) != kNone_SkPdfObjectType) {
938 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)obj;
939 return doXObject_Pattern(pdfContext, canvas, pattern);
940 }
941 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000942 }
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000943 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000944}
945
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000946static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
947 if (!skobj) {
948 return kIgnoreError_PdfResult;
949 }
950
951 if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
952 return kNYI_PdfResult;
953 }
954
955 SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
956
957 if (!stream) {
958 return kIgnoreError_PdfResult;
959 }
960
961 if (CheckRecursiveRendering::IsInRendering(skobj)) {
962 // Oops, corrupt PDF!
963 return kIgnoreError_PdfResult;
964 }
965 CheckRecursiveRendering checkRecursion(skobj);
966
967
968 PdfOp_q(pdfContext, canvas, NULL);
969
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000970
971 if (skobj->Resources(pdfContext->fPdfDoc)) {
972 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
973 }
974
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000975 // TODO(edisonn): MediaBox can be inherited!!!!
976 SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc);
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000977 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000978 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
979 doGroup_before(pdfContext, canvas, bbox, tgroup, true);
980 } else {
981 canvas->save();
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000982 }
983
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000984
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000985 SkPdfNativeTokenizer* tokenizer =
986 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
987 if (tokenizer != NULL) {
988 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
989 looper.loop();
990 delete tokenizer;
991 }
992
993 // TODO(edisonn): should we restore the variable stack at the same state?
994 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
995 canvas->restore();
996 PdfOp_Q(pdfContext, canvas, NULL);
997 return kPartial_PdfResult;
998}
999
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001000PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1001 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
1002 canvas->save();
1003 return kOK_PdfResult;
1004}
1005
1006PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1007 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
1008 pdfContext->fStateStack.pop();
1009 canvas->restore();
1010 return kOK_PdfResult;
1011}
1012
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001013static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001014 double array[6];
1015 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001016 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001017 pdfContext->fObjectStack.pop();
1018 }
1019
1020 // a b
1021 // c d
1022 // e f
1023
1024 // 0 1
1025 // 2 3
1026 // 4 5
1027
1028 // sx ky
1029 // kx sy
1030 // tx ty
1031 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1032
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001033 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001034
1035#ifdef PDF_TRACE
1036 printf("cm ");
1037 for (int i = 0 ; i < 6 ; i++) {
1038 printf("%f ", array[i]);
1039 }
1040 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001041 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001042#endif
1043
1044 return kOK_PdfResult;
1045}
1046
1047//leading TL Set the text leading, Tl
1048//, to leading, which is a number expressed in unscaled text
1049//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001050static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001051 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001052
1053 pdfContext->fGraphicsState.fTextLeading = ty;
1054
1055 return kOK_PdfResult;
1056}
1057
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001058static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001059#ifdef PDF_TRACE
1060 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
1061#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +00001062 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001063 SkPdfObject* obj = pdfContext->fObjectStack.top();
1064 obj = obj;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001065 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001066
1067 double array[6] = {1, 0, 0, 1, tx, ty};
1068 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1069
1070 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1071 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
1072
1073 return kPartial_PdfResult;
1074}
1075
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001076static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001077 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1078 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001079
edisonn@google.com571c70b2013-07-10 17:09:50 +00001080 // TODO(edisonn): Create factory methods or constructors so native is hidden
1081 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001082 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001083
1084 PdfOp_TL(pdfContext, canvas, looper);
1085
edisonn@google.com571c70b2013-07-10 17:09:50 +00001086 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001087 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001088
edisonn@google.com571c70b2013-07-10 17:09:50 +00001089 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001090 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001091
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001092 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
1093
1094 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
1095 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001096
1097 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001098}
1099
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001100static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001101 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1102 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1103 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1104 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1105 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1106 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001107
1108 double array[6];
1109 array[0] = a;
1110 array[1] = b;
1111 array[2] = c;
1112 array[3] = d;
1113 array[4] = e;
1114 array[5] = f;
1115
1116 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001117 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001118
1119 // TODO(edisonn): Text positioning.
1120 pdfContext->fGraphicsState.fMatrixTm = matrix;
1121 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
1122
1123 return kPartial_PdfResult;
1124}
1125
1126//— T* Move to the start of the next line. This operator has the same effect as the code
1127//0 Tl Td
1128//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001129static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001130 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
1131 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001132
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001133 pdfContext->fObjectStack.push(zero);
1134 pdfContext->fObjectStack.push(tl);
1135
1136 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
1137
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001138 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001139}
1140
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001141static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001142 if (pdfContext->fGraphicsState.fPathClosed) {
1143 pdfContext->fGraphicsState.fPath.reset();
1144 pdfContext->fGraphicsState.fPathClosed = false;
1145 }
1146
edisonn@google.com571c70b2013-07-10 17:09:50 +00001147 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1148 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001149
1150 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1151 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1152
1153 return kOK_PdfResult;
1154}
1155
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001156static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001157 if (pdfContext->fGraphicsState.fPathClosed) {
1158 pdfContext->fGraphicsState.fPath.reset();
1159 pdfContext->fGraphicsState.fPathClosed = false;
1160 }
1161
edisonn@google.com571c70b2013-07-10 17:09:50 +00001162 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1163 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001164
1165 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1166 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1167
1168 return kOK_PdfResult;
1169}
1170
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001171static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001172 if (pdfContext->fGraphicsState.fPathClosed) {
1173 pdfContext->fGraphicsState.fPath.reset();
1174 pdfContext->fGraphicsState.fPathClosed = false;
1175 }
1176
edisonn@google.com571c70b2013-07-10 17:09:50 +00001177 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1178 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1179 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1180 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1181 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1182 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001183
1184 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1185 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1186 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1187
1188 pdfContext->fGraphicsState.fCurPosX = x3;
1189 pdfContext->fGraphicsState.fCurPosY = y3;
1190
1191 return kOK_PdfResult;
1192}
1193
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001194static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001195 if (pdfContext->fGraphicsState.fPathClosed) {
1196 pdfContext->fGraphicsState.fPath.reset();
1197 pdfContext->fGraphicsState.fPathClosed = false;
1198 }
1199
edisonn@google.com571c70b2013-07-10 17:09:50 +00001200 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1201 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1202 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1203 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001204 double y1 = pdfContext->fGraphicsState.fCurPosY;
1205 double x1 = pdfContext->fGraphicsState.fCurPosX;
1206
1207 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1208 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1209 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1210
1211 pdfContext->fGraphicsState.fCurPosX = x3;
1212 pdfContext->fGraphicsState.fCurPosY = y3;
1213
1214 return kOK_PdfResult;
1215}
1216
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001217static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001218 if (pdfContext->fGraphicsState.fPathClosed) {
1219 pdfContext->fGraphicsState.fPath.reset();
1220 pdfContext->fGraphicsState.fPathClosed = false;
1221 }
1222
edisonn@google.com571c70b2013-07-10 17:09:50 +00001223 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1224 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001225 double y2 = pdfContext->fGraphicsState.fCurPosY;
1226 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001227 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1228 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001229
1230 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1231 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1232 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1233
1234 pdfContext->fGraphicsState.fCurPosX = x3;
1235 pdfContext->fGraphicsState.fCurPosY = y3;
1236
1237 return kOK_PdfResult;
1238}
1239
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001240static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001241 if (pdfContext->fGraphicsState.fPathClosed) {
1242 pdfContext->fGraphicsState.fPath.reset();
1243 pdfContext->fGraphicsState.fPathClosed = false;
1244 }
1245
edisonn@google.com571c70b2013-07-10 17:09:50 +00001246 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1247 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1248 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1249 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001250
1251 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1252 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1253
1254 pdfContext->fGraphicsState.fCurPosX = x;
1255 pdfContext->fGraphicsState.fCurPosY = y + height;
1256
1257 return kOK_PdfResult;
1258}
1259
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001260static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001261 pdfContext->fGraphicsState.fPath.close();
1262 return kOK_PdfResult;
1263}
1264
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001265static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001266 SkPath path = pdfContext->fGraphicsState.fPath;
1267
1268 if (close) {
1269 path.close();
1270 }
1271
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001272 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001273
1274 SkPaint paint;
1275
1276 SkPoint line[2];
1277 if (fill && !stroke && path.isLine(line)) {
1278 paint.setStyle(SkPaint::kStroke_Style);
1279
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001280 // TODO(edisonn): implement this with patterns
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001281 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1282 paint.setStrokeWidth(SkDoubleToScalar(0));
1283
1284 canvas->drawPath(path, paint);
1285 } else {
1286 if (fill) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001287 if (strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0 &&
1288 pdfContext->fGraphicsState.fNonStroking.fPattern != NULL) {
1289
1290 // TODO(edisonn): we can use a shader here, like imageshader to draw fast. ultimately,
1291 // if this is not possible, and we are in rasper mode, and the cells don't intersect, we could even have multiple cpus.
1292
1293 canvas->save();
1294 PdfOp_q(pdfContext, canvas, NULL);
1295
1296 if (evenOdd) {
1297 path.setFillType(SkPath::kEvenOdd_FillType);
1298 }
1299 canvas->clipPath(path);
1300
1301 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(pdfContext->fGraphicsState.fNonStroking.fPattern) != kNone_SkPdfObjectType) {
1302 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)pdfContext->fGraphicsState.fNonStroking.fPattern;
1303
1304 // TODO(edisonn): constants
1305 // TODO(edisonn): colored
1306 if (pattern->PaintType(pdfContext->fPdfDoc) == 1) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001307 // TODO(edisonn): don't use abs, iterate as asked, if the cells intersect
1308 // it will change the result iterating in reverse
1309 int xStep = abs((int)pattern->XStep(pdfContext->fPdfDoc));
1310 int yStep = abs((int)pattern->YStep(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001311
1312 SkRect bounds = path.getBounds();
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001313
1314 // TODO(edisonn): xstep and ystep can be negative, and we need to iterate in reverse
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001315 // TODO(edisonn): don't do that!
1316 bounds.sort();
1317
1318 SkScalar x;
1319 SkScalar y;
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001320
1321 y = bounds.top();
1322 int totalx = 0;
1323 int totaly = 0;
1324 while (y < bounds.bottom()) {
1325 x = bounds.left();
1326 totalx = 0;
1327
1328 while (x < bounds.right()) {
1329 doXObject(pdfContext, canvas, pattern);
1330
1331 pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(xStep), SkIntToScalar(0));
1332 totalx += xStep;
1333 x += SkIntToScalar(xStep);
1334 }
1335 pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(-totalx), SkIntToScalar(0));
1336
1337 pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(0), SkIntToScalar(-yStep));
1338 totaly += yStep;
1339 y += SkIntToScalar(yStep);
1340 }
1341 pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(0), SkIntToScalar(totaly));
1342 }
1343 }
1344
1345 // apply matrix
1346 // get xstep, y step, bbox ... for cliping, and bos of the path
1347
1348 PdfOp_Q(pdfContext, canvas, NULL);
1349 canvas->restore();
1350 } else {
1351 paint.setStyle(SkPaint::kFill_Style);
1352 if (evenOdd) {
1353 path.setFillType(SkPath::kEvenOdd_FillType);
1354 }
1355
1356 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1357
1358 canvas->drawPath(path, paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001359 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001360 }
1361
1362 if (stroke) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001363 if (false && strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0) {
1364 // TODO(edisonn): implement Pattern for strokes
1365 paint.setStyle(SkPaint::kStroke_Style);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001366
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001367 paint.setColor(SK_ColorGREEN);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001368
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001369 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1370 canvas->drawPath(path, paint);
1371 } else {
1372 paint.setStyle(SkPaint::kStroke_Style);
1373
1374 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1375
1376 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1377 canvas->drawPath(path, paint);
1378 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001379 }
1380 }
1381
1382 pdfContext->fGraphicsState.fPath.reset();
1383 // todo zoom ... other stuff ?
1384
1385 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1386#ifndef PDF_DEBUG_NO_CLIPING
1387 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1388#endif
1389 }
1390
1391 //pdfContext->fGraphicsState.fClipPath.reset();
1392 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1393
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001394 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001395
1396}
1397
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001398static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001399 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1400}
1401
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001402static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001403 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1404}
1405
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001406static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001407 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1408}
1409
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001410static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001411 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1412}
1413
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001414static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001415 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1416}
1417
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001418static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001419 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1420}
1421
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001422static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001423 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1424}
1425
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001426static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001427 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1428}
1429
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001430static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001431 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1432}
1433
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001434static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001435 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001436 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1437#ifndef PDF_DEBUG_NO_CLIPING
1438 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1439#endif
1440 }
1441
1442 //pdfContext->fGraphicsState.fClipPath.reset();
1443 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1444
1445 pdfContext->fGraphicsState.fPathClosed = true;
1446
1447 return kOK_PdfResult;
1448}
1449
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001450static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001451 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001452 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
1453 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001454
1455 return kPartial_PdfResult;
1456}
1457
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001458static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001459 if (!pdfContext->fGraphicsState.fTextBlock) {
1460 return kIgnoreError_PdfResult;
1461 }
1462 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001463 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001464}
1465
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001466PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001467#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001468 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001469#endif
1470
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001471 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1472 // TODO(edisonn): try to recover and draw it any way?
1473 return kIgnoreError_PdfResult;
1474 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001475
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001476 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1477 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1478 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1479 // TODO(edisonn): try to recover and draw it any way?
1480 return kIgnoreError_PdfResult;
1481 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001482
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001483 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1484
1485 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1486
1487 if (skfont) {
1488 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001489 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001490 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001491 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001492}
1493
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001494//font size Tf Set the text font, Tf
1495//, to font and the text font size, Tfs, to size. font is the name of a
1496//font resource in the Fontsubdictionary of the current resource dictionary; size is
1497//a number representing a scale factor. There is no initial value for either font or
1498//size; they must be specified explicitly using Tf before any text is shown.
1499static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1500 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1501 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1502 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1503}
1504
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001505static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001506 if (!pdfContext->fGraphicsState.fTextBlock) {
1507 // TODO(edisonn): try to recover and draw it any way?
1508 return kIgnoreError_PdfResult;
1509 }
1510
1511 PdfResult ret = DrawText(pdfContext,
1512 pdfContext->fObjectStack.top(),
1513 canvas);
1514 pdfContext->fObjectStack.pop();
1515
1516 return ret;
1517}
1518
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001519static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001520 if (!pdfContext->fGraphicsState.fTextBlock) {
1521 // TODO(edisonn): try to recover and draw it any way?
1522 return kIgnoreError_PdfResult;
1523 }
1524
1525 PdfOp_T_star(pdfContext, canvas, looper);
1526 // Do not pop, and push, just transfer the param to Tj
1527 return PdfOp_Tj(pdfContext, canvas, looper);
1528}
1529
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001530static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001531 if (!pdfContext->fGraphicsState.fTextBlock) {
1532 // TODO(edisonn): try to recover and draw it any way?
1533 return kIgnoreError_PdfResult;
1534 }
1535
1536 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1537 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1538 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1539
1540 pdfContext->fObjectStack.push(aw);
1541 PdfOp_Tw(pdfContext, canvas, looper);
1542
1543 pdfContext->fObjectStack.push(ac);
1544 PdfOp_Tc(pdfContext, canvas, looper);
1545
1546 pdfContext->fObjectStack.push(str);
1547 PdfOp_quote(pdfContext, canvas, looper);
1548
1549 return kPartial_PdfResult;
1550}
1551
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001552static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001553 if (!pdfContext->fGraphicsState.fTextBlock) {
1554 // TODO(edisonn): try to recover and draw it any way?
1555 return kIgnoreError_PdfResult;
1556 }
1557
edisonn@google.com571c70b2013-07-10 17:09:50 +00001558 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001559 pdfContext->fObjectStack.pop();
1560
edisonn@google.com571c70b2013-07-10 17:09:50 +00001561 if (!array->isArray()) {
1562 return kIgnoreError_PdfResult;
1563 }
1564
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001565 for( int i=0; i<static_cast<int>(array->size()); i++ )
1566 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001567 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001568 SkPdfObject* obj = (*array)[i];
1569 DrawText(pdfContext,
1570 obj,
1571 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001572 } else if ((*array)[i]->isNumber()) {
1573 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001574 SkMatrix matrix;
1575 matrix.setAll(SkDoubleToScalar(1),
1576 SkDoubleToScalar(0),
1577 // TODO(edisonn): use writing mode, vertical/horizontal.
1578 SkDoubleToScalar(-dx), // amount is substracted!!!
1579 SkDoubleToScalar(0),
1580 SkDoubleToScalar(1),
1581 SkDoubleToScalar(0),
1582 SkDoubleToScalar(0),
1583 SkDoubleToScalar(0),
1584 SkDoubleToScalar(1));
1585
1586 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1587 }
1588 }
1589 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1590}
1591
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001592static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001593 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001594 return kOK_PdfResult;
1595}
1596
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001597static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001598 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1599}
1600
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001601static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001602 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1603}
1604
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001605static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001606 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001607// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001608
1609 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1610
1611 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001612 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001613 doubles = false;
1614 }
1615
1616#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001617 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001618#endif
1619
1620 for (int i = n - 1; i >= 0 ; i--) {
1621 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001622 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1623// } else {
1624// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001625 }
1626 }
1627
1628 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001629 // TODO(edisonn): do possible field values to enum at parsing time!
1630 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001631 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001632 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001633 }
1634 return kPartial_PdfResult;
1635}
1636
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001637static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001638 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1639}
1640
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001641static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001642 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1643}
1644
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001645static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001646 if (pdfContext->fObjectStack.top()->isName()) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001647 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1648
1649 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001650 SkPdfDictionary* patternResources = pdfContext->fGraphicsState.fResources->Pattern(pdfContext->fPdfDoc);
edisonn@google.com276fed92013-08-01 21:20:47 +00001651
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001652 if (patternResources == NULL) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001653#ifdef PDF_TRACE
1654 printf("ExtGState is NULL!\n");
1655#endif
1656 return kIgnoreError_PdfResult;
1657 }
1658
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001659 colorOperator->setPatternColorSpace(pdfContext->fPdfDoc->resolveReference(patternResources->get(name)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001660 }
1661
1662 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1663 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1664
1665 return kPartial_PdfResult;
1666}
1667
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001668static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001669 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1670}
1671
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001672static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001673 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1674}
1675
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001676static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001677 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001678 return kNYI_PdfResult;
1679}
1680
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001681static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001682 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1683}
1684
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001685static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001686 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1687}
1688
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001689static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001690 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1691 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1692 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001693
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001694 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001695 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001696 return kOK_PdfResult;
1697}
1698
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001699static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001700 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1701}
1702
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001703static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001704 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1705}
1706
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001707static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001708 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001709 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1710 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1711 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1712 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001713
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001714 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001715 // TODO(edisonn): Set color.
1716 return kNYI_PdfResult;
1717}
1718
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001719static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001720 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1721}
1722
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001723static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001724 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1725}
1726
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001727static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001728 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1729 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1730
1731 return kOK_PdfResult;
1732}
1733
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001734static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001735 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1736
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001737 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1738 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1739
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001740 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001741}
1742
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001743static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001744 *looper = new PdfCompatibilitySectionLooper();
1745 return kOK_PdfResult;
1746}
1747
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001748static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001749#ifdef ASSERT_BAD_PDF_OPS
1750 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1751 // have the assert when testing good pdfs.
1752#endif
1753 return kIgnoreError_PdfResult;
1754}
1755
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001756static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001757 *looper = new PdfInlineImageLooper();
1758 return kOK_PdfResult;
1759}
1760
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001761static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001762#ifdef ASSERT_BAD_PDF_OPS
1763 SkASSERT(false); // must be processed in inline image looper, but let's
1764 // have the assert when testing good pdfs.
1765#endif
1766 return kIgnoreError_PdfResult;
1767}
1768
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001769static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001770#ifdef ASSERT_BAD_PDF_OPS
1771 SkASSERT(false); // must be processed in inline image looper, but let's
1772 // have the assert when testing good pdfs.
1773#endif
1774 return kIgnoreError_PdfResult;
1775}
1776
edisonn@google.com24cdf132013-07-30 16:06:12 +00001777
1778// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1779PdfResult skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1780 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1781 return kOK_PdfResult;
1782}
1783
1784PdfResult skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1785 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1786 return kOK_PdfResult;
1787}
1788
1789PdfResult skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001790 pdfContext->fGraphicsState.fLineWidth = lineWidth;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001791 return kOK_PdfResult;
1792}
1793
1794PdfResult skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1795 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1796 return kOK_PdfResult;
1797}
1798
1799PdfResult skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1800 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1801 return kOK_PdfResult;
1802}
1803
1804PdfResult skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1805 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1806 return kOK_PdfResult;
1807}
1808
1809// TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure
1810/*
18111) [ ] 0 No dash; solid, unbroken lines
18122) [3] 0 3 units on, 3 units off, …
18133) [2] 1 1 on, 2 off, 2 on, 2 off, …
18144) [2 1] 0 2 on, 1 off, 2 on, 1 off, …
18155) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, …
18166) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, …
1817 */
1818
1819PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* intervals, SkPdfObject* phase) {
1820 int cnt = intervals->size();
1821 if (cnt >= 256) {
1822 // TODO(edisonn): report error/warning, unsuported;
1823 // TODO(edisonn): alloc memory
1824 return kIgnoreError_PdfResult;
1825 }
1826 for (int i = 0; i < cnt; i++) {
1827 if (!intervals->objAtAIndex(i)->isNumber()) {
1828 // TODO(edisonn): report error/warning
1829 return kIgnoreError_PdfResult;
1830 }
1831 }
1832
edisonn@google.com24cdf132013-07-30 16:06:12 +00001833 double total = 0;
1834 for (int i = 0 ; i < cnt; i++) {
1835 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1836 total += pdfContext->fGraphicsState.fDashArray[i];
1837 }
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001838 if (cnt & 1) {
1839 if (cnt == 1) {
1840 pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0];
1841 cnt++;
1842 } else {
1843 // TODO(edisonn): report error/warning
1844 return kNYI_PdfResult;
1845 }
1846 }
1847 pdfContext->fGraphicsState.fDashArrayLength = cnt;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001848 pdfContext->fGraphicsState.fDashPhase = phase->scalarValue();
1849 if (pdfContext->fGraphicsState.fDashPhase == 0) {
1850 // other rules, changes?
1851 pdfContext->fGraphicsState.fDashPhase = total;
1852 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001853
1854 return kOK_PdfResult;
1855}
1856
edisonn@google.com24cdf132013-07-30 16:06:12 +00001857PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1858 // TODO(edisonn): verify input
1859 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1860 // TODO(edisonn): report error/warning
1861 return kIgnoreError_PdfResult;
1862 }
1863 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1));
1864}
1865
1866void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1867 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1868 // TODO(edisonn): report error/warning
1869 return;
1870 }
1871 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1872}
1873
1874
1875//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
1876static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1877 double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1878 return skpdfGraphicsStateApplyLW(pdfContext, lw);
1879}
1880
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001881//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 +00001882static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001883 int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1884 return skpdfGraphicsStateApplyLC(pdfContext, lc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001885}
1886
1887//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 +00001888static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001889 double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1890 return skpdfGraphicsStateApplyLJ(pdfContext, lj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001891}
1892
1893//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001894static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001895 double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1896 return skpdfGraphicsStateApplyML(pdfContext, ml);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001897}
1898
1899//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1900//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001901static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001902 SkPdfObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1903 SkPdfObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001904
edisonn@google.com24cdf132013-07-30 16:06:12 +00001905 if (!array->isArray()) {
1906 return kIgnoreError_PdfResult;
1907 }
1908
1909 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001910}
1911
1912//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 +00001913static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001914 pdfContext->fObjectStack.pop();
1915
1916 return kNYI_PdfResult;
1917}
1918
1919//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1920//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1921//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001922static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001923 pdfContext->fObjectStack.pop();
1924
1925 return kNYI_PdfResult;
1926}
1927
edisonn@google.come878e722013-07-29 19:10:58 +00001928SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1929
1930class InitBlendModes {
1931public:
1932 InitBlendModes() {
1933 // TODO(edisonn): use the python code generator?
1934 // TABLE 7.2 Standard separable blend modes
1935 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1936 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1937 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1938 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1939 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1940 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1941 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1942 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1943 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1944 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1945 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1946 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1947
1948 // TABLE 7.3 Standard nonseparable blend modes
1949 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1950 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1951 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1952 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1953 }
1954};
1955
1956InitBlendModes _gDummyInniter;
1957
1958SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1959 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1960 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1961 return mode;
1962 }
1963
1964 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1965}
1966
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001967void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001968 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1969 if (mode <= SkXfermode::kLastMode) {
1970 pdfContext->fGraphicsState.fBlendModesLength = 1;
1971 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1972 } else {
1973 // TODO(edisonn): report unknown blend mode
1974 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001975}
1976
1977void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001978 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
1979 // TODO(edisonn): report error/warning
1980 return;
1981 }
1982 SkXfermode::Mode modes[256];
1983 int cnt = blendModes->size();
1984 for (int i = 0; i < cnt; i++) {
1985 SkPdfObject* name = blendModes->objAtAIndex(i);
1986 if (!name->isName()) {
1987 // TODO(edisonn): report error/warning
1988 return;
1989 }
1990 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
1991 if (mode > SkXfermode::kLastMode) {
1992 // TODO(edisonn): report error/warning
1993 return;
1994 }
1995 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001996
edisonn@google.come878e722013-07-29 19:10:58 +00001997 pdfContext->fGraphicsState.fBlendModesLength = cnt;
1998 for (int i = 0; i < cnt; i++) {
1999 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
2000 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002001}
2002
2003void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
2004 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00002005 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002006 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00002007 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
2008 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
2009 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
2010 } else {
2011 // TODO (edisonn): report error/warning
2012 }
2013}
2014
2015void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002016 if (sMask == "None") {
2017 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002018 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002019 return;
2020 }
2021
edisonn@google.come878e722013-07-29 19:10:58 +00002022 //Next, get the ExtGState Dictionary from the Resource Dictionary:
2023 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
2024
2025 if (extGStateDictionary == NULL) {
2026#ifdef PDF_TRACE
2027 printf("ExtGState is NULL!\n");
2028#endif
2029 // TODO (edisonn): report error/warning
2030 return;
2031 }
2032
2033 SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
2034 if (!obj || !obj->isDictionary()) {
2035 // TODO (edisonn): report error/warning
2036 return;
2037 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002038
2039 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002040 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002041
edisonn@google.come878e722013-07-29 19:10:58 +00002042 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002043}
2044
2045void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
2046 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
2047}
2048
2049
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002050//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
2051//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 +00002052static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002053 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002054
2055#ifdef PDF_TRACE
2056 std::string str;
2057#endif
2058
2059 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00002060 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002061
2062 if (extGStateDictionary == NULL) {
2063#ifdef PDF_TRACE
2064 printf("ExtGState is NULL!\n");
2065#endif
2066 return kIgnoreError_PdfResult;
2067 }
2068
edisonn@google.com571c70b2013-07-10 17:09:50 +00002069 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002070
edisonn@google.com571c70b2013-07-10 17:09:50 +00002071 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
2072 return kIgnoreError_PdfResult;
2073 }
2074 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002075
2076 // TODO(edisonn): now load all those properties in graphic state.
2077 if (gs == NULL) {
2078 return kIgnoreError_PdfResult;
2079 }
2080
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002081 if (gs->has_LW()) {
2082 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
2083 }
2084
2085 if (gs->has_LC()) {
2086 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
2087 }
2088
2089 if (gs->has_LJ()) {
2090 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
2091 }
2092
2093 if (gs->has_ML()) {
2094 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
2095 }
2096
2097 if (gs->has_D()) {
2098 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
2099 }
2100
2101 if (gs->has_Font()) {
2102 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
2103 }
2104
2105 if (gs->has_BM()) {
2106 if (gs->isBMAName(pdfContext->fPdfDoc)) {
2107 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
2108 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
2109 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
2110 } else {
2111 // TODO(edisonn): report/warn
2112 }
2113 }
2114
2115 if (gs->has_SMask()) {
2116 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
2117 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
2118 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
2119 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
2120 } else {
2121 // TODO(edisonn): report/warn
2122 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002123 }
2124
2125 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002126 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002127 }
2128
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002129 if (gs->has_CA()) {
2130 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
2131 }
2132
2133 if (gs->has_AIS()) {
2134 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002135 }
2136
edisonn@google.com9a43c182013-08-01 20:06:42 +00002137 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002138}
2139
2140//charSpace Tc Set the character spacing, Tc
2141//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
2142//Initial value: 0.
2143PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002144 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002145 pdfContext->fGraphicsState.fCharSpace = charSpace;
2146
2147 return kOK_PdfResult;
2148}
2149
2150//wordSpace Tw Set the word spacing, T
2151//w
2152//, to wordSpace, which is a number expressed in unscaled
2153//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
2154//value: 0.
2155PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002156 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002157 pdfContext->fGraphicsState.fWordSpace = wordSpace;
2158
2159 return kOK_PdfResult;
2160}
2161
2162//scale Tz Set the horizontal scaling, Th
2163//, to (scale ˜ 100). scale is a number specifying the
2164//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002165static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002166 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002167
2168 return kNYI_PdfResult;
2169}
2170
2171//render Tr Set the text rendering mode, T
2172//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002173static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002174 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002175
2176 return kNYI_PdfResult;
2177}
2178//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
2179//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002180static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002181 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002182
2183 return kNYI_PdfResult;
2184}
2185
2186//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002187static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002188 pdfContext->fObjectStack.pop();
2189 pdfContext->fObjectStack.pop();
2190
2191 return kNYI_PdfResult;
2192}
2193
2194//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002195static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002196 pdfContext->fObjectStack.pop();
2197 pdfContext->fObjectStack.pop();
2198 pdfContext->fObjectStack.pop();
2199 pdfContext->fObjectStack.pop();
2200 pdfContext->fObjectStack.pop();
2201 pdfContext->fObjectStack.pop();
2202
2203 return kNYI_PdfResult;
2204}
2205
2206//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002207static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002208 pdfContext->fObjectStack.pop();
2209
2210 return kNYI_PdfResult;
2211}
2212
2213//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002214static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002215 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002216
edisonn@google.com571c70b2013-07-10 17:09:50 +00002217 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002218
2219 if (xObject == NULL) {
2220#ifdef PDF_TRACE
2221 printf("XObject is NULL!\n");
2222#endif
2223 return kIgnoreError_PdfResult;
2224 }
2225
edisonn@google.com571c70b2013-07-10 17:09:50 +00002226 SkPdfObject* value = xObject->get(name);
2227 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002228
2229#ifdef PDF_TRACE
2230// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002231// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002232#endif
2233
edisonn@google.com571c70b2013-07-10 17:09:50 +00002234 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002235}
2236
2237//tag MP Designate a marked-content point. tag is a name object indicating the role or
2238//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002239static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002240 pdfContext->fObjectStack.pop();
2241
2242 return kNYI_PdfResult;
2243}
2244
2245//tag properties DP Designate a marked-content point with an associated property list. tag is a
2246//name object indicating the role or significance of the point; properties is
2247//either an inline dictionary containing the property list or a name object
2248//associated with it in the Properties subdictionary of the current resource
2249//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002250static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002251 pdfContext->fObjectStack.pop();
2252 pdfContext->fObjectStack.pop();
2253
2254 return kNYI_PdfResult;
2255}
2256
2257//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
2258//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002259static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002260 pdfContext->fObjectStack.pop();
2261
2262 return kNYI_PdfResult;
2263}
2264
2265//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
2266//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
2267//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 +00002268static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002269 pdfContext->fObjectStack.pop();
2270 pdfContext->fObjectStack.pop();
2271
2272 return kNYI_PdfResult;
2273}
2274
2275//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002276static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002277 return kNYI_PdfResult;
2278}
2279
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002280static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002281 static bool gInitialized = false;
2282 if (gInitialized) {
2283 return;
2284 }
2285
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002286 gPdfOps.set("q", PdfOp_q);
2287 gPdfOps.set("Q", PdfOp_Q);
2288 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002289
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002290 gPdfOps.set("TD", PdfOp_TD);
2291 gPdfOps.set("Td", PdfOp_Td);
2292 gPdfOps.set("Tm", PdfOp_Tm);
2293 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002294
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002295 gPdfOps.set("m", PdfOp_m);
2296 gPdfOps.set("l", PdfOp_l);
2297 gPdfOps.set("c", PdfOp_c);
2298 gPdfOps.set("v", PdfOp_v);
2299 gPdfOps.set("y", PdfOp_y);
2300 gPdfOps.set("h", PdfOp_h);
2301 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002302
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002303 gPdfOps.set("S", PdfOp_S);
2304 gPdfOps.set("s", PdfOp_s);
2305 gPdfOps.set("f", PdfOp_f);
2306 gPdfOps.set("F", PdfOp_F);
2307 gPdfOps.set("f*", PdfOp_f_star);
2308 gPdfOps.set("B", PdfOp_B);
2309 gPdfOps.set("B*", PdfOp_B_star);
2310 gPdfOps.set("b", PdfOp_b);
2311 gPdfOps.set("b*", PdfOp_b_star);
2312 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002313
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002314 gPdfOps.set("BT", PdfOp_BT);
2315 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002316
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002317 gPdfOps.set("Tj", PdfOp_Tj);
2318 gPdfOps.set("'", PdfOp_quote);
2319 gPdfOps.set("\"", PdfOp_doublequote);
2320 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002321
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002322 gPdfOps.set("CS", PdfOp_CS);
2323 gPdfOps.set("cs", PdfOp_cs);
2324 gPdfOps.set("SC", PdfOp_SC);
2325 gPdfOps.set("SCN", PdfOp_SCN);
2326 gPdfOps.set("sc", PdfOp_sc);
2327 gPdfOps.set("scn", PdfOp_scn);
2328 gPdfOps.set("G", PdfOp_G);
2329 gPdfOps.set("g", PdfOp_g);
2330 gPdfOps.set("RG", PdfOp_RG);
2331 gPdfOps.set("rg", PdfOp_rg);
2332 gPdfOps.set("K", PdfOp_K);
2333 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002334
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002335 gPdfOps.set("W", PdfOp_W);
2336 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002337
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002338 gPdfOps.set("BX", PdfOp_BX);
2339 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002340
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002341 gPdfOps.set("BI", PdfOp_BI);
2342 gPdfOps.set("ID", PdfOp_ID);
2343 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002344
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002345 gPdfOps.set("w", PdfOp_w);
2346 gPdfOps.set("J", PdfOp_J);
2347 gPdfOps.set("j", PdfOp_j);
2348 gPdfOps.set("M", PdfOp_M);
2349 gPdfOps.set("d", PdfOp_d);
2350 gPdfOps.set("ri", PdfOp_ri);
2351 gPdfOps.set("i", PdfOp_i);
2352 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002353
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002354 gPdfOps.set("Tc", PdfOp_Tc);
2355 gPdfOps.set("Tw", PdfOp_Tw);
2356 gPdfOps.set("Tz", PdfOp_Tz);
2357 gPdfOps.set("TL", PdfOp_TL);
2358 gPdfOps.set("Tf", PdfOp_Tf);
2359 gPdfOps.set("Tr", PdfOp_Tr);
2360 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002361
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002362 gPdfOps.set("d0", PdfOp_d0);
2363 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002364
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002365 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002366
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002367 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002368
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002369 gPdfOps.set("MP", PdfOp_MP);
2370 gPdfOps.set("DP", PdfOp_DP);
2371 gPdfOps.set("BMC", PdfOp_BMC);
2372 gPdfOps.set("BDC", PdfOp_BDC);
2373 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002374
2375 gInitialized = true;
2376}
2377
2378class InitPdfOps {
2379public:
2380 InitPdfOps() {
2381 initPdfOperatorRenderes();
2382 }
2383};
2384
2385InitPdfOps gInitPdfOps;
2386
2387void reportPdfRenderStats() {
2388 std::map<std::string, int>::iterator iter;
2389
2390 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002391 SkTDict<int>::Iter iter(gRenderStats[i]);
2392 const char* key;
2393 int value = 0;
2394 while ((key = iter.next(&value)) != NULL) {
2395 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002396 }
2397 }
2398}
2399
2400PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002401 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002402 {
2403 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002404 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002405 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002406 // caller, main work is done by pdfOperatorRenderer(...)
2407 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002408 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
2409
2410 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002411 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2412 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002413
2414 if (childLooper) {
2415 childLooper->setUp(this);
2416 childLooper->loop();
2417 delete childLooper;
2418 }
2419 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002420 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002421 gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2422 gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002423 }
2424 }
2425 else if (token.fType == kObject_TokenType)
2426 {
2427 fPdfContext->fObjectStack.push( token.fObject );
2428 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002429 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002430 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002431 return kIgnoreError_PdfResult;
2432 }
2433 return kOK_PdfResult;
2434}
2435
2436void PdfMainLooper::loop() {
2437 PdfToken token;
2438 while (readToken(fTokenizer, &token)) {
2439 consumeToken(token);
2440 }
2441}
2442
2443PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002444 SkASSERT(false);
2445 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002446}
2447
2448void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002449 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002450}
2451
2452PdfResult PdfInlineImageLooper::done() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002453 return kNYI_PdfResult;
2454}
2455
2456PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2457 return fParent->consumeToken(token);
2458}
2459
2460void PdfCompatibilitySectionLooper::loop() {
2461 // TODO(edisonn): save stacks position, or create a new stack?
2462 // TODO(edisonn): what happens if we pop out more variables then when we started?
2463 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2464 // pop-ing too much will not affect outside the section.
2465 PdfToken token;
2466 while (readToken(fTokenizer, &token)) {
2467 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2468 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2469 looper->setUp(this);
2470 looper->loop();
2471 delete looper;
2472 } else {
2473 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2474 fParent->consumeToken(token);
2475 }
2476 }
2477 // TODO(edisonn): restore stack.
2478}
2479
2480// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2481// TODO(edisonn): Add API for Forms viewing and editing
2482// e.g. SkBitmap getPage(int page);
2483// int formsCount();
2484// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2485// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2486// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2487// resources needed, so the preview is fast.
2488// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2489// references automatically.
2490
edisonn@google.com222382b2013-07-10 22:33:10 +00002491PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002492
edisonn@google.com444e25a2013-07-11 15:20:50 +00002493bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002494 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002495 return false;
2496 }
2497
edisonn@google.com222382b2013-07-10 22:33:10 +00002498 if (page < 0 || page >= pages()) {
2499 return false;
2500 }
2501
edisonn@google.com222382b2013-07-10 22:33:10 +00002502 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002503
edisonn@google.com222382b2013-07-10 22:33:10 +00002504 pdfContext.fOriginalMatrix = SkMatrix::I();
2505 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2506
2507 gPdfContext = &pdfContext;
2508
2509 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002510 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002511 SkScalar w = dst.width();
2512 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002513
edisonn@google.com444e25a2013-07-11 15:20:50 +00002514 SkScalar wp = fPdfDoc->MediaBox(page).width();
2515 SkScalar hp = fPdfDoc->MediaBox(page).height();
2516
2517 SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(wp, z), SkPoint::Make(wp, hp), SkPoint::Make(z, hp)};
edisonn@google.com222382b2013-07-10 22:33:10 +00002518// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2519
2520 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2521 // TODO(edisonn): add flagg for no clipping.
2522 // Use larger image to make sure we do not draw anything outside of page
2523 // could be used in tests.
2524
2525#ifdef PDF_DEBUG_3X
2526 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)};
2527#else
2528 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2529#endif
2530 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2531 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2532
2533 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2534 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2535
2536 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2537 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2538
2539 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2540 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2541
2542
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002543 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
2544 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2545 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002546
edisonn@google.com222382b2013-07-10 22:33:10 +00002547#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002548 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002549#endif
2550
edisonn@google.com444e25a2013-07-11 15:20:50 +00002551 canvas->setMatrix(pdfContext.fOriginalMatrix);
2552
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002553 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2554
2555 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002556// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002557// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002558// canvas->drawRect(rect, paint);
2559
edisonn@google.com222382b2013-07-10 22:33:10 +00002560
2561 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002562 return true;
2563}
edisonn@google.com222382b2013-07-10 22:33:10 +00002564
2565bool SkPdfRenderer::load(const SkString inputFileName) {
2566 unload();
2567
2568 // TODO(edisonn): create static function that could return NULL if there are errors
2569 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002570 if (fPdfDoc->pages() == 0) {
2571 delete fPdfDoc;
2572 fPdfDoc = NULL;
2573 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002574
2575 return fPdfDoc != NULL;
2576}
2577
edisonn@google.com147adb12013-07-24 15:56:19 +00002578bool SkPdfRenderer::load(SkStream* stream) {
2579 unload();
2580
2581 // TODO(edisonn): create static function that could return NULL if there are errors
2582 fPdfDoc = new SkNativeParsedPDF(stream);
2583 if (fPdfDoc->pages() == 0) {
2584 delete fPdfDoc;
2585 fPdfDoc = NULL;
2586 }
2587
2588 return fPdfDoc != NULL;
2589}
2590
2591
edisonn@google.com222382b2013-07-10 22:33:10 +00002592int SkPdfRenderer::pages() const {
2593 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2594}
2595
2596void SkPdfRenderer::unload() {
2597 delete fPdfDoc;
2598 fPdfDoc = NULL;
2599}
2600
2601SkRect SkPdfRenderer::MediaBox(int page) const {
2602 SkASSERT(fPdfDoc);
2603 return fPdfDoc->MediaBox(page);
2604}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002605
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002606size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002607 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2608}
edisonn@google.com147adb12013-07-24 15:56:19 +00002609
2610bool SkPDFNativeRenderToBitmap(SkStream* stream,
2611 SkBitmap* output,
2612 int page,
2613 SkPdfContent content,
2614 double dpi) {
2615 SkASSERT(page >= 0);
2616 SkPdfRenderer renderer;
2617 renderer.load(stream);
2618 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2619 return false;
2620 }
2621
2622 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2623
2624 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2625 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2626
2627 rect = SkRect::MakeWH(width, height);
2628
2629 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2630
2631 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2632 SkCanvas canvas(device);
2633
2634 return renderer.renderPage(page, &canvas, rect);
2635}