blob: ef13c553052373ee498960d777cda1839e026fa1 [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.com3aa35552013-08-14 18:26:20 +000021#include "SkPdfGraphicsState.h"
edisonn@google.com15b11182013-07-11 14:43:15 +000022#include "SkPdfNativeTokenizer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000023
edisonn@google.com3aa35552013-08-14 18:26:20 +000024extern "C" SkPdfContext* gPdfContext;
edisonn@google.com15b11182013-07-11 14:43:15 +000025extern "C" SkBitmap* gDumpBitmap;
26extern "C" SkCanvas* gDumpCanvas;
27
edisonn@google.com131d4ee2013-06-26 17:48:12 +000028__SK_FORCE_IMAGE_DECODER_LINKING;
29
30// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
31// keep for each object pos in file
32// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
33
34// TODO(edisonn): security - validate all the user input, all pdf!
35
edisonn@google.com6e49c342013-06-27 20:03:43 +000036// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
37// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000038
edisonn@google.com3aac1f92013-07-02 22:42:53 +000039// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
40/*
41#ifdef PDF_TRACE
edisonn@google.com063d7072013-08-16 15:05:08 +000042 SkString str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000043 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000044 printf("Print Tf Resources: %s\n", str.c_str());
45#endif
46 */
47
edisonn@google.com131d4ee2013-06-26 17:48:12 +000048#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000049#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000050#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000051
edisonn@google.com131d4ee2013-06-26 17:48:12 +000052#include "SkPdfUtils.h"
53
54#include "SkPdfFont.h"
55
edisonn@google.com131d4ee2013-06-26 17:48:12 +000056/*
57 * TODO(edisonn):
58 * - all font types and all ppdf font features
59 * - word spacing
60 * - load font for baidu.pdf
61 * - load font for youtube.pdf
62 * - parser for pdf from the definition already available in pdfspec_autogen.py
63 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000064 * - 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 +000065 * - load gs/ especially smask and already known prop (skp) ... in progress
66 * - wrapper on classes for customizations? e.g.
67 * SkPdfPageObjectVanila - has only the basic loaders/getters
68 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
69 * need to find a nice object model for all this with constructors and factories
70 * - deal with inheritable automatically ?
71 * - deal with specific type in spec directly, add all dictionary types to known types
72*/
73
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000074NotOwnedString strings_DeviceRGB;
75NotOwnedString strings_DeviceCMYK;
edisonn@google.com222382b2013-07-10 22:33:10 +000076
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000077class StringsInit {
78public:
79 StringsInit() {
80 NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB");
81 NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK");
82 }
83};
84
85StringsInit gStringsInit;
edisonn@google.com222382b2013-07-10 22:33:10 +000086
87// TODO(edisonn): Document PdfTokenLooper and subclasses.
88class PdfTokenLooper {
89protected:
90 PdfTokenLooper* fParent;
91 SkPdfNativeTokenizer* fTokenizer;
edisonn@google.com3aa35552013-08-14 18:26:20 +000092 SkPdfContext* fPdfContext;
edisonn@google.com222382b2013-07-10 22:33:10 +000093 SkCanvas* fCanvas;
94
95public:
96 PdfTokenLooper(PdfTokenLooper* parent,
97 SkPdfNativeTokenizer* tokenizer,
edisonn@google.com3aa35552013-08-14 18:26:20 +000098 SkPdfContext* pdfContext,
edisonn@google.com222382b2013-07-10 22:33:10 +000099 SkCanvas* canvas)
100 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
101
102 virtual ~PdfTokenLooper() {}
103
edisonn@google.com3aa35552013-08-14 18:26:20 +0000104 virtual SkPdfResult consumeToken(PdfToken& token) = 0;
edisonn@google.com222382b2013-07-10 22:33:10 +0000105 virtual void loop() = 0;
106
107 void setUp(PdfTokenLooper* parent) {
108 fParent = parent;
109 fTokenizer = parent->fTokenizer;
110 fPdfContext = parent->fPdfContext;
111 fCanvas = parent->fCanvas;
112 }
edisonn@google.com78b38b12013-07-15 18:20:58 +0000113
114 SkPdfNativeTokenizer* tokenizer() { return fTokenizer; }
edisonn@google.com222382b2013-07-10 22:33:10 +0000115};
116
117class PdfMainLooper : public PdfTokenLooper {
118public:
119 PdfMainLooper(PdfTokenLooper* parent,
120 SkPdfNativeTokenizer* tokenizer,
edisonn@google.com3aa35552013-08-14 18:26:20 +0000121 SkPdfContext* pdfContext,
edisonn@google.com222382b2013-07-10 22:33:10 +0000122 SkCanvas* canvas)
123 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
124
edisonn@google.com3aa35552013-08-14 18:26:20 +0000125 virtual SkPdfResult consumeToken(PdfToken& token);
edisonn@google.com222382b2013-07-10 22:33:10 +0000126 virtual void loop();
127};
128
129class PdfInlineImageLooper : public PdfTokenLooper {
130public:
131 PdfInlineImageLooper()
132 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
133
edisonn@google.com3aa35552013-08-14 18:26:20 +0000134 virtual SkPdfResult consumeToken(PdfToken& token);
edisonn@google.com222382b2013-07-10 22:33:10 +0000135 virtual void loop();
edisonn@google.com3aa35552013-08-14 18:26:20 +0000136 SkPdfResult done();
edisonn@google.com222382b2013-07-10 22:33:10 +0000137};
138
139class PdfCompatibilitySectionLooper : public PdfTokenLooper {
140public:
141 PdfCompatibilitySectionLooper()
142 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
143
edisonn@google.com3aa35552013-08-14 18:26:20 +0000144 virtual SkPdfResult consumeToken(PdfToken& token);
edisonn@google.com222382b2013-07-10 22:33:10 +0000145 virtual void loop();
146};
147
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000148// Utilities
149static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
150 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
151
152 bitmap->allocPixels();
153 bitmap->eraseColor(color);
154}
155
156// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000157static int GetColorSpaceComponents(NotOwnedString& colorSpace) {
158 if (colorSpace.equals("DeviceCMYK")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000159 return 4;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000160 } else if (colorSpace.equals("DeviceGray") ||
161 colorSpace.equals("CalGray") ||
162 colorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000163 return 1;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000164 } else if (colorSpace.equals("DeviceRGB") ||
165 colorSpace.equals("CalRGB") ||
166 colorSpace.equals("Lab")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000167 return 3;
168 } else {
169 return 0;
170 }
171}
172
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000173SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000174 SkMatrix matrix;
175 matrix.setAll(SkDoubleToScalar(array[0]),
176 SkDoubleToScalar(array[2]),
177 SkDoubleToScalar(array[4]),
178 SkDoubleToScalar(array[1]),
179 SkDoubleToScalar(array[3]),
180 SkDoubleToScalar(array[5]),
181 SkDoubleToScalar(0),
182 SkDoubleToScalar(0),
183 SkDoubleToScalar(1));
184
185 return matrix;
186}
187
188SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
189 double array[6];
190
191 // TODO(edisonn): security issue, ret if size() != 6
192 for (int i = 0; i < 6; i++) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000193 const SkPdfNativeObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000194 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000195 return SkMatrix::I(); // TODO(edisonn): report issue
196 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000197 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000198 }
199
200 return SkMatrixFromPdfMatrix(array);
201}
202
edisonn@google.com222382b2013-07-10 22:33:10 +0000203
edisonn@google.com3aa35552013-08-14 18:26:20 +0000204extern "C" SkPdfNativeDoc* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000205SkBitmap* gDumpBitmap = NULL;
206SkCanvas* gDumpCanvas = NULL;
207char gLastKeyword[100] = "";
208int gLastOpKeyword = -1;
209char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
210int gReadOp = 0;
211
212
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000213#ifdef PDF_TRACE_DIFF_IN_PNG
214static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000215 return true;
216 if (*pdfOp == '\0') return false;
217
218 char markedPdfOp[100] = ",";
219 strcat(markedPdfOp, pdfOp);
220 strcat(markedPdfOp, ",");
221
222 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
223}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000224#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000225
edisonn@google.com222382b2013-07-10 22:33:10 +0000226
227
edisonn@google.com3aa35552013-08-14 18:26:20 +0000228// TODO(edisonn): Pass SkPdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000229static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000230 bool ret = fTokenizer->readToken(token);
231
232 gReadOp++;
edisonn@google.com0f901902013-08-07 11:56:16 +0000233 gLastOpKeyword++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000234#ifdef PDF_TRACE_DIFF_IN_PNG
235 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
236 // the numbar and name of last operation, so the file name will reflect op that changed.
edisonn@google.com0f901902013-08-07 11:56:16 +0000237 if (gLastKeyword[0] && hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000238 gDumpCanvas->flush();
239
240 SkBitmap bitmap;
241 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
242
243 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
244
245 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
246 SkCanvas canvas(device);
247
248 // draw context stuff here
249 SkPaint blueBorder;
250 blueBorder.setColor(SK_ColorBLUE);
251 blueBorder.setStyle(SkPaint::kStroke_Style);
252 blueBorder.setTextSize(SkDoubleToScalar(20));
253
254 SkString str;
255
256 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
257 if (clipStack) {
258 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
259 const SkClipStack::Element* elem;
260 double y = 0;
261 int total = 0;
edisonn@google.com91ce6982013-08-05 20:45:40 +0000262 while ((elem = iter.next()) != NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000263 total++;
264 y += 30;
265
266 switch (elem->getType()) {
267 case SkClipStack::Element::kRect_Type:
268 canvas.drawRect(elem->getRect(), blueBorder);
269 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
270 break;
271 case SkClipStack::Element::kPath_Type:
272 canvas.drawPath(elem->getPath(), blueBorder);
273 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
274 break;
275 case SkClipStack::Element::kEmpty_Type:
276 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
277 break;
278 default:
279 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
280 break;
281 }
282 }
283
284 y += 30;
285 str.printf("Number of clips in stack: %i", total);
286 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
287 }
288
289 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
290 SkPath clipPath;
291 if (clipRegion.getBoundaryPath(&clipPath)) {
292 SkPaint redBorder;
293 redBorder.setColor(SK_ColorRED);
294 redBorder.setStyle(SkPaint::kStroke_Style);
295 canvas.drawPath(clipPath, redBorder);
296 }
297
298 canvas.flush();
299
300 SkString out;
301
302 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
303 // ... and other properties, to be able to debug th code easily
304
305 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
306 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
307 }
edisonn@google.com0f901902013-08-07 11:56:16 +0000308
309 if (ret && token->fType == kKeyword_TokenType && token->fKeyword && token->fKeywordLength > 0 && token->fKeywordLength < 100) {
310 strncpy(gLastKeyword, token->fKeyword, token->fKeywordLength);
311 gLastKeyword[token->fKeywordLength] = '\0';
312 } else {
313 gLastKeyword[0] = '\0';
314 }
315
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000316#endif
317
318 return ret;
319}
320
321
322
edisonn@google.com3aa35552013-08-14 18:26:20 +0000323typedef SkPdfResult (*PdfOperatorRenderer)(SkPdfContext*, SkCanvas*, PdfTokenLooper**);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000324
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000325SkTDict<PdfOperatorRenderer> gPdfOps(100);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000326
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000327
328template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
329public:
330 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
331};
332
edisonn@google.com3aa35552013-08-14 18:26:20 +0000333SkTDictWithDefaultConstructor<int> gRenderStats[kCount_SkPdfResult];
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000334
edisonn@google.com3aa35552013-08-14 18:26:20 +0000335const char* gRenderStatsNames[kCount_SkPdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000336 "Success",
337 "Partially implemented",
338 "Not yet implemented",
339 "Ignore Error",
340 "Error",
341 "Unsupported/Unknown"
342};
343
edisonn@google.com3aa35552013-08-14 18:26:20 +0000344static SkPdfResult DrawText(SkPdfContext* pdfContext,
345 const SkPdfNativeObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000346 SkCanvas* canvas)
347{
348
349 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
350 if (skfont == NULL) {
351 skfont = SkPdfFont::Default();
352 }
353
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000354
edisonn@google.com571c70b2013-07-10 17:09:50 +0000355 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000356 // TODO(edisonn): report warning
edisonn@google.com3aa35552013-08-14 18:26:20 +0000357 return kIgnoreError_SkPdfResult;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000358 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000359 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000360
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000361 SkUnencodedText binary(str);
362
363 SkDecodedText decoded;
364
365 if (skfont->encoding() == NULL) {
366 // TODO(edisonn): report warning
edisonn@google.com3aa35552013-08-14 18:26:20 +0000367 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000368 }
369
370 skfont->encoding()->decodeText(binary, &decoded);
371
372 SkPaint paint;
373 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
374 // Or maybe just not call setTextSize at all?
375 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
376 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
377 }
378
379// if (fCurFont && fCurFont->GetFontScale() != 0) {
380// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
381// }
382
383 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
384
edisonn@google.com6e49c342013-06-27 20:03:43 +0000385 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000386
edisonn@google.com3aa35552013-08-14 18:26:20 +0000387 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000388}
389
390// TODO(edisonn): create header files with declarations!
edisonn@google.com3aa35552013-08-14 18:26:20 +0000391SkPdfResult PdfOp_q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
392SkPdfResult PdfOp_Q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
393SkPdfResult PdfOp_Tw(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
394SkPdfResult PdfOp_Tc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000395
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000396// TODO(edisonn): perf!!!
397
398static SkColorTable* getGrayColortable() {
399 static SkColorTable* grayColortable = NULL;
400 if (grayColortable == NULL) {
401 SkPMColor* colors = new SkPMColor[256];
402 for (int i = 0 ; i < 256; i++) {
403 colors[i] = SkPreMultiplyARGB(255, i, i, i);
404 }
405 grayColortable = new SkColorTable(colors, 256);
406 }
407 return grayColortable;
408}
409
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000410static SkBitmap* transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000411 int width, int height, int bytesPerLine,
edisonn@google.com063d7072013-08-16 15:05:08 +0000412 int bpc, const SkString& colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000413 bool transparencyMask) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000414 SkBitmap* bitmap = new SkBitmap();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000415
edisonn@google.com571c70b2013-07-10 17:09:50 +0000416 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000417//#define MAX_COMPONENTS 10
418
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000419 // TODO(edisonn): assume start of lines are aligned at 32 bits?
420 // Is there a faster way to load the uncompressed stream into a bitmap?
421
422 // minimal support for now
edisonn@google.com063d7072013-08-16 15:05:08 +0000423 if ((colorSpace.equals("DeviceRGB") || colorSpace.equals("RGB")) && bpc == 8) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000424 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
425
426 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000427 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000428 for (int w = 0 ; w < width; w++) {
429 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
430 uncompressedStream[3 * w + 1],
431 uncompressedStream[3 * w + 2]);
432 i++;
433 }
434 uncompressedStream += bytesPerLine;
435 }
436
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000437 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
438 bitmap->setPixels(uncompressedStreamArgb);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000439 }
edisonn@google.com063d7072013-08-16 15:05:08 +0000440 else if ((colorSpace.equals("DeviceGray") || colorSpace.equals("Gray")) && bpc == 8) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000441 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
442
443 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000444 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000445 for (int w = 0 ; w < width; w++) {
446 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
447 uncompressedStream[w];
448 i++;
449 }
450 uncompressedStream += bytesPerLine;
451 }
452
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000453 bitmap->setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000454 width, height);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000455 bitmap->setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000456 }
457
458 // TODO(edisonn): Report Warning, NYI, or error
459 return bitmap;
460}
461
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000462// utils
463
464// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
465// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
466// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
467// skia format, through a table
468
469// this functions returns the image, it does not look at the smask.
470
edisonn@google.com3aa35552013-08-14 18:26:20 +0000471static SkBitmap* getImageFromObjectCore(SkPdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000472 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000473 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000474 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000475 }
476
edisonn@google.com33f11b62013-08-14 21:35:27 +0000477 int bpc = (int)image->BitsPerComponent(pdfContext->fPdfDoc);
478 int width = (int)image->Width(pdfContext->fPdfDoc);
479 int height = (int)image->Height(pdfContext->fPdfDoc);
edisonn@google.com063d7072013-08-16 15:05:08 +0000480 SkString colorSpace("DeviceRGB");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000481
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000482 bool indexed = false;
483 SkPMColor colors[256];
484 int cnt = 0;
485
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000486 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000487 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
488 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000489 } else if (image->isColorSpaceAArray(pdfContext->fPdfDoc)) {
490 SkPdfArray* array = image->getColorSpaceAsArray(pdfContext->fPdfDoc);
491 if (array && array->size() == 4 && array->objAtAIndex(0)->isName("Indexed") &&
492 (array->objAtAIndex(1)->isName("DeviceRGB") || array->objAtAIndex(1)->isName("RGB")) &&
493 array->objAtAIndex(2)->isInteger() &&
494 array->objAtAIndex(3)->isHexString()
495 ) {
496 // TODO(edisonn): suport only DeviceRGB for now.
497 indexed = true;
edisonn@google.com33f11b62013-08-14 21:35:27 +0000498 cnt = (int)array->objAtAIndex(2)->intValue() + 1;
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000499 if (cnt > 256) {
500 // TODO(edionn): report NYIs
501 return NULL;
502 }
503 SkColorTable colorTable(cnt);
504 NotOwnedString data = array->objAtAIndex(3)->strRef();
505 if (data.fBytes != (unsigned int)cnt * 3) {
506 // TODO(edionn): report error/warning
507 return NULL;
508 }
509 for (int i = 0 ; i < cnt; i++) {
510 colors[i] = SkPreMultiplyARGB(0xff, data.fBuffer[3 * i], data.fBuffer[3 * i + 1], data.fBuffer[3 * i + 2]);
511 }
512 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000513 }
514
515/*
516 bool imageMask = image->imageMask();
517
518 if (imageMask) {
519 if (bpc != 0 && bpc != 1) {
520 // TODO(edisonn): report warning to be used in testing.
521 return SkBitmap();
522 }
523 bpc = 1;
524 }
525*/
526
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000527 const unsigned char* uncompressedStream = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000528 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000529
edisonn@google.com571c70b2013-07-10 17:09:50 +0000530 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000531
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000532 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000533 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000534 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000535 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000536 }
537
edisonn@google.com571c70b2013-07-10 17:09:50 +0000538 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
539
540 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
edisonn@google.com063d7072013-08-16 15:05:08 +0000541 streamDict->getFilterAsName(NULL).equals("DCTDecode")) ||
edisonn@google.com571c70b2013-07-10 17:09:50 +0000542 (streamDict->isFilterAArray(NULL) &&
543 streamDict->getFilterAsArray(NULL)->size() > 0 &&
544 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
edisonn@google.com063d7072013-08-16 15:05:08 +0000545 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2().equals("DCTDecode")))) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000546 SkBitmap* bitmap = new SkBitmap();
547 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, bitmap);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000548 return bitmap;
549 }
550
551
552
553 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
554// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
555// obj.GetDictionary().GetKey(PdfName("Filter")));
556// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
557// value = resolveReferenceObject(pdfContext->fPdfDoc,
558// &value->GetArray()[0]);
559// }
560// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
561// SkStream stream = SkStream::
562// SkImageDecoder::Factory()
563// }
564
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000565 // TODO(edisonn): assumes RGB for now, since it is the only onwe implemented
566 if (indexed) {
567 SkBitmap* bitmap = new SkBitmap();
568 bitmap->setConfig(SkBitmap::kIndex8_Config, width, height);
569 SkColorTable* colorTable = new SkColorTable(colors, cnt);
570 bitmap->setPixels((void*)uncompressedStream, colorTable);
571 return bitmap;
572 }
573
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000574 int bytesPerLine = (int)(uncompressedStreamLength / height);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000575#ifdef PDF_TRACE
576 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000577 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000578 }
579#endif
580
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000581 SkBitmap* bitmap = transferImageStreamToBitmap(
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000582 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000583 (int)width, (int)height, bytesPerLine,
584 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000585 transparencyMask);
586
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000587 return bitmap;
588}
589
edisonn@google.com3aa35552013-08-14 18:26:20 +0000590static SkBitmap* getImageFromObject(SkPdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000591 if (!transparencyMask) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000592 if (!image->hasData(SkPdfNativeObject::kBitmap_Data)) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000593 SkBitmap* bitmap = getImageFromObjectCore(pdfContext, image, transparencyMask);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000594 image->setData(bitmap, SkPdfNativeObject::kBitmap_Data);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000595 }
edisonn@google.com3aa35552013-08-14 18:26:20 +0000596 return (SkBitmap*) image->data(SkPdfNativeObject::kBitmap_Data);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000597 } else {
598 return getImageFromObjectCore(pdfContext, image, transparencyMask);
599 }
600}
601
edisonn@google.com3aa35552013-08-14 18:26:20 +0000602static SkBitmap* getSmaskFromObject(SkPdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000603 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000604
605 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000606 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000607 }
608
609 // TODO(edisonn): implement GS SMask. Default to empty right now.
610 return pdfContext->fGraphicsState.fSMask;
611}
612
edisonn@google.com3aa35552013-08-14 18:26:20 +0000613static SkPdfResult doXObject_Image(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000614 if (skpdfimage == NULL) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000615 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000616 }
617
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000618 SkBitmap* image = getImageFromObject(pdfContext, skpdfimage, false);
619 SkBitmap* sMask = getSmaskFromObject(pdfContext, skpdfimage);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000620
621 canvas->save();
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000622 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000623
edisonn@google.com571c70b2013-07-10 17:09:50 +0000624 SkScalar z = SkIntToScalar(0);
625 SkScalar one = SkIntToScalar(1);
626
627 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
628 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
629 SkMatrix flip;
630 SkAssertResult(flip.setPolyToPoly(from, to, 4));
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000631 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000632 solveImageFlip.preConcat(flip);
633 canvas->setMatrix(solveImageFlip);
edisonn@google.com0f901902013-08-07 11:56:16 +0000634
635#ifdef PDF_TRACE
636 SkPoint final[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
637 solveImageFlip.mapPoints(final, 4);
638 printf("IMAGE rect = ");
639 for (int i = 0; i < 4; i++) {
640 printf("(%f %f) ", SkScalarToDouble(final[i].x()), SkScalarToDouble(final[i].y()));
641 }
642 printf("\n");
643#endif // PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +0000644
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000645 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
646
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000647 // TODO(edisonn): soft mask type? alpha/luminosity.
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000648 SkPaint paint;
649 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
650
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000651 if (!sMask || sMask->empty()) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000652 canvas->drawBitmapRect(*image, dst, &paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000653 } else {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000654 canvas->saveLayer(&dst, &paint);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000655 canvas->drawBitmapRect(*image, dst, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000656 SkPaint xfer;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000657 // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf?
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000658 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000659 canvas->drawBitmapRect(*sMask, dst, &xfer);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000660 canvas->restore();
661 }
662
663 canvas->restore();
664
edisonn@google.com3aa35552013-08-14 18:26:20 +0000665 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000666}
667
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000668//TODO(edisonn): options for implementing isolation and knockout
669// 1) emulate them (current solution)
670// PRO: simple
671// CON: will need to use readPixels, which means serious perf issues
672// 2) Compile a plan for an array of matrixes, compose the result at the end
673// PRO: might be faster then 1, no need to readPixels
674// CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan
675// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
676// 3) support them natively in SkCanvas
677// PRO: simple
678// CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1)
679// 4) compile a plan using pathops, and render once without any fancy rules with backdrop
680// PRO: simple, fast
681// CON: pathops must be bug free first + time to compute new paths
682// pay a price at loading pdf to compute a pdf draw plan
683// 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 +0000684// 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 +0000685
686
687// TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree!
688// TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed
689
690
691
edisonn@google.com3aa35552013-08-14 18:26:20 +0000692static void doGroup_before(SkPdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000693 SkRect bboxOrig = bbox;
694 SkBitmap backdrop;
695 bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc);
696// bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000697 SkPaint paint;
698 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
699 canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000700}
701
edisonn@google.com251176e2013-08-01 13:24:00 +0000702// TODO(edisonn): non isolation implemented in skia
edisonn@google.com3aa35552013-08-14 18:26:20 +0000703//static void doGroup_after(SkPdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup) {
edisonn@google.com251176e2013-08-01 13:24:00 +0000704// if not isolated
705// canvas->drawBitmapRect(backdrop, bboxOrig, NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000706//}
707
edisonn@google.com3aa35552013-08-14 18:26:20 +0000708static SkPdfResult doXObject_Form(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000709 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000710 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000711 }
712
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000713 if (!skobj->has_BBox()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000714 return kIgnoreError_SkPdfResult;
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000715 }
716
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000717 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000718
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000719
edisonn@google.com571c70b2013-07-10 17:09:50 +0000720 if (skobj->Resources(pdfContext->fPdfDoc)) {
721 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000722 }
723
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000724 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000725
edisonn@google.com571c70b2013-07-10 17:09:50 +0000726 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000727 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000728 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
729 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
730 pdfContext->fGraphicsState.fMatrixTm = matrix;
731 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000732 // TODO(edisonn) reset matrixTm and matricTlm also?
733 }
734
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000735 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com0f901902013-08-07 11:56:16 +0000736 pdfContext->fGraphicsState.fContentStreamMatrix = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000737
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000738 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000739
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000740 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
741 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000742
743 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
744 // For this PdfContentsTokenizer needs to be extended.
745
edisonn@google.come878e722013-07-29 19:10:58 +0000746 // This is a group?
747 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000748 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
749 doGroup_before(pdfContext, canvas, bbox, tgroup, false);
edisonn@google.come878e722013-07-29 19:10:58 +0000750 }
751
edisonn@google.com571c70b2013-07-10 17:09:50 +0000752 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000753
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000754 SkPdfNativeTokenizer* tokenizer =
755 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000756 if (tokenizer != NULL) {
757 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
758 looper.loop();
759 delete tokenizer;
760 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000761
762 // TODO(edisonn): should we restore the variable stack at the same state?
763 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000764
765 if (skobj->has_Group()) {
766 canvas->restore();
767 }
768
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000769 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000770 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000771}
772
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000773
774// TODO(edisonn): Extract a class like ObjWithStream
edisonn@google.com3aa35552013-08-14 18:26:20 +0000775static SkPdfResult doXObject_Pattern(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfType1PatternDictionary* skobj) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000776 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000777 return kIgnoreError_SkPdfResult;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000778 }
779
780 if (!skobj->has_BBox()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000781 return kIgnoreError_SkPdfResult;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000782 }
783
784 PdfOp_q(pdfContext, canvas, NULL);
785
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000786
787 if (skobj->Resources(pdfContext->fPdfDoc)) {
788 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
789 }
790
edisonn@google.com0f901902013-08-07 11:56:16 +0000791 SkTraceMatrix(pdfContext->fGraphicsState.fContentStreamMatrix, "Current Content stream matrix");
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000792
793 if (skobj->has_Matrix()) {
edisonn@google.com0f901902013-08-07 11:56:16 +0000794 pdfContext->fGraphicsState.fContentStreamMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000795 }
796
edisonn@google.com0f901902013-08-07 11:56:16 +0000797 SkTraceMatrix(pdfContext->fGraphicsState.fContentStreamMatrix, "Total Content stream matrix");
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000798
edisonn@google.com0f901902013-08-07 11:56:16 +0000799 canvas->setMatrix(pdfContext->fGraphicsState.fContentStreamMatrix);
800 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fContentStreamMatrix;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000801
802 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
803 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
804
805 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
806 // For this PdfContentsTokenizer needs to be extended.
807
808 SkPdfStream* stream = (SkPdfStream*)skobj;
809
810 SkPdfNativeTokenizer* tokenizer =
811 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
812 if (tokenizer != NULL) {
813 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
814 looper.loop();
815 delete tokenizer;
816 }
817
818 // TODO(edisonn): should we restore the variable stack at the same state?
819 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
820
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000821 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000822 return kPartial_SkPdfResult;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000823}
824
825
edisonn@google.com3aa35552013-08-14 18:26:20 +0000826//static SkPdfResult doXObject_PS(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* obj) {
827// return kNYI_SkPdfResult;
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000828//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000829
edisonn@google.com3aa35552013-08-14 18:26:20 +0000830SkPdfResult doType3Char(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000831 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000832 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000833 }
834
835 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000836
837 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
838 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000839 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrixTm;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000840
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000841 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
edisonn@google.come57c62d2013-08-07 18:04:15 +0000842 pdfContext->fGraphicsState.fCTM.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000843
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000844 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000845
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000846 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000847
848 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000849 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000850
851 SkTraceRect(rm, "bbox mapped");
852
853 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
854
855 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
856 // For this PdfContentsTokenizer needs to be extended.
857
edisonn@google.com571c70b2013-07-10 17:09:50 +0000858 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000859
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000860 SkPdfNativeTokenizer* tokenizer =
861 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000862 if (tokenizer != NULL) {
863 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
864 looper.loop();
865 delete tokenizer;
866 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000867
868 // TODO(edisonn): should we restore the variable stack at the same state?
869 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000870 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000871
edisonn@google.com3aa35552013-08-14 18:26:20 +0000872 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000873}
874
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000875class CheckRecursiveRendering {
edisonn@google.com063d7072013-08-16 15:05:08 +0000876 SkPdfNativeObject* fObj;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000877public:
edisonn@google.com063d7072013-08-16 15:05:08 +0000878 CheckRecursiveRendering(SkPdfNativeObject* obj) : fObj(obj) {
879 SkASSERT(!obj->inRendering());
880 obj->startRendering();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000881 }
882
883 ~CheckRecursiveRendering() {
edisonn@google.com063d7072013-08-16 15:05:08 +0000884 SkASSERT(fObj->inRendering());
885 fObj->doneRendering();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000886 }
887
edisonn@google.com3aa35552013-08-14 18:26:20 +0000888 static bool IsInRendering(const SkPdfNativeObject* obj) {
edisonn@google.com063d7072013-08-16 15:05:08 +0000889 return obj->inRendering();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000890 }
891};
892
edisonn@google.com063d7072013-08-16 15:05:08 +0000893static SkPdfResult doXObject(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfNativeObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000894 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000895 // Oops, corrupt PDF!
edisonn@google.com3aa35552013-08-14 18:26:20 +0000896 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000897 }
898
edisonn@google.com571c70b2013-07-10 17:09:50 +0000899 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000900
edisonn@google.com571c70b2013-07-10 17:09:50 +0000901 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000902 {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000903 case kImageDictionary_SkPdfNativeObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000904 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000905 case kType1FormDictionary_SkPdfNativeObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000906 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000907 //case kObjectDictionaryXObjectPS_SkPdfNativeObjectType:
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000908 //return doXObject_PS(skxobj.asPS());
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000909 default: {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000910 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(obj) != kNone_SkPdfNativeObjectType) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000911 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)obj;
912 return doXObject_Pattern(pdfContext, canvas, pattern);
913 }
914 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000915 }
edisonn@google.com3aa35552013-08-14 18:26:20 +0000916 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000917}
918
edisonn@google.com3aa35552013-08-14 18:26:20 +0000919static SkPdfResult doPage(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000920 if (!skobj) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000921 return kIgnoreError_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000922 }
923
924 if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000925 return kNYI_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000926 }
927
928 SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
929
930 if (!stream) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000931 return kIgnoreError_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000932 }
933
934 if (CheckRecursiveRendering::IsInRendering(skobj)) {
935 // Oops, corrupt PDF!
edisonn@google.com3aa35552013-08-14 18:26:20 +0000936 return kIgnoreError_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000937 }
938 CheckRecursiveRendering checkRecursion(skobj);
939
940
941 PdfOp_q(pdfContext, canvas, NULL);
942
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000943
944 if (skobj->Resources(pdfContext->fPdfDoc)) {
945 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
946 }
947
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000948 // TODO(edisonn): MediaBox can be inherited!!!!
949 SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc);
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000950 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000951 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
952 doGroup_before(pdfContext, canvas, bbox, tgroup, true);
953 } else {
954 canvas->save();
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000955 }
956
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000957
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000958 SkPdfNativeTokenizer* tokenizer =
959 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
960 if (tokenizer != NULL) {
961 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
962 looper.loop();
963 delete tokenizer;
964 }
965
966 // TODO(edisonn): should we restore the variable stack at the same state?
967 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
968 canvas->restore();
969 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000970 return kPartial_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000971}
972
edisonn@google.com3aa35552013-08-14 18:26:20 +0000973SkPdfResult PdfOp_q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000974 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
975 canvas->save();
edisonn@google.com3aa35552013-08-14 18:26:20 +0000976 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000977}
978
edisonn@google.com3aa35552013-08-14 18:26:20 +0000979SkPdfResult PdfOp_Q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000980 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
981 pdfContext->fStateStack.pop();
982 canvas->restore();
edisonn@google.com3aa35552013-08-14 18:26:20 +0000983 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000984}
985
edisonn@google.com3aa35552013-08-14 18:26:20 +0000986static SkPdfResult PdfOp_cm(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000987 double array[6];
988 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000989 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000990 pdfContext->fObjectStack.pop();
991 }
992
993 // a b
994 // c d
995 // e f
996
997 // 0 1
998 // 2 3
999 // 4 5
1000
1001 // sx ky
1002 // kx sy
1003 // tx ty
1004 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1005
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001006 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001007
1008#ifdef PDF_TRACE
1009 printf("cm ");
1010 for (int i = 0 ; i < 6 ; i++) {
1011 printf("%f ", array[i]);
1012 }
1013 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001014 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001015#endif
1016
edisonn@google.com3aa35552013-08-14 18:26:20 +00001017 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001018}
1019
1020//leading TL Set the text leading, Tl
1021//, to leading, which is a number expressed in unscaled text
1022//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001023static SkPdfResult PdfOp_TL(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001024 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001025
1026 pdfContext->fGraphicsState.fTextLeading = ty;
1027
edisonn@google.com3aa35552013-08-14 18:26:20 +00001028 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001029}
1030
edisonn@google.com3aa35552013-08-14 18:26:20 +00001031static SkPdfResult PdfOp_Td(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001032#ifdef PDF_TRACE
1033 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
1034#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +00001035 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1036 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001037
edisonn@google.come57c62d2013-08-07 18:04:15 +00001038 double array[6] = {1, 0, 0, 1, tx, -ty};
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001039 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1040
1041 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1042 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
1043
edisonn@google.com3aa35552013-08-14 18:26:20 +00001044 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001045}
1046
edisonn@google.com3aa35552013-08-14 18:26:20 +00001047static SkPdfResult PdfOp_TD(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001048 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1049 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001050
edisonn@google.com571c70b2013-07-10 17:09:50 +00001051 // TODO(edisonn): Create factory methods or constructors so native is hidden
1052 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001053 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001054
1055 PdfOp_TL(pdfContext, canvas, looper);
1056
edisonn@google.com571c70b2013-07-10 17:09:50 +00001057 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001058 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001059
edisonn@google.com571c70b2013-07-10 17:09:50 +00001060 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001061 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001062
edisonn@google.com3aa35552013-08-14 18:26:20 +00001063 SkPdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001064
1065 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
1066 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001067
1068 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001069}
1070
edisonn@google.com3aa35552013-08-14 18:26:20 +00001071static SkPdfResult PdfOp_Tm(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001072 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1073 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1074 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1075 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1076 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1077 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001078
1079 double array[6];
1080 array[0] = a;
1081 array[1] = b;
1082 array[2] = c;
1083 array[3] = d;
1084 array[4] = e;
1085 array[5] = f;
1086
1087 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001088 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.come57c62d2013-08-07 18:04:15 +00001089 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001090
1091 // TODO(edisonn): Text positioning.
1092 pdfContext->fGraphicsState.fMatrixTm = matrix;
1093 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
1094
edisonn@google.com3aa35552013-08-14 18:26:20 +00001095 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001096}
1097
1098//— T* Move to the start of the next line. This operator has the same effect as the code
1099//0 Tl Td
1100//where Tl is the current leading parameter in the text state
edisonn@google.com3aa35552013-08-14 18:26:20 +00001101static SkPdfResult PdfOp_T_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001102 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
1103 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001104
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001105 pdfContext->fObjectStack.push(zero);
1106 pdfContext->fObjectStack.push(tl);
1107
edisonn@google.com3aa35552013-08-14 18:26:20 +00001108 SkPdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001109
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001110 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001111}
1112
edisonn@google.com3aa35552013-08-14 18:26:20 +00001113static SkPdfResult PdfOp_m(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001114 if (pdfContext->fGraphicsState.fPathClosed) {
1115 pdfContext->fGraphicsState.fPath.reset();
1116 pdfContext->fGraphicsState.fPathClosed = false;
1117 }
1118
edisonn@google.com571c70b2013-07-10 17:09:50 +00001119 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1120 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001121
1122 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1123 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1124
edisonn@google.com3aa35552013-08-14 18:26:20 +00001125 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001126}
1127
edisonn@google.com3aa35552013-08-14 18:26:20 +00001128static SkPdfResult PdfOp_l(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001129 if (pdfContext->fGraphicsState.fPathClosed) {
1130 pdfContext->fGraphicsState.fPath.reset();
1131 pdfContext->fGraphicsState.fPathClosed = false;
1132 }
1133
edisonn@google.com571c70b2013-07-10 17:09:50 +00001134 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1135 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001136
1137 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1138 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1139
edisonn@google.com3aa35552013-08-14 18:26:20 +00001140 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001141}
1142
edisonn@google.com3aa35552013-08-14 18:26:20 +00001143static SkPdfResult PdfOp_c(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001144 if (pdfContext->fGraphicsState.fPathClosed) {
1145 pdfContext->fGraphicsState.fPath.reset();
1146 pdfContext->fGraphicsState.fPathClosed = false;
1147 }
1148
edisonn@google.com571c70b2013-07-10 17:09:50 +00001149 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1150 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1151 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1152 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1153 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1154 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001155
1156 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1157 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1158 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1159
1160 pdfContext->fGraphicsState.fCurPosX = x3;
1161 pdfContext->fGraphicsState.fCurPosY = y3;
1162
edisonn@google.com3aa35552013-08-14 18:26:20 +00001163 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001164}
1165
edisonn@google.com3aa35552013-08-14 18:26:20 +00001166static SkPdfResult PdfOp_v(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001167 if (pdfContext->fGraphicsState.fPathClosed) {
1168 pdfContext->fGraphicsState.fPath.reset();
1169 pdfContext->fGraphicsState.fPathClosed = false;
1170 }
1171
edisonn@google.com571c70b2013-07-10 17:09:50 +00001172 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1173 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1174 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1175 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001176 double y1 = pdfContext->fGraphicsState.fCurPosY;
1177 double x1 = pdfContext->fGraphicsState.fCurPosX;
1178
1179 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1180 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1181 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1182
1183 pdfContext->fGraphicsState.fCurPosX = x3;
1184 pdfContext->fGraphicsState.fCurPosY = y3;
1185
edisonn@google.com3aa35552013-08-14 18:26:20 +00001186 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001187}
1188
edisonn@google.com3aa35552013-08-14 18:26:20 +00001189static SkPdfResult PdfOp_y(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001190 if (pdfContext->fGraphicsState.fPathClosed) {
1191 pdfContext->fGraphicsState.fPath.reset();
1192 pdfContext->fGraphicsState.fPathClosed = false;
1193 }
1194
edisonn@google.com571c70b2013-07-10 17:09:50 +00001195 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1196 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001197 double y2 = pdfContext->fGraphicsState.fCurPosY;
1198 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001199 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1200 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001201
1202 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1203 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1204 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1205
1206 pdfContext->fGraphicsState.fCurPosX = x3;
1207 pdfContext->fGraphicsState.fCurPosY = y3;
1208
edisonn@google.com3aa35552013-08-14 18:26:20 +00001209 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001210}
1211
edisonn@google.com3aa35552013-08-14 18:26:20 +00001212static SkPdfResult PdfOp_re(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001213 if (pdfContext->fGraphicsState.fPathClosed) {
1214 pdfContext->fGraphicsState.fPath.reset();
1215 pdfContext->fGraphicsState.fPathClosed = false;
1216 }
1217
edisonn@google.com571c70b2013-07-10 17:09:50 +00001218 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1219 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1220 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1221 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001222
1223 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1224 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1225
1226 pdfContext->fGraphicsState.fCurPosX = x;
1227 pdfContext->fGraphicsState.fCurPosY = y + height;
1228
edisonn@google.com3aa35552013-08-14 18:26:20 +00001229 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001230}
1231
edisonn@google.com3aa35552013-08-14 18:26:20 +00001232static SkPdfResult PdfOp_h(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001233 pdfContext->fGraphicsState.fPath.close();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001234 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001235}
1236
edisonn@google.com3aa35552013-08-14 18:26:20 +00001237static SkPdfResult PdfOp_fillAndStroke(SkPdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001238 SkPath path = pdfContext->fGraphicsState.fPath;
1239
1240 if (close) {
1241 path.close();
1242 }
1243
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001244 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001245
1246 SkPaint paint;
1247
1248 SkPoint line[2];
1249 if (fill && !stroke && path.isLine(line)) {
1250 paint.setStyle(SkPaint::kStroke_Style);
1251
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001252 // TODO(edisonn): implement this with patterns
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001253 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1254 paint.setStrokeWidth(SkDoubleToScalar(0));
1255
1256 canvas->drawPath(path, paint);
1257 } else {
1258 if (fill) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001259 if (strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0 &&
1260 pdfContext->fGraphicsState.fNonStroking.fPattern != NULL) {
1261
1262 // TODO(edisonn): we can use a shader here, like imageshader to draw fast. ultimately,
1263 // if this is not possible, and we are in rasper mode, and the cells don't intersect, we could even have multiple cpus.
1264
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001265 PdfOp_q(pdfContext, canvas, NULL);
1266
1267 if (evenOdd) {
1268 path.setFillType(SkPath::kEvenOdd_FillType);
1269 }
1270 canvas->clipPath(path);
1271
edisonn@google.com3aa35552013-08-14 18:26:20 +00001272 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(pdfContext->fGraphicsState.fNonStroking.fPattern) != kNone_SkPdfNativeObjectType) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001273 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)pdfContext->fGraphicsState.fNonStroking.fPattern;
1274
1275 // TODO(edisonn): constants
1276 // TODO(edisonn): colored
1277 if (pattern->PaintType(pdfContext->fPdfDoc) == 1) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001278 // TODO(edisonn): don't use abs, iterate as asked, if the cells intersect
1279 // it will change the result iterating in reverse
1280 int xStep = abs((int)pattern->XStep(pdfContext->fPdfDoc));
1281 int yStep = abs((int)pattern->YStep(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001282
1283 SkRect bounds = path.getBounds();
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001284
1285 // TODO(edisonn): xstep and ystep can be negative, and we need to iterate in reverse
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001286 // TODO(edisonn): don't do that!
1287 bounds.sort();
1288
1289 SkScalar x;
1290 SkScalar y;
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001291
1292 y = bounds.top();
1293 int totalx = 0;
1294 int totaly = 0;
1295 while (y < bounds.bottom()) {
1296 x = bounds.left();
1297 totalx = 0;
1298
1299 while (x < bounds.right()) {
1300 doXObject(pdfContext, canvas, pattern);
1301
edisonn@google.com0f901902013-08-07 11:56:16 +00001302 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(xStep), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001303 totalx += xStep;
1304 x += SkIntToScalar(xStep);
1305 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001306 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(-totalx), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001307
edisonn@google.com0f901902013-08-07 11:56:16 +00001308 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(-yStep));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001309 totaly += yStep;
1310 y += SkIntToScalar(yStep);
1311 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001312 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(totaly));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001313 }
1314 }
1315
1316 // apply matrix
1317 // get xstep, y step, bbox ... for cliping, and bos of the path
1318
1319 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001320 } else {
1321 paint.setStyle(SkPaint::kFill_Style);
1322 if (evenOdd) {
1323 path.setFillType(SkPath::kEvenOdd_FillType);
1324 }
1325
1326 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1327
1328 canvas->drawPath(path, paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001329 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001330 }
1331
1332 if (stroke) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001333 if (false && strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0) {
1334 // TODO(edisonn): implement Pattern for strokes
1335 paint.setStyle(SkPaint::kStroke_Style);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001336
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001337 paint.setColor(SK_ColorGREEN);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001338
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001339 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1340 canvas->drawPath(path, paint);
1341 } else {
1342 paint.setStyle(SkPaint::kStroke_Style);
1343
1344 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1345
1346 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1347 canvas->drawPath(path, paint);
1348 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001349 }
1350 }
1351
1352 pdfContext->fGraphicsState.fPath.reset();
1353 // todo zoom ... other stuff ?
1354
1355 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1356#ifndef PDF_DEBUG_NO_CLIPING
1357 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1358#endif
1359 }
1360
1361 //pdfContext->fGraphicsState.fClipPath.reset();
1362 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1363
edisonn@google.com3aa35552013-08-14 18:26:20 +00001364 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001365
1366}
1367
edisonn@google.com3aa35552013-08-14 18:26:20 +00001368static SkPdfResult PdfOp_S(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001369 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1370}
1371
edisonn@google.com3aa35552013-08-14 18:26:20 +00001372static SkPdfResult PdfOp_s(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001373 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1374}
1375
edisonn@google.com3aa35552013-08-14 18:26:20 +00001376static SkPdfResult PdfOp_F(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001377 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1378}
1379
edisonn@google.com3aa35552013-08-14 18:26:20 +00001380static SkPdfResult PdfOp_f(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001381 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1382}
1383
edisonn@google.com3aa35552013-08-14 18:26:20 +00001384static SkPdfResult PdfOp_f_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001385 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1386}
1387
edisonn@google.com3aa35552013-08-14 18:26:20 +00001388static SkPdfResult PdfOp_B(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001389 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1390}
1391
edisonn@google.com3aa35552013-08-14 18:26:20 +00001392static SkPdfResult PdfOp_B_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001393 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1394}
1395
edisonn@google.com3aa35552013-08-14 18:26:20 +00001396static SkPdfResult PdfOp_b(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001397 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1398}
1399
edisonn@google.com3aa35552013-08-14 18:26:20 +00001400static SkPdfResult PdfOp_b_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001401 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1402}
1403
edisonn@google.com3aa35552013-08-14 18:26:20 +00001404static SkPdfResult PdfOp_n(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001405 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001406 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1407#ifndef PDF_DEBUG_NO_CLIPING
1408 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1409#endif
1410 }
1411
1412 //pdfContext->fGraphicsState.fClipPath.reset();
1413 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1414
1415 pdfContext->fGraphicsState.fPathClosed = true;
1416
edisonn@google.com3aa35552013-08-14 18:26:20 +00001417 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001418}
1419
edisonn@google.com3aa35552013-08-14 18:26:20 +00001420static SkPdfResult PdfOp_BT(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001421 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.come57c62d2013-08-07 18:04:15 +00001422 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
1423 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
1424 pdfContext->fGraphicsState.fMatrixTm = matrix;
1425 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001426
edisonn@google.com3aa35552013-08-14 18:26:20 +00001427 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001428}
1429
edisonn@google.com3aa35552013-08-14 18:26:20 +00001430static SkPdfResult PdfOp_ET(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001431 if (!pdfContext->fGraphicsState.fTextBlock) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001432 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001433 }
1434 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001435 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001436}
1437
edisonn@google.com33f11b62013-08-14 21:35:27 +00001438static SkPdfResult skpdfGraphicsStateApplyFontCore(SkPdfContext* pdfContext, const SkPdfNativeObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001439#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001440 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001441#endif
1442
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001443 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1444 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001445 return kIgnoreError_SkPdfResult;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001446 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001447
edisonn@google.com3aa35552013-08-14 18:26:20 +00001448 SkPdfNativeObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001449 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
edisonn@google.com3aa35552013-08-14 18:26:20 +00001450 if (kNone_SkPdfNativeObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001451 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001452 return kIgnoreError_SkPdfResult;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001453 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001454
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001455 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1456
1457 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1458
1459 if (skfont) {
1460 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001461 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001462 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001463 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001464}
1465
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001466//font size Tf Set the text font, Tf
1467//, to font and the text font size, Tfs, to size. font is the name of a
1468//font resource in the Fontsubdictionary of the current resource dictionary; size is
1469//a number representing a scale factor. There is no initial value for either font or
1470//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001471static SkPdfResult PdfOp_Tf(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001472 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001473 SkPdfNativeObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001474 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1475}
1476
edisonn@google.com3aa35552013-08-14 18:26:20 +00001477static SkPdfResult PdfOp_Tj(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001478 if (!pdfContext->fGraphicsState.fTextBlock) {
1479 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001480 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001481 }
1482
edisonn@google.com3aa35552013-08-14 18:26:20 +00001483 SkPdfResult ret = DrawText(pdfContext,
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001484 pdfContext->fObjectStack.top(),
1485 canvas);
1486 pdfContext->fObjectStack.pop();
1487
1488 return ret;
1489}
1490
edisonn@google.com3aa35552013-08-14 18:26:20 +00001491static SkPdfResult PdfOp_quote(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001492 if (!pdfContext->fGraphicsState.fTextBlock) {
1493 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001494 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001495 }
1496
1497 PdfOp_T_star(pdfContext, canvas, looper);
1498 // Do not pop, and push, just transfer the param to Tj
1499 return PdfOp_Tj(pdfContext, canvas, looper);
1500}
1501
edisonn@google.com3aa35552013-08-14 18:26:20 +00001502static SkPdfResult PdfOp_doublequote(SkPdfContext* 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?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001505 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001506 }
1507
edisonn@google.com3aa35552013-08-14 18:26:20 +00001508 SkPdfNativeObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1509 SkPdfNativeObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1510 SkPdfNativeObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001511
1512 pdfContext->fObjectStack.push(aw);
1513 PdfOp_Tw(pdfContext, canvas, looper);
1514
1515 pdfContext->fObjectStack.push(ac);
1516 PdfOp_Tc(pdfContext, canvas, looper);
1517
1518 pdfContext->fObjectStack.push(str);
1519 PdfOp_quote(pdfContext, canvas, looper);
1520
edisonn@google.com3aa35552013-08-14 18:26:20 +00001521 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001522}
1523
edisonn@google.com3aa35552013-08-14 18:26:20 +00001524static SkPdfResult PdfOp_TJ(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001525 if (!pdfContext->fGraphicsState.fTextBlock) {
1526 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001527 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001528 }
1529
edisonn@google.com571c70b2013-07-10 17:09:50 +00001530 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001531 pdfContext->fObjectStack.pop();
1532
edisonn@google.com571c70b2013-07-10 17:09:50 +00001533 if (!array->isArray()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001534 return kIgnoreError_SkPdfResult;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001535 }
1536
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001537 for( int i=0; i<static_cast<int>(array->size()); i++ )
1538 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001539 if( (*array)[i]->isAnyString()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001540 SkPdfNativeObject* obj = (*array)[i];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001541 DrawText(pdfContext,
1542 obj,
1543 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001544 } else if ((*array)[i]->isNumber()) {
1545 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001546 SkMatrix matrix;
1547 matrix.setAll(SkDoubleToScalar(1),
1548 SkDoubleToScalar(0),
1549 // TODO(edisonn): use writing mode, vertical/horizontal.
1550 SkDoubleToScalar(-dx), // amount is substracted!!!
1551 SkDoubleToScalar(0),
1552 SkDoubleToScalar(1),
1553 SkDoubleToScalar(0),
1554 SkDoubleToScalar(0),
1555 SkDoubleToScalar(0),
1556 SkDoubleToScalar(1));
1557
1558 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1559 }
1560 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001561 return kPartial_SkPdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001562}
1563
edisonn@google.com3aa35552013-08-14 18:26:20 +00001564static SkPdfResult PdfOp_CS_cs(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
1565 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com4f898b72013-08-07 21:11:57 +00001566
1567 //Next, get the ColorSpace Dictionary from the Resource Dictionary:
1568 SkPdfDictionary* colorSpaceResource = pdfContext->fGraphicsState.fResources->ColorSpace(pdfContext->fPdfDoc);
1569
edisonn@google.com3aa35552013-08-14 18:26:20 +00001570 SkPdfNativeObject* colorSpace = colorSpaceResource ? pdfContext->fPdfDoc->resolveReference(colorSpaceResource->get(name)) : name;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001571
1572 if (colorSpace == NULL) {
1573 colorOperator->fColorSpace = name->strRef();
1574 } else {
1575#ifdef PDF_TRACE
1576 printf("CS = %s\n", colorSpace->toString(0, 0).c_str());
1577#endif // PDF_TRACE
1578 if (colorSpace->isName()) {
1579 colorOperator->fColorSpace = colorSpace->strRef();
1580 } else if (colorSpace->isArray()) {
1581 int cnt = colorSpace->size();
1582 if (cnt == 0) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001583 return kIgnoreError_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001584 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001585 SkPdfNativeObject* type = colorSpace->objAtAIndex(0);
edisonn@google.com4f898b72013-08-07 21:11:57 +00001586 type = pdfContext->fPdfDoc->resolveReference(type);
1587
1588 if (type->isName("ICCBased")) {
1589 if (cnt != 2) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001590 return kIgnoreError_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001591 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001592 SkPdfNativeObject* prop = colorSpace->objAtAIndex(1);
edisonn@google.com4f898b72013-08-07 21:11:57 +00001593 prop = pdfContext->fPdfDoc->resolveReference(prop);
1594#ifdef PDF_TRACE
1595 printf("ICCBased prop = %s\n", prop->toString(0, 0).c_str());
1596#endif // PDF_TRACE
1597 // TODO(edisonn): hack
1598 if (prop && prop->isDictionary() && prop->get("N") && prop->get("N")->isInteger() && prop->get("N")->intValue() == 3) {
1599 colorOperator->setColorSpace(&strings_DeviceRGB);
edisonn@google.com3aa35552013-08-14 18:26:20 +00001600 return kPartial_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001601 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001602 return kNYI_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001603 }
1604 }
1605 }
1606
edisonn@google.com3aa35552013-08-14 18:26:20 +00001607 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001608}
1609
edisonn@google.com3aa35552013-08-14 18:26:20 +00001610static SkPdfResult PdfOp_CS(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001611 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1612}
1613
edisonn@google.com3aa35552013-08-14 18:26:20 +00001614static SkPdfResult PdfOp_cs(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001615 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1616}
1617
edisonn@google.com3aa35552013-08-14 18:26:20 +00001618static SkPdfResult PdfOp_SC_sc(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001619 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001620// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001621
1622 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1623
1624 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001625 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001626 doubles = false;
1627 }
1628
1629#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001630 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001631#endif
1632
1633 for (int i = n - 1; i >= 0 ; i--) {
1634 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001635 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1636// } else {
1637// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001638 }
1639 }
1640
1641 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001642 // TODO(edisonn): do possible field values to enum at parsing time!
1643 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001644 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001645 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001646 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001647 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001648}
1649
edisonn@google.com3aa35552013-08-14 18:26:20 +00001650static SkPdfResult PdfOp_SC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001651 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1652}
1653
edisonn@google.com3aa35552013-08-14 18:26:20 +00001654static SkPdfResult PdfOp_sc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001655 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1656}
1657
edisonn@google.com3aa35552013-08-14 18:26:20 +00001658static SkPdfResult PdfOp_SCN_scn(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001659 if (pdfContext->fObjectStack.top()->isName()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001660 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com276fed92013-08-01 21:20:47 +00001661
1662 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001663 SkPdfDictionary* patternResources = pdfContext->fGraphicsState.fResources->Pattern(pdfContext->fPdfDoc);
edisonn@google.com276fed92013-08-01 21:20:47 +00001664
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001665 if (patternResources == NULL) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001666#ifdef PDF_TRACE
1667 printf("ExtGState is NULL!\n");
1668#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001669 return kIgnoreError_SkPdfResult;
edisonn@google.com276fed92013-08-01 21:20:47 +00001670 }
1671
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001672 colorOperator->setPatternColorSpace(pdfContext->fPdfDoc->resolveReference(patternResources->get(name)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001673 }
1674
1675 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1676 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1677
edisonn@google.com3aa35552013-08-14 18:26:20 +00001678 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001679}
1680
edisonn@google.com3aa35552013-08-14 18:26:20 +00001681static SkPdfResult PdfOp_SCN(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001682 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1683}
1684
edisonn@google.com3aa35552013-08-14 18:26:20 +00001685static SkPdfResult PdfOp_scn(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001686 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1687}
1688
edisonn@google.com3aa35552013-08-14 18:26:20 +00001689static SkPdfResult PdfOp_G_g(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001690 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001691 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001692}
1693
edisonn@google.com3aa35552013-08-14 18:26:20 +00001694static SkPdfResult PdfOp_G(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001695 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1696}
1697
edisonn@google.com3aa35552013-08-14 18:26:20 +00001698static SkPdfResult PdfOp_g(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001699 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1700}
1701
edisonn@google.com3aa35552013-08-14 18:26:20 +00001702static SkPdfResult PdfOp_RG_rg(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001703 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1704 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1705 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001706
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001707 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001708 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com3aa35552013-08-14 18:26:20 +00001709 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001710}
1711
edisonn@google.com3aa35552013-08-14 18:26:20 +00001712static SkPdfResult PdfOp_RG(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001713 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1714}
1715
edisonn@google.com3aa35552013-08-14 18:26:20 +00001716static SkPdfResult PdfOp_rg(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001717 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1718}
1719
edisonn@google.com3aa35552013-08-14 18:26:20 +00001720static SkPdfResult PdfOp_K_k(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001721 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001722 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1723 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1724 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1725 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001726
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001727 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001728 // TODO(edisonn): Set color.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001729 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001730}
1731
edisonn@google.com3aa35552013-08-14 18:26:20 +00001732static SkPdfResult PdfOp_K(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001733 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1734}
1735
edisonn@google.com3aa35552013-08-14 18:26:20 +00001736static SkPdfResult PdfOp_k(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001737 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1738}
1739
edisonn@google.com3aa35552013-08-14 18:26:20 +00001740static SkPdfResult PdfOp_W(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001741 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1742 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1743
edisonn@google.com3aa35552013-08-14 18:26:20 +00001744 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001745}
1746
edisonn@google.com3aa35552013-08-14 18:26:20 +00001747static SkPdfResult PdfOp_W_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001748 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1749
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001750 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1751 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1752
edisonn@google.com3aa35552013-08-14 18:26:20 +00001753 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001754}
1755
edisonn@google.com3aa35552013-08-14 18:26:20 +00001756static SkPdfResult PdfOp_BX(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001757 *looper = new PdfCompatibilitySectionLooper();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001758 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001759}
1760
edisonn@google.com3aa35552013-08-14 18:26:20 +00001761static SkPdfResult PdfOp_EX(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001762#ifdef ASSERT_BAD_PDF_OPS
1763 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1764 // have the assert when testing good pdfs.
1765#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001766 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001767}
1768
edisonn@google.com3aa35552013-08-14 18:26:20 +00001769static SkPdfResult PdfOp_BI(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001770 *looper = new PdfInlineImageLooper();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001771 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001772}
1773
edisonn@google.com3aa35552013-08-14 18:26:20 +00001774static SkPdfResult PdfOp_ID(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001775#ifdef ASSERT_BAD_PDF_OPS
1776 SkASSERT(false); // must be processed in inline image looper, but let's
1777 // have the assert when testing good pdfs.
1778#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001779 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001780}
1781
edisonn@google.com3aa35552013-08-14 18:26:20 +00001782static SkPdfResult PdfOp_EI(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001783#ifdef ASSERT_BAD_PDF_OPS
1784 SkASSERT(false); // must be processed in inline image looper, but let's
1785 // have the assert when testing good pdfs.
1786#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001787 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001788}
1789
edisonn@google.com24cdf132013-07-30 16:06:12 +00001790
1791// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
edisonn@google.com33f11b62013-08-14 21:35:27 +00001792static SkPdfResult skpdfGraphicsStateApply_ca(SkPdfContext* pdfContext, double ca) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001793 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001794 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001795}
1796
edisonn@google.com33f11b62013-08-14 21:35:27 +00001797static SkPdfResult skpdfGraphicsStateApply_CA(SkPdfContext* pdfContext, double CA) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001798 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001799 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001800}
1801
edisonn@google.com33f11b62013-08-14 21:35:27 +00001802static SkPdfResult skpdfGraphicsStateApplyLW(SkPdfContext* pdfContext, double lineWidth) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001803 pdfContext->fGraphicsState.fLineWidth = lineWidth;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001804 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001805}
1806
edisonn@google.com33f11b62013-08-14 21:35:27 +00001807static SkPdfResult skpdfGraphicsStateApplyLC(SkPdfContext* pdfContext, int64_t lineCap) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001808 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001809 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001810}
1811
edisonn@google.com33f11b62013-08-14 21:35:27 +00001812static SkPdfResult skpdfGraphicsStateApplyLJ(SkPdfContext* pdfContext, int64_t lineJoin) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001813 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001814 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001815}
1816
edisonn@google.com33f11b62013-08-14 21:35:27 +00001817static SkPdfResult skpdfGraphicsStateApplyML(SkPdfContext* pdfContext, double miterLimit) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001818 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001819 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001820}
1821
1822// TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure
1823/*
18241) [ ] 0 No dash; solid, unbroken lines
18252) [3] 0 3 units on, 3 units off, …
18263) [2] 1 1 on, 2 off, 2 on, 2 off, …
18274) [2 1] 0 2 on, 1 off, 2 on, 1 off, …
18285) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, …
18296) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, …
1830 */
1831
edisonn@google.com33f11b62013-08-14 21:35:27 +00001832static SkPdfResult skpdfGraphicsStateApplyD(SkPdfContext* pdfContext, SkPdfArray* intervals, SkPdfNativeObject* phase) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001833 int cnt = intervals->size();
1834 if (cnt >= 256) {
1835 // TODO(edisonn): report error/warning, unsuported;
1836 // TODO(edisonn): alloc memory
edisonn@google.com3aa35552013-08-14 18:26:20 +00001837 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001838 }
1839 for (int i = 0; i < cnt; i++) {
1840 if (!intervals->objAtAIndex(i)->isNumber()) {
1841 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001842 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001843 }
1844 }
1845
edisonn@google.com24cdf132013-07-30 16:06:12 +00001846 double total = 0;
1847 for (int i = 0 ; i < cnt; i++) {
1848 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1849 total += pdfContext->fGraphicsState.fDashArray[i];
1850 }
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001851 if (cnt & 1) {
1852 if (cnt == 1) {
1853 pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0];
1854 cnt++;
1855 } else {
1856 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001857 return kNYI_SkPdfResult;
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001858 }
1859 }
1860 pdfContext->fGraphicsState.fDashArrayLength = cnt;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001861 pdfContext->fGraphicsState.fDashPhase = phase->scalarValue();
1862 if (pdfContext->fGraphicsState.fDashPhase == 0) {
1863 // other rules, changes?
edisonn@google.com063d7072013-08-16 15:05:08 +00001864 pdfContext->fGraphicsState.fDashPhase = SkDoubleToScalar(total);
edisonn@google.com24cdf132013-07-30 16:06:12 +00001865 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001866
edisonn@google.com3aa35552013-08-14 18:26:20 +00001867 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001868}
1869
edisonn@google.com33f11b62013-08-14 21:35:27 +00001870static SkPdfResult skpdfGraphicsStateApplyD(SkPdfContext* pdfContext, SkPdfArray* dash) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001871 // TODO(edisonn): verify input
1872 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1873 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001874 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001875 }
1876 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1));
1877}
1878
edisonn@google.com33f11b62013-08-14 21:35:27 +00001879static void skpdfGraphicsStateApplyFont(SkPdfContext* pdfContext, SkPdfArray* fontAndSize) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001880 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1881 // TODO(edisonn): report error/warning
1882 return;
1883 }
1884 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1885}
1886
1887
1888//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001889static SkPdfResult PdfOp_w(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001890 double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1891 return skpdfGraphicsStateApplyLW(pdfContext, lw);
1892}
1893
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001894//lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001895static SkPdfResult PdfOp_J(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com063d7072013-08-16 15:05:08 +00001896 // TODO(edisonn): round/ceil to int?
1897 int lc = (int)pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com24cdf132013-07-30 16:06:12 +00001898 return skpdfGraphicsStateApplyLC(pdfContext, lc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001899}
1900
1901//lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001902static SkPdfResult PdfOp_j(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com063d7072013-08-16 15:05:08 +00001903 // TODO(edisonn): round/ceil to int?
1904 int lj = (int)pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com24cdf132013-07-30 16:06:12 +00001905 return skpdfGraphicsStateApplyLJ(pdfContext, lj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001906}
1907
1908//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001909static SkPdfResult PdfOp_M(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001910 double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1911 return skpdfGraphicsStateApplyML(pdfContext, ml);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001912}
1913
1914//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1915//page 155).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001916static SkPdfResult PdfOp_d(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1917 SkPdfNativeObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1918 SkPdfNativeObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001919
edisonn@google.com24cdf132013-07-30 16:06:12 +00001920 if (!array->isArray()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001921 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001922 }
1923
1924 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001925}
1926
1927//intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001928static SkPdfResult PdfOp_ri(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001929 pdfContext->fObjectStack.pop();
1930
edisonn@google.com3aa35552013-08-14 18:26:20 +00001931 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001932}
1933
1934//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1935//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1936//fies the output device’s default flatness tolerance.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001937static SkPdfResult PdfOp_i(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001938 pdfContext->fObjectStack.pop();
1939
edisonn@google.com3aa35552013-08-14 18:26:20 +00001940 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001941}
1942
edisonn@google.come878e722013-07-29 19:10:58 +00001943SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1944
1945class InitBlendModes {
1946public:
1947 InitBlendModes() {
1948 // TODO(edisonn): use the python code generator?
1949 // TABLE 7.2 Standard separable blend modes
1950 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1951 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1952 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1953 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1954 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1955 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1956 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1957 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1958 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1959 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1960 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1961 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1962
1963 // TABLE 7.3 Standard nonseparable blend modes
1964 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1965 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1966 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1967 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1968 }
1969};
1970
1971InitBlendModes _gDummyInniter;
1972
edisonn@google.com33f11b62013-08-14 21:35:27 +00001973static SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
edisonn@google.come878e722013-07-29 19:10:58 +00001974 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1975 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1976 return mode;
1977 }
1978
1979 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1980}
1981
edisonn@google.com063d7072013-08-16 15:05:08 +00001982static void skpdfGraphicsStateApplyBM_name(SkPdfContext* pdfContext, const SkString& blendMode) {
1983 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.size());
edisonn@google.come878e722013-07-29 19:10:58 +00001984 if (mode <= SkXfermode::kLastMode) {
1985 pdfContext->fGraphicsState.fBlendModesLength = 1;
1986 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1987 } else {
1988 // TODO(edisonn): report unknown blend mode
1989 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001990}
1991
edisonn@google.com33f11b62013-08-14 21:35:27 +00001992static void skpdfGraphicsStateApplyBM_array(SkPdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001993 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
1994 // TODO(edisonn): report error/warning
1995 return;
1996 }
1997 SkXfermode::Mode modes[256];
1998 int cnt = blendModes->size();
1999 for (int i = 0; i < cnt; i++) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002000 SkPdfNativeObject* name = blendModes->objAtAIndex(i);
edisonn@google.come878e722013-07-29 19:10:58 +00002001 if (!name->isName()) {
2002 // TODO(edisonn): report error/warning
2003 return;
2004 }
2005 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
2006 if (mode > SkXfermode::kLastMode) {
2007 // TODO(edisonn): report error/warning
2008 return;
2009 }
2010 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002011
edisonn@google.come878e722013-07-29 19:10:58 +00002012 pdfContext->fGraphicsState.fBlendModesLength = cnt;
2013 for (int i = 0; i < cnt; i++) {
2014 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
2015 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002016}
2017
edisonn@google.com33f11b62013-08-14 21:35:27 +00002018static void skpdfGraphicsStateApplySMask_dict(SkPdfContext* pdfContext, SkPdfDictionary* sMask) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002019 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00002020 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002021 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00002022 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
2023 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
2024 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
2025 } else {
2026 // TODO (edisonn): report error/warning
2027 }
2028}
2029
edisonn@google.com063d7072013-08-16 15:05:08 +00002030static void skpdfGraphicsStateApplySMask_name(SkPdfContext* pdfContext, const SkString& sMask) {
2031 if (sMask.equals("None")) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002032 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002033 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002034 return;
2035 }
2036
edisonn@google.come878e722013-07-29 19:10:58 +00002037 //Next, get the ExtGState Dictionary from the Resource Dictionary:
2038 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
2039
2040 if (extGStateDictionary == NULL) {
2041#ifdef PDF_TRACE
2042 printf("ExtGState is NULL!\n");
2043#endif
2044 // TODO (edisonn): report error/warning
2045 return;
2046 }
2047
edisonn@google.com3aa35552013-08-14 18:26:20 +00002048 SkPdfNativeObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
edisonn@google.come878e722013-07-29 19:10:58 +00002049 if (!obj || !obj->isDictionary()) {
2050 // TODO (edisonn): report error/warning
2051 return;
2052 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002053
2054 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002055 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002056
edisonn@google.come878e722013-07-29 19:10:58 +00002057 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002058}
2059
edisonn@google.com33f11b62013-08-14 21:35:27 +00002060static void skpdfGraphicsStateApplyAIS(SkPdfContext* pdfContext, bool alphaSource) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002061 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
2062}
2063
2064
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002065//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
2066//the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002067static SkPdfResult PdfOp_gs(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2068 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002069
2070#ifdef PDF_TRACE
edisonn@google.com063d7072013-08-16 15:05:08 +00002071 SkString str;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002072#endif
2073
2074 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00002075 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002076
2077 if (extGStateDictionary == NULL) {
2078#ifdef PDF_TRACE
2079 printf("ExtGState is NULL!\n");
2080#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00002081 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002082 }
2083
edisonn@google.com3aa35552013-08-14 18:26:20 +00002084 SkPdfNativeObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002085
edisonn@google.com3aa35552013-08-14 18:26:20 +00002086 if (kNone_SkPdfNativeObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
2087 return kIgnoreError_SkPdfResult;
edisonn@google.com571c70b2013-07-10 17:09:50 +00002088 }
2089 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002090
2091 // TODO(edisonn): now load all those properties in graphic state.
2092 if (gs == NULL) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002093 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002094 }
2095
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002096 if (gs->has_LW()) {
2097 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
2098 }
2099
2100 if (gs->has_LC()) {
2101 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
2102 }
2103
2104 if (gs->has_LJ()) {
2105 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
2106 }
2107
2108 if (gs->has_ML()) {
2109 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
2110 }
2111
2112 if (gs->has_D()) {
2113 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
2114 }
2115
2116 if (gs->has_Font()) {
2117 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
2118 }
2119
2120 if (gs->has_BM()) {
2121 if (gs->isBMAName(pdfContext->fPdfDoc)) {
2122 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
2123 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
2124 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
2125 } else {
2126 // TODO(edisonn): report/warn
2127 }
2128 }
2129
2130 if (gs->has_SMask()) {
2131 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
2132 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
2133 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
2134 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
2135 } else {
2136 // TODO(edisonn): report/warn
2137 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002138 }
2139
2140 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002141 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002142 }
2143
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002144 if (gs->has_CA()) {
2145 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
2146 }
2147
2148 if (gs->has_AIS()) {
2149 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002150 }
2151
edisonn@google.com3aa35552013-08-14 18:26:20 +00002152 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002153}
2154
2155//charSpace Tc Set the character spacing, Tc
2156//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
2157//Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002158SkPdfResult PdfOp_Tc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002159 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002160 pdfContext->fGraphicsState.fCharSpace = charSpace;
2161
edisonn@google.com3aa35552013-08-14 18:26:20 +00002162 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002163}
2164
2165//wordSpace Tw Set the word spacing, T
2166//w
2167//, to wordSpace, which is a number expressed in unscaled
2168//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
2169//value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002170SkPdfResult PdfOp_Tw(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002171 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002172 pdfContext->fGraphicsState.fWordSpace = wordSpace;
2173
edisonn@google.com3aa35552013-08-14 18:26:20 +00002174 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002175}
2176
2177//scale Tz Set the horizontal scaling, Th
2178//, to (scale ˜ 100). scale is a number specifying the
2179//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002180static SkPdfResult PdfOp_Tz(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002181 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002182
edisonn@google.com3aa35552013-08-14 18:26:20 +00002183 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002184}
2185
2186//render Tr Set the text rendering mode, T
2187//mode, to render, which is an integer. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002188static SkPdfResult PdfOp_Tr(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002189 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002190
edisonn@google.com3aa35552013-08-14 18:26:20 +00002191 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002192}
2193//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
2194//units. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002195static SkPdfResult PdfOp_Ts(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002196 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002197
edisonn@google.com3aa35552013-08-14 18:26:20 +00002198 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002199}
2200
2201//wx wy d0
edisonn@google.com3aa35552013-08-14 18:26:20 +00002202static SkPdfResult PdfOp_d0(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002203 pdfContext->fObjectStack.pop();
2204 pdfContext->fObjectStack.pop();
2205
edisonn@google.com3aa35552013-08-14 18:26:20 +00002206 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002207}
2208
2209//wx wy llx lly urx ury d1
edisonn@google.com3aa35552013-08-14 18:26:20 +00002210static SkPdfResult PdfOp_d1(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002211 pdfContext->fObjectStack.pop();
2212 pdfContext->fObjectStack.pop();
2213 pdfContext->fObjectStack.pop();
2214 pdfContext->fObjectStack.pop();
2215 pdfContext->fObjectStack.pop();
2216 pdfContext->fObjectStack.pop();
2217
edisonn@google.com3aa35552013-08-14 18:26:20 +00002218 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002219}
2220
2221//name sh
edisonn@google.com3aa35552013-08-14 18:26:20 +00002222static SkPdfResult PdfOp_sh(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002223 pdfContext->fObjectStack.pop();
2224
edisonn@google.com3aa35552013-08-14 18:26:20 +00002225 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002226}
2227
2228//name Do
edisonn@google.com3aa35552013-08-14 18:26:20 +00002229static SkPdfResult PdfOp_Do(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2230 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002231
edisonn@google.com571c70b2013-07-10 17:09:50 +00002232 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002233
2234 if (xObject == NULL) {
2235#ifdef PDF_TRACE
2236 printf("XObject is NULL!\n");
2237#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00002238 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002239 }
2240
edisonn@google.com3aa35552013-08-14 18:26:20 +00002241 SkPdfNativeObject* value = xObject->get(name);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002242 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002243
2244#ifdef PDF_TRACE
2245// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002246// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002247#endif
2248
edisonn@google.com571c70b2013-07-10 17:09:50 +00002249 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002250}
2251
2252//tag MP Designate a marked-content point. tag is a name object indicating the role or
2253//significance of the point.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002254static SkPdfResult PdfOp_MP(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002255 pdfContext->fObjectStack.pop();
2256
edisonn@google.com3aa35552013-08-14 18:26:20 +00002257 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002258}
2259
2260//tag properties DP Designate a marked-content point with an associated property list. tag is a
2261//name object indicating the role or significance of the point; properties is
2262//either an inline dictionary containing the property list or a name object
2263//associated with it in the Properties subdictionary of the current resource
2264//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002265static SkPdfResult PdfOp_DP(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002266 pdfContext->fObjectStack.pop();
2267 pdfContext->fObjectStack.pop();
2268
edisonn@google.com3aa35552013-08-14 18:26:20 +00002269 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002270}
2271
2272//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
2273//tag is a name object indicating the role or significance of the sequence.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002274static SkPdfResult PdfOp_BMC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002275 pdfContext->fObjectStack.pop();
2276
edisonn@google.com3aa35552013-08-14 18:26:20 +00002277 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002278}
2279
2280//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
2281//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
2282//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.com3aa35552013-08-14 18:26:20 +00002283static SkPdfResult PdfOp_BDC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002284 pdfContext->fObjectStack.pop();
2285 pdfContext->fObjectStack.pop();
2286
edisonn@google.com3aa35552013-08-14 18:26:20 +00002287 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002288}
2289
2290//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002291static SkPdfResult PdfOp_EMC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2292 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002293}
2294
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002295static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002296 static bool gInitialized = false;
2297 if (gInitialized) {
2298 return;
2299 }
2300
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002301 gPdfOps.set("q", PdfOp_q);
2302 gPdfOps.set("Q", PdfOp_Q);
2303 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002304
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002305 gPdfOps.set("TD", PdfOp_TD);
2306 gPdfOps.set("Td", PdfOp_Td);
2307 gPdfOps.set("Tm", PdfOp_Tm);
2308 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002309
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002310 gPdfOps.set("m", PdfOp_m);
2311 gPdfOps.set("l", PdfOp_l);
2312 gPdfOps.set("c", PdfOp_c);
2313 gPdfOps.set("v", PdfOp_v);
2314 gPdfOps.set("y", PdfOp_y);
2315 gPdfOps.set("h", PdfOp_h);
2316 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002317
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002318 gPdfOps.set("S", PdfOp_S);
2319 gPdfOps.set("s", PdfOp_s);
2320 gPdfOps.set("f", PdfOp_f);
2321 gPdfOps.set("F", PdfOp_F);
2322 gPdfOps.set("f*", PdfOp_f_star);
2323 gPdfOps.set("B", PdfOp_B);
2324 gPdfOps.set("B*", PdfOp_B_star);
2325 gPdfOps.set("b", PdfOp_b);
2326 gPdfOps.set("b*", PdfOp_b_star);
2327 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002328
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002329 gPdfOps.set("BT", PdfOp_BT);
2330 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002331
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002332 gPdfOps.set("Tj", PdfOp_Tj);
2333 gPdfOps.set("'", PdfOp_quote);
2334 gPdfOps.set("\"", PdfOp_doublequote);
2335 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002336
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002337 gPdfOps.set("CS", PdfOp_CS);
2338 gPdfOps.set("cs", PdfOp_cs);
2339 gPdfOps.set("SC", PdfOp_SC);
2340 gPdfOps.set("SCN", PdfOp_SCN);
2341 gPdfOps.set("sc", PdfOp_sc);
2342 gPdfOps.set("scn", PdfOp_scn);
2343 gPdfOps.set("G", PdfOp_G);
2344 gPdfOps.set("g", PdfOp_g);
2345 gPdfOps.set("RG", PdfOp_RG);
2346 gPdfOps.set("rg", PdfOp_rg);
2347 gPdfOps.set("K", PdfOp_K);
2348 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002349
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002350 gPdfOps.set("W", PdfOp_W);
2351 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002352
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002353 gPdfOps.set("BX", PdfOp_BX);
2354 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002355
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002356 gPdfOps.set("BI", PdfOp_BI);
2357 gPdfOps.set("ID", PdfOp_ID);
2358 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002359
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002360 gPdfOps.set("w", PdfOp_w);
2361 gPdfOps.set("J", PdfOp_J);
2362 gPdfOps.set("j", PdfOp_j);
2363 gPdfOps.set("M", PdfOp_M);
2364 gPdfOps.set("d", PdfOp_d);
2365 gPdfOps.set("ri", PdfOp_ri);
2366 gPdfOps.set("i", PdfOp_i);
2367 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002368
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002369 gPdfOps.set("Tc", PdfOp_Tc);
2370 gPdfOps.set("Tw", PdfOp_Tw);
2371 gPdfOps.set("Tz", PdfOp_Tz);
2372 gPdfOps.set("TL", PdfOp_TL);
2373 gPdfOps.set("Tf", PdfOp_Tf);
2374 gPdfOps.set("Tr", PdfOp_Tr);
2375 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002376
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002377 gPdfOps.set("d0", PdfOp_d0);
2378 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002379
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002380 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002381
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002382 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002383
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002384 gPdfOps.set("MP", PdfOp_MP);
2385 gPdfOps.set("DP", PdfOp_DP);
2386 gPdfOps.set("BMC", PdfOp_BMC);
2387 gPdfOps.set("BDC", PdfOp_BDC);
2388 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002389
2390 gInitialized = true;
2391}
2392
2393class InitPdfOps {
2394public:
2395 InitPdfOps() {
2396 initPdfOperatorRenderes();
2397 }
2398};
2399
2400InitPdfOps gInitPdfOps;
2401
2402void reportPdfRenderStats() {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002403 for (int i = 0 ; i < kCount_SkPdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002404 SkTDict<int>::Iter iter(gRenderStats[i]);
2405 const char* key;
2406 int value = 0;
2407 while ((key = iter.next(&value)) != NULL) {
2408 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002409 }
2410 }
2411}
2412
edisonn@google.com3aa35552013-08-14 18:26:20 +00002413SkPdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002414 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002415 {
2416 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002417 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002418 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002419 // caller, main work is done by pdfOperatorRenderer(...)
2420 PdfTokenLooper* childLooper = NULL;
edisonn@google.com3aa35552013-08-14 18:26:20 +00002421 SkPdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002422
2423 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002424 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2425 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002426
2427 if (childLooper) {
2428 childLooper->setUp(this);
2429 childLooper->loop();
2430 delete childLooper;
2431 }
2432 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002433 int cnt = 0;
edisonn@google.com3aa35552013-08-14 18:26:20 +00002434 gRenderStats[kUnsupported_SkPdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2435 gRenderStats[kUnsupported_SkPdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002436 }
2437 }
2438 else if (token.fType == kObject_TokenType)
2439 {
2440 fPdfContext->fObjectStack.push( token.fObject );
2441 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002442 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002443 // TODO(edisonn): deine or use assert not reached
edisonn@google.com3aa35552013-08-14 18:26:20 +00002444 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002445 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00002446 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002447}
2448
2449void PdfMainLooper::loop() {
2450 PdfToken token;
2451 while (readToken(fTokenizer, &token)) {
2452 consumeToken(token);
2453 }
2454}
2455
edisonn@google.com3aa35552013-08-14 18:26:20 +00002456SkPdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002457 SkASSERT(false);
edisonn@google.com3aa35552013-08-14 18:26:20 +00002458 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002459}
2460
2461void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002462 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002463}
2464
edisonn@google.com3aa35552013-08-14 18:26:20 +00002465SkPdfResult PdfInlineImageLooper::done() {
2466 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002467}
2468
edisonn@google.com3aa35552013-08-14 18:26:20 +00002469SkPdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002470 return fParent->consumeToken(token);
2471}
2472
2473void PdfCompatibilitySectionLooper::loop() {
2474 // TODO(edisonn): save stacks position, or create a new stack?
2475 // TODO(edisonn): what happens if we pop out more variables then when we started?
2476 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2477 // pop-ing too much will not affect outside the section.
2478 PdfToken token;
2479 while (readToken(fTokenizer, &token)) {
2480 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2481 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2482 looper->setUp(this);
2483 looper->loop();
2484 delete looper;
2485 } else {
2486 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2487 fParent->consumeToken(token);
2488 }
2489 }
2490 // TODO(edisonn): restore stack.
2491}
2492
2493// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2494// TODO(edisonn): Add API for Forms viewing and editing
2495// e.g. SkBitmap getPage(int page);
2496// int formsCount();
2497// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2498// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2499// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2500// resources needed, so the preview is fast.
2501// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2502// references automatically.
2503
edisonn@google.com3aa35552013-08-14 18:26:20 +00002504SkPdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002505
edisonn@google.com444e25a2013-07-11 15:20:50 +00002506bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002507 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002508 return false;
2509 }
2510
edisonn@google.com222382b2013-07-10 22:33:10 +00002511 if (page < 0 || page >= pages()) {
2512 return false;
2513 }
2514
edisonn@google.com3aa35552013-08-14 18:26:20 +00002515 SkPdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002516
edisonn@google.com222382b2013-07-10 22:33:10 +00002517 pdfContext.fOriginalMatrix = SkMatrix::I();
2518 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2519
2520 gPdfContext = &pdfContext;
2521
2522 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002523 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002524 SkScalar w = dst.width();
2525 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002526
edisonn@google.com444e25a2013-07-11 15:20:50 +00002527 SkScalar wp = fPdfDoc->MediaBox(page).width();
2528 SkScalar hp = fPdfDoc->MediaBox(page).height();
2529
2530 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 +00002531// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2532
2533 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2534 // TODO(edisonn): add flagg for no clipping.
2535 // Use larger image to make sure we do not draw anything outside of page
2536 // could be used in tests.
2537
2538#ifdef PDF_DEBUG_3X
2539 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)};
2540#else
2541 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2542#endif
2543 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2544 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2545
2546 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2547 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2548
2549 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2550 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2551
2552 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2553 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2554
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002555 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
edisonn@google.com0f901902013-08-07 11:56:16 +00002556 pdfContext.fGraphicsState.fContentStreamMatrix = pdfContext.fOriginalMatrix;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002557 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2558 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002559
edisonn@google.com222382b2013-07-10 22:33:10 +00002560#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002561 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002562#endif
2563
edisonn@google.com444e25a2013-07-11 15:20:50 +00002564 canvas->setMatrix(pdfContext.fOriginalMatrix);
2565
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002566 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2567
2568 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002569// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002570// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002571// canvas->drawRect(rect, paint);
2572
edisonn@google.com222382b2013-07-10 22:33:10 +00002573
2574 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002575 return true;
2576}
edisonn@google.com222382b2013-07-10 22:33:10 +00002577
2578bool SkPdfRenderer::load(const SkString inputFileName) {
2579 unload();
2580
2581 // TODO(edisonn): create static function that could return NULL if there are errors
edisonn@google.com3aa35552013-08-14 18:26:20 +00002582 fPdfDoc = new SkPdfNativeDoc(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002583 if (fPdfDoc->pages() == 0) {
2584 delete fPdfDoc;
2585 fPdfDoc = NULL;
2586 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002587
2588 return fPdfDoc != NULL;
2589}
2590
edisonn@google.com147adb12013-07-24 15:56:19 +00002591bool SkPdfRenderer::load(SkStream* stream) {
2592 unload();
2593
2594 // TODO(edisonn): create static function that could return NULL if there are errors
edisonn@google.com3aa35552013-08-14 18:26:20 +00002595 fPdfDoc = new SkPdfNativeDoc(stream);
edisonn@google.com147adb12013-07-24 15:56:19 +00002596 if (fPdfDoc->pages() == 0) {
2597 delete fPdfDoc;
2598 fPdfDoc = NULL;
2599 }
2600
2601 return fPdfDoc != NULL;
2602}
2603
2604
edisonn@google.com222382b2013-07-10 22:33:10 +00002605int SkPdfRenderer::pages() const {
2606 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2607}
2608
2609void SkPdfRenderer::unload() {
2610 delete fPdfDoc;
2611 fPdfDoc = NULL;
2612}
2613
2614SkRect SkPdfRenderer::MediaBox(int page) const {
2615 SkASSERT(fPdfDoc);
2616 return fPdfDoc->MediaBox(page);
2617}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002618
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002619size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002620 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2621}
edisonn@google.com147adb12013-07-24 15:56:19 +00002622
2623bool SkPDFNativeRenderToBitmap(SkStream* stream,
2624 SkBitmap* output,
2625 int page,
2626 SkPdfContent content,
2627 double dpi) {
2628 SkASSERT(page >= 0);
2629 SkPdfRenderer renderer;
2630 renderer.load(stream);
2631 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2632 return false;
2633 }
2634
2635 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2636
2637 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2638 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2639
2640 rect = SkRect::MakeWH(width, height);
2641
2642 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2643
2644 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2645 SkCanvas canvas(device);
2646
2647 return renderer.renderPage(page, &canvas, rect);
2648}