blob: 8502ac54b28da157cb871b5ea2178d1058492756 [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.com131d4ee2013-06-26 17:48:12 +000019
edisonn@google.com15b11182013-07-11 14:43:15 +000020#include "SkPdfBasics.h"
21#include "SkPdfNativeTokenizer.h"
22
edisonn@google.com131d4ee2013-06-26 17:48:12 +000023#include <iostream>
24#include <cstdio>
25#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000026#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000027
edisonn@google.com15b11182013-07-11 14:43:15 +000028extern "C" PdfContext* gPdfContext;
29extern "C" SkBitmap* gDumpBitmap;
30extern "C" SkCanvas* gDumpCanvas;
31
edisonn@google.com131d4ee2013-06-26 17:48:12 +000032__SK_FORCE_IMAGE_DECODER_LINKING;
33
34// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
35// keep for each object pos in file
36// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
37
38// TODO(edisonn): security - validate all the user input, all pdf!
39
edisonn@google.com6e49c342013-06-27 20:03:43 +000040// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
41// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000042
edisonn@google.com3aac1f92013-07-02 22:42:53 +000043// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
44/*
45#ifdef PDF_TRACE
46 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000047 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000048 printf("Print Tf Resources: %s\n", str.c_str());
49#endif
50 */
51
edisonn@google.com131d4ee2013-06-26 17:48:12 +000052#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000053#include "SkPdfMapper_autogen.h"
edisonn@google.com222382b2013-07-10 22:33:10 +000054#include "SkPdfRenderer.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000055
56#include "SkPdfBasics.h"
57#include "SkPdfUtils.h"
58
59#include "SkPdfFont.h"
60
edisonn@google.com131d4ee2013-06-26 17:48:12 +000061/*
62 * TODO(edisonn):
63 * - all font types and all ppdf font features
64 * - word spacing
65 * - load font for baidu.pdf
66 * - load font for youtube.pdf
67 * - parser for pdf from the definition already available in pdfspec_autogen.py
68 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000069 * - 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 +000070 * - load gs/ especially smask and already known prop (skp) ... in progress
71 * - wrapper on classes for customizations? e.g.
72 * SkPdfPageObjectVanila - has only the basic loaders/getters
73 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
74 * need to find a nice object model for all this with constructors and factories
75 * - deal with inheritable automatically ?
76 * - deal with specific type in spec directly, add all dictionary types to known types
77*/
78
79using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000080
edisonn@google.com222382b2013-07-10 22:33:10 +000081
82
83// TODO(edisonn): Document PdfTokenLooper and subclasses.
84class PdfTokenLooper {
85protected:
86 PdfTokenLooper* fParent;
87 SkPdfNativeTokenizer* fTokenizer;
88 PdfContext* fPdfContext;
89 SkCanvas* fCanvas;
90
91public:
92 PdfTokenLooper(PdfTokenLooper* parent,
93 SkPdfNativeTokenizer* tokenizer,
94 PdfContext* pdfContext,
95 SkCanvas* canvas)
96 : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {}
97
98 virtual ~PdfTokenLooper() {}
99
100 virtual PdfResult consumeToken(PdfToken& token) = 0;
101 virtual void loop() = 0;
102
103 void setUp(PdfTokenLooper* parent) {
104 fParent = parent;
105 fTokenizer = parent->fTokenizer;
106 fPdfContext = parent->fPdfContext;
107 fCanvas = parent->fCanvas;
108 }
109};
110
111class PdfMainLooper : public PdfTokenLooper {
112public:
113 PdfMainLooper(PdfTokenLooper* parent,
114 SkPdfNativeTokenizer* tokenizer,
115 PdfContext* pdfContext,
116 SkCanvas* canvas)
117 : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {}
118
119 virtual PdfResult consumeToken(PdfToken& token);
120 virtual void loop();
121};
122
123class PdfInlineImageLooper : public PdfTokenLooper {
124public:
125 PdfInlineImageLooper()
126 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
127
128 virtual PdfResult consumeToken(PdfToken& token);
129 virtual void loop();
130 PdfResult done();
131};
132
133class PdfCompatibilitySectionLooper : public PdfTokenLooper {
134public:
135 PdfCompatibilitySectionLooper()
136 : PdfTokenLooper(NULL, NULL, NULL, NULL) {}
137
138 virtual PdfResult consumeToken(PdfToken& token);
139 virtual void loop();
140};
141
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000142// Utilities
143static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
144 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
145
146 bitmap->allocPixels();
147 bitmap->eraseColor(color);
148}
149
150// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000151static int GetColorSpaceComponents(const std::string& colorSpace) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000152 if (colorSpace == "DeviceCMYK") {
153 return 4;
154 } else if (colorSpace == "DeviceGray" ||
155 colorSpace == "CalGray" ||
156 colorSpace == "Indexed") {
157 return 1;
158 } else if (colorSpace == "DeviceRGB" ||
159 colorSpace == "CalRGB" ||
160 colorSpace == "Lab") {
161 return 3;
162 } else {
163 return 0;
164 }
165}
166
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000167SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000168 SkMatrix matrix;
169 matrix.setAll(SkDoubleToScalar(array[0]),
170 SkDoubleToScalar(array[2]),
171 SkDoubleToScalar(array[4]),
172 SkDoubleToScalar(array[1]),
173 SkDoubleToScalar(array[3]),
174 SkDoubleToScalar(array[5]),
175 SkDoubleToScalar(0),
176 SkDoubleToScalar(0),
177 SkDoubleToScalar(1));
178
179 return matrix;
180}
181
182SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
183 double array[6];
184
185 // TODO(edisonn): security issue, ret if size() != 6
186 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000187 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000188 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000189 return SkMatrix::I(); // TODO(edisonn): report issue
190 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000191 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000192 }
193
194 return SkMatrixFromPdfMatrix(array);
195}
196
edisonn@google.com222382b2013-07-10 22:33:10 +0000197
198extern "C" SkNativeParsedPDF* gDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000199SkBitmap* gDumpBitmap = NULL;
200SkCanvas* gDumpCanvas = NULL;
201char gLastKeyword[100] = "";
202int gLastOpKeyword = -1;
203char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
204int gReadOp = 0;
205
206
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000207#ifdef PDF_TRACE_DIFF_IN_PNG
208static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000209 return true;
210 if (*pdfOp == '\0') return false;
211
212 char markedPdfOp[100] = ",";
213 strcat(markedPdfOp, pdfOp);
214 strcat(markedPdfOp, ",");
215
216 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
217}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000218#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000219
edisonn@google.com222382b2013-07-10 22:33:10 +0000220
221
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000222// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000223static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000224 bool ret = fTokenizer->readToken(token);
225
226 gReadOp++;
227
228#ifdef PDF_TRACE_DIFF_IN_PNG
229 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
230 // the numbar and name of last operation, so the file name will reflect op that changed.
231 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
232 gDumpCanvas->flush();
233
234 SkBitmap bitmap;
235 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
236
237 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
238
239 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
240 SkCanvas canvas(device);
241
242 // draw context stuff here
243 SkPaint blueBorder;
244 blueBorder.setColor(SK_ColorBLUE);
245 blueBorder.setStyle(SkPaint::kStroke_Style);
246 blueBorder.setTextSize(SkDoubleToScalar(20));
247
248 SkString str;
249
250 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
251 if (clipStack) {
252 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
253 const SkClipStack::Element* elem;
254 double y = 0;
255 int total = 0;
256 while (elem = iter.next()) {
257 total++;
258 y += 30;
259
260 switch (elem->getType()) {
261 case SkClipStack::Element::kRect_Type:
262 canvas.drawRect(elem->getRect(), blueBorder);
263 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
264 break;
265 case SkClipStack::Element::kPath_Type:
266 canvas.drawPath(elem->getPath(), blueBorder);
267 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
268 break;
269 case SkClipStack::Element::kEmpty_Type:
270 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
271 break;
272 default:
273 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
274 break;
275 }
276 }
277
278 y += 30;
279 str.printf("Number of clips in stack: %i", total);
280 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
281 }
282
283 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
284 SkPath clipPath;
285 if (clipRegion.getBoundaryPath(&clipPath)) {
286 SkPaint redBorder;
287 redBorder.setColor(SK_ColorRED);
288 redBorder.setStyle(SkPaint::kStroke_Style);
289 canvas.drawPath(clipPath, redBorder);
290 }
291
292 canvas.flush();
293
294 SkString out;
295
296 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
297 // ... and other properties, to be able to debug th code easily
298
299 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
300 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
301 }
302
303 if (token->fType == kKeyword_TokenType) {
304 strcpy(gLastKeyword, token->fKeyword);
305 gLastOpKeyword = gReadOp;
306 } else {
307 strcpy(gLastKeyword, "");
308 }
309#endif
310
311 return ret;
312}
313
314
315
316typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
317
318map<std::string, PdfOperatorRenderer> gPdfOps;
319
320map<std::string, int> gRenderStats[kCount_PdfResult];
321
edisonn@google.com571c70b2013-07-10 17:09:50 +0000322const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000323 "Success",
324 "Partially implemented",
325 "Not yet implemented",
326 "Ignore Error",
327 "Error",
328 "Unsupported/Unknown"
329};
330
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000331static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000332 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000333 SkCanvas* canvas)
334{
335
336 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
337 if (skfont == NULL) {
338 skfont = SkPdfFont::Default();
339 }
340
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000341
edisonn@google.com571c70b2013-07-10 17:09:50 +0000342 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000343 // TODO(edisonn): report warning
344 return kIgnoreError_PdfResult;
345 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000346 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000347
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000348 SkUnencodedText binary(str);
349
350 SkDecodedText decoded;
351
352 if (skfont->encoding() == NULL) {
353 // TODO(edisonn): report warning
354 return kNYI_PdfResult;
355 }
356
357 skfont->encoding()->decodeText(binary, &decoded);
358
359 SkPaint paint;
360 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
361 // Or maybe just not call setTextSize at all?
362 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
363 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
364 }
365
366// if (fCurFont && fCurFont->GetFontScale() != 0) {
367// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
368// }
369
370 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
371
372 canvas->save();
373
374#if 1
375 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
376
377 SkPoint point1;
378 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
379
380 SkMatrix mirror;
381 mirror.setTranslate(0, -point1.y());
382 // TODO(edisonn): fix rotated text, and skewed too
383 mirror.postScale(SK_Scalar1, -SK_Scalar1);
384 // TODO(edisonn): post rotate, skew
385 mirror.postTranslate(0, point1.y());
386
387 matrix.postConcat(mirror);
388
389 canvas->setMatrix(matrix);
390
391 SkTraceMatrix(matrix, "mirrored");
392#endif
393
edisonn@google.com6e49c342013-06-27 20:03:43 +0000394 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000395 canvas->restore();
396
397 return kPartial_PdfResult;
398}
399
400// TODO(edisonn): create header files with declarations!
401PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
402PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
403PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
404PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
405
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000406// TODO(edisonn): perf!!!
407
408static SkColorTable* getGrayColortable() {
409 static SkColorTable* grayColortable = NULL;
410 if (grayColortable == NULL) {
411 SkPMColor* colors = new SkPMColor[256];
412 for (int i = 0 ; i < 256; i++) {
413 colors[i] = SkPreMultiplyARGB(255, i, i, i);
414 }
415 grayColortable = new SkColorTable(colors, 256);
416 }
417 return grayColortable;
418}
419
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000420static SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000421 int width, int height, int bytesPerLine,
422 int bpc, const std::string& colorSpace,
423 bool transparencyMask) {
424 SkBitmap bitmap;
425
edisonn@google.com571c70b2013-07-10 17:09:50 +0000426 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000427//#define MAX_COMPONENTS 10
428
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000429 // TODO(edisonn): assume start of lines are aligned at 32 bits?
430 // Is there a faster way to load the uncompressed stream into a bitmap?
431
432 // minimal support for now
433 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
434 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
435
436 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000437 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000438 for (int w = 0 ; w < width; w++) {
439 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
440 uncompressedStream[3 * w + 1],
441 uncompressedStream[3 * w + 2]);
442 i++;
443 }
444 uncompressedStream += bytesPerLine;
445 }
446
447 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
448 bitmap.setPixels(uncompressedStreamArgb);
449 }
450 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
451 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
452
453 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000454 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000455 for (int w = 0 ; w < width; w++) {
456 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
457 uncompressedStream[w];
458 i++;
459 }
460 uncompressedStream += bytesPerLine;
461 }
462
463 bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
464 width, height);
465 bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
466 }
467
468 // TODO(edisonn): Report Warning, NYI, or error
469 return bitmap;
470}
471
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000472// utils
473
474// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
475// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
476// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
477// skia format, through a table
478
479// this functions returns the image, it does not look at the smask.
480
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000481static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000482 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000483 // TODO(edisonn): report warning to be used in testing.
484 return SkBitmap();
485 }
486
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000487 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
488 int64_t width = image->Width(pdfContext->fPdfDoc);
489 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000490 std::string colorSpace = "DeviceRGB";
491
492 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000493 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
494 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000495 }
496
497/*
498 bool imageMask = image->imageMask();
499
500 if (imageMask) {
501 if (bpc != 0 && bpc != 1) {
502 // TODO(edisonn): report warning to be used in testing.
503 return SkBitmap();
504 }
505 bpc = 1;
506 }
507*/
508
edisonn@google.com571c70b2013-07-10 17:09:50 +0000509 unsigned char* uncompressedStream = NULL;
510 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000511
edisonn@google.com571c70b2013-07-10 17:09:50 +0000512 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000513
edisonn@google.com571c70b2013-07-10 17:09:50 +0000514 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000515 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000516 // TODO(edisonn): report warning to be used in testing.
517 return SkBitmap();
518 }
519
edisonn@google.com571c70b2013-07-10 17:09:50 +0000520 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
521
522 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
523 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
524 (streamDict->isFilterAArray(NULL) &&
525 streamDict->getFilterAsArray(NULL)->size() > 0 &&
526 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
527 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
528 SkBitmap bitmap;
529 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap);
530 return bitmap;
531 }
532
533
534
535 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
536// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
537// obj.GetDictionary().GetKey(PdfName("Filter")));
538// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
539// value = resolveReferenceObject(pdfContext->fPdfDoc,
540// &value->GetArray()[0]);
541// }
542// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
543// SkStream stream = SkStream::
544// SkImageDecoder::Factory()
545// }
546
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000547 int bytesPerLine = uncompressedStreamLength / height;
548#ifdef PDF_TRACE
549 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000550 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000551 }
552#endif
553
554 SkBitmap bitmap = transferImageStreamToBitmap(
555 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000556 (int)width, (int)height, bytesPerLine,
557 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000558 transparencyMask);
559
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000560 return bitmap;
561}
562
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000563static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000564 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000565
566 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000567 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000568 }
569
570 // TODO(edisonn): implement GS SMask. Default to empty right now.
571 return pdfContext->fGraphicsState.fSMask;
572}
573
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000574static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000575 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000576 return kIgnoreError_PdfResult;
577 }
578
579 SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false);
580 SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage);
581
582 canvas->save();
583 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000584
585#if 1
586 SkScalar z = SkIntToScalar(0);
587 SkScalar one = SkIntToScalar(1);
588
589 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
590 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
591 SkMatrix flip;
592 SkAssertResult(flip.setPolyToPoly(from, to, 4));
593 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fMatrix;
594 solveImageFlip.preConcat(flip);
595 canvas->setMatrix(solveImageFlip);
596#endif
597
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000598 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
599
600 if (sMask.empty()) {
601 canvas->drawBitmapRect(image, dst, NULL);
602 } else {
603 canvas->saveLayer(&dst, NULL);
604 canvas->drawBitmapRect(image, dst, NULL);
605 SkPaint xfer;
606 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
607 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
608 canvas->drawBitmapRect(sMask, dst, &xfer);
609 canvas->restore();
610 }
611
612 canvas->restore();
613
614 return kPartial_PdfResult;
615}
616
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000617
618
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000619
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000620static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000621 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000622 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000623 }
624
625 PdfOp_q(pdfContext, canvas, NULL);
626 canvas->save();
627
628
edisonn@google.com571c70b2013-07-10 17:09:50 +0000629 if (skobj->Resources(pdfContext->fPdfDoc)) {
630 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000631 }
632
633 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix");
634
edisonn@google.com571c70b2013-07-10 17:09:50 +0000635 if (skobj->has_Matrix()) {
636 pdfContext->fGraphicsState.fMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000637 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
638 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
639 // TODO(edisonn) reset matrixTm and matricTlm also?
640 }
641
642 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
643
644 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
645
edisonn@google.com571c70b2013-07-10 17:09:50 +0000646 if (skobj->has_BBox()) {
647 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000648 }
649
650 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
651 // For this PdfContentsTokenizer needs to be extended.
652
edisonn@google.com571c70b2013-07-10 17:09:50 +0000653 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000654
edisonn@google.com571c70b2013-07-10 17:09:50 +0000655 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000656 if (tokenizer != NULL) {
657 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
658 looper.loop();
659 delete tokenizer;
660 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000661
662 // TODO(edisonn): should we restore the variable stack at the same state?
663 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
664 canvas->restore();
665 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000666 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000667}
668
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000669//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
670// return kNYI_PdfResult;
671//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000672
edisonn@google.com571c70b2013-07-10 17:09:50 +0000673PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
674 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000675 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000676 }
677
678 PdfOp_q(pdfContext, canvas, NULL);
679 canvas->save();
680
681 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
682 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
683
684 pdfContext->fGraphicsState.fMatrix = pdfContext->fGraphicsState.fMatrixTm;
685 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
686
687 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
688
689 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
690
691 SkRect rm = bBox;
692 pdfContext->fGraphicsState.fMatrix.mapRect(&rm);
693
694 SkTraceRect(rm, "bbox mapped");
695
696 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
697
698 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
699 // For this PdfContentsTokenizer needs to be extended.
700
edisonn@google.com571c70b2013-07-10 17:09:50 +0000701 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000702
edisonn@google.com571c70b2013-07-10 17:09:50 +0000703 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000704 if (tokenizer != NULL) {
705 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
706 looper.loop();
707 delete tokenizer;
708 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000709
710 // TODO(edisonn): should we restore the variable stack at the same state?
711 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
712 canvas->restore();
713 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000714
715 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000716}
717
718
edisonn@google.com571c70b2013-07-10 17:09:50 +0000719// TODO(edisonn): make sure the pointer is unique
720std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000721
722class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000723 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000724public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000725 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000726 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000727 }
728
729 ~CheckRecursiveRendering() {
730 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000731 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000732 }
733
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000734 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000735 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000736 }
737};
738
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000739static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000740 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000741 // Oops, corrupt PDF!
742 return kIgnoreError_PdfResult;
743 }
744
edisonn@google.com571c70b2013-07-10 17:09:50 +0000745 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000746
edisonn@google.com571c70b2013-07-10 17:09:50 +0000747 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000748 {
749 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000750 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000751 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000752 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000753 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
754 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000755 default:
756 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000757 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000758}
759
760PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
761 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
762 canvas->save();
763 return kOK_PdfResult;
764}
765
766PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
767 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
768 pdfContext->fStateStack.pop();
769 canvas->restore();
770 return kOK_PdfResult;
771}
772
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000773static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000774 double array[6];
775 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000776 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000777 pdfContext->fObjectStack.pop();
778 }
779
780 // a b
781 // c d
782 // e f
783
784 // 0 1
785 // 2 3
786 // 4 5
787
788 // sx ky
789 // kx sy
790 // tx ty
791 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
792
793 pdfContext->fGraphicsState.fMatrix.preConcat(matrix);
794
795#ifdef PDF_TRACE
796 printf("cm ");
797 for (int i = 0 ; i < 6 ; i++) {
798 printf("%f ", array[i]);
799 }
800 printf("\n");
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000801 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000802#endif
803
804 return kOK_PdfResult;
805}
806
807//leading TL Set the text leading, Tl
808//, to leading, which is a number expressed in unscaled text
809//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000810static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000811 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000812
813 pdfContext->fGraphicsState.fTextLeading = ty;
814
815 return kOK_PdfResult;
816}
817
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000818static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000819 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
820 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000821
822 double array[6] = {1, 0, 0, 1, tx, ty};
823 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
824
825 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
826 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
827
828 return kPartial_PdfResult;
829}
830
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000831static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000832 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
833 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000834
edisonn@google.com571c70b2013-07-10 17:09:50 +0000835 // TODO(edisonn): Create factory methods or constructors so native is hidden
836 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000837 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000838
839 PdfOp_TL(pdfContext, canvas, looper);
840
edisonn@google.com571c70b2013-07-10 17:09:50 +0000841 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000842 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000843
edisonn@google.com571c70b2013-07-10 17:09:50 +0000844 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000845 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000846
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000847 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
848
849 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
850 // and the cleanup can happen while the user looks at the image
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000851
852 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000853}
854
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000855static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000856 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
857 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
858 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
859 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
860 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
861 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000862
863 double array[6];
864 array[0] = a;
865 array[1] = b;
866 array[2] = c;
867 array[3] = d;
868 array[4] = e;
869 array[5] = f;
870
871 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
872 matrix.postConcat(pdfContext->fGraphicsState.fMatrix);
873
874 // TODO(edisonn): Text positioning.
875 pdfContext->fGraphicsState.fMatrixTm = matrix;
876 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
877
878 return kPartial_PdfResult;
879}
880
881//— T* Move to the start of the next line. This operator has the same effect as the code
882//0 Tl Td
883//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000884static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000885 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
886 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000887
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000888 pdfContext->fObjectStack.push(zero);
889 pdfContext->fObjectStack.push(tl);
890
891 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
892
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000893 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000894}
895
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000896static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000897 if (pdfContext->fGraphicsState.fPathClosed) {
898 pdfContext->fGraphicsState.fPath.reset();
899 pdfContext->fGraphicsState.fPathClosed = false;
900 }
901
edisonn@google.com571c70b2013-07-10 17:09:50 +0000902 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
903 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000904
905 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
906 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
907
908 return kOK_PdfResult;
909}
910
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000911static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000912 if (pdfContext->fGraphicsState.fPathClosed) {
913 pdfContext->fGraphicsState.fPath.reset();
914 pdfContext->fGraphicsState.fPathClosed = false;
915 }
916
edisonn@google.com571c70b2013-07-10 17:09:50 +0000917 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
918 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000919
920 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
921 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
922
923 return kOK_PdfResult;
924}
925
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000926static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000927 if (pdfContext->fGraphicsState.fPathClosed) {
928 pdfContext->fGraphicsState.fPath.reset();
929 pdfContext->fGraphicsState.fPathClosed = false;
930 }
931
edisonn@google.com571c70b2013-07-10 17:09:50 +0000932 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
933 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
934 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
935 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
936 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
937 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000938
939 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
940 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
941 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
942
943 pdfContext->fGraphicsState.fCurPosX = x3;
944 pdfContext->fGraphicsState.fCurPosY = y3;
945
946 return kOK_PdfResult;
947}
948
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000949static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000950 if (pdfContext->fGraphicsState.fPathClosed) {
951 pdfContext->fGraphicsState.fPath.reset();
952 pdfContext->fGraphicsState.fPathClosed = false;
953 }
954
edisonn@google.com571c70b2013-07-10 17:09:50 +0000955 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
956 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
957 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
958 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000959 double y1 = pdfContext->fGraphicsState.fCurPosY;
960 double x1 = pdfContext->fGraphicsState.fCurPosX;
961
962 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
963 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
964 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
965
966 pdfContext->fGraphicsState.fCurPosX = x3;
967 pdfContext->fGraphicsState.fCurPosY = y3;
968
969 return kOK_PdfResult;
970}
971
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000972static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000973 if (pdfContext->fGraphicsState.fPathClosed) {
974 pdfContext->fGraphicsState.fPath.reset();
975 pdfContext->fGraphicsState.fPathClosed = false;
976 }
977
edisonn@google.com571c70b2013-07-10 17:09:50 +0000978 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
979 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000980 double y2 = pdfContext->fGraphicsState.fCurPosY;
981 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000982 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
983 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000984
985 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
986 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
987 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
988
989 pdfContext->fGraphicsState.fCurPosX = x3;
990 pdfContext->fGraphicsState.fCurPosY = y3;
991
992 return kOK_PdfResult;
993}
994
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000995static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000996 if (pdfContext->fGraphicsState.fPathClosed) {
997 pdfContext->fGraphicsState.fPath.reset();
998 pdfContext->fGraphicsState.fPathClosed = false;
999 }
1000
edisonn@google.com571c70b2013-07-10 17:09:50 +00001001 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1002 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1003 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1004 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001005
1006 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
1007 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
1008
1009 pdfContext->fGraphicsState.fCurPosX = x;
1010 pdfContext->fGraphicsState.fCurPosY = y + height;
1011
1012 return kOK_PdfResult;
1013}
1014
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001015static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001016 pdfContext->fGraphicsState.fPath.close();
1017 return kOK_PdfResult;
1018}
1019
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001020static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001021 SkPath path = pdfContext->fGraphicsState.fPath;
1022
1023 if (close) {
1024 path.close();
1025 }
1026
1027 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1028
1029 SkPaint paint;
1030
1031 SkPoint line[2];
1032 if (fill && !stroke && path.isLine(line)) {
1033 paint.setStyle(SkPaint::kStroke_Style);
1034
1035 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1036 paint.setStrokeWidth(SkDoubleToScalar(0));
1037
1038 canvas->drawPath(path, paint);
1039 } else {
1040 if (fill) {
1041 paint.setStyle(SkPaint::kFill_Style);
1042 if (evenOdd) {
1043 path.setFillType(SkPath::kEvenOdd_FillType);
1044 }
1045
1046 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
1047
1048 canvas->drawPath(path, paint);
1049 }
1050
1051 if (stroke) {
1052 paint.setStyle(SkPaint::kStroke_Style);
1053
1054 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
1055
1056 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
1057 canvas->drawPath(path, paint);
1058 }
1059 }
1060
1061 pdfContext->fGraphicsState.fPath.reset();
1062 // todo zoom ... other stuff ?
1063
1064 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1065#ifndef PDF_DEBUG_NO_CLIPING
1066 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1067#endif
1068 }
1069
1070 //pdfContext->fGraphicsState.fClipPath.reset();
1071 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1072
1073 return kPartial_PdfResult;
1074
1075}
1076
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001077static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001078 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1079}
1080
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001081static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001082 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1083}
1084
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001085static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001086 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1087}
1088
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001089static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001090 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1091}
1092
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001093static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001094 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1095}
1096
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001097static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001098 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1099}
1100
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001101static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001102 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1103}
1104
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001105static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001106 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1107}
1108
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001109static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001110 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1111}
1112
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001113static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001114 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1115 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1116#ifndef PDF_DEBUG_NO_CLIPING
1117 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1118#endif
1119 }
1120
1121 //pdfContext->fGraphicsState.fClipPath.reset();
1122 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1123
1124 pdfContext->fGraphicsState.fPathClosed = true;
1125
1126 return kOK_PdfResult;
1127}
1128
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001129static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001130 pdfContext->fGraphicsState.fTextBlock = true;
1131 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
1132 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
1133
1134 return kPartial_PdfResult;
1135}
1136
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001137static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001138 if (!pdfContext->fGraphicsState.fTextBlock) {
1139 return kIgnoreError_PdfResult;
1140 }
1141 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
1142 return kPartial_PdfResult;
1143}
1144
1145//font size Tf Set the text font, Tf
1146//, to font and the text font size, Tfs, to size. font is the name of a
1147//font resource in the Fontsubdictionary of the current resource dictionary; size is
1148//a number representing a scale factor. There is no initial value for either font or
1149//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001150static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001151 pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1152 const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001153
1154#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001155 printf("font name: %s\n", fontName);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001156#endif
1157
edisonn@google.com571c70b2013-07-10 17:09:50 +00001158 if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1159 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1160 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1161 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1162 // TODO(edisonn): try to recover and draw it any way?
1163 return kIgnoreError_PdfResult;
1164 }
1165 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1166
1167 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1168
1169 if (skfont) {
1170 pdfContext->fGraphicsState.fSkFont = skfont;
1171 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001172 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001173 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001174}
1175
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001176static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001177 if (!pdfContext->fGraphicsState.fTextBlock) {
1178 // TODO(edisonn): try to recover and draw it any way?
1179 return kIgnoreError_PdfResult;
1180 }
1181
1182 PdfResult ret = DrawText(pdfContext,
1183 pdfContext->fObjectStack.top(),
1184 canvas);
1185 pdfContext->fObjectStack.pop();
1186
1187 return ret;
1188}
1189
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001190static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001191 if (!pdfContext->fGraphicsState.fTextBlock) {
1192 // TODO(edisonn): try to recover and draw it any way?
1193 return kIgnoreError_PdfResult;
1194 }
1195
1196 PdfOp_T_star(pdfContext, canvas, looper);
1197 // Do not pop, and push, just transfer the param to Tj
1198 return PdfOp_Tj(pdfContext, canvas, looper);
1199}
1200
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001201static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001202 if (!pdfContext->fGraphicsState.fTextBlock) {
1203 // TODO(edisonn): try to recover and draw it any way?
1204 return kIgnoreError_PdfResult;
1205 }
1206
1207 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1208 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1209 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1210
1211 pdfContext->fObjectStack.push(aw);
1212 PdfOp_Tw(pdfContext, canvas, looper);
1213
1214 pdfContext->fObjectStack.push(ac);
1215 PdfOp_Tc(pdfContext, canvas, looper);
1216
1217 pdfContext->fObjectStack.push(str);
1218 PdfOp_quote(pdfContext, canvas, looper);
1219
1220 return kPartial_PdfResult;
1221}
1222
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001223static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001224 if (!pdfContext->fGraphicsState.fTextBlock) {
1225 // TODO(edisonn): try to recover and draw it any way?
1226 return kIgnoreError_PdfResult;
1227 }
1228
edisonn@google.com571c70b2013-07-10 17:09:50 +00001229 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001230 pdfContext->fObjectStack.pop();
1231
edisonn@google.com571c70b2013-07-10 17:09:50 +00001232 if (!array->isArray()) {
1233 return kIgnoreError_PdfResult;
1234 }
1235
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001236 for( int i=0; i<static_cast<int>(array->size()); i++ )
1237 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001238 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001239 SkPdfObject* obj = (*array)[i];
1240 DrawText(pdfContext,
1241 obj,
1242 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001243 } else if ((*array)[i]->isNumber()) {
1244 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001245 SkMatrix matrix;
1246 matrix.setAll(SkDoubleToScalar(1),
1247 SkDoubleToScalar(0),
1248 // TODO(edisonn): use writing mode, vertical/horizontal.
1249 SkDoubleToScalar(-dx), // amount is substracted!!!
1250 SkDoubleToScalar(0),
1251 SkDoubleToScalar(1),
1252 SkDoubleToScalar(0),
1253 SkDoubleToScalar(0),
1254 SkDoubleToScalar(0),
1255 SkDoubleToScalar(1));
1256
1257 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1258 }
1259 }
1260 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1261}
1262
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001263static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001264 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001265 return kOK_PdfResult;
1266}
1267
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001268static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001269 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1270}
1271
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001272static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001273 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1274}
1275
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001276static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001277 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001278// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001279
1280 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1281
1282 bool doubles = true;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001283 if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001284 doubles = false;
1285 }
1286
1287#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001288 printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001289#endif
1290
1291 for (int i = n - 1; i >= 0 ; i--) {
1292 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001293 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1294// } else {
1295// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001296 }
1297 }
1298
1299 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001300 // TODO(edisonn): do possible field values to enum at parsing time!
1301 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
1302 if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001303 colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2]));
1304 }
1305 return kPartial_PdfResult;
1306}
1307
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001308static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001309 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1310}
1311
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001312static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001313 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1314}
1315
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001316static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001317 //SkPdfString* name;
1318 if (pdfContext->fObjectStack.top()->isName()) {
1319 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001320 pdfContext->fObjectStack.pop();
1321 }
1322
1323 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1324 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1325
1326 return kPartial_PdfResult;
1327}
1328
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001329static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001330 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1331}
1332
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001333static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001334 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1335}
1336
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001337static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001338 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001339 return kNYI_PdfResult;
1340}
1341
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001342static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001343 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1344}
1345
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001346static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001347 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1348}
1349
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001350static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001351 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1352 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1353 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001354
1355 colorOperator->fColorSpace = "DeviceRGB";
1356 colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b));
1357 return kOK_PdfResult;
1358}
1359
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001360static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001361 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1362}
1363
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001364static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001365 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1366}
1367
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001368static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001369 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001370 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1371 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1372 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1373 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001374
1375 colorOperator->fColorSpace = "DeviceCMYK";
1376 // TODO(edisonn): Set color.
1377 return kNYI_PdfResult;
1378}
1379
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001380static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001381 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1382}
1383
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001384static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001385 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1386}
1387
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001388static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001389 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1390 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1391
1392 return kOK_PdfResult;
1393}
1394
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001395static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001396 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1397
1398#ifdef PDF_TRACE
1399 if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) {
1400 printf("CLIP IS RECT\n");
1401 }
1402#endif
1403
1404 // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd.
1405 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1406 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1407
1408 return kPartial_PdfResult;
1409}
1410
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001411static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001412 *looper = new PdfCompatibilitySectionLooper();
1413 return kOK_PdfResult;
1414}
1415
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001416static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001417#ifdef ASSERT_BAD_PDF_OPS
1418 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1419 // have the assert when testing good pdfs.
1420#endif
1421 return kIgnoreError_PdfResult;
1422}
1423
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001424static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001425 *looper = new PdfInlineImageLooper();
1426 return kOK_PdfResult;
1427}
1428
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001429static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001430#ifdef ASSERT_BAD_PDF_OPS
1431 SkASSERT(false); // must be processed in inline image looper, but let's
1432 // have the assert when testing good pdfs.
1433#endif
1434 return kIgnoreError_PdfResult;
1435}
1436
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001437static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001438#ifdef ASSERT_BAD_PDF_OPS
1439 SkASSERT(false); // must be processed in inline image looper, but let's
1440 // have the assert when testing good pdfs.
1441#endif
1442 return kIgnoreError_PdfResult;
1443}
1444
1445//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001446static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001447 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001448 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1449
1450 return kOK_PdfResult;
1451}
1452
1453//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 +00001454static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001455 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001456 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001457
1458 return kNYI_PdfResult;
1459}
1460
1461//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 +00001462static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001463 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001464 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001465
1466 return kNYI_PdfResult;
1467}
1468
1469//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001470static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001471 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001472 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001473
1474 return kNYI_PdfResult;
1475}
1476
1477//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1478//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001479static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001480 pdfContext->fObjectStack.pop();
1481 pdfContext->fObjectStack.pop();
1482
1483 return kNYI_PdfResult;
1484}
1485
1486//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 +00001487static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001488 pdfContext->fObjectStack.pop();
1489
1490 return kNYI_PdfResult;
1491}
1492
1493//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1494//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1495//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001496static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001497 pdfContext->fObjectStack.pop();
1498
1499 return kNYI_PdfResult;
1500}
1501
1502//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1503//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 +00001504static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001505 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001506
1507#ifdef PDF_TRACE
1508 std::string str;
1509#endif
1510
1511 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001512 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001513
1514 if (extGStateDictionary == NULL) {
1515#ifdef PDF_TRACE
1516 printf("ExtGState is NULL!\n");
1517#endif
1518 return kIgnoreError_PdfResult;
1519 }
1520
edisonn@google.com571c70b2013-07-10 17:09:50 +00001521 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001522
edisonn@google.com571c70b2013-07-10 17:09:50 +00001523 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1524 return kIgnoreError_PdfResult;
1525 }
1526 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001527
1528 // TODO(edisonn): now load all those properties in graphic state.
1529 if (gs == NULL) {
1530 return kIgnoreError_PdfResult;
1531 }
1532
1533 if (gs->has_CA()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001534 pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001535 }
1536
1537 if (gs->has_ca()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001538 pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001539 }
1540
1541 if (gs->has_LW()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001542 pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001543 }
1544
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001545 return kNYI_PdfResult;
1546}
1547
1548//charSpace Tc Set the character spacing, Tc
1549//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1550//Initial value: 0.
1551PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001552 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001553 pdfContext->fGraphicsState.fCharSpace = charSpace;
1554
1555 return kOK_PdfResult;
1556}
1557
1558//wordSpace Tw Set the word spacing, T
1559//w
1560//, to wordSpace, which is a number expressed in unscaled
1561//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1562//value: 0.
1563PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001564 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001565 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1566
1567 return kOK_PdfResult;
1568}
1569
1570//scale Tz Set the horizontal scaling, Th
1571//, to (scale ˜ 100). scale is a number specifying the
1572//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001573static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001574 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001575
1576 return kNYI_PdfResult;
1577}
1578
1579//render Tr Set the text rendering mode, T
1580//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001581static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001582 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001583
1584 return kNYI_PdfResult;
1585}
1586//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1587//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001588static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001589 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001590
1591 return kNYI_PdfResult;
1592}
1593
1594//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001595static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001596 pdfContext->fObjectStack.pop();
1597 pdfContext->fObjectStack.pop();
1598
1599 return kNYI_PdfResult;
1600}
1601
1602//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001603static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001604 pdfContext->fObjectStack.pop();
1605 pdfContext->fObjectStack.pop();
1606 pdfContext->fObjectStack.pop();
1607 pdfContext->fObjectStack.pop();
1608 pdfContext->fObjectStack.pop();
1609 pdfContext->fObjectStack.pop();
1610
1611 return kNYI_PdfResult;
1612}
1613
1614//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001615static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001616 pdfContext->fObjectStack.pop();
1617
1618 return kNYI_PdfResult;
1619}
1620
1621//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001622static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001623 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001624
edisonn@google.com571c70b2013-07-10 17:09:50 +00001625 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001626
1627 if (xObject == NULL) {
1628#ifdef PDF_TRACE
1629 printf("XObject is NULL!\n");
1630#endif
1631 return kIgnoreError_PdfResult;
1632 }
1633
edisonn@google.com571c70b2013-07-10 17:09:50 +00001634 SkPdfObject* value = xObject->get(name);
1635 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001636
1637#ifdef PDF_TRACE
1638// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001639// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001640#endif
1641
edisonn@google.com571c70b2013-07-10 17:09:50 +00001642 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001643}
1644
1645//tag MP Designate a marked-content point. tag is a name object indicating the role or
1646//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001647static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001648 pdfContext->fObjectStack.pop();
1649
1650 return kNYI_PdfResult;
1651}
1652
1653//tag properties DP Designate a marked-content point with an associated property list. tag is a
1654//name object indicating the role or significance of the point; properties is
1655//either an inline dictionary containing the property list or a name object
1656//associated with it in the Properties subdictionary of the current resource
1657//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001658static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001659 pdfContext->fObjectStack.pop();
1660 pdfContext->fObjectStack.pop();
1661
1662 return kNYI_PdfResult;
1663}
1664
1665//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1666//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001667static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001668 pdfContext->fObjectStack.pop();
1669
1670 return kNYI_PdfResult;
1671}
1672
1673//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1674//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1675//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 +00001676static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001677 pdfContext->fObjectStack.pop();
1678 pdfContext->fObjectStack.pop();
1679
1680 return kNYI_PdfResult;
1681}
1682
1683//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001684static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001685 return kNYI_PdfResult;
1686}
1687
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001688static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001689 static bool gInitialized = false;
1690 if (gInitialized) {
1691 return;
1692 }
1693
1694 gPdfOps["q"] = PdfOp_q;
1695 gPdfOps["Q"] = PdfOp_Q;
1696 gPdfOps["cm"] = PdfOp_cm;
1697
1698 gPdfOps["TD"] = PdfOp_TD;
1699 gPdfOps["Td"] = PdfOp_Td;
1700 gPdfOps["Tm"] = PdfOp_Tm;
1701 gPdfOps["T*"] = PdfOp_T_star;
1702
1703 gPdfOps["m"] = PdfOp_m;
1704 gPdfOps["l"] = PdfOp_l;
1705 gPdfOps["c"] = PdfOp_c;
1706 gPdfOps["v"] = PdfOp_v;
1707 gPdfOps["y"] = PdfOp_y;
1708 gPdfOps["h"] = PdfOp_h;
1709 gPdfOps["re"] = PdfOp_re;
1710
1711 gPdfOps["S"] = PdfOp_S;
1712 gPdfOps["s"] = PdfOp_s;
1713 gPdfOps["f"] = PdfOp_f;
1714 gPdfOps["F"] = PdfOp_F;
1715 gPdfOps["f*"] = PdfOp_f_star;
1716 gPdfOps["B"] = PdfOp_B;
1717 gPdfOps["B*"] = PdfOp_B_star;
1718 gPdfOps["b"] = PdfOp_b;
1719 gPdfOps["b*"] = PdfOp_b_star;
1720 gPdfOps["n"] = PdfOp_n;
1721
1722 gPdfOps["BT"] = PdfOp_BT;
1723 gPdfOps["ET"] = PdfOp_ET;
1724
1725 gPdfOps["Tj"] = PdfOp_Tj;
1726 gPdfOps["'"] = PdfOp_quote;
1727 gPdfOps["\""] = PdfOp_doublequote;
1728 gPdfOps["TJ"] = PdfOp_TJ;
1729
1730 gPdfOps["CS"] = PdfOp_CS;
1731 gPdfOps["cs"] = PdfOp_cs;
1732 gPdfOps["SC"] = PdfOp_SC;
1733 gPdfOps["SCN"] = PdfOp_SCN;
1734 gPdfOps["sc"] = PdfOp_sc;
1735 gPdfOps["scn"] = PdfOp_scn;
1736 gPdfOps["G"] = PdfOp_G;
1737 gPdfOps["g"] = PdfOp_g;
1738 gPdfOps["RG"] = PdfOp_RG;
1739 gPdfOps["rg"] = PdfOp_rg;
1740 gPdfOps["K"] = PdfOp_K;
1741 gPdfOps["k"] = PdfOp_k;
1742
1743 gPdfOps["W"] = PdfOp_W;
1744 gPdfOps["W*"] = PdfOp_W_star;
1745
1746 gPdfOps["BX"] = PdfOp_BX;
1747 gPdfOps["EX"] = PdfOp_EX;
1748
1749 gPdfOps["BI"] = PdfOp_BI;
1750 gPdfOps["ID"] = PdfOp_ID;
1751 gPdfOps["EI"] = PdfOp_EI;
1752
1753 gPdfOps["w"] = PdfOp_w;
1754 gPdfOps["J"] = PdfOp_J;
1755 gPdfOps["j"] = PdfOp_j;
1756 gPdfOps["M"] = PdfOp_M;
1757 gPdfOps["d"] = PdfOp_d;
1758 gPdfOps["ri"] = PdfOp_ri;
1759 gPdfOps["i"] = PdfOp_i;
1760 gPdfOps["gs"] = PdfOp_gs;
1761
1762 gPdfOps["Tc"] = PdfOp_Tc;
1763 gPdfOps["Tw"] = PdfOp_Tw;
1764 gPdfOps["Tz"] = PdfOp_Tz;
1765 gPdfOps["TL"] = PdfOp_TL;
1766 gPdfOps["Tf"] = PdfOp_Tf;
1767 gPdfOps["Tr"] = PdfOp_Tr;
1768 gPdfOps["Ts"] = PdfOp_Ts;
1769
1770 gPdfOps["d0"] = PdfOp_d0;
1771 gPdfOps["d1"] = PdfOp_d1;
1772
1773 gPdfOps["sh"] = PdfOp_sh;
1774
1775 gPdfOps["Do"] = PdfOp_Do;
1776
1777 gPdfOps["MP"] = PdfOp_MP;
1778 gPdfOps["DP"] = PdfOp_DP;
1779 gPdfOps["BMC"] = PdfOp_BMC;
1780 gPdfOps["BDC"] = PdfOp_BDC;
1781 gPdfOps["EMC"] = PdfOp_EMC;
1782
1783 gInitialized = true;
1784}
1785
1786class InitPdfOps {
1787public:
1788 InitPdfOps() {
1789 initPdfOperatorRenderes();
1790 }
1791};
1792
1793InitPdfOps gInitPdfOps;
1794
1795void reportPdfRenderStats() {
1796 std::map<std::string, int>::iterator iter;
1797
1798 for (int i = 0 ; i < kCount_PdfResult; i++) {
1799 for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) {
1800 printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second);
1801 }
1802 }
1803}
1804
1805PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001806 char keyword[256];
1807
1808 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001809 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001810 strncpy(keyword, token.fKeyword, token.fKeywordLength);
1811 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001812 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com571c70b2013-07-10 17:09:50 +00001813 PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001814 if (pdfOperatorRenderer) {
1815 // caller, main work is done by pdfOperatorRenderer(...)
1816 PdfTokenLooper* childLooper = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001817 gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001818
1819 if (childLooper) {
1820 childLooper->setUp(this);
1821 childLooper->loop();
1822 delete childLooper;
1823 }
1824 } else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001825 gRenderStats[kUnsupported_PdfResult][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001826 }
1827 }
1828 else if (token.fType == kObject_TokenType)
1829 {
1830 fPdfContext->fObjectStack.push( token.fObject );
1831 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001832 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001833 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001834 return kIgnoreError_PdfResult;
1835 }
1836 return kOK_PdfResult;
1837}
1838
1839void PdfMainLooper::loop() {
1840 PdfToken token;
1841 while (readToken(fTokenizer, &token)) {
1842 consumeToken(token);
1843 }
1844}
1845
1846PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
1847 //pdfContext.fInlineImage.fKeyValuePairs[key] = value;
1848 return kNYI_PdfResult;
1849}
1850
1851void PdfInlineImageLooper::loop() {
1852 PdfToken token;
1853 while (readToken(fTokenizer, &token)) {
1854 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1855 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1856 looper->setUp(this);
1857 looper->loop();
1858 } else {
1859 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EI") == 0) {
1860 done();
1861 return;
1862 }
1863
1864 consumeToken(token);
1865 }
1866 }
1867 // TODO(edisonn): report error/warning, EOF without EI.
1868}
1869
1870PdfResult PdfInlineImageLooper::done() {
1871
1872 // TODO(edisonn): long to short names
1873 // TODO(edisonn): set properties in a map
1874 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
1875 // the stream.
1876
1877 SkBitmap bitmap;
1878 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
1879
1880 // TODO(edisonn): matrix use.
1881 // Draw dummy red square, to show the prezence of the inline image.
1882 fCanvas->drawBitmap(bitmap,
1883 SkDoubleToScalar(0),
1884 SkDoubleToScalar(0),
1885 NULL);
1886 return kNYI_PdfResult;
1887}
1888
1889PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
1890 return fParent->consumeToken(token);
1891}
1892
1893void PdfCompatibilitySectionLooper::loop() {
1894 // TODO(edisonn): save stacks position, or create a new stack?
1895 // TODO(edisonn): what happens if we pop out more variables then when we started?
1896 // restore them? fail? We could create a new operands stack for every new BX/EX section,
1897 // pop-ing too much will not affect outside the section.
1898 PdfToken token;
1899 while (readToken(fTokenizer, &token)) {
1900 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1901 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1902 looper->setUp(this);
1903 looper->loop();
1904 delete looper;
1905 } else {
1906 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
1907 fParent->consumeToken(token);
1908 }
1909 }
1910 // TODO(edisonn): restore stack.
1911}
1912
1913// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
1914// TODO(edisonn): Add API for Forms viewing and editing
1915// e.g. SkBitmap getPage(int page);
1916// int formsCount();
1917// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
1918// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
1919// if we load the first page, and we zoom to fit to screen horizontally, then load only those
1920// resources needed, so the preview is fast.
1921// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
1922// references automatically.
1923
edisonn@google.com222382b2013-07-10 22:33:10 +00001924PdfContext* gPdfContext = NULL;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001925
edisonn@google.com444e25a2013-07-11 15:20:50 +00001926bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const {
edisonn@google.com222382b2013-07-10 22:33:10 +00001927 if (!fPdfDoc) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001928 return false;
1929 }
1930
edisonn@google.com222382b2013-07-10 22:33:10 +00001931 if (page < 0 || page >= pages()) {
1932 return false;
1933 }
1934
1935 SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page);
1936
1937 PdfContext pdfContext(fPdfDoc);
1938 pdfContext.fOriginalMatrix = SkMatrix::I();
1939 pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
1940
1941 gPdfContext = &pdfContext;
1942
1943 // TODO(edisonn): get matrix stuff right.
edisonn@google.com222382b2013-07-10 22:33:10 +00001944 SkScalar z = SkIntToScalar(0);
edisonn@google.com444e25a2013-07-11 15:20:50 +00001945 SkScalar w = dst.width();
1946 SkScalar h = dst.height();
edisonn@google.com222382b2013-07-10 22:33:10 +00001947
edisonn@google.com444e25a2013-07-11 15:20:50 +00001948 SkScalar wp = fPdfDoc->MediaBox(page).width();
1949 SkScalar hp = fPdfDoc->MediaBox(page).height();
1950
1951 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 +00001952// SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
1953
1954 // TODO(edisonn): add flag for this app to create sourunding buffer zone
1955 // TODO(edisonn): add flagg for no clipping.
1956 // Use larger image to make sure we do not draw anything outside of page
1957 // could be used in tests.
1958
1959#ifdef PDF_DEBUG_3X
1960 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)};
1961#else
1962 SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)};
1963#endif
1964 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)};
1965 //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)};
1966
1967 //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)};
1968 //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)};
1969
1970 //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)};
1971 //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)};
1972
1973 SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4));
1974 SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix");
1975
1976
1977 pdfContext.fGraphicsState.fMatrix = pdfContext.fOriginalMatrix;
1978 pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fMatrix;
1979 pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fMatrix;
1980
edisonn@google.com222382b2013-07-10 22:33:10 +00001981#ifndef PDF_DEBUG_NO_PAGE_CLIPING
edisonn@google.com444e25a2013-07-11 15:20:50 +00001982 canvas->clipRect(dst, SkRegion::kIntersect_Op, true);
edisonn@google.com222382b2013-07-10 22:33:10 +00001983#endif
1984
edisonn@google.com444e25a2013-07-11 15:20:50 +00001985 canvas->setMatrix(pdfContext.fOriginalMatrix);
1986
edisonn@google.com222382b2013-07-10 22:33:10 +00001987// erase with red before?
1988// SkPaint paint;
1989// paint.setColor(SK_ColorRED);
1990// canvas->drawRect(rect, paint);
1991
1992 PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
1993 looper.loop();
1994
1995 delete tokenizer;
1996
1997 canvas->flush();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001998 return true;
1999}
edisonn@google.com222382b2013-07-10 22:33:10 +00002000
2001bool SkPdfRenderer::load(const SkString inputFileName) {
2002 unload();
2003
2004 // TODO(edisonn): create static function that could return NULL if there are errors
2005 fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str());
2006
2007 return fPdfDoc != NULL;
2008}
2009
2010int SkPdfRenderer::pages() const {
2011 return fPdfDoc != NULL ? fPdfDoc->pages() : 0;
2012}
2013
2014void SkPdfRenderer::unload() {
2015 delete fPdfDoc;
2016 fPdfDoc = NULL;
2017}
2018
2019SkRect SkPdfRenderer::MediaBox(int page) const {
2020 SkASSERT(fPdfDoc);
2021 return fPdfDoc->MediaBox(page);
2022}
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002023
edisonn@google.com7b328fd2013-07-11 12:53:06 +00002024size_t SkPdfRenderer::bytesUsed() const {
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002025 return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
2026}
2027