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