blob: eb3695d78194e0e8ebd7a6f541b9817b88d9bccc [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
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000618 // TODO(edisonn): soft mask type? alpha/luminosity.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000619 if (sMask.empty()) {
620 canvas->drawBitmapRect(image, dst, NULL);
621 } else {
622 canvas->saveLayer(&dst, NULL);
623 canvas->drawBitmapRect(image, dst, NULL);
624 SkPaint xfer;
625 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000626 // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf?
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000627 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
628 canvas->drawBitmapRect(sMask, dst, &xfer);
629 canvas->restore();
630 }
631
632 canvas->restore();
633
634 return kPartial_PdfResult;
635}
636
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000637static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000638 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000639 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000640 }
641
642 PdfOp_q(pdfContext, canvas, NULL);
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000643
644
645
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000646 canvas->save();
647
648
edisonn@google.com571c70b2013-07-10 17:09:50 +0000649 if (skobj->Resources(pdfContext->fPdfDoc)) {
650 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000651 }
652
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000653 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000654
edisonn@google.com571c70b2013-07-10 17:09:50 +0000655 if (skobj->has_Matrix()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000656 pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
657 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
658 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000659 // TODO(edisonn) reset matrixTm and matricTlm also?
660 }
661
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000662 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000663
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000664 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000665
edisonn@google.com571c70b2013-07-10 17:09:50 +0000666 if (skobj->has_BBox()) {
667 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000668 }
669
670 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
671 // For this PdfContentsTokenizer needs to be extended.
672
edisonn@google.come878e722013-07-29 19:10:58 +0000673 // This is a group?
674 if (skobj->has_Group()) {
675 //TransparencyGroupDictionary* ...
676 }
677
edisonn@google.com571c70b2013-07-10 17:09:50 +0000678 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000679
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000680 SkPdfNativeTokenizer* tokenizer =
681 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000682 if (tokenizer != NULL) {
683 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
684 looper.loop();
685 delete tokenizer;
686 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000687
688 // TODO(edisonn): should we restore the variable stack at the same state?
689 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
690 canvas->restore();
691 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000692 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000693}
694
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000695//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
696// return kNYI_PdfResult;
697//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000698
edisonn@google.com571c70b2013-07-10 17:09:50 +0000699PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
700 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000701 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000702 }
703
704 PdfOp_q(pdfContext, canvas, NULL);
705 canvas->save();
706
707 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
708 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
709
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000710 pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
711 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000712
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000713 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000714
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000715 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000716
717 SkRect rm = bBox;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000718 pdfContext->fGraphicsState.fCTM.mapRect(&rm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000719
720 SkTraceRect(rm, "bbox mapped");
721
722 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
723
724 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
725 // For this PdfContentsTokenizer needs to be extended.
726
edisonn@google.com571c70b2013-07-10 17:09:50 +0000727 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000728
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000729 SkPdfNativeTokenizer* tokenizer =
730 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000731 if (tokenizer != NULL) {
732 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
733 looper.loop();
734 delete tokenizer;
735 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000736
737 // TODO(edisonn): should we restore the variable stack at the same state?
738 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
739 canvas->restore();
740 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000741
742 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000743}
744
745
edisonn@google.com571c70b2013-07-10 17:09:50 +0000746// TODO(edisonn): make sure the pointer is unique
747std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000748
749class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000750 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000751public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000752 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000753 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000754 }
755
756 ~CheckRecursiveRendering() {
757 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000758 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000759 }
760
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000761 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000762 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000763 }
764};
765
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000766static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000767 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000768 // Oops, corrupt PDF!
769 return kIgnoreError_PdfResult;
770 }
771
edisonn@google.com571c70b2013-07-10 17:09:50 +0000772 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000773
edisonn@google.com571c70b2013-07-10 17:09:50 +0000774 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000775 {
776 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000777 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000778 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000779 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000780 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
781 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000782 default:
783 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000784 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000785}
786
edisonn@google.com88fc03d2013-07-30 13:34:10 +0000787static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
788 if (!skobj) {
789 return kIgnoreError_PdfResult;
790 }
791
792 if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
793 return kNYI_PdfResult;
794 }
795
796 SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
797
798 if (!stream) {
799 return kIgnoreError_PdfResult;
800 }
801
802 if (CheckRecursiveRendering::IsInRendering(skobj)) {
803 // Oops, corrupt PDF!
804 return kIgnoreError_PdfResult;
805 }
806 CheckRecursiveRendering checkRecursion(skobj);
807
808
809 PdfOp_q(pdfContext, canvas, NULL);
810
811 canvas->save();
812
813 if (skobj->Resources(pdfContext->fPdfDoc)) {
814 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
815 }
816
817 // TODO(edisonn): refactor common path with doXObject()
818 // This is a group?
819 if (skobj->has_Group()) {
820 //TransparencyGroupDictionary* ...
821 }
822
823 SkPdfNativeTokenizer* tokenizer =
824 pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
825 if (tokenizer != NULL) {
826 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
827 looper.loop();
828 delete tokenizer;
829 }
830
831 // TODO(edisonn): should we restore the variable stack at the same state?
832 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
833 canvas->restore();
834 PdfOp_Q(pdfContext, canvas, NULL);
835 return kPartial_PdfResult;
836}
837
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000838PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
839 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
840 canvas->save();
841 return kOK_PdfResult;
842}
843
844PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
845 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
846 pdfContext->fStateStack.pop();
847 canvas->restore();
848 return kOK_PdfResult;
849}
850
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000851static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000852 double array[6];
853 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000854 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000855 pdfContext->fObjectStack.pop();
856 }
857
858 // a b
859 // c d
860 // e f
861
862 // 0 1
863 // 2 3
864 // 4 5
865
866 // sx ky
867 // kx sy
868 // tx ty
869 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
870
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000871 pdfContext->fGraphicsState.fCTM.preConcat(matrix);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000872
873#ifdef PDF_TRACE
874 printf("cm ");
875 for (int i = 0 ; i < 6 ; i++) {
876 printf("%f ", array[i]);
877 }
878 printf("\n");
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000879 SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000880#endif
881
882 return kOK_PdfResult;
883}
884
885//leading TL Set the text leading, Tl
886//, to leading, which is a number expressed in unscaled text
887//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000888static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000889 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000890
891 pdfContext->fGraphicsState.fTextLeading = ty;
892
893 return kOK_PdfResult;
894}
895
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000896static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000897#ifdef PDF_TRACE
898 printf("stack size = %i\n", (int)pdfContext->fObjectStack.size());
899#endif
edisonn@google.com571c70b2013-07-10 17:09:50 +0000900 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000901 SkPdfObject* obj = pdfContext->fObjectStack.top();
902 obj = obj;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000903 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000904
905 double array[6] = {1, 0, 0, 1, tx, ty};
906 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
907
908 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
909 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
910
911 return kPartial_PdfResult;
912}
913
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000914static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000915 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
916 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000917
edisonn@google.com571c70b2013-07-10 17:09:50 +0000918 // TODO(edisonn): Create factory methods or constructors so native is hidden
919 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000920 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000921
922 PdfOp_TL(pdfContext, canvas, looper);
923
edisonn@google.com571c70b2013-07-10 17:09:50 +0000924 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000925 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000926
edisonn@google.com571c70b2013-07-10 17:09:50 +0000927 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000928 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000929
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000930 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
931
932 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
933 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000934
935 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000936}
937
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000938static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000939 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
940 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
941 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
942 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
943 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
944 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000945
946 double array[6];
947 array[0] = a;
948 array[1] = b;
949 array[2] = c;
950 array[3] = d;
951 array[4] = e;
952 array[5] = f;
953
954 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000955 matrix.postConcat(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000956
957 // TODO(edisonn): Text positioning.
958 pdfContext->fGraphicsState.fMatrixTm = matrix;
959 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
960
961 return kPartial_PdfResult;
962}
963
964//— T* Move to the start of the next line. This operator has the same effect as the code
965//0 Tl Td
966//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000967static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000968 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
969 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000970
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000971 pdfContext->fObjectStack.push(zero);
972 pdfContext->fObjectStack.push(tl);
973
974 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
975
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000976 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000977}
978
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000979static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000980 if (pdfContext->fGraphicsState.fPathClosed) {
981 pdfContext->fGraphicsState.fPath.reset();
982 pdfContext->fGraphicsState.fPathClosed = false;
983 }
984
edisonn@google.com571c70b2013-07-10 17:09:50 +0000985 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
986 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000987
988 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
989 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
990
991 return kOK_PdfResult;
992}
993
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000994static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000995 if (pdfContext->fGraphicsState.fPathClosed) {
996 pdfContext->fGraphicsState.fPath.reset();
997 pdfContext->fGraphicsState.fPathClosed = false;
998 }
999
edisonn@google.com571c70b2013-07-10 17:09:50 +00001000 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1001 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001002
1003 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
1004 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
1005
1006 return kOK_PdfResult;
1007}
1008
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001009static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001010 if (pdfContext->fGraphicsState.fPathClosed) {
1011 pdfContext->fGraphicsState.fPath.reset();
1012 pdfContext->fGraphicsState.fPathClosed = false;
1013 }
1014
edisonn@google.com571c70b2013-07-10 17:09:50 +00001015 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1016 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1017 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1018 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1019 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1020 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001021
1022 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1023 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1024 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1025
1026 pdfContext->fGraphicsState.fCurPosX = x3;
1027 pdfContext->fGraphicsState.fCurPosY = y3;
1028
1029 return kOK_PdfResult;
1030}
1031
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001032static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001033 if (pdfContext->fGraphicsState.fPathClosed) {
1034 pdfContext->fGraphicsState.fPath.reset();
1035 pdfContext->fGraphicsState.fPathClosed = false;
1036 }
1037
edisonn@google.com571c70b2013-07-10 17:09:50 +00001038 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1039 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1040 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1041 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001042 double y1 = pdfContext->fGraphicsState.fCurPosY;
1043 double x1 = pdfContext->fGraphicsState.fCurPosX;
1044
1045 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1046 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1047 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1048
1049 pdfContext->fGraphicsState.fCurPosX = x3;
1050 pdfContext->fGraphicsState.fCurPosY = y3;
1051
1052 return kOK_PdfResult;
1053}
1054
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001055static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001056 if (pdfContext->fGraphicsState.fPathClosed) {
1057 pdfContext->fGraphicsState.fPath.reset();
1058 pdfContext->fGraphicsState.fPathClosed = false;
1059 }
1060
edisonn@google.com571c70b2013-07-10 17:09:50 +00001061 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1062 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001063 double y2 = pdfContext->fGraphicsState.fCurPosY;
1064 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001065 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1066 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001067
1068 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
1069 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
1070 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
1071
1072 pdfContext->fGraphicsState.fCurPosX = x3;
1073 pdfContext->fGraphicsState.fCurPosY = y3;
1074
1075 return kOK_PdfResult;
1076}
1077
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001078static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001079 if (pdfContext->fGraphicsState.fPathClosed) {
1080 pdfContext->fGraphicsState.fPath.reset();
1081 pdfContext->fGraphicsState.fPathClosed = false;
1082 }
1083
edisonn@google.com571c70b2013-07-10 17:09:50 +00001084 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1085 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1086 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1087 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001088
1089 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1090 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1091
1092 pdfContext->fGraphicsState.fCurPosX = x;
1093 pdfContext->fGraphicsState.fCurPosY = y + height;
1094
1095 return kOK_PdfResult;
1096}
1097
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001098static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001099 pdfContext->fGraphicsState.fPath.close();
1100 return kOK_PdfResult;
1101}
1102
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001103static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001104 SkPath path = pdfContext->fGraphicsState.fPath;
1105
1106 if (close) {
1107 path.close();
1108 }
1109
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001110 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001111
1112 SkPaint paint;
1113
1114 SkPoint line[2];
1115 if (fill && !stroke && path.isLine(line)) {
1116 paint.setStyle(SkPaint::kStroke_Style);
1117
1118 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1119 paint.setStrokeWidth(SkDoubleToScalar(0));
1120
1121 canvas->drawPath(path, paint);
1122 } else {
1123 if (fill) {
1124 paint.setStyle(SkPaint::kFill_Style);
1125 if (evenOdd) {
1126 path.setFillType(SkPath::kEvenOdd_FillType);
1127 }
1128
1129 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1130
1131 canvas->drawPath(path, paint);
1132 }
1133
1134 if (stroke) {
1135 paint.setStyle(SkPaint::kStroke_Style);
1136
1137 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1138
1139 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1140 canvas->drawPath(path, paint);
1141 }
1142 }
1143
1144 pdfContext->fGraphicsState.fPath.reset();
1145 // todo zoom ... other stuff ?
1146
1147 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1148#ifndef PDF_DEBUG_NO_CLIPING
1149 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1150#endif
1151 }
1152
1153 //pdfContext->fGraphicsState.fClipPath.reset();
1154 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1155
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001156 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001157
1158}
1159
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001160static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001161 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1162}
1163
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001164static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001165 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1166}
1167
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001168static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001169 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1170}
1171
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001172static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001173 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1174}
1175
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001176static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001177 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1178}
1179
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001180static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001181 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1182}
1183
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001184static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001185 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1186}
1187
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001188static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001189 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1190}
1191
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001192static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001193 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1194}
1195
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001196static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001197 canvas->setMatrix(pdfContext->fGraphicsState.fCTM);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001198 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1199#ifndef PDF_DEBUG_NO_CLIPING
1200 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1201#endif
1202 }
1203
1204 //pdfContext->fGraphicsState.fClipPath.reset();
1205 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1206
1207 pdfContext->fGraphicsState.fPathClosed = true;
1208
1209 return kOK_PdfResult;
1210}
1211
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001212static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001213 pdfContext->fGraphicsState.fTextBlock = true;
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001214 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
1215 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001216
1217 return kPartial_PdfResult;
1218}
1219
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001220static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001221 if (!pdfContext->fGraphicsState.fTextBlock) {
1222 return kIgnoreError_PdfResult;
1223 }
1224 // 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 +00001225 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001226}
1227
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001228PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001229#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001230 printf("font name: %s\n", fontName->nameValue2().c_str());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001231#endif
1232
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001233 if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1234 // TODO(edisonn): try to recover and draw it any way?
1235 return kIgnoreError_PdfResult;
1236 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001237
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001238 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1239 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1240 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1241 // TODO(edisonn): try to recover and draw it any way?
1242 return kIgnoreError_PdfResult;
1243 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001244
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001245 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1246
1247 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1248
1249 if (skfont) {
1250 pdfContext->fGraphicsState.fSkFont = skfont;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001251 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001252 pdfContext->fGraphicsState.fCurFontSize = fontSize;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001253 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001254}
1255
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001256//font size Tf Set the text font, Tf
1257//, to font and the text font size, Tfs, to size. font is the name of a
1258//font resource in the Fontsubdictionary of the current resource dictionary; size is
1259//a number representing a scale factor. There is no initial value for either font or
1260//size; they must be specified explicitly using Tf before any text is shown.
1261static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
1262 double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1263 SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1264 return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize);
1265}
1266
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001267static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001268 if (!pdfContext->fGraphicsState.fTextBlock) {
1269 // TODO(edisonn): try to recover and draw it any way?
1270 return kIgnoreError_PdfResult;
1271 }
1272
1273 PdfResult ret = DrawText(pdfContext,
1274 pdfContext->fObjectStack.top(),
1275 canvas);
1276 pdfContext->fObjectStack.pop();
1277
1278 return ret;
1279}
1280
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001281static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001282 if (!pdfContext->fGraphicsState.fTextBlock) {
1283 // TODO(edisonn): try to recover and draw it any way?
1284 return kIgnoreError_PdfResult;
1285 }
1286
1287 PdfOp_T_star(pdfContext, canvas, looper);
1288 // Do not pop, and push, just transfer the param to Tj
1289 return PdfOp_Tj(pdfContext, canvas, looper);
1290}
1291
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001292static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001293 if (!pdfContext->fGraphicsState.fTextBlock) {
1294 // TODO(edisonn): try to recover and draw it any way?
1295 return kIgnoreError_PdfResult;
1296 }
1297
1298 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1299 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1300 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1301
1302 pdfContext->fObjectStack.push(aw);
1303 PdfOp_Tw(pdfContext, canvas, looper);
1304
1305 pdfContext->fObjectStack.push(ac);
1306 PdfOp_Tc(pdfContext, canvas, looper);
1307
1308 pdfContext->fObjectStack.push(str);
1309 PdfOp_quote(pdfContext, canvas, looper);
1310
1311 return kPartial_PdfResult;
1312}
1313
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001314static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001315 if (!pdfContext->fGraphicsState.fTextBlock) {
1316 // TODO(edisonn): try to recover and draw it any way?
1317 return kIgnoreError_PdfResult;
1318 }
1319
edisonn@google.com571c70b2013-07-10 17:09:50 +00001320 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001321 pdfContext->fObjectStack.pop();
1322
edisonn@google.com571c70b2013-07-10 17:09:50 +00001323 if (!array->isArray()) {
1324 return kIgnoreError_PdfResult;
1325 }
1326
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001327 for( int i=0; i<static_cast<int>(array->size()); i++ )
1328 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001329 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001330 SkPdfObject* obj = (*array)[i];
1331 DrawText(pdfContext,
1332 obj,
1333 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001334 } else if ((*array)[i]->isNumber()) {
1335 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001336 SkMatrix matrix;
1337 matrix.setAll(SkDoubleToScalar(1),
1338 SkDoubleToScalar(0),
1339 // TODO(edisonn): use writing mode, vertical/horizontal.
1340 SkDoubleToScalar(-dx), // amount is substracted!!!
1341 SkDoubleToScalar(0),
1342 SkDoubleToScalar(1),
1343 SkDoubleToScalar(0),
1344 SkDoubleToScalar(0),
1345 SkDoubleToScalar(0),
1346 SkDoubleToScalar(1));
1347
1348 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1349 }
1350 }
1351 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1352}
1353
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001354static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001355 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001356 return kOK_PdfResult;
1357}
1358
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001359static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001360 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1361}
1362
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001363static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001364 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1365}
1366
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001367static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001368 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001369// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001370
1371 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1372
1373 bool doubles = true;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001374 if (colorOperator->fColorSpace.equals("Indexed")) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001375 doubles = false;
1376 }
1377
1378#ifdef PDF_TRACE
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001379 printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001380#endif
1381
1382 for (int i = n - 1; i >= 0 ; i--) {
1383 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001384 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1385// } else {
1386// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001387 }
1388 }
1389
1390 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001391 // TODO(edisonn): do possible field values to enum at parsing time!
1392 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001393 if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001394 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2])));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001395 }
1396 return kPartial_PdfResult;
1397}
1398
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001399static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001400 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1401}
1402
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001403static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001404 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1405}
1406
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001407static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001408 //SkPdfString* name;
1409 if (pdfContext->fObjectStack.top()->isName()) {
1410 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001411 pdfContext->fObjectStack.pop();
1412 }
1413
1414 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1415 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1416
1417 return kPartial_PdfResult;
1418}
1419
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001420static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001421 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1422}
1423
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001424static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001425 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1426}
1427
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001428static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001429 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001430 return kNYI_PdfResult;
1431}
1432
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001433static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001434 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1435}
1436
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001437static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1439}
1440
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001441static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001442 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1443 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1444 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001445
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001446 colorOperator->fColorSpace = strings_DeviceRGB;
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001447 colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b)));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001448 return kOK_PdfResult;
1449}
1450
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001451static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001452 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1453}
1454
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001455static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001456 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1457}
1458
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001459static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001460 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001461 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1462 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1463 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1464 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001465
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001466 colorOperator->fColorSpace = strings_DeviceCMYK;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001467 // TODO(edisonn): Set color.
1468 return kNYI_PdfResult;
1469}
1470
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001471static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001472 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1473}
1474
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001475static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001476 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1477}
1478
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001479static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001480 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1481 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1482
1483 return kOK_PdfResult;
1484}
1485
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001486static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001487 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1488
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001489 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1490 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1491
edisonn@google.com96ba3aa2013-07-28 20:04:35 +00001492 return kOK_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001493}
1494
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001495static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001496 *looper = new PdfCompatibilitySectionLooper();
1497 return kOK_PdfResult;
1498}
1499
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001500static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001501#ifdef ASSERT_BAD_PDF_OPS
1502 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1503 // have the assert when testing good pdfs.
1504#endif
1505 return kIgnoreError_PdfResult;
1506}
1507
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001508static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001509 *looper = new PdfInlineImageLooper();
1510 return kOK_PdfResult;
1511}
1512
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001513static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001514#ifdef ASSERT_BAD_PDF_OPS
1515 SkASSERT(false); // must be processed in inline image looper, but let's
1516 // have the assert when testing good pdfs.
1517#endif
1518 return kIgnoreError_PdfResult;
1519}
1520
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001521static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001522#ifdef ASSERT_BAD_PDF_OPS
1523 SkASSERT(false); // must be processed in inline image looper, but let's
1524 // have the assert when testing good pdfs.
1525#endif
1526 return kIgnoreError_PdfResult;
1527}
1528
1529//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001530static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001531 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001532 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1533
1534 return kOK_PdfResult;
1535}
1536
1537//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 +00001538static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001539 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001540 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001541
1542 return kNYI_PdfResult;
1543}
1544
1545//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 +00001546static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001547 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001548 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001549
1550 return kNYI_PdfResult;
1551}
1552
1553//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001554static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001555 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001556 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001557
1558 return kNYI_PdfResult;
1559}
1560
1561//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1562//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001563static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001564 pdfContext->fObjectStack.pop();
1565 pdfContext->fObjectStack.pop();
1566
1567 return kNYI_PdfResult;
1568}
1569
1570//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 +00001571static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001572 pdfContext->fObjectStack.pop();
1573
1574 return kNYI_PdfResult;
1575}
1576
1577//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1578//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1579//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001580static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001581 pdfContext->fObjectStack.pop();
1582
1583 return kNYI_PdfResult;
1584}
1585
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001586
1587// TODO(edisonn): security review here, make sure all parameters are valid, and safe.
1588void skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) {
1589 pdfContext->fGraphicsState.fNonStroking.fOpacity = ca;
1590}
1591
1592void skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) {
1593 pdfContext->fGraphicsState.fStroking.fOpacity = CA;
1594}
1595
1596void skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) {
1597 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1598}
1599
1600void skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) {
1601 pdfContext->fGraphicsState.fLineCap = (int)lineCap;
1602}
1603
1604void skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) {
1605 pdfContext->fGraphicsState.fLineJoin = (int)lineJoin;
1606}
1607
1608void skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) {
1609 pdfContext->fGraphicsState.fMiterLimit = miterLimit;
1610}
1611
1612void skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) {
1613 // TODO(edisonn): verify input
1614 if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) {
1615 // TODO(edisonn): report error/warning
1616 return;
1617 }
1618
1619 SkPdfArray* intervals = (SkPdfArray*)dash->objAtAIndex(0);
1620 int cnt = intervals->size();
1621 if (cnt >= 256) {
1622 // TODO(edisonn): report error/warning, unsuported;
1623 // TODO(edisonn): alloc memory
1624 return;
1625 }
1626 for (int i = 0; i < cnt; i++) {
1627 if (!intervals->objAtAIndex(i)->isNumber()) {
1628 // TODO(edisonn): report error/warning
1629 return;
1630 }
1631 }
1632
1633 pdfContext->fGraphicsState.fDashPhase = dash->objAtAIndex(1)->scalarValue();
1634 pdfContext->fGraphicsState.fDashArrayLength = cnt;
1635 for (int i = 0 ; i < cnt; i++) {
1636 pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue();
1637 }
1638}
1639
1640void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) {
1641 if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) {
1642 // TODO(edisonn): report error/warning
1643 return;
1644 }
1645 skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue());
1646}
1647
edisonn@google.come878e722013-07-29 19:10:58 +00001648SkTDict<SkXfermode::Mode> gPdfBlendModes(20);
1649
1650class InitBlendModes {
1651public:
1652 InitBlendModes() {
1653 // TODO(edisonn): use the python code generator?
1654 // TABLE 7.2 Standard separable blend modes
1655 gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode);
1656 gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode);
1657 gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode);
1658 gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode);
1659 gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode);
1660 gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode);
1661 gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode);
1662 gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode);
1663 gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode);
1664 gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode);
1665 gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode);
1666 gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode);
1667
1668 // TABLE 7.3 Standard nonseparable blend modes
1669 gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode);
1670 gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode);
1671 gPdfBlendModes.set("Color", SkXfermode::kColor_Mode);
1672 gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode);
1673 }
1674};
1675
1676InitBlendModes _gDummyInniter;
1677
1678SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) {
1679 SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1680 if (gPdfBlendModes.find(blendMode, len, &mode)) {
1681 return mode;
1682 }
1683
1684 return (SkXfermode::Mode)(SkXfermode::kLastMode + 1);
1685}
1686
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001687void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) {
edisonn@google.come878e722013-07-29 19:10:58 +00001688 SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length());
1689 if (mode <= SkXfermode::kLastMode) {
1690 pdfContext->fGraphicsState.fBlendModesLength = 1;
1691 pdfContext->fGraphicsState.fBlendModes[0] = mode;
1692 } else {
1693 // TODO(edisonn): report unknown blend mode
1694 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001695}
1696
1697void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) {
edisonn@google.come878e722013-07-29 19:10:58 +00001698 if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) {
1699 // TODO(edisonn): report error/warning
1700 return;
1701 }
1702 SkXfermode::Mode modes[256];
1703 int cnt = blendModes->size();
1704 for (int i = 0; i < cnt; i++) {
1705 SkPdfObject* name = blendModes->objAtAIndex(i);
1706 if (!name->isName()) {
1707 // TODO(edisonn): report error/warning
1708 return;
1709 }
1710 SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr());
1711 if (mode > SkXfermode::kLastMode) {
1712 // TODO(edisonn): report error/warning
1713 return;
1714 }
1715 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001716
edisonn@google.come878e722013-07-29 19:10:58 +00001717 pdfContext->fGraphicsState.fBlendModesLength = cnt;
1718 for (int i = 0; i < cnt; i++) {
1719 pdfContext->fGraphicsState.fBlendModes[i] = modes[i];
1720 }
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001721}
1722
1723void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) {
1724 // TODO(edisonn): verify input
edisonn@google.come878e722013-07-29 19:10:58 +00001725 if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001726 pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask;
edisonn@google.come878e722013-07-29 19:10:58 +00001727 } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) {
1728 SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask;
1729 pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true);
1730 } else {
1731 // TODO (edisonn): report error/warning
1732 }
1733}
1734
1735void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) {
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001736 if (sMask == "None") {
1737 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
1738 pdfContext->fGraphicsState.fSMask = SkBitmap();
1739 return;
1740 }
1741
edisonn@google.come878e722013-07-29 19:10:58 +00001742 //Next, get the ExtGState Dictionary from the Resource Dictionary:
1743 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
1744
1745 if (extGStateDictionary == NULL) {
1746#ifdef PDF_TRACE
1747 printf("ExtGState is NULL!\n");
1748#endif
1749 // TODO (edisonn): report error/warning
1750 return;
1751 }
1752
1753 SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str()));
1754 if (!obj || !obj->isDictionary()) {
1755 // TODO (edisonn): report error/warning
1756 return;
1757 }
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00001758
1759 pdfContext->fGraphicsState.fSoftMaskDictionary = NULL;
1760 pdfContext->fGraphicsState.fSMask = SkBitmap();
1761
edisonn@google.come878e722013-07-29 19:10:58 +00001762 skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary());
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001763}
1764
1765void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) {
1766 pdfContext->fGraphicsState.fAlphaSource = alphaSource;
1767}
1768
1769
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001770//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1771//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 +00001772static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001773 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001774
1775#ifdef PDF_TRACE
1776 std::string str;
1777#endif
1778
1779 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001780 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001781
1782 if (extGStateDictionary == NULL) {
1783#ifdef PDF_TRACE
1784 printf("ExtGState is NULL!\n");
1785#endif
1786 return kIgnoreError_PdfResult;
1787 }
1788
edisonn@google.com571c70b2013-07-10 17:09:50 +00001789 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001790
edisonn@google.com571c70b2013-07-10 17:09:50 +00001791 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1792 return kIgnoreError_PdfResult;
1793 }
1794 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001795
1796 // TODO(edisonn): now load all those properties in graphic state.
1797 if (gs == NULL) {
1798 return kIgnoreError_PdfResult;
1799 }
1800
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001801 if (gs->has_LW()) {
1802 skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc));
1803 }
1804
1805 if (gs->has_LC()) {
1806 skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc));
1807 }
1808
1809 if (gs->has_LJ()) {
1810 skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc));
1811 }
1812
1813 if (gs->has_ML()) {
1814 skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc));
1815 }
1816
1817 if (gs->has_D()) {
1818 skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc));
1819 }
1820
1821 if (gs->has_Font()) {
1822 skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc));
1823 }
1824
1825 if (gs->has_BM()) {
1826 if (gs->isBMAName(pdfContext->fPdfDoc)) {
1827 skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc));
1828 } else if (gs->isBMAArray(pdfContext->fPdfDoc)) {
1829 skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc));
1830 } else {
1831 // TODO(edisonn): report/warn
1832 }
1833 }
1834
1835 if (gs->has_SMask()) {
1836 if (gs->isSMaskAName(pdfContext->fPdfDoc)) {
1837 skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc));
1838 } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) {
1839 skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc));
1840 } else {
1841 // TODO(edisonn): report/warn
1842 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001843 }
1844
1845 if (gs->has_ca()) {
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001846 skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001847 }
1848
edisonn@google.coma0cefa12013-07-28 18:34:14 +00001849 if (gs->has_CA()) {
1850 skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc));
1851 }
1852
1853 if (gs->has_AIS()) {
1854 skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001855 }
1856
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001857 return kNYI_PdfResult;
1858}
1859
1860//charSpace Tc Set the character spacing, Tc
1861//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1862//Initial value: 0.
1863PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001864 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001865 pdfContext->fGraphicsState.fCharSpace = charSpace;
1866
1867 return kOK_PdfResult;
1868}
1869
1870//wordSpace Tw Set the word spacing, T
1871//w
1872//, to wordSpace, which is a number expressed in unscaled
1873//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1874//value: 0.
1875PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001876 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001877 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1878
1879 return kOK_PdfResult;
1880}
1881
1882//scale Tz Set the horizontal scaling, Th
1883//, to (scale ˜ 100). scale is a number specifying the
1884//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001885static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001886 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001887
1888 return kNYI_PdfResult;
1889}
1890
1891//render Tr Set the text rendering mode, T
1892//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001893static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001894 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001895
1896 return kNYI_PdfResult;
1897}
1898//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1899//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001900static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001901 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001902
1903 return kNYI_PdfResult;
1904}
1905
1906//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001907static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001908 pdfContext->fObjectStack.pop();
1909 pdfContext->fObjectStack.pop();
1910
1911 return kNYI_PdfResult;
1912}
1913
1914//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001915static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001916 pdfContext->fObjectStack.pop();
1917 pdfContext->fObjectStack.pop();
1918 pdfContext->fObjectStack.pop();
1919 pdfContext->fObjectStack.pop();
1920 pdfContext->fObjectStack.pop();
1921 pdfContext->fObjectStack.pop();
1922
1923 return kNYI_PdfResult;
1924}
1925
1926//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001927static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001928 pdfContext->fObjectStack.pop();
1929
1930 return kNYI_PdfResult;
1931}
1932
1933//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001934static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00001935 SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001936
edisonn@google.com571c70b2013-07-10 17:09:50 +00001937 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001938
1939 if (xObject == NULL) {
1940#ifdef PDF_TRACE
1941 printf("XObject is NULL!\n");
1942#endif
1943 return kIgnoreError_PdfResult;
1944 }
1945
edisonn@google.com571c70b2013-07-10 17:09:50 +00001946 SkPdfObject* value = xObject->get(name);
1947 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001948
1949#ifdef PDF_TRACE
1950// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001951// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001952#endif
1953
edisonn@google.com571c70b2013-07-10 17:09:50 +00001954 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001955}
1956
1957//tag MP Designate a marked-content point. tag is a name object indicating the role or
1958//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001959static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001960 pdfContext->fObjectStack.pop();
1961
1962 return kNYI_PdfResult;
1963}
1964
1965//tag properties DP Designate a marked-content point with an associated property list. tag is a
1966//name object indicating the role or significance of the point; properties is
1967//either an inline dictionary containing the property list or a name object
1968//associated with it in the Properties subdictionary of the current resource
1969//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001970static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001971 pdfContext->fObjectStack.pop();
1972 pdfContext->fObjectStack.pop();
1973
1974 return kNYI_PdfResult;
1975}
1976
1977//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1978//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001979static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001980 pdfContext->fObjectStack.pop();
1981
1982 return kNYI_PdfResult;
1983}
1984
1985//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1986//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1987//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 +00001988static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001989 pdfContext->fObjectStack.pop();
1990 pdfContext->fObjectStack.pop();
1991
1992 return kNYI_PdfResult;
1993}
1994
1995//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001996static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001997 return kNYI_PdfResult;
1998}
1999
edisonn@google.coma3356fc2013-07-10 18:20:06 +00002000static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002001 static bool gInitialized = false;
2002 if (gInitialized) {
2003 return;
2004 }
2005
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002006 gPdfOps.set("q", PdfOp_q);
2007 gPdfOps.set("Q", PdfOp_Q);
2008 gPdfOps.set("cm", PdfOp_cm);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002009
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002010 gPdfOps.set("TD", PdfOp_TD);
2011 gPdfOps.set("Td", PdfOp_Td);
2012 gPdfOps.set("Tm", PdfOp_Tm);
2013 gPdfOps.set("T*", PdfOp_T_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002014
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002015 gPdfOps.set("m", PdfOp_m);
2016 gPdfOps.set("l", PdfOp_l);
2017 gPdfOps.set("c", PdfOp_c);
2018 gPdfOps.set("v", PdfOp_v);
2019 gPdfOps.set("y", PdfOp_y);
2020 gPdfOps.set("h", PdfOp_h);
2021 gPdfOps.set("re", PdfOp_re);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002022
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002023 gPdfOps.set("S", PdfOp_S);
2024 gPdfOps.set("s", PdfOp_s);
2025 gPdfOps.set("f", PdfOp_f);
2026 gPdfOps.set("F", PdfOp_F);
2027 gPdfOps.set("f*", PdfOp_f_star);
2028 gPdfOps.set("B", PdfOp_B);
2029 gPdfOps.set("B*", PdfOp_B_star);
2030 gPdfOps.set("b", PdfOp_b);
2031 gPdfOps.set("b*", PdfOp_b_star);
2032 gPdfOps.set("n", PdfOp_n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002033
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002034 gPdfOps.set("BT", PdfOp_BT);
2035 gPdfOps.set("ET", PdfOp_ET);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002036
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002037 gPdfOps.set("Tj", PdfOp_Tj);
2038 gPdfOps.set("'", PdfOp_quote);
2039 gPdfOps.set("\"", PdfOp_doublequote);
2040 gPdfOps.set("TJ", PdfOp_TJ);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002041
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002042 gPdfOps.set("CS", PdfOp_CS);
2043 gPdfOps.set("cs", PdfOp_cs);
2044 gPdfOps.set("SC", PdfOp_SC);
2045 gPdfOps.set("SCN", PdfOp_SCN);
2046 gPdfOps.set("sc", PdfOp_sc);
2047 gPdfOps.set("scn", PdfOp_scn);
2048 gPdfOps.set("G", PdfOp_G);
2049 gPdfOps.set("g", PdfOp_g);
2050 gPdfOps.set("RG", PdfOp_RG);
2051 gPdfOps.set("rg", PdfOp_rg);
2052 gPdfOps.set("K", PdfOp_K);
2053 gPdfOps.set("k", PdfOp_k);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002054
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002055 gPdfOps.set("W", PdfOp_W);
2056 gPdfOps.set("W*", PdfOp_W_star);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002057
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002058 gPdfOps.set("BX", PdfOp_BX);
2059 gPdfOps.set("EX", PdfOp_EX);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002060
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002061 gPdfOps.set("BI", PdfOp_BI);
2062 gPdfOps.set("ID", PdfOp_ID);
2063 gPdfOps.set("EI", PdfOp_EI);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002064
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002065 gPdfOps.set("w", PdfOp_w);
2066 gPdfOps.set("J", PdfOp_J);
2067 gPdfOps.set("j", PdfOp_j);
2068 gPdfOps.set("M", PdfOp_M);
2069 gPdfOps.set("d", PdfOp_d);
2070 gPdfOps.set("ri", PdfOp_ri);
2071 gPdfOps.set("i", PdfOp_i);
2072 gPdfOps.set("gs", PdfOp_gs);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002073
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002074 gPdfOps.set("Tc", PdfOp_Tc);
2075 gPdfOps.set("Tw", PdfOp_Tw);
2076 gPdfOps.set("Tz", PdfOp_Tz);
2077 gPdfOps.set("TL", PdfOp_TL);
2078 gPdfOps.set("Tf", PdfOp_Tf);
2079 gPdfOps.set("Tr", PdfOp_Tr);
2080 gPdfOps.set("Ts", PdfOp_Ts);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002081
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002082 gPdfOps.set("d0", PdfOp_d0);
2083 gPdfOps.set("d1", PdfOp_d1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002084
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002085 gPdfOps.set("sh", PdfOp_sh);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002086
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002087 gPdfOps.set("Do", PdfOp_Do);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002088
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002089 gPdfOps.set("MP", PdfOp_MP);
2090 gPdfOps.set("DP", PdfOp_DP);
2091 gPdfOps.set("BMC", PdfOp_BMC);
2092 gPdfOps.set("BDC", PdfOp_BDC);
2093 gPdfOps.set("EMC", PdfOp_EMC);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002094
2095 gInitialized = true;
2096}
2097
2098class InitPdfOps {
2099public:
2100 InitPdfOps() {
2101 initPdfOperatorRenderes();
2102 }
2103};
2104
2105InitPdfOps gInitPdfOps;
2106
2107void reportPdfRenderStats() {
2108 std::map<std::string, int>::iterator iter;
2109
2110 for (int i = 0 ; i < kCount_PdfResult; i++) {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002111 SkTDict<int>::Iter iter(gRenderStats[i]);
2112 const char* key;
2113 int value = 0;
2114 while ((key = iter.next(&value)) != NULL) {
2115 printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002116 }
2117 }
2118}
2119
2120PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002121 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002122 {
2123 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002124 PdfOperatorRenderer pdfOperatorRenderer = NULL;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002125 if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002126 // caller, main work is done by pdfOperatorRenderer(...)
2127 PdfTokenLooper* childLooper = NULL;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002128 PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper);
2129
2130 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002131 gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt);
2132 gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002133
2134 if (childLooper) {
2135 childLooper->setUp(this);
2136 childLooper->loop();
2137 delete childLooper;
2138 }
2139 } else {
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002140 int cnt = 0;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +00002141 gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt);
2142 gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002143 }
2144 }
2145 else if (token.fType == kObject_TokenType)
2146 {
2147 fPdfContext->fObjectStack.push( token.fObject );
2148 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002149 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00002150 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002151 return kIgnoreError_PdfResult;
2152 }
2153 return kOK_PdfResult;
2154}
2155
2156void PdfMainLooper::loop() {
2157 PdfToken token;
2158 while (readToken(fTokenizer, &token)) {
2159 consumeToken(token);
2160 }
2161}
2162
2163PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002164 SkASSERT(false);
2165 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002166}
2167
2168void PdfInlineImageLooper::loop() {
edisonn@google.com78b38b12013-07-15 18:20:58 +00002169 doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage());
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002170}
2171
2172PdfResult PdfInlineImageLooper::done() {
2173
2174 // TODO(edisonn): long to short names
2175 // TODO(edisonn): set properties in a map
2176 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
2177 // the stream.
2178
2179 SkBitmap bitmap;
2180 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
2181
2182 // TODO(edisonn): matrix use.
2183 // Draw dummy red square, to show the prezence of the inline image.
2184 fCanvas->drawBitmap(bitmap,
2185 SkDoubleToScalar(0),
2186 SkDoubleToScalar(0),
2187 NULL);
2188 return kNYI_PdfResult;
2189}
2190
2191PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
2192 return fParent->consumeToken(token);
2193}
2194
2195void PdfCompatibilitySectionLooper::loop() {
2196 // TODO(edisonn): save stacks position, or create a new stack?
2197 // TODO(edisonn): what happens if we pop out more variables then when we started?
2198 // restore them? fail? We could create a new operands stack for every new BX/EX section,
2199 // pop-ing too much will not affect outside the section.
2200 PdfToken token;
2201 while (readToken(fTokenizer, &token)) {
2202 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
2203 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
2204 looper->setUp(this);
2205 looper->loop();
2206 delete looper;
2207 } else {
2208 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
2209 fParent->consumeToken(token);
2210 }
2211 }
2212 // TODO(edisonn): restore stack.
2213}
2214
2215// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
2216// TODO(edisonn): Add API for Forms viewing and editing
2217// e.g. SkBitmap getPage(int page);
2218// int formsCount();
2219// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
2220// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
2221// if we load the first page, and we zoom to fit to screen horizontally, then load only those
2222// resources needed, so the preview is fast.
2223// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
2224// references automatically.
2225
edisonn@google.com222382b2013-07-10 22:33:10 +00002226PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00002227
edisonn@google.com444e25a2013-07-11 15:20:50 +00002228bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00002229 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002230 return false;
2231 }
2232
edisonn@google.com222382b2013-07-10 22:33:10 +00002233 if (page < 0 || page >= pages()) {
2234 return false;
2235 }
2236
edisonn@google.com222382b2013-07-10 22:33:10 +00002237 PdfContext pdfContext(fPdfDoc);
edisonn@google.com2ccc3af2013-07-23 17:43:18 +00002238
edisonn@google.com222382b2013-07-10 22:33:10 +00002239 pdfContext.fOriginalMatrix = SkMatrix::I();
2240 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
2241
2242 gPdfContext = &pdfContext;
2243
2244 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00002245 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00002246 SkScalar w = dst.width();
2247 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00002248
edisonn@google.com444e25a2013-07-11 15:20:50 +00002249 SkScalar wp = fPdfDoc->MediaBox(page).width();
2250 SkScalar hp = fPdfDoc->MediaBox(page).height();
2251
2252 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 +00002253// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2254
2255 // TODO(edisonn): add flag for this app to create sourunding buffer zone
2256 // TODO(edisonn): add flagg for no clipping.
2257 // Use larger image to make sure we do not draw anything outside of page
2258 // could be used in tests.
2259
2260#ifdef PDF_DEBUG_3X
2261 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)};
2262#else
2263 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
2264#endif
2265 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
2266 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
2267
2268 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
2269 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
2270
2271 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
2272 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
2273
2274 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
2275 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
2276
2277
edisonn@google.coma0cefa12013-07-28 18:34:14 +00002278 pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix;
2279 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM;
2280 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM;
edisonn@google.com222382b2013-07-10 22:33:10 +00002281
edisonn@google.com222382b2013-07-10 22:33:10 +00002282#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00002283 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00002284#endif
2285
edisonn@google.com444e25a2013-07-11 15:20:50 +00002286 canvas->setMatrix(pdfContext.fOriginalMatrix);
2287
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002288 doPage(&pdfContext, canvas, fPdfDoc->page(page));
2289
2290 // TODO(edisonn:) erase with white before draw?
edisonn@google.com222382b2013-07-10 22:33:10 +00002291// SkPaint paint;
edisonn@google.com88fc03d2013-07-30 13:34:10 +00002292// paint.setColor(SK_ColorWHITE);
edisonn@google.com222382b2013-07-10 22:33:10 +00002293// canvas->drawRect(rect, paint);
2294
edisonn@google.com222382b2013-07-10 22:33:10 +00002295
2296 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00002297 return true;
2298}
edisonn@google.com222382b2013-07-10 22:33:10 +00002299
2300bool SkPdfRenderer::load(const SkString inputFileName) {
2301 unload();
2302
2303 // TODO(edisonn): create static function that could return NULL if there are errors
2304 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com6a9d4362013-07-11 16:25:51 +00002305 if (fPdfDoc->pages() == 0) {
2306 delete fPdfDoc;
2307 fPdfDoc = NULL;
2308 }
edisonn@google.com222382b2013-07-10 22:33:10 +00002309
2310 return fPdfDoc != NULL;
2311}
2312
edisonn@google.com147adb12013-07-24 15:56:19 +00002313bool SkPdfRenderer::load(SkStream* stream) {
2314 unload();
2315
2316 // TODO(edisonn): create static function that could return NULL if there are errors
2317 fPdfDoc = new SkNativeParsedPDF(stream);
2318 if (fPdfDoc->pages() == 0) {
2319 delete fPdfDoc;
2320 fPdfDoc = NULL;
2321 }
2322
2323 return fPdfDoc != NULL;
2324}
2325
2326
edisonn@google.com222382b2013-07-10 22:33:10 +00002327int SkPdfRenderer::pages() const {
2328 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2329}
2330
2331void SkPdfRenderer::unload() {
2332 delete fPdfDoc;
2333 fPdfDoc = NULL;
2334}
2335
2336SkRect SkPdfRenderer::MediaBox(int page) const {
2337 SkASSERT(fPdfDoc);
2338 return fPdfDoc->MediaBox(page);
2339}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002340
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002341size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002342 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2343}
edisonn@google.com147adb12013-07-24 15:56:19 +00002344
2345bool SkPDFNativeRenderToBitmap(SkStream* stream,
2346 SkBitmap* output,
2347 int page,
2348 SkPdfContent content,
2349 double dpi) {
2350 SkASSERT(page >= 0);
2351 SkPdfRenderer renderer;
2352 renderer.load(stream);
2353 if (!renderer.loaded() || page >= renderer.pages() || page < 0) {
2354 return false;
2355 }
2356
2357 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
2358
2359 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2360 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0)));
2361
2362 rect = SkRect::MakeWH(width, height);
2363
2364 setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
2365
2366 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output)));
2367 SkCanvas canvas(device);
2368
2369 return renderer.renderPage(page, &canvas, rect);
2370}