edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 19 | |
edisonn@google.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame^] | 20 | #include "SkPdfBasics.h" |
| 21 | #include "SkPdfNativeTokenizer.h" |
| 22 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <cstdio> |
| 25 | #include <stack> |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 26 | #include <set> |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 27 | |
edisonn@google.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame^] | 28 | extern "C" PdfContext* gPdfContext; |
| 29 | extern "C" SkBitmap* gDumpBitmap; |
| 30 | extern "C" SkCanvas* gDumpCanvas; |
| 31 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 32 | __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.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 40 | // 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 42 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 43 | // 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 47 | pdfContext->fGraphicsState.fResources->native()->ToString(str); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 48 | printf("Print Tf Resources: %s\n", str.c_str()); |
| 49 | #endif |
| 50 | */ |
| 51 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 52 | #include "SkPdfHeaders_autogen.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 53 | #include "SkPdfMapper_autogen.h" |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 54 | #include "SkPdfRenderer.h" |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 55 | |
| 56 | #include "SkPdfBasics.h" |
| 57 | #include "SkPdfUtils.h" |
| 58 | |
| 59 | #include "SkPdfFont.h" |
| 60 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 61 | /* |
| 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 69 | * - encapsulate native in the pdf api so the skpdf does not know anything about native ... in progress |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 70 | * - 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 | |
| 79 | using namespace std; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 80 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | // TODO(edisonn): Document PdfTokenLooper and subclasses. |
| 84 | class PdfTokenLooper { |
| 85 | protected: |
| 86 | PdfTokenLooper* fParent; |
| 87 | SkPdfNativeTokenizer* fTokenizer; |
| 88 | PdfContext* fPdfContext; |
| 89 | SkCanvas* fCanvas; |
| 90 | |
| 91 | public: |
| 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 | |
| 111 | class PdfMainLooper : public PdfTokenLooper { |
| 112 | public: |
| 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 | |
| 123 | class PdfInlineImageLooper : public PdfTokenLooper { |
| 124 | public: |
| 125 | PdfInlineImageLooper() |
| 126 | : PdfTokenLooper(NULL, NULL, NULL, NULL) {} |
| 127 | |
| 128 | virtual PdfResult consumeToken(PdfToken& token); |
| 129 | virtual void loop(); |
| 130 | PdfResult done(); |
| 131 | }; |
| 132 | |
| 133 | class PdfCompatibilitySectionLooper : public PdfTokenLooper { |
| 134 | public: |
| 135 | PdfCompatibilitySectionLooper() |
| 136 | : PdfTokenLooper(NULL, NULL, NULL, NULL) {} |
| 137 | |
| 138 | virtual PdfResult consumeToken(PdfToken& token); |
| 139 | virtual void loop(); |
| 140 | }; |
| 141 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 142 | // Utilities |
| 143 | static 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 151 | static int GetColorSpaceComponents(const std::string& colorSpace) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 152 | 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 167 | SkMatrix SkMatrixFromPdfMatrix(double array[6]) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 168 | 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 | |
| 182 | SkMatrix 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 187 | const SkPdfObject* elem = pdfArray->operator [](i); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 188 | if (elem == NULL || !elem->isNumber()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 189 | return SkMatrix::I(); // TODO(edisonn): report issue |
| 190 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 191 | array[i] = elem->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return SkMatrixFromPdfMatrix(array); |
| 195 | } |
| 196 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 197 | |
| 198 | extern "C" SkNativeParsedPDF* gDoc; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 199 | SkBitmap* gDumpBitmap = NULL; |
| 200 | SkCanvas* gDumpCanvas = NULL; |
| 201 | char gLastKeyword[100] = ""; |
| 202 | int gLastOpKeyword = -1; |
| 203 | char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,"; |
| 204 | int gReadOp = 0; |
| 205 | |
| 206 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 207 | #ifdef PDF_TRACE_DIFF_IN_PNG |
| 208 | static bool hasVisualEffect(const char* pdfOp) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 209 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 218 | #endif // PDF_TRACE_DIFF_IN_PNG |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 219 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 220 | |
| 221 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 222 | // TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 223 | static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 224 | 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 | |
| 316 | typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**); |
| 317 | |
| 318 | map<std::string, PdfOperatorRenderer> gPdfOps; |
| 319 | |
| 320 | map<std::string, int> gRenderStats[kCount_PdfResult]; |
| 321 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 322 | const char* gRenderStatsNames[kCount_PdfResult] = { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 323 | "Success", |
| 324 | "Partially implemented", |
| 325 | "Not yet implemented", |
| 326 | "Ignore Error", |
| 327 | "Error", |
| 328 | "Unsupported/Unknown" |
| 329 | }; |
| 330 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 331 | static PdfResult DrawText(PdfContext* pdfContext, |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 332 | const SkPdfObject* _str, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 333 | SkCanvas* canvas) |
| 334 | { |
| 335 | |
| 336 | SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont; |
| 337 | if (skfont == NULL) { |
| 338 | skfont = SkPdfFont::Default(); |
| 339 | } |
| 340 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 341 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 342 | if (_str == NULL || !_str->isAnyString()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 343 | // TODO(edisonn): report warning |
| 344 | return kIgnoreError_PdfResult; |
| 345 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 346 | const SkPdfString* str = (const SkPdfString*)_str; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 347 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 348 | 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.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 394 | skfont->drawText(decoded, &paint, pdfContext, canvas); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 395 | canvas->restore(); |
| 396 | |
| 397 | return kPartial_PdfResult; |
| 398 | } |
| 399 | |
| 400 | // TODO(edisonn): create header files with declarations! |
| 401 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 402 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 403 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 404 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 405 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 406 | // TODO(edisonn): perf!!! |
| 407 | |
| 408 | static 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 420 | static SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 421 | int width, int height, int bytesPerLine, |
| 422 | int bpc, const std::string& colorSpace, |
| 423 | bool transparencyMask) { |
| 424 | SkBitmap bitmap; |
| 425 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 426 | //int components = GetColorSpaceComponents(colorSpace); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 427 | //#define MAX_COMPONENTS 10 |
| 428 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 429 | // 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 437 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 438 | 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 454 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 455 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 472 | // 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 481 | static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 482 | if (image == NULL || !image->hasStream()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 483 | // TODO(edisonn): report warning to be used in testing. |
| 484 | return SkBitmap(); |
| 485 | } |
| 486 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 487 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 490 | std::string colorSpace = "DeviceRGB"; |
| 491 | |
| 492 | // TODO(edisonn): color space can be an array too! |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 493 | if (image->isColorSpaceAName(pdfContext->fPdfDoc)) { |
| 494 | colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 495 | } |
| 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 509 | unsigned char* uncompressedStream = NULL; |
| 510 | size_t uncompressedStreamLength = 0; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 511 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 512 | SkPdfStream* stream = (SkPdfStream*)image; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 513 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 514 | if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) || |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 515 | uncompressedStream == NULL || uncompressedStreamLength == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 516 | // TODO(edisonn): report warning to be used in testing. |
| 517 | return SkBitmap(); |
| 518 | } |
| 519 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 520 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 547 | int bytesPerLine = uncompressedStreamLength / height; |
| 548 | #ifdef PDF_TRACE |
| 549 | if (uncompressedStreamLength % height != 0) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 550 | printf("Warning uncompressedStreamLength modulo height != 0 !!!\n"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 551 | } |
| 552 | #endif |
| 553 | |
| 554 | SkBitmap bitmap = transferImageStreamToBitmap( |
| 555 | (unsigned char*)uncompressedStream, uncompressedStreamLength, |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 556 | (int)width, (int)height, bytesPerLine, |
| 557 | (int)bpc, colorSpace, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 558 | transparencyMask); |
| 559 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 560 | return bitmap; |
| 561 | } |
| 562 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 563 | static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 564 | SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 565 | |
| 566 | if (sMask) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 567 | return getImageFromObject(pdfContext, sMask, true); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // TODO(edisonn): implement GS SMask. Default to empty right now. |
| 571 | return pdfContext->fGraphicsState.fSMask; |
| 572 | } |
| 573 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 574 | static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 575 | if (skpdfimage == NULL) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 576 | 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 584 | |
| 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 598 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 617 | |
| 618 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 619 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 620 | static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 621 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 622 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | PdfOp_q(pdfContext, canvas, NULL); |
| 626 | canvas->save(); |
| 627 | |
| 628 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 629 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 630 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix"); |
| 634 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 635 | if (skobj->has_Matrix()) { |
| 636 | pdfContext->fGraphicsState.fMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 637 | 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 646 | if (skobj->has_BBox()) { |
| 647 | canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 648 | } |
| 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 653 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 654 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 655 | SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 656 | if (tokenizer != NULL) { |
| 657 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 658 | looper.loop(); |
| 659 | delete tokenizer; |
| 660 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 661 | |
| 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 666 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 667 | } |
| 668 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 669 | //static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
| 670 | // return kNYI_PdfResult; |
| 671 | //} |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 672 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 673 | PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) { |
| 674 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 675 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 676 | } |
| 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 701 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 702 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 703 | SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 704 | if (tokenizer != NULL) { |
| 705 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 706 | looper.loop(); |
| 707 | delete tokenizer; |
| 708 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 709 | |
| 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 714 | |
| 715 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 719 | // TODO(edisonn): make sure the pointer is unique |
| 720 | std::set<const SkPdfObject*> gInRendering; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 721 | |
| 722 | class CheckRecursiveRendering { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 723 | const SkPdfObject* fUniqueData; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 724 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 725 | CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 726 | gInRendering.insert(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | ~CheckRecursiveRendering() { |
| 730 | //SkASSERT(fObj.fInRendering); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 731 | gInRendering.erase(fUniqueData); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 732 | } |
| 733 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 734 | static bool IsInRendering(const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 735 | return gInRendering.find(obj) != gInRendering.end(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 736 | } |
| 737 | }; |
| 738 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 739 | static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 740 | if (CheckRecursiveRendering::IsInRendering(obj)) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 741 | // Oops, corrupt PDF! |
| 742 | return kIgnoreError_PdfResult; |
| 743 | } |
| 744 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 745 | CheckRecursiveRendering checkRecursion(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 746 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 747 | switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj)) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 748 | { |
| 749 | case kImageDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 750 | return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 751 | case kType1FormDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 752 | return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 753 | //case kObjectDictionaryXObjectPS_SkPdfObjectType: |
| 754 | //return doXObject_PS(skxobj.asPS()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 755 | default: |
| 756 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 757 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 761 | pdfContext->fStateStack.push(pdfContext->fGraphicsState); |
| 762 | canvas->save(); |
| 763 | return kOK_PdfResult; |
| 764 | } |
| 765 | |
| 766 | PdfResult 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 773 | static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 774 | double array[6]; |
| 775 | for (int i = 0 ; i < 6 ; i++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 776 | array[5 - i] = pdfContext->fObjectStack.top()->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 777 | 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 801 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "cm"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 802 | #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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 810 | static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 811 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 812 | |
| 813 | pdfContext->fGraphicsState.fTextLeading = ty; |
| 814 | |
| 815 | return kOK_PdfResult; |
| 816 | } |
| 817 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 818 | static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 819 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 820 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 821 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 831 | static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 832 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 833 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 834 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 835 | // TODO(edisonn): Create factory methods or constructors so native is hidden |
| 836 | SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 837 | pdfContext->fObjectStack.push(_ty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 838 | |
| 839 | PdfOp_TL(pdfContext, canvas, looper); |
| 840 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 841 | SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 842 | pdfContext->fObjectStack.push(vtx); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 843 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 844 | SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 845 | pdfContext->fObjectStack.push(vty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 846 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 847 | 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.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 851 | |
| 852 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 853 | } |
| 854 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 855 | static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 856 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 862 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 884 | static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 885 | SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0); |
| 886 | SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 887 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 888 | pdfContext->fObjectStack.push(zero); |
| 889 | pdfContext->fObjectStack.push(tl); |
| 890 | |
| 891 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 892 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 893 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 894 | } |
| 895 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 896 | static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 897 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 898 | pdfContext->fGraphicsState.fPath.reset(); |
| 899 | pdfContext->fGraphicsState.fPathClosed = false; |
| 900 | } |
| 901 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 902 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 903 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 904 | |
| 905 | pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 906 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 907 | |
| 908 | return kOK_PdfResult; |
| 909 | } |
| 910 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 911 | static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 912 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 913 | pdfContext->fGraphicsState.fPath.reset(); |
| 914 | pdfContext->fGraphicsState.fPathClosed = false; |
| 915 | } |
| 916 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 917 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 918 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 919 | |
| 920 | pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 921 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 922 | |
| 923 | return kOK_PdfResult; |
| 924 | } |
| 925 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 926 | static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 927 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 928 | pdfContext->fGraphicsState.fPath.reset(); |
| 929 | pdfContext->fGraphicsState.fPathClosed = false; |
| 930 | } |
| 931 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 932 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 938 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 949 | static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 950 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 951 | pdfContext->fGraphicsState.fPath.reset(); |
| 952 | pdfContext->fGraphicsState.fPathClosed = false; |
| 953 | } |
| 954 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 955 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 959 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 972 | static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 973 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 974 | pdfContext->fGraphicsState.fPath.reset(); |
| 975 | pdfContext->fGraphicsState.fPathClosed = false; |
| 976 | } |
| 977 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 978 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 979 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 980 | double y2 = pdfContext->fGraphicsState.fCurPosY; |
| 981 | double x2 = pdfContext->fGraphicsState.fCurPosX; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 982 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 983 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 984 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 995 | static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 996 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 997 | pdfContext->fGraphicsState.fPath.reset(); |
| 998 | pdfContext->fGraphicsState.fPathClosed = false; |
| 999 | } |
| 1000 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1001 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1005 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1015 | static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1016 | pdfContext->fGraphicsState.fPath.close(); |
| 1017 | return kOK_PdfResult; |
| 1018 | } |
| 1019 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1020 | static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1021 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1077 | static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1078 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false); |
| 1079 | } |
| 1080 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1081 | static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1082 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false); |
| 1083 | } |
| 1084 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1085 | static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1086 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1087 | } |
| 1088 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1089 | static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1090 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1091 | } |
| 1092 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1093 | static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1094 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true); |
| 1095 | } |
| 1096 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1097 | static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1098 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false); |
| 1099 | } |
| 1100 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1101 | static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1102 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true); |
| 1103 | } |
| 1104 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1105 | static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1106 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false); |
| 1107 | } |
| 1108 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1109 | static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1110 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true); |
| 1111 | } |
| 1112 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1113 | static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1114 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1129 | static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1130 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1137 | static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1138 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1150 | static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1151 | pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1152 | const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1153 | |
| 1154 | #ifdef PDF_TRACE |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1155 | printf("font name: %s\n", fontName); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1156 | #endif |
| 1157 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1158 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1172 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1173 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1176 | static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1177 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1190 | static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1191 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1201 | static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1202 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1223 | static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1224 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1225 | // TODO(edisonn): try to recover and draw it any way? |
| 1226 | return kIgnoreError_PdfResult; |
| 1227 | } |
| 1228 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1229 | SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1230 | pdfContext->fObjectStack.pop(); |
| 1231 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1232 | if (!array->isArray()) { |
| 1233 | return kIgnoreError_PdfResult; |
| 1234 | } |
| 1235 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1236 | for( int i=0; i<static_cast<int>(array->size()); i++ ) |
| 1237 | { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1238 | if( (*array)[i]->isAnyString()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1239 | SkPdfObject* obj = (*array)[i]; |
| 1240 | DrawText(pdfContext, |
| 1241 | obj, |
| 1242 | canvas); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1243 | } else if ((*array)[i]->isNumber()) { |
| 1244 | double dx = (*array)[i]->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1245 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1263 | static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1264 | colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1265 | return kOK_PdfResult; |
| 1266 | } |
| 1267 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1268 | static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1269 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1270 | } |
| 1271 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1272 | static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1273 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1274 | } |
| 1275 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1276 | static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1277 | double c[4]; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1278 | // int64_t v[4]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1279 | |
| 1280 | int n = GetColorSpaceComponents(colorOperator->fColorSpace); |
| 1281 | |
| 1282 | bool doubles = true; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1283 | if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1284 | doubles = false; |
| 1285 | } |
| 1286 | |
| 1287 | #ifdef PDF_TRACE |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1288 | printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1289 | #endif |
| 1290 | |
| 1291 | for (int i = n - 1; i >= 0 ; i--) { |
| 1292 | if (doubles) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1293 | c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1294 | // } else { |
| 1295 | // v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | // TODO(edisonn): Now, set that color. Only DeviceRGB supported. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1300 | // 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1303 | colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2])); |
| 1304 | } |
| 1305 | return kPartial_PdfResult; |
| 1306 | } |
| 1307 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1308 | static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1309 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1310 | } |
| 1311 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1312 | static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1313 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1314 | } |
| 1315 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1316 | static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1317 | //SkPdfString* name; |
| 1318 | if (pdfContext->fObjectStack.top()->isName()) { |
| 1319 | // TODO(edisonn): get name, pass it |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1320 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1329 | static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1330 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1331 | } |
| 1332 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1333 | static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1334 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1335 | } |
| 1336 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1337 | static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1338 | /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1339 | return kNYI_PdfResult; |
| 1340 | } |
| 1341 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1342 | static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1343 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1344 | } |
| 1345 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1346 | static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1347 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1348 | } |
| 1349 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1350 | static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1351 | 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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1354 | |
| 1355 | colorOperator->fColorSpace = "DeviceRGB"; |
| 1356 | colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b)); |
| 1357 | return kOK_PdfResult; |
| 1358 | } |
| 1359 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1360 | static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1361 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1362 | } |
| 1363 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1364 | static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1365 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1366 | } |
| 1367 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1368 | static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1369 | // TODO(edisonn): spec has some rules about overprint, implement them. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1370 | /*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.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1374 | |
| 1375 | colorOperator->fColorSpace = "DeviceCMYK"; |
| 1376 | // TODO(edisonn): Set color. |
| 1377 | return kNYI_PdfResult; |
| 1378 | } |
| 1379 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1380 | static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1381 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1382 | } |
| 1383 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1384 | static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1385 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1386 | } |
| 1387 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1388 | static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1389 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1390 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1391 | |
| 1392 | return kOK_PdfResult; |
| 1393 | } |
| 1394 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1395 | static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1396 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1411 | static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1412 | *looper = new PdfCompatibilitySectionLooper(); |
| 1413 | return kOK_PdfResult; |
| 1414 | } |
| 1415 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1416 | static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1417 | #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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1424 | static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1425 | *looper = new PdfInlineImageLooper(); |
| 1426 | return kOK_PdfResult; |
| 1427 | } |
| 1428 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1429 | static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1430 | #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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1437 | static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1438 | #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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1446 | static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1447 | double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1448 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1454 | static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1455 | pdfContext->fObjectStack.pop(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1456 | //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1457 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1462 | static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1463 | pdfContext->fObjectStack.pop(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1464 | //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1465 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1470 | static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1471 | pdfContext->fObjectStack.pop(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1472 | //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1473 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1479 | static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1480 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1487 | static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1488 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1496 | static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1497 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1504 | static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1505 | const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1506 | |
| 1507 | #ifdef PDF_TRACE |
| 1508 | std::string str; |
| 1509 | #endif |
| 1510 | |
| 1511 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1512 | SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1513 | |
| 1514 | if (extGStateDictionary == NULL) { |
| 1515 | #ifdef PDF_TRACE |
| 1516 | printf("ExtGState is NULL!\n"); |
| 1517 | #endif |
| 1518 | return kIgnoreError_PdfResult; |
| 1519 | } |
| 1520 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1521 | SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1522 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1523 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) { |
| 1524 | return kIgnoreError_PdfResult; |
| 1525 | } |
| 1526 | SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1527 | |
| 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.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1534 | pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | if (gs->has_ca()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1538 | pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | if (gs->has_LW()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1542 | pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1545 | 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. |
| 1551 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1552 | double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1553 | 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. |
| 1563 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1564 | double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1565 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1573 | static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1574 | /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1575 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1581 | static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1582 | /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1583 | |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1588 | static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1589 | /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1590 | |
| 1591 | return kNYI_PdfResult; |
| 1592 | } |
| 1593 | |
| 1594 | //wx wy d0 |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1595 | static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1596 | pdfContext->fObjectStack.pop(); |
| 1597 | pdfContext->fObjectStack.pop(); |
| 1598 | |
| 1599 | return kNYI_PdfResult; |
| 1600 | } |
| 1601 | |
| 1602 | //wx wy llx lly urx ury d1 |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1603 | static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1604 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1615 | static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1616 | pdfContext->fObjectStack.pop(); |
| 1617 | |
| 1618 | return kNYI_PdfResult; |
| 1619 | } |
| 1620 | |
| 1621 | //name Do |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1622 | static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1623 | const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1624 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1625 | SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1626 | |
| 1627 | if (xObject == NULL) { |
| 1628 | #ifdef PDF_TRACE |
| 1629 | printf("XObject is NULL!\n"); |
| 1630 | #endif |
| 1631 | return kIgnoreError_PdfResult; |
| 1632 | } |
| 1633 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1634 | SkPdfObject* value = xObject->get(name); |
| 1635 | value = pdfContext->fPdfDoc->resolveReference(value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1636 | |
| 1637 | #ifdef PDF_TRACE |
| 1638 | // value->ToString(str); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1639 | // printf("Do object value: %s\n", str); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1640 | #endif |
| 1641 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1642 | return doXObject(pdfContext, canvas, value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1643 | } |
| 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1647 | static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1648 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1658 | static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1659 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1667 | static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1668 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1676 | static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1677 | 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.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1684 | static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1685 | return kNYI_PdfResult; |
| 1686 | } |
| 1687 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1688 | static void initPdfOperatorRenderes() { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1689 | 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 | |
| 1786 | class InitPdfOps { |
| 1787 | public: |
| 1788 | InitPdfOps() { |
| 1789 | initPdfOperatorRenderes(); |
| 1790 | } |
| 1791 | }; |
| 1792 | |
| 1793 | InitPdfOps gInitPdfOps; |
| 1794 | |
| 1795 | void 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 | |
| 1805 | PdfResult PdfMainLooper::consumeToken(PdfToken& token) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1806 | char keyword[256]; |
| 1807 | |
| 1808 | if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1809 | { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1810 | strncpy(keyword, token.fKeyword, token.fKeywordLength); |
| 1811 | keyword[token.fKeywordLength] = '\0'; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1812 | // TODO(edisonn): log trace flag (verbose, error, info, warning, ...) |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1813 | PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1814 | if (pdfOperatorRenderer) { |
| 1815 | // caller, main work is done by pdfOperatorRenderer(...) |
| 1816 | PdfTokenLooper* childLooper = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1817 | gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1818 | |
| 1819 | if (childLooper) { |
| 1820 | childLooper->setUp(this); |
| 1821 | childLooper->loop(); |
| 1822 | delete childLooper; |
| 1823 | } |
| 1824 | } else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1825 | gRenderStats[kUnsupported_PdfResult][keyword]++; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | else if (token.fType == kObject_TokenType) |
| 1829 | { |
| 1830 | fPdfContext->fObjectStack.push( token.fObject ); |
| 1831 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1832 | else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1833 | // TODO(edisonn): deine or use assert not reached |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1834 | return kIgnoreError_PdfResult; |
| 1835 | } |
| 1836 | return kOK_PdfResult; |
| 1837 | } |
| 1838 | |
| 1839 | void PdfMainLooper::loop() { |
| 1840 | PdfToken token; |
| 1841 | while (readToken(fTokenizer, &token)) { |
| 1842 | consumeToken(token); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) { |
| 1847 | //pdfContext.fInlineImage.fKeyValuePairs[key] = value; |
| 1848 | return kNYI_PdfResult; |
| 1849 | } |
| 1850 | |
| 1851 | void 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 | |
| 1870 | PdfResult 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 | |
| 1889 | PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) { |
| 1890 | return fParent->consumeToken(token); |
| 1891 | } |
| 1892 | |
| 1893 | void 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.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 1924 | PdfContext* gPdfContext = NULL; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1925 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 1926 | bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas) const { |
| 1927 | if (!fPdfDoc) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1928 | return false; |
| 1929 | } |
| 1930 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 1931 | 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. |
| 1944 | // TODO(edisonn): add DPI/scale/zoom. |
| 1945 | SkScalar z = SkIntToScalar(0); |
| 1946 | SkRect rect = fPdfDoc->MediaBox(page); |
| 1947 | SkScalar w = rect.width(); |
| 1948 | SkScalar h = rect.height(); |
| 1949 | |
| 1950 | SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(w, z), SkPoint::Make(w, h), SkPoint::Make(z, h)}; |
| 1951 | // SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 1952 | |
| 1953 | // TODO(edisonn): add flag for this app to create sourunding buffer zone |
| 1954 | // TODO(edisonn): add flagg for no clipping. |
| 1955 | // Use larger image to make sure we do not draw anything outside of page |
| 1956 | // could be used in tests. |
| 1957 | |
| 1958 | #ifdef PDF_DEBUG_3X |
| 1959 | 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)}; |
| 1960 | #else |
| 1961 | SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 1962 | #endif |
| 1963 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)}; |
| 1964 | //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)}; |
| 1965 | |
| 1966 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)}; |
| 1967 | //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)}; |
| 1968 | |
| 1969 | //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)}; |
| 1970 | //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)}; |
| 1971 | |
| 1972 | SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4)); |
| 1973 | SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix"); |
| 1974 | |
| 1975 | |
| 1976 | pdfContext.fGraphicsState.fMatrix = pdfContext.fOriginalMatrix; |
| 1977 | pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fMatrix; |
| 1978 | pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fMatrix; |
| 1979 | |
| 1980 | canvas->setMatrix(pdfContext.fOriginalMatrix); |
| 1981 | |
| 1982 | #ifndef PDF_DEBUG_NO_PAGE_CLIPING |
| 1983 | canvas->clipRect(SkRect::MakeXYWH(z, z, w, h), SkRegion::kIntersect_Op, true); |
| 1984 | #endif |
| 1985 | |
| 1986 | // erase with red before? |
| 1987 | // SkPaint paint; |
| 1988 | // paint.setColor(SK_ColorRED); |
| 1989 | // canvas->drawRect(rect, paint); |
| 1990 | |
| 1991 | PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas); |
| 1992 | looper.loop(); |
| 1993 | |
| 1994 | delete tokenizer; |
| 1995 | |
| 1996 | canvas->flush(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1997 | return true; |
| 1998 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 1999 | |
| 2000 | bool SkPdfRenderer::load(const SkString inputFileName) { |
| 2001 | unload(); |
| 2002 | |
| 2003 | // TODO(edisonn): create static function that could return NULL if there are errors |
| 2004 | fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str()); |
| 2005 | |
| 2006 | return fPdfDoc != NULL; |
| 2007 | } |
| 2008 | |
| 2009 | int SkPdfRenderer::pages() const { |
| 2010 | return fPdfDoc != NULL ? fPdfDoc->pages() : 0; |
| 2011 | } |
| 2012 | |
| 2013 | void SkPdfRenderer::unload() { |
| 2014 | delete fPdfDoc; |
| 2015 | fPdfDoc = NULL; |
| 2016 | } |
| 2017 | |
| 2018 | SkRect SkPdfRenderer::MediaBox(int page) const { |
| 2019 | SkASSERT(fPdfDoc); |
| 2020 | return fPdfDoc->MediaBox(page); |
| 2021 | } |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 2022 | |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 2023 | size_t SkPdfRenderer::bytesUsed() const { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 2024 | return fPdfDoc ? fPdfDoc->bytesUsed() : 0; |
| 2025 | } |
| 2026 | |