blob: bd95a982baba4046fa802bc185a1faffdfbdb052 [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#include <cstdio>
24#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000025#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000026
edisonn@google.com3aa35552013-08-14 18:26:20 +000027extern "C" SkPdfContext* gPdfContext;
edisonn@google.com15b11182013-07-11 14:43:15 +000028extern "C" SkBitmap* gDumpBitmap;
29extern "C" SkCanvas* gDumpCanvas;
30
edisonn@google.com131d4ee2013-06-26 17:48:12 +000031__SK_FORCE_IMAGE_DECODER_LINKING;
32
33// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
34// keep for each object pos in file
35// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
36
37// TODO(edisonn): security - validate all the user input, all pdf!
38
edisonn@google.com6e49c342013-06-27 20:03:43 +000039// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
40// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000041
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
43/*
44#ifdef PDF_TRACE
45 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000046 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000047 printf("Print Tf Resources: %s\n", str.c_str());
48#endif
49 */
50
edisonn@google.com131d4ee2013-06-26 17:48:12 +000051#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000052#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000053#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000054
edisonn@google.com131d4ee2013-06-26 17:48:12 +000055#include "SkPdfUtils.h"
56
57#include "SkPdfFont.h"
58
edisonn@google.com131d4ee2013-06-26 17:48:12 +000059/*
60 * TODO(edisonn):
61 * - all font types and all ppdf font features
62 * - word spacing
63 * - load font for baidu.pdf
64 * - load font for youtube.pdf
65 * - parser for pdf from the definition already available in pdfspec_autogen.py
66 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000067 * - 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 +000068 * - load gs/ especially smask and already known prop (skp) ... in progress
69 * - wrapper on classes for customizations? e.g.
70 * SkPdfPageObjectVanila - has only the basic loaders/getters
71 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
72 * need to find a nice object model for all this with constructors and factories
73 * - deal with inheritable automatically ?
74 * - deal with specific type in spec directly, add all dictionary types to known types
75*/
76
77using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000078
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000079NotOwnedString strings_DeviceRGB;
80NotOwnedString strings_DeviceCMYK;
edisonn@google.com222382b2013-07-10 22:33:10 +000081
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000082class StringsInit {
83public:
84 StringsInit() {
85 NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB");
86 NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK");
87 }
88};
89
90StringsInit gStringsInit;
edisonn@google.com222382b2013-07-10 22:33:10 +000091
92// TODO(edisonn): Document PdfTokenLooper and subclasses.
93class PdfTokenLooper {
94protected:
95 PdfTokenLooper* fParent;
96 SkPdfNativeTokenizer* fTokenizer;
edisonn@google.com3aa35552013-08-14 18:26:20 +000097 SkPdfContext* fPdfContext;
edisonn@google.com222382b2013-07-10 22:33:10 +000098 SkCanvas* fCanvas;
99
100public:
101 PdfTokenLooper(PdfTokenLooper* parent,
102 SkPdfNativeTokenizer* tokenizer,
edisonn@google.com3aa35552013-08-14 18:26:20 +0000103 SkPdfContext* pdfContext,
edisonn@google.com222382b2013-07-10 22:33:10 +0000104 SkCanvas* canvas)
105 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
106
107 virtual ~PdfTokenLooper() {}
108
edisonn@google.com3aa35552013-08-14 18:26:20 +0000109 virtual SkPdfResult consumeToken(PdfToken& token) = 0;
edisonn@google.com222382b2013-07-10 22:33:10 +0000110 virtual void loop() = 0;
111
112 void setUp(PdfTokenLooper* parent) {
113 fParent = parent;
114 fTokenizer = parent->fTokenizer;
115 fPdfContext = parent->fPdfContext;
116 fCanvas = parent->fCanvas;
117 }
edisonn@google.com78b38b12013-07-15 18:20:58 +0000118
119 SkPdfNativeTokenizer* tokenizer() { return fTokenizer; }
edisonn@google.com222382b2013-07-10 22:33:10 +0000120};
121
122class PdfMainLooper : public PdfTokenLooper {
123public:
124 PdfMainLooper(PdfTokenLooper* parent,
125 SkPdfNativeTokenizer* tokenizer,
edisonn@google.com3aa35552013-08-14 18:26:20 +0000126 SkPdfContext* pdfContext,
edisonn@google.com222382b2013-07-10 22:33:10 +0000127 SkCanvas* canvas)
128 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
129
edisonn@google.com3aa35552013-08-14 18:26:20 +0000130 virtual SkPdfResult consumeToken(PdfToken& token);
edisonn@google.com222382b2013-07-10 22:33:10 +0000131 virtual void loop();
132};
133
134class PdfInlineImageLooper : public PdfTokenLooper {
135public:
136 PdfInlineImageLooper()
137 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
138
edisonn@google.com3aa35552013-08-14 18:26:20 +0000139 virtual SkPdfResult consumeToken(PdfToken& token);
edisonn@google.com222382b2013-07-10 22:33:10 +0000140 virtual void loop();
edisonn@google.com3aa35552013-08-14 18:26:20 +0000141 SkPdfResult done();
edisonn@google.com222382b2013-07-10 22:33:10 +0000142};
143
144class PdfCompatibilitySectionLooper : public PdfTokenLooper {
145public:
146 PdfCompatibilitySectionLooper()
147 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
148
edisonn@google.com3aa35552013-08-14 18:26:20 +0000149 virtual SkPdfResult consumeToken(PdfToken& token);
edisonn@google.com222382b2013-07-10 22:33:10 +0000150 virtual void loop();
151};
152
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000153// Utilities
154static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
155 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
156
157 bitmap->allocPixels();
158 bitmap->eraseColor(color);
159}
160
161// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000162static int GetColorSpaceComponents(NotOwnedString& colorSpace) {
163 if (colorSpace.equals("DeviceCMYK")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000164 return 4;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000165 } else if (colorSpace.equals("DeviceGray") ||
166 colorSpace.equals("CalGray") ||
167 colorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000168 return 1;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000169 } else if (colorSpace.equals("DeviceRGB") ||
170 colorSpace.equals("CalRGB") ||
171 colorSpace.equals("Lab")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000172 return 3;
173 } else {
174 return 0;
175 }
176}
177
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000178SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000179 SkMatrix matrix;
180 matrix.setAll(SkDoubleToScalar(array[0]),
181 SkDoubleToScalar(array[2]),
182 SkDoubleToScalar(array[4]),
183 SkDoubleToScalar(array[1]),
184 SkDoubleToScalar(array[3]),
185 SkDoubleToScalar(array[5]),
186 SkDoubleToScalar(0),
187 SkDoubleToScalar(0),
188 SkDoubleToScalar(1));
189
190 return matrix;
191}
192
193SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
194 double array[6];
195
196 // TODO(edisonn): security issue, ret if size() != 6
197 for (int i = 0; i < 6; i++) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000198 const SkPdfNativeObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000199 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000200 return SkMatrix::I(); // TODO(edisonn): report issue
201 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000202 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000203 }
204
205 return SkMatrixFromPdfMatrix(array);
206}
207
edisonn@google.com222382b2013-07-10 22:33:10 +0000208
edisonn@google.com3aa35552013-08-14 18:26:20 +0000209extern "C" SkPdfNativeDoc* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000210SkBitmap* gDumpBitmap = NULL;
211SkCanvas* gDumpCanvas = NULL;
212char gLastKeyword[100] = "";
213int gLastOpKeyword = -1;
214char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
215int gReadOp = 0;
216
217
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000218#ifdef PDF_TRACE_DIFF_IN_PNG
219static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000220 return true;
221 if (*pdfOp == '\0') return false;
222
223 char markedPdfOp[100] = ",";
224 strcat(markedPdfOp, pdfOp);
225 strcat(markedPdfOp, ",");
226
227 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
228}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000229#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000230
edisonn@google.com222382b2013-07-10 22:33:10 +0000231
232
edisonn@google.com3aa35552013-08-14 18:26:20 +0000233// TODO(edisonn): Pass SkPdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000234static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000235 bool ret = fTokenizer->readToken(token);
236
237 gReadOp++;
edisonn@google.com0f901902013-08-07 11:56:16 +0000238 gLastOpKeyword++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000239#ifdef PDF_TRACE_DIFF_IN_PNG
240 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
241 // the numbar and name of last operation, so the file name will reflect op that changed.
edisonn@google.com0f901902013-08-07 11:56:16 +0000242 if (gLastKeyword[0] && hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000243 gDumpCanvas->flush();
244
245 SkBitmap bitmap;
246 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
247
248 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
249
250 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
251 SkCanvas canvas(device);
252
253 // draw context stuff here
254 SkPaint blueBorder;
255 blueBorder.setColor(SK_ColorBLUE);
256 blueBorder.setStyle(SkPaint::kStroke_Style);
257 blueBorder.setTextSize(SkDoubleToScalar(20));
258
259 SkString str;
260
261 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
262 if (clipStack) {
263 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
264 const SkClipStack::Element* elem;
265 double y = 0;
266 int total = 0;
edisonn@google.com91ce6982013-08-05 20:45:40 +0000267 while ((elem = iter.next()) != NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000268 total++;
269 y += 30;
270
271 switch (elem->getType()) {
272 case SkClipStack::Element::kRect_Type:
273 canvas.drawRect(elem->getRect(), blueBorder);
274 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
275 break;
276 case SkClipStack::Element::kPath_Type:
277 canvas.drawPath(elem->getPath(), blueBorder);
278 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
279 break;
280 case SkClipStack::Element::kEmpty_Type:
281 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
282 break;
283 default:
284 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
285 break;
286 }
287 }
288
289 y += 30;
290 str.printf("Number of clips in stack: %i", total);
291 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
292 }
293
294 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
295 SkPath clipPath;
296 if (clipRegion.getBoundaryPath(&clipPath)) {
297 SkPaint redBorder;
298 redBorder.setColor(SK_ColorRED);
299 redBorder.setStyle(SkPaint::kStroke_Style);
300 canvas.drawPath(clipPath, redBorder);
301 }
302
303 canvas.flush();
304
305 SkString out;
306
307 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
308 // ... and other properties, to be able to debug th code easily
309
310 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
311 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
312 }
edisonn@google.com0f901902013-08-07 11:56:16 +0000313
314 if (ret && token->fType == kKeyword_TokenType && token->fKeyword && token->fKeywordLength > 0 && token->fKeywordLength < 100) {
315 strncpy(gLastKeyword, token->fKeyword, token->fKeywordLength);
316 gLastKeyword[token->fKeywordLength] = '\0';
317 } else {
318 gLastKeyword[0] = '\0';
319 }
320
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000321#endif
322
323 return ret;
324}
325
326
327
edisonn@google.com3aa35552013-08-14 18:26:20 +0000328typedef SkPdfResult (*PdfOperatorRenderer)(SkPdfContext*, SkCanvas*, PdfTokenLooper**);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000329
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000330SkTDict<PdfOperatorRenderer> gPdfOps(100);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000331
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000332
333template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
334public:
335 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
336};
337
edisonn@google.com3aa35552013-08-14 18:26:20 +0000338SkTDictWithDefaultConstructor<int> gRenderStats[kCount_SkPdfResult];
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000339
edisonn@google.com3aa35552013-08-14 18:26:20 +0000340const char* gRenderStatsNames[kCount_SkPdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000341 "Success",
342 "Partially implemented",
343 "Not yet implemented",
344 "Ignore Error",
345 "Error",
346 "Unsupported/Unknown"
347};
348
edisonn@google.com3aa35552013-08-14 18:26:20 +0000349static SkPdfResult DrawText(SkPdfContext* pdfContext,
350 const SkPdfNativeObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000351 SkCanvas* canvas)
352{
353
354 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
355 if (skfont == NULL) {
356 skfont = SkPdfFont::Default();
357 }
358
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000359
edisonn@google.com571c70b2013-07-10 17:09:50 +0000360 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000361 // TODO(edisonn): report warning
edisonn@google.com3aa35552013-08-14 18:26:20 +0000362 return kIgnoreError_SkPdfResult;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000363 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000364 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000365
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000366 SkUnencodedText binary(str);
367
368 SkDecodedText decoded;
369
370 if (skfont->encoding() == NULL) {
371 // TODO(edisonn): report warning
edisonn@google.com3aa35552013-08-14 18:26:20 +0000372 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000373 }
374
375 skfont->encoding()->decodeText(binary, &decoded);
376
377 SkPaint paint;
378 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
379 // Or maybe just not call setTextSize at all?
380 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
381 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
382 }
383
384// if (fCurFont && fCurFont->GetFontScale() != 0) {
385// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
386// }
387
388 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
389
edisonn@google.com6e49c342013-06-27 20:03:43 +0000390 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000391
edisonn@google.com3aa35552013-08-14 18:26:20 +0000392 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000393}
394
395// TODO(edisonn): create header files with declarations!
edisonn@google.com3aa35552013-08-14 18:26:20 +0000396SkPdfResult PdfOp_q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
397SkPdfResult PdfOp_Q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
398SkPdfResult PdfOp_Tw(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
399SkPdfResult PdfOp_Tc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000400
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000401// TODO(edisonn): perf!!!
402
403static SkColorTable* getGrayColortable() {
404 static SkColorTable* grayColortable = NULL;
405 if (grayColortable == NULL) {
406 SkPMColor* colors = new SkPMColor[256];
407 for (int i = 0 ; i < 256; i++) {
408 colors[i] = SkPreMultiplyARGB(255, i, i, i);
409 }
410 grayColortable = new SkColorTable(colors, 256);
411 }
412 return grayColortable;
413}
414
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000415static SkBitmap* transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000416 int width, int height, int bytesPerLine,
417 int bpc, const std::string& colorSpace,
418 bool transparencyMask) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000419 SkBitmap* bitmap = new SkBitmap();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000420
edisonn@google.com571c70b2013-07-10 17:09:50 +0000421 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000422//#define MAX_COMPONENTS 10
423
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000424 // TODO(edisonn): assume start of lines are aligned at 32 bits?
425 // Is there a faster way to load the uncompressed stream into a bitmap?
426
427 // minimal support for now
428 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
429 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
430
431 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000432 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000433 for (int w = 0 ; w < width; w++) {
434 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
435 uncompressedStream[3 * w + 1],
436 uncompressedStream[3 * w + 2]);
437 i++;
438 }
439 uncompressedStream += bytesPerLine;
440 }
441
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000442 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
443 bitmap->setPixels(uncompressedStreamArgb);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000444 }
445 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
446 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
447
448 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000449 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000450 for (int w = 0 ; w < width; w++) {
451 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
452 uncompressedStream[w];
453 i++;
454 }
455 uncompressedStream += bytesPerLine;
456 }
457
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000458 bitmap->setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000459 width, height);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000460 bitmap->setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000461 }
462
463 // TODO(edisonn): Report Warning, NYI, or error
464 return bitmap;
465}
466
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000467// utils
468
469// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
470// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
471// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
472// skia format, through a table
473
474// this functions returns the image, it does not look at the smask.
475
edisonn@google.com3aa35552013-08-14 18:26:20 +0000476static SkBitmap* getImageFromObjectCore(SkPdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000477 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000478 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000479 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000480 }
481
edisonn@google.com33f11b62013-08-14 21:35:27 +0000482 int bpc = (int)image->BitsPerComponent(pdfContext->fPdfDoc);
483 int width = (int)image->Width(pdfContext->fPdfDoc);
484 int height = (int)image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000485 std::string colorSpace = "DeviceRGB";
486
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000487 bool indexed = false;
488 SkPMColor colors[256];
489 int cnt = 0;
490
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000491 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000492 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
493 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000494 } else if (image->isColorSpaceAArray(pdfContext->fPdfDoc)) {
495 SkPdfArray* array = image->getColorSpaceAsArray(pdfContext->fPdfDoc);
496 if (array && array->size() == 4 && array->objAtAIndex(0)->isName("Indexed") &&
497 (array->objAtAIndex(1)->isName("DeviceRGB") || array->objAtAIndex(1)->isName("RGB")) &&
498 array->objAtAIndex(2)->isInteger() &&
499 array->objAtAIndex(3)->isHexString()
500 ) {
501 // TODO(edisonn): suport only DeviceRGB for now.
502 indexed = true;
edisonn@google.com33f11b62013-08-14 21:35:27 +0000503 cnt = (int)array->objAtAIndex(2)->intValue() + 1;
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000504 if (cnt > 256) {
505 // TODO(edionn): report NYIs
506 return NULL;
507 }
508 SkColorTable colorTable(cnt);
509 NotOwnedString data = array->objAtAIndex(3)->strRef();
510 if (data.fBytes != (unsigned int)cnt * 3) {
511 // TODO(edionn): report error/warning
512 return NULL;
513 }
514 for (int i = 0 ; i < cnt; i++) {
515 colors[i] = SkPreMultiplyARGB(0xff, data.fBuffer[3 * i], data.fBuffer[3 * i + 1], data.fBuffer[3 * i + 2]);
516 }
517 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000518 }
519
520/*
521 bool imageMask = image->imageMask();
522
523 if (imageMask) {
524 if (bpc != 0 && bpc != 1) {
525 // TODO(edisonn): report warning to be used in testing.
526 return SkBitmap();
527 }
528 bpc = 1;
529 }
530*/
531
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000532 const unsigned char* uncompressedStream = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000533 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000534
edisonn@google.com571c70b2013-07-10 17:09:50 +0000535 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000536
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000537 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000538 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000539 // TODO(edisonn): report warning to be used in testing.
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000540 return NULL;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000541 }
542
edisonn@google.com571c70b2013-07-10 17:09:50 +0000543 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
544
545 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
546 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
547 (streamDict->isFilterAArray(NULL) &&
548 streamDict->getFilterAsArray(NULL)->size() > 0 &&
549 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
550 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000551 SkBitmap* bitmap = new SkBitmap();
552 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, bitmap);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000553 return bitmap;
554 }
555
556
557
558 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
559// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
560// obj.GetDictionary().GetKey(PdfName("Filter")));
561// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
562// value = resolveReferenceObject(pdfContext->fPdfDoc,
563// &value->GetArray()[0]);
564// }
565// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
566// SkStream stream = SkStream::
567// SkImageDecoder::Factory()
568// }
569
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000570 // TODO(edisonn): assumes RGB for now, since it is the only onwe implemented
571 if (indexed) {
572 SkBitmap* bitmap = new SkBitmap();
573 bitmap->setConfig(SkBitmap::kIndex8_Config, width, height);
574 SkColorTable* colorTable = new SkColorTable(colors, cnt);
575 bitmap->setPixels((void*)uncompressedStream, colorTable);
576 return bitmap;
577 }
578
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000579 int bytesPerLine = (int)(uncompressedStreamLength / height);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000580#ifdef PDF_TRACE
581 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000582 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000583 }
584#endif
585
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000586 SkBitmap* bitmap = transferImageStreamToBitmap(
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000587 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000588 (int)width, (int)height, bytesPerLine,
589 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000590 transparencyMask);
591
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000592 return bitmap;
593}
594
edisonn@google.com3aa35552013-08-14 18:26:20 +0000595static SkBitmap* getImageFromObject(SkPdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000596 if (!transparencyMask) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000597 if (!image->hasData(SkPdfNativeObject::kBitmap_Data)) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000598 SkBitmap* bitmap = getImageFromObjectCore(pdfContext, image, transparencyMask);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000599 image->setData(bitmap, SkPdfNativeObject::kBitmap_Data);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000600 }
edisonn@google.com3aa35552013-08-14 18:26:20 +0000601 return (SkBitmap*) image->data(SkPdfNativeObject::kBitmap_Data);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000602 } else {
603 return getImageFromObjectCore(pdfContext, image, transparencyMask);
604 }
605}
606
edisonn@google.com3aa35552013-08-14 18:26:20 +0000607static SkBitmap* getSmaskFromObject(SkPdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000608 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000609
610 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000611 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000612 }
613
614 // TODO(edisonn): implement GS SMask. Default to empty right now.
615 return pdfContext->fGraphicsState.fSMask;
616}
617
edisonn@google.com3aa35552013-08-14 18:26:20 +0000618static SkPdfResult doXObject_Image(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000619 if (skpdfimage == NULL) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000620 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000621 }
622
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000623 SkBitmap* image = getImageFromObject(pdfContext, skpdfimage, false);
624 SkBitmap* sMask = getSmaskFromObject(pdfContext, skpdfimage);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000625
626 canvas->save();
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000627 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000628
edisonn@google.com571c70b2013-07-10 17:09:50 +0000629 SkScalar z = SkIntToScalar(0);
630 SkScalar one = SkIntToScalar(1);
631
632 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
633 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
634 SkMatrix flip;
635 SkAssertResult(flip.setPolyToPoly(from, to, 4));
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000636 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000637 solveImageFlip.preConcat(flip);
638 canvas->setMatrix(solveImageFlip);
edisonn@google.com0f901902013-08-07 11:56:16 +0000639
640#ifdef PDF_TRACE
641 SkPoint final[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
642 solveImageFlip.mapPoints(final, 4);
643 printf("IMAGE rect = ");
644 for (int i = 0; i < 4; i++) {
645 printf("(%f %f) ", SkScalarToDouble(final[i].x()), SkScalarToDouble(final[i].y()));
646 }
647 printf("\n");
648#endif // PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +0000649
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000650 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
651
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000652 // TODO(edisonn): soft mask type? alpha/luminosity.
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000653 SkPaint paint;
654 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
655
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000656 if (!sMask || sMask->empty()) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000657 canvas->drawBitmapRect(*image, dst, &paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000658 } else {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000659 canvas->saveLayer(&dst, &paint);
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000660 canvas->drawBitmapRect(*image, dst, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000661 SkPaint xfer;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000662 // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf?
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000663 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000664 canvas->drawBitmapRect(*sMask, dst, &xfer);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000665 canvas->restore();
666 }
667
668 canvas->restore();
669
edisonn@google.com3aa35552013-08-14 18:26:20 +0000670 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000671}
672
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000673//TODO(edisonn): options for implementing isolation and knockout
674// 1) emulate them (current solution)
675// PRO: simple
676// CON: will need to use readPixels, which means serious perf issues
677// 2) Compile a plan for an array of matrixes, compose the result at the end
678// PRO: might be faster then 1, no need to readPixels
679// CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan
680// on average, a load with empty draw is 100ms on all the skps we have, for complete sites
681// 3) support them natively in SkCanvas
682// PRO: simple
683// CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1)
684// 4) compile a plan using pathops, and render once without any fancy rules with backdrop
685// PRO: simple, fast
686// CON: pathops must be bug free first + time to compute new paths
687// pay a price at loading pdf to compute a pdf draw plan
688// 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 +0000689// 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 +0000690
691
692// TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree!
693// TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed
694
695
696
edisonn@google.com3aa35552013-08-14 18:26:20 +0000697static void doGroup_before(SkPdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000698 SkRect bboxOrig = bbox;
699 SkBitmap backdrop;
700 bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc);
701// bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000702 SkPaint paint;
703 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
704 canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000705}
706
edisonn@google.com251176e2013-08-01 13:24:00 +0000707// TODO(edisonn): non isolation implemented in skia
edisonn@google.com3aa35552013-08-14 18:26:20 +0000708//static void doGroup_after(SkPdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup) {
edisonn@google.com251176e2013-08-01 13:24:00 +0000709// if not isolated
710// canvas->drawBitmapRect(backdrop, bboxOrig, NULL);
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000711//}
712
edisonn@google.com3aa35552013-08-14 18:26:20 +0000713static SkPdfResult doXObject_Form(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000714 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000715 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000716 }
717
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000718 if (!skobj->has_BBox()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000719 return kIgnoreError_SkPdfResult;
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000720 }
721
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000722 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000723
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000724
edisonn@google.com571c70b2013-07-10 17:09:50 +0000725 if (skobj->Resources(pdfContext->fPdfDoc)) {
726 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000727 }
728
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000729 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000730
edisonn@google.com571c70b2013-07-10 17:09:50 +0000731 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000732 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000733 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
734 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
735 pdfContext->fGraphicsState.fMatrixTm = matrix;
736 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000737 // TODO(edisonn) reset matrixTm and matricTlm also?
738 }
739
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000740 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com0f901902013-08-07 11:56:16 +0000741 pdfContext->fGraphicsState.fContentStreamMatrix = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000742
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000743 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000744
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000745 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
746 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000747
748 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
749 // For this PdfContentsTokenizer needs to be extended.
750
edisonn@google.come878e722013-07-29 19:10:58 +0000751 // This is a group?
752 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000753 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
754 doGroup_before(pdfContext, canvas, bbox, tgroup, false);
edisonn@google.come878e722013-07-29 19:10:58 +0000755 }
756
edisonn@google.com571c70b2013-07-10 17:09:50 +0000757 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000758
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000759 SkPdfNativeTokenizer* tokenizer =
760 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000761 if (tokenizer != NULL) {
762 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
763 looper.loop();
764 delete tokenizer;
765 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000766
767 // TODO(edisonn): should we restore the variable stack at the same state?
768 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000769
770 if (skobj->has_Group()) {
771 canvas->restore();
772 }
773
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000774 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000775 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000776}
777
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000778
779// TODO(edisonn): Extract a class like ObjWithStream
edisonn@google.com3aa35552013-08-14 18:26:20 +0000780static SkPdfResult doXObject_Pattern(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfType1PatternDictionary* skobj) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000781 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000782 return kIgnoreError_SkPdfResult;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000783 }
784
785 if (!skobj->has_BBox()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000786 return kIgnoreError_SkPdfResult;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000787 }
788
789 PdfOp_q(pdfContext, canvas, NULL);
790
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000791
792 if (skobj->Resources(pdfContext->fPdfDoc)) {
793 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
794 }
795
edisonn@google.com0f901902013-08-07 11:56:16 +0000796 SkTraceMatrix(pdfContext->fGraphicsState.fContentStreamMatrix, "Current Content stream matrix");
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000797
798 if (skobj->has_Matrix()) {
edisonn@google.com0f901902013-08-07 11:56:16 +0000799 pdfContext->fGraphicsState.fContentStreamMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000800 }
801
edisonn@google.com0f901902013-08-07 11:56:16 +0000802 SkTraceMatrix(pdfContext->fGraphicsState.fContentStreamMatrix, "Total Content stream matrix");
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000803
edisonn@google.com0f901902013-08-07 11:56:16 +0000804 canvas->setMatrix(pdfContext->fGraphicsState.fContentStreamMatrix);
805 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fContentStreamMatrix;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000806
807 SkRect bbox = skobj->BBox(pdfContext->fPdfDoc);
808 canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
809
810 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
811 // For this PdfContentsTokenizer needs to be extended.
812
813 SkPdfStream* stream = (SkPdfStream*)skobj;
814
815 SkPdfNativeTokenizer* tokenizer =
816 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
817 if (tokenizer != NULL) {
818 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
819 looper.loop();
820 delete tokenizer;
821 }
822
823 // TODO(edisonn): should we restore the variable stack at the same state?
824 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
825
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000826 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000827 return kPartial_SkPdfResult;
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000828}
829
830
edisonn@google.com3aa35552013-08-14 18:26:20 +0000831//static SkPdfResult doXObject_PS(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* obj) {
832// return kNYI_SkPdfResult;
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000833//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000834
edisonn@google.com3aa35552013-08-14 18:26:20 +0000835SkPdfResult doType3Char(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000836 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000837 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000838 }
839
840 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000841
842 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
843 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
edisonn@google.come57c62d2013-08-07 18:04:15 +0000844 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrixTm;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000845
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000846 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
edisonn@google.come57c62d2013-08-07 18:04:15 +0000847 pdfContext->fGraphicsState.fCTM.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000848
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000849 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000850
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000851 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000852
853 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000854 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000855
856 SkTraceRect(rm, "bbox mapped");
857
858 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
859
860 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
861 // For this PdfContentsTokenizer needs to be extended.
862
edisonn@google.com571c70b2013-07-10 17:09:50 +0000863 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000864
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000865 SkPdfNativeTokenizer* tokenizer =
866 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000867 if (tokenizer != NULL) {
868 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
869 looper.loop();
870 delete tokenizer;
871 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000872
873 // TODO(edisonn): should we restore the variable stack at the same state?
874 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000875 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000876
edisonn@google.com3aa35552013-08-14 18:26:20 +0000877 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000878}
879
880
edisonn@google.com571c70b2013-07-10 17:09:50 +0000881// TODO(edisonn): make sure the pointer is unique
edisonn@google.com3aa35552013-08-14 18:26:20 +0000882std::set<const SkPdfNativeObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000883
884class CheckRecursiveRendering {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000885 const SkPdfNativeObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000886public:
edisonn@google.com3aa35552013-08-14 18:26:20 +0000887 CheckRecursiveRendering(const SkPdfNativeObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000888 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000889 }
890
891 ~CheckRecursiveRendering() {
892 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000893 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000894 }
895
edisonn@google.com3aa35552013-08-14 18:26:20 +0000896 static bool IsInRendering(const SkPdfNativeObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000897 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000898 }
899};
900
edisonn@google.com3aa35552013-08-14 18:26:20 +0000901static SkPdfResult doXObject(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000902 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000903 // Oops, corrupt PDF!
edisonn@google.com3aa35552013-08-14 18:26:20 +0000904 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000905 }
906
edisonn@google.com571c70b2013-07-10 17:09:50 +0000907 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000908
edisonn@google.com571c70b2013-07-10 17:09:50 +0000909 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000910 {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000911 case kImageDictionary_SkPdfNativeObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000912 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000913 case kType1FormDictionary_SkPdfNativeObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000914 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000915 //case kObjectDictionaryXObjectPS_SkPdfNativeObjectType:
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000916 //return doXObject_PS(skxobj.asPS());
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000917 default: {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000918 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(obj) != kNone_SkPdfNativeObjectType) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +0000919 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)obj;
920 return doXObject_Pattern(pdfContext, canvas, pattern);
921 }
922 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000923 }
edisonn@google.com3aa35552013-08-14 18:26:20 +0000924 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000925}
926
edisonn@google.com3aa35552013-08-14 18:26:20 +0000927static SkPdfResult doPage(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000928 if (!skobj) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000929 return kIgnoreError_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000930 }
931
932 if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000933 return kNYI_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000934 }
935
936 SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
937
938 if (!stream) {
edisonn@google.com3aa35552013-08-14 18:26:20 +0000939 return kIgnoreError_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000940 }
941
942 if (CheckRecursiveRendering::IsInRendering(skobj)) {
943 // Oops, corrupt PDF!
edisonn@google.com3aa35552013-08-14 18:26:20 +0000944 return kIgnoreError_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000945 }
946 CheckRecursiveRendering checkRecursion(skobj);
947
948
949 PdfOp_q(pdfContext, canvas, NULL);
950
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000951
952 if (skobj->Resources(pdfContext->fPdfDoc)) {
953 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
954 }
955
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000956 // TODO(edisonn): MediaBox can be inherited!!!!
957 SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc);
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000958 if (skobj->has_Group()) {
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000959 SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc);
960 doGroup_before(pdfContext, canvas, bbox, tgroup, true);
961 } else {
962 canvas->save();
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000963 }
964
edisonn@google.comf111a4b2013-07-31 18:22:36 +0000965
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000966 SkPdfNativeTokenizer* tokenizer =
967 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
968 if (tokenizer != NULL) {
969 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
970 looper.loop();
971 delete tokenizer;
972 }
973
974 // TODO(edisonn): should we restore the variable stack at the same state?
975 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
976 canvas->restore();
977 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aa35552013-08-14 18:26:20 +0000978 return kPartial_SkPdfResult;
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000979}
980
edisonn@google.com3aa35552013-08-14 18:26:20 +0000981SkPdfResult PdfOp_q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000982 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
983 canvas->save();
edisonn@google.com3aa35552013-08-14 18:26:20 +0000984 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000985}
986
edisonn@google.com3aa35552013-08-14 18:26:20 +0000987SkPdfResult PdfOp_Q(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000988 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
989 pdfContext->fStateStack.pop();
990 canvas->restore();
edisonn@google.com3aa35552013-08-14 18:26:20 +0000991 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000992}
993
edisonn@google.com3aa35552013-08-14 18:26:20 +0000994static SkPdfResult PdfOp_cm(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000995 double array[6];
996 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000997 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000998 pdfContext->fObjectStack.pop();
999 }
1000
1001 // a b
1002 // c d
1003 // e f
1004
1005 // 0 1
1006 // 2 3
1007 // 4 5
1008
1009 // sx ky
1010 // kx sy
1011 // tx ty
1012 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1013
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001014 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001015
1016#ifdef PDF_TRACE
1017 printf("cm ");
1018 for (int i = 0 ; i < 6 ; i++) {
1019 printf("%f ", array[i]);
1020 }
1021 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001022 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001023#endif
1024
edisonn@google.com3aa35552013-08-14 18:26:20 +00001025 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001026}
1027
1028//leading TL Set the text leading, Tl
1029//, to leading, which is a number expressed in unscaled text
1030//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001031static SkPdfResult PdfOp_TL(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001032 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001033
1034 pdfContext->fGraphicsState.fTextLeading = ty;
1035
edisonn@google.com3aa35552013-08-14 18:26:20 +00001036 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001037}
1038
edisonn@google.com3aa35552013-08-14 18:26:20 +00001039static SkPdfResult PdfOp_Td(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001040#ifdef PDF_TRACE
1041 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
1042#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +00001043 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1044 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001045
edisonn@google.come57c62d2013-08-07 18:04:15 +00001046 double array[6] = {1, 0, 0, 1, tx, -ty};
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001047 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1048
1049 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1050 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
1051
edisonn@google.com3aa35552013-08-14 18:26:20 +00001052 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001053}
1054
edisonn@google.com3aa35552013-08-14 18:26:20 +00001055static SkPdfResult PdfOp_TD(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001056 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1057 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001058
edisonn@google.com571c70b2013-07-10 17:09:50 +00001059 // TODO(edisonn): Create factory methods or constructors so native is hidden
1060 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001061 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001062
1063 PdfOp_TL(pdfContext, canvas, looper);
1064
edisonn@google.com571c70b2013-07-10 17:09:50 +00001065 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001066 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001067
edisonn@google.com571c70b2013-07-10 17:09:50 +00001068 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001069 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001070
edisonn@google.com3aa35552013-08-14 18:26:20 +00001071 SkPdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001072
1073 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
1074 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001075
1076 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001077}
1078
edisonn@google.com3aa35552013-08-14 18:26:20 +00001079static SkPdfResult PdfOp_Tm(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001080 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1081 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1082 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1083 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1084 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1085 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001086
1087 double array[6];
1088 array[0] = a;
1089 array[1] = b;
1090 array[2] = c;
1091 array[3] = d;
1092 array[4] = e;
1093 array[5] = f;
1094
1095 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001096 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.come57c62d2013-08-07 18:04:15 +00001097 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001098
1099 // TODO(edisonn): Text positioning.
1100 pdfContext->fGraphicsState.fMatrixTm = matrix;
1101 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
1102
edisonn@google.com3aa35552013-08-14 18:26:20 +00001103 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001104}
1105
1106//— T* Move to the start of the next line. This operator has the same effect as the code
1107//0 Tl Td
1108//where Tl is the current leading parameter in the text state
edisonn@google.com3aa35552013-08-14 18:26:20 +00001109static SkPdfResult PdfOp_T_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001110 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
1111 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001112
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001113 pdfContext->fObjectStack.push(zero);
1114 pdfContext->fObjectStack.push(tl);
1115
edisonn@google.com3aa35552013-08-14 18:26:20 +00001116 SkPdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001117
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001118 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001119}
1120
edisonn@google.com3aa35552013-08-14 18:26:20 +00001121static SkPdfResult PdfOp_m(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001122 if (pdfContext->fGraphicsState.fPathClosed) {
1123 pdfContext->fGraphicsState.fPath.reset();
1124 pdfContext->fGraphicsState.fPathClosed = false;
1125 }
1126
edisonn@google.com571c70b2013-07-10 17:09:50 +00001127 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1128 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001129
1130 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1131 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1132
edisonn@google.com3aa35552013-08-14 18:26:20 +00001133 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001134}
1135
edisonn@google.com3aa35552013-08-14 18:26:20 +00001136static SkPdfResult PdfOp_l(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001137 if (pdfContext->fGraphicsState.fPathClosed) {
1138 pdfContext->fGraphicsState.fPath.reset();
1139 pdfContext->fGraphicsState.fPathClosed = false;
1140 }
1141
edisonn@google.com571c70b2013-07-10 17:09:50 +00001142 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1143 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001144
1145 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1146 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1147
edisonn@google.com3aa35552013-08-14 18:26:20 +00001148 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001149}
1150
edisonn@google.com3aa35552013-08-14 18:26:20 +00001151static SkPdfResult PdfOp_c(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001152 if (pdfContext->fGraphicsState.fPathClosed) {
1153 pdfContext->fGraphicsState.fPath.reset();
1154 pdfContext->fGraphicsState.fPathClosed = false;
1155 }
1156
edisonn@google.com571c70b2013-07-10 17:09:50 +00001157 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1158 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1159 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1160 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1161 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1162 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001163
1164 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1165 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1166 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1167
1168 pdfContext->fGraphicsState.fCurPosX = x3;
1169 pdfContext->fGraphicsState.fCurPosY = y3;
1170
edisonn@google.com3aa35552013-08-14 18:26:20 +00001171 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001172}
1173
edisonn@google.com3aa35552013-08-14 18:26:20 +00001174static SkPdfResult PdfOp_v(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001175 if (pdfContext->fGraphicsState.fPathClosed) {
1176 pdfContext->fGraphicsState.fPath.reset();
1177 pdfContext->fGraphicsState.fPathClosed = false;
1178 }
1179
edisonn@google.com571c70b2013-07-10 17:09:50 +00001180 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1181 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1182 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1183 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001184 double y1 = pdfContext->fGraphicsState.fCurPosY;
1185 double x1 = pdfContext->fGraphicsState.fCurPosX;
1186
1187 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1188 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1189 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1190
1191 pdfContext->fGraphicsState.fCurPosX = x3;
1192 pdfContext->fGraphicsState.fCurPosY = y3;
1193
edisonn@google.com3aa35552013-08-14 18:26:20 +00001194 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001195}
1196
edisonn@google.com3aa35552013-08-14 18:26:20 +00001197static SkPdfResult PdfOp_y(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001198 if (pdfContext->fGraphicsState.fPathClosed) {
1199 pdfContext->fGraphicsState.fPath.reset();
1200 pdfContext->fGraphicsState.fPathClosed = false;
1201 }
1202
edisonn@google.com571c70b2013-07-10 17:09:50 +00001203 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1204 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001205 double y2 = pdfContext->fGraphicsState.fCurPosY;
1206 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001207 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1208 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001209
1210 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1211 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1212 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1213
1214 pdfContext->fGraphicsState.fCurPosX = x3;
1215 pdfContext->fGraphicsState.fCurPosY = y3;
1216
edisonn@google.com3aa35552013-08-14 18:26:20 +00001217 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001218}
1219
edisonn@google.com3aa35552013-08-14 18:26:20 +00001220static SkPdfResult PdfOp_re(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001221 if (pdfContext->fGraphicsState.fPathClosed) {
1222 pdfContext->fGraphicsState.fPath.reset();
1223 pdfContext->fGraphicsState.fPathClosed = false;
1224 }
1225
edisonn@google.com571c70b2013-07-10 17:09:50 +00001226 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1227 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1228 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1229 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001230
1231 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1232 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1233
1234 pdfContext->fGraphicsState.fCurPosX = x;
1235 pdfContext->fGraphicsState.fCurPosY = y + height;
1236
edisonn@google.com3aa35552013-08-14 18:26:20 +00001237 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001238}
1239
edisonn@google.com3aa35552013-08-14 18:26:20 +00001240static SkPdfResult PdfOp_h(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001241 pdfContext->fGraphicsState.fPath.close();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001242 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001243}
1244
edisonn@google.com3aa35552013-08-14 18:26:20 +00001245static SkPdfResult PdfOp_fillAndStroke(SkPdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001246 SkPath path = pdfContext->fGraphicsState.fPath;
1247
1248 if (close) {
1249 path.close();
1250 }
1251
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001252 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001253
1254 SkPaint paint;
1255
1256 SkPoint line[2];
1257 if (fill && !stroke && path.isLine(line)) {
1258 paint.setStyle(SkPaint::kStroke_Style);
1259
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001260 // TODO(edisonn): implement this with patterns
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001261 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1262 paint.setStrokeWidth(SkDoubleToScalar(0));
1263
1264 canvas->drawPath(path, paint);
1265 } else {
1266 if (fill) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001267 if (strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0 &&
1268 pdfContext->fGraphicsState.fNonStroking.fPattern != NULL) {
1269
1270 // TODO(edisonn): we can use a shader here, like imageshader to draw fast. ultimately,
1271 // if this is not possible, and we are in rasper mode, and the cells don't intersect, we could even have multiple cpus.
1272
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001273 PdfOp_q(pdfContext, canvas, NULL);
1274
1275 if (evenOdd) {
1276 path.setFillType(SkPath::kEvenOdd_FillType);
1277 }
1278 canvas->clipPath(path);
1279
edisonn@google.com3aa35552013-08-14 18:26:20 +00001280 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(pdfContext->fGraphicsState.fNonStroking.fPattern) != kNone_SkPdfNativeObjectType) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001281 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)pdfContext->fGraphicsState.fNonStroking.fPattern;
1282
1283 // TODO(edisonn): constants
1284 // TODO(edisonn): colored
1285 if (pattern->PaintType(pdfContext->fPdfDoc) == 1) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001286 // TODO(edisonn): don't use abs, iterate as asked, if the cells intersect
1287 // it will change the result iterating in reverse
1288 int xStep = abs((int)pattern->XStep(pdfContext->fPdfDoc));
1289 int yStep = abs((int)pattern->YStep(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001290
1291 SkRect bounds = path.getBounds();
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001292
1293 // TODO(edisonn): xstep and ystep can be negative, and we need to iterate in reverse
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001294 // TODO(edisonn): don't do that!
1295 bounds.sort();
1296
1297 SkScalar x;
1298 SkScalar y;
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001299
1300 y = bounds.top();
1301 int totalx = 0;
1302 int totaly = 0;
1303 while (y < bounds.bottom()) {
1304 x = bounds.left();
1305 totalx = 0;
1306
1307 while (x < bounds.right()) {
1308 doXObject(pdfContext, canvas, pattern);
1309
edisonn@google.com0f901902013-08-07 11:56:16 +00001310 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(xStep), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001311 totalx += xStep;
1312 x += SkIntToScalar(xStep);
1313 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001314 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(-totalx), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001315
edisonn@google.com0f901902013-08-07 11:56:16 +00001316 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(-yStep));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001317 totaly += yStep;
1318 y += SkIntToScalar(yStep);
1319 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001320 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(totaly));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001321 }
1322 }
1323
1324 // apply matrix
1325 // get xstep, y step, bbox ... for cliping, and bos of the path
1326
1327 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001328 } else {
1329 paint.setStyle(SkPaint::kFill_Style);
1330 if (evenOdd) {
1331 path.setFillType(SkPath::kEvenOdd_FillType);
1332 }
1333
1334 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1335
1336 canvas->drawPath(path, paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001337 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001338 }
1339
1340 if (stroke) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001341 if (false && strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0) {
1342 // TODO(edisonn): implement Pattern for strokes
1343 paint.setStyle(SkPaint::kStroke_Style);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001344
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001345 paint.setColor(SK_ColorGREEN);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001346
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001347 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1348 canvas->drawPath(path, paint);
1349 } else {
1350 paint.setStyle(SkPaint::kStroke_Style);
1351
1352 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1353
1354 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1355 canvas->drawPath(path, paint);
1356 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001357 }
1358 }
1359
1360 pdfContext->fGraphicsState.fPath.reset();
1361 // todo zoom ... other stuff ?
1362
1363 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1364#ifndef PDF_DEBUG_NO_CLIPING
1365 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1366#endif
1367 }
1368
1369 //pdfContext->fGraphicsState.fClipPath.reset();
1370 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1371
edisonn@google.com3aa35552013-08-14 18:26:20 +00001372 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001373
1374}
1375
edisonn@google.com3aa35552013-08-14 18:26:20 +00001376static SkPdfResult PdfOp_S(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001377 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1378}
1379
edisonn@google.com3aa35552013-08-14 18:26:20 +00001380static SkPdfResult PdfOp_s(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001381 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1382}
1383
edisonn@google.com3aa35552013-08-14 18:26:20 +00001384static SkPdfResult PdfOp_F(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001385 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1386}
1387
edisonn@google.com3aa35552013-08-14 18:26:20 +00001388static SkPdfResult PdfOp_f(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001389 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1390}
1391
edisonn@google.com3aa35552013-08-14 18:26:20 +00001392static SkPdfResult PdfOp_f_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001393 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, 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, false, 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, false, true);
1402}
1403
edisonn@google.com3aa35552013-08-14 18:26:20 +00001404static SkPdfResult PdfOp_b(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001405 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1406}
1407
edisonn@google.com3aa35552013-08-14 18:26:20 +00001408static SkPdfResult PdfOp_b_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001409 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1410}
1411
edisonn@google.com3aa35552013-08-14 18:26:20 +00001412static SkPdfResult PdfOp_n(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001413 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001414 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1415#ifndef PDF_DEBUG_NO_CLIPING
1416 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1417#endif
1418 }
1419
1420 //pdfContext->fGraphicsState.fClipPath.reset();
1421 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1422
1423 pdfContext->fGraphicsState.fPathClosed = true;
1424
edisonn@google.com3aa35552013-08-14 18:26:20 +00001425 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001426}
1427
edisonn@google.com3aa35552013-08-14 18:26:20 +00001428static SkPdfResult PdfOp_BT(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001429 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.come57c62d2013-08-07 18:04:15 +00001430 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
1431 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
1432 pdfContext->fGraphicsState.fMatrixTm = matrix;
1433 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001434
edisonn@google.com3aa35552013-08-14 18:26:20 +00001435 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001436}
1437
edisonn@google.com3aa35552013-08-14 18:26:20 +00001438static SkPdfResult PdfOp_ET(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001439 if (!pdfContext->fGraphicsState.fTextBlock) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001440 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001441 }
1442 // 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 +00001443 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001444}
1445
edisonn@google.com33f11b62013-08-14 21:35:27 +00001446static SkPdfResult skpdfGraphicsStateApplyFontCore(SkPdfContext* pdfContext, const SkPdfNativeObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001447#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001448 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001449#endif
1450
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001451 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1452 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001453 return kIgnoreError_SkPdfResult;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001454 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001455
edisonn@google.com3aa35552013-08-14 18:26:20 +00001456 SkPdfNativeObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001457 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
edisonn@google.com3aa35552013-08-14 18:26:20 +00001458 if (kNone_SkPdfNativeObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001459 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001460 return kIgnoreError_SkPdfResult;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001461 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001462
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001463 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1464
1465 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1466
1467 if (skfont) {
1468 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001469 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001470 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001471 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001472}
1473
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001474//font size Tf Set the text font, Tf
1475//, to font and the text font size, Tfs, to size. font is the name of a
1476//font resource in the Fontsubdictionary of the current resource dictionary; size is
1477//a number representing a scale factor. There is no initial value for either font or
1478//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001479static SkPdfResult PdfOp_Tf(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001480 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001481 SkPdfNativeObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001482 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1483}
1484
edisonn@google.com3aa35552013-08-14 18:26:20 +00001485static SkPdfResult PdfOp_Tj(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001486 if (!pdfContext->fGraphicsState.fTextBlock) {
1487 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001488 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001489 }
1490
edisonn@google.com3aa35552013-08-14 18:26:20 +00001491 SkPdfResult ret = DrawText(pdfContext,
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001492 pdfContext->fObjectStack.top(),
1493 canvas);
1494 pdfContext->fObjectStack.pop();
1495
1496 return ret;
1497}
1498
edisonn@google.com3aa35552013-08-14 18:26:20 +00001499static SkPdfResult PdfOp_quote(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001500 if (!pdfContext->fGraphicsState.fTextBlock) {
1501 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001502 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001503 }
1504
1505 PdfOp_T_star(pdfContext, canvas, looper);
1506 // Do not pop, and push, just transfer the param to Tj
1507 return PdfOp_Tj(pdfContext, canvas, looper);
1508}
1509
edisonn@google.com3aa35552013-08-14 18:26:20 +00001510static SkPdfResult PdfOp_doublequote(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001511 if (!pdfContext->fGraphicsState.fTextBlock) {
1512 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001513 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001514 }
1515
edisonn@google.com3aa35552013-08-14 18:26:20 +00001516 SkPdfNativeObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1517 SkPdfNativeObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1518 SkPdfNativeObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001519
1520 pdfContext->fObjectStack.push(aw);
1521 PdfOp_Tw(pdfContext, canvas, looper);
1522
1523 pdfContext->fObjectStack.push(ac);
1524 PdfOp_Tc(pdfContext, canvas, looper);
1525
1526 pdfContext->fObjectStack.push(str);
1527 PdfOp_quote(pdfContext, canvas, looper);
1528
edisonn@google.com3aa35552013-08-14 18:26:20 +00001529 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001530}
1531
edisonn@google.com3aa35552013-08-14 18:26:20 +00001532static SkPdfResult PdfOp_TJ(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001533 if (!pdfContext->fGraphicsState.fTextBlock) {
1534 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001535 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001536 }
1537
edisonn@google.com571c70b2013-07-10 17:09:50 +00001538 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001539 pdfContext->fObjectStack.pop();
1540
edisonn@google.com571c70b2013-07-10 17:09:50 +00001541 if (!array->isArray()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001542 return kIgnoreError_SkPdfResult;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001543 }
1544
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001545 for( int i=0; i<static_cast<int>(array->size()); i++ )
1546 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001547 if( (*array)[i]->isAnyString()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001548 SkPdfNativeObject* obj = (*array)[i];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001549 DrawText(pdfContext,
1550 obj,
1551 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001552 } else if ((*array)[i]->isNumber()) {
1553 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001554 SkMatrix matrix;
1555 matrix.setAll(SkDoubleToScalar(1),
1556 SkDoubleToScalar(0),
1557 // TODO(edisonn): use writing mode, vertical/horizontal.
1558 SkDoubleToScalar(-dx), // amount is substracted!!!
1559 SkDoubleToScalar(0),
1560 SkDoubleToScalar(1),
1561 SkDoubleToScalar(0),
1562 SkDoubleToScalar(0),
1563 SkDoubleToScalar(0),
1564 SkDoubleToScalar(1));
1565
1566 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1567 }
1568 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001569 return kPartial_SkPdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001570}
1571
edisonn@google.com3aa35552013-08-14 18:26:20 +00001572static SkPdfResult PdfOp_CS_cs(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
1573 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com4f898b72013-08-07 21:11:57 +00001574
1575 //Next, get the ColorSpace Dictionary from the Resource Dictionary:
1576 SkPdfDictionary* colorSpaceResource = pdfContext->fGraphicsState.fResources->ColorSpace(pdfContext->fPdfDoc);
1577
edisonn@google.com3aa35552013-08-14 18:26:20 +00001578 SkPdfNativeObject* colorSpace = colorSpaceResource ? pdfContext->fPdfDoc->resolveReference(colorSpaceResource->get(name)) : name;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001579
1580 if (colorSpace == NULL) {
1581 colorOperator->fColorSpace = name->strRef();
1582 } else {
1583#ifdef PDF_TRACE
1584 printf("CS = %s\n", colorSpace->toString(0, 0).c_str());
1585#endif // PDF_TRACE
1586 if (colorSpace->isName()) {
1587 colorOperator->fColorSpace = colorSpace->strRef();
1588 } else if (colorSpace->isArray()) {
1589 int cnt = colorSpace->size();
1590 if (cnt == 0) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001591 return kIgnoreError_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001592 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001593 SkPdfNativeObject* type = colorSpace->objAtAIndex(0);
edisonn@google.com4f898b72013-08-07 21:11:57 +00001594 type = pdfContext->fPdfDoc->resolveReference(type);
1595
1596 if (type->isName("ICCBased")) {
1597 if (cnt != 2) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001598 return kIgnoreError_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001599 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001600 SkPdfNativeObject* prop = colorSpace->objAtAIndex(1);
edisonn@google.com4f898b72013-08-07 21:11:57 +00001601 prop = pdfContext->fPdfDoc->resolveReference(prop);
1602#ifdef PDF_TRACE
1603 printf("ICCBased prop = %s\n", prop->toString(0, 0).c_str());
1604#endif // PDF_TRACE
1605 // TODO(edisonn): hack
1606 if (prop && prop->isDictionary() && prop->get("N") && prop->get("N")->isInteger() && prop->get("N")->intValue() == 3) {
1607 colorOperator->setColorSpace(&strings_DeviceRGB);
edisonn@google.com3aa35552013-08-14 18:26:20 +00001608 return kPartial_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001609 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001610 return kNYI_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001611 }
1612 }
1613 }
1614
edisonn@google.com3aa35552013-08-14 18:26:20 +00001615 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001616}
1617
edisonn@google.com3aa35552013-08-14 18:26:20 +00001618static SkPdfResult PdfOp_CS(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001619 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1620}
1621
edisonn@google.com3aa35552013-08-14 18:26:20 +00001622static SkPdfResult PdfOp_cs(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001623 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1624}
1625
edisonn@google.com3aa35552013-08-14 18:26:20 +00001626static SkPdfResult PdfOp_SC_sc(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001627 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001628// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001629
1630 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1631
1632 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001633 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001634 doubles = false;
1635 }
1636
1637#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001638 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001639#endif
1640
1641 for (int i = n - 1; i >= 0 ; i--) {
1642 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001643 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1644// } else {
1645// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001646 }
1647 }
1648
1649 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001650 // TODO(edisonn): do possible field values to enum at parsing time!
1651 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001652 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001653 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001654 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001655 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001656}
1657
edisonn@google.com3aa35552013-08-14 18:26:20 +00001658static SkPdfResult PdfOp_SC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001659 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1660}
1661
edisonn@google.com3aa35552013-08-14 18:26:20 +00001662static SkPdfResult PdfOp_sc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001663 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1664}
1665
edisonn@google.com3aa35552013-08-14 18:26:20 +00001666static SkPdfResult PdfOp_SCN_scn(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001667 if (pdfContext->fObjectStack.top()->isName()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001668 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com276fed92013-08-01 21:20:47 +00001669
1670 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001671 SkPdfDictionary* patternResources = pdfContext->fGraphicsState.fResources->Pattern(pdfContext->fPdfDoc);
edisonn@google.com276fed92013-08-01 21:20:47 +00001672
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001673 if (patternResources == NULL) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001674#ifdef PDF_TRACE
1675 printf("ExtGState is NULL!\n");
1676#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001677 return kIgnoreError_SkPdfResult;
edisonn@google.com276fed92013-08-01 21:20:47 +00001678 }
1679
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001680 colorOperator->setPatternColorSpace(pdfContext->fPdfDoc->resolveReference(patternResources->get(name)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001681 }
1682
1683 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1684 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1685
edisonn@google.com3aa35552013-08-14 18:26:20 +00001686 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001687}
1688
edisonn@google.com3aa35552013-08-14 18:26:20 +00001689static SkPdfResult PdfOp_SCN(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001690 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1691}
1692
edisonn@google.com3aa35552013-08-14 18:26:20 +00001693static SkPdfResult PdfOp_scn(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001694 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1695}
1696
edisonn@google.com3aa35552013-08-14 18:26:20 +00001697static SkPdfResult PdfOp_G_g(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001698 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001699 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001700}
1701
edisonn@google.com3aa35552013-08-14 18:26:20 +00001702static SkPdfResult PdfOp_G(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001703 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1704}
1705
edisonn@google.com3aa35552013-08-14 18:26:20 +00001706static SkPdfResult PdfOp_g(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001707 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1708}
1709
edisonn@google.com3aa35552013-08-14 18:26:20 +00001710static SkPdfResult PdfOp_RG_rg(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001711 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1712 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1713 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001714
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001715 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001716 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com3aa35552013-08-14 18:26:20 +00001717 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001718}
1719
edisonn@google.com3aa35552013-08-14 18:26:20 +00001720static SkPdfResult PdfOp_RG(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001721 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1722}
1723
edisonn@google.com3aa35552013-08-14 18:26:20 +00001724static SkPdfResult PdfOp_rg(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001725 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1726}
1727
edisonn@google.com3aa35552013-08-14 18:26:20 +00001728static SkPdfResult PdfOp_K_k(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001729 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001730 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1731 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1732 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1733 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001734
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001735 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001736 // TODO(edisonn): Set color.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001737 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001738}
1739
edisonn@google.com3aa35552013-08-14 18:26:20 +00001740static SkPdfResult PdfOp_K(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001741 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1742}
1743
edisonn@google.com3aa35552013-08-14 18:26:20 +00001744static SkPdfResult PdfOp_k(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001745 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1746}
1747
edisonn@google.com3aa35552013-08-14 18:26:20 +00001748static SkPdfResult PdfOp_W(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001749 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1750 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1751
edisonn@google.com3aa35552013-08-14 18:26:20 +00001752 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001753}
1754
edisonn@google.com3aa35552013-08-14 18:26:20 +00001755static SkPdfResult PdfOp_W_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001756 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1757
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001758 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1759 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1760
edisonn@google.com3aa35552013-08-14 18:26:20 +00001761 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001762}
1763
edisonn@google.com3aa35552013-08-14 18:26:20 +00001764static SkPdfResult PdfOp_BX(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001765 *looper = new PdfCompatibilitySectionLooper();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001766 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001767}
1768
edisonn@google.com3aa35552013-08-14 18:26:20 +00001769static SkPdfResult PdfOp_EX(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001770#ifdef ASSERT_BAD_PDF_OPS
1771 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1772 // have the assert when testing good pdfs.
1773#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001774 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001775}
1776
edisonn@google.com3aa35552013-08-14 18:26:20 +00001777static SkPdfResult PdfOp_BI(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001778 *looper = new PdfInlineImageLooper();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001779 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001780}
1781
edisonn@google.com3aa35552013-08-14 18:26:20 +00001782static SkPdfResult PdfOp_ID(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.com3aa35552013-08-14 18:26:20 +00001790static SkPdfResult PdfOp_EI(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001791#ifdef ASSERT_BAD_PDF_OPS
1792 SkASSERT(false); // must be processed in inline image looper, but let's
1793 // have the assert when testing good pdfs.
1794#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001795 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001796}
1797
edisonn@google.com24cdf132013-07-30 16:06:12 +00001798
1799// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
edisonn@google.com33f11b62013-08-14 21:35:27 +00001800static SkPdfResult skpdfGraphicsStateApply_ca(SkPdfContext* pdfContext, double ca) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001801 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001802 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001803}
1804
edisonn@google.com33f11b62013-08-14 21:35:27 +00001805static SkPdfResult skpdfGraphicsStateApply_CA(SkPdfContext* pdfContext, double CA) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001806 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001807 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001808}
1809
edisonn@google.com33f11b62013-08-14 21:35:27 +00001810static SkPdfResult skpdfGraphicsStateApplyLW(SkPdfContext* pdfContext, double lineWidth) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001811 pdfContext->fGraphicsState.fLineWidth = lineWidth;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001812 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001813}
1814
edisonn@google.com33f11b62013-08-14 21:35:27 +00001815static SkPdfResult skpdfGraphicsStateApplyLC(SkPdfContext* pdfContext, int64_t lineCap) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001816 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001817 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001818}
1819
edisonn@google.com33f11b62013-08-14 21:35:27 +00001820static SkPdfResult skpdfGraphicsStateApplyLJ(SkPdfContext* pdfContext, int64_t lineJoin) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001821 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001822 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001823}
1824
edisonn@google.com33f11b62013-08-14 21:35:27 +00001825static SkPdfResult skpdfGraphicsStateApplyML(SkPdfContext* pdfContext, double miterLimit) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001826 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001827 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001828}
1829
1830// TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure
1831/*
18321) [ ] 0 No dash; solid, unbroken lines
18332) [3] 0 3 units on, 3 units off, …
18343) [2] 1 1 on, 2 off, 2 on, 2 off, …
18354) [2 1] 0 2 on, 1 off, 2 on, 1 off, …
18365) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, …
18376) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, …
1838 */
1839
edisonn@google.com33f11b62013-08-14 21:35:27 +00001840static SkPdfResult skpdfGraphicsStateApplyD(SkPdfContext* pdfContext, SkPdfArray* intervals, SkPdfNativeObject* phase) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001841 int cnt = intervals->size();
1842 if (cnt >= 256) {
1843 // TODO(edisonn): report error/warning, unsuported;
1844 // TODO(edisonn): alloc memory
edisonn@google.com3aa35552013-08-14 18:26:20 +00001845 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001846 }
1847 for (int i = 0; i < cnt; i++) {
1848 if (!intervals->objAtAIndex(i)->isNumber()) {
1849 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001850 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001851 }
1852 }
1853
edisonn@google.com24cdf132013-07-30 16:06:12 +00001854 double total = 0;
1855 for (int i = 0 ; i < cnt; i++) {
1856 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1857 total += pdfContext->fGraphicsState.fDashArray[i];
1858 }
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001859 if (cnt & 1) {
1860 if (cnt == 1) {
1861 pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0];
1862 cnt++;
1863 } else {
1864 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001865 return kNYI_SkPdfResult;
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001866 }
1867 }
1868 pdfContext->fGraphicsState.fDashArrayLength = cnt;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001869 pdfContext->fGraphicsState.fDashPhase = phase->scalarValue();
1870 if (pdfContext->fGraphicsState.fDashPhase == 0) {
1871 // other rules, changes?
1872 pdfContext->fGraphicsState.fDashPhase = total;
1873 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001874
edisonn@google.com3aa35552013-08-14 18:26:20 +00001875 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001876}
1877
edisonn@google.com33f11b62013-08-14 21:35:27 +00001878static SkPdfResult skpdfGraphicsStateApplyD(SkPdfContext* pdfContext, SkPdfArray* dash) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001879 // TODO(edisonn): verify input
1880 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1881 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001882 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001883 }
1884 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1));
1885}
1886
edisonn@google.com33f11b62013-08-14 21:35:27 +00001887static void skpdfGraphicsStateApplyFont(SkPdfContext* pdfContext, SkPdfArray* fontAndSize) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001888 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1889 // TODO(edisonn): report error/warning
1890 return;
1891 }
1892 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1893}
1894
1895
1896//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001897static SkPdfResult PdfOp_w(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001898 double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1899 return skpdfGraphicsStateApplyLW(pdfContext, lw);
1900}
1901
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001902//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 +00001903static SkPdfResult PdfOp_J(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001904 int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1905 return skpdfGraphicsStateApplyLC(pdfContext, lc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001906}
1907
1908//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 +00001909static SkPdfResult PdfOp_j(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001910 double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1911 return skpdfGraphicsStateApplyLJ(pdfContext, lj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001912}
1913
1914//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001915static SkPdfResult PdfOp_M(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001916 double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1917 return skpdfGraphicsStateApplyML(pdfContext, ml);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001918}
1919
1920//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1921//page 155).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001922static SkPdfResult PdfOp_d(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1923 SkPdfNativeObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1924 SkPdfNativeObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001925
edisonn@google.com24cdf132013-07-30 16:06:12 +00001926 if (!array->isArray()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001927 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001928 }
1929
1930 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001931}
1932
1933//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 +00001934static SkPdfResult PdfOp_ri(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001935 pdfContext->fObjectStack.pop();
1936
edisonn@google.com3aa35552013-08-14 18:26:20 +00001937 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001938}
1939
1940//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1941//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1942//fies the output device’s default flatness tolerance.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001943static SkPdfResult PdfOp_i(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001944 pdfContext->fObjectStack.pop();
1945
edisonn@google.com3aa35552013-08-14 18:26:20 +00001946 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001947}
1948
edisonn@google.come878e722013-07-29 19:10:58 +00001949SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1950
1951class InitBlendModes {
1952public:
1953 InitBlendModes() {
1954 // TODO(edisonn): use the python code generator?
1955 // TABLE 7.2 Standard separable blend modes
1956 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1957 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1958 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1959 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1960 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1961 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1962 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1963 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1964 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1965 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1966 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1967 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1968
1969 // TABLE 7.3 Standard nonseparable blend modes
1970 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1971 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1972 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1973 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1974 }
1975};
1976
1977InitBlendModes _gDummyInniter;
1978
edisonn@google.com33f11b62013-08-14 21:35:27 +00001979static SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
edisonn@google.come878e722013-07-29 19:10:58 +00001980 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1981 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1982 return mode;
1983 }
1984
1985 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1986}
1987
edisonn@google.com33f11b62013-08-14 21:35:27 +00001988static void skpdfGraphicsStateApplyBM_name(SkPdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001989 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1990 if (mode <= SkXfermode::kLastMode) {
1991 pdfContext->fGraphicsState.fBlendModesLength = 1;
1992 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1993 } else {
1994 // TODO(edisonn): report unknown blend mode
1995 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001996}
1997
edisonn@google.com33f11b62013-08-14 21:35:27 +00001998static void skpdfGraphicsStateApplyBM_array(SkPdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001999 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
2000 // TODO(edisonn): report error/warning
2001 return;
2002 }
2003 SkXfermode::Mode modes[256];
2004 int cnt = blendModes->size();
2005 for (int i = 0; i < cnt; i++) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002006 SkPdfNativeObject* name = blendModes->objAtAIndex(i);
edisonn@google.come878e722013-07-29 19:10:58 +00002007 if (!name->isName()) {
2008 // TODO(edisonn): report error/warning
2009 return;
2010 }
2011 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
2012 if (mode > SkXfermode::kLastMode) {
2013 // TODO(edisonn): report error/warning
2014 return;
2015 }
2016 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002017
edisonn@google.come878e722013-07-29 19:10:58 +00002018 pdfContext->fGraphicsState.fBlendModesLength = cnt;
2019 for (int i = 0; i < cnt; i++) {
2020 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
2021 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002022}
2023
edisonn@google.com33f11b62013-08-14 21:35:27 +00002024static void skpdfGraphicsStateApplySMask_dict(SkPdfContext* pdfContext, SkPdfDictionary* sMask) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002025 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00002026 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002027 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00002028 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
2029 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
2030 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
2031 } else {
2032 // TODO (edisonn): report error/warning
2033 }
2034}
2035
edisonn@google.com33f11b62013-08-14 21:35:27 +00002036static void skpdfGraphicsStateApplySMask_name(SkPdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002037 if (sMask == "None") {
2038 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002039 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002040 return;
2041 }
2042
edisonn@google.come878e722013-07-29 19:10:58 +00002043 //Next, get the ExtGState Dictionary from the Resource Dictionary:
2044 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
2045
2046 if (extGStateDictionary == NULL) {
2047#ifdef PDF_TRACE
2048 printf("ExtGState is NULL!\n");
2049#endif
2050 // TODO (edisonn): report error/warning
2051 return;
2052 }
2053
edisonn@google.com3aa35552013-08-14 18:26:20 +00002054 SkPdfNativeObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
edisonn@google.come878e722013-07-29 19:10:58 +00002055 if (!obj || !obj->isDictionary()) {
2056 // TODO (edisonn): report error/warning
2057 return;
2058 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002059
2060 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002061 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002062
edisonn@google.come878e722013-07-29 19:10:58 +00002063 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002064}
2065
edisonn@google.com33f11b62013-08-14 21:35:27 +00002066static void skpdfGraphicsStateApplyAIS(SkPdfContext* pdfContext, bool alphaSource) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002067 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
2068}
2069
2070
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002071//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
2072//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 +00002073static SkPdfResult PdfOp_gs(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2074 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002075
2076#ifdef PDF_TRACE
2077 std::string str;
2078#endif
2079
2080 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00002081 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002082
2083 if (extGStateDictionary == NULL) {
2084#ifdef PDF_TRACE
2085 printf("ExtGState is NULL!\n");
2086#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00002087 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002088 }
2089
edisonn@google.com3aa35552013-08-14 18:26:20 +00002090 SkPdfNativeObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002091
edisonn@google.com3aa35552013-08-14 18:26:20 +00002092 if (kNone_SkPdfNativeObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
2093 return kIgnoreError_SkPdfResult;
edisonn@google.com571c70b2013-07-10 17:09:50 +00002094 }
2095 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002096
2097 // TODO(edisonn): now load all those properties in graphic state.
2098 if (gs == NULL) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002099 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002100 }
2101
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002102 if (gs->has_LW()) {
2103 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
2104 }
2105
2106 if (gs->has_LC()) {
2107 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
2108 }
2109
2110 if (gs->has_LJ()) {
2111 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
2112 }
2113
2114 if (gs->has_ML()) {
2115 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
2116 }
2117
2118 if (gs->has_D()) {
2119 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
2120 }
2121
2122 if (gs->has_Font()) {
2123 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
2124 }
2125
2126 if (gs->has_BM()) {
2127 if (gs->isBMAName(pdfContext->fPdfDoc)) {
2128 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
2129 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
2130 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
2131 } else {
2132 // TODO(edisonn): report/warn
2133 }
2134 }
2135
2136 if (gs->has_SMask()) {
2137 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
2138 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
2139 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
2140 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
2141 } else {
2142 // TODO(edisonn): report/warn
2143 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002144 }
2145
2146 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002147 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002148 }
2149
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002150 if (gs->has_CA()) {
2151 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
2152 }
2153
2154 if (gs->has_AIS()) {
2155 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002156 }
2157
edisonn@google.com3aa35552013-08-14 18:26:20 +00002158 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002159}
2160
2161//charSpace Tc Set the character spacing, Tc
2162//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
2163//Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002164SkPdfResult PdfOp_Tc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002165 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002166 pdfContext->fGraphicsState.fCharSpace = charSpace;
2167
edisonn@google.com3aa35552013-08-14 18:26:20 +00002168 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002169}
2170
2171//wordSpace Tw Set the word spacing, T
2172//w
2173//, to wordSpace, which is a number expressed in unscaled
2174//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
2175//value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002176SkPdfResult PdfOp_Tw(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002177 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002178 pdfContext->fGraphicsState.fWordSpace = wordSpace;
2179
edisonn@google.com3aa35552013-08-14 18:26:20 +00002180 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002181}
2182
2183//scale Tz Set the horizontal scaling, Th
2184//, to (scale ˜ 100). scale is a number specifying the
2185//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002186static SkPdfResult PdfOp_Tz(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002187 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002188
edisonn@google.com3aa35552013-08-14 18:26:20 +00002189 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002190}
2191
2192//render Tr Set the text rendering mode, T
2193//mode, to render, which is an integer. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002194static SkPdfResult PdfOp_Tr(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002195 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002196
edisonn@google.com3aa35552013-08-14 18:26:20 +00002197 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002198}
2199//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
2200//units. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002201static SkPdfResult PdfOp_Ts(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002202 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002203
edisonn@google.com3aa35552013-08-14 18:26:20 +00002204 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002205}
2206
2207//wx wy d0
edisonn@google.com3aa35552013-08-14 18:26:20 +00002208static SkPdfResult PdfOp_d0(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002209 pdfContext->fObjectStack.pop();
2210 pdfContext->fObjectStack.pop();
2211
edisonn@google.com3aa35552013-08-14 18:26:20 +00002212 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002213}
2214
2215//wx wy llx lly urx ury d1
edisonn@google.com3aa35552013-08-14 18:26:20 +00002216static SkPdfResult PdfOp_d1(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002217 pdfContext->fObjectStack.pop();
2218 pdfContext->fObjectStack.pop();
2219 pdfContext->fObjectStack.pop();
2220 pdfContext->fObjectStack.pop();
2221 pdfContext->fObjectStack.pop();
2222 pdfContext->fObjectStack.pop();
2223
edisonn@google.com3aa35552013-08-14 18:26:20 +00002224 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002225}
2226
2227//name sh
edisonn@google.com3aa35552013-08-14 18:26:20 +00002228static SkPdfResult PdfOp_sh(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002229 pdfContext->fObjectStack.pop();
2230
edisonn@google.com3aa35552013-08-14 18:26:20 +00002231 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002232}
2233
2234//name Do
edisonn@google.com3aa35552013-08-14 18:26:20 +00002235static SkPdfResult PdfOp_Do(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2236 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002237
edisonn@google.com571c70b2013-07-10 17:09:50 +00002238 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002239
2240 if (xObject == NULL) {
2241#ifdef PDF_TRACE
2242 printf("XObject is NULL!\n");
2243#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00002244 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002245 }
2246
edisonn@google.com3aa35552013-08-14 18:26:20 +00002247 SkPdfNativeObject* value = xObject->get(name);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002248 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002249
2250#ifdef PDF_TRACE
2251// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002252// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002253#endif
2254
edisonn@google.com571c70b2013-07-10 17:09:50 +00002255 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002256}
2257
2258//tag MP Designate a marked-content point. tag is a name object indicating the role or
2259//significance of the point.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002260static SkPdfResult PdfOp_MP(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002261 pdfContext->fObjectStack.pop();
2262
edisonn@google.com3aa35552013-08-14 18:26:20 +00002263 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002264}
2265
2266//tag properties DP Designate a marked-content point with an associated property list. tag is a
2267//name object indicating the role or significance of the point; properties is
2268//either an inline dictionary containing the property list or a name object
2269//associated with it in the Properties subdictionary of the current resource
2270//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002271static SkPdfResult PdfOp_DP(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002272 pdfContext->fObjectStack.pop();
2273 pdfContext->fObjectStack.pop();
2274
edisonn@google.com3aa35552013-08-14 18:26:20 +00002275 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002276}
2277
2278//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
2279//tag is a name object indicating the role or significance of the sequence.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002280static SkPdfResult PdfOp_BMC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002281 pdfContext->fObjectStack.pop();
2282
edisonn@google.com3aa35552013-08-14 18:26:20 +00002283 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002284}
2285
2286//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
2287//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
2288//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 +00002289static SkPdfResult PdfOp_BDC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002290 pdfContext->fObjectStack.pop();
2291 pdfContext->fObjectStack.pop();
2292
edisonn@google.com3aa35552013-08-14 18:26:20 +00002293 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002294}
2295
2296//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002297static SkPdfResult PdfOp_EMC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2298 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002299}
2300
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002301static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002302 static bool gInitialized = false;
2303 if (gInitialized) {
2304 return;
2305 }
2306
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002307 gPdfOps.set("q", PdfOp_q);
2308 gPdfOps.set("Q", PdfOp_Q);
2309 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002310
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002311 gPdfOps.set("TD", PdfOp_TD);
2312 gPdfOps.set("Td", PdfOp_Td);
2313 gPdfOps.set("Tm", PdfOp_Tm);
2314 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002315
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002316 gPdfOps.set("m", PdfOp_m);
2317 gPdfOps.set("l", PdfOp_l);
2318 gPdfOps.set("c", PdfOp_c);
2319 gPdfOps.set("v", PdfOp_v);
2320 gPdfOps.set("y", PdfOp_y);
2321 gPdfOps.set("h", PdfOp_h);
2322 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002323
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002324 gPdfOps.set("S", PdfOp_S);
2325 gPdfOps.set("s", PdfOp_s);
2326 gPdfOps.set("f", PdfOp_f);
2327 gPdfOps.set("F", PdfOp_F);
2328 gPdfOps.set("f*", PdfOp_f_star);
2329 gPdfOps.set("B", PdfOp_B);
2330 gPdfOps.set("B*", PdfOp_B_star);
2331 gPdfOps.set("b", PdfOp_b);
2332 gPdfOps.set("b*", PdfOp_b_star);
2333 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002334
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002335 gPdfOps.set("BT", PdfOp_BT);
2336 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002337
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002338 gPdfOps.set("Tj", PdfOp_Tj);
2339 gPdfOps.set("'", PdfOp_quote);
2340 gPdfOps.set("\"", PdfOp_doublequote);
2341 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002342
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002343 gPdfOps.set("CS", PdfOp_CS);
2344 gPdfOps.set("cs", PdfOp_cs);
2345 gPdfOps.set("SC", PdfOp_SC);
2346 gPdfOps.set("SCN", PdfOp_SCN);
2347 gPdfOps.set("sc", PdfOp_sc);
2348 gPdfOps.set("scn", PdfOp_scn);
2349 gPdfOps.set("G", PdfOp_G);
2350 gPdfOps.set("g", PdfOp_g);
2351 gPdfOps.set("RG", PdfOp_RG);
2352 gPdfOps.set("rg", PdfOp_rg);
2353 gPdfOps.set("K", PdfOp_K);
2354 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002355
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002356 gPdfOps.set("W", PdfOp_W);
2357 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002358
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002359 gPdfOps.set("BX", PdfOp_BX);
2360 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002361
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002362 gPdfOps.set("BI", PdfOp_BI);
2363 gPdfOps.set("ID", PdfOp_ID);
2364 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002365
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002366 gPdfOps.set("w", PdfOp_w);
2367 gPdfOps.set("J", PdfOp_J);
2368 gPdfOps.set("j", PdfOp_j);
2369 gPdfOps.set("M", PdfOp_M);
2370 gPdfOps.set("d", PdfOp_d);
2371 gPdfOps.set("ri", PdfOp_ri);
2372 gPdfOps.set("i", PdfOp_i);
2373 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002374
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002375 gPdfOps.set("Tc", PdfOp_Tc);
2376 gPdfOps.set("Tw", PdfOp_Tw);
2377 gPdfOps.set("Tz", PdfOp_Tz);
2378 gPdfOps.set("TL", PdfOp_TL);
2379 gPdfOps.set("Tf", PdfOp_Tf);
2380 gPdfOps.set("Tr", PdfOp_Tr);
2381 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002382
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002383 gPdfOps.set("d0", PdfOp_d0);
2384 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002385
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002386 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002387
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002388 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002389
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002390 gPdfOps.set("MP", PdfOp_MP);
2391 gPdfOps.set("DP", PdfOp_DP);
2392 gPdfOps.set("BMC", PdfOp_BMC);
2393 gPdfOps.set("BDC", PdfOp_BDC);
2394 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002395
2396 gInitialized = true;
2397}
2398
2399class InitPdfOps {
2400public:
2401 InitPdfOps() {
2402 initPdfOperatorRenderes();
2403 }
2404};
2405
2406InitPdfOps gInitPdfOps;
2407
2408void reportPdfRenderStats() {
2409 std::map<std::string, int>::iterator iter;
2410
edisonn@google.com3aa35552013-08-14 18:26:20 +00002411 for (int i = 0 ; i < kCount_SkPdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002412 SkTDict<int>::Iter iter(gRenderStats[i]);
2413 const char* key;
2414 int value = 0;
2415 while ((key = iter.next(&value)) != NULL) {
2416 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002417 }
2418 }
2419}
2420
edisonn@google.com3aa35552013-08-14 18:26:20 +00002421SkPdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002422 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002423 {
2424 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002425 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002426 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002427 // caller, main work is done by pdfOperatorRenderer(...)
2428 PdfTokenLooper* childLooper = NULL;
edisonn@google.com3aa35552013-08-14 18:26:20 +00002429 SkPdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002430
2431 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002432 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2433 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002434
2435 if (childLooper) {
2436 childLooper->setUp(this);
2437 childLooper->loop();
2438 delete childLooper;
2439 }
2440 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002441 int cnt = 0;
edisonn@google.com3aa35552013-08-14 18:26:20 +00002442 gRenderStats[kUnsupported_SkPdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2443 gRenderStats[kUnsupported_SkPdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002444 }
2445 }
2446 else if (token.fType == kObject_TokenType)
2447 {
2448 fPdfContext->fObjectStack.push( token.fObject );
2449 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002450 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002451 // TODO(edisonn): deine or use assert not reached
edisonn@google.com3aa35552013-08-14 18:26:20 +00002452 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002453 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00002454 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002455}
2456
2457void PdfMainLooper::loop() {
2458 PdfToken token;
2459 while (readToken(fTokenizer, &token)) {
2460 consumeToken(token);
2461 }
2462}
2463
edisonn@google.com3aa35552013-08-14 18:26:20 +00002464SkPdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002465 SkASSERT(false);
edisonn@google.com3aa35552013-08-14 18:26:20 +00002466 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002467}
2468
2469void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002470 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002471}
2472
edisonn@google.com3aa35552013-08-14 18:26:20 +00002473SkPdfResult PdfInlineImageLooper::done() {
2474 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002475}
2476
edisonn@google.com3aa35552013-08-14 18:26:20 +00002477SkPdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002478 return fParent->consumeToken(token);
2479}
2480
2481void PdfCompatibilitySectionLooper::loop() {
2482 // TODO(edisonn): save stacks position, or create a new stack?
2483 // TODO(edisonn): what happens if we pop out more variables then when we started?
2484 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2485 // pop-ing too much will not affect outside the section.
2486 PdfToken token;
2487 while (readToken(fTokenizer, &token)) {
2488 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2489 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2490 looper->setUp(this);
2491 looper->loop();
2492 delete looper;
2493 } else {
2494 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2495 fParent->consumeToken(token);
2496 }
2497 }
2498 // TODO(edisonn): restore stack.
2499}
2500
2501// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2502// TODO(edisonn): Add API for Forms viewing and editing
2503// e.g. SkBitmap getPage(int page);
2504// int formsCount();
2505// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2506// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2507// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2508// resources needed, so the preview is fast.
2509// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2510// references automatically.
2511
edisonn@google.com3aa35552013-08-14 18:26:20 +00002512SkPdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002513
edisonn@google.com444e25a2013-07-11 15:20:50 +00002514bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002515 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002516 return false;
2517 }
2518
edisonn@google.com222382b2013-07-10 22:33:10 +00002519 if (page < 0 || page >= pages()) {
2520 return false;
2521 }
2522
edisonn@google.com3aa35552013-08-14 18:26:20 +00002523 SkPdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002524
edisonn@google.com222382b2013-07-10 22:33:10 +00002525 pdfContext.fOriginalMatrix = SkMatrix::I();
2526 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2527
2528 gPdfContext = &pdfContext;
2529
2530 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002531 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002532 SkScalar w = dst.width();
2533 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002534
edisonn@google.com444e25a2013-07-11 15:20:50 +00002535 SkScalar wp = fPdfDoc->MediaBox(page).width();
2536 SkScalar hp = fPdfDoc->MediaBox(page).height();
2537
2538 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 +00002539// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2540
2541 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2542 // TODO(edisonn): add flagg for no clipping.
2543 // Use larger image to make sure we do not draw anything outside of page
2544 // could be used in tests.
2545
2546#ifdef PDF_DEBUG_3X
2547 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)};
2548#else
2549 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2550#endif
2551 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2552 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2553
2554 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2555 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2556
2557 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2558 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2559
2560 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2561 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2562
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002563 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
edisonn@google.com0f901902013-08-07 11:56:16 +00002564 pdfContext.fGraphicsState.fContentStreamMatrix = pdfContext.fOriginalMatrix;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002565 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2566 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002567
edisonn@google.com222382b2013-07-10 22:33:10 +00002568#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002569 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002570#endif
2571
edisonn@google.com444e25a2013-07-11 15:20:50 +00002572 canvas->setMatrix(pdfContext.fOriginalMatrix);
2573
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002574 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2575
2576 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002577// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002578// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002579// canvas->drawRect(rect, paint);
2580
edisonn@google.com222382b2013-07-10 22:33:10 +00002581
2582 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002583 return true;
2584}
edisonn@google.com222382b2013-07-10 22:33:10 +00002585
2586bool SkPdfRenderer::load(const SkString inputFileName) {
2587 unload();
2588
2589 // TODO(edisonn): create static function that could return NULL if there are errors
edisonn@google.com3aa35552013-08-14 18:26:20 +00002590 fPdfDoc = new SkPdfNativeDoc(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002591 if (fPdfDoc->pages() == 0) {
2592 delete fPdfDoc;
2593 fPdfDoc = NULL;
2594 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002595
2596 return fPdfDoc != NULL;
2597}
2598
edisonn@google.com147adb12013-07-24 15:56:19 +00002599bool SkPdfRenderer::load(SkStream* stream) {
2600 unload();
2601
2602 // TODO(edisonn): create static function that could return NULL if there are errors
edisonn@google.com3aa35552013-08-14 18:26:20 +00002603 fPdfDoc = new SkPdfNativeDoc(stream);
edisonn@google.com147adb12013-07-24 15:56:19 +00002604 if (fPdfDoc->pages() == 0) {
2605 delete fPdfDoc;
2606 fPdfDoc = NULL;
2607 }
2608
2609 return fPdfDoc != NULL;
2610}
2611
2612
edisonn@google.com222382b2013-07-10 22:33:10 +00002613int SkPdfRenderer::pages() const {
2614 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2615}
2616
2617void SkPdfRenderer::unload() {
2618 delete fPdfDoc;
2619 fPdfDoc = NULL;
2620}
2621
2622SkRect SkPdfRenderer::MediaBox(int page) const {
2623 SkASSERT(fPdfDoc);
2624 return fPdfDoc->MediaBox(page);
2625}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002626
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002627size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002628 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2629}
edisonn@google.com147adb12013-07-24 15:56:19 +00002630
2631bool SkPDFNativeRenderToBitmap(SkStream* stream,
2632 SkBitmap* output,
2633 int page,
2634 SkPdfContent content,
2635 double dpi) {
2636 SkASSERT(page >= 0);
2637 SkPdfRenderer renderer;
2638 renderer.load(stream);
2639 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2640 return false;
2641 }
2642
2643 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2644
2645 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2646 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2647
2648 rect = SkRect::MakeWH(width, height);
2649
2650 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2651
2652 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2653 SkCanvas canvas(device);
2654
2655 return renderer.renderPage(page, &canvas, rect);
2656}