edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +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 "SkGraphics.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkOSFile.h" |
| 14 | #include "SkPicture.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkTypeface.h" |
| 17 | #include "SkTArray.h" |
| 18 | #include "picture_utils.h" |
| 19 | |
| 20 | #include <iostream> |
| 21 | #include <cstdio> |
| 22 | #include <stack> |
| 23 | |
| 24 | #include "podofo.h" |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 25 | using namespace PoDoFo; |
| 26 | |
| 27 | bool LongFromDictionary(const PdfMemDocument* pdfDoc, |
| 28 | const PdfDictionary& dict, |
| 29 | const char* key, |
| 30 | const char* abr, |
| 31 | long* data); |
| 32 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 33 | bool DoubleFromDictionary(const PdfMemDocument* pdfDoc, |
| 34 | const PdfDictionary& dict, |
| 35 | const char* key, |
| 36 | const char* abr, |
| 37 | double* data); |
| 38 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 39 | bool BoolFromDictionary(const PdfMemDocument* pdfDoc, |
| 40 | const PdfDictionary& dict, |
| 41 | const char* key, |
| 42 | const char* abr, |
| 43 | bool* data); |
| 44 | |
| 45 | bool NameFromDictionary(const PdfMemDocument* pdfDoc, |
| 46 | const PdfDictionary& dict, |
| 47 | const char* key, |
| 48 | const char* abr, |
| 49 | std::string* data); |
| 50 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 51 | bool StringFromDictionary(const PdfMemDocument* pdfDoc, |
| 52 | const PdfDictionary& dict, |
| 53 | const char* key, |
| 54 | const char* abr, |
| 55 | std::string* data); |
| 56 | |
| 57 | class SkPdfDictionary; |
| 58 | bool DictionaryFromDictionary(const PdfMemDocument* pdfDoc, |
| 59 | const PdfDictionary& dict, |
| 60 | const char* key, |
| 61 | const char* abr, |
| 62 | SkPdfDictionary** data); |
| 63 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 64 | template <typename T> |
| 65 | bool DictionaryFromDictionary2(const PdfMemDocument* pdfDoc, |
| 66 | const PdfDictionary& dict, |
| 67 | const char* key, |
| 68 | const char* abr, |
| 69 | T** data); |
| 70 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 71 | class SkPdfObject; |
| 72 | bool ObjectFromDictionary(const PdfMemDocument* pdfDoc, |
| 73 | const PdfDictionary& dict, |
| 74 | const char* key, |
| 75 | const char* abr, |
| 76 | SkPdfObject** data); |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 77 | |
| 78 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame^] | 79 | struct SkPdfFileSpec {}; |
| 80 | class SkPdfArray; |
| 81 | struct SkPdfStream {}; |
| 82 | struct SkPdfDate {}; |
| 83 | struct SkPdfTree {}; |
| 84 | struct SkPdfFunction {}; |
| 85 | |
| 86 | bool ArrayFromDictionary(const PdfMemDocument* pdfDoc, |
| 87 | const PdfDictionary& dict, |
| 88 | const char* key, |
| 89 | const char* abr, |
| 90 | SkPdfArray* data); |
| 91 | |
| 92 | |
| 93 | bool FileSpecFromDictionary(const PdfMemDocument* pdfDoc, |
| 94 | const PdfDictionary& dict, |
| 95 | const char* key, |
| 96 | const char* abr, |
| 97 | SkPdfFileSpec* data); |
| 98 | |
| 99 | |
| 100 | bool StreamFromDictionary(const PdfMemDocument* pdfDoc, |
| 101 | const PdfDictionary& dict, |
| 102 | const char* key, |
| 103 | const char* abr, |
| 104 | SkPdfStream* data); |
| 105 | |
| 106 | bool TreeFromDictionary(const PdfMemDocument* pdfDoc, |
| 107 | const PdfDictionary& dict, |
| 108 | const char* key, |
| 109 | const char* abr, |
| 110 | SkPdfTree** data); |
| 111 | |
| 112 | bool DateFromDictionary(const PdfMemDocument* pdfDoc, |
| 113 | const PdfDictionary& dict, |
| 114 | const char* key, |
| 115 | const char* abr, |
| 116 | SkPdfDate* data); |
| 117 | |
| 118 | bool SkRectFromDictionary(const PdfMemDocument* pdfDoc, |
| 119 | const PdfDictionary& dict, |
| 120 | const char* key, |
| 121 | const char* abr, |
| 122 | SkRect* data); |
| 123 | |
| 124 | bool FunctionFromDictionary(const PdfMemDocument* pdfDoc, |
| 125 | const PdfDictionary& dict, |
| 126 | const char* key, |
| 127 | const char* abr, |
| 128 | SkPdfFunction* data); |
| 129 | |
| 130 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 131 | #include "pdf_auto_gen.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 132 | |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame^] | 133 | bool ArrayFromDictionary(const PdfMemDocument* pdfDoc, |
| 134 | const PdfDictionary& dict, |
| 135 | const char* key, |
| 136 | const char* abr, |
| 137 | SkPdfArray* data) {return false;} |
| 138 | |
| 139 | bool FileSpecFromDictionary(const PdfMemDocument* pdfDoc, |
| 140 | const PdfDictionary& dict, |
| 141 | const char* key, |
| 142 | const char* abr, |
| 143 | SkPdfFileSpec* data) {return false;} |
| 144 | |
| 145 | bool StreamFromDictionary(const PdfMemDocument* pdfDoc, |
| 146 | const PdfDictionary& dict, |
| 147 | const char* key, |
| 148 | const char* abr, |
| 149 | SkPdfStream* data) {return false;} |
| 150 | |
| 151 | bool TreeFromDictionary(const PdfMemDocument* pdfDoc, |
| 152 | const PdfDictionary& dict, |
| 153 | const char* key, |
| 154 | const char* abr, |
| 155 | SkPdfTree** data) {return false;} |
| 156 | |
| 157 | bool DateFromDictionary(const PdfMemDocument* pdfDoc, |
| 158 | const PdfDictionary& dict, |
| 159 | const char* key, |
| 160 | const char* abr, |
| 161 | SkPdfDate* data) {return false;} |
| 162 | |
| 163 | bool SkRectFromDictionary(const PdfMemDocument* pdfDoc, |
| 164 | const PdfDictionary& dict, |
| 165 | const char* key, |
| 166 | const char* abr, |
| 167 | SkRect* data) {return false;} |
| 168 | |
| 169 | bool FunctionFromDictionary(const PdfMemDocument* pdfDoc, |
| 170 | const PdfDictionary& dict, |
| 171 | const char* key, |
| 172 | const char* abr, |
| 173 | SkPdfFunction* data) {return false;} |
| 174 | |
| 175 | |
| 176 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 177 | /* |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 178 | * TODO(edisonn): |
| 179 | * - encapsulate podofo in the pdf api so the skpdf does not know anything about podofo |
| 180 | * - ASAP so skp -> pdf -> png looks great |
| 181 | * - load gs/ especially smask and already known prop (skp) |
| 182 | * - use transparency (I think ca and CA ops) (skp) |
| 183 | * - all font types |
| 184 | * - word spacing |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 185 | * - load font for baidu.pdf |
| 186 | * - load font for youtube.pdf |
| 187 | */ |
| 188 | |
edisonn@google.com | e4d11be | 2013-06-12 19:53:42 +0000 | [diff] [blame] | 189 | //#define PDF_TRACE |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 190 | //#define PDF_TRACE_DIFF_IN_PNG |
| 191 | //#define PDF_DEBUG_NO_CLIPING |
| 192 | //#define PDF_DEBUG_NO_PAGE_CLIPING |
| 193 | //#define PDF_DEBUG_3X |
| 194 | |
| 195 | // TODO(edisonn): move in trace util. |
| 196 | #ifdef PDF_TRACE |
| 197 | static void SkTraceMatrix(const SkMatrix& matrix, const char* sz = "") { |
| 198 | printf("SkMatrix %s ", sz); |
| 199 | for (int i = 0 ; i < 9 ; i++) { |
| 200 | printf("%f ", SkScalarToDouble(matrix.get(i))); |
| 201 | } |
| 202 | printf("\n"); |
| 203 | } |
| 204 | #else |
| 205 | #define SkTraceMatrix(a,b) |
| 206 | #endif |
| 207 | |
| 208 | using namespace std; |
| 209 | using namespace PoDoFo; |
| 210 | |
| 211 | // Utilities |
| 212 | static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) { |
| 213 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 214 | |
| 215 | bitmap->allocPixels(); |
| 216 | bitmap->eraseColor(color); |
| 217 | } |
| 218 | |
| 219 | // TODO(edisonn): synonyms? DeviceRGB and RGB ... |
| 220 | int GetColorSpaceComponents(const std::string& colorSpace) { |
| 221 | if (colorSpace == "DeviceCMYK") { |
| 222 | return 4; |
| 223 | } else if (colorSpace == "DeviceGray" || |
| 224 | colorSpace == "CalGray" || |
| 225 | colorSpace == "Indexed") { |
| 226 | return 1; |
| 227 | } else if (colorSpace == "DeviceRGB" || |
| 228 | colorSpace == "CalRGB" || |
| 229 | colorSpace == "Lab") { |
| 230 | return 3; |
| 231 | } else { |
| 232 | return 0; |
| 233 | } |
| 234 | } |
| 235 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 236 | const PdfObject* resolveReferenceObject(const PdfMemDocument* pdfDoc, |
| 237 | const PdfObject* obj, |
| 238 | bool resolveOneElementArrays = false) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 239 | while (obj && (obj->IsReference() || (resolveOneElementArrays && |
| 240 | obj->IsArray() && |
| 241 | obj->GetArray().GetSize() == 1))) { |
| 242 | if (obj->IsReference()) { |
| 243 | // We need to force the non const, the only update we will do is for recurssion checks. |
| 244 | PdfReference& ref = (PdfReference&)obj->GetReference(); |
| 245 | obj = pdfDoc->GetObjects().GetObject(ref); |
| 246 | } else { |
| 247 | obj = &obj->GetArray()[0]; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return obj; |
| 252 | } |
| 253 | |
| 254 | static SkMatrix SkMatrixFromPdfMatrix(double array[6]) { |
| 255 | SkMatrix matrix; |
| 256 | matrix.setAll(SkDoubleToScalar(array[0]), |
| 257 | SkDoubleToScalar(array[2]), |
| 258 | SkDoubleToScalar(array[4]), |
| 259 | SkDoubleToScalar(array[1]), |
| 260 | SkDoubleToScalar(array[3]), |
| 261 | SkDoubleToScalar(array[5]), |
| 262 | SkDoubleToScalar(0), |
| 263 | SkDoubleToScalar(0), |
| 264 | SkDoubleToScalar(1)); |
| 265 | |
| 266 | return matrix; |
| 267 | } |
| 268 | |
| 269 | // TODO(edisonn): better class design. |
| 270 | struct PdfColorOperator { |
| 271 | std::string fColorSpace; // TODO(edisonn): use SkString |
| 272 | SkColor fColor; |
| 273 | // TODO(edisonn): add here other color space options. |
| 274 | |
| 275 | void setRGBColor(SkColor color) { |
| 276 | // TODO(edisonn): ASSERT DeviceRGB is the color space. |
| 277 | fColor = color; |
| 278 | } |
| 279 | // TODO(edisonn): double check the default values for all fields. |
| 280 | PdfColorOperator() : fColor(SK_ColorBLACK) {} |
| 281 | }; |
| 282 | |
| 283 | // TODO(edisonn): better class design. |
| 284 | struct PdfGraphicsState { |
| 285 | SkMatrix fMatrix; |
| 286 | SkMatrix fMatrixTm; |
| 287 | SkMatrix fMatrixTlm; |
| 288 | |
| 289 | double fCurPosX; |
| 290 | double fCurPosY; |
| 291 | |
| 292 | double fCurFontSize; |
| 293 | bool fTextBlock; |
| 294 | PdfFont* fCurFont; |
| 295 | SkPath fPath; |
| 296 | bool fPathClosed; |
| 297 | |
| 298 | // Clip that is applied after the drawing is done!!! |
| 299 | bool fHasClipPathToApply; |
| 300 | SkPath fClipPath; |
| 301 | |
| 302 | PdfColorOperator fStroking; |
| 303 | PdfColorOperator fNonStroking; |
| 304 | |
| 305 | double fLineWidth; |
| 306 | double fTextLeading; |
| 307 | double fWordSpace; |
| 308 | double fCharSpace; |
| 309 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 310 | SkPdfResourceDictionary fResources; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 311 | |
| 312 | SkBitmap fSMask; |
| 313 | |
| 314 | PdfGraphicsState() { |
| 315 | fCurPosX = 0.0; |
| 316 | fCurPosY = 0.0; |
| 317 | fCurFontSize = 0.0; |
| 318 | fTextBlock = false; |
| 319 | fCurFont = NULL; |
| 320 | fMatrix = SkMatrix::I(); |
| 321 | fMatrixTm = SkMatrix::I(); |
| 322 | fMatrixTlm = SkMatrix::I(); |
| 323 | fPathClosed = true; |
| 324 | fLineWidth = 0; |
| 325 | fTextLeading = 0; |
| 326 | fWordSpace = 0; |
| 327 | fCharSpace = 0; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 328 | fHasClipPathToApply = false; |
| 329 | } |
| 330 | }; |
| 331 | |
| 332 | // TODO(edisonn): better class design. |
| 333 | struct PdfInlineImage { |
| 334 | std::map<std::string, std::string> fKeyValuePairs; |
| 335 | std::string fImageData; |
| 336 | |
| 337 | }; |
| 338 | |
| 339 | // TODO(edisonn): better class design. |
| 340 | struct PdfContext { |
| 341 | std::stack<PdfVariant> fVarStack; |
| 342 | std::stack<PdfGraphicsState> fStateStack; |
| 343 | PdfGraphicsState fGraphicsState; |
| 344 | PoDoFo::PdfPage* fPdfPage; |
| 345 | PdfMemDocument* fPdfDoc; |
| 346 | SkMatrix fOriginalMatrix; |
| 347 | |
| 348 | PdfInlineImage fInlineImage; |
| 349 | |
| 350 | PdfContext() : fPdfPage(NULL), |
| 351 | fPdfDoc(NULL) {} |
| 352 | |
| 353 | }; |
| 354 | |
| 355 | // TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered. |
| 356 | enum PdfResult { |
| 357 | kOK_PdfResult, |
| 358 | kPartial_PdfResult, |
| 359 | kNYI_PdfResult, |
| 360 | kIgnoreError_PdfResult, |
| 361 | kError_PdfResult, |
| 362 | kUnsupported_PdfResult, |
| 363 | |
| 364 | kCount_PdfResult |
| 365 | }; |
| 366 | |
| 367 | struct PdfToken { |
| 368 | const char* pszToken; |
| 369 | PdfVariant var; |
| 370 | EPdfContentsType eType; |
| 371 | |
| 372 | PdfToken() : pszToken(NULL) {} |
| 373 | }; |
| 374 | |
| 375 | PdfContext* gPdfContext = NULL; |
| 376 | SkBitmap* gDumpBitmap = NULL; |
| 377 | SkCanvas* gDumpCanvas = NULL; |
| 378 | char gLastKeyword[100] = ""; |
| 379 | int gLastOpKeyword = -1; |
| 380 | char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX"; |
| 381 | int gReadOp = 0; |
| 382 | |
| 383 | |
| 384 | |
| 385 | bool hasVisualEffect(const char* pdfOp) { |
| 386 | return true; |
| 387 | if (*pdfOp == '\0') return false; |
| 388 | |
| 389 | char markedPdfOp[100] = ","; |
| 390 | strcat(markedPdfOp, pdfOp); |
| 391 | strcat(markedPdfOp, ","); |
| 392 | |
| 393 | return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL); |
| 394 | } |
| 395 | |
| 396 | // TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation. |
| 397 | static bool readToken(PdfContentsTokenizer* fTokenizer, PdfToken* token) { |
| 398 | bool ret = fTokenizer->ReadNext(token->eType, token->pszToken, token->var); |
| 399 | |
| 400 | gReadOp++; |
| 401 | |
| 402 | #ifdef PDF_TRACE_DIFF_IN_PNG |
| 403 | // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save |
| 404 | // the numbar and name of last operation, so the file name will reflect op that changed. |
| 405 | if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits. |
| 406 | gDumpCanvas->flush(); |
| 407 | |
| 408 | SkBitmap bitmap; |
| 409 | setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height()); |
| 410 | |
| 411 | memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize()); |
| 412 | |
| 413 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
| 414 | SkCanvas canvas(device); |
| 415 | |
| 416 | // draw context stuff here |
| 417 | SkPaint blueBorder; |
| 418 | blueBorder.setColor(SK_ColorBLUE); |
| 419 | blueBorder.setStyle(SkPaint::kStroke_Style); |
| 420 | blueBorder.setTextSize(SkDoubleToScalar(20)); |
| 421 | |
| 422 | SkString str; |
| 423 | |
| 424 | const SkClipStack* clipStack = gDumpCanvas->getClipStack(); |
| 425 | if (clipStack) { |
| 426 | SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 427 | const SkClipStack::Element* elem; |
| 428 | double y = 0; |
| 429 | int total = 0; |
| 430 | while (elem = iter.next()) { |
| 431 | total++; |
| 432 | y += 30; |
| 433 | |
| 434 | switch (elem->getType()) { |
| 435 | case SkClipStack::Element::kRect_Type: |
| 436 | canvas.drawRect(elem->getRect(), blueBorder); |
| 437 | canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 438 | break; |
| 439 | case SkClipStack::Element::kPath_Type: |
| 440 | canvas.drawPath(elem->getPath(), blueBorder); |
| 441 | canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 442 | break; |
| 443 | case SkClipStack::Element::kEmpty_Type: |
| 444 | canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 445 | break; |
| 446 | default: |
| 447 | canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | y += 30; |
| 453 | str.printf("Number of clips in stack: %i", total); |
| 454 | canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 455 | } |
| 456 | |
| 457 | const SkRegion& clipRegion = gDumpCanvas->getTotalClip(); |
| 458 | SkPath clipPath; |
| 459 | if (clipRegion.getBoundaryPath(&clipPath)) { |
| 460 | SkPaint redBorder; |
| 461 | redBorder.setColor(SK_ColorRED); |
| 462 | redBorder.setStyle(SkPaint::kStroke_Style); |
| 463 | canvas.drawPath(clipPath, redBorder); |
| 464 | } |
| 465 | |
| 466 | canvas.flush(); |
| 467 | |
| 468 | SkString out; |
| 469 | |
| 470 | // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack, |
| 471 | // ... and other properties, to be able to debug th code easily |
| 472 | |
| 473 | out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword); |
| 474 | SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 475 | } |
| 476 | |
| 477 | if (token->eType == ePdfContentsType_Keyword) { |
| 478 | strcpy(gLastKeyword, token->pszToken); |
| 479 | gLastOpKeyword = gReadOp; |
| 480 | } else { |
| 481 | strcpy(gLastKeyword, ""); |
| 482 | } |
| 483 | #endif |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | // TODO(edisonn): Document PdfTokenLooper and subclasses. |
| 489 | class PdfTokenLooper { |
| 490 | protected: |
| 491 | PdfTokenLooper* fParent; |
| 492 | PdfContentsTokenizer* fTokenizer; |
| 493 | PdfContext* fPdfContext; |
| 494 | SkCanvas* fCanvas; |
| 495 | |
| 496 | public: |
| 497 | PdfTokenLooper(PdfTokenLooper* parent, |
| 498 | PdfContentsTokenizer* tokenizer, |
| 499 | PdfContext* pdfContext, |
| 500 | SkCanvas* canvas) |
| 501 | : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {} |
| 502 | |
| 503 | virtual PdfResult consumeToken(PdfToken& token) = 0; |
| 504 | virtual void loop() = 0; |
| 505 | |
| 506 | void setUp(PdfTokenLooper* parent) { |
| 507 | fParent = parent; |
| 508 | fTokenizer = parent->fTokenizer; |
| 509 | fPdfContext = parent->fPdfContext; |
| 510 | fCanvas = parent->fCanvas; |
| 511 | } |
| 512 | }; |
| 513 | |
| 514 | class PdfMainLooper : public PdfTokenLooper { |
| 515 | public: |
| 516 | PdfMainLooper(PdfTokenLooper* parent, |
| 517 | PdfContentsTokenizer* tokenizer, |
| 518 | PdfContext* pdfContext, |
| 519 | SkCanvas* canvas) |
| 520 | : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {} |
| 521 | |
| 522 | virtual PdfResult consumeToken(PdfToken& token); |
| 523 | virtual void loop(); |
| 524 | }; |
| 525 | |
| 526 | class PdfInlineImageLooper : public PdfTokenLooper { |
| 527 | public: |
| 528 | PdfInlineImageLooper() |
| 529 | : PdfTokenLooper(NULL, NULL, NULL, NULL) {} |
| 530 | |
| 531 | virtual PdfResult consumeToken(PdfToken& token); |
| 532 | virtual void loop(); |
| 533 | PdfResult done(); |
| 534 | }; |
| 535 | |
| 536 | class PdfCompatibilitySectionLooper : public PdfTokenLooper { |
| 537 | public: |
| 538 | PdfCompatibilitySectionLooper() |
| 539 | : PdfTokenLooper(NULL, NULL, NULL, NULL) {} |
| 540 | |
| 541 | virtual PdfResult consumeToken(PdfToken& token); |
| 542 | virtual void loop(); |
| 543 | }; |
| 544 | |
| 545 | typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**); |
| 546 | |
| 547 | map<std::string, PdfOperatorRenderer> gPdfOps; |
| 548 | |
| 549 | map<std::string, int> gRenderStats[kCount_PdfResult]; |
| 550 | |
| 551 | char* gRenderStatsNames[kCount_PdfResult] = { |
| 552 | "Success", |
| 553 | "Partially implemented", |
| 554 | "Not yet implemented", |
| 555 | "Ignore Error", |
| 556 | "Error", |
| 557 | "Unsupported/Unknown" |
| 558 | }; |
| 559 | |
| 560 | struct SkPdfStandardFont { |
| 561 | const char* fName; |
| 562 | bool fIsBold; |
| 563 | bool fIsItalic; |
| 564 | }; |
| 565 | |
| 566 | static map<std::string, SkPdfStandardFont>& getStandardFonts() { |
| 567 | static std::map<std::string, SkPdfStandardFont> gPdfStandardFonts; |
| 568 | |
| 569 | // TODO (edisonn): , vs - ? what does it mean? |
| 570 | // TODO (edisonn): MT, PS, Oblique=italic?, ... what does it mean? |
| 571 | if (gPdfStandardFonts.empty()) { |
| 572 | gPdfStandardFonts["Arial"] = {"Arial", false, false}; |
| 573 | gPdfStandardFonts["Arial,Bold"] = {"Arial", true, false}; |
| 574 | gPdfStandardFonts["Arial,BoldItalic"] = {"Arial", true, true}; |
| 575 | gPdfStandardFonts["Arial,Italic"] = {"Arial", false, true}; |
| 576 | gPdfStandardFonts["Arial-Bold"] = {"Arial", true, false}; |
| 577 | gPdfStandardFonts["Arial-BoldItalic"] = {"Arial", true, true}; |
| 578 | gPdfStandardFonts["Arial-BoldItalicMT"] = {"Arial", true, true}; |
| 579 | gPdfStandardFonts["Arial-BoldMT"] = {"Arial", true, false}; |
| 580 | gPdfStandardFonts["Arial-Italic"] = {"Arial", false, true}; |
| 581 | gPdfStandardFonts["Arial-ItalicMT"] = {"Arial", false, true}; |
| 582 | gPdfStandardFonts["ArialMT"] = {"Arial", false, false}; |
| 583 | gPdfStandardFonts["Courier"] = {"Courier New", false, false}; |
| 584 | gPdfStandardFonts["Courier,Bold"] = {"Courier New", true, false}; |
| 585 | gPdfStandardFonts["Courier,BoldItalic"] = {"Courier New", true, true}; |
| 586 | gPdfStandardFonts["Courier,Italic"] = {"Courier New", false, true}; |
| 587 | gPdfStandardFonts["Courier-Bold"] = {"Courier New", true, false}; |
| 588 | gPdfStandardFonts["Courier-BoldOblique"] = {"Courier New", true, true}; |
| 589 | gPdfStandardFonts["Courier-Oblique"] = {"Courier New", false, true}; |
| 590 | gPdfStandardFonts["CourierNew"] = {"Courier New", false, false}; |
| 591 | gPdfStandardFonts["CourierNew,Bold"] = {"Courier New", true, false}; |
| 592 | gPdfStandardFonts["CourierNew,BoldItalic"] = {"Courier New", true, true}; |
| 593 | gPdfStandardFonts["CourierNew,Italic"] = {"Courier New", false, true}; |
| 594 | gPdfStandardFonts["CourierNew-Bold"] = {"Courier New", true, false}; |
| 595 | gPdfStandardFonts["CourierNew-BoldItalic"] = {"Courier New", true, true}; |
| 596 | gPdfStandardFonts["CourierNew-Italic"] = {"Courier New", false, true}; |
| 597 | gPdfStandardFonts["CourierNewPS-BoldItalicMT"] = {"Courier New", true, true}; |
| 598 | gPdfStandardFonts["CourierNewPS-BoldMT"] = {"Courier New", true, false}; |
| 599 | gPdfStandardFonts["CourierNewPS-ItalicMT"] = {"Courier New", false, true}; |
| 600 | gPdfStandardFonts["CourierNewPSMT"] = {"Courier New", false, false}; |
| 601 | gPdfStandardFonts["Helvetica"] = {"Helvetica", false, false}; |
| 602 | gPdfStandardFonts["Helvetica,Bold"] = {"Helvetica", true, false}; |
| 603 | gPdfStandardFonts["Helvetica,BoldItalic"] = {"Helvetica", true, true}; |
| 604 | gPdfStandardFonts["Helvetica,Italic"] = {"Helvetica", false, true}; |
| 605 | gPdfStandardFonts["Helvetica-Bold"] = {"Helvetica", true, false}; |
| 606 | gPdfStandardFonts["Helvetica-BoldItalic"] = {"Helvetica", true, true}; |
| 607 | gPdfStandardFonts["Helvetica-BoldOblique"] = {"Helvetica", true, true}; |
| 608 | gPdfStandardFonts["Helvetica-Italic"] = {"Helvetica", false, true}; |
| 609 | gPdfStandardFonts["Helvetica-Oblique"] = {"Helvetica", false, true}; |
| 610 | gPdfStandardFonts["Times-Bold"] = {"Times", true, false}; |
| 611 | gPdfStandardFonts["Times-BoldItalic"] = {"Times", true, true}; |
| 612 | gPdfStandardFonts["Times-Italic"] = {"Times", false, true}; |
| 613 | gPdfStandardFonts["Times-Roman"] = {"Times New Roman", false, false}; |
| 614 | gPdfStandardFonts["TimesNewRoman"] = {"Times New Roman", false, false}; |
| 615 | gPdfStandardFonts["TimesNewRoman,Bold"] = {"Times New Roman", true, false}; |
| 616 | gPdfStandardFonts["TimesNewRoman,BoldItalic"] = {"Times New Roman", true, true}; |
| 617 | gPdfStandardFonts["TimesNewRoman,Italic"] = {"Times New Roman", false, true}; |
| 618 | gPdfStandardFonts["TimesNewRoman-Bold"] = {"Times New Roman", true, false}; |
| 619 | gPdfStandardFonts["TimesNewRoman-BoldItalic"] = {"Times New Roman", true, true}; |
| 620 | gPdfStandardFonts["TimesNewRoman-Italic"] = {"Times New Roman", false, true}; |
| 621 | gPdfStandardFonts["TimesNewRomanPS"] = {"Times New Roman", false, false}; |
| 622 | gPdfStandardFonts["TimesNewRomanPS-Bold"] = {"Times New Roman", true, false}; |
| 623 | gPdfStandardFonts["TimesNewRomanPS-BoldItalic"] = {"Times New Roman", true, true}; |
| 624 | gPdfStandardFonts["TimesNewRomanPS-BoldItalicMT"] = {"Times New Roman", true, true}; |
| 625 | gPdfStandardFonts["TimesNewRomanPS-BoldMT"] = {"Times New Roman", true, false}; |
| 626 | gPdfStandardFonts["TimesNewRomanPS-Italic"] = {"Times New Roman", false, true}; |
| 627 | gPdfStandardFonts["TimesNewRomanPS-ItalicMT"] = {"Times New Roman", false, true}; |
| 628 | gPdfStandardFonts["TimesNewRomanPSMT"] = {"Times New Roman", false, false}; |
| 629 | } |
| 630 | |
| 631 | return gPdfStandardFonts; |
| 632 | } |
| 633 | |
| 634 | static SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic) { |
| 635 | map<std::string, SkPdfStandardFont>& standardFontMap = getStandardFonts(); |
| 636 | |
| 637 | if (standardFontMap.find(fontName) != standardFontMap.end()) { |
| 638 | SkPdfStandardFont fontData = standardFontMap[fontName]; |
| 639 | |
| 640 | // TODO(edisonn): How does the bold/italic specified in standard definition combines with |
| 641 | // the one in /font key? use OR for now. |
| 642 | bold = bold || fontData.fIsBold; |
| 643 | italic = italic || fontData.fIsItalic; |
| 644 | |
| 645 | SkTypeface* typeface = SkTypeface::CreateFromName( |
| 646 | fontData.fName, |
| 647 | SkTypeface::Style((bold ? SkTypeface::kBold : 0) | |
| 648 | (italic ? SkTypeface::kItalic : 0))); |
| 649 | if (typeface) { |
| 650 | typeface->ref(); |
| 651 | } |
| 652 | return typeface; |
| 653 | } |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | static SkTypeface* SkTypefaceFromPdfFont(PdfFont* font) { |
| 658 | PdfObject* fontObject = font->GetObject(); |
| 659 | |
| 660 | PdfObject* pBaseFont = NULL; |
| 661 | // TODO(edisonn): warning, PoDoFo has a bug in PdfFont constructor, does not call InitVars() |
| 662 | // for now fixed locally. |
| 663 | pBaseFont = fontObject->GetIndirectKey( "BaseFont" ); |
| 664 | const char* pszBaseFontName = pBaseFont->GetName().GetName().c_str(); |
| 665 | |
| 666 | #ifdef PDF_TRACE |
| 667 | std::string str; |
| 668 | fontObject->ToString(str); |
| 669 | printf("Base Font Name: %s\n", pszBaseFontName); |
| 670 | printf("Font Object Data: %s\n", str.c_str()); |
| 671 | #endif |
| 672 | |
| 673 | SkTypeface* typeface = SkTypefaceFromPdfStandardFont(pszBaseFontName, font->IsBold(), font->IsItalic()); |
| 674 | |
| 675 | if (typeface != NULL) { |
| 676 | return typeface; |
| 677 | } |
| 678 | |
| 679 | char name[1000]; |
| 680 | // HACK |
| 681 | strncpy(name, pszBaseFontName, 1000); |
| 682 | char* comma = strstr(name, ","); |
| 683 | char* dash = strstr(name, "-"); |
| 684 | if (comma) *comma = '\0'; |
| 685 | if (dash) *dash = '\0'; |
| 686 | |
| 687 | typeface = SkTypeface::CreateFromName( |
| 688 | name, |
| 689 | SkTypeface::Style((font->IsBold() ? SkTypeface::kBold : 0) | |
| 690 | (font->IsItalic() ? SkTypeface::kItalic : 0))); |
| 691 | |
| 692 | if (typeface != NULL) { |
| 693 | #ifdef PDF_TRACE |
| 694 | printf("HACKED FONT found %s\n", name); |
| 695 | #endif |
| 696 | return typeface; |
| 697 | } |
| 698 | |
| 699 | #ifdef PDF_TRACE |
| 700 | printf("FONT_NOT_FOUND %s\n", pszBaseFontName); |
| 701 | #endif |
| 702 | |
| 703 | // TODO(edisonn): Report Warning, NYI |
| 704 | return SkTypeface::CreateFromName( |
| 705 | "Times New Roman", |
| 706 | SkTypeface::Style((font->IsBold() ? SkTypeface::kBold : 0) | |
| 707 | (font->IsItalic() ? SkTypeface::kItalic : 0))); |
| 708 | } |
| 709 | |
| 710 | // TODO(edisonn): move this code in podofo, so we don't have to fix the font. |
| 711 | // This logic needs to be moved in PdfEncodingObjectFactory::CreateEncoding |
| 712 | std::map<PdfFont*, PdfCMapEncoding*> gFontsFixed; |
| 713 | PdfEncoding* FixPdfFont(PdfContext* pdfContext, PdfFont* fCurFont) { |
| 714 | // TODO(edisonn): and is Identity-H |
| 715 | if (gFontsFixed.find(fCurFont) == gFontsFixed.end()) { |
| 716 | if (fCurFont->GetObject()->IsDictionary() && fCurFont->GetObject()->GetDictionary().HasKey(PdfName("ToUnicode"))) { |
| 717 | PdfCMapEncoding* enc = new PdfCMapEncoding( |
| 718 | fCurFont->GetObject(), |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 719 | (PdfObject*)resolveReferenceObject(pdfContext->fPdfDoc, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 720 | fCurFont->GetObject()->GetDictionary().GetKey(PdfName("ToUnicode"))), |
| 721 | PdfCMapEncoding::eBaseEncoding_Identity); // todo, read the base encoding |
| 722 | gFontsFixed[fCurFont] = enc; |
| 723 | return enc; |
| 724 | } |
| 725 | |
| 726 | return NULL; |
| 727 | } |
| 728 | |
| 729 | return gFontsFixed[fCurFont]; |
| 730 | } |
| 731 | |
| 732 | PdfResult DrawText(PdfContext* pdfContext, |
| 733 | PdfFont* fCurFont, |
| 734 | const PdfString& rString, |
| 735 | SkCanvas* canvas) |
| 736 | { |
| 737 | if (!fCurFont) |
| 738 | { |
| 739 | // TODO(edisonn): ignore the error, use the default font? |
| 740 | return kError_PdfResult; |
| 741 | } |
| 742 | |
| 743 | const PdfEncoding* enc = FixPdfFont(pdfContext, fCurFont); |
| 744 | bool cMapUnicodeFont = enc != NULL; |
| 745 | if (!enc) enc = fCurFont->GetEncoding(); |
| 746 | if (!enc) |
| 747 | { |
| 748 | // TODO(edisonn): Can we recover from this error? |
| 749 | return kError_PdfResult; |
| 750 | } |
| 751 | |
| 752 | PdfString r2 = rString; |
| 753 | PdfString unicode; |
| 754 | |
| 755 | if (cMapUnicodeFont) { |
| 756 | r2 = PdfString((pdf_utf16be*)rString.GetString(), rString.GetLength() / 2); |
| 757 | } |
| 758 | |
| 759 | unicode = enc->ConvertToUnicode( r2, fCurFont ); |
| 760 | |
| 761 | #ifdef PDF_TRACE |
| 762 | printf("%i %i ? %c rString.len = %i\n", (int)rString.GetString()[0], (int)rString.GetString()[1], (int)rString.GetString()[1], rString.GetLength()); |
| 763 | printf("%i %i %i %i %c unicode.len = %i\n", (int)unicode.GetString()[0], (int)unicode.GetString()[1], (int)unicode.GetString()[2], (int)unicode.GetString()[3], (int)unicode.GetString()[0], unicode.GetLength()); |
| 764 | #endif |
| 765 | |
| 766 | SkPaint paint; |
| 767 | // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0? |
| 768 | // Or maybe just not call setTextSize at all? |
| 769 | if (pdfContext->fGraphicsState.fCurFontSize != 0) { |
| 770 | paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize)); |
| 771 | } |
| 772 | if (fCurFont->GetFontScale() != 0) { |
| 773 | paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0)); |
| 774 | } |
| 775 | paint.setColor(pdfContext->fGraphicsState.fNonStroking.fColor); |
| 776 | |
| 777 | paint.setTypeface(SkTypefaceFromPdfFont(fCurFont)); |
| 778 | |
| 779 | paint.setAntiAlias(true); |
| 780 | // TODO(edisonn): paint.setStyle(...); |
| 781 | |
| 782 | canvas->save(); |
| 783 | SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm; |
| 784 | |
| 785 | #if 0 |
| 786 | // Reverse now the space, otherwise the text is upside down. |
| 787 | SkScalar z = SkIntToScalar(0); |
| 788 | SkScalar one = SkIntToScalar(1); |
| 789 | |
| 790 | SkPoint normalSpace1[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)}; |
| 791 | SkPoint mirrorSpace1[4]; |
| 792 | pdfContext->fGraphicsState.fMatrixTm.mapPoints(mirrorSpace1, normalSpace1, 4); |
| 793 | |
| 794 | SkPoint normalSpace2[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, -one), SkPoint::Make(z, -one)}; |
| 795 | SkPoint mirrorSpace2[4]; |
| 796 | pdfContext->fGraphicsState.fMatrixTm.mapPoints(mirrorSpace2, normalSpace2, 4); |
| 797 | |
| 798 | #ifdef PDF_TRACE |
| 799 | printf("mirror1[0], x = %f y = %f\n", SkScalarToDouble(mirrorSpace1[0].x()), SkScalarToDouble(mirrorSpace1[0].y())); |
| 800 | printf("mirror1[1], x = %f y = %f\n", SkScalarToDouble(mirrorSpace1[1].x()), SkScalarToDouble(mirrorSpace1[1].y())); |
| 801 | printf("mirror1[2], x = %f y = %f\n", SkScalarToDouble(mirrorSpace1[2].x()), SkScalarToDouble(mirrorSpace1[2].y())); |
| 802 | printf("mirror1[3], x = %f y = %f\n", SkScalarToDouble(mirrorSpace1[3].x()), SkScalarToDouble(mirrorSpace1[3].y())); |
| 803 | printf("mirror2[0], x = %f y = %f\n", SkScalarToDouble(mirrorSpace2[0].x()), SkScalarToDouble(mirrorSpace2[0].y())); |
| 804 | printf("mirror2[1], x = %f y = %f\n", SkScalarToDouble(mirrorSpace2[1].x()), SkScalarToDouble(mirrorSpace2[1].y())); |
| 805 | printf("mirror2[2], x = %f y = %f\n", SkScalarToDouble(mirrorSpace2[2].x()), SkScalarToDouble(mirrorSpace2[2].y())); |
| 806 | printf("mirror2[3], x = %f y = %f\n", SkScalarToDouble(mirrorSpace2[3].x()), SkScalarToDouble(mirrorSpace2[3].y())); |
| 807 | #endif |
| 808 | |
| 809 | SkMatrix mirror; |
| 810 | SkASSERT(mirror.setPolyToPoly(mirrorSpace1, mirrorSpace2, 4)); |
| 811 | |
| 812 | // TODO(edisonn): text positioning wrong right now. Need to get matrix operations right. |
| 813 | matrix.preConcat(mirror); |
| 814 | canvas->setMatrix(matrix); |
| 815 | #endif |
| 816 | |
| 817 | SkPoint point1; |
| 818 | pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1); |
| 819 | |
| 820 | SkMatrix mirror; |
| 821 | mirror.setTranslate(0, -point1.y()); |
| 822 | // TODO(edisonn): fix rotated text, and skewed too |
| 823 | mirror.postScale(SK_Scalar1, -SK_Scalar1); |
| 824 | // TODO(edisonn): post rotate, skew |
| 825 | mirror.postTranslate(0, point1.y()); |
| 826 | |
| 827 | matrix.postConcat(mirror); |
| 828 | |
| 829 | canvas->setMatrix(matrix); |
| 830 | |
| 831 | SkTraceMatrix(matrix, "mirrored"); |
| 832 | |
| 833 | #ifdef PDF_TRACE |
| 834 | SkPoint point; |
| 835 | pdfContext->fGraphicsState.fMatrixTm.mapXY(SkDoubleToScalar(0), SkDoubleToScalar(0), &point); |
| 836 | printf("Original SkCanvas resolved coordinates, x = %f y = %f\n", SkScalarToDouble(point.x()), SkScalarToDouble(point.y())); |
| 837 | matrix.mapXY(SkDoubleToScalar(0), SkDoubleToScalar(0), &point); |
| 838 | printf("Mirored SkCanvas resolved coordinates, x = %f y = %f\n", SkScalarToDouble(point.x()), SkScalarToDouble(point.y())); |
| 839 | #endif |
| 840 | |
| 841 | // TODO(edisonn): remove this call once we load the font properly |
| 842 | // The extra * will show that we got at least the text positioning right |
| 843 | // even if font failed to be loaded |
| 844 | // canvas->drawText(".", 1, SkDoubleToScalar(-5.0), SkDoubleToScalar(0.0), paint); |
| 845 | |
| 846 | |
| 847 | |
| 848 | // TODO(edisonn): use character and word spacing .. add utility function |
| 849 | if (cMapUnicodeFont) { |
| 850 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 851 | SkScalar textWidth = paint.measureText(unicode.GetString(), unicode.GetLength()); |
| 852 | pdfContext->fGraphicsState.fMatrixTm.preTranslate(textWidth, SkDoubleToScalar(0.0)); |
| 853 | canvas->drawText(unicode.GetString(), unicode.GetLength(), SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), paint); |
| 854 | } |
| 855 | else { |
| 856 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 857 | SkScalar textWidth = paint.measureText(unicode.GetStringUtf8().c_str(), strlen(unicode.GetStringUtf8().c_str())); |
| 858 | pdfContext->fGraphicsState.fMatrixTm.preTranslate(textWidth, SkDoubleToScalar(0.0)); |
| 859 | canvas->drawText(unicode.GetStringUtf8().c_str(), strlen(unicode.GetStringUtf8().c_str()), SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), paint); |
| 860 | } |
| 861 | |
| 862 | // paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 863 | // unsigned char ch = *(unicode.GetString() + 3); |
| 864 | // if ((ch & 0xC0) != 0x80 && ch < 0x80) { |
| 865 | // printf("x%i", ch); |
| 866 | // SkScalar textWidth = paint.measureText(&ch, 1); |
| 867 | // pdfContext->fGraphicsState.fMatrixTm.preTranslate(textWidth, SkDoubleToScalar(0.0)); |
| 868 | // canvas->drawText(&ch, 1, SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), paint); |
| 869 | // } |
| 870 | |
| 871 | canvas->restore(); |
| 872 | |
| 873 | |
| 874 | return kPartial_PdfResult; |
| 875 | } |
| 876 | |
| 877 | // TODO(edisonn): create header files with declarations! |
| 878 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 879 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 880 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 881 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 882 | |
| 883 | // TODO(edisonn): deal with synonyms (/BPC == /BitsPerComponent), here or in GetKey? |
| 884 | // Always pass long form in key, and have a map of long -> short key |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 885 | bool LongFromDictionary(const PdfMemDocument* pdfDoc, |
| 886 | const PdfDictionary& dict, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 887 | const char* key, |
| 888 | long* data) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 889 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 890 | dict.GetKey(PdfName(key))); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 891 | |
| 892 | if (value == NULL || !value->IsNumber()) { |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | *data = value->GetNumber(); |
| 897 | return true; |
| 898 | } |
| 899 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 900 | bool LongFromDictionary(const PdfMemDocument* pdfDoc, |
| 901 | const PdfDictionary& dict, |
| 902 | const char* key, |
| 903 | const char* abr, |
| 904 | long* data) { |
| 905 | if (LongFromDictionary(pdfDoc, dict, key, data)) return true; |
| 906 | if (abr == NULL || *abr == '\0') return false; |
| 907 | return LongFromDictionary(pdfDoc, dict, abr, data); |
| 908 | } |
| 909 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 910 | bool DoubleFromDictionary(const PdfMemDocument* pdfDoc, |
| 911 | const PdfDictionary& dict, |
| 912 | const char* key, |
| 913 | double* data) { |
| 914 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 915 | dict.GetKey(PdfName(key))); |
| 916 | |
| 917 | if (value == NULL || !value->IsReal()) { |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | *data = value->GetReal(); |
| 922 | return true; |
| 923 | } |
| 924 | |
| 925 | bool DoubleFromDictionary(const PdfMemDocument* pdfDoc, |
| 926 | const PdfDictionary& dict, |
| 927 | const char* key, |
| 928 | const char* abr, |
| 929 | double* data) { |
| 930 | if (DoubleFromDictionary(pdfDoc, dict, key, data)) return true; |
| 931 | if (abr == NULL || *abr == '\0') return false; |
| 932 | return DoubleFromDictionary(pdfDoc, dict, abr, data); |
| 933 | } |
| 934 | |
| 935 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 936 | bool BoolFromDictionary(const PdfMemDocument* pdfDoc, |
| 937 | const PdfDictionary& dict, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 938 | const char* key, |
| 939 | bool* data) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 940 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 941 | dict.GetKey(PdfName(key))); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 942 | |
| 943 | if (value == NULL || !value->IsBool()) { |
| 944 | return false; |
| 945 | } |
| 946 | |
| 947 | *data = value->GetBool(); |
| 948 | return true; |
| 949 | } |
| 950 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 951 | bool BoolFromDictionary(const PdfMemDocument* pdfDoc, |
| 952 | const PdfDictionary& dict, |
| 953 | const char* key, |
| 954 | const char* abr, |
| 955 | bool* data) { |
| 956 | if (BoolFromDictionary(pdfDoc, dict, key, data)) return true; |
| 957 | if (abr == NULL || *abr == '\0') return false; |
| 958 | return BoolFromDictionary(pdfDoc, dict, abr, data); |
| 959 | } |
| 960 | |
| 961 | bool NameFromDictionary(const PdfMemDocument* pdfDoc, |
| 962 | const PdfDictionary& dict, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 963 | const char* key, |
| 964 | std::string* data) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 965 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 966 | dict.GetKey(PdfName(key)), |
| 967 | true); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 968 | if (value == NULL || !value->IsName()) { |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | *data = value->GetName().GetName(); |
| 973 | return true; |
| 974 | } |
| 975 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 976 | bool NameFromDictionary(const PdfMemDocument* pdfDoc, |
| 977 | const PdfDictionary& dict, |
| 978 | const char* key, |
| 979 | const char* abr, |
| 980 | std::string* data) { |
| 981 | if (NameFromDictionary(pdfDoc, dict, key, data)) return true; |
| 982 | if (abr == NULL || *abr == '\0') return false; |
| 983 | return NameFromDictionary(pdfDoc, dict, abr, data); |
| 984 | } |
| 985 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 986 | bool StringFromDictionary(const PdfMemDocument* pdfDoc, |
| 987 | const PdfDictionary& dict, |
| 988 | const char* key, |
| 989 | std::string* data) { |
| 990 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 991 | dict.GetKey(PdfName(key)), |
| 992 | true); |
| 993 | if (value == NULL || (!value->IsString() && !value->IsHexString())) { |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | *data = value->GetString().GetString(); |
| 998 | return true; |
| 999 | } |
| 1000 | |
| 1001 | bool StringFromDictionary(const PdfMemDocument* pdfDoc, |
| 1002 | const PdfDictionary& dict, |
| 1003 | const char* key, |
| 1004 | const char* abr, |
| 1005 | std::string* data) { |
| 1006 | if (StringFromDictionary(pdfDoc, dict, key, data)) return true; |
| 1007 | if (abr == NULL || *abr == '\0') return false; |
| 1008 | return StringFromDictionary(pdfDoc, dict, abr, data); |
| 1009 | } |
| 1010 | |
| 1011 | bool DictionaryFromDictionary(const PdfMemDocument* pdfDoc, |
| 1012 | const PdfDictionary& dict, |
| 1013 | const char* key, |
| 1014 | SkPdfDictionary** data) { |
| 1015 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 1016 | dict.GetKey(PdfName(key)), |
| 1017 | true); |
| 1018 | if (value == NULL || !value->IsDictionary()) { |
| 1019 | return false; |
| 1020 | } |
| 1021 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1022 | return PodofoMapper::map(*pdfDoc, *value, (SkPdfObject**)data); |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | bool DictionaryFromDictionary(const PdfMemDocument* pdfDoc, |
| 1026 | const PdfDictionary& dict, |
| 1027 | const char* key, |
| 1028 | const char* abr, |
| 1029 | SkPdfDictionary** data) { |
| 1030 | if (DictionaryFromDictionary(pdfDoc, dict, key, data)) return true; |
| 1031 | if (abr == NULL || *abr == '\0') return false; |
| 1032 | return DictionaryFromDictionary(pdfDoc, dict, abr, data); |
| 1033 | } |
| 1034 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1035 | template <typename T> |
| 1036 | bool DictionaryFromDictionary2(const PdfMemDocument* pdfDoc, |
| 1037 | const PdfDictionary& dict, |
| 1038 | const char* key, |
| 1039 | SkPdfDictionary** data) { |
| 1040 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 1041 | dict.GetKey(PdfName(key)), |
| 1042 | true); |
| 1043 | if (value == NULL || !value->IsDictionary()) { |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | return PodofoMapper::map(*pdfDoc, *value, (T**)data); |
| 1048 | } |
| 1049 | |
| 1050 | template <typename T> |
| 1051 | bool DictionaryFromDictionary2(const PdfMemDocument* pdfDoc, |
| 1052 | const PdfDictionary& dict, |
| 1053 | const char* key, |
| 1054 | const char* abr, |
| 1055 | T** data) { |
| 1056 | if (DictionaryFromDictionary2<T>(pdfDoc, dict, key, data)) return true; |
| 1057 | if (abr == NULL || *abr == '\0') return false; |
| 1058 | return DictionaryFromDictionary2<T>(pdfDoc, dict, abr, data); |
| 1059 | } |
| 1060 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1061 | bool ObjectFromDictionary(const PdfMemDocument* pdfDoc, |
| 1062 | const PdfDictionary& dict, |
| 1063 | const char* key, |
| 1064 | SkPdfObject** data) { |
| 1065 | const PdfObject* value = resolveReferenceObject(pdfDoc, |
| 1066 | dict.GetKey(PdfName(key)), |
| 1067 | true); |
| 1068 | if (value == NULL) { |
| 1069 | return false; |
| 1070 | } |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1071 | return PodofoMapper::map(*pdfDoc, *value, data); |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | bool ObjectFromDictionary(const PdfMemDocument* pdfDoc, |
| 1075 | const PdfDictionary& dict, |
| 1076 | const char* key, |
| 1077 | const char* abr, |
| 1078 | SkPdfObject** data) { |
| 1079 | if (ObjectFromDictionary(pdfDoc, dict, key, data)) return true; |
| 1080 | if (abr == NULL || *abr == '\0') return false; |
| 1081 | return ObjectFromDictionary(pdfDoc, dict, abr, data); |
| 1082 | } |
| 1083 | |
| 1084 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1085 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1086 | // TODO(edisonn): perf!!! |
| 1087 | |
| 1088 | static SkColorTable* getGrayColortable() { |
| 1089 | static SkColorTable* grayColortable = NULL; |
| 1090 | if (grayColortable == NULL) { |
| 1091 | SkPMColor* colors = new SkPMColor[256]; |
| 1092 | for (int i = 0 ; i < 256; i++) { |
| 1093 | colors[i] = SkPreMultiplyARGB(255, i, i, i); |
| 1094 | } |
| 1095 | grayColortable = new SkColorTable(colors, 256); |
| 1096 | } |
| 1097 | return grayColortable; |
| 1098 | } |
| 1099 | |
| 1100 | SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, pdf_long uncompressedStreamLength, |
| 1101 | int width, int height, int bytesPerLine, |
| 1102 | int bpc, const std::string& colorSpace, |
| 1103 | bool transparencyMask) { |
| 1104 | SkBitmap bitmap; |
| 1105 | |
| 1106 | int components = GetColorSpaceComponents(colorSpace); |
| 1107 | //#define MAX_COMPONENTS 10 |
| 1108 | |
| 1109 | int bitsPerLine = width * components * bpc; |
| 1110 | // TODO(edisonn): assume start of lines are aligned at 32 bits? |
| 1111 | // Is there a faster way to load the uncompressed stream into a bitmap? |
| 1112 | |
| 1113 | // minimal support for now |
| 1114 | if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) { |
| 1115 | SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor)); |
| 1116 | |
| 1117 | for (int h = 0 ; h < height; h++) { |
| 1118 | long i = width * (height - 1 - h); |
| 1119 | for (int w = 0 ; w < width; w++) { |
| 1120 | uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w], |
| 1121 | uncompressedStream[3 * w + 1], |
| 1122 | uncompressedStream[3 * w + 2]); |
| 1123 | i++; |
| 1124 | } |
| 1125 | uncompressedStream += bytesPerLine; |
| 1126 | } |
| 1127 | |
| 1128 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 1129 | bitmap.setPixels(uncompressedStreamArgb); |
| 1130 | } |
| 1131 | else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) { |
| 1132 | unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height); |
| 1133 | |
| 1134 | for (int h = 0 ; h < height; h++) { |
| 1135 | long i = width * (height - 1 - h); |
| 1136 | for (int w = 0 ; w < width; w++) { |
| 1137 | uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] : |
| 1138 | uncompressedStream[w]; |
| 1139 | i++; |
| 1140 | } |
| 1141 | uncompressedStream += bytesPerLine; |
| 1142 | } |
| 1143 | |
| 1144 | bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config, |
| 1145 | width, height); |
| 1146 | bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable()); |
| 1147 | } |
| 1148 | |
| 1149 | // TODO(edisonn): Report Warning, NYI, or error |
| 1150 | return bitmap; |
| 1151 | } |
| 1152 | |
| 1153 | bool transferImageStreamToARGB(unsigned char* uncompressedStream, pdf_long uncompressedStreamLength, |
| 1154 | int width, int bytesPerLine, |
| 1155 | int bpc, const std::string& colorSpace, |
| 1156 | SkColor** uncompressedStreamArgb, |
| 1157 | pdf_long* uncompressedStreamLengthInBytesArgb) { |
| 1158 | int components = GetColorSpaceComponents(colorSpace); |
| 1159 | //#define MAX_COMPONENTS 10 |
| 1160 | |
| 1161 | int bitsPerLine = width * components * bpc; |
| 1162 | // TODO(edisonn): assume start of lines are aligned at 32 bits? |
| 1163 | int height = uncompressedStreamLength / bytesPerLine; |
| 1164 | |
| 1165 | // minimal support for now |
| 1166 | if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) { |
| 1167 | *uncompressedStreamLengthInBytesArgb = width * height * 4; |
| 1168 | *uncompressedStreamArgb = (SkColor*)malloc(*uncompressedStreamLengthInBytesArgb); |
| 1169 | |
| 1170 | for (int h = 0 ; h < height; h++) { |
| 1171 | long i = width * (height - 1 - h); |
| 1172 | for (int w = 0 ; w < width; w++) { |
| 1173 | (*uncompressedStreamArgb)[i] = SkColorSetRGB(uncompressedStream[3 * w], |
| 1174 | uncompressedStream[3 * w + 1], |
| 1175 | uncompressedStream[3 * w + 2]); |
| 1176 | i++; |
| 1177 | } |
| 1178 | uncompressedStream += bytesPerLine; |
| 1179 | } |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) { |
| 1184 | *uncompressedStreamLengthInBytesArgb = width * height * 4; |
| 1185 | *uncompressedStreamArgb = (SkColor*)malloc(*uncompressedStreamLengthInBytesArgb); |
| 1186 | |
| 1187 | for (int h = 0 ; h < height; h++) { |
| 1188 | long i = width * (height - 1 - h); |
| 1189 | for (int w = 0 ; w < width; w++) { |
| 1190 | (*uncompressedStreamArgb)[i] = SkColorSetRGB(uncompressedStream[w], |
| 1191 | uncompressedStream[w], |
| 1192 | uncompressedStream[w]); |
| 1193 | i++; |
| 1194 | } |
| 1195 | uncompressedStream += bytesPerLine; |
| 1196 | } |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | // utils |
| 1204 | |
| 1205 | // TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject |
| 1206 | // TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config |
| 1207 | // TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest |
| 1208 | // skia format, through a table |
| 1209 | |
| 1210 | // this functions returns the image, it does not look at the smask. |
| 1211 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1212 | SkBitmap getImageFromObject(PdfContext* pdfContext, const SkPdfImageDictionary* image, bool transparencyMask) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1213 | if (image == NULL || !image->valid()) { |
| 1214 | // TODO(edisonn): report warning to be used in testing. |
| 1215 | return SkBitmap(); |
| 1216 | } |
| 1217 | |
| 1218 | // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ... |
| 1219 | // PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 1220 | // obj.GetDictionary().GetKey(PdfName("Filter"))); |
| 1221 | // if (value && value->IsArray() && value->GetArray().GetSize() == 1) { |
| 1222 | // value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 1223 | // &value->GetArray()[0]); |
| 1224 | // } |
| 1225 | // if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") { |
| 1226 | // SkStream stream = SkStream:: |
| 1227 | // SkImageDecoder::Factory() |
| 1228 | // } |
| 1229 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1230 | long bpc = image->BitsPerComponent(); |
| 1231 | long width = image->Width(); |
| 1232 | long height = image->Height(); |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1233 | std::string colorSpace = "DeviceRGB"; |
edisonn@google.com | 1277cf0 | 2013-06-17 23:36:45 +0000 | [diff] [blame^] | 1234 | |
| 1235 | // TODO(edisonn): color space can be an array too! |
| 1236 | if (image->isColorSpaceAName()) { |
| 1237 | colorSpace = image->getColorSpaceAsName(); |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1238 | } |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | bool imageMask = image->imageMask(); |
| 1242 | |
| 1243 | if (imageMask) { |
| 1244 | if (bpc != 0 && bpc != 1) { |
| 1245 | // TODO(edisonn): report warning to be used in testing. |
| 1246 | return SkBitmap(); |
| 1247 | } |
| 1248 | bpc = 1; |
| 1249 | } |
| 1250 | */ |
| 1251 | |
| 1252 | const PdfObject* obj = image->podofo(); |
| 1253 | |
| 1254 | char* uncompressedStream = NULL; |
| 1255 | pdf_long uncompressedStreamLength = 0; |
| 1256 | |
| 1257 | PdfResult ret = kPartial_PdfResult; |
| 1258 | // TODO(edisonn): get rid of try/catch exceptions! We should not throw on user data! |
| 1259 | try { |
| 1260 | obj->GetStream()->GetFilteredCopy(&uncompressedStream, &uncompressedStreamLength); |
| 1261 | } catch (PdfError& e) { |
| 1262 | // TODO(edisonn): report warning to be used in testing. |
| 1263 | return SkBitmap(); |
| 1264 | } |
| 1265 | |
| 1266 | int bytesPerLine = uncompressedStreamLength / height; |
| 1267 | #ifdef PDF_TRACE |
| 1268 | if (uncompressedStreamLength % height != 0) { |
| 1269 | printf("Warning uncompressedStreamLength % height != 0 !!!\n"); |
| 1270 | } |
| 1271 | #endif |
| 1272 | |
| 1273 | SkBitmap bitmap = transferImageStreamToBitmap( |
| 1274 | (unsigned char*)uncompressedStream, uncompressedStreamLength, |
| 1275 | width, height, bytesPerLine, |
| 1276 | bpc, colorSpace, |
| 1277 | transparencyMask); |
| 1278 | |
| 1279 | free(uncompressedStream); |
| 1280 | |
| 1281 | return bitmap; |
| 1282 | } |
| 1283 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1284 | SkBitmap getSmaskFromObject(PdfContext* pdfContext, const SkPdfImageDictionary* obj) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1285 | const PdfObject* sMask = resolveReferenceObject(pdfContext->fPdfDoc, |
| 1286 | obj->podofo()->GetDictionary().GetKey(PdfName("SMask"))); |
| 1287 | |
| 1288 | #ifdef PDF_TRACE |
| 1289 | std::string str; |
| 1290 | if (sMask) { |
| 1291 | sMask->ToString(str); |
| 1292 | printf("/SMask of /Subtype /Image: %s\n", str.c_str()); |
| 1293 | } |
| 1294 | #endif |
| 1295 | |
| 1296 | if (sMask) { |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1297 | SkPdfImageDictionary skxobjmask(pdfContext->fPdfDoc, sMask); |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1298 | return getImageFromObject(pdfContext, &skxobjmask, true); |
| 1299 | } |
| 1300 | |
| 1301 | // TODO(edisonn): implement GS SMask. Default to empty right now. |
| 1302 | return pdfContext->fGraphicsState.fSMask; |
| 1303 | } |
| 1304 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1305 | PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfImageDictionary* skpdfimage) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1306 | if (skpdfimage == NULL || !skpdfimage->valid()) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1307 | return kIgnoreError_PdfResult; |
| 1308 | } |
| 1309 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1310 | SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false); |
| 1311 | SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1312 | |
| 1313 | canvas->save(); |
| 1314 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1315 | SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0)); |
| 1316 | |
| 1317 | if (sMask.empty()) { |
| 1318 | canvas->drawBitmapRect(image, dst, NULL); |
| 1319 | } else { |
| 1320 | canvas->saveLayer(&dst, NULL); |
| 1321 | canvas->drawBitmapRect(image, dst, NULL); |
| 1322 | SkPaint xfer; |
| 1323 | xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode |
| 1324 | canvas->drawBitmapRect(sMask, dst, &xfer); |
| 1325 | canvas->restore(); |
| 1326 | } |
| 1327 | |
| 1328 | canvas->restore(); |
| 1329 | |
| 1330 | return kPartial_PdfResult; |
| 1331 | } |
| 1332 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1333 | bool SkMatrixFromDictionary(PdfContext* pdfContext, |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1334 | const PdfDictionary& dict, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1335 | const char* key, |
| 1336 | SkMatrix* matrix) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1337 | const PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1338 | dict.GetKey(PdfName(key))); |
| 1339 | |
| 1340 | if (value == NULL || !value->IsArray()) { |
| 1341 | return false; |
| 1342 | } |
| 1343 | |
| 1344 | if (value->GetArray().GetSize() != 6) { |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
| 1348 | double array[6]; |
| 1349 | for (int i = 0; i < 6; i++) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1350 | const PdfObject* elem = resolveReferenceObject(pdfContext->fPdfDoc, &value->GetArray()[i]); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1351 | if (elem == NULL || (!elem->IsReal() && !elem->IsNumber())) { |
| 1352 | return false; |
| 1353 | } |
| 1354 | array[i] = elem->GetReal(); |
| 1355 | } |
| 1356 | |
| 1357 | *matrix = SkMatrixFromPdfMatrix(array); |
| 1358 | return true; |
| 1359 | } |
| 1360 | |
| 1361 | bool SkRectFromDictionary(PdfContext* pdfContext, |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1362 | const PdfDictionary& dict, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1363 | const char* key, |
| 1364 | SkRect* rect) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1365 | const PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1366 | dict.GetKey(PdfName(key))); |
| 1367 | |
| 1368 | if (value == NULL || !value->IsArray()) { |
| 1369 | return false; |
| 1370 | } |
| 1371 | |
| 1372 | if (value->GetArray().GetSize() != 4) { |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | double array[4]; |
| 1377 | for (int i = 0; i < 4; i++) { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1378 | const PdfObject* elem = resolveReferenceObject(pdfContext->fPdfDoc, &value->GetArray()[i]); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1379 | if (elem == NULL || (!elem->IsReal() && !elem->IsNumber())) { |
| 1380 | return false; |
| 1381 | } |
| 1382 | array[i] = elem->GetReal(); |
| 1383 | } |
| 1384 | |
| 1385 | *rect = SkRect::MakeLTRB(SkDoubleToScalar(array[0]), |
| 1386 | SkDoubleToScalar(array[1]), |
| 1387 | SkDoubleToScalar(array[2]), |
| 1388 | SkDoubleToScalar(array[3])); |
| 1389 | return true; |
| 1390 | } |
| 1391 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1392 | SkPdfObject* get(const SkPdfObject* obj, const char* key, const char* abr = "") { |
| 1393 | SkPdfObject* ret = NULL; |
| 1394 | if (obj == NULL) return NULL; |
| 1395 | const SkPdfDictionary* dict = obj->asDictionary(); |
| 1396 | if (dict == NULL) return NULL; |
| 1397 | if (!dict->podofo()->IsDictionary()) return NULL; |
| 1398 | ObjectFromDictionary(dict->doc(), dict->podofo()->GetDictionary(), key, abr, &ret); |
| 1399 | return ret; |
| 1400 | } |
| 1401 | |
| 1402 | PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) { |
| 1403 | if (!skobj || !skobj->podofo() || !skobj->podofo()->HasStream() || skobj->podofo()->GetStream() == NULL || skobj->podofo()->GetStream()->GetLength() == 0) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1404 | return kOK_PdfResult; |
| 1405 | } |
| 1406 | |
| 1407 | PdfOp_q(pdfContext, canvas, NULL); |
| 1408 | canvas->save(); |
| 1409 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1410 | if (get(skobj, "Resources")) { |
| 1411 | SkPdfResourceDictionary* res = NULL; |
| 1412 | |
| 1413 | PodofoMapper::map(*get(skobj, "Resources"), &res); |
| 1414 | |
| 1415 | if (res) { |
| 1416 | pdfContext->fGraphicsState.fResources = *res; |
| 1417 | delete res; |
| 1418 | } |
| 1419 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1420 | |
| 1421 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix"); |
| 1422 | |
| 1423 | SkMatrix matrix; |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1424 | if (SkMatrixFromDictionary(pdfContext, skobj->podofo()->GetDictionary(), "Matrix", &matrix)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1425 | pdfContext->fGraphicsState.fMatrix.preConcat(matrix); |
| 1426 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix; |
| 1427 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix; |
| 1428 | // TODO(edisonn) reset matrixTm and matricTlm also? |
| 1429 | } |
| 1430 | |
| 1431 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix"); |
| 1432 | |
| 1433 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1434 | |
| 1435 | SkRect bbox; |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1436 | if (SkRectFromDictionary(pdfContext, skobj->podofo()->GetDictionary(), "BBox", &bbox)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1437 | canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
| 1438 | } |
| 1439 | |
| 1440 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 1441 | // For this PdfContentsTokenizer needs to be extended. |
| 1442 | |
| 1443 | char* uncompressedStream = NULL; |
| 1444 | pdf_long uncompressedStreamLength = 0; |
| 1445 | |
| 1446 | PdfResult ret = kPartial_PdfResult; |
| 1447 | |
| 1448 | // TODO(edisonn): get rid of try/catch exceptions! We should not throw on user data! |
| 1449 | try { |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1450 | skobj->podofo()->GetStream()->GetFilteredCopy(&uncompressedStream, &uncompressedStreamLength); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1451 | if (uncompressedStream != NULL && uncompressedStreamLength != 0) { |
| 1452 | PdfContentsTokenizer tokenizer(uncompressedStream, uncompressedStreamLength); |
| 1453 | PdfMainLooper looper(NULL, &tokenizer, pdfContext, canvas); |
| 1454 | looper.loop(); |
| 1455 | } |
| 1456 | free(uncompressedStream); |
| 1457 | } catch (PdfError& e) { |
| 1458 | ret = kIgnoreError_PdfResult; |
| 1459 | } |
| 1460 | |
| 1461 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 1462 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 1463 | canvas->restore(); |
| 1464 | PdfOp_Q(pdfContext, canvas, NULL); |
| 1465 | return ret; |
| 1466 | } |
| 1467 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1468 | PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const PdfObject& obj) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1469 | return kNYI_PdfResult; |
| 1470 | } |
| 1471 | |
| 1472 | // TODO(edisonn): faster, have the property on the PdfObject itself. |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1473 | std::set<const PdfObject*> gInRendering; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1474 | |
| 1475 | class CheckRecursiveRendering { |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1476 | const PdfObject& fObj; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1477 | public: |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1478 | CheckRecursiveRendering(const PdfObject& obj) : fObj(obj) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1479 | gInRendering.insert(&obj); |
| 1480 | } |
| 1481 | |
| 1482 | ~CheckRecursiveRendering() { |
| 1483 | //SkASSERT(fObj.fInRendering); |
| 1484 | gInRendering.erase(&fObj); |
| 1485 | } |
| 1486 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1487 | static bool IsInRendering(const PdfObject& obj) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1488 | return gInRendering.find(&obj) != gInRendering.end(); |
| 1489 | } |
| 1490 | }; |
| 1491 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1492 | PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const PdfObject& obj) { |
| 1493 | if (CheckRecursiveRendering::IsInRendering(obj)) { |
| 1494 | // Oops, corrupt PDF! |
| 1495 | return kIgnoreError_PdfResult; |
| 1496 | } |
| 1497 | |
| 1498 | CheckRecursiveRendering checkRecursion(obj); |
| 1499 | |
| 1500 | // TODO(edisonn): check type |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1501 | SkPdfXObjectDictionary* skobj = NULL; |
| 1502 | if (!PodofoMapper::map(*pdfContext->fPdfDoc, obj, &skobj)) return kIgnoreError_PdfResult; |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (!skobj || !skobj->valid()) return kIgnoreError_PdfResult; |
| 1505 | |
| 1506 | PdfResult ret = kIgnoreError_PdfResult; |
| 1507 | switch (skobj->getType()) |
| 1508 | { |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1509 | case kObjectDictionaryXObjectDictionaryImageDictionary_SkPdfObjectType: |
| 1510 | ret = doXObject_Image(pdfContext, canvas, skobj->asImageDictionary()); |
edisonn@google.com | e4d11be | 2013-06-12 19:53:42 +0000 | [diff] [blame] | 1511 | break; |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 1512 | case kObjectDictionaryXObjectDictionaryType1FormDictionary_SkPdfObjectType: |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 1513 | ret = doXObject_Form(pdfContext, canvas, skobj->asType1FormDictionary()); |
edisonn@google.com | e4d11be | 2013-06-12 19:53:42 +0000 | [diff] [blame] | 1514 | break; |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 1515 | //case kObjectDictionaryXObjectPS_SkPdfObjectType: |
| 1516 | //return doXObject_PS(skxobj.asPS()); |
| 1517 | } |
| 1518 | |
| 1519 | delete skobj; |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 1523 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1524 | pdfContext->fStateStack.push(pdfContext->fGraphicsState); |
| 1525 | canvas->save(); |
| 1526 | return kOK_PdfResult; |
| 1527 | } |
| 1528 | |
| 1529 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1530 | pdfContext->fGraphicsState = pdfContext->fStateStack.top(); |
| 1531 | pdfContext->fStateStack.pop(); |
| 1532 | canvas->restore(); |
| 1533 | return kOK_PdfResult; |
| 1534 | } |
| 1535 | |
| 1536 | PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1537 | double array[6]; |
| 1538 | for (int i = 0 ; i < 6 ; i++) { |
| 1539 | array[5 - i] = pdfContext->fVarStack.top().GetReal(); |
| 1540 | pdfContext->fVarStack.pop(); |
| 1541 | } |
| 1542 | |
| 1543 | // a b |
| 1544 | // c d |
| 1545 | // e f |
| 1546 | |
| 1547 | // 0 1 |
| 1548 | // 2 3 |
| 1549 | // 4 5 |
| 1550 | |
| 1551 | // sx ky |
| 1552 | // kx sy |
| 1553 | // tx ty |
| 1554 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 1555 | |
| 1556 | pdfContext->fGraphicsState.fMatrix.preConcat(matrix); |
| 1557 | |
| 1558 | #ifdef PDF_TRACE |
| 1559 | printf("cm "); |
| 1560 | for (int i = 0 ; i < 6 ; i++) { |
| 1561 | printf("%f ", array[i]); |
| 1562 | } |
| 1563 | printf("\n"); |
| 1564 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1565 | #endif |
| 1566 | |
| 1567 | return kOK_PdfResult; |
| 1568 | } |
| 1569 | |
| 1570 | //leading TL Set the text leading, Tl |
| 1571 | //, to leading, which is a number expressed in unscaled text |
| 1572 | //space units. Text leading is used only by the T*, ', and " operators. Initial value: 0. |
| 1573 | PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1574 | double ty = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1575 | |
| 1576 | pdfContext->fGraphicsState.fTextLeading = ty; |
| 1577 | |
| 1578 | return kOK_PdfResult; |
| 1579 | } |
| 1580 | |
| 1581 | PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1582 | double ty = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1583 | double tx = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1584 | |
| 1585 | double array[6] = {1, 0, 0, 1, tx, ty}; |
| 1586 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 1587 | |
| 1588 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 1589 | pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix); |
| 1590 | |
| 1591 | return kPartial_PdfResult; |
| 1592 | } |
| 1593 | |
| 1594 | PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1595 | double ty = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1596 | double tx = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1597 | |
| 1598 | PdfVariant _ty(-ty); |
| 1599 | pdfContext->fVarStack.push(_ty); |
| 1600 | PdfOp_TL(pdfContext, canvas, looper); |
| 1601 | |
| 1602 | PdfVariant vtx(tx); |
| 1603 | PdfVariant vty(ty); |
| 1604 | pdfContext->fVarStack.push(vtx); |
| 1605 | pdfContext->fVarStack.push(vty); |
| 1606 | return PdfOp_Td(pdfContext, canvas, looper); |
| 1607 | } |
| 1608 | |
| 1609 | PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1610 | double f = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1611 | double e = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1612 | double d = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1613 | double c = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1614 | double b = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1615 | double a = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1616 | |
| 1617 | double array[6]; |
| 1618 | array[0] = a; |
| 1619 | array[1] = b; |
| 1620 | array[2] = c; |
| 1621 | array[3] = d; |
| 1622 | array[4] = e; |
| 1623 | array[5] = f; |
| 1624 | |
| 1625 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 1626 | matrix.postConcat(pdfContext->fGraphicsState.fMatrix); |
| 1627 | |
| 1628 | // TODO(edisonn): Text positioning. |
| 1629 | pdfContext->fGraphicsState.fMatrixTm = matrix; |
| 1630 | pdfContext->fGraphicsState.fMatrixTlm = matrix;; |
| 1631 | |
| 1632 | return kPartial_PdfResult; |
| 1633 | } |
| 1634 | |
| 1635 | //— T* Move to the start of the next line. This operator has the same effect as the code |
| 1636 | //0 Tl Td |
| 1637 | //where Tl is the current leading parameter in the text state |
| 1638 | PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1639 | PdfVariant zero(0.0); |
| 1640 | PdfVariant tl(pdfContext->fGraphicsState.fTextLeading); |
| 1641 | |
| 1642 | pdfContext->fVarStack.push(zero); |
| 1643 | pdfContext->fVarStack.push(tl); |
| 1644 | return PdfOp_Td(pdfContext, canvas, looper); |
| 1645 | } |
| 1646 | |
| 1647 | PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1648 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1649 | pdfContext->fGraphicsState.fPath.reset(); |
| 1650 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1651 | } |
| 1652 | |
| 1653 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1654 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1655 | |
| 1656 | pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 1657 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 1658 | |
| 1659 | return kOK_PdfResult; |
| 1660 | } |
| 1661 | |
| 1662 | PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1663 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1664 | pdfContext->fGraphicsState.fPath.reset(); |
| 1665 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1666 | } |
| 1667 | |
| 1668 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1669 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1670 | |
| 1671 | pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 1672 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 1673 | |
| 1674 | return kOK_PdfResult; |
| 1675 | } |
| 1676 | |
| 1677 | PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1678 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1679 | pdfContext->fGraphicsState.fPath.reset(); |
| 1680 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1681 | } |
| 1682 | |
| 1683 | double y3 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1684 | double x3 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1685 | double y2 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1686 | double x2 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1687 | double y1 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1688 | double x1 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1689 | |
| 1690 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1691 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1692 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1693 | |
| 1694 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1695 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1696 | |
| 1697 | return kOK_PdfResult; |
| 1698 | } |
| 1699 | |
| 1700 | PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1701 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1702 | pdfContext->fGraphicsState.fPath.reset(); |
| 1703 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1704 | } |
| 1705 | |
| 1706 | double y3 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1707 | double x3 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1708 | double y2 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1709 | double x2 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1710 | double y1 = pdfContext->fGraphicsState.fCurPosY; |
| 1711 | double x1 = pdfContext->fGraphicsState.fCurPosX; |
| 1712 | |
| 1713 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1714 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1715 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1716 | |
| 1717 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1718 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1719 | |
| 1720 | return kOK_PdfResult; |
| 1721 | } |
| 1722 | |
| 1723 | PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1724 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1725 | pdfContext->fGraphicsState.fPath.reset(); |
| 1726 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1727 | } |
| 1728 | |
| 1729 | double y3 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1730 | double x3 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1731 | double y2 = pdfContext->fGraphicsState.fCurPosY; |
| 1732 | double x2 = pdfContext->fGraphicsState.fCurPosX; |
| 1733 | double y1 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1734 | double x1 = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1735 | |
| 1736 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1737 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1738 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1739 | |
| 1740 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1741 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1742 | |
| 1743 | return kOK_PdfResult; |
| 1744 | } |
| 1745 | |
| 1746 | PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1747 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1748 | pdfContext->fGraphicsState.fPath.reset(); |
| 1749 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1750 | } |
| 1751 | |
| 1752 | double height = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1753 | double width = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1754 | double y = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1755 | double x = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1756 | |
| 1757 | pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y), |
| 1758 | SkDoubleToScalar(x + width), SkDoubleToScalar(y + height)); |
| 1759 | |
| 1760 | pdfContext->fGraphicsState.fCurPosX = x; |
| 1761 | pdfContext->fGraphicsState.fCurPosY = y + height; |
| 1762 | |
| 1763 | return kOK_PdfResult; |
| 1764 | } |
| 1765 | |
| 1766 | PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1767 | pdfContext->fGraphicsState.fPath.close(); |
| 1768 | pdfContext->fGraphicsState.fPathClosed = true; |
| 1769 | return kOK_PdfResult; |
| 1770 | } |
| 1771 | |
| 1772 | PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) { |
| 1773 | SkPath path = pdfContext->fGraphicsState.fPath; |
| 1774 | |
| 1775 | if (close) { |
| 1776 | path.close(); |
| 1777 | } |
| 1778 | |
| 1779 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1780 | |
| 1781 | SkPaint paint; |
| 1782 | |
| 1783 | // TODO(edisonn): get this from pdfContext->options, |
| 1784 | // or pdfContext->addPaintOptions(&paint); |
| 1785 | paint.setAntiAlias(true); |
| 1786 | |
| 1787 | // TODO(edisonn): dashing, miter, ... |
| 1788 | |
| 1789 | // path.transform(pdfContext->fGraphicsState.fMatrix); |
| 1790 | // path.transform(pdfContext->fOriginalMatrix); |
| 1791 | |
| 1792 | SkPoint line[2]; |
| 1793 | if (fill && !stroke && path.isLine(line)) { |
| 1794 | paint.setStyle(SkPaint::kStroke_Style); |
| 1795 | paint.setColor(pdfContext->fGraphicsState.fNonStroking.fColor); |
| 1796 | paint.setStrokeWidth(SkDoubleToScalar(0)); |
| 1797 | canvas->drawPath(path, paint); |
| 1798 | } else { |
| 1799 | if (fill) { |
| 1800 | paint.setStyle(SkPaint::kFill_Style); |
| 1801 | if (evenOdd) { |
| 1802 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1803 | } |
| 1804 | paint.setColor(pdfContext->fGraphicsState.fNonStroking.fColor); |
| 1805 | canvas->drawPath(path, paint); |
| 1806 | } |
| 1807 | |
| 1808 | if (stroke) { |
| 1809 | paint.setStyle(SkPaint::kStroke_Style); |
| 1810 | paint.setColor(pdfContext->fGraphicsState.fStroking.fColor); |
| 1811 | paint.setStrokeWidth(SkDoubleToScalar(pdfContext->fGraphicsState.fLineWidth)); |
| 1812 | path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke |
| 1813 | canvas->drawPath(path, paint); |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | pdfContext->fGraphicsState.fPath.reset(); |
| 1818 | // todo zoom ... other stuff ? |
| 1819 | |
| 1820 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1821 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1822 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1823 | #endif |
| 1824 | } |
| 1825 | |
| 1826 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1827 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1828 | |
| 1829 | return kPartial_PdfResult; |
| 1830 | |
| 1831 | } |
| 1832 | |
| 1833 | PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1834 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false); |
| 1835 | } |
| 1836 | |
| 1837 | PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1838 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false); |
| 1839 | } |
| 1840 | |
| 1841 | PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1842 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1843 | } |
| 1844 | |
| 1845 | PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1846 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1847 | } |
| 1848 | |
| 1849 | PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1850 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true); |
| 1851 | } |
| 1852 | |
| 1853 | PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1854 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false); |
| 1855 | } |
| 1856 | |
| 1857 | PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1858 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true); |
| 1859 | } |
| 1860 | |
| 1861 | PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1862 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false); |
| 1863 | } |
| 1864 | |
| 1865 | PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1866 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true); |
| 1867 | } |
| 1868 | |
| 1869 | PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1870 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1871 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1872 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1873 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1874 | #endif |
| 1875 | } |
| 1876 | |
| 1877 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1878 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1879 | |
| 1880 | pdfContext->fGraphicsState.fPathClosed = true; |
| 1881 | |
| 1882 | return kOK_PdfResult; |
| 1883 | } |
| 1884 | |
| 1885 | PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1886 | pdfContext->fGraphicsState.fTextBlock = true; |
| 1887 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix; |
| 1888 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix; |
| 1889 | |
| 1890 | return kPartial_PdfResult; |
| 1891 | } |
| 1892 | |
| 1893 | PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1894 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1895 | return kIgnoreError_PdfResult; |
| 1896 | } |
| 1897 | // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack? |
| 1898 | return kPartial_PdfResult; |
| 1899 | } |
| 1900 | |
| 1901 | //font size Tf Set the text font, Tf |
| 1902 | //, to font and the text font size, Tfs, to size. font is the name of a |
| 1903 | //font resource in the Fontsubdictionary of the current resource dictionary; size is |
| 1904 | //a number representing a scale factor. There is no initial value for either font or |
| 1905 | //size; they must be specified explicitly using Tf before any text is shown. |
| 1906 | PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1907 | pdfContext->fGraphicsState.fCurFontSize = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 1908 | PdfName fontName = pdfContext->fVarStack.top().GetName(); pdfContext->fVarStack.pop(); |
| 1909 | |
| 1910 | // TODO(edisonn): Load font from pdfContext->fGraphicsState.fObjectWithResources ? |
| 1911 | PdfObject* pFont = pdfContext->fPdfPage->GetFromResources( PdfName("Font"), fontName ); |
| 1912 | if( !pFont ) |
| 1913 | { |
| 1914 | // TODO(edisonn): try to ignore the error, make sure we do not crash. |
| 1915 | return kIgnoreError_PdfResult; |
| 1916 | } |
| 1917 | |
| 1918 | pdfContext->fGraphicsState.fCurFont = pdfContext->fPdfDoc->GetFont( pFont ); |
| 1919 | if( !pdfContext->fGraphicsState.fCurFont ) |
| 1920 | { |
| 1921 | // TODO(edisonn): check ~/crasing, for one of the files PoDoFo throws exception |
| 1922 | // when calling pFont->Reference(), with Linked list corruption. |
| 1923 | return kIgnoreError_PdfResult; |
| 1924 | } |
| 1925 | |
| 1926 | return kPartial_PdfResult; |
| 1927 | } |
| 1928 | |
| 1929 | PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1930 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1931 | // TODO(edisonn): try to recover and draw it any way? |
| 1932 | return kIgnoreError_PdfResult; |
| 1933 | } |
| 1934 | |
| 1935 | PdfResult ret = DrawText(pdfContext, |
| 1936 | pdfContext->fGraphicsState.fCurFont, |
| 1937 | pdfContext->fVarStack.top().GetString(), |
| 1938 | canvas); |
| 1939 | pdfContext->fVarStack.pop(); |
| 1940 | |
| 1941 | return ret; |
| 1942 | } |
| 1943 | |
| 1944 | PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1945 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1946 | // TODO(edisonn): try to recover and draw it any way? |
| 1947 | return kIgnoreError_PdfResult; |
| 1948 | } |
| 1949 | |
| 1950 | PdfOp_T_star(pdfContext, canvas, looper); |
| 1951 | // Do not pop, and push, just transfer the param to Tj |
| 1952 | return PdfOp_Tj(pdfContext, canvas, looper); |
| 1953 | } |
| 1954 | |
| 1955 | PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1956 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1957 | // TODO(edisonn): try to recover and draw it any way? |
| 1958 | return kIgnoreError_PdfResult; |
| 1959 | } |
| 1960 | |
| 1961 | PdfVariant str = pdfContext->fVarStack.top(); pdfContext->fVarStack.pop(); |
| 1962 | PdfVariant ac = pdfContext->fVarStack.top(); pdfContext->fVarStack.pop(); |
| 1963 | PdfVariant aw = pdfContext->fVarStack.top(); pdfContext->fVarStack.pop(); |
| 1964 | |
| 1965 | pdfContext->fVarStack.push(aw); |
| 1966 | PdfOp_Tw(pdfContext, canvas, looper); |
| 1967 | |
| 1968 | pdfContext->fVarStack.push(ac); |
| 1969 | PdfOp_Tc(pdfContext, canvas, looper); |
| 1970 | |
| 1971 | pdfContext->fVarStack.push(str); |
| 1972 | PdfOp_quote(pdfContext, canvas, looper); |
| 1973 | |
| 1974 | return kPartial_PdfResult; |
| 1975 | } |
| 1976 | |
| 1977 | PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1978 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1979 | // TODO(edisonn): try to recover and draw it any way? |
| 1980 | return kIgnoreError_PdfResult; |
| 1981 | } |
| 1982 | |
| 1983 | PdfArray array = pdfContext->fVarStack.top().GetArray(); |
| 1984 | pdfContext->fVarStack.pop(); |
| 1985 | |
| 1986 | for( int i=0; i<static_cast<int>(array.GetSize()); i++ ) |
| 1987 | { |
| 1988 | if( array[i].IsString() || array[i].IsHexString() ) { |
| 1989 | DrawText(pdfContext, |
| 1990 | pdfContext->fGraphicsState.fCurFont, |
| 1991 | array[i].GetString(), |
| 1992 | canvas); |
| 1993 | } else if (array[i].IsReal() || array[i].IsNumber()) { |
| 1994 | double dx = array[i].GetReal(); |
| 1995 | SkMatrix matrix; |
| 1996 | matrix.setAll(SkDoubleToScalar(1), |
| 1997 | SkDoubleToScalar(0), |
| 1998 | // TODO(edisonn): use writing mode, vertical/horizontal. |
| 1999 | SkDoubleToScalar(-dx), // amount is substracted!!! |
| 2000 | SkDoubleToScalar(0), |
| 2001 | SkDoubleToScalar(1), |
| 2002 | SkDoubleToScalar(0), |
| 2003 | SkDoubleToScalar(0), |
| 2004 | SkDoubleToScalar(0), |
| 2005 | SkDoubleToScalar(1)); |
| 2006 | |
| 2007 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 2008 | } |
| 2009 | } |
| 2010 | return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK. |
| 2011 | } |
| 2012 | |
| 2013 | PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfColorOperator* colorOperator) { |
| 2014 | colorOperator->fColorSpace = pdfContext->fVarStack.top().GetName().GetName(); pdfContext->fVarStack.pop(); |
| 2015 | return kOK_PdfResult; |
| 2016 | } |
| 2017 | |
| 2018 | PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2019 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 2020 | } |
| 2021 | |
| 2022 | PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2023 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 2024 | } |
| 2025 | |
| 2026 | PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfColorOperator* colorOperator) { |
| 2027 | double c[4]; |
| 2028 | pdf_int64 v[4]; |
| 2029 | |
| 2030 | int n = GetColorSpaceComponents(colorOperator->fColorSpace); |
| 2031 | |
| 2032 | bool doubles = true; |
| 2033 | if (colorOperator->fColorSpace == "Indexed") { |
| 2034 | doubles = false; |
| 2035 | } |
| 2036 | |
| 2037 | #ifdef PDF_TRACE |
| 2038 | printf("color space = %s, N = %i\n", colorOperator->fColorSpace.c_str(), n); |
| 2039 | #endif |
| 2040 | |
| 2041 | for (int i = n - 1; i >= 0 ; i--) { |
| 2042 | if (doubles) { |
| 2043 | c[i] = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2044 | } else { |
| 2045 | v[i] = pdfContext->fVarStack.top().GetNumber(); pdfContext->fVarStack.pop(); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | // TODO(edisonn): Now, set that color. Only DeviceRGB supported. |
| 2050 | if (colorOperator->fColorSpace == "DeviceRGB") { |
| 2051 | colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2])); |
| 2052 | } |
| 2053 | return kPartial_PdfResult; |
| 2054 | } |
| 2055 | |
| 2056 | PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2057 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 2058 | } |
| 2059 | |
| 2060 | PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2061 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 2062 | } |
| 2063 | |
| 2064 | PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfColorOperator* colorOperator) { |
| 2065 | PdfString name; |
| 2066 | |
| 2067 | if (pdfContext->fVarStack.top().IsName()) { |
| 2068 | pdfContext->fVarStack.pop(); |
| 2069 | } |
| 2070 | |
| 2071 | // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec. |
| 2072 | PdfOp_SC_sc(pdfContext, canvas, colorOperator); |
| 2073 | |
| 2074 | return kPartial_PdfResult; |
| 2075 | } |
| 2076 | |
| 2077 | PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2078 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 2079 | } |
| 2080 | |
| 2081 | PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2082 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 2083 | } |
| 2084 | |
| 2085 | PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, PdfColorOperator* colorOperator) { |
| 2086 | double gray = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2087 | return kNYI_PdfResult; |
| 2088 | } |
| 2089 | |
| 2090 | PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2091 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 2092 | } |
| 2093 | |
| 2094 | PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2095 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 2096 | } |
| 2097 | |
| 2098 | PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfColorOperator* colorOperator) { |
| 2099 | double b = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2100 | double g = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2101 | double r = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2102 | |
| 2103 | colorOperator->fColorSpace = "DeviceRGB"; |
| 2104 | colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b)); |
| 2105 | return kOK_PdfResult; |
| 2106 | } |
| 2107 | |
| 2108 | PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2109 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 2110 | } |
| 2111 | |
| 2112 | PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2113 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 2114 | } |
| 2115 | |
| 2116 | PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, PdfColorOperator* colorOperator) { |
| 2117 | // TODO(edisonn): spec has some rules about overprint, implement them. |
| 2118 | double k = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2119 | double y = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2120 | double m = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2121 | double c = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2122 | |
| 2123 | colorOperator->fColorSpace = "DeviceCMYK"; |
| 2124 | // TODO(edisonn): Set color. |
| 2125 | return kNYI_PdfResult; |
| 2126 | } |
| 2127 | |
| 2128 | PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2129 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 2130 | } |
| 2131 | |
| 2132 | PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2133 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 2134 | } |
| 2135 | |
| 2136 | PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2137 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 2138 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 2139 | |
| 2140 | return kOK_PdfResult; |
| 2141 | } |
| 2142 | |
| 2143 | PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2144 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 2145 | |
| 2146 | #ifdef PDF_TRACE |
| 2147 | if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) { |
| 2148 | printf("CLIP IS RECT\n"); |
| 2149 | } |
| 2150 | #endif |
| 2151 | |
| 2152 | // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd. |
| 2153 | pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType); |
| 2154 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 2155 | |
| 2156 | return kPartial_PdfResult; |
| 2157 | } |
| 2158 | |
| 2159 | PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2160 | *looper = new PdfCompatibilitySectionLooper(); |
| 2161 | return kOK_PdfResult; |
| 2162 | } |
| 2163 | |
| 2164 | PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2165 | #ifdef ASSERT_BAD_PDF_OPS |
| 2166 | SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's |
| 2167 | // have the assert when testing good pdfs. |
| 2168 | #endif |
| 2169 | return kIgnoreError_PdfResult; |
| 2170 | } |
| 2171 | |
| 2172 | PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2173 | *looper = new PdfInlineImageLooper(); |
| 2174 | return kOK_PdfResult; |
| 2175 | } |
| 2176 | |
| 2177 | PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2178 | #ifdef ASSERT_BAD_PDF_OPS |
| 2179 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 2180 | // have the assert when testing good pdfs. |
| 2181 | #endif |
| 2182 | return kIgnoreError_PdfResult; |
| 2183 | } |
| 2184 | |
| 2185 | PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2186 | #ifdef ASSERT_BAD_PDF_OPS |
| 2187 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 2188 | // have the assert when testing good pdfs. |
| 2189 | #endif |
| 2190 | return kIgnoreError_PdfResult; |
| 2191 | } |
| 2192 | |
| 2193 | //lineWidth w Set the line width in the graphics state (see “Line Width” on page 152). |
| 2194 | PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2195 | double lineWidth = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2196 | pdfContext->fGraphicsState.fLineWidth = lineWidth; |
| 2197 | |
| 2198 | return kOK_PdfResult; |
| 2199 | } |
| 2200 | |
| 2201 | //lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153). |
| 2202 | PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2203 | pdfContext->fVarStack.pop(); |
| 2204 | //double lineCap = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2205 | |
| 2206 | return kNYI_PdfResult; |
| 2207 | } |
| 2208 | |
| 2209 | //lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153). |
| 2210 | PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2211 | pdfContext->fVarStack.pop(); |
| 2212 | //double lineJoin = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2213 | |
| 2214 | return kNYI_PdfResult; |
| 2215 | } |
| 2216 | |
| 2217 | //miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153). |
| 2218 | PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2219 | pdfContext->fVarStack.pop(); |
| 2220 | //double miterLimit = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2221 | |
| 2222 | return kNYI_PdfResult; |
| 2223 | } |
| 2224 | |
| 2225 | //dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on |
| 2226 | //page 155). |
| 2227 | PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2228 | pdfContext->fVarStack.pop(); |
| 2229 | pdfContext->fVarStack.pop(); |
| 2230 | |
| 2231 | return kNYI_PdfResult; |
| 2232 | } |
| 2233 | |
| 2234 | //intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197). |
| 2235 | PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2236 | pdfContext->fVarStack.pop(); |
| 2237 | |
| 2238 | return kNYI_PdfResult; |
| 2239 | } |
| 2240 | |
| 2241 | //flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness |
| 2242 | //Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci- |
| 2243 | //fies the output device’s default flatness tolerance. |
| 2244 | PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2245 | pdfContext->fVarStack.pop(); |
| 2246 | |
| 2247 | return kNYI_PdfResult; |
| 2248 | } |
| 2249 | |
| 2250 | //dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is |
| 2251 | //the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section). |
| 2252 | PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2253 | PdfName name = pdfContext->fVarStack.top().GetName(); pdfContext->fVarStack.pop(); |
| 2254 | |
edisonn@google.com | a2fab9d | 2013-06-14 19:22:19 +0000 | [diff] [blame] | 2255 | #ifdef PDF_TRACE |
| 2256 | std::string str; |
| 2257 | #endif |
| 2258 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 2259 | const PdfObject* resources = pdfContext->fGraphicsState.fResources.podofo(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (resources == NULL) { |
| 2262 | #ifdef PDF_TRACE |
| 2263 | printf("WARNING: No Resources for a page with 'gs' operator!\n"); |
| 2264 | #endif |
| 2265 | return kIgnoreError_PdfResult; |
| 2266 | } |
| 2267 | |
| 2268 | #ifdef PDF_TRACE |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2269 | resources->ToString(str); |
| 2270 | printf("Print gs Page Resources: %s\n", str.c_str()); |
| 2271 | #endif |
| 2272 | |
| 2273 | if (!resources->IsDictionary()) { |
| 2274 | #ifdef PDF_TRACE |
| 2275 | printf("Resources is not a dictionary!\n"); |
| 2276 | #endif |
| 2277 | return kIgnoreError_PdfResult; |
| 2278 | } |
| 2279 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 2280 | const PdfDictionary& resourceDict = resources->GetDictionary(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2281 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 2282 | const PdfObject* extGStateDictionary = resolveReferenceObject(pdfContext->fPdfDoc, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2283 | resourceDict.GetKey("ExtGState")); |
| 2284 | |
| 2285 | if (extGStateDictionary == NULL) { |
| 2286 | #ifdef PDF_TRACE |
| 2287 | printf("ExtGState is NULL!\n"); |
| 2288 | #endif |
| 2289 | return kIgnoreError_PdfResult; |
| 2290 | } |
| 2291 | |
| 2292 | if (!extGStateDictionary->IsDictionary()) { |
| 2293 | #ifdef PDF_TRACE |
| 2294 | printf("extGStateDictionary is not a dictionary!\n"); |
| 2295 | #endif |
| 2296 | return kIgnoreError_PdfResult; |
| 2297 | } |
| 2298 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 2299 | const PdfObject* value = |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2300 | resolveReferenceObject(pdfContext->fPdfDoc, |
| 2301 | extGStateDictionary->GetDictionary().GetKey(name)); |
| 2302 | |
| 2303 | if (value == NULL) { |
| 2304 | #ifdef PDF_TRACE |
| 2305 | printf("Named object not found!\n"); |
| 2306 | #endif |
| 2307 | return kIgnoreError_PdfResult; |
| 2308 | } |
| 2309 | |
| 2310 | #ifdef PDF_TRACE |
| 2311 | value->ToString(str); |
| 2312 | printf("gs object value: %s\n", str.c_str()); |
| 2313 | #endif |
| 2314 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 2315 | SkPdfGraphicsStateDictionary gs(pdfContext->fPdfDoc, value); |
| 2316 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2317 | // TODO(edisonn): now load all those properties in graphic state. |
| 2318 | |
| 2319 | return kNYI_PdfResult; |
| 2320 | } |
| 2321 | |
| 2322 | //charSpace Tc Set the character spacing, Tc |
| 2323 | //, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators. |
| 2324 | //Initial value: 0. |
| 2325 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2326 | double charSpace = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2327 | pdfContext->fGraphicsState.fCharSpace = charSpace; |
| 2328 | |
| 2329 | return kOK_PdfResult; |
| 2330 | } |
| 2331 | |
| 2332 | //wordSpace Tw Set the word spacing, T |
| 2333 | //w |
| 2334 | //, to wordSpace, which is a number expressed in unscaled |
| 2335 | //text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial |
| 2336 | //value: 0. |
| 2337 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2338 | double wordSpace = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2339 | pdfContext->fGraphicsState.fWordSpace = wordSpace; |
| 2340 | |
| 2341 | return kOK_PdfResult; |
| 2342 | } |
| 2343 | |
| 2344 | //scale Tz Set the horizontal scaling, Th |
| 2345 | //, to (scale ˜ 100). scale is a number specifying the |
| 2346 | //percentage of the normal width. Initial value: 100 (normal width). |
| 2347 | PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2348 | double scale = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2349 | |
| 2350 | return kNYI_PdfResult; |
| 2351 | } |
| 2352 | |
| 2353 | //render Tr Set the text rendering mode, T |
| 2354 | //mode, to render, which is an integer. Initial value: 0. |
| 2355 | PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2356 | double render = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2357 | |
| 2358 | return kNYI_PdfResult; |
| 2359 | } |
| 2360 | |
| 2361 | //rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space |
| 2362 | //units. Initial value: 0. |
| 2363 | PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2364 | double rise = pdfContext->fVarStack.top().GetReal(); pdfContext->fVarStack.pop(); |
| 2365 | |
| 2366 | return kNYI_PdfResult; |
| 2367 | } |
| 2368 | |
| 2369 | //wx wy d0 |
| 2370 | PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2371 | pdfContext->fVarStack.pop(); |
| 2372 | pdfContext->fVarStack.pop(); |
| 2373 | |
| 2374 | return kNYI_PdfResult; |
| 2375 | } |
| 2376 | |
| 2377 | //wx wy llx lly urx ury d1 |
| 2378 | PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2379 | pdfContext->fVarStack.pop(); |
| 2380 | pdfContext->fVarStack.pop(); |
| 2381 | pdfContext->fVarStack.pop(); |
| 2382 | pdfContext->fVarStack.pop(); |
| 2383 | pdfContext->fVarStack.pop(); |
| 2384 | pdfContext->fVarStack.pop(); |
| 2385 | |
| 2386 | return kNYI_PdfResult; |
| 2387 | } |
| 2388 | |
| 2389 | //name sh |
| 2390 | PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2391 | pdfContext->fVarStack.pop(); |
| 2392 | |
| 2393 | return kNYI_PdfResult; |
| 2394 | } |
| 2395 | |
| 2396 | //name Do |
| 2397 | PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2398 | PdfName name = pdfContext->fVarStack.top().GetName(); pdfContext->fVarStack.pop(); |
| 2399 | |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 2400 | const PdfObject* resources = pdfContext->fGraphicsState.fResources.podofo(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (resources == NULL) { |
| 2403 | #ifdef PDF_TRACE |
| 2404 | printf("WARNING: No Resources for a page with 'Do' operator!s\n"); |
| 2405 | #endif |
| 2406 | return kIgnoreError_PdfResult; |
| 2407 | } |
| 2408 | |
| 2409 | #ifdef PDF_TRACE |
| 2410 | std::string str; |
| 2411 | resources->ToString(str); |
| 2412 | printf("Print Do Page Resources: %s\n", str.c_str()); |
| 2413 | #endif |
| 2414 | |
| 2415 | if (!resources->IsDictionary()) { |
| 2416 | #ifdef PDF_TRACE |
| 2417 | printf("Resources is not a dictionary!\n"); |
| 2418 | #endif |
| 2419 | return kIgnoreError_PdfResult; |
| 2420 | } |
| 2421 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 2422 | const PdfDictionary& resourceDict = resources->GetDictionary(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2423 | //Next, get the XObject Dictionary from the Resource Dictionary: |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 2424 | const PdfObject* xObjectDictionary = resolveReferenceObject(pdfContext->fPdfDoc, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2425 | resourceDict.GetKey("XObject")); |
| 2426 | |
| 2427 | if (xObjectDictionary == NULL) { |
| 2428 | #ifdef PDF_TRACE |
| 2429 | printf("XObject is NULL!\n"); |
| 2430 | #endif |
| 2431 | return kIgnoreError_PdfResult; |
| 2432 | } |
| 2433 | |
| 2434 | if (!xObjectDictionary->IsDictionary()) { |
| 2435 | #ifdef PDF_TRACE |
| 2436 | printf("xObjectDictionary is not a dictionary!\n"); |
| 2437 | #endif |
| 2438 | return kIgnoreError_PdfResult; |
| 2439 | } |
| 2440 | |
edisonn@google.com | af3daa0 | 2013-06-12 19:07:45 +0000 | [diff] [blame] | 2441 | const PdfObject* value = |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2442 | resolveReferenceObject(pdfContext->fPdfDoc, |
| 2443 | xObjectDictionary->GetDictionary().GetKey(name)); |
| 2444 | |
| 2445 | if (value == NULL) { |
| 2446 | #ifdef PDF_TRACE |
| 2447 | printf("Named object not found!\n"); |
| 2448 | #endif |
| 2449 | return kIgnoreError_PdfResult; |
| 2450 | } |
| 2451 | |
| 2452 | #ifdef PDF_TRACE |
| 2453 | value->ToString(str); |
| 2454 | printf("Do object value: %s\n", str.c_str()); |
| 2455 | #endif |
| 2456 | |
| 2457 | return doXObject(pdfContext, canvas, *value); |
| 2458 | } |
| 2459 | |
| 2460 | |
| 2461 | //tag MP Designate a marked-content point. tag is a name object indicating the role or |
| 2462 | //significance of the point. |
| 2463 | PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2464 | pdfContext->fVarStack.pop(); |
| 2465 | |
| 2466 | return kNYI_PdfResult; |
| 2467 | } |
| 2468 | |
| 2469 | //tag properties DP Designate a marked-content point with an associated property list. tag is a |
| 2470 | //name object indicating the role or significance of the point; properties is |
| 2471 | //either an inline dictionary containing the property list or a name object |
| 2472 | //associated with it in the Properties subdictionary of the current resource |
| 2473 | //dictionary (see Section 9.5.1, “Property Lists”). |
| 2474 | PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2475 | pdfContext->fVarStack.pop(); |
| 2476 | pdfContext->fVarStack.pop(); |
| 2477 | |
| 2478 | return kNYI_PdfResult; |
| 2479 | } |
| 2480 | |
| 2481 | //tag BMC Begin a marked-content sequence terminated by a balancing EMC operator. |
| 2482 | //tag is a name object indicating the role or significance of the sequence. |
| 2483 | PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2484 | pdfContext->fVarStack.pop(); |
| 2485 | |
| 2486 | return kNYI_PdfResult; |
| 2487 | } |
| 2488 | |
| 2489 | //tag properties BDC Begin a marked-content sequence with an associated property list, terminated |
| 2490 | //by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the |
| 2491 | //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”). |
| 2492 | PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2493 | pdfContext->fVarStack.pop(); |
| 2494 | pdfContext->fVarStack.pop(); |
| 2495 | |
| 2496 | return kNYI_PdfResult; |
| 2497 | } |
| 2498 | |
| 2499 | //— EMC End a marked-content sequence begun by a BMC or BDC operator. |
| 2500 | PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 2501 | return kNYI_PdfResult; |
| 2502 | } |
| 2503 | |
| 2504 | void initPdfOperatorRenderes() { |
| 2505 | static bool gInitialized = false; |
| 2506 | if (gInitialized) { |
| 2507 | return; |
| 2508 | } |
| 2509 | |
| 2510 | gPdfOps["q"] = PdfOp_q; |
| 2511 | gPdfOps["Q"] = PdfOp_Q; |
| 2512 | gPdfOps["cm"] = PdfOp_cm; |
| 2513 | |
| 2514 | gPdfOps["TD"] = PdfOp_TD; |
| 2515 | gPdfOps["Td"] = PdfOp_Td; |
| 2516 | gPdfOps["Tm"] = PdfOp_Tm; |
| 2517 | gPdfOps["T*"] = PdfOp_T_star; |
| 2518 | |
| 2519 | gPdfOps["m"] = PdfOp_m; |
| 2520 | gPdfOps["l"] = PdfOp_l; |
| 2521 | gPdfOps["c"] = PdfOp_c; |
| 2522 | gPdfOps["v"] = PdfOp_v; |
| 2523 | gPdfOps["y"] = PdfOp_y; |
| 2524 | gPdfOps["h"] = PdfOp_h; |
| 2525 | gPdfOps["re"] = PdfOp_re; |
| 2526 | |
| 2527 | gPdfOps["S"] = PdfOp_S; |
| 2528 | gPdfOps["s"] = PdfOp_s; |
| 2529 | gPdfOps["f"] = PdfOp_f; |
| 2530 | gPdfOps["F"] = PdfOp_F; |
| 2531 | gPdfOps["f*"] = PdfOp_f_star; |
| 2532 | gPdfOps["B"] = PdfOp_B; |
| 2533 | gPdfOps["B*"] = PdfOp_B_star; |
| 2534 | gPdfOps["b"] = PdfOp_b; |
| 2535 | gPdfOps["b*"] = PdfOp_b_star; |
| 2536 | gPdfOps["n"] = PdfOp_n; |
| 2537 | |
| 2538 | gPdfOps["BT"] = PdfOp_BT; |
| 2539 | gPdfOps["ET"] = PdfOp_ET; |
| 2540 | |
| 2541 | gPdfOps["Tj"] = PdfOp_Tj; |
| 2542 | gPdfOps["'"] = PdfOp_quote; |
| 2543 | gPdfOps["\""] = PdfOp_doublequote; |
| 2544 | gPdfOps["TJ"] = PdfOp_TJ; |
| 2545 | |
| 2546 | gPdfOps["CS"] = PdfOp_CS; |
| 2547 | gPdfOps["cs"] = PdfOp_cs; |
| 2548 | gPdfOps["SC"] = PdfOp_SC; |
| 2549 | gPdfOps["SCN"] = PdfOp_SCN; |
| 2550 | gPdfOps["sc"] = PdfOp_sc; |
| 2551 | gPdfOps["scn"] = PdfOp_scn; |
| 2552 | gPdfOps["G"] = PdfOp_G; |
| 2553 | gPdfOps["g"] = PdfOp_g; |
| 2554 | gPdfOps["RG"] = PdfOp_RG; |
| 2555 | gPdfOps["rg"] = PdfOp_rg; |
| 2556 | gPdfOps["K"] = PdfOp_K; |
| 2557 | gPdfOps["k"] = PdfOp_k; |
| 2558 | |
| 2559 | gPdfOps["W"] = PdfOp_W; |
| 2560 | gPdfOps["W*"] = PdfOp_W_star; |
| 2561 | |
| 2562 | gPdfOps["BX"] = PdfOp_BX; |
| 2563 | gPdfOps["EX"] = PdfOp_EX; |
| 2564 | |
| 2565 | gPdfOps["BI"] = PdfOp_BI; |
| 2566 | gPdfOps["ID"] = PdfOp_ID; |
| 2567 | gPdfOps["EI"] = PdfOp_EI; |
| 2568 | |
| 2569 | gPdfOps["w"] = PdfOp_w; |
| 2570 | gPdfOps["J"] = PdfOp_J; |
| 2571 | gPdfOps["j"] = PdfOp_j; |
| 2572 | gPdfOps["M"] = PdfOp_M; |
| 2573 | gPdfOps["d"] = PdfOp_d; |
| 2574 | gPdfOps["ri"] = PdfOp_ri; |
| 2575 | gPdfOps["i"] = PdfOp_i; |
| 2576 | gPdfOps["gs"] = PdfOp_gs; |
| 2577 | |
| 2578 | gPdfOps["Tc"] = PdfOp_Tc; |
| 2579 | gPdfOps["Tw"] = PdfOp_Tw; |
| 2580 | gPdfOps["Tz"] = PdfOp_Tz; |
| 2581 | gPdfOps["TL"] = PdfOp_TL; |
| 2582 | gPdfOps["Tf"] = PdfOp_Tf; |
| 2583 | gPdfOps["Tr"] = PdfOp_Tr; |
| 2584 | gPdfOps["Ts"] = PdfOp_Ts; |
| 2585 | |
| 2586 | gPdfOps["d0"] = PdfOp_d0; |
| 2587 | gPdfOps["d1"] = PdfOp_d1; |
| 2588 | |
| 2589 | gPdfOps["sh"] = PdfOp_sh; |
| 2590 | |
| 2591 | gPdfOps["Do"] = PdfOp_Do; |
| 2592 | |
| 2593 | gPdfOps["MP"] = PdfOp_MP; |
| 2594 | gPdfOps["DP"] = PdfOp_DP; |
| 2595 | gPdfOps["BMC"] = PdfOp_BMC; |
| 2596 | gPdfOps["BDC"] = PdfOp_BDC; |
| 2597 | gPdfOps["EMC"] = PdfOp_EMC; |
| 2598 | |
| 2599 | gInitialized = true; |
| 2600 | } |
| 2601 | |
| 2602 | void reportPdfRenderStats() { |
| 2603 | std::map<std::string, int>::iterator iter; |
| 2604 | |
| 2605 | for (int i = 0 ; i < kCount_PdfResult; i++) { |
| 2606 | for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) { |
| 2607 | printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second); |
| 2608 | } |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | PdfResult PdfMainLooper::consumeToken(PdfToken& token) { |
| 2613 | if( token.eType == ePdfContentsType_Keyword ) |
| 2614 | { |
| 2615 | // TODO(edisonn): log trace flag (verbose, error, info, warning, ...) |
| 2616 | #ifdef PDF_TRACE |
| 2617 | printf("KEYWORD: %s\n", token.pszToken); |
| 2618 | #endif |
| 2619 | PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[token.pszToken]; |
| 2620 | if (pdfOperatorRenderer) { |
| 2621 | // caller, main work is done by pdfOperatorRenderer(...) |
| 2622 | PdfTokenLooper* childLooper = NULL; |
| 2623 | gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][token.pszToken]++; |
| 2624 | |
| 2625 | if (childLooper) { |
| 2626 | childLooper->setUp(this); |
| 2627 | childLooper->loop(); |
| 2628 | delete childLooper; |
| 2629 | } |
| 2630 | } else { |
| 2631 | gRenderStats[kUnsupported_PdfResult][token.pszToken]++; |
| 2632 | } |
| 2633 | } |
| 2634 | else if ( token.eType == ePdfContentsType_Variant ) |
| 2635 | { |
| 2636 | #ifdef PDF_TRACE |
| 2637 | std::string _var; |
| 2638 | token.var.ToString(_var); |
| 2639 | printf("var: %s\n", _var.c_str()); |
| 2640 | #endif |
| 2641 | fPdfContext->fVarStack.push( token.var ); |
| 2642 | } |
| 2643 | else if ( token.eType == ePdfContentsType_ImageData) { |
| 2644 | // TODO(edisonn): implement inline image. |
| 2645 | } |
| 2646 | else { |
| 2647 | return kIgnoreError_PdfResult; |
| 2648 | } |
| 2649 | return kOK_PdfResult; |
| 2650 | } |
| 2651 | |
| 2652 | void PdfMainLooper::loop() { |
| 2653 | PdfToken token; |
| 2654 | while (readToken(fTokenizer, &token)) { |
| 2655 | consumeToken(token); |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) { |
| 2660 | //pdfContext.fInlineImage.fKeyValuePairs[key] = value; |
| 2661 | return kNYI_PdfResult; |
| 2662 | } |
| 2663 | |
| 2664 | void PdfInlineImageLooper::loop() { |
| 2665 | PdfToken token; |
| 2666 | while (readToken(fTokenizer, &token)) { |
| 2667 | if (token.eType == ePdfContentsType_Keyword && strcmp(token.pszToken, "BX") == 0) { |
| 2668 | PdfTokenLooper* looper = new PdfCompatibilitySectionLooper(); |
| 2669 | looper->setUp(this); |
| 2670 | looper->loop(); |
| 2671 | } else { |
| 2672 | if (token.eType == ePdfContentsType_Keyword && strcmp(token.pszToken, "EI") == 0) { |
| 2673 | done(); |
| 2674 | return; |
| 2675 | } |
| 2676 | |
| 2677 | consumeToken(token); |
| 2678 | } |
| 2679 | } |
| 2680 | // TODO(edisonn): report error/warning, EOF without EI. |
| 2681 | } |
| 2682 | |
| 2683 | PdfResult PdfInlineImageLooper::done() { |
| 2684 | |
| 2685 | // TODO(edisonn): long to short names |
| 2686 | // TODO(edisonn): set properties in a map |
| 2687 | // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress |
| 2688 | // the stream. |
| 2689 | |
| 2690 | SkBitmap bitmap; |
| 2691 | setup_bitmap(&bitmap, 50, 50, SK_ColorRED); |
| 2692 | |
| 2693 | // TODO(edisonn): matrix use. |
| 2694 | // Draw dummy red square, to show the prezence of the inline image. |
| 2695 | fCanvas->drawBitmap(bitmap, |
| 2696 | SkDoubleToScalar(0), |
| 2697 | SkDoubleToScalar(0), |
| 2698 | NULL); |
| 2699 | return kNYI_PdfResult; |
| 2700 | } |
| 2701 | |
| 2702 | PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) { |
| 2703 | return fParent->consumeToken(token); |
| 2704 | } |
| 2705 | |
| 2706 | void PdfCompatibilitySectionLooper::loop() { |
| 2707 | // TODO(edisonn): save stacks position, or create a new stack? |
| 2708 | // TODO(edisonn): what happens if we pop out more variables then when we started? |
| 2709 | // restore them? fail? We could create a new operands stack for every new BX/EX section, |
| 2710 | // pop-ing too much will not affect outside the section. |
| 2711 | PdfToken token; |
| 2712 | while (readToken(fTokenizer, &token)) { |
| 2713 | if (token.eType == ePdfContentsType_Keyword && strcmp(token.pszToken, "BX") == 0) { |
| 2714 | PdfTokenLooper* looper = new PdfCompatibilitySectionLooper(); |
| 2715 | looper->setUp(this); |
| 2716 | looper->loop(); |
| 2717 | delete looper; |
| 2718 | } else { |
| 2719 | if (token.eType == ePdfContentsType_Keyword && strcmp(token.pszToken, "EX") == 0) break; |
| 2720 | fParent->consumeToken(token); |
| 2721 | } |
| 2722 | } |
| 2723 | // TODO(edisonn): restore stack. |
| 2724 | } |
| 2725 | |
| 2726 | // TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf |
| 2727 | // TODO(edisonn): Add API for Forms viewing and editing |
| 2728 | // e.g. SkBitmap getPage(int page); |
| 2729 | // int formsCount(); |
| 2730 | // SkForm getForm(int formID); // SkForm(SkRect, .. other data) |
| 2731 | // TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ... |
| 2732 | // if we load the first page, and we zoom to fit to screen horizontally, then load only those |
| 2733 | // resources needed, so the preview is fast. |
| 2734 | // TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve |
| 2735 | // references automatically. |
| 2736 | class SkPdfViewer : public SkRefCnt { |
| 2737 | public: |
| 2738 | |
| 2739 | bool load(const SkString inputFileName, SkPicture* out) { |
| 2740 | |
| 2741 | initPdfOperatorRenderes(); |
| 2742 | |
| 2743 | try |
| 2744 | { |
| 2745 | std::cout << "Init: " << inputFileName.c_str() << std::endl; |
| 2746 | |
| 2747 | PdfMemDocument doc(inputFileName.c_str()); |
| 2748 | if( !doc.GetPageCount() ) |
| 2749 | { |
| 2750 | std::cout << "ERROR: Empty Document" << inputFileName.c_str() << std::endl; |
| 2751 | return false; |
| 2752 | } else { |
| 2753 | |
| 2754 | for (int pn = 0; pn < doc.GetPageCount(); ++pn) { |
| 2755 | PoDoFo::PdfPage* page = doc.GetPage(pn); |
| 2756 | PdfRect rect = page->GetMediaBox(); |
| 2757 | #ifdef PDF_TRACE |
| 2758 | printf("Page Width: %f, Page Height: %f\n", rect.GetWidth(), rect.GetHeight()); |
| 2759 | #endif |
| 2760 | |
| 2761 | // TODO(edisonn): page->GetCropBox(), page->GetTrimBox() ... how to use? |
| 2762 | |
| 2763 | SkBitmap bitmap; |
| 2764 | #ifdef PDF_DEBUG_3X |
| 2765 | setup_bitmap(&bitmap, 3*rect.GetWidth(), 3*rect.GetHeight()); |
| 2766 | #else |
| 2767 | setup_bitmap(&bitmap, rect.GetWidth(), rect.GetHeight()); |
| 2768 | #endif |
| 2769 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
| 2770 | SkCanvas canvas(device); |
| 2771 | |
| 2772 | |
| 2773 | const char* pszToken = NULL; |
| 2774 | PdfVariant var; |
| 2775 | EPdfContentsType eType; |
| 2776 | |
| 2777 | PdfContentsTokenizer tokenizer( page ); |
| 2778 | |
| 2779 | PdfContext pdfContext; |
| 2780 | pdfContext.fPdfPage = page; |
| 2781 | pdfContext.fPdfDoc = &doc; |
| 2782 | pdfContext.fOriginalMatrix = SkMatrix::I(); |
edisonn@google.com | 68d15c8 | 2013-06-17 20:46:27 +0000 | [diff] [blame] | 2783 | pdfContext.fGraphicsState.fResources = SkPdfResourceDictionary(pdfContext.fPdfDoc, resolveReferenceObject(pdfContext.fPdfDoc, |
| 2784 | pdfContext.fPdfPage->GetResources())); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 2785 | |
| 2786 | gPdfContext = &pdfContext; |
| 2787 | gDumpBitmap = &bitmap; |
| 2788 | gDumpCanvas = &canvas; |
| 2789 | |
| 2790 | |
| 2791 | // TODO(edisonn): get matrix stuff right. |
| 2792 | // TODO(edisonn): add DPI/scale/zoom. |
| 2793 | SkScalar z = SkIntToScalar(0); |
| 2794 | SkScalar w = SkDoubleToScalar(rect.GetWidth()); |
| 2795 | SkScalar h = SkDoubleToScalar(rect.GetHeight()); |
| 2796 | |
| 2797 | SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(w, z), SkPoint::Make(w, h), SkPoint::Make(z, h)}; |
| 2798 | // SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 2799 | |
| 2800 | // TODO(edisonn): add flag for this app to create sourunding buffer zone |
| 2801 | // TODO(edisonn): add flagg for no clipping. |
| 2802 | // Use larger image to make sure we do not draw anything outside of page |
| 2803 | // could be used in tests. |
| 2804 | |
| 2805 | #ifdef PDF_DEBUG_3X |
| 2806 | 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)}; |
| 2807 | #else |
| 2808 | SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 2809 | #endif |
| 2810 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)}; |
| 2811 | //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)}; |
| 2812 | |
| 2813 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)}; |
| 2814 | //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)}; |
| 2815 | |
| 2816 | //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)}; |
| 2817 | //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)}; |
| 2818 | |
| 2819 | SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4)); |
| 2820 | SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix"); |
| 2821 | |
| 2822 | |
| 2823 | pdfContext.fGraphicsState.fMatrix = pdfContext.fOriginalMatrix; |
| 2824 | pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fMatrix; |
| 2825 | pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fMatrix; |
| 2826 | |
| 2827 | canvas.setMatrix(pdfContext.fOriginalMatrix); |
| 2828 | |
| 2829 | #ifndef PDF_DEBUG_NO_PAGE_CLIPING |
| 2830 | canvas.clipRect(SkRect::MakeXYWH(z, z, w, h), SkRegion::kIntersect_Op, true); |
| 2831 | #endif |
| 2832 | |
| 2833 | PdfMainLooper looper(NULL, &tokenizer, &pdfContext, &canvas); |
| 2834 | looper.loop(); |
| 2835 | |
| 2836 | canvas.flush(); |
| 2837 | |
| 2838 | SkString out; |
| 2839 | out.appendf("%s-%i.png", inputFileName.c_str(), pn); |
| 2840 | SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 2841 | } |
| 2842 | return true; |
| 2843 | } |
| 2844 | } |
| 2845 | catch( PdfError & e ) |
| 2846 | { |
| 2847 | std::cout << "ERROR: PDF can't be parsed!" << inputFileName.c_str() << std::endl; |
| 2848 | return false; |
| 2849 | } |
| 2850 | |
| 2851 | return true; |
| 2852 | } |
| 2853 | bool write(void*) const { return false; } |
| 2854 | }; |
| 2855 | |
| 2856 | |
| 2857 | |
| 2858 | /** |
| 2859 | * Given list of directories and files to use as input, expects to find .pdf |
| 2860 | * files and it will convert them to .png files writing them in the same directory |
| 2861 | * one file for each page. |
| 2862 | * |
| 2863 | * Returns zero exit code if all .pdf files were converted successfully, |
| 2864 | * otherwise returns error code 1. |
| 2865 | */ |
| 2866 | |
| 2867 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 2868 | static const char PNG_FILE_EXTENSION[] = "png"; |
| 2869 | |
| 2870 | // TODO(edisonn): add ability to write to a new directory. |
| 2871 | static void usage(const char* argv0) { |
| 2872 | SkDebugf("PDF to PNG rendering tool\n"); |
| 2873 | SkDebugf("\n" |
| 2874 | "Usage: \n" |
| 2875 | " %s <input>... -w <outputDir> \n" |
| 2876 | , argv0); |
| 2877 | SkDebugf("\n\n"); |
| 2878 | SkDebugf( |
| 2879 | " input: A list of directories and files to use as input. Files are\n" |
| 2880 | " expected to have the .skp extension.\n\n"); |
| 2881 | SkDebugf( |
| 2882 | " outputDir: directory to write the rendered pdfs.\n\n"); |
| 2883 | SkDebugf("\n"); |
| 2884 | } |
| 2885 | |
| 2886 | /** Replaces the extension of a file. |
| 2887 | * @param path File name whose extension will be changed. |
| 2888 | * @param old_extension The old extension. |
| 2889 | * @param new_extension The new extension. |
| 2890 | * @returns false if the file did not has the expected extension. |
| 2891 | * if false is returned, contents of path are undefined. |
| 2892 | */ |
| 2893 | static bool replace_filename_extension(SkString* path, |
| 2894 | const char old_extension[], |
| 2895 | const char new_extension[]) { |
| 2896 | if (path->endsWith(old_extension)) { |
| 2897 | path->remove(path->size() - strlen(old_extension), |
| 2898 | strlen(old_extension)); |
| 2899 | if (!path->endsWith(".")) { |
| 2900 | return false; |
| 2901 | } |
| 2902 | path->append(new_extension); |
| 2903 | return true; |
| 2904 | } |
| 2905 | return false; |
| 2906 | } |
| 2907 | |
| 2908 | /** Builds the output filename. path = dir/name, and it replaces expected |
| 2909 | * .skp extension with .pdf extention. |
| 2910 | * @param path Output filename. |
| 2911 | * @param name The name of the file. |
| 2912 | * @returns false if the file did not has the expected extension. |
| 2913 | * if false is returned, contents of path are undefined. |
| 2914 | */ |
| 2915 | static bool make_output_filepath(SkString* path, const SkString& dir, |
| 2916 | const SkString& name) { |
| 2917 | sk_tools::make_filepath(path, dir, name); |
| 2918 | return replace_filename_extension(path, |
| 2919 | PDF_FILE_EXTENSION, |
| 2920 | PNG_FILE_EXTENSION); |
| 2921 | } |
| 2922 | |
| 2923 | /** Write the output of pdf renderer to a file. |
| 2924 | * @param outputDir Output dir. |
| 2925 | * @param inputFilename The skp file that was read. |
| 2926 | * @param renderer The object responsible to write the pdf file. |
| 2927 | */ |
| 2928 | static bool write_output(const SkString& outputDir, |
| 2929 | const SkString& inputFilename, |
| 2930 | const SkPdfViewer& renderer) { |
| 2931 | if (outputDir.isEmpty()) { |
| 2932 | SkDynamicMemoryWStream stream; |
| 2933 | renderer.write(&stream); |
| 2934 | return true; |
| 2935 | } |
| 2936 | |
| 2937 | SkString outputPath; |
| 2938 | if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { |
| 2939 | return false; |
| 2940 | } |
| 2941 | |
| 2942 | SkFILEWStream stream(outputPath.c_str()); |
| 2943 | if (!stream.isValid()) { |
| 2944 | SkDebugf("Could not write to file %s\n", outputPath.c_str()); |
| 2945 | return false; |
| 2946 | } |
| 2947 | renderer.write(&stream); |
| 2948 | |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
| 2952 | /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 2953 | * @param inputPath The skp file to be read. |
| 2954 | * @param outputDir Output dir. |
| 2955 | * @param renderer The object responsible to render the skp object into pdf. |
| 2956 | */ |
| 2957 | static bool parse_pdf(const SkString& inputPath, const SkString& outputDir, |
| 2958 | SkPdfViewer& renderer) { |
| 2959 | SkString inputFilename; |
| 2960 | sk_tools::get_basename(&inputFilename, inputPath); |
| 2961 | |
| 2962 | SkFILEStream inputStream; |
| 2963 | inputStream.setPath(inputPath.c_str()); |
| 2964 | if (!inputStream.isValid()) { |
| 2965 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 2966 | return false; |
| 2967 | } |
| 2968 | |
| 2969 | bool success = false; |
| 2970 | |
| 2971 | success = renderer.load(inputPath, NULL); |
| 2972 | |
| 2973 | |
| 2974 | // success = write_output(outputDir, inputFilename, renderer); |
| 2975 | |
| 2976 | //renderer.end(); |
| 2977 | return success; |
| 2978 | } |
| 2979 | |
| 2980 | /** For each file in the directory or for the file passed in input, call |
| 2981 | * parse_pdf. |
| 2982 | * @param input A directory or an pdf file. |
| 2983 | * @param outputDir Output dir. |
| 2984 | * @param renderer The object responsible to render the skp object into pdf. |
| 2985 | */ |
| 2986 | static int process_input(const SkString& input, const SkString& outputDir, |
| 2987 | SkPdfViewer& renderer) { |
| 2988 | int failures = 0; |
| 2989 | if (sk_isdir(input.c_str())) { |
| 2990 | SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION); |
| 2991 | SkString inputFilename; |
| 2992 | while (iter.next(&inputFilename)) { |
| 2993 | SkString inputPath; |
| 2994 | sk_tools::make_filepath(&inputPath, input, inputFilename); |
| 2995 | if (!parse_pdf(inputPath, outputDir, renderer)) { |
| 2996 | ++failures; |
| 2997 | } |
| 2998 | } |
| 2999 | } else { |
| 3000 | SkString inputPath(input); |
| 3001 | if (!parse_pdf(inputPath, outputDir, renderer)) { |
| 3002 | ++failures; |
| 3003 | } |
| 3004 | } |
| 3005 | return failures; |
| 3006 | } |
| 3007 | |
| 3008 | static void parse_commandline(int argc, char* const argv[], |
| 3009 | SkTArray<SkString>* inputs, |
| 3010 | SkString* outputDir) { |
| 3011 | const char* argv0 = argv[0]; |
| 3012 | char* const* stop = argv + argc; |
| 3013 | |
| 3014 | for (++argv; argv < stop; ++argv) { |
| 3015 | if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { |
| 3016 | usage(argv0); |
| 3017 | exit(-1); |
| 3018 | } else if (0 == strcmp(*argv, "-w")) { |
| 3019 | ++argv; |
| 3020 | if (argv >= stop) { |
| 3021 | SkDebugf("Missing outputDir for -w\n"); |
| 3022 | usage(argv0); |
| 3023 | exit(-1); |
| 3024 | } |
| 3025 | *outputDir = SkString(*argv); |
| 3026 | } else { |
| 3027 | inputs->push_back(SkString(*argv)); |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | if (inputs->count() < 1) { |
| 3032 | usage(argv0); |
| 3033 | exit(-1); |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | int tool_main(int argc, char** argv); |
| 3038 | int tool_main(int argc, char** argv) { |
| 3039 | SkAutoGraphics ag; |
| 3040 | SkTArray<SkString> inputs; |
| 3041 | |
| 3042 | SkAutoTUnref<SkPdfViewer> |
| 3043 | renderer(SkNEW(SkPdfViewer)); |
| 3044 | SkASSERT(renderer.get()); |
| 3045 | |
| 3046 | SkString outputDir; |
| 3047 | parse_commandline(argc, argv, &inputs, &outputDir); |
| 3048 | |
| 3049 | int failures = 0; |
| 3050 | for (int i = 0; i < inputs.count(); i ++) { |
| 3051 | failures += process_input(inputs[i], outputDir, *renderer); |
| 3052 | } |
| 3053 | |
| 3054 | reportPdfRenderStats(); |
| 3055 | |
| 3056 | if (failures != 0) { |
| 3057 | SkDebugf("Failed to render %i PDFs.\n", failures); |
| 3058 | return 1; |
| 3059 | } |
| 3060 | |
| 3061 | return 0; |
| 3062 | } |
| 3063 | |
| 3064 | #if !defined SK_BUILD_FOR_IOS |
| 3065 | int main(int argc, char * const argv[]) { |
| 3066 | return tool_main(argc, (char**) argv); |
| 3067 | } |
| 3068 | #endif |