blob: e2805c74f82123ac6b7eecf25c68187e91de8bd7 [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.come878e722013-07-29 19:10:58 +0000671 // This is a group?
672 if (skobj->has_Group()) {
673 //TransparencyGroupDictionary* ...
674 }
675
edisonn@google.com571c70b2013-07-10 17:09:50 +0000676 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000677
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000678 SkPdfNativeTokenizer* tokenizer =
679 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000680 if (tokenizer != NULL) {
681 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
682 looper.loop();
683 delete tokenizer;
684 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000685
686 // TODO(edisonn): should we restore the variable stack at the same state?
687 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
688 canvas->restore();
689 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000690 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000691}
692
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000693//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
694// return kNYI_PdfResult;
695//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000696
edisonn@google.com571c70b2013-07-10 17:09:50 +0000697PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
698 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000699 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000700 }
701
702 PdfOp_q(pdfContext, canvas, NULL);
703 canvas->save();
704
705 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
706 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
707
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000708 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
709 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000710
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000711 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000712
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000713 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000714
715 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000716 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000717
718 SkTraceRect(rm, "bbox mapped");
719
720 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
721
722 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
723 // For this PdfContentsTokenizer needs to be extended.
724
edisonn@google.com571c70b2013-07-10 17:09:50 +0000725 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000726
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000727 SkPdfNativeTokenizer* tokenizer =
728 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000729 if (tokenizer != NULL) {
730 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
731 looper.loop();
732 delete tokenizer;
733 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000734
735 // TODO(edisonn): should we restore the variable stack at the same state?
736 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
737 canvas->restore();
738 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000739
740 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000741}
742
743
edisonn@google.com571c70b2013-07-10 17:09:50 +0000744// TODO(edisonn): make sure the pointer is unique
745std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000746
747class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000748 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000749public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000750 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000751 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000752 }
753
754 ~CheckRecursiveRendering() {
755 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000756 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000757 }
758
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000759 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000760 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000761 }
762};
763
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000764static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000765 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000766 // Oops, corrupt PDF!
767 return kIgnoreError_PdfResult;
768 }
769
edisonn@google.com571c70b2013-07-10 17:09:50 +0000770 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000771
edisonn@google.com571c70b2013-07-10 17:09:50 +0000772 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000773 {
774 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000775 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000776 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000777 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000778 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
779 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000780 default:
781 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000782 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000783}
784
785PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
786 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
787 canvas->save();
788 return kOK_PdfResult;
789}
790
791PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
792 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
793 pdfContext->fStateStack.pop();
794 canvas->restore();
795 return kOK_PdfResult;
796}
797
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000798static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000799 double array[6];
800 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000801 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000802 pdfContext->fObjectStack.pop();
803 }
804
805 // a b
806 // c d
807 // e f
808
809 // 0 1
810 // 2 3
811 // 4 5
812
813 // sx ky
814 // kx sy
815 // tx ty
816 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
817
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000818 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000819
820#ifdef PDF_TRACE
821 printf("cm ");
822 for (int i = 0 ; i < 6 ; i++) {
823 printf("%f ", array[i]);
824 }
825 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000826 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000827#endif
828
829 return kOK_PdfResult;
830}
831
832//leading TL Set the text leading, Tl
833//, to leading, which is a number expressed in unscaled text
834//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000835static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000836 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000837
838 pdfContext->fGraphicsState.fTextLeading = ty;
839
840 return kOK_PdfResult;
841}
842
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000843static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000844 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
845 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000846
847 double array[6] = {1, 0, 0, 1, tx, ty};
848 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
849
850 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
851 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
852
853 return kPartial_PdfResult;
854}
855
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000856static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000857 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
858 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000859
edisonn@google.com571c70b2013-07-10 17:09:50 +0000860 // TODO(edisonn): Create factory methods or constructors so native is hidden
861 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000862 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000863
864 PdfOp_TL(pdfContext, canvas, looper);
865
edisonn@google.com571c70b2013-07-10 17:09:50 +0000866 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000867 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000868
edisonn@google.com571c70b2013-07-10 17:09:50 +0000869 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000870 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000871
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000872 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
873
874 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
875 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000876
877 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000878}
879
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000880static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000881 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
882 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
883 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
884 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
885 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
886 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000887
888 double array[6];
889 array[0] = a;
890 array[1] = b;
891 array[2] = c;
892 array[3] = d;
893 array[4] = e;
894 array[5] = f;
895
896 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000897 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000898
899 // TODO(edisonn): Text positioning.
900 pdfContext->fGraphicsState.fMatrixTm = matrix;
901 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
902
903 return kPartial_PdfResult;
904}
905
906//— T* Move to the start of the next line. This operator has the same effect as the code
907//0 Tl Td
908//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000909static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000910 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
911 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000912
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000913 pdfContext->fObjectStack.push(zero);
914 pdfContext->fObjectStack.push(tl);
915
916 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
917
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000918 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000919}
920
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000921static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000922 if (pdfContext->fGraphicsState.fPathClosed) {
923 pdfContext->fGraphicsState.fPath.reset();
924 pdfContext->fGraphicsState.fPathClosed = false;
925 }
926
edisonn@google.com571c70b2013-07-10 17:09:50 +0000927 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
928 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000929
930 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
931 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
932
933 return kOK_PdfResult;
934}
935
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000936static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000937 if (pdfContext->fGraphicsState.fPathClosed) {
938 pdfContext->fGraphicsState.fPath.reset();
939 pdfContext->fGraphicsState.fPathClosed = false;
940 }
941
edisonn@google.com571c70b2013-07-10 17:09:50 +0000942 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
943 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000944
945 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
946 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
947
948 return kOK_PdfResult;
949}
950
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000951static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000952 if (pdfContext->fGraphicsState.fPathClosed) {
953 pdfContext->fGraphicsState.fPath.reset();
954 pdfContext->fGraphicsState.fPathClosed = false;
955 }
956
edisonn@google.com571c70b2013-07-10 17:09:50 +0000957 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
958 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
959 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
960 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
961 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
962 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000963
964 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
965 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
966 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
967
968 pdfContext->fGraphicsState.fCurPosX = x3;
969 pdfContext->fGraphicsState.fCurPosY = y3;
970
971 return kOK_PdfResult;
972}
973
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000974static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000975 if (pdfContext->fGraphicsState.fPathClosed) {
976 pdfContext->fGraphicsState.fPath.reset();
977 pdfContext->fGraphicsState.fPathClosed = false;
978 }
979
edisonn@google.com571c70b2013-07-10 17:09:50 +0000980 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
981 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
982 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
983 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000984 double y1 = pdfContext->fGraphicsState.fCurPosY;
985 double x1 = pdfContext->fGraphicsState.fCurPosX;
986
987 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
988 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
989 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
990
991 pdfContext->fGraphicsState.fCurPosX = x3;
992 pdfContext->fGraphicsState.fCurPosY = y3;
993
994 return kOK_PdfResult;
995}
996
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000997static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000998 if (pdfContext->fGraphicsState.fPathClosed) {
999 pdfContext->fGraphicsState.fPath.reset();
1000 pdfContext->fGraphicsState.fPathClosed = false;
1001 }
1002
edisonn@google.com571c70b2013-07-10 17:09:50 +00001003 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1004 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001005 double y2 = pdfContext->fGraphicsState.fCurPosY;
1006 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001007 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1008 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001009
1010 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1011 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1012 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1013
1014 pdfContext->fGraphicsState.fCurPosX = x3;
1015 pdfContext->fGraphicsState.fCurPosY = y3;
1016
1017 return kOK_PdfResult;
1018}
1019
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001020static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001021 if (pdfContext->fGraphicsState.fPathClosed) {
1022 pdfContext->fGraphicsState.fPath.reset();
1023 pdfContext->fGraphicsState.fPathClosed = false;
1024 }
1025
edisonn@google.com571c70b2013-07-10 17:09:50 +00001026 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1027 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1028 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1029 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001030
1031 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1032 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1033
1034 pdfContext->fGraphicsState.fCurPosX = x;
1035 pdfContext->fGraphicsState.fCurPosY = y + height;
1036
1037 return kOK_PdfResult;
1038}
1039
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001040static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001041 pdfContext->fGraphicsState.fPath.close();
1042 return kOK_PdfResult;
1043}
1044
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001045static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001046 SkPath path = pdfContext->fGraphicsState.fPath;
1047
1048 if (close) {
1049 path.close();
1050 }
1051
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001052 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001053
1054 SkPaint paint;
1055
1056 SkPoint line[2];
1057 if (fill && !stroke && path.isLine(line)) {
1058 paint.setStyle(SkPaint::kStroke_Style);
1059
1060 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1061 paint.setStrokeWidth(SkDoubleToScalar(0));
1062
1063 canvas->drawPath(path, paint);
1064 } else {
1065 if (fill) {
1066 paint.setStyle(SkPaint::kFill_Style);
1067 if (evenOdd) {
1068 path.setFillType(SkPath::kEvenOdd_FillType);
1069 }
1070
1071 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1072
1073 canvas->drawPath(path, paint);
1074 }
1075
1076 if (stroke) {
1077 paint.setStyle(SkPaint::kStroke_Style);
1078
1079 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1080
1081 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1082 canvas->drawPath(path, paint);
1083 }
1084 }
1085
1086 pdfContext->fGraphicsState.fPath.reset();
1087 // todo zoom ... other stuff ?
1088
1089 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1090#ifndef PDF_DEBUG_NO_CLIPING
1091 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1092#endif
1093 }
1094
1095 //pdfContext->fGraphicsState.fClipPath.reset();
1096 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1097
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001098 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001099
1100}
1101
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001102static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001103 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1104}
1105
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001106static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001107 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1108}
1109
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001110static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001111 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1112}
1113
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001114static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001115 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1116}
1117
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001118static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001119 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1120}
1121
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001122static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001123 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1124}
1125
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001126static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001127 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1128}
1129
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001130static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001131 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1132}
1133
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001134static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001135 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1136}
1137
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001138static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001139 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001140 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1141#ifndef PDF_DEBUG_NO_CLIPING
1142 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1143#endif
1144 }
1145
1146 //pdfContext->fGraphicsState.fClipPath.reset();
1147 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1148
1149 pdfContext->fGraphicsState.fPathClosed = true;
1150
1151 return kOK_PdfResult;
1152}
1153
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001154static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001155 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001156 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
1157 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001158
1159 return kPartial_PdfResult;
1160}
1161
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001162static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001163 if (!pdfContext->fGraphicsState.fTextBlock) {
1164 return kIgnoreError_PdfResult;
1165 }
1166 // 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 +00001167 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001168}
1169
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001170PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001171#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001172 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001173#endif
1174
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001175 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1176 // TODO(edisonn): try to recover and draw it any way?
1177 return kIgnoreError_PdfResult;
1178 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001179
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001180 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1181 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1182 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1183 // TODO(edisonn): try to recover and draw it any way?
1184 return kIgnoreError_PdfResult;
1185 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001186
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001187 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1188
1189 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1190
1191 if (skfont) {
1192 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001193 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001194 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001195 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001196}
1197
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001198//font size Tf Set the text font, Tf
1199//, to font and the text font size, Tfs, to size. font is the name of a
1200//font resource in the Fontsubdictionary of the current resource dictionary; size is
1201//a number representing a scale factor. There is no initial value for either font or
1202//size; they must be specified explicitly using Tf before any text is shown.
1203static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1204 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1205 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1206 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1207}
1208
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001209static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001210 if (!pdfContext->fGraphicsState.fTextBlock) {
1211 // TODO(edisonn): try to recover and draw it any way?
1212 return kIgnoreError_PdfResult;
1213 }
1214
1215 PdfResult ret = DrawText(pdfContext,
1216 pdfContext->fObjectStack.top(),
1217 canvas);
1218 pdfContext->fObjectStack.pop();
1219
1220 return ret;
1221}
1222
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001223static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001224 if (!pdfContext->fGraphicsState.fTextBlock) {
1225 // TODO(edisonn): try to recover and draw it any way?
1226 return kIgnoreError_PdfResult;
1227 }
1228
1229 PdfOp_T_star(pdfContext, canvas, looper);
1230 // Do not pop, and push, just transfer the param to Tj
1231 return PdfOp_Tj(pdfContext, canvas, looper);
1232}
1233
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001234static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001235 if (!pdfContext->fGraphicsState.fTextBlock) {
1236 // TODO(edisonn): try to recover and draw it any way?
1237 return kIgnoreError_PdfResult;
1238 }
1239
1240 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1241 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1242 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1243
1244 pdfContext->fObjectStack.push(aw);
1245 PdfOp_Tw(pdfContext, canvas, looper);
1246
1247 pdfContext->fObjectStack.push(ac);
1248 PdfOp_Tc(pdfContext, canvas, looper);
1249
1250 pdfContext->fObjectStack.push(str);
1251 PdfOp_quote(pdfContext, canvas, looper);
1252
1253 return kPartial_PdfResult;
1254}
1255
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001256static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001257 if (!pdfContext->fGraphicsState.fTextBlock) {
1258 // TODO(edisonn): try to recover and draw it any way?
1259 return kIgnoreError_PdfResult;
1260 }
1261
edisonn@google.com571c70b2013-07-10 17:09:50 +00001262 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001263 pdfContext->fObjectStack.pop();
1264
edisonn@google.com571c70b2013-07-10 17:09:50 +00001265 if (!array->isArray()) {
1266 return kIgnoreError_PdfResult;
1267 }
1268
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001269 for( int i=0; i<static_cast<int>(array->size()); i++ )
1270 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001271 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001272 SkPdfObject* obj = (*array)[i];
1273 DrawText(pdfContext,
1274 obj,
1275 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001276 } else if ((*array)[i]->isNumber()) {
1277 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001278 SkMatrix matrix;
1279 matrix.setAll(SkDoubleToScalar(1),
1280 SkDoubleToScalar(0),
1281 // TODO(edisonn): use writing mode, vertical/horizontal.
1282 SkDoubleToScalar(-dx), // amount is substracted!!!
1283 SkDoubleToScalar(0),
1284 SkDoubleToScalar(1),
1285 SkDoubleToScalar(0),
1286 SkDoubleToScalar(0),
1287 SkDoubleToScalar(0),
1288 SkDoubleToScalar(1));
1289
1290 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1291 }
1292 }
1293 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1294}
1295
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001296static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001297 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001298 return kOK_PdfResult;
1299}
1300
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001301static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001302 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1303}
1304
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001305static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001306 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1307}
1308
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001309static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001310 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001311// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001312
1313 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1314
1315 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001316 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001317 doubles = false;
1318 }
1319
1320#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001321 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001322#endif
1323
1324 for (int i = n - 1; i >= 0 ; i--) {
1325 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001326 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1327// } else {
1328// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001329 }
1330 }
1331
1332 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001333 // TODO(edisonn): do possible field values to enum at parsing time!
1334 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001335 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001336 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001337 }
1338 return kPartial_PdfResult;
1339}
1340
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001341static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001342 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1343}
1344
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001345static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001346 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1347}
1348
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001349static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001350 //SkPdfString* name;
1351 if (pdfContext->fObjectStack.top()->isName()) {
1352 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001353 pdfContext->fObjectStack.pop();
1354 }
1355
1356 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1357 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1358
1359 return kPartial_PdfResult;
1360}
1361
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001362static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001363 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1364}
1365
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001366static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001367 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1368}
1369
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001370static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001371 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001372 return kNYI_PdfResult;
1373}
1374
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001375static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001376 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1377}
1378
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001379static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001380 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1381}
1382
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001383static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001384 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1385 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1386 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001387
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001388 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001389 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001390 return kOK_PdfResult;
1391}
1392
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001393static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001394 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1395}
1396
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001397static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001398 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1399}
1400
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001401static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001402 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001403 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1404 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1405 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1406 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001407
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001408 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001409 // TODO(edisonn): Set color.
1410 return kNYI_PdfResult;
1411}
1412
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001413static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001414 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1415}
1416
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001417static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001418 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1419}
1420
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001421static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001422 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1423 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1424
1425 return kOK_PdfResult;
1426}
1427
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001428static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001429 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1430
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001431 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1432 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1433
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001434 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001435}
1436
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001437static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438 *looper = new PdfCompatibilitySectionLooper();
1439 return kOK_PdfResult;
1440}
1441
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001442static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001443#ifdef ASSERT_BAD_PDF_OPS
1444 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1445 // have the assert when testing good pdfs.
1446#endif
1447 return kIgnoreError_PdfResult;
1448}
1449
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001450static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001451 *looper = new PdfInlineImageLooper();
1452 return kOK_PdfResult;
1453}
1454
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001455static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001456#ifdef ASSERT_BAD_PDF_OPS
1457 SkASSERT(false); // must be processed in inline image looper, but let's
1458 // have the assert when testing good pdfs.
1459#endif
1460 return kIgnoreError_PdfResult;
1461}
1462
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001463static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001464#ifdef ASSERT_BAD_PDF_OPS
1465 SkASSERT(false); // must be processed in inline image looper, but let's
1466 // have the assert when testing good pdfs.
1467#endif
1468 return kIgnoreError_PdfResult;
1469}
1470
1471//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001472static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001473 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001474 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1475
1476 return kOK_PdfResult;
1477}
1478
1479//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 +00001480static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001481 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001482 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001483
1484 return kNYI_PdfResult;
1485}
1486
1487//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 +00001488static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001489 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001490 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001491
1492 return kNYI_PdfResult;
1493}
1494
1495//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001496static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001497 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001498 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001499
1500 return kNYI_PdfResult;
1501}
1502
1503//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1504//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001505static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001506 pdfContext->fObjectStack.pop();
1507 pdfContext->fObjectStack.pop();
1508
1509 return kNYI_PdfResult;
1510}
1511
1512//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 +00001513static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001514 pdfContext->fObjectStack.pop();
1515
1516 return kNYI_PdfResult;
1517}
1518
1519//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1520//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1521//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001522static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001523 pdfContext->fObjectStack.pop();
1524
1525 return kNYI_PdfResult;
1526}
1527
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001528
1529// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1530void skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1531 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1532}
1533
1534void skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1535 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1536}
1537
1538void skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
1539 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1540}
1541
1542void skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1543 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1544}
1545
1546void skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1547 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1548}
1549
1550void skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1551 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1552}
1553
1554void skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1555 // TODO(edisonn): verify input
1556 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1557 // TODO(edisonn): report error/warning
1558 return;
1559 }
1560
1561 SkPdfArray* intervals = (SkPdfArray*)dash->objAtAIndex(0);
1562 int cnt = intervals->size();
1563 if (cnt >= 256) {
1564 // TODO(edisonn): report error/warning, unsuported;
1565 // TODO(edisonn): alloc memory
1566 return;
1567 }
1568 for (int i = 0; i < cnt; i++) {
1569 if (!intervals->objAtAIndex(i)->isNumber()) {
1570 // TODO(edisonn): report error/warning
1571 return;
1572 }
1573 }
1574
1575 pdfContext->fGraphicsState.fDashPhase = dash->objAtAIndex(1)->scalarValue();
1576 pdfContext->fGraphicsState.fDashArrayLength = cnt;
1577 for (int i = 0 ; i < cnt; i++) {
1578 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1579 }
1580}
1581
1582void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1583 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1584 // TODO(edisonn): report error/warning
1585 return;
1586 }
1587 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1588}
1589
edisonn@google.come878e722013-07-29 19:10:58 +00001590SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1591
1592class InitBlendModes {
1593public:
1594 InitBlendModes() {
1595 // TODO(edisonn): use the python code generator?
1596 // TABLE 7.2 Standard separable blend modes
1597 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1598 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1599 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1600 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1601 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1602 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1603 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1604 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1605 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1606 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1607 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1608 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1609
1610 // TABLE 7.3 Standard nonseparable blend modes
1611 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1612 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1613 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1614 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1615 }
1616};
1617
1618InitBlendModes _gDummyInniter;
1619
1620SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1621 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1622 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1623 return mode;
1624 }
1625
1626 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1627}
1628
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001629void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001630 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1631 if (mode <= SkXfermode::kLastMode) {
1632 pdfContext->fGraphicsState.fBlendModesLength = 1;
1633 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1634 } else {
1635 // TODO(edisonn): report unknown blend mode
1636 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001637}
1638
1639void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001640 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
1641 // TODO(edisonn): report error/warning
1642 return;
1643 }
1644 SkXfermode::Mode modes[256];
1645 int cnt = blendModes->size();
1646 for (int i = 0; i < cnt; i++) {
1647 SkPdfObject* name = blendModes->objAtAIndex(i);
1648 if (!name->isName()) {
1649 // TODO(edisonn): report error/warning
1650 return;
1651 }
1652 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
1653 if (mode > SkXfermode::kLastMode) {
1654 // TODO(edisonn): report error/warning
1655 return;
1656 }
1657 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001658
edisonn@google.come878e722013-07-29 19:10:58 +00001659 pdfContext->fGraphicsState.fBlendModesLength = cnt;
1660 for (int i = 0; i < cnt; i++) {
1661 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
1662 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001663}
1664
1665void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
1666 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00001667 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
1668 //SkPdfSoftMaskDictionary* smd = (SkPdfSoftMaskDictionary*)sMask;
1669 // TODO(edisonn): load soft mask
1670 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
1671 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
1672 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
1673 } else {
1674 // TODO (edisonn): report error/warning
1675 }
1676}
1677
1678void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
1679 //Next, get the ExtGState Dictionary from the Resource Dictionary:
1680 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
1681
1682 if (extGStateDictionary == NULL) {
1683#ifdef PDF_TRACE
1684 printf("ExtGState is NULL!\n");
1685#endif
1686 // TODO (edisonn): report error/warning
1687 return;
1688 }
1689
1690 SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
1691 if (!obj || !obj->isDictionary()) {
1692 // TODO (edisonn): report error/warning
1693 return;
1694 }
1695 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001696}
1697
1698void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
1699 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
1700}
1701
1702
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001703//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1704//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 +00001705static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001706 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001707
1708#ifdef PDF_TRACE
1709 std::string str;
1710#endif
1711
1712 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001713 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001714
1715 if (extGStateDictionary == NULL) {
1716#ifdef PDF_TRACE
1717 printf("ExtGState is NULL!\n");
1718#endif
1719 return kIgnoreError_PdfResult;
1720 }
1721
edisonn@google.com571c70b2013-07-10 17:09:50 +00001722 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001723
edisonn@google.com571c70b2013-07-10 17:09:50 +00001724 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1725 return kIgnoreError_PdfResult;
1726 }
1727 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001728
1729 // TODO(edisonn): now load all those properties in graphic state.
1730 if (gs == NULL) {
1731 return kIgnoreError_PdfResult;
1732 }
1733
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001734 if (gs->has_LW()) {
1735 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
1736 }
1737
1738 if (gs->has_LC()) {
1739 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
1740 }
1741
1742 if (gs->has_LJ()) {
1743 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
1744 }
1745
1746 if (gs->has_ML()) {
1747 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
1748 }
1749
1750 if (gs->has_D()) {
1751 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
1752 }
1753
1754 if (gs->has_Font()) {
1755 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
1756 }
1757
1758 if (gs->has_BM()) {
1759 if (gs->isBMAName(pdfContext->fPdfDoc)) {
1760 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
1761 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
1762 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
1763 } else {
1764 // TODO(edisonn): report/warn
1765 }
1766 }
1767
1768 if (gs->has_SMask()) {
1769 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
1770 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
1771 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
1772 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
1773 } else {
1774 // TODO(edisonn): report/warn
1775 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001776 }
1777
1778 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001779 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001780 }
1781
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001782 if (gs->has_CA()) {
1783 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
1784 }
1785
1786 if (gs->has_AIS()) {
1787 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001788 }
1789
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001790 return kNYI_PdfResult;
1791}
1792
1793//charSpace Tc Set the character spacing, Tc
1794//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1795//Initial value: 0.
1796PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001797 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001798 pdfContext->fGraphicsState.fCharSpace = charSpace;
1799
1800 return kOK_PdfResult;
1801}
1802
1803//wordSpace Tw Set the word spacing, T
1804//w
1805//, to wordSpace, which is a number expressed in unscaled
1806//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1807//value: 0.
1808PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001809 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001810 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1811
1812 return kOK_PdfResult;
1813}
1814
1815//scale Tz Set the horizontal scaling, Th
1816//, to (scale ˜ 100). scale is a number specifying the
1817//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001818static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001819 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001820
1821 return kNYI_PdfResult;
1822}
1823
1824//render Tr Set the text rendering mode, T
1825//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001826static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001827 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001828
1829 return kNYI_PdfResult;
1830}
1831//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1832//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001833static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001834 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001835
1836 return kNYI_PdfResult;
1837}
1838
1839//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001840static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001841 pdfContext->fObjectStack.pop();
1842 pdfContext->fObjectStack.pop();
1843
1844 return kNYI_PdfResult;
1845}
1846
1847//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001848static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001849 pdfContext->fObjectStack.pop();
1850 pdfContext->fObjectStack.pop();
1851 pdfContext->fObjectStack.pop();
1852 pdfContext->fObjectStack.pop();
1853 pdfContext->fObjectStack.pop();
1854 pdfContext->fObjectStack.pop();
1855
1856 return kNYI_PdfResult;
1857}
1858
1859//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001860static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001861 pdfContext->fObjectStack.pop();
1862
1863 return kNYI_PdfResult;
1864}
1865
1866//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001867static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001868 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001869
edisonn@google.com571c70b2013-07-10 17:09:50 +00001870 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001871
1872 if (xObject == NULL) {
1873#ifdef PDF_TRACE
1874 printf("XObject is NULL!\n");
1875#endif
1876 return kIgnoreError_PdfResult;
1877 }
1878
edisonn@google.com571c70b2013-07-10 17:09:50 +00001879 SkPdfObject* value = xObject->get(name);
1880 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001881
1882#ifdef PDF_TRACE
1883// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001884// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001885#endif
1886
edisonn@google.com571c70b2013-07-10 17:09:50 +00001887 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001888}
1889
1890//tag MP Designate a marked-content point. tag is a name object indicating the role or
1891//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001892static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001893 pdfContext->fObjectStack.pop();
1894
1895 return kNYI_PdfResult;
1896}
1897
1898//tag properties DP Designate a marked-content point with an associated property list. tag is a
1899//name object indicating the role or significance of the point; properties is
1900//either an inline dictionary containing the property list or a name object
1901//associated with it in the Properties subdictionary of the current resource
1902//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001903static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001904 pdfContext->fObjectStack.pop();
1905 pdfContext->fObjectStack.pop();
1906
1907 return kNYI_PdfResult;
1908}
1909
1910//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1911//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001912static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001913 pdfContext->fObjectStack.pop();
1914
1915 return kNYI_PdfResult;
1916}
1917
1918//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1919//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1920//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 +00001921static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001922 pdfContext->fObjectStack.pop();
1923 pdfContext->fObjectStack.pop();
1924
1925 return kNYI_PdfResult;
1926}
1927
1928//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001929static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001930 return kNYI_PdfResult;
1931}
1932
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001933static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001934 static bool gInitialized = false;
1935 if (gInitialized) {
1936 return;
1937 }
1938
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001939 gPdfOps.set("q", PdfOp_q);
1940 gPdfOps.set("Q", PdfOp_Q);
1941 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001942
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001943 gPdfOps.set("TD", PdfOp_TD);
1944 gPdfOps.set("Td", PdfOp_Td);
1945 gPdfOps.set("Tm", PdfOp_Tm);
1946 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001947
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001948 gPdfOps.set("m", PdfOp_m);
1949 gPdfOps.set("l", PdfOp_l);
1950 gPdfOps.set("c", PdfOp_c);
1951 gPdfOps.set("v", PdfOp_v);
1952 gPdfOps.set("y", PdfOp_y);
1953 gPdfOps.set("h", PdfOp_h);
1954 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001955
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001956 gPdfOps.set("S", PdfOp_S);
1957 gPdfOps.set("s", PdfOp_s);
1958 gPdfOps.set("f", PdfOp_f);
1959 gPdfOps.set("F", PdfOp_F);
1960 gPdfOps.set("f*", PdfOp_f_star);
1961 gPdfOps.set("B", PdfOp_B);
1962 gPdfOps.set("B*", PdfOp_B_star);
1963 gPdfOps.set("b", PdfOp_b);
1964 gPdfOps.set("b*", PdfOp_b_star);
1965 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001966
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001967 gPdfOps.set("BT", PdfOp_BT);
1968 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001969
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001970 gPdfOps.set("Tj", PdfOp_Tj);
1971 gPdfOps.set("'", PdfOp_quote);
1972 gPdfOps.set("\"", PdfOp_doublequote);
1973 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001974
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001975 gPdfOps.set("CS", PdfOp_CS);
1976 gPdfOps.set("cs", PdfOp_cs);
1977 gPdfOps.set("SC", PdfOp_SC);
1978 gPdfOps.set("SCN", PdfOp_SCN);
1979 gPdfOps.set("sc", PdfOp_sc);
1980 gPdfOps.set("scn", PdfOp_scn);
1981 gPdfOps.set("G", PdfOp_G);
1982 gPdfOps.set("g", PdfOp_g);
1983 gPdfOps.set("RG", PdfOp_RG);
1984 gPdfOps.set("rg", PdfOp_rg);
1985 gPdfOps.set("K", PdfOp_K);
1986 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001987
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001988 gPdfOps.set("W", PdfOp_W);
1989 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001990
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001991 gPdfOps.set("BX", PdfOp_BX);
1992 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001993
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001994 gPdfOps.set("BI", PdfOp_BI);
1995 gPdfOps.set("ID", PdfOp_ID);
1996 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001997
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001998 gPdfOps.set("w", PdfOp_w);
1999 gPdfOps.set("J", PdfOp_J);
2000 gPdfOps.set("j", PdfOp_j);
2001 gPdfOps.set("M", PdfOp_M);
2002 gPdfOps.set("d", PdfOp_d);
2003 gPdfOps.set("ri", PdfOp_ri);
2004 gPdfOps.set("i", PdfOp_i);
2005 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002006
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002007 gPdfOps.set("Tc", PdfOp_Tc);
2008 gPdfOps.set("Tw", PdfOp_Tw);
2009 gPdfOps.set("Tz", PdfOp_Tz);
2010 gPdfOps.set("TL", PdfOp_TL);
2011 gPdfOps.set("Tf", PdfOp_Tf);
2012 gPdfOps.set("Tr", PdfOp_Tr);
2013 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002014
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002015 gPdfOps.set("d0", PdfOp_d0);
2016 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002017
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002018 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002019
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002020 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002021
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002022 gPdfOps.set("MP", PdfOp_MP);
2023 gPdfOps.set("DP", PdfOp_DP);
2024 gPdfOps.set("BMC", PdfOp_BMC);
2025 gPdfOps.set("BDC", PdfOp_BDC);
2026 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002027
2028 gInitialized = true;
2029}
2030
2031class InitPdfOps {
2032public:
2033 InitPdfOps() {
2034 initPdfOperatorRenderes();
2035 }
2036};
2037
2038InitPdfOps gInitPdfOps;
2039
2040void reportPdfRenderStats() {
2041 std::map<std::string, int>::iterator iter;
2042
2043 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002044 SkTDict<int>::Iter iter(gRenderStats[i]);
2045 const char* key;
2046 int value = 0;
2047 while ((key = iter.next(&value)) != NULL) {
2048 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002049 }
2050 }
2051}
2052
2053PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002054 char keyword[256];
2055
2056 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002057 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002058 strncpy(keyword, token.fKeyword, token.fKeywordLength);
2059 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002060 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002061 PdfOperatorRenderer pdfOperatorRenderer = NULL;
2062 if (gPdfOps.find(keyword, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002063 // caller, main work is done by pdfOperatorRenderer(...)
2064 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002065 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
2066
2067 int cnt = 0;
2068 gRenderStats[result].find(keyword, &cnt);
2069 gRenderStats[result].set(keyword, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002070
2071 if (childLooper) {
2072 childLooper->setUp(this);
2073 childLooper->loop();
2074 delete childLooper;
2075 }
2076 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002077 int cnt = 0;
2078 gRenderStats[kUnsupported_PdfResult].find(keyword, &cnt);
2079 gRenderStats[kUnsupported_PdfResult].set(keyword, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002080 }
2081 }
2082 else if (token.fType == kObject_TokenType)
2083 {
2084 fPdfContext->fObjectStack.push( token.fObject );
2085 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002086 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002087 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002088 return kIgnoreError_PdfResult;
2089 }
2090 return kOK_PdfResult;
2091}
2092
2093void PdfMainLooper::loop() {
2094 PdfToken token;
2095 while (readToken(fTokenizer, &token)) {
2096 consumeToken(token);
2097 }
2098}
2099
2100PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002101 SkASSERT(false);
2102 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002103}
2104
2105void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002106 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002107}
2108
2109PdfResult PdfInlineImageLooper::done() {
2110
2111 // TODO(edisonn): long to short names
2112 // TODO(edisonn): set properties in a map
2113 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
2114 // the stream.
2115
2116 SkBitmap bitmap;
2117 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
2118
2119 // TODO(edisonn): matrix use.
2120 // Draw dummy red square, to show the prezence of the inline image.
2121 fCanvas->drawBitmap(bitmap,
2122 SkDoubleToScalar(0),
2123 SkDoubleToScalar(0),
2124 NULL);
2125 return kNYI_PdfResult;
2126}
2127
2128PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2129 return fParent->consumeToken(token);
2130}
2131
2132void PdfCompatibilitySectionLooper::loop() {
2133 // TODO(edisonn): save stacks position, or create a new stack?
2134 // TODO(edisonn): what happens if we pop out more variables then when we started?
2135 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2136 // pop-ing too much will not affect outside the section.
2137 PdfToken token;
2138 while (readToken(fTokenizer, &token)) {
2139 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2140 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2141 looper->setUp(this);
2142 looper->loop();
2143 delete looper;
2144 } else {
2145 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2146 fParent->consumeToken(token);
2147 }
2148 }
2149 // TODO(edisonn): restore stack.
2150}
2151
2152// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2153// TODO(edisonn): Add API for Forms viewing and editing
2154// e.g. SkBitmap getPage(int page);
2155// int formsCount();
2156// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2157// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2158// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2159// resources needed, so the preview is fast.
2160// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2161// references automatically.
2162
edisonn@google.com222382b2013-07-10 22:33:10 +00002163PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002164
edisonn@google.com444e25a2013-07-11 15:20:50 +00002165bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002166 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002167 return false;
2168 }
2169
edisonn@google.com222382b2013-07-10 22:33:10 +00002170 if (page < 0 || page >= pages()) {
2171 return false;
2172 }
2173
edisonn@google.com222382b2013-07-10 22:33:10 +00002174 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002175
2176 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page, pdfContext.fTmpPageAllocator);
edisonn@google.com1f080162013-07-23 21:05:49 +00002177 if (!tokenizer) {
2178 // TODO(edisonn): report/warning/debug
2179 return false;
2180 }
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002181
edisonn@google.com222382b2013-07-10 22:33:10 +00002182 pdfContext.fOriginalMatrix = SkMatrix::I();
2183 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2184
2185 gPdfContext = &pdfContext;
2186
2187 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002188 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002189 SkScalar w = dst.width();
2190 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002191
edisonn@google.com444e25a2013-07-11 15:20:50 +00002192 SkScalar wp = fPdfDoc->MediaBox(page).width();
2193 SkScalar hp = fPdfDoc->MediaBox(page).height();
2194
2195 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 +00002196// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2197
2198 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2199 // TODO(edisonn): add flagg for no clipping.
2200 // Use larger image to make sure we do not draw anything outside of page
2201 // could be used in tests.
2202
2203#ifdef PDF_DEBUG_3X
2204 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)};
2205#else
2206 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2207#endif
2208 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2209 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2210
2211 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2212 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2213
2214 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2215 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2216
2217 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2218 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2219
2220
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002221 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
2222 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2223 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002224
edisonn@google.com222382b2013-07-10 22:33:10 +00002225#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002226 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002227#endif
2228
edisonn@google.com444e25a2013-07-11 15:20:50 +00002229 canvas->setMatrix(pdfContext.fOriginalMatrix);
2230
edisonn@google.com222382b2013-07-10 22:33:10 +00002231// erase with red before?
2232// SkPaint paint;
2233// paint.setColor(SK_ColorRED);
2234// canvas->drawRect(rect, paint);
2235
2236 PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
2237 looper.loop();
2238
2239 delete tokenizer;
2240
2241 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002242 return true;
2243}
edisonn@google.com222382b2013-07-10 22:33:10 +00002244
2245bool SkPdfRenderer::load(const SkString inputFileName) {
2246 unload();
2247
2248 // TODO(edisonn): create static function that could return NULL if there are errors
2249 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002250 if (fPdfDoc->pages() == 0) {
2251 delete fPdfDoc;
2252 fPdfDoc = NULL;
2253 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002254
2255 return fPdfDoc != NULL;
2256}
2257
edisonn@google.com147adb12013-07-24 15:56:19 +00002258bool SkPdfRenderer::load(SkStream* stream) {
2259 unload();
2260
2261 // TODO(edisonn): create static function that could return NULL if there are errors
2262 fPdfDoc = new SkNativeParsedPDF(stream);
2263 if (fPdfDoc->pages() == 0) {
2264 delete fPdfDoc;
2265 fPdfDoc = NULL;
2266 }
2267
2268 return fPdfDoc != NULL;
2269}
2270
2271
edisonn@google.com222382b2013-07-10 22:33:10 +00002272int SkPdfRenderer::pages() const {
2273 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2274}
2275
2276void SkPdfRenderer::unload() {
2277 delete fPdfDoc;
2278 fPdfDoc = NULL;
2279}
2280
2281SkRect SkPdfRenderer::MediaBox(int page) const {
2282 SkASSERT(fPdfDoc);
2283 return fPdfDoc->MediaBox(page);
2284}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002285
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002286size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002287 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2288}
edisonn@google.com147adb12013-07-24 15:56:19 +00002289
2290bool SkPDFNativeRenderToBitmap(SkStream* stream,
2291 SkBitmap* output,
2292 int page,
2293 SkPdfContent content,
2294 double dpi) {
2295 SkASSERT(page >= 0);
2296 SkPdfRenderer renderer;
2297 renderer.load(stream);
2298 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2299 return false;
2300 }
2301
2302 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2303
2304 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2305 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2306
2307 rect = SkRect::MakeWH(width, height);
2308
2309 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2310
2311 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2312 SkCanvas canvas(device);
2313
2314 return renderer.renderPage(page, &canvas, rect);
2315}