blob: 090383e5f9dbf0d0e8645fb83750398ef6201d80 [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.coma3356fc2013-07-10 18:20:06 +0000482 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
483 int64_t width = image->Width(pdfContext->fPdfDoc);
484 int64_t height = 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;
503 cnt = array->objAtAIndex(2)->intValue() + 1;
504 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();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001044 SkPdfNativeObject* obj = pdfContext->fObjectStack.top();
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001045 obj = obj;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001046 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001047
edisonn@google.come57c62d2013-08-07 18:04:15 +00001048 double array[6] = {1, 0, 0, 1, tx, -ty};
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001049 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
1050
1051 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1052 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
1053
edisonn@google.com3aa35552013-08-14 18:26:20 +00001054 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001055}
1056
edisonn@google.com3aa35552013-08-14 18:26:20 +00001057static SkPdfResult PdfOp_TD(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001058 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1059 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001060
edisonn@google.com571c70b2013-07-10 17:09:50 +00001061 // TODO(edisonn): Create factory methods or constructors so native is hidden
1062 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001063 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001064
1065 PdfOp_TL(pdfContext, canvas, looper);
1066
edisonn@google.com571c70b2013-07-10 17:09:50 +00001067 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001068 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001069
edisonn@google.com571c70b2013-07-10 17:09:50 +00001070 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001071 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001072
edisonn@google.com3aa35552013-08-14 18:26:20 +00001073 SkPdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001074
1075 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
1076 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001077
1078 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001079}
1080
edisonn@google.com3aa35552013-08-14 18:26:20 +00001081static SkPdfResult PdfOp_Tm(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001082 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1083 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1084 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1085 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1086 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1087 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001088
1089 double array[6];
1090 array[0] = a;
1091 array[1] = b;
1092 array[2] = c;
1093 array[3] = d;
1094 array[4] = e;
1095 array[5] = f;
1096
1097 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001098 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.come57c62d2013-08-07 18:04:15 +00001099 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001100
1101 // TODO(edisonn): Text positioning.
1102 pdfContext->fGraphicsState.fMatrixTm = matrix;
1103 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
1104
edisonn@google.com3aa35552013-08-14 18:26:20 +00001105 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001106}
1107
1108//— T* Move to the start of the next line. This operator has the same effect as the code
1109//0 Tl Td
1110//where Tl is the current leading parameter in the text state
edisonn@google.com3aa35552013-08-14 18:26:20 +00001111static SkPdfResult PdfOp_T_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001112 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
1113 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001114
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001115 pdfContext->fObjectStack.push(zero);
1116 pdfContext->fObjectStack.push(tl);
1117
edisonn@google.com3aa35552013-08-14 18:26:20 +00001118 SkPdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001119
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001120 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001121}
1122
edisonn@google.com3aa35552013-08-14 18:26:20 +00001123static SkPdfResult PdfOp_m(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001124 if (pdfContext->fGraphicsState.fPathClosed) {
1125 pdfContext->fGraphicsState.fPath.reset();
1126 pdfContext->fGraphicsState.fPathClosed = false;
1127 }
1128
edisonn@google.com571c70b2013-07-10 17:09:50 +00001129 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1130 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001131
1132 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1133 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1134
edisonn@google.com3aa35552013-08-14 18:26:20 +00001135 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001136}
1137
edisonn@google.com3aa35552013-08-14 18:26:20 +00001138static SkPdfResult PdfOp_l(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001139 if (pdfContext->fGraphicsState.fPathClosed) {
1140 pdfContext->fGraphicsState.fPath.reset();
1141 pdfContext->fGraphicsState.fPathClosed = false;
1142 }
1143
edisonn@google.com571c70b2013-07-10 17:09:50 +00001144 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1145 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001146
1147 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1148 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1149
edisonn@google.com3aa35552013-08-14 18:26:20 +00001150 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001151}
1152
edisonn@google.com3aa35552013-08-14 18:26:20 +00001153static SkPdfResult PdfOp_c(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001154 if (pdfContext->fGraphicsState.fPathClosed) {
1155 pdfContext->fGraphicsState.fPath.reset();
1156 pdfContext->fGraphicsState.fPathClosed = false;
1157 }
1158
edisonn@google.com571c70b2013-07-10 17:09:50 +00001159 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1160 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1161 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1162 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1163 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1164 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001165
1166 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1167 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1168 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1169
1170 pdfContext->fGraphicsState.fCurPosX = x3;
1171 pdfContext->fGraphicsState.fCurPosY = y3;
1172
edisonn@google.com3aa35552013-08-14 18:26:20 +00001173 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001174}
1175
edisonn@google.com3aa35552013-08-14 18:26:20 +00001176static SkPdfResult PdfOp_v(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001177 if (pdfContext->fGraphicsState.fPathClosed) {
1178 pdfContext->fGraphicsState.fPath.reset();
1179 pdfContext->fGraphicsState.fPathClosed = false;
1180 }
1181
edisonn@google.com571c70b2013-07-10 17:09:50 +00001182 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1183 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1184 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1185 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001186 double y1 = pdfContext->fGraphicsState.fCurPosY;
1187 double x1 = pdfContext->fGraphicsState.fCurPosX;
1188
1189 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1190 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1191 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1192
1193 pdfContext->fGraphicsState.fCurPosX = x3;
1194 pdfContext->fGraphicsState.fCurPosY = y3;
1195
edisonn@google.com3aa35552013-08-14 18:26:20 +00001196 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001197}
1198
edisonn@google.com3aa35552013-08-14 18:26:20 +00001199static SkPdfResult PdfOp_y(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001200 if (pdfContext->fGraphicsState.fPathClosed) {
1201 pdfContext->fGraphicsState.fPath.reset();
1202 pdfContext->fGraphicsState.fPathClosed = false;
1203 }
1204
edisonn@google.com571c70b2013-07-10 17:09:50 +00001205 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1206 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001207 double y2 = pdfContext->fGraphicsState.fCurPosY;
1208 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001209 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1210 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001211
1212 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1213 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1214 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1215
1216 pdfContext->fGraphicsState.fCurPosX = x3;
1217 pdfContext->fGraphicsState.fCurPosY = y3;
1218
edisonn@google.com3aa35552013-08-14 18:26:20 +00001219 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001220}
1221
edisonn@google.com3aa35552013-08-14 18:26:20 +00001222static SkPdfResult PdfOp_re(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001223 if (pdfContext->fGraphicsState.fPathClosed) {
1224 pdfContext->fGraphicsState.fPath.reset();
1225 pdfContext->fGraphicsState.fPathClosed = false;
1226 }
1227
edisonn@google.com571c70b2013-07-10 17:09:50 +00001228 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1229 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1230 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1231 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001232
1233 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1234 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1235
1236 pdfContext->fGraphicsState.fCurPosX = x;
1237 pdfContext->fGraphicsState.fCurPosY = y + height;
1238
edisonn@google.com3aa35552013-08-14 18:26:20 +00001239 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001240}
1241
edisonn@google.com3aa35552013-08-14 18:26:20 +00001242static SkPdfResult PdfOp_h(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001243 pdfContext->fGraphicsState.fPath.close();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001244 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001245}
1246
edisonn@google.com3aa35552013-08-14 18:26:20 +00001247static SkPdfResult PdfOp_fillAndStroke(SkPdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001248 SkPath path = pdfContext->fGraphicsState.fPath;
1249
1250 if (close) {
1251 path.close();
1252 }
1253
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001254 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001255
1256 SkPaint paint;
1257
1258 SkPoint line[2];
1259 if (fill && !stroke && path.isLine(line)) {
1260 paint.setStyle(SkPaint::kStroke_Style);
1261
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001262 // TODO(edisonn): implement this with patterns
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001263 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1264 paint.setStrokeWidth(SkDoubleToScalar(0));
1265
1266 canvas->drawPath(path, paint);
1267 } else {
1268 if (fill) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001269 if (strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0 &&
1270 pdfContext->fGraphicsState.fNonStroking.fPattern != NULL) {
1271
1272 // TODO(edisonn): we can use a shader here, like imageshader to draw fast. ultimately,
1273 // if this is not possible, and we are in rasper mode, and the cells don't intersect, we could even have multiple cpus.
1274
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001275 PdfOp_q(pdfContext, canvas, NULL);
1276
1277 if (evenOdd) {
1278 path.setFillType(SkPath::kEvenOdd_FillType);
1279 }
1280 canvas->clipPath(path);
1281
edisonn@google.com3aa35552013-08-14 18:26:20 +00001282 if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(pdfContext->fGraphicsState.fNonStroking.fPattern) != kNone_SkPdfNativeObjectType) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001283 SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)pdfContext->fGraphicsState.fNonStroking.fPattern;
1284
1285 // TODO(edisonn): constants
1286 // TODO(edisonn): colored
1287 if (pattern->PaintType(pdfContext->fPdfDoc) == 1) {
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001288 // TODO(edisonn): don't use abs, iterate as asked, if the cells intersect
1289 // it will change the result iterating in reverse
1290 int xStep = abs((int)pattern->XStep(pdfContext->fPdfDoc));
1291 int yStep = abs((int)pattern->YStep(pdfContext->fPdfDoc));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001292
1293 SkRect bounds = path.getBounds();
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001294
1295 // TODO(edisonn): xstep and ystep can be negative, and we need to iterate in reverse
edisonn@google.comb0145ce2013-08-05 16:23:23 +00001296 // TODO(edisonn): don't do that!
1297 bounds.sort();
1298
1299 SkScalar x;
1300 SkScalar y;
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001301
1302 y = bounds.top();
1303 int totalx = 0;
1304 int totaly = 0;
1305 while (y < bounds.bottom()) {
1306 x = bounds.left();
1307 totalx = 0;
1308
1309 while (x < bounds.right()) {
1310 doXObject(pdfContext, canvas, pattern);
1311
edisonn@google.com0f901902013-08-07 11:56:16 +00001312 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(xStep), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001313 totalx += xStep;
1314 x += SkIntToScalar(xStep);
1315 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001316 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(-totalx), SkIntToScalar(0));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001317
edisonn@google.com0f901902013-08-07 11:56:16 +00001318 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(-yStep));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001319 totaly += yStep;
1320 y += SkIntToScalar(yStep);
1321 }
edisonn@google.com0f901902013-08-07 11:56:16 +00001322 pdfContext->fGraphicsState.fContentStreamMatrix.preTranslate(SkIntToScalar(0), SkIntToScalar(totaly));
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001323 }
1324 }
1325
1326 // apply matrix
1327 // get xstep, y step, bbox ... for cliping, and bos of the path
1328
1329 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001330 } else {
1331 paint.setStyle(SkPaint::kFill_Style);
1332 if (evenOdd) {
1333 path.setFillType(SkPath::kEvenOdd_FillType);
1334 }
1335
1336 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1337
1338 canvas->drawPath(path, paint);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001339 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001340 }
1341
1342 if (stroke) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001343 if (false && strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0) {
1344 // TODO(edisonn): implement Pattern for strokes
1345 paint.setStyle(SkPaint::kStroke_Style);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001346
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001347 paint.setColor(SK_ColorGREEN);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001348
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001349 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1350 canvas->drawPath(path, paint);
1351 } else {
1352 paint.setStyle(SkPaint::kStroke_Style);
1353
1354 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1355
1356 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1357 canvas->drawPath(path, paint);
1358 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001359 }
1360 }
1361
1362 pdfContext->fGraphicsState.fPath.reset();
1363 // todo zoom ... other stuff ?
1364
1365 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1366#ifndef PDF_DEBUG_NO_CLIPING
1367 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1368#endif
1369 }
1370
1371 //pdfContext->fGraphicsState.fClipPath.reset();
1372 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1373
edisonn@google.com3aa35552013-08-14 18:26:20 +00001374 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001375
1376}
1377
edisonn@google.com3aa35552013-08-14 18:26:20 +00001378static SkPdfResult PdfOp_S(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001379 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1380}
1381
edisonn@google.com3aa35552013-08-14 18:26:20 +00001382static SkPdfResult PdfOp_s(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001383 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1384}
1385
edisonn@google.com3aa35552013-08-14 18:26:20 +00001386static SkPdfResult PdfOp_F(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001387 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1388}
1389
edisonn@google.com3aa35552013-08-14 18:26:20 +00001390static SkPdfResult PdfOp_f(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001391 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1392}
1393
edisonn@google.com3aa35552013-08-14 18:26:20 +00001394static SkPdfResult PdfOp_f_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001395 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1396}
1397
edisonn@google.com3aa35552013-08-14 18:26:20 +00001398static SkPdfResult PdfOp_B(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001399 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1400}
1401
edisonn@google.com3aa35552013-08-14 18:26:20 +00001402static SkPdfResult PdfOp_B_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001403 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1404}
1405
edisonn@google.com3aa35552013-08-14 18:26:20 +00001406static SkPdfResult PdfOp_b(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001407 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1408}
1409
edisonn@google.com3aa35552013-08-14 18:26:20 +00001410static SkPdfResult PdfOp_b_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001411 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1412}
1413
edisonn@google.com3aa35552013-08-14 18:26:20 +00001414static SkPdfResult PdfOp_n(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001415 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001416 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1417#ifndef PDF_DEBUG_NO_CLIPING
1418 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1419#endif
1420 }
1421
1422 //pdfContext->fGraphicsState.fClipPath.reset();
1423 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1424
1425 pdfContext->fGraphicsState.fPathClosed = true;
1426
edisonn@google.com3aa35552013-08-14 18:26:20 +00001427 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001428}
1429
edisonn@google.com3aa35552013-08-14 18:26:20 +00001430static SkPdfResult PdfOp_BT(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001431 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.come57c62d2013-08-07 18:04:15 +00001432 SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
1433 matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
1434 pdfContext->fGraphicsState.fMatrixTm = matrix;
1435 pdfContext->fGraphicsState.fMatrixTlm = matrix;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001436
edisonn@google.com3aa35552013-08-14 18:26:20 +00001437 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438}
1439
edisonn@google.com3aa35552013-08-14 18:26:20 +00001440static SkPdfResult PdfOp_ET(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001441 if (!pdfContext->fGraphicsState.fTextBlock) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001442 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001443 }
1444 // 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 +00001445 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001446}
1447
edisonn@google.com3aa35552013-08-14 18:26:20 +00001448SkPdfResult skpdfGraphicsStateApplyFontCore(SkPdfContext* pdfContext, const SkPdfNativeObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001449#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001450 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001451#endif
1452
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001453 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1454 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001455 return kIgnoreError_SkPdfResult;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001456 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001457
edisonn@google.com3aa35552013-08-14 18:26:20 +00001458 SkPdfNativeObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001459 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
edisonn@google.com3aa35552013-08-14 18:26:20 +00001460 if (kNone_SkPdfNativeObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001461 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001462 return kIgnoreError_SkPdfResult;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001463 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001464
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001465 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1466
1467 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1468
1469 if (skfont) {
1470 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001471 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001472 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001473 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001474}
1475
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001476//font size Tf Set the text font, Tf
1477//, to font and the text font size, Tfs, to size. font is the name of a
1478//font resource in the Fontsubdictionary of the current resource dictionary; size is
1479//a number representing a scale factor. There is no initial value for either font or
1480//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001481static SkPdfResult PdfOp_Tf(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001482 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001483 SkPdfNativeObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001484 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1485}
1486
edisonn@google.com3aa35552013-08-14 18:26:20 +00001487static SkPdfResult PdfOp_Tj(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001488 if (!pdfContext->fGraphicsState.fTextBlock) {
1489 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001490 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001491 }
1492
edisonn@google.com3aa35552013-08-14 18:26:20 +00001493 SkPdfResult ret = DrawText(pdfContext,
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001494 pdfContext->fObjectStack.top(),
1495 canvas);
1496 pdfContext->fObjectStack.pop();
1497
1498 return ret;
1499}
1500
edisonn@google.com3aa35552013-08-14 18:26:20 +00001501static SkPdfResult PdfOp_quote(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001502 if (!pdfContext->fGraphicsState.fTextBlock) {
1503 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001504 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001505 }
1506
1507 PdfOp_T_star(pdfContext, canvas, looper);
1508 // Do not pop, and push, just transfer the param to Tj
1509 return PdfOp_Tj(pdfContext, canvas, looper);
1510}
1511
edisonn@google.com3aa35552013-08-14 18:26:20 +00001512static SkPdfResult PdfOp_doublequote(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001513 if (!pdfContext->fGraphicsState.fTextBlock) {
1514 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001515 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001516 }
1517
edisonn@google.com3aa35552013-08-14 18:26:20 +00001518 SkPdfNativeObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1519 SkPdfNativeObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1520 SkPdfNativeObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001521
1522 pdfContext->fObjectStack.push(aw);
1523 PdfOp_Tw(pdfContext, canvas, looper);
1524
1525 pdfContext->fObjectStack.push(ac);
1526 PdfOp_Tc(pdfContext, canvas, looper);
1527
1528 pdfContext->fObjectStack.push(str);
1529 PdfOp_quote(pdfContext, canvas, looper);
1530
edisonn@google.com3aa35552013-08-14 18:26:20 +00001531 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001532}
1533
edisonn@google.com3aa35552013-08-14 18:26:20 +00001534static SkPdfResult PdfOp_TJ(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001535 if (!pdfContext->fGraphicsState.fTextBlock) {
1536 // TODO(edisonn): try to recover and draw it any way?
edisonn@google.com3aa35552013-08-14 18:26:20 +00001537 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001538 }
1539
edisonn@google.com571c70b2013-07-10 17:09:50 +00001540 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001541 pdfContext->fObjectStack.pop();
1542
edisonn@google.com571c70b2013-07-10 17:09:50 +00001543 if (!array->isArray()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001544 return kIgnoreError_SkPdfResult;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001545 }
1546
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001547 for( int i=0; i<static_cast<int>(array->size()); i++ )
1548 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001549 if( (*array)[i]->isAnyString()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001550 SkPdfNativeObject* obj = (*array)[i];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001551 DrawText(pdfContext,
1552 obj,
1553 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001554 } else if ((*array)[i]->isNumber()) {
1555 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001556 SkMatrix matrix;
1557 matrix.setAll(SkDoubleToScalar(1),
1558 SkDoubleToScalar(0),
1559 // TODO(edisonn): use writing mode, vertical/horizontal.
1560 SkDoubleToScalar(-dx), // amount is substracted!!!
1561 SkDoubleToScalar(0),
1562 SkDoubleToScalar(1),
1563 SkDoubleToScalar(0),
1564 SkDoubleToScalar(0),
1565 SkDoubleToScalar(0),
1566 SkDoubleToScalar(1));
1567
1568 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1569 }
1570 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001571 return kPartial_SkPdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001572}
1573
edisonn@google.com3aa35552013-08-14 18:26:20 +00001574static SkPdfResult PdfOp_CS_cs(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
1575 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com4f898b72013-08-07 21:11:57 +00001576
1577 //Next, get the ColorSpace Dictionary from the Resource Dictionary:
1578 SkPdfDictionary* colorSpaceResource = pdfContext->fGraphicsState.fResources->ColorSpace(pdfContext->fPdfDoc);
1579
edisonn@google.com3aa35552013-08-14 18:26:20 +00001580 SkPdfNativeObject* colorSpace = colorSpaceResource ? pdfContext->fPdfDoc->resolveReference(colorSpaceResource->get(name)) : name;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001581
1582 if (colorSpace == NULL) {
1583 colorOperator->fColorSpace = name->strRef();
1584 } else {
1585#ifdef PDF_TRACE
1586 printf("CS = %s\n", colorSpace->toString(0, 0).c_str());
1587#endif // PDF_TRACE
1588 if (colorSpace->isName()) {
1589 colorOperator->fColorSpace = colorSpace->strRef();
1590 } else if (colorSpace->isArray()) {
1591 int cnt = colorSpace->size();
1592 if (cnt == 0) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001593 return kIgnoreError_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001594 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001595 SkPdfNativeObject* type = colorSpace->objAtAIndex(0);
edisonn@google.com4f898b72013-08-07 21:11:57 +00001596 type = pdfContext->fPdfDoc->resolveReference(type);
1597
1598 if (type->isName("ICCBased")) {
1599 if (cnt != 2) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001600 return kIgnoreError_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001601 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001602 SkPdfNativeObject* prop = colorSpace->objAtAIndex(1);
edisonn@google.com4f898b72013-08-07 21:11:57 +00001603 prop = pdfContext->fPdfDoc->resolveReference(prop);
1604#ifdef PDF_TRACE
1605 printf("ICCBased prop = %s\n", prop->toString(0, 0).c_str());
1606#endif // PDF_TRACE
1607 // TODO(edisonn): hack
1608 if (prop && prop->isDictionary() && prop->get("N") && prop->get("N")->isInteger() && prop->get("N")->intValue() == 3) {
1609 colorOperator->setColorSpace(&strings_DeviceRGB);
edisonn@google.com3aa35552013-08-14 18:26:20 +00001610 return kPartial_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001611 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001612 return kNYI_SkPdfResult;
edisonn@google.com4f898b72013-08-07 21:11:57 +00001613 }
1614 }
1615 }
1616
edisonn@google.com3aa35552013-08-14 18:26:20 +00001617 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001618}
1619
edisonn@google.com3aa35552013-08-14 18:26:20 +00001620static SkPdfResult PdfOp_CS(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001621 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1622}
1623
edisonn@google.com3aa35552013-08-14 18:26:20 +00001624static SkPdfResult PdfOp_cs(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001625 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1626}
1627
edisonn@google.com3aa35552013-08-14 18:26:20 +00001628static SkPdfResult PdfOp_SC_sc(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001629 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001630// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001631
1632 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1633
1634 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001635 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001636 doubles = false;
1637 }
1638
1639#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001640 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001641#endif
1642
1643 for (int i = n - 1; i >= 0 ; i--) {
1644 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001645 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1646// } else {
1647// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001648 }
1649 }
1650
1651 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001652 // TODO(edisonn): do possible field values to enum at parsing time!
1653 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001654 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001655 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001656 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00001657 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001658}
1659
edisonn@google.com3aa35552013-08-14 18:26:20 +00001660static SkPdfResult PdfOp_SC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001661 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1662}
1663
edisonn@google.com3aa35552013-08-14 18:26:20 +00001664static SkPdfResult PdfOp_sc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001665 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1666}
1667
edisonn@google.com3aa35552013-08-14 18:26:20 +00001668static SkPdfResult PdfOp_SCN_scn(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001669 if (pdfContext->fObjectStack.top()->isName()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001670 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com276fed92013-08-01 21:20:47 +00001671
1672 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001673 SkPdfDictionary* patternResources = pdfContext->fGraphicsState.fResources->Pattern(pdfContext->fPdfDoc);
edisonn@google.com276fed92013-08-01 21:20:47 +00001674
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001675 if (patternResources == NULL) {
edisonn@google.com276fed92013-08-01 21:20:47 +00001676#ifdef PDF_TRACE
1677 printf("ExtGState is NULL!\n");
1678#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001679 return kIgnoreError_SkPdfResult;
edisonn@google.com276fed92013-08-01 21:20:47 +00001680 }
1681
edisonn@google.come2e01ff2013-08-02 20:24:48 +00001682 colorOperator->setPatternColorSpace(pdfContext->fPdfDoc->resolveReference(patternResources->get(name)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001683 }
1684
1685 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1686 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1687
edisonn@google.com3aa35552013-08-14 18:26:20 +00001688 return kPartial_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001689}
1690
edisonn@google.com3aa35552013-08-14 18:26:20 +00001691static SkPdfResult PdfOp_SCN(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001692 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1693}
1694
edisonn@google.com3aa35552013-08-14 18:26:20 +00001695static SkPdfResult PdfOp_scn(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001696 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1697}
1698
edisonn@google.com3aa35552013-08-14 18:26:20 +00001699static SkPdfResult PdfOp_G_g(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001700 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001701 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001702}
1703
edisonn@google.com3aa35552013-08-14 18:26:20 +00001704static SkPdfResult PdfOp_G(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001705 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1706}
1707
edisonn@google.com3aa35552013-08-14 18:26:20 +00001708static SkPdfResult PdfOp_g(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001709 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1710}
1711
edisonn@google.com3aa35552013-08-14 18:26:20 +00001712static SkPdfResult PdfOp_RG_rg(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001713 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1714 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1715 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001716
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001717 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001718 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com3aa35552013-08-14 18:26:20 +00001719 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001720}
1721
edisonn@google.com3aa35552013-08-14 18:26:20 +00001722static SkPdfResult PdfOp_RG(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001723 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1724}
1725
edisonn@google.com3aa35552013-08-14 18:26:20 +00001726static SkPdfResult PdfOp_rg(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001727 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1728}
1729
edisonn@google.com3aa35552013-08-14 18:26:20 +00001730static SkPdfResult PdfOp_K_k(SkPdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001731 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001732 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1733 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1734 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1735 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001736
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001737 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001738 // TODO(edisonn): Set color.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001739 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001740}
1741
edisonn@google.com3aa35552013-08-14 18:26:20 +00001742static SkPdfResult PdfOp_K(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001743 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1744}
1745
edisonn@google.com3aa35552013-08-14 18:26:20 +00001746static SkPdfResult PdfOp_k(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001747 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1748}
1749
edisonn@google.com3aa35552013-08-14 18:26:20 +00001750static SkPdfResult PdfOp_W(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001751 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1752 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1753
edisonn@google.com3aa35552013-08-14 18:26:20 +00001754 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001755}
1756
edisonn@google.com3aa35552013-08-14 18:26:20 +00001757static SkPdfResult PdfOp_W_star(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001758 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1759
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001760 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1761 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1762
edisonn@google.com3aa35552013-08-14 18:26:20 +00001763 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001764}
1765
edisonn@google.com3aa35552013-08-14 18:26:20 +00001766static SkPdfResult PdfOp_BX(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001767 *looper = new PdfCompatibilitySectionLooper();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001768 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001769}
1770
edisonn@google.com3aa35552013-08-14 18:26:20 +00001771static SkPdfResult PdfOp_EX(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001772#ifdef ASSERT_BAD_PDF_OPS
1773 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1774 // have the assert when testing good pdfs.
1775#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001776 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001777}
1778
edisonn@google.com3aa35552013-08-14 18:26:20 +00001779static SkPdfResult PdfOp_BI(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001780 *looper = new PdfInlineImageLooper();
edisonn@google.com3aa35552013-08-14 18:26:20 +00001781 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001782}
1783
edisonn@google.com3aa35552013-08-14 18:26:20 +00001784static SkPdfResult PdfOp_ID(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001785#ifdef ASSERT_BAD_PDF_OPS
1786 SkASSERT(false); // must be processed in inline image looper, but let's
1787 // have the assert when testing good pdfs.
1788#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001789 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001790}
1791
edisonn@google.com3aa35552013-08-14 18:26:20 +00001792static SkPdfResult PdfOp_EI(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001793#ifdef ASSERT_BAD_PDF_OPS
1794 SkASSERT(false); // must be processed in inline image looper, but let's
1795 // have the assert when testing good pdfs.
1796#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00001797 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001798}
1799
edisonn@google.com24cdf132013-07-30 16:06:12 +00001800
1801// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001802SkPdfResult skpdfGraphicsStateApply_ca(SkPdfContext* pdfContext, double ca) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001803 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001804 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001805}
1806
edisonn@google.com3aa35552013-08-14 18:26:20 +00001807SkPdfResult skpdfGraphicsStateApply_CA(SkPdfContext* pdfContext, double CA) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001808 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001809 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001810}
1811
edisonn@google.com3aa35552013-08-14 18:26:20 +00001812SkPdfResult skpdfGraphicsStateApplyLW(SkPdfContext* pdfContext, double lineWidth) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001813 pdfContext->fGraphicsState.fLineWidth = lineWidth;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001814 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001815}
1816
edisonn@google.com3aa35552013-08-14 18:26:20 +00001817SkPdfResult skpdfGraphicsStateApplyLC(SkPdfContext* pdfContext, int64_t lineCap) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001818 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001819 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001820}
1821
edisonn@google.com3aa35552013-08-14 18:26:20 +00001822SkPdfResult skpdfGraphicsStateApplyLJ(SkPdfContext* pdfContext, int64_t lineJoin) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001823 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001824 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001825}
1826
edisonn@google.com3aa35552013-08-14 18:26:20 +00001827SkPdfResult skpdfGraphicsStateApplyML(SkPdfContext* pdfContext, double miterLimit) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001828 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
edisonn@google.com3aa35552013-08-14 18:26:20 +00001829 return kOK_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001830}
1831
1832// TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure
1833/*
18341) [ ] 0 No dash; solid, unbroken lines
18352) [3] 0 3 units on, 3 units off, …
18363) [2] 1 1 on, 2 off, 2 on, 2 off, …
18374) [2 1] 0 2 on, 1 off, 2 on, 1 off, …
18385) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, …
18396) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, …
1840 */
1841
edisonn@google.com3aa35552013-08-14 18:26:20 +00001842SkPdfResult skpdfGraphicsStateApplyD(SkPdfContext* pdfContext, SkPdfArray* intervals, SkPdfNativeObject* phase) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001843 int cnt = intervals->size();
1844 if (cnt >= 256) {
1845 // TODO(edisonn): report error/warning, unsuported;
1846 // TODO(edisonn): alloc memory
edisonn@google.com3aa35552013-08-14 18:26:20 +00001847 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001848 }
1849 for (int i = 0; i < cnt; i++) {
1850 if (!intervals->objAtAIndex(i)->isNumber()) {
1851 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001852 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001853 }
1854 }
1855
edisonn@google.com24cdf132013-07-30 16:06:12 +00001856 double total = 0;
1857 for (int i = 0 ; i < cnt; i++) {
1858 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1859 total += pdfContext->fGraphicsState.fDashArray[i];
1860 }
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001861 if (cnt & 1) {
1862 if (cnt == 1) {
1863 pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0];
1864 cnt++;
1865 } else {
1866 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001867 return kNYI_SkPdfResult;
edisonn@google.comf111a4b2013-07-31 18:22:36 +00001868 }
1869 }
1870 pdfContext->fGraphicsState.fDashArrayLength = cnt;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001871 pdfContext->fGraphicsState.fDashPhase = phase->scalarValue();
1872 if (pdfContext->fGraphicsState.fDashPhase == 0) {
1873 // other rules, changes?
1874 pdfContext->fGraphicsState.fDashPhase = total;
1875 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001876
edisonn@google.com3aa35552013-08-14 18:26:20 +00001877 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001878}
1879
edisonn@google.com3aa35552013-08-14 18:26:20 +00001880SkPdfResult skpdfGraphicsStateApplyD(SkPdfContext* pdfContext, SkPdfArray* dash) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001881 // TODO(edisonn): verify input
1882 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1883 // TODO(edisonn): report error/warning
edisonn@google.com3aa35552013-08-14 18:26:20 +00001884 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001885 }
1886 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1));
1887}
1888
edisonn@google.com3aa35552013-08-14 18:26:20 +00001889void skpdfGraphicsStateApplyFont(SkPdfContext* pdfContext, SkPdfArray* fontAndSize) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001890 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1891 // TODO(edisonn): report error/warning
1892 return;
1893 }
1894 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1895}
1896
1897
1898//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001899static SkPdfResult PdfOp_w(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001900 double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1901 return skpdfGraphicsStateApplyLW(pdfContext, lw);
1902}
1903
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001904//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 +00001905static SkPdfResult PdfOp_J(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001906 int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1907 return skpdfGraphicsStateApplyLC(pdfContext, lc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001908}
1909
1910//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 +00001911static SkPdfResult PdfOp_j(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001912 double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1913 return skpdfGraphicsStateApplyLJ(pdfContext, lj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001914}
1915
1916//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001917static SkPdfResult PdfOp_M(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com24cdf132013-07-30 16:06:12 +00001918 double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1919 return skpdfGraphicsStateApplyML(pdfContext, ml);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001920}
1921
1922//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1923//page 155).
edisonn@google.com3aa35552013-08-14 18:26:20 +00001924static SkPdfResult PdfOp_d(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1925 SkPdfNativeObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1926 SkPdfNativeObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001927
edisonn@google.com24cdf132013-07-30 16:06:12 +00001928 if (!array->isArray()) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00001929 return kIgnoreError_SkPdfResult;
edisonn@google.com24cdf132013-07-30 16:06:12 +00001930 }
1931
1932 return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001933}
1934
1935//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 +00001936static SkPdfResult PdfOp_ri(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001937 pdfContext->fObjectStack.pop();
1938
edisonn@google.com3aa35552013-08-14 18:26:20 +00001939 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001940}
1941
1942//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1943//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1944//fies the output device’s default flatness tolerance.
edisonn@google.com3aa35552013-08-14 18:26:20 +00001945static SkPdfResult PdfOp_i(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001946 pdfContext->fObjectStack.pop();
1947
edisonn@google.com3aa35552013-08-14 18:26:20 +00001948 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001949}
1950
edisonn@google.come878e722013-07-29 19:10:58 +00001951SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1952
1953class InitBlendModes {
1954public:
1955 InitBlendModes() {
1956 // TODO(edisonn): use the python code generator?
1957 // TABLE 7.2 Standard separable blend modes
1958 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1959 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1960 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1961 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1962 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1963 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1964 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1965 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1966 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1967 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1968 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1969 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1970
1971 // TABLE 7.3 Standard nonseparable blend modes
1972 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1973 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1974 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1975 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1976 }
1977};
1978
1979InitBlendModes _gDummyInniter;
1980
1981SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1982 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1983 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1984 return mode;
1985 }
1986
1987 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1988}
1989
edisonn@google.com3aa35552013-08-14 18:26:20 +00001990void skpdfGraphicsStateApplyBM_name(SkPdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001991 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1992 if (mode <= SkXfermode::kLastMode) {
1993 pdfContext->fGraphicsState.fBlendModesLength = 1;
1994 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1995 } else {
1996 // TODO(edisonn): report unknown blend mode
1997 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001998}
1999
edisonn@google.com3aa35552013-08-14 18:26:20 +00002000void skpdfGraphicsStateApplyBM_array(SkPdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00002001 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
2002 // TODO(edisonn): report error/warning
2003 return;
2004 }
2005 SkXfermode::Mode modes[256];
2006 int cnt = blendModes->size();
2007 for (int i = 0; i < cnt; i++) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002008 SkPdfNativeObject* name = blendModes->objAtAIndex(i);
edisonn@google.come878e722013-07-29 19:10:58 +00002009 if (!name->isName()) {
2010 // TODO(edisonn): report error/warning
2011 return;
2012 }
2013 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
2014 if (mode > SkXfermode::kLastMode) {
2015 // TODO(edisonn): report error/warning
2016 return;
2017 }
2018 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002019
edisonn@google.come878e722013-07-29 19:10:58 +00002020 pdfContext->fGraphicsState.fBlendModesLength = cnt;
2021 for (int i = 0; i < cnt; i++) {
2022 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
2023 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002024}
2025
edisonn@google.com3aa35552013-08-14 18:26:20 +00002026void skpdfGraphicsStateApplySMask_dict(SkPdfContext* pdfContext, SkPdfDictionary* sMask) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002027 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00002028 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002029 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00002030 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
2031 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
2032 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
2033 } else {
2034 // TODO (edisonn): report error/warning
2035 }
2036}
2037
edisonn@google.com3aa35552013-08-14 18:26:20 +00002038void skpdfGraphicsStateApplySMask_name(SkPdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002039 if (sMask == "None") {
2040 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002041 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002042 return;
2043 }
2044
edisonn@google.come878e722013-07-29 19:10:58 +00002045 //Next, get the ExtGState Dictionary from the Resource Dictionary:
2046 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
2047
2048 if (extGStateDictionary == NULL) {
2049#ifdef PDF_TRACE
2050 printf("ExtGState is NULL!\n");
2051#endif
2052 // TODO (edisonn): report error/warning
2053 return;
2054 }
2055
edisonn@google.com3aa35552013-08-14 18:26:20 +00002056 SkPdfNativeObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
edisonn@google.come878e722013-07-29 19:10:58 +00002057 if (!obj || !obj->isDictionary()) {
2058 // TODO (edisonn): report error/warning
2059 return;
2060 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002061
2062 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
edisonn@google.comb0145ce2013-08-05 16:23:23 +00002063 pdfContext->fGraphicsState.fSMask = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002064
edisonn@google.come878e722013-07-29 19:10:58 +00002065 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002066}
2067
edisonn@google.com3aa35552013-08-14 18:26:20 +00002068void skpdfGraphicsStateApplyAIS(SkPdfContext* pdfContext, bool alphaSource) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002069 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
2070}
2071
2072
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002073//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
2074//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 +00002075static SkPdfResult PdfOp_gs(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2076 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002077
2078#ifdef PDF_TRACE
2079 std::string str;
2080#endif
2081
2082 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00002083 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002084
2085 if (extGStateDictionary == NULL) {
2086#ifdef PDF_TRACE
2087 printf("ExtGState is NULL!\n");
2088#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00002089 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002090 }
2091
edisonn@google.com3aa35552013-08-14 18:26:20 +00002092 SkPdfNativeObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002093
edisonn@google.com3aa35552013-08-14 18:26:20 +00002094 if (kNone_SkPdfNativeObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
2095 return kIgnoreError_SkPdfResult;
edisonn@google.com571c70b2013-07-10 17:09:50 +00002096 }
2097 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002098
2099 // TODO(edisonn): now load all those properties in graphic state.
2100 if (gs == NULL) {
edisonn@google.com3aa35552013-08-14 18:26:20 +00002101 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002102 }
2103
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002104 if (gs->has_LW()) {
2105 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
2106 }
2107
2108 if (gs->has_LC()) {
2109 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
2110 }
2111
2112 if (gs->has_LJ()) {
2113 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
2114 }
2115
2116 if (gs->has_ML()) {
2117 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
2118 }
2119
2120 if (gs->has_D()) {
2121 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
2122 }
2123
2124 if (gs->has_Font()) {
2125 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
2126 }
2127
2128 if (gs->has_BM()) {
2129 if (gs->isBMAName(pdfContext->fPdfDoc)) {
2130 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
2131 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
2132 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
2133 } else {
2134 // TODO(edisonn): report/warn
2135 }
2136 }
2137
2138 if (gs->has_SMask()) {
2139 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
2140 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
2141 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
2142 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
2143 } else {
2144 // TODO(edisonn): report/warn
2145 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002146 }
2147
2148 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002149 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002150 }
2151
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002152 if (gs->has_CA()) {
2153 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
2154 }
2155
2156 if (gs->has_AIS()) {
2157 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002158 }
2159
edisonn@google.com3aa35552013-08-14 18:26:20 +00002160 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002161}
2162
2163//charSpace Tc Set the character spacing, Tc
2164//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
2165//Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002166SkPdfResult PdfOp_Tc(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002167 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002168 pdfContext->fGraphicsState.fCharSpace = charSpace;
2169
edisonn@google.com3aa35552013-08-14 18:26:20 +00002170 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002171}
2172
2173//wordSpace Tw Set the word spacing, T
2174//w
2175//, to wordSpace, which is a number expressed in unscaled
2176//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
2177//value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002178SkPdfResult PdfOp_Tw(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002179 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002180 pdfContext->fGraphicsState.fWordSpace = wordSpace;
2181
edisonn@google.com3aa35552013-08-14 18:26:20 +00002182 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002183}
2184
2185//scale Tz Set the horizontal scaling, Th
2186//, to (scale ˜ 100). scale is a number specifying the
2187//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002188static SkPdfResult PdfOp_Tz(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002189 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002190
edisonn@google.com3aa35552013-08-14 18:26:20 +00002191 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002192}
2193
2194//render Tr Set the text rendering mode, T
2195//mode, to render, which is an integer. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002196static SkPdfResult PdfOp_Tr(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002197 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002198
edisonn@google.com3aa35552013-08-14 18:26:20 +00002199 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002200}
2201//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
2202//units. Initial value: 0.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002203static SkPdfResult PdfOp_Ts(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002204 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002205
edisonn@google.com3aa35552013-08-14 18:26:20 +00002206 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002207}
2208
2209//wx wy d0
edisonn@google.com3aa35552013-08-14 18:26:20 +00002210static SkPdfResult PdfOp_d0(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002211 pdfContext->fObjectStack.pop();
2212 pdfContext->fObjectStack.pop();
2213
edisonn@google.com3aa35552013-08-14 18:26:20 +00002214 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002215}
2216
2217//wx wy llx lly urx ury d1
edisonn@google.com3aa35552013-08-14 18:26:20 +00002218static SkPdfResult PdfOp_d1(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002219 pdfContext->fObjectStack.pop();
2220 pdfContext->fObjectStack.pop();
2221 pdfContext->fObjectStack.pop();
2222 pdfContext->fObjectStack.pop();
2223 pdfContext->fObjectStack.pop();
2224 pdfContext->fObjectStack.pop();
2225
edisonn@google.com3aa35552013-08-14 18:26:20 +00002226 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002227}
2228
2229//name sh
edisonn@google.com3aa35552013-08-14 18:26:20 +00002230static SkPdfResult PdfOp_sh(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002231 pdfContext->fObjectStack.pop();
2232
edisonn@google.com3aa35552013-08-14 18:26:20 +00002233 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002234}
2235
2236//name Do
edisonn@google.com3aa35552013-08-14 18:26:20 +00002237static SkPdfResult PdfOp_Do(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2238 SkPdfNativeObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002239
edisonn@google.com571c70b2013-07-10 17:09:50 +00002240 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002241
2242 if (xObject == NULL) {
2243#ifdef PDF_TRACE
2244 printf("XObject is NULL!\n");
2245#endif
edisonn@google.com3aa35552013-08-14 18:26:20 +00002246 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002247 }
2248
edisonn@google.com3aa35552013-08-14 18:26:20 +00002249 SkPdfNativeObject* value = xObject->get(name);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002250 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002251
2252#ifdef PDF_TRACE
2253// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00002254// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002255#endif
2256
edisonn@google.com571c70b2013-07-10 17:09:50 +00002257 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002258}
2259
2260//tag MP Designate a marked-content point. tag is a name object indicating the role or
2261//significance of the point.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002262static SkPdfResult PdfOp_MP(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002263 pdfContext->fObjectStack.pop();
2264
edisonn@google.com3aa35552013-08-14 18:26:20 +00002265 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002266}
2267
2268//tag properties DP Designate a marked-content point with an associated property list. tag is a
2269//name object indicating the role or significance of the point; properties is
2270//either an inline dictionary containing the property list or a name object
2271//associated with it in the Properties subdictionary of the current resource
2272//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.com3aa35552013-08-14 18:26:20 +00002273static SkPdfResult PdfOp_DP(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002274 pdfContext->fObjectStack.pop();
2275 pdfContext->fObjectStack.pop();
2276
edisonn@google.com3aa35552013-08-14 18:26:20 +00002277 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002278}
2279
2280//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
2281//tag is a name object indicating the role or significance of the sequence.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002282static SkPdfResult PdfOp_BMC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002283 pdfContext->fObjectStack.pop();
2284
edisonn@google.com3aa35552013-08-14 18:26:20 +00002285 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002286}
2287
2288//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
2289//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
2290//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 +00002291static SkPdfResult PdfOp_BDC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002292 pdfContext->fObjectStack.pop();
2293 pdfContext->fObjectStack.pop();
2294
edisonn@google.com3aa35552013-08-14 18:26:20 +00002295 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002296}
2297
2298//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.com3aa35552013-08-14 18:26:20 +00002299static SkPdfResult PdfOp_EMC(SkPdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
2300 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002301}
2302
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002303static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002304 static bool gInitialized = false;
2305 if (gInitialized) {
2306 return;
2307 }
2308
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002309 gPdfOps.set("q", PdfOp_q);
2310 gPdfOps.set("Q", PdfOp_Q);
2311 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002312
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002313 gPdfOps.set("TD", PdfOp_TD);
2314 gPdfOps.set("Td", PdfOp_Td);
2315 gPdfOps.set("Tm", PdfOp_Tm);
2316 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002317
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002318 gPdfOps.set("m", PdfOp_m);
2319 gPdfOps.set("l", PdfOp_l);
2320 gPdfOps.set("c", PdfOp_c);
2321 gPdfOps.set("v", PdfOp_v);
2322 gPdfOps.set("y", PdfOp_y);
2323 gPdfOps.set("h", PdfOp_h);
2324 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002325
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002326 gPdfOps.set("S", PdfOp_S);
2327 gPdfOps.set("s", PdfOp_s);
2328 gPdfOps.set("f", PdfOp_f);
2329 gPdfOps.set("F", PdfOp_F);
2330 gPdfOps.set("f*", PdfOp_f_star);
2331 gPdfOps.set("B", PdfOp_B);
2332 gPdfOps.set("B*", PdfOp_B_star);
2333 gPdfOps.set("b", PdfOp_b);
2334 gPdfOps.set("b*", PdfOp_b_star);
2335 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002336
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002337 gPdfOps.set("BT", PdfOp_BT);
2338 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002339
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002340 gPdfOps.set("Tj", PdfOp_Tj);
2341 gPdfOps.set("'", PdfOp_quote);
2342 gPdfOps.set("\"", PdfOp_doublequote);
2343 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002344
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002345 gPdfOps.set("CS", PdfOp_CS);
2346 gPdfOps.set("cs", PdfOp_cs);
2347 gPdfOps.set("SC", PdfOp_SC);
2348 gPdfOps.set("SCN", PdfOp_SCN);
2349 gPdfOps.set("sc", PdfOp_sc);
2350 gPdfOps.set("scn", PdfOp_scn);
2351 gPdfOps.set("G", PdfOp_G);
2352 gPdfOps.set("g", PdfOp_g);
2353 gPdfOps.set("RG", PdfOp_RG);
2354 gPdfOps.set("rg", PdfOp_rg);
2355 gPdfOps.set("K", PdfOp_K);
2356 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002357
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002358 gPdfOps.set("W", PdfOp_W);
2359 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002360
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002361 gPdfOps.set("BX", PdfOp_BX);
2362 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002363
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002364 gPdfOps.set("BI", PdfOp_BI);
2365 gPdfOps.set("ID", PdfOp_ID);
2366 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002367
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002368 gPdfOps.set("w", PdfOp_w);
2369 gPdfOps.set("J", PdfOp_J);
2370 gPdfOps.set("j", PdfOp_j);
2371 gPdfOps.set("M", PdfOp_M);
2372 gPdfOps.set("d", PdfOp_d);
2373 gPdfOps.set("ri", PdfOp_ri);
2374 gPdfOps.set("i", PdfOp_i);
2375 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002376
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002377 gPdfOps.set("Tc", PdfOp_Tc);
2378 gPdfOps.set("Tw", PdfOp_Tw);
2379 gPdfOps.set("Tz", PdfOp_Tz);
2380 gPdfOps.set("TL", PdfOp_TL);
2381 gPdfOps.set("Tf", PdfOp_Tf);
2382 gPdfOps.set("Tr", PdfOp_Tr);
2383 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002384
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002385 gPdfOps.set("d0", PdfOp_d0);
2386 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002387
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002388 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002389
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002390 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002391
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002392 gPdfOps.set("MP", PdfOp_MP);
2393 gPdfOps.set("DP", PdfOp_DP);
2394 gPdfOps.set("BMC", PdfOp_BMC);
2395 gPdfOps.set("BDC", PdfOp_BDC);
2396 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002397
2398 gInitialized = true;
2399}
2400
2401class InitPdfOps {
2402public:
2403 InitPdfOps() {
2404 initPdfOperatorRenderes();
2405 }
2406};
2407
2408InitPdfOps gInitPdfOps;
2409
2410void reportPdfRenderStats() {
2411 std::map<std::string, int>::iterator iter;
2412
edisonn@google.com3aa35552013-08-14 18:26:20 +00002413 for (int i = 0 ; i < kCount_SkPdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002414 SkTDict<int>::Iter iter(gRenderStats[i]);
2415 const char* key;
2416 int value = 0;
2417 while ((key = iter.next(&value)) != NULL) {
2418 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002419 }
2420 }
2421}
2422
edisonn@google.com3aa35552013-08-14 18:26:20 +00002423SkPdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002424 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002425 {
2426 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002427 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002428 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002429 // caller, main work is done by pdfOperatorRenderer(...)
2430 PdfTokenLooper* childLooper = NULL;
edisonn@google.com3aa35552013-08-14 18:26:20 +00002431 SkPdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002432
2433 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002434 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2435 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002436
2437 if (childLooper) {
2438 childLooper->setUp(this);
2439 childLooper->loop();
2440 delete childLooper;
2441 }
2442 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002443 int cnt = 0;
edisonn@google.com3aa35552013-08-14 18:26:20 +00002444 gRenderStats[kUnsupported_SkPdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2445 gRenderStats[kUnsupported_SkPdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002446 }
2447 }
2448 else if (token.fType == kObject_TokenType)
2449 {
2450 fPdfContext->fObjectStack.push( token.fObject );
2451 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002452 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002453 // TODO(edisonn): deine or use assert not reached
edisonn@google.com3aa35552013-08-14 18:26:20 +00002454 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002455 }
edisonn@google.com3aa35552013-08-14 18:26:20 +00002456 return kOK_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002457}
2458
2459void PdfMainLooper::loop() {
2460 PdfToken token;
2461 while (readToken(fTokenizer, &token)) {
2462 consumeToken(token);
2463 }
2464}
2465
edisonn@google.com3aa35552013-08-14 18:26:20 +00002466SkPdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002467 SkASSERT(false);
edisonn@google.com3aa35552013-08-14 18:26:20 +00002468 return kIgnoreError_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002469}
2470
2471void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002472 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002473}
2474
edisonn@google.com3aa35552013-08-14 18:26:20 +00002475SkPdfResult PdfInlineImageLooper::done() {
2476 return kNYI_SkPdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002477}
2478
edisonn@google.com3aa35552013-08-14 18:26:20 +00002479SkPdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002480 return fParent->consumeToken(token);
2481}
2482
2483void PdfCompatibilitySectionLooper::loop() {
2484 // TODO(edisonn): save stacks position, or create a new stack?
2485 // TODO(edisonn): what happens if we pop out more variables then when we started?
2486 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2487 // pop-ing too much will not affect outside the section.
2488 PdfToken token;
2489 while (readToken(fTokenizer, &token)) {
2490 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2491 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2492 looper->setUp(this);
2493 looper->loop();
2494 delete looper;
2495 } else {
2496 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2497 fParent->consumeToken(token);
2498 }
2499 }
2500 // TODO(edisonn): restore stack.
2501}
2502
2503// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2504// TODO(edisonn): Add API for Forms viewing and editing
2505// e.g. SkBitmap getPage(int page);
2506// int formsCount();
2507// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2508// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2509// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2510// resources needed, so the preview is fast.
2511// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2512// references automatically.
2513
edisonn@google.com3aa35552013-08-14 18:26:20 +00002514SkPdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002515
edisonn@google.com444e25a2013-07-11 15:20:50 +00002516bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002517 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002518 return false;
2519 }
2520
edisonn@google.com222382b2013-07-10 22:33:10 +00002521 if (page < 0 || page >= pages()) {
2522 return false;
2523 }
2524
edisonn@google.com3aa35552013-08-14 18:26:20 +00002525 SkPdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002526
edisonn@google.com222382b2013-07-10 22:33:10 +00002527 pdfContext.fOriginalMatrix = SkMatrix::I();
2528 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2529
2530 gPdfContext = &pdfContext;
2531
2532 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002533 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002534 SkScalar w = dst.width();
2535 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002536
edisonn@google.com444e25a2013-07-11 15:20:50 +00002537 SkScalar wp = fPdfDoc->MediaBox(page).width();
2538 SkScalar hp = fPdfDoc->MediaBox(page).height();
2539
2540 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 +00002541// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2542
2543 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2544 // TODO(edisonn): add flagg for no clipping.
2545 // Use larger image to make sure we do not draw anything outside of page
2546 // could be used in tests.
2547
2548#ifdef PDF_DEBUG_3X
2549 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)};
2550#else
2551 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2552#endif
2553 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2554 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2555
2556 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2557 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2558
2559 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2560 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2561
2562 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2563 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2564
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002565 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
edisonn@google.com0f901902013-08-07 11:56:16 +00002566 pdfContext.fGraphicsState.fContentStreamMatrix = pdfContext.fOriginalMatrix;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002567 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2568 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002569
edisonn@google.com222382b2013-07-10 22:33:10 +00002570#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002571 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002572#endif
2573
edisonn@google.com444e25a2013-07-11 15:20:50 +00002574 canvas->setMatrix(pdfContext.fOriginalMatrix);
2575
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002576 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2577
2578 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002579// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002580// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002581// canvas->drawRect(rect, paint);
2582
edisonn@google.com222382b2013-07-10 22:33:10 +00002583
2584 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002585 return true;
2586}
edisonn@google.com222382b2013-07-10 22:33:10 +00002587
2588bool SkPdfRenderer::load(const SkString inputFileName) {
2589 unload();
2590
2591 // TODO(edisonn): create static function that could return NULL if there are errors
edisonn@google.com3aa35552013-08-14 18:26:20 +00002592 fPdfDoc = new SkPdfNativeDoc(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002593 if (fPdfDoc->pages() == 0) {
2594 delete fPdfDoc;
2595 fPdfDoc = NULL;
2596 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002597
2598 return fPdfDoc != NULL;
2599}
2600
edisonn@google.com147adb12013-07-24 15:56:19 +00002601bool SkPdfRenderer::load(SkStream* stream) {
2602 unload();
2603
2604 // TODO(edisonn): create static function that could return NULL if there are errors
edisonn@google.com3aa35552013-08-14 18:26:20 +00002605 fPdfDoc = new SkPdfNativeDoc(stream);
edisonn@google.com147adb12013-07-24 15:56:19 +00002606 if (fPdfDoc->pages() == 0) {
2607 delete fPdfDoc;
2608 fPdfDoc = NULL;
2609 }
2610
2611 return fPdfDoc != NULL;
2612}
2613
2614
edisonn@google.com222382b2013-07-10 22:33:10 +00002615int SkPdfRenderer::pages() const {
2616 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2617}
2618
2619void SkPdfRenderer::unload() {
2620 delete fPdfDoc;
2621 fPdfDoc = NULL;
2622}
2623
2624SkRect SkPdfRenderer::MediaBox(int page) const {
2625 SkASSERT(fPdfDoc);
2626 return fPdfDoc->MediaBox(page);
2627}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002628
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002629size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002630 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2631}
edisonn@google.com147adb12013-07-24 15:56:19 +00002632
2633bool SkPDFNativeRenderToBitmap(SkStream* stream,
2634 SkBitmap* output,
2635 int page,
2636 SkPdfContent content,
2637 double dpi) {
2638 SkASSERT(page >= 0);
2639 SkPdfRenderer renderer;
2640 renderer.load(stream);
2641 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2642 return false;
2643 }
2644
2645 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2646
2647 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2648 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2649
2650 rect = SkRect::MakeWH(width, height);
2651
2652 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2653
2654 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2655 SkCanvas canvas(device);
2656
2657 return renderer.renderPage(page, &canvas, rect);
2658}