blob: e7bfdb6c6d9a83d0d0340a2b15e9320674334649 [file] [log] [blame]
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCanvas.h"
9#include "SkDevice.h"
10#include "SkForceLinking.h"
11#include "SkGraphics.h"
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
14#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTypeface.h"
18#include "SkTArray.h"
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000019#include "SkTDict.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000020
edisonn@google.com15b11182013-07-11 14:43:15 +000021#include "SkPdfBasics.h"
22#include "SkPdfNativeTokenizer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000023#include <cstdio>
24#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000025#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000026
edisonn@google.com15b11182013-07-11 14:43:15 +000027extern "C" PdfContext* gPdfContext;
28extern "C" SkBitmap* gDumpBitmap;
29extern "C" SkCanvas* gDumpCanvas;
30
edisonn@google.com131d4ee2013-06-26 17:48:12 +000031__SK_FORCE_IMAGE_DECODER_LINKING;
32
33// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
34// keep for each object pos in file
35// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
36
37// TODO(edisonn): security - validate all the user input, all pdf!
38
edisonn@google.com6e49c342013-06-27 20:03:43 +000039// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
40// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000041
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
43/*
44#ifdef PDF_TRACE
45 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000046 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000047 printf("Print Tf Resources: %s\n", str.c_str());
48#endif
49 */
50
edisonn@google.com131d4ee2013-06-26 17:48:12 +000051#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000052#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000053#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000054
55#include "SkPdfBasics.h"
56#include "SkPdfUtils.h"
57
58#include "SkPdfFont.h"
59
edisonn@google.com131d4ee2013-06-26 17:48:12 +000060/*
61 * TODO(edisonn):
62 * - all font types and all ppdf font features
63 * - word spacing
64 * - load font for baidu.pdf
65 * - load font for youtube.pdf
66 * - parser for pdf from the definition already available in pdfspec_autogen.py
67 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000068 * - encapsulate native in the pdf api so the skpdf does not know anything about native ... in progress
edisonn@google.com131d4ee2013-06-26 17:48:12 +000069 * - load gs/ especially smask and already known prop (skp) ... in progress
70 * - wrapper on classes for customizations? e.g.
71 * SkPdfPageObjectVanila - has only the basic loaders/getters
72 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
73 * need to find a nice object model for all this with constructors and factories
74 * - deal with inheritable automatically ?
75 * - deal with specific type in spec directly, add all dictionary types to known types
76*/
77
78using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000079
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000080NotOwnedString strings_DeviceRGB;
81NotOwnedString strings_DeviceCMYK;
edisonn@google.com222382b2013-07-10 22:33:10 +000082
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000083class StringsInit {
84public:
85 StringsInit() {
86 NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB");
87 NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK");
88 }
89};
90
91StringsInit gStringsInit;
edisonn@google.com222382b2013-07-10 22:33:10 +000092
93// TODO(edisonn): Document PdfTokenLooper and subclasses.
94class PdfTokenLooper {
95protected:
96 PdfTokenLooper* fParent;
97 SkPdfNativeTokenizer* fTokenizer;
98 PdfContext* fPdfContext;
99 SkCanvas* fCanvas;
100
101public:
102 PdfTokenLooper(PdfTokenLooper* parent,
103 SkPdfNativeTokenizer* tokenizer,
104 PdfContext* pdfContext,
105 SkCanvas* canvas)
106 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
107
108 virtual ~PdfTokenLooper() {}
109
110 virtual PdfResult consumeToken(PdfToken& token) = 0;
111 virtual void loop() = 0;
112
113 void setUp(PdfTokenLooper* parent) {
114 fParent = parent;
115 fTokenizer = parent->fTokenizer;
116 fPdfContext = parent->fPdfContext;
117 fCanvas = parent->fCanvas;
118 }
edisonn@google.com78b38b12013-07-15 18:20:58 +0000119
120 SkPdfNativeTokenizer* tokenizer() { return fTokenizer; }
edisonn@google.com222382b2013-07-10 22:33:10 +0000121};
122
123class PdfMainLooper : public PdfTokenLooper {
124public:
125 PdfMainLooper(PdfTokenLooper* parent,
126 SkPdfNativeTokenizer* tokenizer,
127 PdfContext* pdfContext,
128 SkCanvas* canvas)
129 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
130
131 virtual PdfResult consumeToken(PdfToken& token);
132 virtual void loop();
133};
134
135class PdfInlineImageLooper : public PdfTokenLooper {
136public:
137 PdfInlineImageLooper()
138 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
139
140 virtual PdfResult consumeToken(PdfToken& token);
141 virtual void loop();
142 PdfResult done();
143};
144
145class PdfCompatibilitySectionLooper : public PdfTokenLooper {
146public:
147 PdfCompatibilitySectionLooper()
148 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
149
150 virtual PdfResult consumeToken(PdfToken& token);
151 virtual void loop();
152};
153
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000154// Utilities
155static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
156 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
157
158 bitmap->allocPixels();
159 bitmap->eraseColor(color);
160}
161
162// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000163static int GetColorSpaceComponents(NotOwnedString& colorSpace) {
164 if (colorSpace.equals("DeviceCMYK")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000165 return 4;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000166 } else if (colorSpace.equals("DeviceGray") ||
167 colorSpace.equals("CalGray") ||
168 colorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000169 return 1;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000170 } else if (colorSpace.equals("DeviceRGB") ||
171 colorSpace.equals("CalRGB") ||
172 colorSpace.equals("Lab")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000173 return 3;
174 } else {
175 return 0;
176 }
177}
178
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000179SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000180 SkMatrix matrix;
181 matrix.setAll(SkDoubleToScalar(array[0]),
182 SkDoubleToScalar(array[2]),
183 SkDoubleToScalar(array[4]),
184 SkDoubleToScalar(array[1]),
185 SkDoubleToScalar(array[3]),
186 SkDoubleToScalar(array[5]),
187 SkDoubleToScalar(0),
188 SkDoubleToScalar(0),
189 SkDoubleToScalar(1));
190
191 return matrix;
192}
193
194SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
195 double array[6];
196
197 // TODO(edisonn): security issue, ret if size() != 6
198 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000199 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000200 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000201 return SkMatrix::I(); // TODO(edisonn): report issue
202 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000203 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000204 }
205
206 return SkMatrixFromPdfMatrix(array);
207}
208
edisonn@google.com222382b2013-07-10 22:33:10 +0000209
210extern "C" SkNativeParsedPDF* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000211SkBitmap* gDumpBitmap = NULL;
212SkCanvas* gDumpCanvas = NULL;
213char gLastKeyword[100] = "";
214int gLastOpKeyword = -1;
215char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
216int gReadOp = 0;
217
218
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000219#ifdef PDF_TRACE_DIFF_IN_PNG
220static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000221 return true;
222 if (*pdfOp == '\0') return false;
223
224 char markedPdfOp[100] = ",";
225 strcat(markedPdfOp, pdfOp);
226 strcat(markedPdfOp, ",");
227
228 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
229}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000230#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000231
edisonn@google.com222382b2013-07-10 22:33:10 +0000232
233
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000234// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000235static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000236 bool ret = fTokenizer->readToken(token);
237
238 gReadOp++;
239
240#ifdef PDF_TRACE_DIFF_IN_PNG
241 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
242 // the numbar and name of last operation, so the file name will reflect op that changed.
243 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
244 gDumpCanvas->flush();
245
246 SkBitmap bitmap;
247 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
248
249 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
250
251 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
252 SkCanvas canvas(device);
253
254 // draw context stuff here
255 SkPaint blueBorder;
256 blueBorder.setColor(SK_ColorBLUE);
257 blueBorder.setStyle(SkPaint::kStroke_Style);
258 blueBorder.setTextSize(SkDoubleToScalar(20));
259
260 SkString str;
261
262 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
263 if (clipStack) {
264 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
265 const SkClipStack::Element* elem;
266 double y = 0;
267 int total = 0;
268 while (elem = iter.next()) {
269 total++;
270 y += 30;
271
272 switch (elem->getType()) {
273 case SkClipStack::Element::kRect_Type:
274 canvas.drawRect(elem->getRect(), blueBorder);
275 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
276 break;
277 case SkClipStack::Element::kPath_Type:
278 canvas.drawPath(elem->getPath(), blueBorder);
279 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
280 break;
281 case SkClipStack::Element::kEmpty_Type:
282 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
283 break;
284 default:
285 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
286 break;
287 }
288 }
289
290 y += 30;
291 str.printf("Number of clips in stack: %i", total);
292 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
293 }
294
295 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
296 SkPath clipPath;
297 if (clipRegion.getBoundaryPath(&clipPath)) {
298 SkPaint redBorder;
299 redBorder.setColor(SK_ColorRED);
300 redBorder.setStyle(SkPaint::kStroke_Style);
301 canvas.drawPath(clipPath, redBorder);
302 }
303
304 canvas.flush();
305
306 SkString out;
307
308 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
309 // ... and other properties, to be able to debug th code easily
310
311 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
312 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
313 }
314
315 if (token->fType == kKeyword_TokenType) {
316 strcpy(gLastKeyword, token->fKeyword);
317 gLastOpKeyword = gReadOp;
318 } else {
319 strcpy(gLastKeyword, "");
320 }
321#endif
322
323 return ret;
324}
325
326
327
328typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
329
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000330SkTDict<PdfOperatorRenderer> gPdfOps(100);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000331
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000332
333template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> {
334public:
335 SkTDictWithDefaultConstructor() : SkTDict<T>(10) {}
336};
337
338SkTDictWithDefaultConstructor<int> gRenderStats[kCount_PdfResult];
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000339
edisonn@google.com571c70b2013-07-10 17:09:50 +0000340const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000341 "Success",
342 "Partially implemented",
343 "Not yet implemented",
344 "Ignore Error",
345 "Error",
346 "Unsupported/Unknown"
347};
348
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000349static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000350 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000351 SkCanvas* canvas)
352{
353
354 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
355 if (skfont == NULL) {
356 skfont = SkPdfFont::Default();
357 }
358
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000359
edisonn@google.com571c70b2013-07-10 17:09:50 +0000360 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000361 // TODO(edisonn): report warning
362 return kIgnoreError_PdfResult;
363 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000364 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000365
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000366 SkUnencodedText binary(str);
367
368 SkDecodedText decoded;
369
370 if (skfont->encoding() == NULL) {
371 // TODO(edisonn): report warning
372 return kNYI_PdfResult;
373 }
374
375 skfont->encoding()->decodeText(binary, &decoded);
376
377 SkPaint paint;
378 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
379 // Or maybe just not call setTextSize at all?
380 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
381 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
382 }
383
384// if (fCurFont && fCurFont->GetFontScale() != 0) {
385// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
386// }
387
388 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
389
390 canvas->save();
391
392#if 1
393 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
394
395 SkPoint point1;
396 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
397
398 SkMatrix mirror;
399 mirror.setTranslate(0, -point1.y());
400 // TODO(edisonn): fix rotated text, and skewed too
401 mirror.postScale(SK_Scalar1, -SK_Scalar1);
402 // TODO(edisonn): post rotate, skew
403 mirror.postTranslate(0, point1.y());
404
405 matrix.postConcat(mirror);
406
407 canvas->setMatrix(matrix);
408
409 SkTraceMatrix(matrix, "mirrored");
410#endif
411
edisonn@google.com6e49c342013-06-27 20:03:43 +0000412 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000413 canvas->restore();
414
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000415 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000416}
417
418// TODO(edisonn): create header files with declarations!
419PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
420PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
421PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
422PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
423
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000424// TODO(edisonn): perf!!!
425
426static SkColorTable* getGrayColortable() {
427 static SkColorTable* grayColortable = NULL;
428 if (grayColortable == NULL) {
429 SkPMColor* colors = new SkPMColor[256];
430 for (int i = 0 ; i < 256; i++) {
431 colors[i] = SkPreMultiplyARGB(255, i, i, i);
432 }
433 grayColortable = new SkColorTable(colors, 256);
434 }
435 return grayColortable;
436}
437
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000438static SkBitmap transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000439 int width, int height, int bytesPerLine,
440 int bpc, const std::string& colorSpace,
441 bool transparencyMask) {
442 SkBitmap bitmap;
443
edisonn@google.com571c70b2013-07-10 17:09:50 +0000444 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000445//#define MAX_COMPONENTS 10
446
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000447 // TODO(edisonn): assume start of lines are aligned at 32 bits?
448 // Is there a faster way to load the uncompressed stream into a bitmap?
449
450 // minimal support for now
451 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
452 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
453
454 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000455 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000456 for (int w = 0 ; w < width; w++) {
457 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
458 uncompressedStream[3 * w + 1],
459 uncompressedStream[3 * w + 2]);
460 i++;
461 }
462 uncompressedStream += bytesPerLine;
463 }
464
465 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
466 bitmap.setPixels(uncompressedStreamArgb);
467 }
468 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
469 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
470
471 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000472 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000473 for (int w = 0 ; w < width; w++) {
474 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
475 uncompressedStream[w];
476 i++;
477 }
478 uncompressedStream += bytesPerLine;
479 }
480
481 bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
482 width, height);
483 bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
484 }
485
486 // TODO(edisonn): Report Warning, NYI, or error
487 return bitmap;
488}
489
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000490// utils
491
492// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
493// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
494// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
495// skia format, through a table
496
497// this functions returns the image, it does not look at the smask.
498
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000499static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000500 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000501 // TODO(edisonn): report warning to be used in testing.
502 return SkBitmap();
503 }
504
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000505 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
506 int64_t width = image->Width(pdfContext->fPdfDoc);
507 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000508 std::string colorSpace = "DeviceRGB";
509
510 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000511 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
512 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000513 }
514
515/*
516 bool imageMask = image->imageMask();
517
518 if (imageMask) {
519 if (bpc != 0 && bpc != 1) {
520 // TODO(edisonn): report warning to be used in testing.
521 return SkBitmap();
522 }
523 bpc = 1;
524 }
525*/
526
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000527 const unsigned char* uncompressedStream = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000528 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000529
edisonn@google.com571c70b2013-07-10 17:09:50 +0000530 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000531
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000532 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000533 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000534 // TODO(edisonn): report warning to be used in testing.
535 return SkBitmap();
536 }
537
edisonn@google.com571c70b2013-07-10 17:09:50 +0000538 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
539
540 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
541 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
542 (streamDict->isFilterAArray(NULL) &&
543 streamDict->getFilterAsArray(NULL)->size() > 0 &&
544 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
545 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
546 SkBitmap bitmap;
547 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap);
548 return bitmap;
549 }
550
551
552
553 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
554// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
555// obj.GetDictionary().GetKey(PdfName("Filter")));
556// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
557// value = resolveReferenceObject(pdfContext->fPdfDoc,
558// &value->GetArray()[0]);
559// }
560// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
561// SkStream stream = SkStream::
562// SkImageDecoder::Factory()
563// }
564
edisonn@google.com96ba3aa2013-07-28 20:04:35 +0000565 int bytesPerLine = (int)(uncompressedStreamLength / height);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000566#ifdef PDF_TRACE
567 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000568 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000569 }
570#endif
571
572 SkBitmap bitmap = transferImageStreamToBitmap(
573 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000574 (int)width, (int)height, bytesPerLine,
575 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000576 transparencyMask);
577
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000578 return bitmap;
579}
580
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000581static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000582 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000583
584 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000585 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000586 }
587
588 // TODO(edisonn): implement GS SMask. Default to empty right now.
589 return pdfContext->fGraphicsState.fSMask;
590}
591
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000592static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000593 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000594 return kIgnoreError_PdfResult;
595 }
596
597 SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false);
598 SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage);
599
600 canvas->save();
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000601 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000602
603#if 1
604 SkScalar z = SkIntToScalar(0);
605 SkScalar one = SkIntToScalar(1);
606
607 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
608 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
609 SkMatrix flip;
610 SkAssertResult(flip.setPolyToPoly(from, to, 4));
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000611 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000612 solveImageFlip.preConcat(flip);
613 canvas->setMatrix(solveImageFlip);
614#endif
615
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000616 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
617
618 if (sMask.empty()) {
619 canvas->drawBitmapRect(image, dst, NULL);
620 } else {
621 canvas->saveLayer(&dst, NULL);
622 canvas->drawBitmapRect(image, dst, NULL);
623 SkPaint xfer;
624 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
625 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
626 canvas->drawBitmapRect(sMask, dst, &xfer);
627 canvas->restore();
628 }
629
630 canvas->restore();
631
632 return kPartial_PdfResult;
633}
634
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000635
636
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000637
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000638static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000639 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000640 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000641 }
642
643 PdfOp_q(pdfContext, canvas, NULL);
644 canvas->save();
645
646
edisonn@google.com571c70b2013-07-10 17:09:50 +0000647 if (skobj->Resources(pdfContext->fPdfDoc)) {
648 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000649 }
650
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000651 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000652
edisonn@google.com571c70b2013-07-10 17:09:50 +0000653 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000654 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
655 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
656 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000657 // TODO(edisonn) reset matrixTm and matricTlm also?
658 }
659
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000660 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000661
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000662 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000663
edisonn@google.com571c70b2013-07-10 17:09:50 +0000664 if (skobj->has_BBox()) {
665 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000666 }
667
668 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
669 // For this PdfContentsTokenizer needs to be extended.
670
edisonn@google.com571c70b2013-07-10 17:09:50 +0000671 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000672
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000673 SkPdfNativeTokenizer* tokenizer =
674 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000675 if (tokenizer != NULL) {
676 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
677 looper.loop();
678 delete tokenizer;
679 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000680
681 // TODO(edisonn): should we restore the variable stack at the same state?
682 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
683 canvas->restore();
684 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000685 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000686}
687
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000688//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
689// return kNYI_PdfResult;
690//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000691
edisonn@google.com571c70b2013-07-10 17:09:50 +0000692PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
693 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000694 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000695 }
696
697 PdfOp_q(pdfContext, canvas, NULL);
698 canvas->save();
699
700 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
701 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
702
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000703 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
704 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000705
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000706 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000707
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000708 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000709
710 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000711 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000712
713 SkTraceRect(rm, "bbox mapped");
714
715 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
716
717 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
718 // For this PdfContentsTokenizer needs to be extended.
719
edisonn@google.com571c70b2013-07-10 17:09:50 +0000720 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000721
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000722 SkPdfNativeTokenizer* tokenizer =
723 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000724 if (tokenizer != NULL) {
725 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
726 looper.loop();
727 delete tokenizer;
728 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000729
730 // TODO(edisonn): should we restore the variable stack at the same state?
731 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
732 canvas->restore();
733 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000734
735 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000736}
737
738
edisonn@google.com571c70b2013-07-10 17:09:50 +0000739// TODO(edisonn): make sure the pointer is unique
740std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000741
742class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000743 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000744public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000745 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000746 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000747 }
748
749 ~CheckRecursiveRendering() {
750 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000751 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000752 }
753
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000754 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000755 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000756 }
757};
758
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000759static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000760 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000761 // Oops, corrupt PDF!
762 return kIgnoreError_PdfResult;
763 }
764
edisonn@google.com571c70b2013-07-10 17:09:50 +0000765 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000766
edisonn@google.com571c70b2013-07-10 17:09:50 +0000767 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000768 {
769 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000770 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000771 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000772 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000773 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
774 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000775 default:
776 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000777 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000778}
779
780PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
781 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
782 canvas->save();
783 return kOK_PdfResult;
784}
785
786PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
787 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
788 pdfContext->fStateStack.pop();
789 canvas->restore();
790 return kOK_PdfResult;
791}
792
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000793static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000794 double array[6];
795 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000796 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000797 pdfContext->fObjectStack.pop();
798 }
799
800 // a b
801 // c d
802 // e f
803
804 // 0 1
805 // 2 3
806 // 4 5
807
808 // sx ky
809 // kx sy
810 // tx ty
811 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
812
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000813 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000814
815#ifdef PDF_TRACE
816 printf("cm ");
817 for (int i = 0 ; i < 6 ; i++) {
818 printf("%f ", array[i]);
819 }
820 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000821 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000822#endif
823
824 return kOK_PdfResult;
825}
826
827//leading TL Set the text leading, Tl
828//, to leading, which is a number expressed in unscaled text
829//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000830static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000831 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000832
833 pdfContext->fGraphicsState.fTextLeading = ty;
834
835 return kOK_PdfResult;
836}
837
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000838static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000839 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
840 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000841
842 double array[6] = {1, 0, 0, 1, tx, ty};
843 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
844
845 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
846 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
847
848 return kPartial_PdfResult;
849}
850
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000851static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000852 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
853 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000854
edisonn@google.com571c70b2013-07-10 17:09:50 +0000855 // TODO(edisonn): Create factory methods or constructors so native is hidden
856 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000857 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000858
859 PdfOp_TL(pdfContext, canvas, looper);
860
edisonn@google.com571c70b2013-07-10 17:09:50 +0000861 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000862 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000863
edisonn@google.com571c70b2013-07-10 17:09:50 +0000864 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000865 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000866
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000867 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
868
869 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
870 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000871
872 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000873}
874
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000875static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000876 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
877 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
878 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
879 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
880 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
881 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000882
883 double array[6];
884 array[0] = a;
885 array[1] = b;
886 array[2] = c;
887 array[3] = d;
888 array[4] = e;
889 array[5] = f;
890
891 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000892 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000893
894 // TODO(edisonn): Text positioning.
895 pdfContext->fGraphicsState.fMatrixTm = matrix;
896 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
897
898 return kPartial_PdfResult;
899}
900
901//— T* Move to the start of the next line. This operator has the same effect as the code
902//0 Tl Td
903//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000904static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000905 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
906 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000907
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000908 pdfContext->fObjectStack.push(zero);
909 pdfContext->fObjectStack.push(tl);
910
911 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
912
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000913 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000914}
915
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000916static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000917 if (pdfContext->fGraphicsState.fPathClosed) {
918 pdfContext->fGraphicsState.fPath.reset();
919 pdfContext->fGraphicsState.fPathClosed = false;
920 }
921
edisonn@google.com571c70b2013-07-10 17:09:50 +0000922 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
923 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000924
925 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
926 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
927
928 return kOK_PdfResult;
929}
930
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000931static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000932 if (pdfContext->fGraphicsState.fPathClosed) {
933 pdfContext->fGraphicsState.fPath.reset();
934 pdfContext->fGraphicsState.fPathClosed = false;
935 }
936
edisonn@google.com571c70b2013-07-10 17:09:50 +0000937 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
938 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000939
940 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
941 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
942
943 return kOK_PdfResult;
944}
945
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000946static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000947 if (pdfContext->fGraphicsState.fPathClosed) {
948 pdfContext->fGraphicsState.fPath.reset();
949 pdfContext->fGraphicsState.fPathClosed = false;
950 }
951
edisonn@google.com571c70b2013-07-10 17:09:50 +0000952 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
953 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
954 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
955 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
956 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
957 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000958
959 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
960 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
961 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
962
963 pdfContext->fGraphicsState.fCurPosX = x3;
964 pdfContext->fGraphicsState.fCurPosY = y3;
965
966 return kOK_PdfResult;
967}
968
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000969static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000970 if (pdfContext->fGraphicsState.fPathClosed) {
971 pdfContext->fGraphicsState.fPath.reset();
972 pdfContext->fGraphicsState.fPathClosed = false;
973 }
974
edisonn@google.com571c70b2013-07-10 17:09:50 +0000975 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
976 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
977 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
978 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000979 double y1 = pdfContext->fGraphicsState.fCurPosY;
980 double x1 = pdfContext->fGraphicsState.fCurPosX;
981
982 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
983 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
984 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
985
986 pdfContext->fGraphicsState.fCurPosX = x3;
987 pdfContext->fGraphicsState.fCurPosY = y3;
988
989 return kOK_PdfResult;
990}
991
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000992static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000993 if (pdfContext->fGraphicsState.fPathClosed) {
994 pdfContext->fGraphicsState.fPath.reset();
995 pdfContext->fGraphicsState.fPathClosed = false;
996 }
997
edisonn@google.com571c70b2013-07-10 17:09:50 +0000998 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
999 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001000 double y2 = pdfContext->fGraphicsState.fCurPosY;
1001 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001002 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1003 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001004
1005 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1006 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1007 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1008
1009 pdfContext->fGraphicsState.fCurPosX = x3;
1010 pdfContext->fGraphicsState.fCurPosY = y3;
1011
1012 return kOK_PdfResult;
1013}
1014
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001015static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001016 if (pdfContext->fGraphicsState.fPathClosed) {
1017 pdfContext->fGraphicsState.fPath.reset();
1018 pdfContext->fGraphicsState.fPathClosed = false;
1019 }
1020
edisonn@google.com571c70b2013-07-10 17:09:50 +00001021 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1022 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1023 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1024 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001025
1026 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1027 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1028
1029 pdfContext->fGraphicsState.fCurPosX = x;
1030 pdfContext->fGraphicsState.fCurPosY = y + height;
1031
1032 return kOK_PdfResult;
1033}
1034
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001035static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001036 pdfContext->fGraphicsState.fPath.close();
1037 return kOK_PdfResult;
1038}
1039
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001040static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001041 SkPath path = pdfContext->fGraphicsState.fPath;
1042
1043 if (close) {
1044 path.close();
1045 }
1046
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001047 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001048
1049 SkPaint paint;
1050
1051 SkPoint line[2];
1052 if (fill && !stroke && path.isLine(line)) {
1053 paint.setStyle(SkPaint::kStroke_Style);
1054
1055 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1056 paint.setStrokeWidth(SkDoubleToScalar(0));
1057
1058 canvas->drawPath(path, paint);
1059 } else {
1060 if (fill) {
1061 paint.setStyle(SkPaint::kFill_Style);
1062 if (evenOdd) {
1063 path.setFillType(SkPath::kEvenOdd_FillType);
1064 }
1065
1066 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1067
1068 canvas->drawPath(path, paint);
1069 }
1070
1071 if (stroke) {
1072 paint.setStyle(SkPaint::kStroke_Style);
1073
1074 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1075
1076 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1077 canvas->drawPath(path, paint);
1078 }
1079 }
1080
1081 pdfContext->fGraphicsState.fPath.reset();
1082 // todo zoom ... other stuff ?
1083
1084 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1085#ifndef PDF_DEBUG_NO_CLIPING
1086 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1087#endif
1088 }
1089
1090 //pdfContext->fGraphicsState.fClipPath.reset();
1091 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1092
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001093 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001094
1095}
1096
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001097static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001098 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1099}
1100
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001101static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001102 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1103}
1104
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001105static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001106 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1107}
1108
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001109static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001110 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1111}
1112
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001113static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001114 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1115}
1116
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001117static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001118 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1119}
1120
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001121static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001122 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1123}
1124
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001125static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001126 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1127}
1128
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001129static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001130 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1131}
1132
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001133static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001134 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001135 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1136#ifndef PDF_DEBUG_NO_CLIPING
1137 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1138#endif
1139 }
1140
1141 //pdfContext->fGraphicsState.fClipPath.reset();
1142 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1143
1144 pdfContext->fGraphicsState.fPathClosed = true;
1145
1146 return kOK_PdfResult;
1147}
1148
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001149static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001150 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001151 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
1152 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001153
1154 return kPartial_PdfResult;
1155}
1156
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001157static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001158 if (!pdfContext->fGraphicsState.fTextBlock) {
1159 return kIgnoreError_PdfResult;
1160 }
1161 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001162 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001163}
1164
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001165PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001166#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001167 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001168#endif
1169
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001170 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1171 // TODO(edisonn): try to recover and draw it any way?
1172 return kIgnoreError_PdfResult;
1173 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001174
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001175 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1176 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1177 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1178 // TODO(edisonn): try to recover and draw it any way?
1179 return kIgnoreError_PdfResult;
1180 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001181
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001182 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1183
1184 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1185
1186 if (skfont) {
1187 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001188 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001189 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001190 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001191}
1192
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001193//font size Tf Set the text font, Tf
1194//, to font and the text font size, Tfs, to size. font is the name of a
1195//font resource in the Fontsubdictionary of the current resource dictionary; size is
1196//a number representing a scale factor. There is no initial value for either font or
1197//size; they must be specified explicitly using Tf before any text is shown.
1198static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1199 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1200 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1201 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1202}
1203
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001204static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001205 if (!pdfContext->fGraphicsState.fTextBlock) {
1206 // TODO(edisonn): try to recover and draw it any way?
1207 return kIgnoreError_PdfResult;
1208 }
1209
1210 PdfResult ret = DrawText(pdfContext,
1211 pdfContext->fObjectStack.top(),
1212 canvas);
1213 pdfContext->fObjectStack.pop();
1214
1215 return ret;
1216}
1217
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001218static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001219 if (!pdfContext->fGraphicsState.fTextBlock) {
1220 // TODO(edisonn): try to recover and draw it any way?
1221 return kIgnoreError_PdfResult;
1222 }
1223
1224 PdfOp_T_star(pdfContext, canvas, looper);
1225 // Do not pop, and push, just transfer the param to Tj
1226 return PdfOp_Tj(pdfContext, canvas, looper);
1227}
1228
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001229static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001230 if (!pdfContext->fGraphicsState.fTextBlock) {
1231 // TODO(edisonn): try to recover and draw it any way?
1232 return kIgnoreError_PdfResult;
1233 }
1234
1235 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1236 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1237 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1238
1239 pdfContext->fObjectStack.push(aw);
1240 PdfOp_Tw(pdfContext, canvas, looper);
1241
1242 pdfContext->fObjectStack.push(ac);
1243 PdfOp_Tc(pdfContext, canvas, looper);
1244
1245 pdfContext->fObjectStack.push(str);
1246 PdfOp_quote(pdfContext, canvas, looper);
1247
1248 return kPartial_PdfResult;
1249}
1250
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001251static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001252 if (!pdfContext->fGraphicsState.fTextBlock) {
1253 // TODO(edisonn): try to recover and draw it any way?
1254 return kIgnoreError_PdfResult;
1255 }
1256
edisonn@google.com571c70b2013-07-10 17:09:50 +00001257 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001258 pdfContext->fObjectStack.pop();
1259
edisonn@google.com571c70b2013-07-10 17:09:50 +00001260 if (!array->isArray()) {
1261 return kIgnoreError_PdfResult;
1262 }
1263
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001264 for( int i=0; i<static_cast<int>(array->size()); i++ )
1265 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001266 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001267 SkPdfObject* obj = (*array)[i];
1268 DrawText(pdfContext,
1269 obj,
1270 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001271 } else if ((*array)[i]->isNumber()) {
1272 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001273 SkMatrix matrix;
1274 matrix.setAll(SkDoubleToScalar(1),
1275 SkDoubleToScalar(0),
1276 // TODO(edisonn): use writing mode, vertical/horizontal.
1277 SkDoubleToScalar(-dx), // amount is substracted!!!
1278 SkDoubleToScalar(0),
1279 SkDoubleToScalar(1),
1280 SkDoubleToScalar(0),
1281 SkDoubleToScalar(0),
1282 SkDoubleToScalar(0),
1283 SkDoubleToScalar(1));
1284
1285 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1286 }
1287 }
1288 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1289}
1290
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001291static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001292 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001293 return kOK_PdfResult;
1294}
1295
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001296static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001297 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1298}
1299
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001300static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001301 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1302}
1303
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001304static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001305 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001306// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001307
1308 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1309
1310 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001311 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001312 doubles = false;
1313 }
1314
1315#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001316 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001317#endif
1318
1319 for (int i = n - 1; i >= 0 ; i--) {
1320 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001321 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1322// } else {
1323// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001324 }
1325 }
1326
1327 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001328 // TODO(edisonn): do possible field values to enum at parsing time!
1329 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001330 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001331 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001332 }
1333 return kPartial_PdfResult;
1334}
1335
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001336static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001337 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1338}
1339
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001340static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001341 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1342}
1343
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001344static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001345 //SkPdfString* name;
1346 if (pdfContext->fObjectStack.top()->isName()) {
1347 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001348 pdfContext->fObjectStack.pop();
1349 }
1350
1351 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1352 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1353
1354 return kPartial_PdfResult;
1355}
1356
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001357static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001358 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1359}
1360
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001361static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001362 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1363}
1364
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001365static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001366 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001367 return kNYI_PdfResult;
1368}
1369
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001370static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001371 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1372}
1373
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001374static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001375 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1376}
1377
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001378static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001379 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1380 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1381 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001382
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001383 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001384 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001385 return kOK_PdfResult;
1386}
1387
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001388static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001389 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1390}
1391
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001392static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001393 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1394}
1395
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001396static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001397 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001398 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1399 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1400 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1401 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001402
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001403 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001404 // TODO(edisonn): Set color.
1405 return kNYI_PdfResult;
1406}
1407
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001408static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001409 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1410}
1411
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001412static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001413 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1414}
1415
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001416static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001417 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1418 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1419
1420 return kOK_PdfResult;
1421}
1422
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001423static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001424 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1425
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001426 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1427 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1428
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001429 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001430}
1431
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001432static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001433 *looper = new PdfCompatibilitySectionLooper();
1434 return kOK_PdfResult;
1435}
1436
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001437static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438#ifdef ASSERT_BAD_PDF_OPS
1439 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1440 // have the assert when testing good pdfs.
1441#endif
1442 return kIgnoreError_PdfResult;
1443}
1444
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001445static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001446 *looper = new PdfInlineImageLooper();
1447 return kOK_PdfResult;
1448}
1449
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001450static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001451#ifdef ASSERT_BAD_PDF_OPS
1452 SkASSERT(false); // must be processed in inline image looper, but let's
1453 // have the assert when testing good pdfs.
1454#endif
1455 return kIgnoreError_PdfResult;
1456}
1457
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001458static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001459#ifdef ASSERT_BAD_PDF_OPS
1460 SkASSERT(false); // must be processed in inline image looper, but let's
1461 // have the assert when testing good pdfs.
1462#endif
1463 return kIgnoreError_PdfResult;
1464}
1465
1466//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001467static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001468 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001469 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1470
1471 return kOK_PdfResult;
1472}
1473
1474//lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001475static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001476 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001477 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001478
1479 return kNYI_PdfResult;
1480}
1481
1482//lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001483static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001484 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001485 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001486
1487 return kNYI_PdfResult;
1488}
1489
1490//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001491static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001492 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001493 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001494
1495 return kNYI_PdfResult;
1496}
1497
1498//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1499//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001500static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001501 pdfContext->fObjectStack.pop();
1502 pdfContext->fObjectStack.pop();
1503
1504 return kNYI_PdfResult;
1505}
1506
1507//intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001508static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001509 pdfContext->fObjectStack.pop();
1510
1511 return kNYI_PdfResult;
1512}
1513
1514//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1515//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1516//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001517static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001518 pdfContext->fObjectStack.pop();
1519
1520 return kNYI_PdfResult;
1521}
1522
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001523
1524// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1525void skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1526 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1527}
1528
1529void skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1530 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1531}
1532
1533void skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
1534 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1535}
1536
1537void skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1538 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1539}
1540
1541void skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1542 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1543}
1544
1545void skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1546 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1547}
1548
1549void skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1550 // TODO(edisonn): verify input
1551 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1552 // TODO(edisonn): report error/warning
1553 return;
1554 }
1555
1556 SkPdfArray* intervals = (SkPdfArray*)dash->objAtAIndex(0);
1557 int cnt = intervals->size();
1558 if (cnt >= 256) {
1559 // TODO(edisonn): report error/warning, unsuported;
1560 // TODO(edisonn): alloc memory
1561 return;
1562 }
1563 for (int i = 0; i < cnt; i++) {
1564 if (!intervals->objAtAIndex(i)->isNumber()) {
1565 // TODO(edisonn): report error/warning
1566 return;
1567 }
1568 }
1569
1570 pdfContext->fGraphicsState.fDashPhase = dash->objAtAIndex(1)->scalarValue();
1571 pdfContext->fGraphicsState.fDashArrayLength = cnt;
1572 for (int i = 0 ; i < cnt; i++) {
1573 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1574 }
1575}
1576
1577void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1578 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1579 // TODO(edisonn): report error/warning
1580 return;
1581 }
1582 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1583}
1584
1585void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
1586 // TODO(edisonn): verify input
1587}
1588
1589void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
1590 // TODO(edisonn): verify input
1591}
1592
1593void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
1594 // TODO(edisonn): verify input
1595}
1596
1597void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
1598 // TODO(edisonn): verify input
1599}
1600
1601void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
1602 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
1603}
1604
1605
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001606//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1607//the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001608static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001609 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001610
1611#ifdef PDF_TRACE
1612 std::string str;
1613#endif
1614
1615 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001616 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001617
1618 if (extGStateDictionary == NULL) {
1619#ifdef PDF_TRACE
1620 printf("ExtGState is NULL!\n");
1621#endif
1622 return kIgnoreError_PdfResult;
1623 }
1624
edisonn@google.com571c70b2013-07-10 17:09:50 +00001625 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001626
edisonn@google.com571c70b2013-07-10 17:09:50 +00001627 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1628 return kIgnoreError_PdfResult;
1629 }
1630 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001631
1632 // TODO(edisonn): now load all those properties in graphic state.
1633 if (gs == NULL) {
1634 return kIgnoreError_PdfResult;
1635 }
1636
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001637 if (gs->has_LW()) {
1638 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
1639 }
1640
1641 if (gs->has_LC()) {
1642 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
1643 }
1644
1645 if (gs->has_LJ()) {
1646 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
1647 }
1648
1649 if (gs->has_ML()) {
1650 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
1651 }
1652
1653 if (gs->has_D()) {
1654 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
1655 }
1656
1657 if (gs->has_Font()) {
1658 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
1659 }
1660
1661 if (gs->has_BM()) {
1662 if (gs->isBMAName(pdfContext->fPdfDoc)) {
1663 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
1664 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
1665 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
1666 } else {
1667 // TODO(edisonn): report/warn
1668 }
1669 }
1670
1671 if (gs->has_SMask()) {
1672 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
1673 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
1674 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
1675 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
1676 } else {
1677 // TODO(edisonn): report/warn
1678 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001679 }
1680
1681 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001682 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001683 }
1684
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001685 if (gs->has_CA()) {
1686 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
1687 }
1688
1689 if (gs->has_AIS()) {
1690 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001691 }
1692
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001693 return kNYI_PdfResult;
1694}
1695
1696//charSpace Tc Set the character spacing, Tc
1697//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1698//Initial value: 0.
1699PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001700 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001701 pdfContext->fGraphicsState.fCharSpace = charSpace;
1702
1703 return kOK_PdfResult;
1704}
1705
1706//wordSpace Tw Set the word spacing, T
1707//w
1708//, to wordSpace, which is a number expressed in unscaled
1709//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1710//value: 0.
1711PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001712 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001713 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1714
1715 return kOK_PdfResult;
1716}
1717
1718//scale Tz Set the horizontal scaling, Th
1719//, to (scale ˜ 100). scale is a number specifying the
1720//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001721static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001722 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001723
1724 return kNYI_PdfResult;
1725}
1726
1727//render Tr Set the text rendering mode, T
1728//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001729static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001730 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001731
1732 return kNYI_PdfResult;
1733}
1734//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1735//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001736static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001737 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001738
1739 return kNYI_PdfResult;
1740}
1741
1742//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001743static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001744 pdfContext->fObjectStack.pop();
1745 pdfContext->fObjectStack.pop();
1746
1747 return kNYI_PdfResult;
1748}
1749
1750//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001751static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001752 pdfContext->fObjectStack.pop();
1753 pdfContext->fObjectStack.pop();
1754 pdfContext->fObjectStack.pop();
1755 pdfContext->fObjectStack.pop();
1756 pdfContext->fObjectStack.pop();
1757 pdfContext->fObjectStack.pop();
1758
1759 return kNYI_PdfResult;
1760}
1761
1762//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001763static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001764 pdfContext->fObjectStack.pop();
1765
1766 return kNYI_PdfResult;
1767}
1768
1769//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001770static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001771 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001772
edisonn@google.com571c70b2013-07-10 17:09:50 +00001773 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001774
1775 if (xObject == NULL) {
1776#ifdef PDF_TRACE
1777 printf("XObject is NULL!\n");
1778#endif
1779 return kIgnoreError_PdfResult;
1780 }
1781
edisonn@google.com571c70b2013-07-10 17:09:50 +00001782 SkPdfObject* value = xObject->get(name);
1783 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001784
1785#ifdef PDF_TRACE
1786// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001787// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001788#endif
1789
edisonn@google.com571c70b2013-07-10 17:09:50 +00001790 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001791}
1792
1793//tag MP Designate a marked-content point. tag is a name object indicating the role or
1794//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001795static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001796 pdfContext->fObjectStack.pop();
1797
1798 return kNYI_PdfResult;
1799}
1800
1801//tag properties DP Designate a marked-content point with an associated property list. tag is a
1802//name object indicating the role or significance of the point; properties is
1803//either an inline dictionary containing the property list or a name object
1804//associated with it in the Properties subdictionary of the current resource
1805//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001806static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001807 pdfContext->fObjectStack.pop();
1808 pdfContext->fObjectStack.pop();
1809
1810 return kNYI_PdfResult;
1811}
1812
1813//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1814//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001815static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001816 pdfContext->fObjectStack.pop();
1817
1818 return kNYI_PdfResult;
1819}
1820
1821//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1822//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1823//property list or a name object associated with it in the Properties subdictionary of the current resource dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001824static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001825 pdfContext->fObjectStack.pop();
1826 pdfContext->fObjectStack.pop();
1827
1828 return kNYI_PdfResult;
1829}
1830
1831//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001832static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001833 return kNYI_PdfResult;
1834}
1835
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001836static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001837 static bool gInitialized = false;
1838 if (gInitialized) {
1839 return;
1840 }
1841
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001842 gPdfOps.set("q", PdfOp_q);
1843 gPdfOps.set("Q", PdfOp_Q);
1844 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001845
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001846 gPdfOps.set("TD", PdfOp_TD);
1847 gPdfOps.set("Td", PdfOp_Td);
1848 gPdfOps.set("Tm", PdfOp_Tm);
1849 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001850
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001851 gPdfOps.set("m", PdfOp_m);
1852 gPdfOps.set("l", PdfOp_l);
1853 gPdfOps.set("c", PdfOp_c);
1854 gPdfOps.set("v", PdfOp_v);
1855 gPdfOps.set("y", PdfOp_y);
1856 gPdfOps.set("h", PdfOp_h);
1857 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001858
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001859 gPdfOps.set("S", PdfOp_S);
1860 gPdfOps.set("s", PdfOp_s);
1861 gPdfOps.set("f", PdfOp_f);
1862 gPdfOps.set("F", PdfOp_F);
1863 gPdfOps.set("f*", PdfOp_f_star);
1864 gPdfOps.set("B", PdfOp_B);
1865 gPdfOps.set("B*", PdfOp_B_star);
1866 gPdfOps.set("b", PdfOp_b);
1867 gPdfOps.set("b*", PdfOp_b_star);
1868 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001869
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001870 gPdfOps.set("BT", PdfOp_BT);
1871 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001872
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001873 gPdfOps.set("Tj", PdfOp_Tj);
1874 gPdfOps.set("'", PdfOp_quote);
1875 gPdfOps.set("\"", PdfOp_doublequote);
1876 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001877
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001878 gPdfOps.set("CS", PdfOp_CS);
1879 gPdfOps.set("cs", PdfOp_cs);
1880 gPdfOps.set("SC", PdfOp_SC);
1881 gPdfOps.set("SCN", PdfOp_SCN);
1882 gPdfOps.set("sc", PdfOp_sc);
1883 gPdfOps.set("scn", PdfOp_scn);
1884 gPdfOps.set("G", PdfOp_G);
1885 gPdfOps.set("g", PdfOp_g);
1886 gPdfOps.set("RG", PdfOp_RG);
1887 gPdfOps.set("rg", PdfOp_rg);
1888 gPdfOps.set("K", PdfOp_K);
1889 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001890
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001891 gPdfOps.set("W", PdfOp_W);
1892 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001893
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001894 gPdfOps.set("BX", PdfOp_BX);
1895 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001896
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001897 gPdfOps.set("BI", PdfOp_BI);
1898 gPdfOps.set("ID", PdfOp_ID);
1899 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001900
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001901 gPdfOps.set("w", PdfOp_w);
1902 gPdfOps.set("J", PdfOp_J);
1903 gPdfOps.set("j", PdfOp_j);
1904 gPdfOps.set("M", PdfOp_M);
1905 gPdfOps.set("d", PdfOp_d);
1906 gPdfOps.set("ri", PdfOp_ri);
1907 gPdfOps.set("i", PdfOp_i);
1908 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001909
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001910 gPdfOps.set("Tc", PdfOp_Tc);
1911 gPdfOps.set("Tw", PdfOp_Tw);
1912 gPdfOps.set("Tz", PdfOp_Tz);
1913 gPdfOps.set("TL", PdfOp_TL);
1914 gPdfOps.set("Tf", PdfOp_Tf);
1915 gPdfOps.set("Tr", PdfOp_Tr);
1916 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001917
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001918 gPdfOps.set("d0", PdfOp_d0);
1919 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001920
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001921 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001922
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001923 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001924
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001925 gPdfOps.set("MP", PdfOp_MP);
1926 gPdfOps.set("DP", PdfOp_DP);
1927 gPdfOps.set("BMC", PdfOp_BMC);
1928 gPdfOps.set("BDC", PdfOp_BDC);
1929 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001930
1931 gInitialized = true;
1932}
1933
1934class InitPdfOps {
1935public:
1936 InitPdfOps() {
1937 initPdfOperatorRenderes();
1938 }
1939};
1940
1941InitPdfOps gInitPdfOps;
1942
1943void reportPdfRenderStats() {
1944 std::map<std::string, int>::iterator iter;
1945
1946 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001947 SkTDict<int>::Iter iter(gRenderStats[i]);
1948 const char* key;
1949 int value = 0;
1950 while ((key = iter.next(&value)) != NULL) {
1951 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001952 }
1953 }
1954}
1955
1956PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001957 char keyword[256];
1958
1959 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001960 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001961 strncpy(keyword, token.fKeyword, token.fKeywordLength);
1962 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001963 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001964 PdfOperatorRenderer pdfOperatorRenderer = NULL;
1965 if (gPdfOps.find(keyword, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001966 // caller, main work is done by pdfOperatorRenderer(...)
1967 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001968 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
1969
1970 int cnt = 0;
1971 gRenderStats[result].find(keyword, &cnt);
1972 gRenderStats[result].set(keyword, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001973
1974 if (childLooper) {
1975 childLooper->setUp(this);
1976 childLooper->loop();
1977 delete childLooper;
1978 }
1979 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001980 int cnt = 0;
1981 gRenderStats[kUnsupported_PdfResult].find(keyword, &cnt);
1982 gRenderStats[kUnsupported_PdfResult].set(keyword, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001983 }
1984 }
1985 else if (token.fType == kObject_TokenType)
1986 {
1987 fPdfContext->fObjectStack.push( token.fObject );
1988 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001989 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001990 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001991 return kIgnoreError_PdfResult;
1992 }
1993 return kOK_PdfResult;
1994}
1995
1996void PdfMainLooper::loop() {
1997 PdfToken token;
1998 while (readToken(fTokenizer, &token)) {
1999 consumeToken(token);
2000 }
2001}
2002
2003PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002004 SkASSERT(false);
2005 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002006}
2007
2008void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002009 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002010}
2011
2012PdfResult PdfInlineImageLooper::done() {
2013
2014 // TODO(edisonn): long to short names
2015 // TODO(edisonn): set properties in a map
2016 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
2017 // the stream.
2018
2019 SkBitmap bitmap;
2020 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
2021
2022 // TODO(edisonn): matrix use.
2023 // Draw dummy red square, to show the prezence of the inline image.
2024 fCanvas->drawBitmap(bitmap,
2025 SkDoubleToScalar(0),
2026 SkDoubleToScalar(0),
2027 NULL);
2028 return kNYI_PdfResult;
2029}
2030
2031PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2032 return fParent->consumeToken(token);
2033}
2034
2035void PdfCompatibilitySectionLooper::loop() {
2036 // TODO(edisonn): save stacks position, or create a new stack?
2037 // TODO(edisonn): what happens if we pop out more variables then when we started?
2038 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2039 // pop-ing too much will not affect outside the section.
2040 PdfToken token;
2041 while (readToken(fTokenizer, &token)) {
2042 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2043 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2044 looper->setUp(this);
2045 looper->loop();
2046 delete looper;
2047 } else {
2048 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2049 fParent->consumeToken(token);
2050 }
2051 }
2052 // TODO(edisonn): restore stack.
2053}
2054
2055// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2056// TODO(edisonn): Add API for Forms viewing and editing
2057// e.g. SkBitmap getPage(int page);
2058// int formsCount();
2059// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2060// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2061// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2062// resources needed, so the preview is fast.
2063// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2064// references automatically.
2065
edisonn@google.com222382b2013-07-10 22:33:10 +00002066PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002067
edisonn@google.com444e25a2013-07-11 15:20:50 +00002068bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002069 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002070 return false;
2071 }
2072
edisonn@google.com222382b2013-07-10 22:33:10 +00002073 if (page < 0 || page >= pages()) {
2074 return false;
2075 }
2076
edisonn@google.com222382b2013-07-10 22:33:10 +00002077 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002078
2079 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page, pdfContext.fTmpPageAllocator);
edisonn@google.com1f080162013-07-23 21:05:49 +00002080 if (!tokenizer) {
2081 // TODO(edisonn): report/warning/debug
2082 return false;
2083 }
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002084
edisonn@google.com222382b2013-07-10 22:33:10 +00002085 pdfContext.fOriginalMatrix = SkMatrix::I();
2086 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2087
2088 gPdfContext = &pdfContext;
2089
2090 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002091 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002092 SkScalar w = dst.width();
2093 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002094
edisonn@google.com444e25a2013-07-11 15:20:50 +00002095 SkScalar wp = fPdfDoc->MediaBox(page).width();
2096 SkScalar hp = fPdfDoc->MediaBox(page).height();
2097
2098 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 +00002099// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2100
2101 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2102 // TODO(edisonn): add flagg for no clipping.
2103 // Use larger image to make sure we do not draw anything outside of page
2104 // could be used in tests.
2105
2106#ifdef PDF_DEBUG_3X
2107 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)};
2108#else
2109 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2110#endif
2111 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2112 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2113
2114 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2115 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2116
2117 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2118 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2119
2120 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2121 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2122
2123
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002124 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
2125 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2126 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002127
edisonn@google.com222382b2013-07-10 22:33:10 +00002128#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002129 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002130#endif
2131
edisonn@google.com444e25a2013-07-11 15:20:50 +00002132 canvas->setMatrix(pdfContext.fOriginalMatrix);
2133
edisonn@google.com222382b2013-07-10 22:33:10 +00002134// erase with red before?
2135// SkPaint paint;
2136// paint.setColor(SK_ColorRED);
2137// canvas->drawRect(rect, paint);
2138
2139 PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
2140 looper.loop();
2141
2142 delete tokenizer;
2143
2144 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002145 return true;
2146}
edisonn@google.com222382b2013-07-10 22:33:10 +00002147
2148bool SkPdfRenderer::load(const SkString inputFileName) {
2149 unload();
2150
2151 // TODO(edisonn): create static function that could return NULL if there are errors
2152 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002153 if (fPdfDoc->pages() == 0) {
2154 delete fPdfDoc;
2155 fPdfDoc = NULL;
2156 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002157
2158 return fPdfDoc != NULL;
2159}
2160
edisonn@google.com147adb12013-07-24 15:56:19 +00002161bool SkPdfRenderer::load(SkStream* stream) {
2162 unload();
2163
2164 // TODO(edisonn): create static function that could return NULL if there are errors
2165 fPdfDoc = new SkNativeParsedPDF(stream);
2166 if (fPdfDoc->pages() == 0) {
2167 delete fPdfDoc;
2168 fPdfDoc = NULL;
2169 }
2170
2171 return fPdfDoc != NULL;
2172}
2173
2174
edisonn@google.com222382b2013-07-10 22:33:10 +00002175int SkPdfRenderer::pages() const {
2176 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2177}
2178
2179void SkPdfRenderer::unload() {
2180 delete fPdfDoc;
2181 fPdfDoc = NULL;
2182}
2183
2184SkRect SkPdfRenderer::MediaBox(int page) const {
2185 SkASSERT(fPdfDoc);
2186 return fPdfDoc->MediaBox(page);
2187}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002188
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002189size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002190 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2191}
edisonn@google.com147adb12013-07-24 15:56:19 +00002192
2193bool SkPDFNativeRenderToBitmap(SkStream* stream,
2194 SkBitmap* output,
2195 int page,
2196 SkPdfContent content,
2197 double dpi) {
2198 SkASSERT(page >= 0);
2199 SkPdfRenderer renderer;
2200 renderer.load(stream);
2201 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2202 return false;
2203 }
2204
2205 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2206
2207 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2208 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2209
2210 rect = SkRect::MakeWH(width, height);
2211
2212 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2213
2214 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2215 SkCanvas canvas(device);
2216
2217 return renderer.renderPage(page, &canvas, rect);
2218}