blob: cd2d7d60733a3770496d22094a78240c30db2fc6 [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++;
edisonn@google.com0f901902013-08-07 11:56:16 +0000239 gLastOpKeyword++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000240#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.
edisonn@google.com0f901902013-08-07 11:56:16 +0000243 if (gLastKeyword[0] && hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000244 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 }
edisonn@google.com0f901902013-08-07 11:56:16 +0000314
315 if (ret && token->fType == kKeyword_TokenType && token->fKeyword && token->fKeywordLength > 0 && token->fKeywordLength < 100) {
316 strncpy(gLastKeyword, token->fKeyword, token->fKeywordLength);
317 gLastKeyword[token->fKeywordLength] = '\0';
318 } else {
319 gLastKeyword[0] = '\0';
320 }
321
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000322#endif
323
324 return ret;
325}
326
327
328
329typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
330
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000331SkTDict<PdfOperatorRenderer> gPdfOps(100);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000332
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000333
334template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
335public:
336 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
337};
338
339SkTDictWithDefaultConstructor<int> gRenderStats[kCount_PdfResult];
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000340
edisonn@google.com571c70b2013-07-10 17:09:50 +0000341const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000342 "Success",
343 "Partially implemented",
344 "Not yet implemented",
345 "Ignore Error",
346 "Error",
347 "Unsupported/Unknown"
348};
349
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000350static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000351 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000352 SkCanvas* canvas)
353{
354
355 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
356 if (skfont == NULL) {
357 skfont = SkPdfFont::Default();
358 }
359
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000360
edisonn@google.com571c70b2013-07-10 17:09:50 +0000361 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000362 // TODO(edisonn): report warning
363 return kIgnoreError_PdfResult;
364 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000365 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000366
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000367 SkUnencodedText binary(str);
368
369 SkDecodedText decoded;
370
371 if (skfont->encoding() == NULL) {
372 // TODO(edisonn): report warning
373 return kNYI_PdfResult;
374 }
375
376 skfont->encoding()->decodeText(binary, &decoded);
377
378 SkPaint paint;
379 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
380 // Or maybe just not call setTextSize at all?
381 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
382 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
383 }
384
385// if (fCurFont && fCurFont->GetFontScale() != 0) {
386// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
387// }
388
389 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
390
edisonn@google.com6e49c342013-06-27 20:03:43 +0000391 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000392
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000393 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000394}
395
396// TODO(edisonn): create header files with declarations!
397PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
398PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
399PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
400PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
401
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000402// TODO(edisonn): perf!!!
403
404static SkColorTable* getGrayColortable() {
405 static SkColorTable* grayColortable = NULL;
406 if (grayColortable == NULL) {
407 SkPMColor* colors = new SkPMColor[256];
408 for (int i = 0 ; i < 256; i++) {
409 colors[i] = SkPreMultiplyARGB(255, i, i, i);
410 }
411 grayColortable = new SkColorTable(colors, 256);
412 }
413 return grayColortable;
414}
415
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000416static SkBitmap* transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000417 int width, int height, int bytesPerLine,
418 int bpc, const std::string& colorSpace,
419 bool transparencyMask) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000420 SkBitmap* bitmap = new SkBitmap();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000421
edisonn@google.com571c70b2013-07-10 17:09:50 +0000422 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000423//#define MAX_COMPONENTS 10
424
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000425 // TODO(edisonn): assume start of lines are aligned at 32 bits?
426 // Is there a faster way to load the uncompressed stream into a bitmap?
427
428 // minimal support for now
429 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
430 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
431
432 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000433 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000434 for (int w = 0 ; w < width; w++) {
435 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
436 uncompressedStream[3 * w + 1],
437 uncompressedStream[3 * w + 2]);
438 i++;
439 }
440 uncompressedStream += bytesPerLine;
441 }
442
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000443 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
444 bitmap->setPixels(uncompressedStreamArgb);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000445 }
446 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
447 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
448
449 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000450 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000451 for (int w = 0 ; w < width; w++) {
452 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
453 uncompressedStream[w];
454 i++;
455 }
456 uncompressedStream += bytesPerLine;
457 }
458
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000459 bitmap->setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000460 width, height);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000461 bitmap->setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000462 }
463
464 // TODO(edisonn): Report Warning, NYI, or error
465 return bitmap;
466}
467
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000468// utils
469
470// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
471// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
472// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
473// skia format, through a table
474
475// this functions returns the image, it does not look at the smask.
476
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000477static SkBitmap* getImageFromObjectCore(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000478 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000479 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000480 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000481 }
482
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000483 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
484 int64_t width = image->Width(pdfContext->fPdfDoc);
485 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000486 std::string colorSpace = "DeviceRGB";
487
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000488 bool indexed = false;
489 SkPMColor colors[256];
490 int cnt = 0;
491
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000492 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000493 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
494 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000495 } else if (image->isColorSpaceAArray(pdfContext->fPdfDoc)) {
496 SkPdfArray* array = image->getColorSpaceAsArray(pdfContext->fPdfDoc);
497 if (array && array->size() == 4 && array->objAtAIndex(0)->isName("Indexed") &&
498 (array->objAtAIndex(1)->isName("DeviceRGB") || array->objAtAIndex(1)->isName("RGB")) &&
499 array->objAtAIndex(2)->isInteger() &&
500 array->objAtAIndex(3)->isHexString()
501 ) {
502 // TODO(edisonn): suport only DeviceRGB for now.
503 indexed = true;
504 cnt = array->objAtAIndex(2)->intValue() + 1;
505 if (cnt > 256) {
506 // TODO(edionn): report NYIs
507 return NULL;
508 }
509 SkColorTable colorTable(cnt);
510 NotOwnedString data = array->objAtAIndex(3)->strRef();
511 if (data.fBytes != (unsigned int)cnt * 3) {
512 // TODO(edionn): report error/warning
513 return NULL;
514 }
515 for (int i = 0 ; i < cnt; i++) {
516 colors[i] = SkPreMultiplyARGB(0xff, data.fBuffer[3 * i], data.fBuffer[3 * i + 1], data.fBuffer[3 * i + 2]);
517 }
518 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000519 }
520
521/*
522 bool imageMask = image->imageMask();
523
524 if (imageMask) {
525 if (bpc != 0 && bpc != 1) {
526 // TODO(edisonn): report warning to be used in testing.
527 return SkBitmap();
528 }
529 bpc = 1;
530 }
531*/
532
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000533 const unsigned char* uncompressedStream = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000534 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000535
edisonn@google.com571c70b2013-07-10 17:09:50 +0000536 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000537
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000538 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000539 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000540 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000541 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000542 }
543
edisonn@google.com571c70b2013-07-10 17:09:50 +0000544 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
545
546 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
547 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
548 (streamDict->isFilterAArray(NULL) &&
549 streamDict->getFilterAsArray(NULL)->size() > 0 &&
550 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
551 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000552 SkBitmap* bitmap = new SkBitmap();
553 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, bitmap);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000554 return bitmap;
555 }
556
557
558
559 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
560// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
561// obj.GetDictionary().GetKey(PdfName("Filter")));
562// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
563// value = resolveReferenceObject(pdfContext->fPdfDoc,
564// &value->GetArray()[0]);
565// }
566// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
567// SkStream stream = SkStream::
568// SkImageDecoder::Factory()
569// }
570
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000571 // TODO(edisonn): assumes RGB for now, since it is the only onwe implemented
572 if (indexed) {
573 SkBitmap* bitmap = new SkBitmap();
574 bitmap->setConfig(SkBitmap::kIndex8_Config, width, height);
575 SkColorTable* colorTable = new SkColorTable(colors, cnt);
576 bitmap->setPixels((void*)uncompressedStream, colorTable);
577 return bitmap;
578 }
579
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000580 int bytesPerLine = (int)(uncompressedStreamLength / height);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000581#ifdef PDF_TRACE
582 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000583 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000584 }
585#endif
586
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000587 SkBitmap* bitmap = transferImageStreamToBitmap(
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000588 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000589 (int)width, (int)height, bytesPerLine,
590 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000591 transparencyMask);
592
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000593 return bitmap;
594}
595
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000596static SkBitmap* getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
597 if (!transparencyMask) {
598 if (!image->hasData(SkPdfObject::kBitmap_Data)) {
599 SkBitmap* bitmap = getImageFromObjectCore(pdfContext, image, transparencyMask);
600 image->setData(bitmap, SkPdfObject::kBitmap_Data);
601 }
602 return (SkBitmap*) image->data(SkPdfObject::kBitmap_Data);
603 } else {
604 return getImageFromObjectCore(pdfContext, image, transparencyMask);
605 }
606}
607
608static SkBitmap* getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000609 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000610
611 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000612 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000613 }
614
615 // TODO(edisonn): implement GS SMask. Default to empty right now.
616 return pdfContext->fGraphicsState.fSMask;
617}
618
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000619static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000620 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000621 return kIgnoreError_PdfResult;
622 }
623
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000624 SkBitmap* image = getImageFromObject(pdfContext, skpdfimage, false);
625 SkBitmap* sMask = getSmaskFromObject(pdfContext, skpdfimage);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000626
627 canvas->save();
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000628 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000629
edisonn@google.com571c70b2013-07-10 17:09:50 +0000630 SkScalar z = SkIntToScalar(0);
631 SkScalar one = SkIntToScalar(1);
632
633 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
634 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
635 SkMatrix flip;
636 SkAssertResult(flip.setPolyToPoly(from, to, 4));
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000637 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000638 solveImageFlip.preConcat(flip);
639 canvas->setMatrix(solveImageFlip);
edisonn@google.com0f901902013-08-07 11:56:16 +0000640
641#ifdef PDF_TRACE
642 SkPoint final[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
643 solveImageFlip.mapPoints(final, 4);
644 printf("IMAGE rect = ");
645 for (int i = 0; i < 4; i++) {
646 printf("(%f %f) ", SkScalarToDouble(final[i].x()), SkScalarToDouble(final[i].y()));
647 }
648 printf("\n");
649#endif // PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +0000650
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000651 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
652
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000653 // TODO(edisonn): soft mask type? alpha/luminosity.
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000654 SkPaint paint;
655 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
656
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000657 if (!sMask || sMask->empty()) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000658 canvas->drawBitmapRect(*image, dst, &paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000659 } else {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000660 canvas->saveLayer(&dst, &paint);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000661 canvas->drawBitmapRect(*image, dst, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000662 SkPaint xfer;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000663 // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf?
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000664 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000665 canvas->drawBitmapRect(*sMask, dst, &xfer);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000666 canvas->restore();
667 }
668
669 canvas->restore();
670
671 return kPartial_PdfResult;
672}
673
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000674//TODO(edisonn): options for implementing isolation and knockout
675// 1) emulate them (current solution)
676// PRO: simple
677// CON: will need to use readPixels, which means serious perf issues
678// 2) Compile a plan for an array of matrixes, compose the result at the end
679// PRO: might be faster then 1, no need to readPixels
680// CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan
681// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
682// 3) support them natively in SkCanvas
683// PRO: simple
684// CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1)
685// 4) compile a plan using pathops, and render once without any fancy rules with backdrop
686// PRO: simple, fast
687// CON: pathops must be bug free first + time to compute new paths
688// pay a price at loading pdf to compute a pdf draw plan
689// 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 +0000690// 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 +0000691
692
693// TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree!
694// TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed
695
696
697
698static void doGroup_before(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) {
699 SkRect bboxOrig = bbox;
700 SkBitmap backdrop;
701 bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc);
702// bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000703 SkPaint paint;
704 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
705 canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000706}
707
edisonn@google.com251176e2013-08-01 13:24:00 +0000708// TODO(edisonn): non isolation implemented in skia
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000709//static void doGroup_after(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup) {
edisonn@google.com251176e2013-08-01 13:24:00 +0000710// if not isolated
711// canvas->drawBitmapRect(backdrop, bboxOrig, NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000712//}
713
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000714static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000715 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000716 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000717 }
718
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000719 if (!skobj->has_BBox()) {
720 return kIgnoreError_PdfResult;
721 }
722
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000723 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000724
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000725
edisonn@google.com571c70b2013-07-10 17:09:50 +0000726 if (skobj->Resources(pdfContext->fPdfDoc)) {
727 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000728 }
729
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000730 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000731
edisonn@google.com571c70b2013-07-10 17:09:50 +0000732 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000733 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000734 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
735 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
736 pdfContext->fGraphicsState.fMatrixTm = matrix;
737 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000738 // TODO(edisonn) reset matrixTm and matricTlm also?
739 }
740
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000741 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com0f901902013-08-07 11:56:16 +0000742 pdfContext->fGraphicsState.fContentStreamMatrix = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000743
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000744 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000745
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000746 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
747 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000748
749 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
750 // For this PdfContentsTokenizer needs to be extended.
751
edisonn@google.come878e722013-07-29 19:10:58 +0000752 // This is a group?
753 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000754 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
755 doGroup_before(pdfContext, canvas, bbox, tgroup, false);
edisonn@google.come878e722013-07-29 19:10:58 +0000756 }
757
edisonn@google.com571c70b2013-07-10 17:09:50 +0000758 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000759
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000760 SkPdfNativeTokenizer* tokenizer =
761 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000762 if (tokenizer != NULL) {
763 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
764 looper.loop();
765 delete tokenizer;
766 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000767
768 // TODO(edisonn): should we restore the variable stack at the same state?
769 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000770
771 if (skobj->has_Group()) {
772 canvas->restore();
773 }
774
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000775 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000776 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000777}
778
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000779
780// TODO(edisonn): Extract a class like ObjWithStream
781static PdfResult doXObject_Pattern(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1PatternDictionary* skobj) {
782 if (!skobj || !skobj->hasStream()) {
783 return kIgnoreError_PdfResult;
784 }
785
786 if (!skobj->has_BBox()) {
787 return kIgnoreError_PdfResult;
788 }
789
790 PdfOp_q(pdfContext, canvas, NULL);
791
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000792
793 if (skobj->Resources(pdfContext->fPdfDoc)) {
794 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
795 }
796
edisonn@google.com0f901902013-08-07 11:56:16 +0000797 SkTraceMatrix(pdfContext->fGraphicsState.fContentStreamMatrix, "Current Content stream matrix");
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000798
799 if (skobj->has_Matrix()) {
edisonn@google.com0f901902013-08-07 11:56:16 +0000800 pdfContext->fGraphicsState.fContentStreamMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000801 }
802
edisonn@google.com0f901902013-08-07 11:56:16 +0000803 SkTraceMatrix(pdfContext->fGraphicsState.fContentStreamMatrix, "Total Content stream matrix");
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000804
edisonn@google.com0f901902013-08-07 11:56:16 +0000805 canvas->setMatrix(pdfContext->fGraphicsState.fContentStreamMatrix);
806 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fContentStreamMatrix;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000807
808 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
809 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
810
811 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
812 // For this PdfContentsTokenizer needs to be extended.
813
814 SkPdfStream* stream = (SkPdfStream*)skobj;
815
816 SkPdfNativeTokenizer* tokenizer =
817 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
818 if (tokenizer != NULL) {
819 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
820 looper.loop();
821 delete tokenizer;
822 }
823
824 // TODO(edisonn): should we restore the variable stack at the same state?
825 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
826
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000827 PdfOp_Q(pdfContext, canvas, NULL);
828 return kPartial_PdfResult;
829}
830
831
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000832//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
833// return kNYI_PdfResult;
834//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000835
edisonn@google.com571c70b2013-07-10 17:09:50 +0000836PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
837 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000838 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000839 }
840
841 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000842
843 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
844 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000845 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrixTm;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000846
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000847 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
edisonn@google.come57c62d2013-08-07 18:04:15 +0000848 pdfContext->fGraphicsState.fCTM.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000849
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000850 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000851
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000852 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000853
854 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000855 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000856
857 SkTraceRect(rm, "bbox mapped");
858
859 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
860
861 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
862 // For this PdfContentsTokenizer needs to be extended.
863
edisonn@google.com571c70b2013-07-10 17:09:50 +0000864 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000865
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000866 SkPdfNativeTokenizer* tokenizer =
867 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000868 if (tokenizer != NULL) {
869 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
870 looper.loop();
871 delete tokenizer;
872 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000873
874 // TODO(edisonn): should we restore the variable stack at the same state?
875 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000876 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000877
878 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000879}
880
881
edisonn@google.com571c70b2013-07-10 17:09:50 +0000882// TODO(edisonn): make sure the pointer is unique
883std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000884
885class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000886 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000887public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000888 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000889 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000890 }
891
892 ~CheckRecursiveRendering() {
893 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000894 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000895 }
896
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000897 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000898 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000899 }
900};
901
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000902static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000903 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000904 // Oops, corrupt PDF!
905 return kIgnoreError_PdfResult;
906 }
907
edisonn@google.com571c70b2013-07-10 17:09:50 +0000908 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000909
edisonn@google.com571c70b2013-07-10 17:09:50 +0000910 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000911 {
912 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000913 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000914 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000915 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000916 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
917 //return doXObject_PS(skxobj.asPS());
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000918 default: {
919 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(obj) != kNone_SkPdfObjectType) {
920 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)obj;
921 return doXObject_Pattern(pdfContext, canvas, pattern);
922 }
923 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000924 }
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000925 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000926}
927
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000928static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
929 if (!skobj) {
930 return kIgnoreError_PdfResult;
931 }
932
933 if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
934 return kNYI_PdfResult;
935 }
936
937 SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
938
939 if (!stream) {
940 return kIgnoreError_PdfResult;
941 }
942
943 if (CheckRecursiveRendering::IsInRendering(skobj)) {
944 // Oops, corrupt PDF!
945 return kIgnoreError_PdfResult;
946 }
947 CheckRecursiveRendering checkRecursion(skobj);
948
949
950 PdfOp_q(pdfContext, canvas, NULL);
951
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000952
953 if (skobj->Resources(pdfContext->fPdfDoc)) {
954 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
955 }
956
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000957 // TODO(edisonn): MediaBox can be inherited!!!!
958 SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc);
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000959 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000960 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
961 doGroup_before(pdfContext, canvas, bbox, tgroup, true);
962 } else {
963 canvas->save();
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000964 }
965
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000966
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000967 SkPdfNativeTokenizer* tokenizer =
968 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
969 if (tokenizer != NULL) {
970 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
971 looper.loop();
972 delete tokenizer;
973 }
974
975 // TODO(edisonn): should we restore the variable stack at the same state?
976 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
977 canvas->restore();
978 PdfOp_Q(pdfContext, canvas, NULL);
979 return kPartial_PdfResult;
980}
981
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000982PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
983 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
984 canvas->save();
985 return kOK_PdfResult;
986}
987
988PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
989 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
990 pdfContext->fStateStack.pop();
991 canvas->restore();
992 return kOK_PdfResult;
993}
994
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000995static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000996 double array[6];
997 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000998 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000999 pdfContext->fObjectStack.pop();
1000 }
1001
1002 // a b
1003 // c d
1004 // e f
1005
1006 // 0 1
1007 // 2 3
1008 // 4 5
1009
1010 // sx ky
1011 // kx sy
1012 // tx ty
1013 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1014
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001015 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001016
1017#ifdef PDF_TRACE
1018 printf("cm ");
1019 for (int i = 0 ; i < 6 ; i++) {
1020 printf("%f ", array[i]);
1021 }
1022 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001023 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001024#endif
1025
1026 return kOK_PdfResult;
1027}
1028
1029//leading TL Set the text leading, Tl
1030//, to leading, which is a number expressed in unscaled text
1031//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001032static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001033 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001034
1035 pdfContext->fGraphicsState.fTextLeading = ty;
1036
1037 return kOK_PdfResult;
1038}
1039
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001040static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001041#ifdef PDF_TRACE
1042 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
1043#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +00001044 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001045 SkPdfObject* obj = pdfContext->fObjectStack.top();
1046 obj = obj;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001047 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001048
edisonn@google.come57c62d2013-08-07 18:04:15 +00001049 double array[6] = {1, 0, 0, 1, tx, -ty};
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001050 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1051
1052 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1053 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
1054
1055 return kPartial_PdfResult;
1056}
1057
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001058static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001059 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1060 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001061
edisonn@google.com571c70b2013-07-10 17:09:50 +00001062 // TODO(edisonn): Create factory methods or constructors so native is hidden
1063 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001064 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001065
1066 PdfOp_TL(pdfContext, canvas, looper);
1067
edisonn@google.com571c70b2013-07-10 17:09:50 +00001068 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001069 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001070
edisonn@google.com571c70b2013-07-10 17:09:50 +00001071 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001072 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001073
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001074 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
1075
1076 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
1077 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001078
1079 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001080}
1081
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001082static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001083 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1084 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1085 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1086 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1087 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1088 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001089
1090 double array[6];
1091 array[0] = a;
1092 array[1] = b;
1093 array[2] = c;
1094 array[3] = d;
1095 array[4] = e;
1096 array[5] = f;
1097
1098 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001099 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.come57c62d2013-08-07 18:04:15 +00001100 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001101
1102 // TODO(edisonn): Text positioning.
1103 pdfContext->fGraphicsState.fMatrixTm = matrix;
1104 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
1105
1106 return kPartial_PdfResult;
1107}
1108
1109//— T* Move to the start of the next line. This operator has the same effect as the code
1110//0 Tl Td
1111//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001112static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001113 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
1114 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001115
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001116 pdfContext->fObjectStack.push(zero);
1117 pdfContext->fObjectStack.push(tl);
1118
1119 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
1120
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001121 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001122}
1123
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001124static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001125 if (pdfContext->fGraphicsState.fPathClosed) {
1126 pdfContext->fGraphicsState.fPath.reset();
1127 pdfContext->fGraphicsState.fPathClosed = false;
1128 }
1129
edisonn@google.com571c70b2013-07-10 17:09:50 +00001130 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1131 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001132
1133 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1134 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1135
1136 return kOK_PdfResult;
1137}
1138
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001139static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001140 if (pdfContext->fGraphicsState.fPathClosed) {
1141 pdfContext->fGraphicsState.fPath.reset();
1142 pdfContext->fGraphicsState.fPathClosed = false;
1143 }
1144
edisonn@google.com571c70b2013-07-10 17:09:50 +00001145 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1146 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001147
1148 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1149 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1150
1151 return kOK_PdfResult;
1152}
1153
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001154static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001155 if (pdfContext->fGraphicsState.fPathClosed) {
1156 pdfContext->fGraphicsState.fPath.reset();
1157 pdfContext->fGraphicsState.fPathClosed = false;
1158 }
1159
edisonn@google.com571c70b2013-07-10 17:09:50 +00001160 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1161 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1162 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1163 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1164 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1165 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001166
1167 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1168 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1169 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1170
1171 pdfContext->fGraphicsState.fCurPosX = x3;
1172 pdfContext->fGraphicsState.fCurPosY = y3;
1173
1174 return kOK_PdfResult;
1175}
1176
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001177static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001178 if (pdfContext->fGraphicsState.fPathClosed) {
1179 pdfContext->fGraphicsState.fPath.reset();
1180 pdfContext->fGraphicsState.fPathClosed = false;
1181 }
1182
edisonn@google.com571c70b2013-07-10 17:09:50 +00001183 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1184 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1185 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1186 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001187 double y1 = pdfContext->fGraphicsState.fCurPosY;
1188 double x1 = pdfContext->fGraphicsState.fCurPosX;
1189
1190 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1191 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1192 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1193
1194 pdfContext->fGraphicsState.fCurPosX = x3;
1195 pdfContext->fGraphicsState.fCurPosY = y3;
1196
1197 return kOK_PdfResult;
1198}
1199
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001200static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001201 if (pdfContext->fGraphicsState.fPathClosed) {
1202 pdfContext->fGraphicsState.fPath.reset();
1203 pdfContext->fGraphicsState.fPathClosed = false;
1204 }
1205
edisonn@google.com571c70b2013-07-10 17:09:50 +00001206 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1207 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001208 double y2 = pdfContext->fGraphicsState.fCurPosY;
1209 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001210 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1211 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001212
1213 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1214 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1215 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1216
1217 pdfContext->fGraphicsState.fCurPosX = x3;
1218 pdfContext->fGraphicsState.fCurPosY = y3;
1219
1220 return kOK_PdfResult;
1221}
1222
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001223static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001224 if (pdfContext->fGraphicsState.fPathClosed) {
1225 pdfContext->fGraphicsState.fPath.reset();
1226 pdfContext->fGraphicsState.fPathClosed = false;
1227 }
1228
edisonn@google.com571c70b2013-07-10 17:09:50 +00001229 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1230 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1231 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1232 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001233
1234 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1235 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1236
1237 pdfContext->fGraphicsState.fCurPosX = x;
1238 pdfContext->fGraphicsState.fCurPosY = y + height;
1239
1240 return kOK_PdfResult;
1241}
1242
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001243static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001244 pdfContext->fGraphicsState.fPath.close();
1245 return kOK_PdfResult;
1246}
1247
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001248static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001249 SkPath path = pdfContext->fGraphicsState.fPath;
1250
1251 if (close) {
1252 path.close();
1253 }
1254
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001255 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001256
1257 SkPaint paint;
1258
1259 SkPoint line[2];
1260 if (fill && !stroke && path.isLine(line)) {
1261 paint.setStyle(SkPaint::kStroke_Style);
1262
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001263 // TODO(edisonn): implement this with patterns
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001264 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1265 paint.setStrokeWidth(SkDoubleToScalar(0));
1266
1267 canvas->drawPath(path, paint);
1268 } else {
1269 if (fill) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001270 if (strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0 &&
1271 pdfContext->fGraphicsState.fNonStroking.fPattern != NULL) {
1272
1273 // TODO(edisonn): we can use a shader here, like imageshader to draw fast. ultimately,
1274 // if this is not possible, and we are in rasper mode, and the cells don't intersect, we could even have multiple cpus.
1275
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001276 PdfOp_q(pdfContext, canvas, NULL);
1277
1278 if (evenOdd) {
1279 path.setFillType(SkPath::kEvenOdd_FillType);
1280 }
1281 canvas->clipPath(path);
1282
1283 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(pdfContext->fGraphicsState.fNonStroking.fPattern) != kNone_SkPdfObjectType) {
1284 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)pdfContext->fGraphicsState.fNonStroking.fPattern;
1285
1286 // TODO(edisonn): constants
1287 // TODO(edisonn): colored
1288 if (pattern->PaintType(pdfContext->fPdfDoc) == 1) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001289 // TODO(edisonn): don't use abs, iterate as asked, if the cells intersect
1290 // it will change the result iterating in reverse
1291 int xStep = abs((int)pattern->XStep(pdfContext->fPdfDoc));
1292 int yStep = abs((int)pattern->YStep(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001293
1294 SkRect bounds = path.getBounds();
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001295
1296 // TODO(edisonn): xstep and ystep can be negative, and we need to iterate in reverse
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001297 // TODO(edisonn): don't do that!
1298 bounds.sort();
1299
1300 SkScalar x;
1301 SkScalar y;
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001302
1303 y = bounds.top();
1304 int totalx = 0;
1305 int totaly = 0;
1306 while (y < bounds.bottom()) {
1307 x = bounds.left();
1308 totalx = 0;
1309
1310 while (x < bounds.right()) {
1311 doXObject(pdfContext, canvas, pattern);
1312
edisonn@google.com0f901902013-08-07 11:56:16 +00001313 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(xStep), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001314 totalx += xStep;
1315 x += SkIntToScalar(xStep);
1316 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001317 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(-totalx), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001318
edisonn@google.com0f901902013-08-07 11:56:16 +00001319 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(-yStep));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001320 totaly += yStep;
1321 y += SkIntToScalar(yStep);
1322 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001323 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(totaly));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001324 }
1325 }
1326
1327 // apply matrix
1328 // get xstep, y step, bbox ... for cliping, and bos of the path
1329
1330 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001331 } else {
1332 paint.setStyle(SkPaint::kFill_Style);
1333 if (evenOdd) {
1334 path.setFillType(SkPath::kEvenOdd_FillType);
1335 }
1336
1337 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1338
1339 canvas->drawPath(path, paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001340 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001341 }
1342
1343 if (stroke) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001344 if (false && strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0) {
1345 // TODO(edisonn): implement Pattern for strokes
1346 paint.setStyle(SkPaint::kStroke_Style);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001347
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001348 paint.setColor(SK_ColorGREEN);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001349
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001350 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1351 canvas->drawPath(path, paint);
1352 } else {
1353 paint.setStyle(SkPaint::kStroke_Style);
1354
1355 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1356
1357 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1358 canvas->drawPath(path, paint);
1359 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001360 }
1361 }
1362
1363 pdfContext->fGraphicsState.fPath.reset();
1364 // todo zoom ... other stuff ?
1365
1366 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1367#ifndef PDF_DEBUG_NO_CLIPING
1368 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1369#endif
1370 }
1371
1372 //pdfContext->fGraphicsState.fClipPath.reset();
1373 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1374
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001375 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001376
1377}
1378
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001379static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001380 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1381}
1382
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001383static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001384 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1385}
1386
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001387static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001388 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1389}
1390
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001391static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001392 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1393}
1394
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001395static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001396 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1397}
1398
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001399static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001400 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1401}
1402
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001403static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001404 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1405}
1406
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001407static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001408 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1409}
1410
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001411static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001412 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1413}
1414
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001415static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001416 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001417 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1418#ifndef PDF_DEBUG_NO_CLIPING
1419 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1420#endif
1421 }
1422
1423 //pdfContext->fGraphicsState.fClipPath.reset();
1424 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1425
1426 pdfContext->fGraphicsState.fPathClosed = true;
1427
1428 return kOK_PdfResult;
1429}
1430
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001431static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001432 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.come57c62d2013-08-07 18:04:15 +00001433 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
1434 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
1435 pdfContext->fGraphicsState.fMatrixTm = matrix;
1436 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001437
1438 return kPartial_PdfResult;
1439}
1440
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001441static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001442 if (!pdfContext->fGraphicsState.fTextBlock) {
1443 return kIgnoreError_PdfResult;
1444 }
1445 // 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 +00001446 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001447}
1448
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001449PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001450#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001451 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001452#endif
1453
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001454 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1455 // TODO(edisonn): try to recover and draw it any way?
1456 return kIgnoreError_PdfResult;
1457 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001458
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001459 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1460 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1461 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1462 // TODO(edisonn): try to recover and draw it any way?
1463 return kIgnoreError_PdfResult;
1464 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001465
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001466 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1467
1468 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1469
1470 if (skfont) {
1471 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001472 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001473 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001474 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001475}
1476
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001477//font size Tf Set the text font, Tf
1478//, to font and the text font size, Tfs, to size. font is the name of a
1479//font resource in the Fontsubdictionary of the current resource dictionary; size is
1480//a number representing a scale factor. There is no initial value for either font or
1481//size; they must be specified explicitly using Tf before any text is shown.
1482static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1483 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1484 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1485 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1486}
1487
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001488static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001489 if (!pdfContext->fGraphicsState.fTextBlock) {
1490 // TODO(edisonn): try to recover and draw it any way?
1491 return kIgnoreError_PdfResult;
1492 }
1493
1494 PdfResult ret = DrawText(pdfContext,
1495 pdfContext->fObjectStack.top(),
1496 canvas);
1497 pdfContext->fObjectStack.pop();
1498
1499 return ret;
1500}
1501
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001502static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001503 if (!pdfContext->fGraphicsState.fTextBlock) {
1504 // TODO(edisonn): try to recover and draw it any way?
1505 return kIgnoreError_PdfResult;
1506 }
1507
1508 PdfOp_T_star(pdfContext, canvas, looper);
1509 // Do not pop, and push, just transfer the param to Tj
1510 return PdfOp_Tj(pdfContext, canvas, looper);
1511}
1512
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001513static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001514 if (!pdfContext->fGraphicsState.fTextBlock) {
1515 // TODO(edisonn): try to recover and draw it any way?
1516 return kIgnoreError_PdfResult;
1517 }
1518
1519 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1520 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1521 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1522
1523 pdfContext->fObjectStack.push(aw);
1524 PdfOp_Tw(pdfContext, canvas, looper);
1525
1526 pdfContext->fObjectStack.push(ac);
1527 PdfOp_Tc(pdfContext, canvas, looper);
1528
1529 pdfContext->fObjectStack.push(str);
1530 PdfOp_quote(pdfContext, canvas, looper);
1531
1532 return kPartial_PdfResult;
1533}
1534
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001535static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001536 if (!pdfContext->fGraphicsState.fTextBlock) {
1537 // TODO(edisonn): try to recover and draw it any way?
1538 return kIgnoreError_PdfResult;
1539 }
1540
edisonn@google.com571c70b2013-07-10 17:09:50 +00001541 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001542 pdfContext->fObjectStack.pop();
1543
edisonn@google.com571c70b2013-07-10 17:09:50 +00001544 if (!array->isArray()) {
1545 return kIgnoreError_PdfResult;
1546 }
1547
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001548 for( int i=0; i<static_cast<int>(array->size()); i++ )
1549 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001550 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001551 SkPdfObject* obj = (*array)[i];
1552 DrawText(pdfContext,
1553 obj,
1554 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001555 } else if ((*array)[i]->isNumber()) {
1556 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001557 SkMatrix matrix;
1558 matrix.setAll(SkDoubleToScalar(1),
1559 SkDoubleToScalar(0),
1560 // TODO(edisonn): use writing mode, vertical/horizontal.
1561 SkDoubleToScalar(-dx), // amount is substracted!!!
1562 SkDoubleToScalar(0),
1563 SkDoubleToScalar(1),
1564 SkDoubleToScalar(0),
1565 SkDoubleToScalar(0),
1566 SkDoubleToScalar(0),
1567 SkDoubleToScalar(1));
1568
1569 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1570 }
1571 }
1572 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1573}
1574
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001575static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com4f898b72013-08-07 21:11:57 +00001576 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1577
1578 //Next, get the ColorSpace Dictionary from the Resource Dictionary:
1579 SkPdfDictionary* colorSpaceResource = pdfContext->fGraphicsState.fResources->ColorSpace(pdfContext->fPdfDoc);
1580
edisonn@google.com53766102013-08-08 12:54:01 +00001581 SkPdfObject* colorSpace = colorSpaceResource ? pdfContext->fPdfDoc->resolveReference(colorSpaceResource->get(name)) : name;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001582
1583 if (colorSpace == NULL) {
1584 colorOperator->fColorSpace = name->strRef();
1585 } else {
1586#ifdef PDF_TRACE
1587 printf("CS = %s\n", colorSpace->toString(0, 0).c_str());
1588#endif // PDF_TRACE
1589 if (colorSpace->isName()) {
1590 colorOperator->fColorSpace = colorSpace->strRef();
1591 } else if (colorSpace->isArray()) {
1592 int cnt = colorSpace->size();
1593 if (cnt == 0) {
1594 return kIgnoreError_PdfResult;
1595 }
1596 SkPdfObject* type = colorSpace->objAtAIndex(0);
1597 type = pdfContext->fPdfDoc->resolveReference(type);
1598
1599 if (type->isName("ICCBased")) {
1600 if (cnt != 2) {
1601 return kIgnoreError_PdfResult;
1602 }
1603 SkPdfObject* prop = colorSpace->objAtAIndex(1);
1604 prop = pdfContext->fPdfDoc->resolveReference(prop);
1605#ifdef PDF_TRACE
1606 printf("ICCBased prop = %s\n", prop->toString(0, 0).c_str());
1607#endif // PDF_TRACE
1608 // TODO(edisonn): hack
1609 if (prop && prop->isDictionary() && prop->get("N") && prop->get("N")->isInteger() && prop->get("N")->intValue() == 3) {
1610 colorOperator->setColorSpace(&strings_DeviceRGB);
1611 return kPartial_PdfResult;
1612 }
1613 return kNYI_PdfResult;
1614 }
1615 }
1616 }
1617
1618 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001619}
1620
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001621static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001622 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1623}
1624
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001625static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001626 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1627}
1628
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001629static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001630 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001631// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001632
1633 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1634
1635 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001636 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001637 doubles = false;
1638 }
1639
1640#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001641 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001642#endif
1643
1644 for (int i = n - 1; i >= 0 ; i--) {
1645 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001646 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1647// } else {
1648// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001649 }
1650 }
1651
1652 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001653 // TODO(edisonn): do possible field values to enum at parsing time!
1654 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001655 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001656 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001657 }
1658 return kPartial_PdfResult;
1659}
1660
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001661static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001662 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1663}
1664
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001665static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001666 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1667}
1668
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001669static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001670 if (pdfContext->fObjectStack.top()->isName()) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001671 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1672
1673 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001674 SkPdfDictionary* patternResources = pdfContext->fGraphicsState.fResources->Pattern(pdfContext->fPdfDoc);
edisonn@google.com276fed92013-08-01 21:20:47 +00001675
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001676 if (patternResources == NULL) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001677#ifdef PDF_TRACE
1678 printf("ExtGState is NULL!\n");
1679#endif
1680 return kIgnoreError_PdfResult;
1681 }
1682
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001683 colorOperator->setPatternColorSpace(pdfContext->fPdfDoc->resolveReference(patternResources->get(name)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001684 }
1685
1686 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1687 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1688
1689 return kPartial_PdfResult;
1690}
1691
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001692static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001693 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1694}
1695
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001696static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001697 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1698}
1699
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001700static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001701 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001702 return kNYI_PdfResult;
1703}
1704
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001705static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001706 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1707}
1708
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001709static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001710 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1711}
1712
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001713static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001714 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1715 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1716 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001717
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001718 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001719 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001720 return kOK_PdfResult;
1721}
1722
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001723static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001724 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1725}
1726
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001727static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001728 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1729}
1730
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001731static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001732 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001733 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1734 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1735 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1736 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001737
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001738 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001739 // TODO(edisonn): Set color.
1740 return kNYI_PdfResult;
1741}
1742
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001743static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001744 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1745}
1746
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001747static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001748 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1749}
1750
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001751static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001752 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1753 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1754
1755 return kOK_PdfResult;
1756}
1757
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001758static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001759 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1760
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001761 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1762 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1763
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001764 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001765}
1766
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001767static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001768 *looper = new PdfCompatibilitySectionLooper();
1769 return kOK_PdfResult;
1770}
1771
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001772static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001773#ifdef ASSERT_BAD_PDF_OPS
1774 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1775 // have the assert when testing good pdfs.
1776#endif
1777 return kIgnoreError_PdfResult;
1778}
1779
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001780static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001781 *looper = new PdfInlineImageLooper();
1782 return kOK_PdfResult;
1783}
1784
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001785static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001786#ifdef ASSERT_BAD_PDF_OPS
1787 SkASSERT(false); // must be processed in inline image looper, but let's
1788 // have the assert when testing good pdfs.
1789#endif
1790 return kIgnoreError_PdfResult;
1791}
1792
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001793static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001794#ifdef ASSERT_BAD_PDF_OPS
1795 SkASSERT(false); // must be processed in inline image looper, but let's
1796 // have the assert when testing good pdfs.
1797#endif
1798 return kIgnoreError_PdfResult;
1799}
1800
edisonn@google.com24cdf132013-07-30 16:06:12 +00001801
1802// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1803PdfResult skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1804 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1805 return kOK_PdfResult;
1806}
1807
1808PdfResult skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1809 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1810 return kOK_PdfResult;
1811}
1812
1813PdfResult skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001814 pdfContext->fGraphicsState.fLineWidth = lineWidth;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001815 return kOK_PdfResult;
1816}
1817
1818PdfResult skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1819 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1820 return kOK_PdfResult;
1821}
1822
1823PdfResult skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1824 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1825 return kOK_PdfResult;
1826}
1827
1828PdfResult skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1829 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1830 return kOK_PdfResult;
1831}
1832
1833// TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure
1834/*
18351) [ ] 0 No dash; solid, unbroken lines
18362) [3] 0 3 units on, 3 units off, …
18373) [2] 1 1 on, 2 off, 2 on, 2 off, …
18384) [2 1] 0 2 on, 1 off, 2 on, 1 off, …
18395) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, …
18406) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, …
1841 */
1842
1843PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* intervals, SkPdfObject* phase) {
1844 int cnt = intervals->size();
1845 if (cnt >= 256) {
1846 // TODO(edisonn): report error/warning, unsuported;
1847 // TODO(edisonn): alloc memory
1848 return kIgnoreError_PdfResult;
1849 }
1850 for (int i = 0; i < cnt; i++) {
1851 if (!intervals->objAtAIndex(i)->isNumber()) {
1852 // TODO(edisonn): report error/warning
1853 return kIgnoreError_PdfResult;
1854 }
1855 }
1856
edisonn@google.com24cdf132013-07-30 16:06:12 +00001857 double total = 0;
1858 for (int i = 0 ; i < cnt; i++) {
1859 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1860 total += pdfContext->fGraphicsState.fDashArray[i];
1861 }
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001862 if (cnt & 1) {
1863 if (cnt == 1) {
1864 pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0];
1865 cnt++;
1866 } else {
1867 // TODO(edisonn): report error/warning
1868 return kNYI_PdfResult;
1869 }
1870 }
1871 pdfContext->fGraphicsState.fDashArrayLength = cnt;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001872 pdfContext->fGraphicsState.fDashPhase = phase->scalarValue();
1873 if (pdfContext->fGraphicsState.fDashPhase == 0) {
1874 // other rules, changes?
1875 pdfContext->fGraphicsState.fDashPhase = total;
1876 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001877
1878 return kOK_PdfResult;
1879}
1880
edisonn@google.com24cdf132013-07-30 16:06:12 +00001881PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1882 // TODO(edisonn): verify input
1883 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1884 // TODO(edisonn): report error/warning
1885 return kIgnoreError_PdfResult;
1886 }
1887 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1));
1888}
1889
1890void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1891 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1892 // TODO(edisonn): report error/warning
1893 return;
1894 }
1895 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1896}
1897
1898
1899//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
1900static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1901 double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1902 return skpdfGraphicsStateApplyLW(pdfContext, lw);
1903}
1904
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001905//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 +00001906static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001907 int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1908 return skpdfGraphicsStateApplyLC(pdfContext, lc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001909}
1910
1911//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 +00001912static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001913 double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1914 return skpdfGraphicsStateApplyLJ(pdfContext, lj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001915}
1916
1917//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001918static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001919 double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1920 return skpdfGraphicsStateApplyML(pdfContext, ml);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001921}
1922
1923//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1924//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001925static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001926 SkPdfObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1927 SkPdfObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001928
edisonn@google.com24cdf132013-07-30 16:06:12 +00001929 if (!array->isArray()) {
1930 return kIgnoreError_PdfResult;
1931 }
1932
1933 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001934}
1935
1936//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 +00001937static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001938 pdfContext->fObjectStack.pop();
1939
1940 return kNYI_PdfResult;
1941}
1942
1943//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1944//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1945//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001946static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001947 pdfContext->fObjectStack.pop();
1948
1949 return kNYI_PdfResult;
1950}
1951
edisonn@google.come878e722013-07-29 19:10:58 +00001952SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1953
1954class InitBlendModes {
1955public:
1956 InitBlendModes() {
1957 // TODO(edisonn): use the python code generator?
1958 // TABLE 7.2 Standard separable blend modes
1959 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1960 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1961 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1962 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1963 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1964 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1965 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1966 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1967 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1968 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1969 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1970 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1971
1972 // TABLE 7.3 Standard nonseparable blend modes
1973 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1974 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1975 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1976 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1977 }
1978};
1979
1980InitBlendModes _gDummyInniter;
1981
1982SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1983 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1984 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1985 return mode;
1986 }
1987
1988 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1989}
1990
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001991void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001992 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1993 if (mode <= SkXfermode::kLastMode) {
1994 pdfContext->fGraphicsState.fBlendModesLength = 1;
1995 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1996 } else {
1997 // TODO(edisonn): report unknown blend mode
1998 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001999}
2000
2001void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00002002 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
2003 // TODO(edisonn): report error/warning
2004 return;
2005 }
2006 SkXfermode::Mode modes[256];
2007 int cnt = blendModes->size();
2008 for (int i = 0; i < cnt; i++) {
2009 SkPdfObject* name = blendModes->objAtAIndex(i);
2010 if (!name->isName()) {
2011 // TODO(edisonn): report error/warning
2012 return;
2013 }
2014 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
2015 if (mode > SkXfermode::kLastMode) {
2016 // TODO(edisonn): report error/warning
2017 return;
2018 }
2019 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002020
edisonn@google.come878e722013-07-29 19:10:58 +00002021 pdfContext->fGraphicsState.fBlendModesLength = cnt;
2022 for (int i = 0; i < cnt; i++) {
2023 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
2024 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002025}
2026
2027void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
2028 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00002029 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002030 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00002031 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
2032 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
2033 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
2034 } else {
2035 // TODO (edisonn): report error/warning
2036 }
2037}
2038
2039void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002040 if (sMask == "None") {
2041 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002042 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002043 return;
2044 }
2045
edisonn@google.come878e722013-07-29 19:10:58 +00002046 //Next, get the ExtGState Dictionary from the Resource Dictionary:
2047 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
2048
2049 if (extGStateDictionary == NULL) {
2050#ifdef PDF_TRACE
2051 printf("ExtGState is NULL!\n");
2052#endif
2053 // TODO (edisonn): report error/warning
2054 return;
2055 }
2056
2057 SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
2058 if (!obj || !obj->isDictionary()) {
2059 // TODO (edisonn): report error/warning
2060 return;
2061 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002062
2063 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002064 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002065
edisonn@google.come878e722013-07-29 19:10:58 +00002066 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002067}
2068
2069void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
2070 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
2071}
2072
2073
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002074//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
2075//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 +00002076static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002077 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002078
2079#ifdef PDF_TRACE
2080 std::string str;
2081#endif
2082
2083 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00002084 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002085
2086 if (extGStateDictionary == NULL) {
2087#ifdef PDF_TRACE
2088 printf("ExtGState is NULL!\n");
2089#endif
2090 return kIgnoreError_PdfResult;
2091 }
2092
edisonn@google.com571c70b2013-07-10 17:09:50 +00002093 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002094
edisonn@google.com571c70b2013-07-10 17:09:50 +00002095 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
2096 return kIgnoreError_PdfResult;
2097 }
2098 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002099
2100 // TODO(edisonn): now load all those properties in graphic state.
2101 if (gs == NULL) {
2102 return kIgnoreError_PdfResult;
2103 }
2104
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002105 if (gs->has_LW()) {
2106 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
2107 }
2108
2109 if (gs->has_LC()) {
2110 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
2111 }
2112
2113 if (gs->has_LJ()) {
2114 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
2115 }
2116
2117 if (gs->has_ML()) {
2118 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
2119 }
2120
2121 if (gs->has_D()) {
2122 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
2123 }
2124
2125 if (gs->has_Font()) {
2126 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
2127 }
2128
2129 if (gs->has_BM()) {
2130 if (gs->isBMAName(pdfContext->fPdfDoc)) {
2131 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
2132 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
2133 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
2134 } else {
2135 // TODO(edisonn): report/warn
2136 }
2137 }
2138
2139 if (gs->has_SMask()) {
2140 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
2141 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
2142 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
2143 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
2144 } else {
2145 // TODO(edisonn): report/warn
2146 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002147 }
2148
2149 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002150 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002151 }
2152
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002153 if (gs->has_CA()) {
2154 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
2155 }
2156
2157 if (gs->has_AIS()) {
2158 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002159 }
2160
edisonn@google.com9a43c182013-08-01 20:06:42 +00002161 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002162}
2163
2164//charSpace Tc Set the character spacing, Tc
2165//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
2166//Initial value: 0.
2167PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002168 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002169 pdfContext->fGraphicsState.fCharSpace = charSpace;
2170
2171 return kOK_PdfResult;
2172}
2173
2174//wordSpace Tw Set the word spacing, T
2175//w
2176//, to wordSpace, which is a number expressed in unscaled
2177//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
2178//value: 0.
2179PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002180 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002181 pdfContext->fGraphicsState.fWordSpace = wordSpace;
2182
2183 return kOK_PdfResult;
2184}
2185
2186//scale Tz Set the horizontal scaling, Th
2187//, to (scale ˜ 100). scale is a number specifying the
2188//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002189static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002190 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002191
2192 return kNYI_PdfResult;
2193}
2194
2195//render Tr Set the text rendering mode, T
2196//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002197static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002198 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002199
2200 return kNYI_PdfResult;
2201}
2202//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
2203//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002204static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002205 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002206
2207 return kNYI_PdfResult;
2208}
2209
2210//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002211static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002212 pdfContext->fObjectStack.pop();
2213 pdfContext->fObjectStack.pop();
2214
2215 return kNYI_PdfResult;
2216}
2217
2218//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002219static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002220 pdfContext->fObjectStack.pop();
2221 pdfContext->fObjectStack.pop();
2222 pdfContext->fObjectStack.pop();
2223 pdfContext->fObjectStack.pop();
2224 pdfContext->fObjectStack.pop();
2225 pdfContext->fObjectStack.pop();
2226
2227 return kNYI_PdfResult;
2228}
2229
2230//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002231static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002232 pdfContext->fObjectStack.pop();
2233
2234 return kNYI_PdfResult;
2235}
2236
2237//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002238static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002239 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002240
edisonn@google.com571c70b2013-07-10 17:09:50 +00002241 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002242
2243 if (xObject == NULL) {
2244#ifdef PDF_TRACE
2245 printf("XObject is NULL!\n");
2246#endif
2247 return kIgnoreError_PdfResult;
2248 }
2249
edisonn@google.com571c70b2013-07-10 17:09:50 +00002250 SkPdfObject* value = xObject->get(name);
2251 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002252
2253#ifdef PDF_TRACE
2254// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002255// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002256#endif
2257
edisonn@google.com571c70b2013-07-10 17:09:50 +00002258 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002259}
2260
2261//tag MP Designate a marked-content point. tag is a name object indicating the role or
2262//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002263static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002264 pdfContext->fObjectStack.pop();
2265
2266 return kNYI_PdfResult;
2267}
2268
2269//tag properties DP Designate a marked-content point with an associated property list. tag is a
2270//name object indicating the role or significance of the point; properties is
2271//either an inline dictionary containing the property list or a name object
2272//associated with it in the Properties subdictionary of the current resource
2273//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002274static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002275 pdfContext->fObjectStack.pop();
2276 pdfContext->fObjectStack.pop();
2277
2278 return kNYI_PdfResult;
2279}
2280
2281//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
2282//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002283static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002284 pdfContext->fObjectStack.pop();
2285
2286 return kNYI_PdfResult;
2287}
2288
2289//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
2290//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
2291//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 +00002292static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002293 pdfContext->fObjectStack.pop();
2294 pdfContext->fObjectStack.pop();
2295
2296 return kNYI_PdfResult;
2297}
2298
2299//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002300static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002301 return kNYI_PdfResult;
2302}
2303
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002304static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002305 static bool gInitialized = false;
2306 if (gInitialized) {
2307 return;
2308 }
2309
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002310 gPdfOps.set("q", PdfOp_q);
2311 gPdfOps.set("Q", PdfOp_Q);
2312 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002313
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002314 gPdfOps.set("TD", PdfOp_TD);
2315 gPdfOps.set("Td", PdfOp_Td);
2316 gPdfOps.set("Tm", PdfOp_Tm);
2317 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002318
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002319 gPdfOps.set("m", PdfOp_m);
2320 gPdfOps.set("l", PdfOp_l);
2321 gPdfOps.set("c", PdfOp_c);
2322 gPdfOps.set("v", PdfOp_v);
2323 gPdfOps.set("y", PdfOp_y);
2324 gPdfOps.set("h", PdfOp_h);
2325 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002326
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002327 gPdfOps.set("S", PdfOp_S);
2328 gPdfOps.set("s", PdfOp_s);
2329 gPdfOps.set("f", PdfOp_f);
2330 gPdfOps.set("F", PdfOp_F);
2331 gPdfOps.set("f*", PdfOp_f_star);
2332 gPdfOps.set("B", PdfOp_B);
2333 gPdfOps.set("B*", PdfOp_B_star);
2334 gPdfOps.set("b", PdfOp_b);
2335 gPdfOps.set("b*", PdfOp_b_star);
2336 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002337
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002338 gPdfOps.set("BT", PdfOp_BT);
2339 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002340
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002341 gPdfOps.set("Tj", PdfOp_Tj);
2342 gPdfOps.set("'", PdfOp_quote);
2343 gPdfOps.set("\"", PdfOp_doublequote);
2344 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002345
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002346 gPdfOps.set("CS", PdfOp_CS);
2347 gPdfOps.set("cs", PdfOp_cs);
2348 gPdfOps.set("SC", PdfOp_SC);
2349 gPdfOps.set("SCN", PdfOp_SCN);
2350 gPdfOps.set("sc", PdfOp_sc);
2351 gPdfOps.set("scn", PdfOp_scn);
2352 gPdfOps.set("G", PdfOp_G);
2353 gPdfOps.set("g", PdfOp_g);
2354 gPdfOps.set("RG", PdfOp_RG);
2355 gPdfOps.set("rg", PdfOp_rg);
2356 gPdfOps.set("K", PdfOp_K);
2357 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002358
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002359 gPdfOps.set("W", PdfOp_W);
2360 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002361
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002362 gPdfOps.set("BX", PdfOp_BX);
2363 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002364
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002365 gPdfOps.set("BI", PdfOp_BI);
2366 gPdfOps.set("ID", PdfOp_ID);
2367 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002368
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002369 gPdfOps.set("w", PdfOp_w);
2370 gPdfOps.set("J", PdfOp_J);
2371 gPdfOps.set("j", PdfOp_j);
2372 gPdfOps.set("M", PdfOp_M);
2373 gPdfOps.set("d", PdfOp_d);
2374 gPdfOps.set("ri", PdfOp_ri);
2375 gPdfOps.set("i", PdfOp_i);
2376 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002377
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002378 gPdfOps.set("Tc", PdfOp_Tc);
2379 gPdfOps.set("Tw", PdfOp_Tw);
2380 gPdfOps.set("Tz", PdfOp_Tz);
2381 gPdfOps.set("TL", PdfOp_TL);
2382 gPdfOps.set("Tf", PdfOp_Tf);
2383 gPdfOps.set("Tr", PdfOp_Tr);
2384 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002385
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002386 gPdfOps.set("d0", PdfOp_d0);
2387 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002388
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002389 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002390
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002391 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002392
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002393 gPdfOps.set("MP", PdfOp_MP);
2394 gPdfOps.set("DP", PdfOp_DP);
2395 gPdfOps.set("BMC", PdfOp_BMC);
2396 gPdfOps.set("BDC", PdfOp_BDC);
2397 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002398
2399 gInitialized = true;
2400}
2401
2402class InitPdfOps {
2403public:
2404 InitPdfOps() {
2405 initPdfOperatorRenderes();
2406 }
2407};
2408
2409InitPdfOps gInitPdfOps;
2410
2411void reportPdfRenderStats() {
2412 std::map<std::string, int>::iterator iter;
2413
2414 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002415 SkTDict<int>::Iter iter(gRenderStats[i]);
2416 const char* key;
2417 int value = 0;
2418 while ((key = iter.next(&value)) != NULL) {
2419 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002420 }
2421 }
2422}
2423
2424PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002425 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002426 {
2427 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002428 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002429 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002430 // caller, main work is done by pdfOperatorRenderer(...)
2431 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002432 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
2433
2434 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002435 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2436 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002437
2438 if (childLooper) {
2439 childLooper->setUp(this);
2440 childLooper->loop();
2441 delete childLooper;
2442 }
2443 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002444 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002445 gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2446 gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002447 }
2448 }
2449 else if (token.fType == kObject_TokenType)
2450 {
2451 fPdfContext->fObjectStack.push( token.fObject );
2452 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002453 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002454 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002455 return kIgnoreError_PdfResult;
2456 }
2457 return kOK_PdfResult;
2458}
2459
2460void PdfMainLooper::loop() {
2461 PdfToken token;
2462 while (readToken(fTokenizer, &token)) {
2463 consumeToken(token);
2464 }
2465}
2466
2467PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002468 SkASSERT(false);
2469 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002470}
2471
2472void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002473 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002474}
2475
2476PdfResult PdfInlineImageLooper::done() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002477 return kNYI_PdfResult;
2478}
2479
2480PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2481 return fParent->consumeToken(token);
2482}
2483
2484void PdfCompatibilitySectionLooper::loop() {
2485 // TODO(edisonn): save stacks position, or create a new stack?
2486 // TODO(edisonn): what happens if we pop out more variables then when we started?
2487 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2488 // pop-ing too much will not affect outside the section.
2489 PdfToken token;
2490 while (readToken(fTokenizer, &token)) {
2491 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2492 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2493 looper->setUp(this);
2494 looper->loop();
2495 delete looper;
2496 } else {
2497 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2498 fParent->consumeToken(token);
2499 }
2500 }
2501 // TODO(edisonn): restore stack.
2502}
2503
2504// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2505// TODO(edisonn): Add API for Forms viewing and editing
2506// e.g. SkBitmap getPage(int page);
2507// int formsCount();
2508// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2509// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2510// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2511// resources needed, so the preview is fast.
2512// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2513// references automatically.
2514
edisonn@google.com222382b2013-07-10 22:33:10 +00002515PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002516
edisonn@google.com444e25a2013-07-11 15:20:50 +00002517bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002518 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002519 return false;
2520 }
2521
edisonn@google.com222382b2013-07-10 22:33:10 +00002522 if (page < 0 || page >= pages()) {
2523 return false;
2524 }
2525
edisonn@google.com222382b2013-07-10 22:33:10 +00002526 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002527
edisonn@google.com222382b2013-07-10 22:33:10 +00002528 pdfContext.fOriginalMatrix = SkMatrix::I();
2529 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2530
2531 gPdfContext = &pdfContext;
2532
2533 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002534 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002535 SkScalar w = dst.width();
2536 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002537
edisonn@google.com444e25a2013-07-11 15:20:50 +00002538 SkScalar wp = fPdfDoc->MediaBox(page).width();
2539 SkScalar hp = fPdfDoc->MediaBox(page).height();
2540
2541 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 +00002542// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2543
2544 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2545 // TODO(edisonn): add flagg for no clipping.
2546 // Use larger image to make sure we do not draw anything outside of page
2547 // could be used in tests.
2548
2549#ifdef PDF_DEBUG_3X
2550 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)};
2551#else
2552 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2553#endif
2554 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2555 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2556
2557 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2558 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2559
2560 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2561 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2562
2563 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2564 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2565
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002566 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
edisonn@google.com0f901902013-08-07 11:56:16 +00002567 pdfContext.fGraphicsState.fContentStreamMatrix = pdfContext.fOriginalMatrix;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002568 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2569 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002570
edisonn@google.com222382b2013-07-10 22:33:10 +00002571#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002572 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002573#endif
2574
edisonn@google.com444e25a2013-07-11 15:20:50 +00002575 canvas->setMatrix(pdfContext.fOriginalMatrix);
2576
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002577 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2578
2579 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002580// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002581// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002582// canvas->drawRect(rect, paint);
2583
edisonn@google.com222382b2013-07-10 22:33:10 +00002584
2585 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002586 return true;
2587}
edisonn@google.com222382b2013-07-10 22:33:10 +00002588
2589bool SkPdfRenderer::load(const SkString inputFileName) {
2590 unload();
2591
2592 // TODO(edisonn): create static function that could return NULL if there are errors
2593 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002594 if (fPdfDoc->pages() == 0) {
2595 delete fPdfDoc;
2596 fPdfDoc = NULL;
2597 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002598
2599 return fPdfDoc != NULL;
2600}
2601
edisonn@google.com147adb12013-07-24 15:56:19 +00002602bool SkPdfRenderer::load(SkStream* stream) {
2603 unload();
2604
2605 // TODO(edisonn): create static function that could return NULL if there are errors
2606 fPdfDoc = new SkNativeParsedPDF(stream);
2607 if (fPdfDoc->pages() == 0) {
2608 delete fPdfDoc;
2609 fPdfDoc = NULL;
2610 }
2611
2612 return fPdfDoc != NULL;
2613}
2614
2615
edisonn@google.com222382b2013-07-10 22:33:10 +00002616int SkPdfRenderer::pages() const {
2617 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2618}
2619
2620void SkPdfRenderer::unload() {
2621 delete fPdfDoc;
2622 fPdfDoc = NULL;
2623}
2624
2625SkRect SkPdfRenderer::MediaBox(int page) const {
2626 SkASSERT(fPdfDoc);
2627 return fPdfDoc->MediaBox(page);
2628}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002629
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002630size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002631 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2632}
edisonn@google.com147adb12013-07-24 15:56:19 +00002633
2634bool SkPDFNativeRenderToBitmap(SkStream* stream,
2635 SkBitmap* output,
2636 int page,
2637 SkPdfContent content,
2638 double dpi) {
2639 SkASSERT(page >= 0);
2640 SkPdfRenderer renderer;
2641 renderer.load(stream);
2642 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2643 return false;
2644 }
2645
2646 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2647
2648 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2649 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2650
2651 rect = SkRect::MakeWH(width, height);
2652
2653 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2654
2655 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2656 SkCanvas canvas(device);
2657
2658 return renderer.renderPage(page, &canvas, rect);
2659}