blob: 805b877418ba87e98290c4a3670f71a07560b7c3 [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"
19#include "picture_utils.h"
20
21#include <iostream>
22#include <cstdio>
23#include <stack>
edisonn@google.com571c70b2013-07-10 17:09:50 +000024#include <set>
edisonn@google.com131d4ee2013-06-26 17:48:12 +000025
26__SK_FORCE_IMAGE_DECODER_LINKING;
27
28// TODO(edisonn): tool, show what objects were read at least, show the ones not even read
29// keep for each object pos in file
30// plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y
31
32// TODO(edisonn): security - validate all the user input, all pdf!
33
edisonn@google.com6e49c342013-06-27 20:03:43 +000034// TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font
35// this way, we look more at other features and layout in diffs
edisonn@google.com131d4ee2013-06-26 17:48:12 +000036
edisonn@google.com3aac1f92013-07-02 22:42:53 +000037// TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically
38/*
39#ifdef PDF_TRACE
40 std::string str;
edisonn@google.com571c70b2013-07-10 17:09:50 +000041 pdfContext->fGraphicsState.fResources->native()->ToString(str);
edisonn@google.com3aac1f92013-07-02 22:42:53 +000042 printf("Print Tf Resources: %s\n", str.c_str());
43#endif
44 */
45
edisonn@google.com131d4ee2013-06-26 17:48:12 +000046#include "SkPdfHeaders_autogen.h"
edisonn@google.com3aac1f92013-07-02 22:42:53 +000047#include "SkPdfMapper_autogen.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +000048#include "SkPdfParser.h"
49
50#include "SkPdfBasics.h"
51#include "SkPdfUtils.h"
52
53#include "SkPdfFont.h"
54
edisonn@google.com131d4ee2013-06-26 17:48:12 +000055/*
56 * TODO(edisonn):
57 * - all font types and all ppdf font features
58 * - word spacing
59 * - load font for baidu.pdf
60 * - load font for youtube.pdf
61 * - parser for pdf from the definition already available in pdfspec_autogen.py
62 * - all docs from ~/work
edisonn@google.com571c70b2013-07-10 17:09:50 +000063 * - 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 +000064 * - load gs/ especially smask and already known prop (skp) ... in progress
65 * - wrapper on classes for customizations? e.g.
66 * SkPdfPageObjectVanila - has only the basic loaders/getters
67 * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here
68 * need to find a nice object model for all this with constructors and factories
69 * - deal with inheritable automatically ?
70 * - deal with specific type in spec directly, add all dictionary types to known types
71*/
72
73using namespace std;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000074
75// Utilities
76static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
77 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
78
79 bitmap->allocPixels();
80 bitmap->eraseColor(color);
81}
82
83// TODO(edisonn): synonyms? DeviceRGB and RGB ...
edisonn@google.coma3356fc2013-07-10 18:20:06 +000084static int GetColorSpaceComponents(const std::string& colorSpace) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +000085 if (colorSpace == "DeviceCMYK") {
86 return 4;
87 } else if (colorSpace == "DeviceGray" ||
88 colorSpace == "CalGray" ||
89 colorSpace == "Indexed") {
90 return 1;
91 } else if (colorSpace == "DeviceRGB" ||
92 colorSpace == "CalRGB" ||
93 colorSpace == "Lab") {
94 return 3;
95 } else {
96 return 0;
97 }
98}
99
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000100SkMatrix SkMatrixFromPdfMatrix(double array[6]) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000101 SkMatrix matrix;
102 matrix.setAll(SkDoubleToScalar(array[0]),
103 SkDoubleToScalar(array[2]),
104 SkDoubleToScalar(array[4]),
105 SkDoubleToScalar(array[1]),
106 SkDoubleToScalar(array[3]),
107 SkDoubleToScalar(array[5]),
108 SkDoubleToScalar(0),
109 SkDoubleToScalar(0),
110 SkDoubleToScalar(1));
111
112 return matrix;
113}
114
115SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) {
116 double array[6];
117
118 // TODO(edisonn): security issue, ret if size() != 6
119 for (int i = 0; i < 6; i++) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000120 const SkPdfObject* elem = pdfArray->operator [](i);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000121 if (elem == NULL || !elem->isNumber()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000122 return SkMatrix::I(); // TODO(edisonn): report issue
123 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000124 array[i] = elem->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000125 }
126
127 return SkMatrixFromPdfMatrix(array);
128}
129
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000130SkBitmap* gDumpBitmap = NULL;
131SkCanvas* gDumpCanvas = NULL;
132char gLastKeyword[100] = "";
133int gLastOpKeyword = -1;
134char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,";
135int gReadOp = 0;
136
137
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000138#ifdef PDF_TRACE_DIFF_IN_PNG
139static bool hasVisualEffect(const char* pdfOp) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000140 return true;
141 if (*pdfOp == '\0') return false;
142
143 char markedPdfOp[100] = ",";
144 strcat(markedPdfOp, pdfOp);
145 strcat(markedPdfOp, ",");
146
147 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL);
148}
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000149#endif // PDF_TRACE_DIFF_IN_PNG
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000150
151// TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation.
edisonn@google.com571c70b2013-07-10 17:09:50 +0000152static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000153 bool ret = fTokenizer->readToken(token);
154
155 gReadOp++;
156
157#ifdef PDF_TRACE_DIFF_IN_PNG
158 // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save
159 // the numbar and name of last operation, so the file name will reflect op that changed.
160 if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits.
161 gDumpCanvas->flush();
162
163 SkBitmap bitmap;
164 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height());
165
166 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize());
167
168 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
169 SkCanvas canvas(device);
170
171 // draw context stuff here
172 SkPaint blueBorder;
173 blueBorder.setColor(SK_ColorBLUE);
174 blueBorder.setStyle(SkPaint::kStroke_Style);
175 blueBorder.setTextSize(SkDoubleToScalar(20));
176
177 SkString str;
178
179 const SkClipStack* clipStack = gDumpCanvas->getClipStack();
180 if (clipStack) {
181 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
182 const SkClipStack::Element* elem;
183 double y = 0;
184 int total = 0;
185 while (elem = iter.next()) {
186 total++;
187 y += 30;
188
189 switch (elem->getType()) {
190 case SkClipStack::Element::kRect_Type:
191 canvas.drawRect(elem->getRect(), blueBorder);
192 canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
193 break;
194 case SkClipStack::Element::kPath_Type:
195 canvas.drawPath(elem->getPath(), blueBorder);
196 canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
197 break;
198 case SkClipStack::Element::kEmpty_Type:
199 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
200 break;
201 default:
202 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
203 break;
204 }
205 }
206
207 y += 30;
208 str.printf("Number of clips in stack: %i", total);
209 canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder);
210 }
211
212 const SkRegion& clipRegion = gDumpCanvas->getTotalClip();
213 SkPath clipPath;
214 if (clipRegion.getBoundaryPath(&clipPath)) {
215 SkPaint redBorder;
216 redBorder.setColor(SK_ColorRED);
217 redBorder.setStyle(SkPaint::kStroke_Style);
218 canvas.drawPath(clipPath, redBorder);
219 }
220
221 canvas.flush();
222
223 SkString out;
224
225 // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack,
226 // ... and other properties, to be able to debug th code easily
227
228 out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword);
229 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
230 }
231
232 if (token->fType == kKeyword_TokenType) {
233 strcpy(gLastKeyword, token->fKeyword);
234 gLastOpKeyword = gReadOp;
235 } else {
236 strcpy(gLastKeyword, "");
237 }
238#endif
239
240 return ret;
241}
242
243
244
245typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**);
246
247map<std::string, PdfOperatorRenderer> gPdfOps;
248
249map<std::string, int> gRenderStats[kCount_PdfResult];
250
edisonn@google.com571c70b2013-07-10 17:09:50 +0000251const char* gRenderStatsNames[kCount_PdfResult] = {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000252 "Success",
253 "Partially implemented",
254 "Not yet implemented",
255 "Ignore Error",
256 "Error",
257 "Unsupported/Unknown"
258};
259
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000260static PdfResult DrawText(PdfContext* pdfContext,
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000261 const SkPdfObject* _str,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000262 SkCanvas* canvas)
263{
264
265 SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont;
266 if (skfont == NULL) {
267 skfont = SkPdfFont::Default();
268 }
269
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000270
edisonn@google.com571c70b2013-07-10 17:09:50 +0000271 if (_str == NULL || !_str->isAnyString()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000272 // TODO(edisonn): report warning
273 return kIgnoreError_PdfResult;
274 }
edisonn@google.com571c70b2013-07-10 17:09:50 +0000275 const SkPdfString* str = (const SkPdfString*)_str;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000276
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000277 SkUnencodedText binary(str);
278
279 SkDecodedText decoded;
280
281 if (skfont->encoding() == NULL) {
282 // TODO(edisonn): report warning
283 return kNYI_PdfResult;
284 }
285
286 skfont->encoding()->decodeText(binary, &decoded);
287
288 SkPaint paint;
289 // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0?
290 // Or maybe just not call setTextSize at all?
291 if (pdfContext->fGraphicsState.fCurFontSize != 0) {
292 paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize));
293 }
294
295// if (fCurFont && fCurFont->GetFontScale() != 0) {
296// paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0));
297// }
298
299 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
300
301 canvas->save();
302
303#if 1
304 SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
305
306 SkPoint point1;
307 pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
308
309 SkMatrix mirror;
310 mirror.setTranslate(0, -point1.y());
311 // TODO(edisonn): fix rotated text, and skewed too
312 mirror.postScale(SK_Scalar1, -SK_Scalar1);
313 // TODO(edisonn): post rotate, skew
314 mirror.postTranslate(0, point1.y());
315
316 matrix.postConcat(mirror);
317
318 canvas->setMatrix(matrix);
319
320 SkTraceMatrix(matrix, "mirrored");
321#endif
322
edisonn@google.com6e49c342013-06-27 20:03:43 +0000323 skfont->drawText(decoded, &paint, pdfContext, canvas);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000324 canvas->restore();
325
326 return kPartial_PdfResult;
327}
328
329// TODO(edisonn): create header files with declarations!
330PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
331PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
332PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
333PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper);
334
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000335// TODO(edisonn): perf!!!
336
337static SkColorTable* getGrayColortable() {
338 static SkColorTable* grayColortable = NULL;
339 if (grayColortable == NULL) {
340 SkPMColor* colors = new SkPMColor[256];
341 for (int i = 0 ; i < 256; i++) {
342 colors[i] = SkPreMultiplyARGB(255, i, i, i);
343 }
344 grayColortable = new SkColorTable(colors, 256);
345 }
346 return grayColortable;
347}
348
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000349static SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000350 int width, int height, int bytesPerLine,
351 int bpc, const std::string& colorSpace,
352 bool transparencyMask) {
353 SkBitmap bitmap;
354
edisonn@google.com571c70b2013-07-10 17:09:50 +0000355 //int components = GetColorSpaceComponents(colorSpace);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000356//#define MAX_COMPONENTS 10
357
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000358 // TODO(edisonn): assume start of lines are aligned at 32 bits?
359 // Is there a faster way to load the uncompressed stream into a bitmap?
360
361 // minimal support for now
362 if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) {
363 SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor));
364
365 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000366 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000367 for (int w = 0 ; w < width; w++) {
368 uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w],
369 uncompressedStream[3 * w + 1],
370 uncompressedStream[3 * w + 2]);
371 i++;
372 }
373 uncompressedStream += bytesPerLine;
374 }
375
376 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
377 bitmap.setPixels(uncompressedStreamArgb);
378 }
379 else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) {
380 unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height);
381
382 for (int h = 0 ; h < height; h++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000383 long i = width * (h);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000384 for (int w = 0 ; w < width; w++) {
385 uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] :
386 uncompressedStream[w];
387 i++;
388 }
389 uncompressedStream += bytesPerLine;
390 }
391
392 bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config,
393 width, height);
394 bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable());
395 }
396
397 // TODO(edisonn): Report Warning, NYI, or error
398 return bitmap;
399}
400
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000401// utils
402
403// TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject
404// TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config
405// TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest
406// skia format, through a table
407
408// this functions returns the image, it does not look at the smask.
409
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000410static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000411 if (image == NULL || !image->hasStream()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000412 // TODO(edisonn): report warning to be used in testing.
413 return SkBitmap();
414 }
415
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000416 int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc);
417 int64_t width = image->Width(pdfContext->fPdfDoc);
418 int64_t height = image->Height(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000419 std::string colorSpace = "DeviceRGB";
420
421 // TODO(edisonn): color space can be an array too!
edisonn@google.com571c70b2013-07-10 17:09:50 +0000422 if (image->isColorSpaceAName(pdfContext->fPdfDoc)) {
423 colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000424 }
425
426/*
427 bool imageMask = image->imageMask();
428
429 if (imageMask) {
430 if (bpc != 0 && bpc != 1) {
431 // TODO(edisonn): report warning to be used in testing.
432 return SkBitmap();
433 }
434 bpc = 1;
435 }
436*/
437
edisonn@google.com571c70b2013-07-10 17:09:50 +0000438 unsigned char* uncompressedStream = NULL;
439 size_t uncompressedStreamLength = 0;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000440
edisonn@google.com571c70b2013-07-10 17:09:50 +0000441 SkPdfStream* stream = (SkPdfStream*)image;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000442
edisonn@google.com571c70b2013-07-10 17:09:50 +0000443 if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) ||
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000444 uncompressedStream == NULL || uncompressedStreamLength == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000445 // TODO(edisonn): report warning to be used in testing.
446 return SkBitmap();
447 }
448
edisonn@google.com571c70b2013-07-10 17:09:50 +0000449 SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream;
450
451 if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) &&
452 streamDict->getFilterAsName(NULL) == "DCTDecode") ||
453 (streamDict->isFilterAArray(NULL) &&
454 streamDict->getFilterAsArray(NULL)->size() > 0 &&
455 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() &&
456 streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) {
457 SkBitmap bitmap;
458 SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap);
459 return bitmap;
460 }
461
462
463
464 // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ...
465// PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc,
466// obj.GetDictionary().GetKey(PdfName("Filter")));
467// if (value && value->IsArray() && value->GetArray().GetSize() == 1) {
468// value = resolveReferenceObject(pdfContext->fPdfDoc,
469// &value->GetArray()[0]);
470// }
471// if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") {
472// SkStream stream = SkStream::
473// SkImageDecoder::Factory()
474// }
475
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000476 int bytesPerLine = uncompressedStreamLength / height;
477#ifdef PDF_TRACE
478 if (uncompressedStreamLength % height != 0) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000479 printf("Warning uncompressedStreamLength modulo height != 0 !!!\n");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000480 }
481#endif
482
483 SkBitmap bitmap = transferImageStreamToBitmap(
484 (unsigned char*)uncompressedStream, uncompressedStreamLength,
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000485 (int)width, (int)height, bytesPerLine,
486 (int)bpc, colorSpace,
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000487 transparencyMask);
488
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000489 return bitmap;
490}
491
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000492static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000493 SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000494
495 if (sMask) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000496 return getImageFromObject(pdfContext, sMask, true);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000497 }
498
499 // TODO(edisonn): implement GS SMask. Default to empty right now.
500 return pdfContext->fGraphicsState.fSMask;
501}
502
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000503static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000504 if (skpdfimage == NULL) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000505 return kIgnoreError_PdfResult;
506 }
507
508 SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false);
509 SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage);
510
511 canvas->save();
512 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
edisonn@google.com571c70b2013-07-10 17:09:50 +0000513
514#if 1
515 SkScalar z = SkIntToScalar(0);
516 SkScalar one = SkIntToScalar(1);
517
518 SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)};
519 SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)};
520 SkMatrix flip;
521 SkAssertResult(flip.setPolyToPoly(from, to, 4));
522 SkMatrix solveImageFlip = pdfContext->fGraphicsState.fMatrix;
523 solveImageFlip.preConcat(flip);
524 canvas->setMatrix(solveImageFlip);
525#endif
526
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000527 SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0));
528
529 if (sMask.empty()) {
530 canvas->drawBitmapRect(image, dst, NULL);
531 } else {
532 canvas->saveLayer(&dst, NULL);
533 canvas->drawBitmapRect(image, dst, NULL);
534 SkPaint xfer;
535 pdfContext->fGraphicsState.applyGraphicsState(&xfer, false);
536 xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode
537 canvas->drawBitmapRect(sMask, dst, &xfer);
538 canvas->restore();
539 }
540
541 canvas->restore();
542
543 return kPartial_PdfResult;
544}
545
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000546
547
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000548
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000549static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000550 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000551 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000552 }
553
554 PdfOp_q(pdfContext, canvas, NULL);
555 canvas->save();
556
557
edisonn@google.com571c70b2013-07-10 17:09:50 +0000558 if (skobj->Resources(pdfContext->fPdfDoc)) {
559 pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000560 }
561
562 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix");
563
edisonn@google.com571c70b2013-07-10 17:09:50 +0000564 if (skobj->has_Matrix()) {
565 pdfContext->fGraphicsState.fMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000566 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
567 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
568 // TODO(edisonn) reset matrixTm and matricTlm also?
569 }
570
571 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
572
573 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
574
edisonn@google.com571c70b2013-07-10 17:09:50 +0000575 if (skobj->has_BBox()) {
576 canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000577 }
578
579 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
580 // For this PdfContentsTokenizer needs to be extended.
581
edisonn@google.com571c70b2013-07-10 17:09:50 +0000582 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000583
edisonn@google.com571c70b2013-07-10 17:09:50 +0000584 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000585 if (tokenizer != NULL) {
586 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
587 looper.loop();
588 delete tokenizer;
589 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000590
591 // TODO(edisonn): should we restore the variable stack at the same state?
592 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
593 canvas->restore();
594 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000595 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000596}
597
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000598//static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
599// return kNYI_PdfResult;
600//}
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000601
edisonn@google.com571c70b2013-07-10 17:09:50 +0000602PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) {
603 if (!skobj || !skobj->hasStream()) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000604 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000605 }
606
607 PdfOp_q(pdfContext, canvas, NULL);
608 canvas->save();
609
610 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
611 pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
612
613 pdfContext->fGraphicsState.fMatrix = pdfContext->fGraphicsState.fMatrixTm;
614 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
615
616 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix");
617
618 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
619
620 SkRect rm = bBox;
621 pdfContext->fGraphicsState.fMatrix.mapRect(&rm);
622
623 SkTraceRect(rm, "bbox mapped");
624
625 canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings.
626
627 // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go.
628 // For this PdfContentsTokenizer needs to be extended.
629
edisonn@google.com571c70b2013-07-10 17:09:50 +0000630 SkPdfStream* stream = (SkPdfStream*)skobj;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000631
edisonn@google.com571c70b2013-07-10 17:09:50 +0000632 SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000633 if (tokenizer != NULL) {
634 PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
635 looper.loop();
636 delete tokenizer;
637 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000638
639 // TODO(edisonn): should we restore the variable stack at the same state?
640 // There could be operands left, that could be consumed by a parent tokenizer when we pop.
641 canvas->restore();
642 PdfOp_Q(pdfContext, canvas, NULL);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000643
644 return kPartial_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000645}
646
647
edisonn@google.com571c70b2013-07-10 17:09:50 +0000648// TODO(edisonn): make sure the pointer is unique
649std::set<const SkPdfObject*> gInRendering;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000650
651class CheckRecursiveRendering {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000652 const SkPdfObject* fUniqueData;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000653public:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000654 CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) {
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000655 gInRendering.insert(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000656 }
657
658 ~CheckRecursiveRendering() {
659 //SkASSERT(fObj.fInRendering);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000660 gInRendering.erase(fUniqueData);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000661 }
662
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000663 static bool IsInRendering(const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000664 return gInRendering.find(obj) != gInRendering.end();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000665 }
666};
667
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000668static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000669 if (CheckRecursiveRendering::IsInRendering(obj)) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000670 // Oops, corrupt PDF!
671 return kIgnoreError_PdfResult;
672 }
673
edisonn@google.com571c70b2013-07-10 17:09:50 +0000674 CheckRecursiveRendering checkRecursion(obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000675
edisonn@google.com571c70b2013-07-10 17:09:50 +0000676 switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj))
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000677 {
678 case kImageDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000679 return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000680 case kType1FormDictionary_SkPdfObjectType:
edisonn@google.com571c70b2013-07-10 17:09:50 +0000681 return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000682 //case kObjectDictionaryXObjectPS_SkPdfObjectType:
683 //return doXObject_PS(skxobj.asPS());
edisonn@google.com571c70b2013-07-10 17:09:50 +0000684 default:
685 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000686 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000687}
688
689PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
690 pdfContext->fStateStack.push(pdfContext->fGraphicsState);
691 canvas->save();
692 return kOK_PdfResult;
693}
694
695PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
696 pdfContext->fGraphicsState = pdfContext->fStateStack.top();
697 pdfContext->fStateStack.pop();
698 canvas->restore();
699 return kOK_PdfResult;
700}
701
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000702static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000703 double array[6];
704 for (int i = 0 ; i < 6 ; i++) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000705 array[5 - i] = pdfContext->fObjectStack.top()->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000706 pdfContext->fObjectStack.pop();
707 }
708
709 // a b
710 // c d
711 // e f
712
713 // 0 1
714 // 2 3
715 // 4 5
716
717 // sx ky
718 // kx sy
719 // tx ty
720 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
721
722 pdfContext->fGraphicsState.fMatrix.preConcat(matrix);
723
724#ifdef PDF_TRACE
725 printf("cm ");
726 for (int i = 0 ; i < 6 ; i++) {
727 printf("%f ", array[i]);
728 }
729 printf("\n");
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000730 SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "cm");
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000731#endif
732
733 return kOK_PdfResult;
734}
735
736//leading TL Set the text leading, Tl
737//, to leading, which is a number expressed in unscaled text
738//space units. Text leading is used only by the T*, ', and " operators. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000739static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000740 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000741
742 pdfContext->fGraphicsState.fTextLeading = ty;
743
744 return kOK_PdfResult;
745}
746
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000747static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000748 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
749 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000750
751 double array[6] = {1, 0, 0, 1, tx, ty};
752 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
753
754 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
755 pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix);
756
757 return kPartial_PdfResult;
758}
759
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000760static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000761 double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
762 double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000763
edisonn@google.com571c70b2013-07-10 17:09:50 +0000764 // TODO(edisonn): Create factory methods or constructors so native is hidden
765 SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000766 pdfContext->fObjectStack.push(_ty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000767
768 PdfOp_TL(pdfContext, canvas, looper);
769
edisonn@google.com571c70b2013-07-10 17:09:50 +0000770 SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000771 pdfContext->fObjectStack.push(vtx);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000772
edisonn@google.com571c70b2013-07-10 17:09:50 +0000773 SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty);
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000774 pdfContext->fObjectStack.push(vty);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000775
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000776 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
777
778 // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster
779 // and the cleanup can happen while the user looks at the image
780 delete _ty;
781 delete vtx;
782 delete vty;
783
784 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000785}
786
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000787static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000788 double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
789 double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
790 double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
791 double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
792 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
793 double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000794
795 double array[6];
796 array[0] = a;
797 array[1] = b;
798 array[2] = c;
799 array[3] = d;
800 array[4] = e;
801 array[5] = f;
802
803 SkMatrix matrix = SkMatrixFromPdfMatrix(array);
804 matrix.postConcat(pdfContext->fGraphicsState.fMatrix);
805
806 // TODO(edisonn): Text positioning.
807 pdfContext->fGraphicsState.fMatrixTm = matrix;
808 pdfContext->fGraphicsState.fMatrixTlm = matrix;;
809
810 return kPartial_PdfResult;
811}
812
813//— T* Move to the start of the next line. This operator has the same effect as the code
814//0 Tl Td
815//where Tl is the current leading parameter in the text state
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000816static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +0000817 SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0);
818 SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading);
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000819
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000820 pdfContext->fObjectStack.push(zero);
821 pdfContext->fObjectStack.push(tl);
822
823 PdfResult ret = PdfOp_Td(pdfContext, canvas, looper);
824
825 delete zero; // TODO(edisonn): do not alocate and delete constants!
826 delete tl;
827
828 return ret;
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000829}
830
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000831static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000832 if (pdfContext->fGraphicsState.fPathClosed) {
833 pdfContext->fGraphicsState.fPath.reset();
834 pdfContext->fGraphicsState.fPathClosed = false;
835 }
836
edisonn@google.com571c70b2013-07-10 17:09:50 +0000837 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
838 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000839
840 pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
841 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
842
843 return kOK_PdfResult;
844}
845
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000846static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000847 if (pdfContext->fGraphicsState.fPathClosed) {
848 pdfContext->fGraphicsState.fPath.reset();
849 pdfContext->fGraphicsState.fPathClosed = false;
850 }
851
edisonn@google.com571c70b2013-07-10 17:09:50 +0000852 pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
853 pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000854
855 pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX),
856 SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY));
857
858 return kOK_PdfResult;
859}
860
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000861static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000862 if (pdfContext->fGraphicsState.fPathClosed) {
863 pdfContext->fGraphicsState.fPath.reset();
864 pdfContext->fGraphicsState.fPathClosed = false;
865 }
866
edisonn@google.com571c70b2013-07-10 17:09:50 +0000867 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
868 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
869 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
870 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
871 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
872 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000873
874 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
875 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
876 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
877
878 pdfContext->fGraphicsState.fCurPosX = x3;
879 pdfContext->fGraphicsState.fCurPosY = y3;
880
881 return kOK_PdfResult;
882}
883
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000884static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000885 if (pdfContext->fGraphicsState.fPathClosed) {
886 pdfContext->fGraphicsState.fPath.reset();
887 pdfContext->fGraphicsState.fPathClosed = false;
888 }
889
edisonn@google.com571c70b2013-07-10 17:09:50 +0000890 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
891 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
892 double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
893 double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000894 double y1 = pdfContext->fGraphicsState.fCurPosY;
895 double x1 = pdfContext->fGraphicsState.fCurPosX;
896
897 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
898 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
899 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
900
901 pdfContext->fGraphicsState.fCurPosX = x3;
902 pdfContext->fGraphicsState.fCurPosY = y3;
903
904 return kOK_PdfResult;
905}
906
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000907static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000908 if (pdfContext->fGraphicsState.fPathClosed) {
909 pdfContext->fGraphicsState.fPath.reset();
910 pdfContext->fGraphicsState.fPathClosed = false;
911 }
912
edisonn@google.com571c70b2013-07-10 17:09:50 +0000913 double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
914 double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000915 double y2 = pdfContext->fGraphicsState.fCurPosY;
916 double x2 = pdfContext->fGraphicsState.fCurPosX;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000917 double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
918 double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000919
920 pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1),
921 SkDoubleToScalar(x2), SkDoubleToScalar(y2),
922 SkDoubleToScalar(x3), SkDoubleToScalar(y3));
923
924 pdfContext->fGraphicsState.fCurPosX = x3;
925 pdfContext->fGraphicsState.fCurPosY = y3;
926
927 return kOK_PdfResult;
928}
929
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000930static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000931 if (pdfContext->fGraphicsState.fPathClosed) {
932 pdfContext->fGraphicsState.fPath.reset();
933 pdfContext->fGraphicsState.fPathClosed = false;
934 }
935
edisonn@google.com571c70b2013-07-10 17:09:50 +0000936 double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
937 double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
938 double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
939 double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000940
941 pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y),
942 SkDoubleToScalar(x + width), SkDoubleToScalar(y + height));
943
944 pdfContext->fGraphicsState.fCurPosX = x;
945 pdfContext->fGraphicsState.fCurPosY = y + height;
946
947 return kOK_PdfResult;
948}
949
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000950static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000951 pdfContext->fGraphicsState.fPath.close();
952 return kOK_PdfResult;
953}
954
edisonn@google.coma3356fc2013-07-10 18:20:06 +0000955static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +0000956 SkPath path = pdfContext->fGraphicsState.fPath;
957
958 if (close) {
959 path.close();
960 }
961
962 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
963
964 SkPaint paint;
965
966 SkPoint line[2];
967 if (fill && !stroke && path.isLine(line)) {
968 paint.setStyle(SkPaint::kStroke_Style);
969
970 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
971 paint.setStrokeWidth(SkDoubleToScalar(0));
972
973 canvas->drawPath(path, paint);
974 } else {
975 if (fill) {
976 paint.setStyle(SkPaint::kFill_Style);
977 if (evenOdd) {
978 path.setFillType(SkPath::kEvenOdd_FillType);
979 }
980
981 pdfContext->fGraphicsState.applyGraphicsState(&paint, false);
982
983 canvas->drawPath(path, paint);
984 }
985
986 if (stroke) {
987 paint.setStyle(SkPaint::kStroke_Style);
988
989 pdfContext->fGraphicsState.applyGraphicsState(&paint, true);
990
991 path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke
992 canvas->drawPath(path, paint);
993 }
994 }
995
996 pdfContext->fGraphicsState.fPath.reset();
997 // todo zoom ... other stuff ?
998
999 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1000#ifndef PDF_DEBUG_NO_CLIPING
1001 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1002#endif
1003 }
1004
1005 //pdfContext->fGraphicsState.fClipPath.reset();
1006 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1007
1008 return kPartial_PdfResult;
1009
1010}
1011
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001012static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001013 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false);
1014}
1015
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001016static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001017 return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false);
1018}
1019
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001020static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001021 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1022}
1023
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001024static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001025 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false);
1026}
1027
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001028static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001029 return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true);
1030}
1031
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001032static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001033 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false);
1034}
1035
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001036static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001037 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true);
1038}
1039
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001040static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001041 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false);
1042}
1043
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001044static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001045 return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true);
1046}
1047
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001048static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001049 canvas->setMatrix(pdfContext->fGraphicsState.fMatrix);
1050 if (pdfContext->fGraphicsState.fHasClipPathToApply) {
1051#ifndef PDF_DEBUG_NO_CLIPING
1052 canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true);
1053#endif
1054 }
1055
1056 //pdfContext->fGraphicsState.fClipPath.reset();
1057 pdfContext->fGraphicsState.fHasClipPathToApply = false;
1058
1059 pdfContext->fGraphicsState.fPathClosed = true;
1060
1061 return kOK_PdfResult;
1062}
1063
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001064static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001065 pdfContext->fGraphicsState.fTextBlock = true;
1066 pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix;
1067 pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix;
1068
1069 return kPartial_PdfResult;
1070}
1071
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001072static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001073 if (!pdfContext->fGraphicsState.fTextBlock) {
1074 return kIgnoreError_PdfResult;
1075 }
1076 // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack?
1077 return kPartial_PdfResult;
1078}
1079
1080//font size Tf Set the text font, Tf
1081//, to font and the text font size, Tfs, to size. font is the name of a
1082//font resource in the Fontsubdictionary of the current resource dictionary; size is
1083//a number representing a scale factor. There is no initial value for either font or
1084//size; they must be specified explicitly using Tf before any text is shown.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001085static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001086 pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1087 const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001088
1089#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001090 printf("font name: %s\n", fontName);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001091#endif
1092
edisonn@google.com571c70b2013-07-10 17:09:50 +00001093 if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) {
1094 SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName);
1095 objFont = pdfContext->fPdfDoc->resolveReference(objFont);
1096 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) {
1097 // TODO(edisonn): try to recover and draw it any way?
1098 return kIgnoreError_PdfResult;
1099 }
1100 SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont;
1101
1102 SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd);
1103
1104 if (skfont) {
1105 pdfContext->fGraphicsState.fSkFont = skfont;
1106 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001107 }
edisonn@google.com571c70b2013-07-10 17:09:50 +00001108 return kIgnoreError_PdfResult;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001109}
1110
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001111static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001112 if (!pdfContext->fGraphicsState.fTextBlock) {
1113 // TODO(edisonn): try to recover and draw it any way?
1114 return kIgnoreError_PdfResult;
1115 }
1116
1117 PdfResult ret = DrawText(pdfContext,
1118 pdfContext->fObjectStack.top(),
1119 canvas);
1120 pdfContext->fObjectStack.pop();
1121
1122 return ret;
1123}
1124
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001125static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001126 if (!pdfContext->fGraphicsState.fTextBlock) {
1127 // TODO(edisonn): try to recover and draw it any way?
1128 return kIgnoreError_PdfResult;
1129 }
1130
1131 PdfOp_T_star(pdfContext, canvas, looper);
1132 // Do not pop, and push, just transfer the param to Tj
1133 return PdfOp_Tj(pdfContext, canvas, looper);
1134}
1135
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001136static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001137 if (!pdfContext->fGraphicsState.fTextBlock) {
1138 // TODO(edisonn): try to recover and draw it any way?
1139 return kIgnoreError_PdfResult;
1140 }
1141
1142 SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1143 SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1144 SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop();
1145
1146 pdfContext->fObjectStack.push(aw);
1147 PdfOp_Tw(pdfContext, canvas, looper);
1148
1149 pdfContext->fObjectStack.push(ac);
1150 PdfOp_Tc(pdfContext, canvas, looper);
1151
1152 pdfContext->fObjectStack.push(str);
1153 PdfOp_quote(pdfContext, canvas, looper);
1154
1155 return kPartial_PdfResult;
1156}
1157
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001158static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001159 if (!pdfContext->fGraphicsState.fTextBlock) {
1160 // TODO(edisonn): try to recover and draw it any way?
1161 return kIgnoreError_PdfResult;
1162 }
1163
edisonn@google.com571c70b2013-07-10 17:09:50 +00001164 SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001165 pdfContext->fObjectStack.pop();
1166
edisonn@google.com571c70b2013-07-10 17:09:50 +00001167 if (!array->isArray()) {
1168 return kIgnoreError_PdfResult;
1169 }
1170
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001171 for( int i=0; i<static_cast<int>(array->size()); i++ )
1172 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001173 if( (*array)[i]->isAnyString()) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001174 SkPdfObject* obj = (*array)[i];
1175 DrawText(pdfContext,
1176 obj,
1177 canvas);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001178 } else if ((*array)[i]->isNumber()) {
1179 double dx = (*array)[i]->numberValue();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001180 SkMatrix matrix;
1181 matrix.setAll(SkDoubleToScalar(1),
1182 SkDoubleToScalar(0),
1183 // TODO(edisonn): use writing mode, vertical/horizontal.
1184 SkDoubleToScalar(-dx), // amount is substracted!!!
1185 SkDoubleToScalar(0),
1186 SkDoubleToScalar(1),
1187 SkDoubleToScalar(0),
1188 SkDoubleToScalar(0),
1189 SkDoubleToScalar(0),
1190 SkDoubleToScalar(1));
1191
1192 pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
1193 }
1194 }
1195 return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK.
1196}
1197
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001198static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001199 colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001200 return kOK_PdfResult;
1201}
1202
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001203static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001204 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1205}
1206
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001207static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001208 return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1209}
1210
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001211static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001212 double c[4];
edisonn@google.com571c70b2013-07-10 17:09:50 +00001213// int64_t v[4];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001214
1215 int n = GetColorSpaceComponents(colorOperator->fColorSpace);
1216
1217 bool doubles = true;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001218 if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001219 doubles = false;
1220 }
1221
1222#ifdef PDF_TRACE
edisonn@google.com571c70b2013-07-10 17:09:50 +00001223 printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001224#endif
1225
1226 for (int i = n - 1; i >= 0 ; i--) {
1227 if (doubles) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001228 c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1229// } else {
1230// v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001231 }
1232 }
1233
1234 // TODO(edisonn): Now, set that color. Only DeviceRGB supported.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001235 // TODO(edisonn): do possible field values to enum at parsing time!
1236 // TODO(edisonn): support also abreviations /DeviceRGB == /RGB
1237 if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001238 colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2]));
1239 }
1240 return kPartial_PdfResult;
1241}
1242
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001243static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001244 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1245}
1246
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001247static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001248 return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1249}
1250
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001251static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001252 //SkPdfString* name;
1253 if (pdfContext->fObjectStack.top()->isName()) {
1254 // TODO(edisonn): get name, pass it
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001255 pdfContext->fObjectStack.pop();
1256 }
1257
1258 // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec.
1259 PdfOp_SC_sc(pdfContext, canvas, colorOperator);
1260
1261 return kPartial_PdfResult;
1262}
1263
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001264static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001265 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1266}
1267
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001268static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001269 return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1270}
1271
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001272static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001273 /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001274 return kNYI_PdfResult;
1275}
1276
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001277static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001278 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1279}
1280
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001281static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001282 return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1283}
1284
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001285static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001286 double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1287 double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1288 double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001289
1290 colorOperator->fColorSpace = "DeviceRGB";
1291 colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b));
1292 return kOK_PdfResult;
1293}
1294
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001295static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001296 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1297}
1298
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001299static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001300 return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1301}
1302
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001303static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001304 // TODO(edisonn): spec has some rules about overprint, implement them.
edisonn@google.com571c70b2013-07-10 17:09:50 +00001305 /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1306 /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1307 /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
1308 /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001309
1310 colorOperator->fColorSpace = "DeviceCMYK";
1311 // TODO(edisonn): Set color.
1312 return kNYI_PdfResult;
1313}
1314
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001315static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001316 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking);
1317}
1318
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001319static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001320 return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking);
1321}
1322
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001323static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001324 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1325 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1326
1327 return kOK_PdfResult;
1328}
1329
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001330static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001331 pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath;
1332
1333#ifdef PDF_TRACE
1334 if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) {
1335 printf("CLIP IS RECT\n");
1336 }
1337#endif
1338
1339 // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd.
1340 pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType);
1341 pdfContext->fGraphicsState.fHasClipPathToApply = true;
1342
1343 return kPartial_PdfResult;
1344}
1345
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001346static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001347 *looper = new PdfCompatibilitySectionLooper();
1348 return kOK_PdfResult;
1349}
1350
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001351static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001352#ifdef ASSERT_BAD_PDF_OPS
1353 SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's
1354 // have the assert when testing good pdfs.
1355#endif
1356 return kIgnoreError_PdfResult;
1357}
1358
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001359static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001360 *looper = new PdfInlineImageLooper();
1361 return kOK_PdfResult;
1362}
1363
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001364static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001365#ifdef ASSERT_BAD_PDF_OPS
1366 SkASSERT(false); // must be processed in inline image looper, but let's
1367 // have the assert when testing good pdfs.
1368#endif
1369 return kIgnoreError_PdfResult;
1370}
1371
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001372static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001373#ifdef ASSERT_BAD_PDF_OPS
1374 SkASSERT(false); // must be processed in inline image looper, but let's
1375 // have the assert when testing good pdfs.
1376#endif
1377 return kIgnoreError_PdfResult;
1378}
1379
1380//lineWidth w Set the line width in the graphics state (see “Line Width” on page 152).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001381static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001382 double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001383 pdfContext->fGraphicsState.fLineWidth = lineWidth;
1384
1385 return kOK_PdfResult;
1386}
1387
1388//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 +00001389static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001390 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001391 //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001392
1393 return kNYI_PdfResult;
1394}
1395
1396//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 +00001397static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001398 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001399 //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001400
1401 return kNYI_PdfResult;
1402}
1403
1404//miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001405static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001406 pdfContext->fObjectStack.pop();
edisonn@google.com571c70b2013-07-10 17:09:50 +00001407 //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001408
1409 return kNYI_PdfResult;
1410}
1411
1412//dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on
1413//page 155).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001414static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001415 pdfContext->fObjectStack.pop();
1416 pdfContext->fObjectStack.pop();
1417
1418 return kNYI_PdfResult;
1419}
1420
1421//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 +00001422static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001423 pdfContext->fObjectStack.pop();
1424
1425 return kNYI_PdfResult;
1426}
1427
1428//flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness
1429//Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci-
1430//fies the output device’s default flatness tolerance.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001431static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001432 pdfContext->fObjectStack.pop();
1433
1434 return kNYI_PdfResult;
1435}
1436
1437//dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is
1438//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 +00001439static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001440 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001441
1442#ifdef PDF_TRACE
1443 std::string str;
1444#endif
1445
1446 //Next, get the ExtGState Dictionary from the Resource Dictionary:
edisonn@google.com571c70b2013-07-10 17:09:50 +00001447 SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001448
1449 if (extGStateDictionary == NULL) {
1450#ifdef PDF_TRACE
1451 printf("ExtGState is NULL!\n");
1452#endif
1453 return kIgnoreError_PdfResult;
1454 }
1455
edisonn@google.com571c70b2013-07-10 17:09:50 +00001456 SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name));
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001457
edisonn@google.com571c70b2013-07-10 17:09:50 +00001458 if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) {
1459 return kIgnoreError_PdfResult;
1460 }
1461 SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001462
1463 // TODO(edisonn): now load all those properties in graphic state.
1464 if (gs == NULL) {
1465 return kIgnoreError_PdfResult;
1466 }
1467
1468 if (gs->has_CA()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001469 pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001470 }
1471
1472 if (gs->has_ca()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001473 pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001474 }
1475
1476 if (gs->has_LW()) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001477 pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001478 }
1479
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001480 return kNYI_PdfResult;
1481}
1482
1483//charSpace Tc Set the character spacing, Tc
1484//, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators.
1485//Initial value: 0.
1486PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001487 double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001488 pdfContext->fGraphicsState.fCharSpace = charSpace;
1489
1490 return kOK_PdfResult;
1491}
1492
1493//wordSpace Tw Set the word spacing, T
1494//w
1495//, to wordSpace, which is a number expressed in unscaled
1496//text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial
1497//value: 0.
1498PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001499 double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001500 pdfContext->fGraphicsState.fWordSpace = wordSpace;
1501
1502 return kOK_PdfResult;
1503}
1504
1505//scale Tz Set the horizontal scaling, Th
1506//, to (scale ˜ 100). scale is a number specifying the
1507//percentage of the normal width. Initial value: 100 (normal width).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001508static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001509 /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001510
1511 return kNYI_PdfResult;
1512}
1513
1514//render Tr Set the text rendering mode, T
1515//mode, to render, which is an integer. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001516static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001517 /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001518
1519 return kNYI_PdfResult;
1520}
1521//rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space
1522//units. Initial value: 0.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001523static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001524 /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001525
1526 return kNYI_PdfResult;
1527}
1528
1529//wx wy d0
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001530static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001531 pdfContext->fObjectStack.pop();
1532 pdfContext->fObjectStack.pop();
1533
1534 return kNYI_PdfResult;
1535}
1536
1537//wx wy llx lly urx ury d1
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001538static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001539 pdfContext->fObjectStack.pop();
1540 pdfContext->fObjectStack.pop();
1541 pdfContext->fObjectStack.pop();
1542 pdfContext->fObjectStack.pop();
1543 pdfContext->fObjectStack.pop();
1544 pdfContext->fObjectStack.pop();
1545
1546 return kNYI_PdfResult;
1547}
1548
1549//name sh
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001550static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001551 pdfContext->fObjectStack.pop();
1552
1553 return kNYI_PdfResult;
1554}
1555
1556//name Do
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001557static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001558 const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop();
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001559
edisonn@google.com571c70b2013-07-10 17:09:50 +00001560 SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001561
1562 if (xObject == NULL) {
1563#ifdef PDF_TRACE
1564 printf("XObject is NULL!\n");
1565#endif
1566 return kIgnoreError_PdfResult;
1567 }
1568
edisonn@google.com571c70b2013-07-10 17:09:50 +00001569 SkPdfObject* value = xObject->get(name);
1570 value = pdfContext->fPdfDoc->resolveReference(value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001571
1572#ifdef PDF_TRACE
1573// value->ToString(str);
edisonn@google.com571c70b2013-07-10 17:09:50 +00001574// printf("Do object value: %s\n", str);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001575#endif
1576
edisonn@google.com571c70b2013-07-10 17:09:50 +00001577 return doXObject(pdfContext, canvas, value);
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001578}
1579
1580//tag MP Designate a marked-content point. tag is a name object indicating the role or
1581//significance of the point.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001582static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001583 pdfContext->fObjectStack.pop();
1584
1585 return kNYI_PdfResult;
1586}
1587
1588//tag properties DP Designate a marked-content point with an associated property list. tag is a
1589//name object indicating the role or significance of the point; properties is
1590//either an inline dictionary containing the property list or a name object
1591//associated with it in the Properties subdictionary of the current resource
1592//dictionary (see Section 9.5.1, “Property Lists”).
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001593static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001594 pdfContext->fObjectStack.pop();
1595 pdfContext->fObjectStack.pop();
1596
1597 return kNYI_PdfResult;
1598}
1599
1600//tag BMC Begin a marked-content sequence terminated by a balancing EMC operator.
1601//tag is a name object indicating the role or significance of the sequence.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001602static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001603 pdfContext->fObjectStack.pop();
1604
1605 return kNYI_PdfResult;
1606}
1607
1608//tag properties BDC Begin a marked-content sequence with an associated property list, terminated
1609//by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the
1610//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 +00001611static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001612 pdfContext->fObjectStack.pop();
1613 pdfContext->fObjectStack.pop();
1614
1615 return kNYI_PdfResult;
1616}
1617
1618//— EMC End a marked-content sequence begun by a BMC or BDC operator.
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001619static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001620 return kNYI_PdfResult;
1621}
1622
edisonn@google.coma3356fc2013-07-10 18:20:06 +00001623static void initPdfOperatorRenderes() {
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001624 static bool gInitialized = false;
1625 if (gInitialized) {
1626 return;
1627 }
1628
1629 gPdfOps["q"] = PdfOp_q;
1630 gPdfOps["Q"] = PdfOp_Q;
1631 gPdfOps["cm"] = PdfOp_cm;
1632
1633 gPdfOps["TD"] = PdfOp_TD;
1634 gPdfOps["Td"] = PdfOp_Td;
1635 gPdfOps["Tm"] = PdfOp_Tm;
1636 gPdfOps["T*"] = PdfOp_T_star;
1637
1638 gPdfOps["m"] = PdfOp_m;
1639 gPdfOps["l"] = PdfOp_l;
1640 gPdfOps["c"] = PdfOp_c;
1641 gPdfOps["v"] = PdfOp_v;
1642 gPdfOps["y"] = PdfOp_y;
1643 gPdfOps["h"] = PdfOp_h;
1644 gPdfOps["re"] = PdfOp_re;
1645
1646 gPdfOps["S"] = PdfOp_S;
1647 gPdfOps["s"] = PdfOp_s;
1648 gPdfOps["f"] = PdfOp_f;
1649 gPdfOps["F"] = PdfOp_F;
1650 gPdfOps["f*"] = PdfOp_f_star;
1651 gPdfOps["B"] = PdfOp_B;
1652 gPdfOps["B*"] = PdfOp_B_star;
1653 gPdfOps["b"] = PdfOp_b;
1654 gPdfOps["b*"] = PdfOp_b_star;
1655 gPdfOps["n"] = PdfOp_n;
1656
1657 gPdfOps["BT"] = PdfOp_BT;
1658 gPdfOps["ET"] = PdfOp_ET;
1659
1660 gPdfOps["Tj"] = PdfOp_Tj;
1661 gPdfOps["'"] = PdfOp_quote;
1662 gPdfOps["\""] = PdfOp_doublequote;
1663 gPdfOps["TJ"] = PdfOp_TJ;
1664
1665 gPdfOps["CS"] = PdfOp_CS;
1666 gPdfOps["cs"] = PdfOp_cs;
1667 gPdfOps["SC"] = PdfOp_SC;
1668 gPdfOps["SCN"] = PdfOp_SCN;
1669 gPdfOps["sc"] = PdfOp_sc;
1670 gPdfOps["scn"] = PdfOp_scn;
1671 gPdfOps["G"] = PdfOp_G;
1672 gPdfOps["g"] = PdfOp_g;
1673 gPdfOps["RG"] = PdfOp_RG;
1674 gPdfOps["rg"] = PdfOp_rg;
1675 gPdfOps["K"] = PdfOp_K;
1676 gPdfOps["k"] = PdfOp_k;
1677
1678 gPdfOps["W"] = PdfOp_W;
1679 gPdfOps["W*"] = PdfOp_W_star;
1680
1681 gPdfOps["BX"] = PdfOp_BX;
1682 gPdfOps["EX"] = PdfOp_EX;
1683
1684 gPdfOps["BI"] = PdfOp_BI;
1685 gPdfOps["ID"] = PdfOp_ID;
1686 gPdfOps["EI"] = PdfOp_EI;
1687
1688 gPdfOps["w"] = PdfOp_w;
1689 gPdfOps["J"] = PdfOp_J;
1690 gPdfOps["j"] = PdfOp_j;
1691 gPdfOps["M"] = PdfOp_M;
1692 gPdfOps["d"] = PdfOp_d;
1693 gPdfOps["ri"] = PdfOp_ri;
1694 gPdfOps["i"] = PdfOp_i;
1695 gPdfOps["gs"] = PdfOp_gs;
1696
1697 gPdfOps["Tc"] = PdfOp_Tc;
1698 gPdfOps["Tw"] = PdfOp_Tw;
1699 gPdfOps["Tz"] = PdfOp_Tz;
1700 gPdfOps["TL"] = PdfOp_TL;
1701 gPdfOps["Tf"] = PdfOp_Tf;
1702 gPdfOps["Tr"] = PdfOp_Tr;
1703 gPdfOps["Ts"] = PdfOp_Ts;
1704
1705 gPdfOps["d0"] = PdfOp_d0;
1706 gPdfOps["d1"] = PdfOp_d1;
1707
1708 gPdfOps["sh"] = PdfOp_sh;
1709
1710 gPdfOps["Do"] = PdfOp_Do;
1711
1712 gPdfOps["MP"] = PdfOp_MP;
1713 gPdfOps["DP"] = PdfOp_DP;
1714 gPdfOps["BMC"] = PdfOp_BMC;
1715 gPdfOps["BDC"] = PdfOp_BDC;
1716 gPdfOps["EMC"] = PdfOp_EMC;
1717
1718 gInitialized = true;
1719}
1720
1721class InitPdfOps {
1722public:
1723 InitPdfOps() {
1724 initPdfOperatorRenderes();
1725 }
1726};
1727
1728InitPdfOps gInitPdfOps;
1729
1730void reportPdfRenderStats() {
1731 std::map<std::string, int>::iterator iter;
1732
1733 for (int i = 0 ; i < kCount_PdfResult; i++) {
1734 for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) {
1735 printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second);
1736 }
1737 }
1738}
1739
1740PdfResult PdfMainLooper::consumeToken(PdfToken& token) {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001741 char keyword[256];
1742
1743 if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256)
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001744 {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001745 strncpy(keyword, token.fKeyword, token.fKeywordLength);
1746 keyword[token.fKeywordLength] = '\0';
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001747 // TODO(edisonn): log trace flag (verbose, error, info, warning, ...)
edisonn@google.com571c70b2013-07-10 17:09:50 +00001748 PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword];
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001749 if (pdfOperatorRenderer) {
1750 // caller, main work is done by pdfOperatorRenderer(...)
1751 PdfTokenLooper* childLooper = NULL;
edisonn@google.com571c70b2013-07-10 17:09:50 +00001752 gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001753
1754 if (childLooper) {
1755 childLooper->setUp(this);
1756 childLooper->loop();
1757 delete childLooper;
1758 }
1759 } else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001760 gRenderStats[kUnsupported_PdfResult][keyword]++;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001761 }
1762 }
1763 else if (token.fType == kObject_TokenType)
1764 {
1765 fPdfContext->fObjectStack.push( token.fObject );
1766 }
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001767 else {
edisonn@google.com571c70b2013-07-10 17:09:50 +00001768 // TODO(edisonn): deine or use assert not reached
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001769 return kIgnoreError_PdfResult;
1770 }
1771 return kOK_PdfResult;
1772}
1773
1774void PdfMainLooper::loop() {
1775 PdfToken token;
1776 while (readToken(fTokenizer, &token)) {
1777 consumeToken(token);
1778 }
1779}
1780
1781PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) {
1782 //pdfContext.fInlineImage.fKeyValuePairs[key] = value;
1783 return kNYI_PdfResult;
1784}
1785
1786void PdfInlineImageLooper::loop() {
1787 PdfToken token;
1788 while (readToken(fTokenizer, &token)) {
1789 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1790 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1791 looper->setUp(this);
1792 looper->loop();
1793 } else {
1794 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EI") == 0) {
1795 done();
1796 return;
1797 }
1798
1799 consumeToken(token);
1800 }
1801 }
1802 // TODO(edisonn): report error/warning, EOF without EI.
1803}
1804
1805PdfResult PdfInlineImageLooper::done() {
1806
1807 // TODO(edisonn): long to short names
1808 // TODO(edisonn): set properties in a map
1809 // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress
1810 // the stream.
1811
1812 SkBitmap bitmap;
1813 setup_bitmap(&bitmap, 50, 50, SK_ColorRED);
1814
1815 // TODO(edisonn): matrix use.
1816 // Draw dummy red square, to show the prezence of the inline image.
1817 fCanvas->drawBitmap(bitmap,
1818 SkDoubleToScalar(0),
1819 SkDoubleToScalar(0),
1820 NULL);
1821 return kNYI_PdfResult;
1822}
1823
1824PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) {
1825 return fParent->consumeToken(token);
1826}
1827
1828void PdfCompatibilitySectionLooper::loop() {
1829 // TODO(edisonn): save stacks position, or create a new stack?
1830 // TODO(edisonn): what happens if we pop out more variables then when we started?
1831 // restore them? fail? We could create a new operands stack for every new BX/EX section,
1832 // pop-ing too much will not affect outside the section.
1833 PdfToken token;
1834 while (readToken(fTokenizer, &token)) {
1835 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) {
1836 PdfTokenLooper* looper = new PdfCompatibilitySectionLooper();
1837 looper->setUp(this);
1838 looper->loop();
1839 delete looper;
1840 } else {
1841 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break;
1842 fParent->consumeToken(token);
1843 }
1844 }
1845 // TODO(edisonn): restore stack.
1846}
1847
1848// TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf
1849// TODO(edisonn): Add API for Forms viewing and editing
1850// e.g. SkBitmap getPage(int page);
1851// int formsCount();
1852// SkForm getForm(int formID); // SkForm(SkRect, .. other data)
1853// TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ...
1854// if we load the first page, and we zoom to fit to screen horizontally, then load only those
1855// resources needed, so the preview is fast.
1856// TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve
1857// references automatically.
1858
1859bool SkPdfViewer::load(const SkString inputFileName, SkPicture* out) {
edisonn@google.com596d2e22013-07-10 17:44:55 +00001860 std::cout << "PDF Loaded: " << inputFileName.c_str() << std::endl;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001861
edisonn@google.com571c70b2013-07-10 17:09:50 +00001862 SkNativeParsedPDF* doc = new SkNativeParsedPDF(inputFileName.c_str());
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001863 if (!doc->pages())
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001864 {
edisonn@google.com596d2e22013-07-10 17:44:55 +00001865 std::cout << "ERROR: Empty PDF Document" << inputFileName.c_str() << std::endl;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001866 return false;
edisonn@google.com3aac1f92013-07-02 22:42:53 +00001867 } else {
1868
1869 for (int pn = 0; pn < doc->pages(); ++pn) {
1870 // TODO(edisonn): implement inheritance properties as per PDF spec
1871 //SkRect rect = page->MediaBox();
1872 SkRect rect = doc->MediaBox(pn);
1873
1874#ifdef PDF_TRACE
1875 printf("Page Width: %f, Page Height: %f\n", SkScalarToDouble(rect.width()), SkScalarToDouble(rect.height()));
1876#endif
1877
1878 // TODO(edisonn): page->GetCropBox(), page->GetTrimBox() ... how to use?
1879
1880 SkBitmap bitmap;
1881#ifdef PDF_DEBUG_3X
1882 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkScalarToDouble(rect.height()));
1883#else
1884 setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDouble(rect.height()));
1885#endif
1886 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
1887 SkCanvas canvas(device);
1888
1889 gDumpBitmap = &bitmap;
1890
1891 gDumpCanvas = &canvas;
1892 doc->drawPage(pn, &canvas);
1893
1894 SkString out;
1895 if (doc->pages() > 1) {
1896 out.appendf("%s-%i.png", inputFileName.c_str(), pn);
1897 } else {
1898 out = inputFileName;
1899 // .pdf -> .png
1900 out[out.size() - 2] = 'n';
1901 out[out.size() - 1] = 'g';
1902 }
1903 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
1904 }
1905 return true;
edisonn@google.com131d4ee2013-06-26 17:48:12 +00001906 }
1907
1908 return true;
1909}