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