edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkDevice.h" |
| 10 | #include "SkForceLinking.h" |
| 11 | #include "SkGraphics.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkImageEncoder.h" |
| 14 | #include "SkOSFile.h" |
| 15 | #include "SkPicture.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTypeface.h" |
| 18 | #include "SkTArray.h" |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 19 | #include "SkTDict.h" |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 20 | |
edisonn@google.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame] | 21 | #include "SkPdfBasics.h" |
| 22 | #include "SkPdfNativeTokenizer.h" |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 23 | #include <cstdio> |
| 24 | #include <stack> |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 25 | #include <set> |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 26 | |
edisonn@google.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame] | 27 | extern "C" PdfContext* gPdfContext; |
| 28 | extern "C" SkBitmap* gDumpBitmap; |
| 29 | extern "C" SkCanvas* gDumpCanvas; |
| 30 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 31 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 32 | |
| 33 | // TODO(edisonn): tool, show what objects were read at least, show the ones not even read |
| 34 | // keep for each object pos in file |
| 35 | // plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y |
| 36 | |
| 37 | // TODO(edisonn): security - validate all the user input, all pdf! |
| 38 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 39 | // TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font |
| 40 | // this way, we look more at other features and layout in diffs |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 41 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 42 | // TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically |
| 43 | /* |
| 44 | #ifdef PDF_TRACE |
| 45 | std::string str; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 46 | pdfContext->fGraphicsState.fResources->native()->ToString(str); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 47 | printf("Print Tf Resources: %s\n", str.c_str()); |
| 48 | #endif |
| 49 | */ |
| 50 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 51 | #include "SkPdfHeaders_autogen.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 52 | #include "SkPdfMapper_autogen.h" |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 53 | #include "SkPdfRenderer.h" |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 54 | |
| 55 | #include "SkPdfBasics.h" |
| 56 | #include "SkPdfUtils.h" |
| 57 | |
| 58 | #include "SkPdfFont.h" |
| 59 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 60 | /* |
| 61 | * TODO(edisonn): |
| 62 | * - all font types and all ppdf font features |
| 63 | * - word spacing |
| 64 | * - load font for baidu.pdf |
| 65 | * - load font for youtube.pdf |
| 66 | * - parser for pdf from the definition already available in pdfspec_autogen.py |
| 67 | * - all docs from ~/work |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 68 | * - encapsulate native in the pdf api so the skpdf does not know anything about native ... in progress |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 69 | * - load gs/ especially smask and already known prop (skp) ... in progress |
| 70 | * - wrapper on classes for customizations? e.g. |
| 71 | * SkPdfPageObjectVanila - has only the basic loaders/getters |
| 72 | * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here |
| 73 | * need to find a nice object model for all this with constructors and factories |
| 74 | * - deal with inheritable automatically ? |
| 75 | * - deal with specific type in spec directly, add all dictionary types to known types |
| 76 | */ |
| 77 | |
| 78 | using namespace std; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 79 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 80 | NotOwnedString strings_DeviceRGB; |
| 81 | NotOwnedString strings_DeviceCMYK; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 82 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 83 | class StringsInit { |
| 84 | public: |
| 85 | StringsInit() { |
| 86 | NotOwnedString::init(&strings_DeviceRGB, "DeviceRGB"); |
| 87 | NotOwnedString::init(&strings_DeviceCMYK, "DeviceCMYK"); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | StringsInit gStringsInit; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 92 | |
| 93 | // TODO(edisonn): Document PdfTokenLooper and subclasses. |
| 94 | class PdfTokenLooper { |
| 95 | protected: |
| 96 | PdfTokenLooper* fParent; |
| 97 | SkPdfNativeTokenizer* fTokenizer; |
| 98 | PdfContext* fPdfContext; |
| 99 | SkCanvas* fCanvas; |
| 100 | |
| 101 | public: |
| 102 | PdfTokenLooper(PdfTokenLooper* parent, |
| 103 | SkPdfNativeTokenizer* tokenizer, |
| 104 | PdfContext* pdfContext, |
| 105 | SkCanvas* canvas) |
| 106 | : fParent(parent), fTokenizer(tokenizer), fPdfContext(pdfContext), fCanvas(canvas) {} |
| 107 | |
| 108 | virtual ~PdfTokenLooper() {} |
| 109 | |
| 110 | virtual PdfResult consumeToken(PdfToken& token) = 0; |
| 111 | virtual void loop() = 0; |
| 112 | |
| 113 | void setUp(PdfTokenLooper* parent) { |
| 114 | fParent = parent; |
| 115 | fTokenizer = parent->fTokenizer; |
| 116 | fPdfContext = parent->fPdfContext; |
| 117 | fCanvas = parent->fCanvas; |
| 118 | } |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 119 | |
| 120 | SkPdfNativeTokenizer* tokenizer() { return fTokenizer; } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | class PdfMainLooper : public PdfTokenLooper { |
| 124 | public: |
| 125 | PdfMainLooper(PdfTokenLooper* parent, |
| 126 | SkPdfNativeTokenizer* tokenizer, |
| 127 | PdfContext* pdfContext, |
| 128 | SkCanvas* canvas) |
| 129 | : PdfTokenLooper(parent, tokenizer, pdfContext, canvas) {} |
| 130 | |
| 131 | virtual PdfResult consumeToken(PdfToken& token); |
| 132 | virtual void loop(); |
| 133 | }; |
| 134 | |
| 135 | class PdfInlineImageLooper : public PdfTokenLooper { |
| 136 | public: |
| 137 | PdfInlineImageLooper() |
| 138 | : PdfTokenLooper(NULL, NULL, NULL, NULL) {} |
| 139 | |
| 140 | virtual PdfResult consumeToken(PdfToken& token); |
| 141 | virtual void loop(); |
| 142 | PdfResult done(); |
| 143 | }; |
| 144 | |
| 145 | class PdfCompatibilitySectionLooper : public PdfTokenLooper { |
| 146 | public: |
| 147 | PdfCompatibilitySectionLooper() |
| 148 | : PdfTokenLooper(NULL, NULL, NULL, NULL) {} |
| 149 | |
| 150 | virtual PdfResult consumeToken(PdfToken& token); |
| 151 | virtual void loop(); |
| 152 | }; |
| 153 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 154 | // Utilities |
| 155 | static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) { |
| 156 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 157 | |
| 158 | bitmap->allocPixels(); |
| 159 | bitmap->eraseColor(color); |
| 160 | } |
| 161 | |
| 162 | // TODO(edisonn): synonyms? DeviceRGB and RGB ... |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 163 | static int GetColorSpaceComponents(NotOwnedString& colorSpace) { |
| 164 | if (colorSpace.equals("DeviceCMYK")) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 165 | return 4; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 166 | } else if (colorSpace.equals("DeviceGray") || |
| 167 | colorSpace.equals("CalGray") || |
| 168 | colorSpace.equals("Indexed")) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 169 | return 1; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 170 | } else if (colorSpace.equals("DeviceRGB") || |
| 171 | colorSpace.equals("CalRGB") || |
| 172 | colorSpace.equals("Lab")) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 173 | return 3; |
| 174 | } else { |
| 175 | return 0; |
| 176 | } |
| 177 | } |
| 178 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 179 | SkMatrix SkMatrixFromPdfMatrix(double array[6]) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 180 | SkMatrix matrix; |
| 181 | matrix.setAll(SkDoubleToScalar(array[0]), |
| 182 | SkDoubleToScalar(array[2]), |
| 183 | SkDoubleToScalar(array[4]), |
| 184 | SkDoubleToScalar(array[1]), |
| 185 | SkDoubleToScalar(array[3]), |
| 186 | SkDoubleToScalar(array[5]), |
| 187 | SkDoubleToScalar(0), |
| 188 | SkDoubleToScalar(0), |
| 189 | SkDoubleToScalar(1)); |
| 190 | |
| 191 | return matrix; |
| 192 | } |
| 193 | |
| 194 | SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) { |
| 195 | double array[6]; |
| 196 | |
| 197 | // TODO(edisonn): security issue, ret if size() != 6 |
| 198 | for (int i = 0; i < 6; i++) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 199 | const SkPdfObject* elem = pdfArray->operator [](i); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 200 | if (elem == NULL || !elem->isNumber()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 201 | return SkMatrix::I(); // TODO(edisonn): report issue |
| 202 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 203 | array[i] = elem->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return SkMatrixFromPdfMatrix(array); |
| 207 | } |
| 208 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 209 | |
| 210 | extern "C" SkNativeParsedPDF* gDoc; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 211 | SkBitmap* gDumpBitmap = NULL; |
| 212 | SkCanvas* gDumpCanvas = NULL; |
| 213 | char gLastKeyword[100] = ""; |
| 214 | int gLastOpKeyword = -1; |
| 215 | char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,"; |
| 216 | int gReadOp = 0; |
| 217 | |
| 218 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 219 | #ifdef PDF_TRACE_DIFF_IN_PNG |
| 220 | static bool hasVisualEffect(const char* pdfOp) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 221 | return true; |
| 222 | if (*pdfOp == '\0') return false; |
| 223 | |
| 224 | char markedPdfOp[100] = ","; |
| 225 | strcat(markedPdfOp, pdfOp); |
| 226 | strcat(markedPdfOp, ","); |
| 227 | |
| 228 | return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL); |
| 229 | } |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 230 | #endif // PDF_TRACE_DIFF_IN_PNG |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 231 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 232 | |
| 233 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 234 | // TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 235 | static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 236 | bool ret = fTokenizer->readToken(token); |
| 237 | |
| 238 | gReadOp++; |
| 239 | |
| 240 | #ifdef PDF_TRACE_DIFF_IN_PNG |
| 241 | // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save |
| 242 | // the numbar and name of last operation, so the file name will reflect op that changed. |
| 243 | if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits. |
| 244 | gDumpCanvas->flush(); |
| 245 | |
| 246 | SkBitmap bitmap; |
| 247 | setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height()); |
| 248 | |
| 249 | memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize()); |
| 250 | |
| 251 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
| 252 | SkCanvas canvas(device); |
| 253 | |
| 254 | // draw context stuff here |
| 255 | SkPaint blueBorder; |
| 256 | blueBorder.setColor(SK_ColorBLUE); |
| 257 | blueBorder.setStyle(SkPaint::kStroke_Style); |
| 258 | blueBorder.setTextSize(SkDoubleToScalar(20)); |
| 259 | |
| 260 | SkString str; |
| 261 | |
| 262 | const SkClipStack* clipStack = gDumpCanvas->getClipStack(); |
| 263 | if (clipStack) { |
| 264 | SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 265 | const SkClipStack::Element* elem; |
| 266 | double y = 0; |
| 267 | int total = 0; |
| 268 | while (elem = iter.next()) { |
| 269 | total++; |
| 270 | y += 30; |
| 271 | |
| 272 | switch (elem->getType()) { |
| 273 | case SkClipStack::Element::kRect_Type: |
| 274 | canvas.drawRect(elem->getRect(), blueBorder); |
| 275 | canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 276 | break; |
| 277 | case SkClipStack::Element::kPath_Type: |
| 278 | canvas.drawPath(elem->getPath(), blueBorder); |
| 279 | canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 280 | break; |
| 281 | case SkClipStack::Element::kEmpty_Type: |
| 282 | canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 283 | break; |
| 284 | default: |
| 285 | canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | y += 30; |
| 291 | str.printf("Number of clips in stack: %i", total); |
| 292 | canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 293 | } |
| 294 | |
| 295 | const SkRegion& clipRegion = gDumpCanvas->getTotalClip(); |
| 296 | SkPath clipPath; |
| 297 | if (clipRegion.getBoundaryPath(&clipPath)) { |
| 298 | SkPaint redBorder; |
| 299 | redBorder.setColor(SK_ColorRED); |
| 300 | redBorder.setStyle(SkPaint::kStroke_Style); |
| 301 | canvas.drawPath(clipPath, redBorder); |
| 302 | } |
| 303 | |
| 304 | canvas.flush(); |
| 305 | |
| 306 | SkString out; |
| 307 | |
| 308 | // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack, |
| 309 | // ... and other properties, to be able to debug th code easily |
| 310 | |
| 311 | out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword); |
| 312 | SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 313 | } |
| 314 | |
| 315 | if (token->fType == kKeyword_TokenType) { |
| 316 | strcpy(gLastKeyword, token->fKeyword); |
| 317 | gLastOpKeyword = gReadOp; |
| 318 | } else { |
| 319 | strcpy(gLastKeyword, ""); |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**); |
| 329 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 330 | SkTDict<PdfOperatorRenderer> gPdfOps(100); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 331 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 332 | |
| 333 | template <typename T> class SkTDictWithDefaultConstructor : public SkTDict<T> { |
| 334 | public: |
| 335 | SkTDictWithDefaultConstructor() : SkTDict<T>(10) {} |
| 336 | }; |
| 337 | |
| 338 | SkTDictWithDefaultConstructor<int> gRenderStats[kCount_PdfResult]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 339 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 340 | const char* gRenderStatsNames[kCount_PdfResult] = { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 341 | "Success", |
| 342 | "Partially implemented", |
| 343 | "Not yet implemented", |
| 344 | "Ignore Error", |
| 345 | "Error", |
| 346 | "Unsupported/Unknown" |
| 347 | }; |
| 348 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 349 | static PdfResult DrawText(PdfContext* pdfContext, |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 350 | const SkPdfObject* _str, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 351 | SkCanvas* canvas) |
| 352 | { |
| 353 | |
| 354 | SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont; |
| 355 | if (skfont == NULL) { |
| 356 | skfont = SkPdfFont::Default(); |
| 357 | } |
| 358 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 359 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 360 | if (_str == NULL || !_str->isAnyString()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 361 | // TODO(edisonn): report warning |
| 362 | return kIgnoreError_PdfResult; |
| 363 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 364 | const SkPdfString* str = (const SkPdfString*)_str; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 365 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 366 | SkUnencodedText binary(str); |
| 367 | |
| 368 | SkDecodedText decoded; |
| 369 | |
| 370 | if (skfont->encoding() == NULL) { |
| 371 | // TODO(edisonn): report warning |
| 372 | return kNYI_PdfResult; |
| 373 | } |
| 374 | |
| 375 | skfont->encoding()->decodeText(binary, &decoded); |
| 376 | |
| 377 | SkPaint paint; |
| 378 | // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0? |
| 379 | // Or maybe just not call setTextSize at all? |
| 380 | if (pdfContext->fGraphicsState.fCurFontSize != 0) { |
| 381 | paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize)); |
| 382 | } |
| 383 | |
| 384 | // if (fCurFont && fCurFont->GetFontScale() != 0) { |
| 385 | // paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0)); |
| 386 | // } |
| 387 | |
| 388 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 389 | |
| 390 | canvas->save(); |
| 391 | |
| 392 | #if 1 |
| 393 | SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm; |
| 394 | |
| 395 | SkPoint point1; |
| 396 | pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1); |
| 397 | |
| 398 | SkMatrix mirror; |
| 399 | mirror.setTranslate(0, -point1.y()); |
| 400 | // TODO(edisonn): fix rotated text, and skewed too |
| 401 | mirror.postScale(SK_Scalar1, -SK_Scalar1); |
| 402 | // TODO(edisonn): post rotate, skew |
| 403 | mirror.postTranslate(0, point1.y()); |
| 404 | |
| 405 | matrix.postConcat(mirror); |
| 406 | |
| 407 | canvas->setMatrix(matrix); |
| 408 | |
| 409 | SkTraceMatrix(matrix, "mirrored"); |
| 410 | #endif |
| 411 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 412 | skfont->drawText(decoded, &paint, pdfContext, canvas); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 413 | canvas->restore(); |
| 414 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 415 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // TODO(edisonn): create header files with declarations! |
| 419 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 420 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 421 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 422 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 423 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 424 | // TODO(edisonn): perf!!! |
| 425 | |
| 426 | static SkColorTable* getGrayColortable() { |
| 427 | static SkColorTable* grayColortable = NULL; |
| 428 | if (grayColortable == NULL) { |
| 429 | SkPMColor* colors = new SkPMColor[256]; |
| 430 | for (int i = 0 ; i < 256; i++) { |
| 431 | colors[i] = SkPreMultiplyARGB(255, i, i, i); |
| 432 | } |
| 433 | grayColortable = new SkColorTable(colors, 256); |
| 434 | } |
| 435 | return grayColortable; |
| 436 | } |
| 437 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 438 | static SkBitmap transferImageStreamToBitmap(const unsigned char* uncompressedStream, size_t uncompressedStreamLength, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 439 | int width, int height, int bytesPerLine, |
| 440 | int bpc, const std::string& colorSpace, |
| 441 | bool transparencyMask) { |
| 442 | SkBitmap bitmap; |
| 443 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 444 | //int components = GetColorSpaceComponents(colorSpace); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 445 | //#define MAX_COMPONENTS 10 |
| 446 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 447 | // TODO(edisonn): assume start of lines are aligned at 32 bits? |
| 448 | // Is there a faster way to load the uncompressed stream into a bitmap? |
| 449 | |
| 450 | // minimal support for now |
| 451 | if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) { |
| 452 | SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor)); |
| 453 | |
| 454 | for (int h = 0 ; h < height; h++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 455 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 456 | for (int w = 0 ; w < width; w++) { |
| 457 | uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w], |
| 458 | uncompressedStream[3 * w + 1], |
| 459 | uncompressedStream[3 * w + 2]); |
| 460 | i++; |
| 461 | } |
| 462 | uncompressedStream += bytesPerLine; |
| 463 | } |
| 464 | |
| 465 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 466 | bitmap.setPixels(uncompressedStreamArgb); |
| 467 | } |
| 468 | else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) { |
| 469 | unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height); |
| 470 | |
| 471 | for (int h = 0 ; h < height; h++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 472 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 473 | for (int w = 0 ; w < width; w++) { |
| 474 | uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] : |
| 475 | uncompressedStream[w]; |
| 476 | i++; |
| 477 | } |
| 478 | uncompressedStream += bytesPerLine; |
| 479 | } |
| 480 | |
| 481 | bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config, |
| 482 | width, height); |
| 483 | bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable()); |
| 484 | } |
| 485 | |
| 486 | // TODO(edisonn): Report Warning, NYI, or error |
| 487 | return bitmap; |
| 488 | } |
| 489 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 490 | // utils |
| 491 | |
| 492 | // TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject |
| 493 | // TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config |
| 494 | // TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest |
| 495 | // skia format, through a table |
| 496 | |
| 497 | // this functions returns the image, it does not look at the smask. |
| 498 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 499 | static SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 500 | if (image == NULL || !image->hasStream()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 501 | // TODO(edisonn): report warning to be used in testing. |
| 502 | return SkBitmap(); |
| 503 | } |
| 504 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 505 | int64_t bpc = image->BitsPerComponent(pdfContext->fPdfDoc); |
| 506 | int64_t width = image->Width(pdfContext->fPdfDoc); |
| 507 | int64_t height = image->Height(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 508 | std::string colorSpace = "DeviceRGB"; |
| 509 | |
| 510 | // TODO(edisonn): color space can be an array too! |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 511 | if (image->isColorSpaceAName(pdfContext->fPdfDoc)) { |
| 512 | colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
| 516 | bool imageMask = image->imageMask(); |
| 517 | |
| 518 | if (imageMask) { |
| 519 | if (bpc != 0 && bpc != 1) { |
| 520 | // TODO(edisonn): report warning to be used in testing. |
| 521 | return SkBitmap(); |
| 522 | } |
| 523 | bpc = 1; |
| 524 | } |
| 525 | */ |
| 526 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 527 | const unsigned char* uncompressedStream = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 528 | size_t uncompressedStreamLength = 0; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 529 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 530 | SkPdfStream* stream = (SkPdfStream*)image; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 531 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 532 | if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) || |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 533 | uncompressedStream == NULL || uncompressedStreamLength == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 534 | // TODO(edisonn): report warning to be used in testing. |
| 535 | return SkBitmap(); |
| 536 | } |
| 537 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 538 | SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream; |
| 539 | |
| 540 | if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) && |
| 541 | streamDict->getFilterAsName(NULL) == "DCTDecode") || |
| 542 | (streamDict->isFilterAArray(NULL) && |
| 543 | streamDict->getFilterAsArray(NULL)->size() > 0 && |
| 544 | streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() && |
| 545 | streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) { |
| 546 | SkBitmap bitmap; |
| 547 | SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap); |
| 548 | return bitmap; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | |
| 553 | // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ... |
| 554 | // PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 555 | // obj.GetDictionary().GetKey(PdfName("Filter"))); |
| 556 | // if (value && value->IsArray() && value->GetArray().GetSize() == 1) { |
| 557 | // value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 558 | // &value->GetArray()[0]); |
| 559 | // } |
| 560 | // if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") { |
| 561 | // SkStream stream = SkStream:: |
| 562 | // SkImageDecoder::Factory() |
| 563 | // } |
| 564 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 565 | int bytesPerLine = (int)(uncompressedStreamLength / height); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 566 | #ifdef PDF_TRACE |
| 567 | if (uncompressedStreamLength % height != 0) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 568 | printf("Warning uncompressedStreamLength modulo height != 0 !!!\n"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 569 | } |
| 570 | #endif |
| 571 | |
| 572 | SkBitmap bitmap = transferImageStreamToBitmap( |
| 573 | (unsigned char*)uncompressedStream, uncompressedStreamLength, |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 574 | (int)width, (int)height, bytesPerLine, |
| 575 | (int)bpc, colorSpace, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 576 | transparencyMask); |
| 577 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 578 | return bitmap; |
| 579 | } |
| 580 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 581 | static SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 582 | SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 583 | |
| 584 | if (sMask) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 585 | return getImageFromObject(pdfContext, sMask, true); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // TODO(edisonn): implement GS SMask. Default to empty right now. |
| 589 | return pdfContext->fGraphicsState.fSMask; |
| 590 | } |
| 591 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 592 | static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 593 | if (skpdfimage == NULL) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 594 | return kIgnoreError_PdfResult; |
| 595 | } |
| 596 | |
| 597 | SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false); |
| 598 | SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage); |
| 599 | |
| 600 | canvas->save(); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 601 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 602 | |
| 603 | #if 1 |
| 604 | SkScalar z = SkIntToScalar(0); |
| 605 | SkScalar one = SkIntToScalar(1); |
| 606 | |
| 607 | SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)}; |
| 608 | SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)}; |
| 609 | SkMatrix flip; |
| 610 | SkAssertResult(flip.setPolyToPoly(from, to, 4)); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 611 | SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 612 | solveImageFlip.preConcat(flip); |
| 613 | canvas->setMatrix(solveImageFlip); |
| 614 | #endif |
| 615 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 616 | SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0)); |
| 617 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 618 | // TODO(edisonn): soft mask type? alpha/luminosity. |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 619 | if (sMask.empty()) { |
| 620 | canvas->drawBitmapRect(image, dst, NULL); |
| 621 | } else { |
| 622 | canvas->saveLayer(&dst, NULL); |
| 623 | canvas->drawBitmapRect(image, dst, NULL); |
| 624 | SkPaint xfer; |
| 625 | pdfContext->fGraphicsState.applyGraphicsState(&xfer, false); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 626 | // TODO(edisonn): is the blend mode specified already implicitly/explicitly in pdf? |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 627 | xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode |
| 628 | canvas->drawBitmapRect(sMask, dst, &xfer); |
| 629 | canvas->restore(); |
| 630 | } |
| 631 | |
| 632 | canvas->restore(); |
| 633 | |
| 634 | return kPartial_PdfResult; |
| 635 | } |
| 636 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 637 | //TODO(edisonn): options for implementing isolation and knockout |
| 638 | // 1) emulate them (current solution) |
| 639 | // PRO: simple |
| 640 | // CON: will need to use readPixels, which means serious perf issues |
| 641 | // 2) Compile a plan for an array of matrixes, compose the result at the end |
| 642 | // PRO: might be faster then 1, no need to readPixels |
| 643 | // CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan |
| 644 | // on average, a load with empty draw is 100ms on all the skps we have, for complete sites |
| 645 | // 3) support them natively in SkCanvas |
| 646 | // PRO: simple |
| 647 | // CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1) |
| 648 | // 4) compile a plan using pathops, and render once without any fancy rules with backdrop |
| 649 | // PRO: simple, fast |
| 650 | // CON: pathops must be bug free first + time to compute new paths |
| 651 | // pay a price at loading pdf to compute a pdf draw plan |
| 652 | // on average, a load with empty draw is 100ms on all the skps we have, for complete sites |
edisonn@google.com | 251176e | 2013-08-01 13:24:00 +0000 | [diff] [blame] | 653 | // 5) for knockout, render the objects in reverse order, and add every object to the clip, and any new draw will be cliped |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 654 | |
| 655 | |
| 656 | // TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree! |
| 657 | // TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed |
| 658 | |
| 659 | |
| 660 | |
| 661 | static void doGroup_before(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) { |
| 662 | SkRect bboxOrig = bbox; |
| 663 | SkBitmap backdrop; |
| 664 | bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc); |
| 665 | // bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc); |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 666 | SkPaint paint; |
| 667 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 668 | canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL); |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 669 | } |
| 670 | |
edisonn@google.com | 251176e | 2013-08-01 13:24:00 +0000 | [diff] [blame] | 671 | // TODO(edisonn): non isolation implemented in skia |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 672 | //static void doGroup_after(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup) { |
edisonn@google.com | 251176e | 2013-08-01 13:24:00 +0000 | [diff] [blame] | 673 | // if not isolated |
| 674 | // canvas->drawBitmapRect(backdrop, bboxOrig, NULL); |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 675 | //} |
| 676 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 677 | static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 678 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 679 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 680 | } |
| 681 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 682 | if (!skobj->has_BBox()) { |
| 683 | return kIgnoreError_PdfResult; |
| 684 | } |
| 685 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 686 | PdfOp_q(pdfContext, canvas, NULL); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 687 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 688 | canvas->save(); |
| 689 | |
| 690 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 691 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 692 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 693 | } |
| 694 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 695 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 696 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 697 | if (skobj->has_Matrix()) { |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 698 | pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc)); |
| 699 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM; |
| 700 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 701 | // TODO(edisonn) reset matrixTm and matricTlm also? |
| 702 | } |
| 703 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 704 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 705 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 706 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 707 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 708 | SkRect bbox = skobj->BBox(pdfContext->fPdfDoc); |
| 709 | canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 710 | |
| 711 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 712 | // For this PdfContentsTokenizer needs to be extended. |
| 713 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 714 | // This is a group? |
| 715 | if (skobj->has_Group()) { |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 716 | SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc); |
| 717 | doGroup_before(pdfContext, canvas, bbox, tgroup, false); |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 718 | } |
| 719 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 720 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 721 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 722 | SkPdfNativeTokenizer* tokenizer = |
| 723 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 724 | if (tokenizer != NULL) { |
| 725 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 726 | looper.loop(); |
| 727 | delete tokenizer; |
| 728 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 729 | |
| 730 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 731 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 732 | |
| 733 | if (skobj->has_Group()) { |
| 734 | canvas->restore(); |
| 735 | } |
| 736 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 737 | canvas->restore(); |
| 738 | PdfOp_Q(pdfContext, canvas, NULL); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 739 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 740 | } |
| 741 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 742 | //static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
| 743 | // return kNYI_PdfResult; |
| 744 | //} |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 745 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 746 | PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) { |
| 747 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 748 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | PdfOp_q(pdfContext, canvas, NULL); |
| 752 | canvas->save(); |
| 753 | |
| 754 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 755 | pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize)); |
| 756 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 757 | pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm; |
| 758 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 759 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 760 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 761 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 762 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 763 | |
| 764 | SkRect rm = bBox; |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 765 | pdfContext->fGraphicsState.fCTM.mapRect(&rm); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 766 | |
| 767 | SkTraceRect(rm, "bbox mapped"); |
| 768 | |
| 769 | canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
| 770 | |
| 771 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 772 | // For this PdfContentsTokenizer needs to be extended. |
| 773 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 774 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 775 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 776 | SkPdfNativeTokenizer* tokenizer = |
| 777 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 778 | if (tokenizer != NULL) { |
| 779 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 780 | looper.loop(); |
| 781 | delete tokenizer; |
| 782 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 783 | |
| 784 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 785 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 786 | canvas->restore(); |
| 787 | PdfOp_Q(pdfContext, canvas, NULL); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 788 | |
| 789 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 793 | // TODO(edisonn): make sure the pointer is unique |
| 794 | std::set<const SkPdfObject*> gInRendering; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 795 | |
| 796 | class CheckRecursiveRendering { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 797 | const SkPdfObject* fUniqueData; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 798 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 799 | CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 800 | gInRendering.insert(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | ~CheckRecursiveRendering() { |
| 804 | //SkASSERT(fObj.fInRendering); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 805 | gInRendering.erase(fUniqueData); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 806 | } |
| 807 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 808 | static bool IsInRendering(const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 809 | return gInRendering.find(obj) != gInRendering.end(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 810 | } |
| 811 | }; |
| 812 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 813 | static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 814 | if (CheckRecursiveRendering::IsInRendering(obj)) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 815 | // Oops, corrupt PDF! |
| 816 | return kIgnoreError_PdfResult; |
| 817 | } |
| 818 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 819 | CheckRecursiveRendering checkRecursion(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 820 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 821 | switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj)) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 822 | { |
| 823 | case kImageDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 824 | return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 825 | case kType1FormDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 826 | return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 827 | //case kObjectDictionaryXObjectPS_SkPdfObjectType: |
| 828 | //return doXObject_PS(skxobj.asPS()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 829 | default: |
| 830 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 831 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 832 | } |
| 833 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 834 | static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) { |
| 835 | if (!skobj) { |
| 836 | return kIgnoreError_PdfResult; |
| 837 | } |
| 838 | |
| 839 | if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) { |
| 840 | return kNYI_PdfResult; |
| 841 | } |
| 842 | |
| 843 | SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc); |
| 844 | |
| 845 | if (!stream) { |
| 846 | return kIgnoreError_PdfResult; |
| 847 | } |
| 848 | |
| 849 | if (CheckRecursiveRendering::IsInRendering(skobj)) { |
| 850 | // Oops, corrupt PDF! |
| 851 | return kIgnoreError_PdfResult; |
| 852 | } |
| 853 | CheckRecursiveRendering checkRecursion(skobj); |
| 854 | |
| 855 | |
| 856 | PdfOp_q(pdfContext, canvas, NULL); |
| 857 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 858 | |
| 859 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 860 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
| 861 | } |
| 862 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 863 | // TODO(edisonn): MediaBox can be inherited!!!! |
| 864 | SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc); |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 865 | if (skobj->has_Group()) { |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 866 | SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc); |
| 867 | doGroup_before(pdfContext, canvas, bbox, tgroup, true); |
| 868 | } else { |
| 869 | canvas->save(); |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 870 | } |
| 871 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 872 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 873 | SkPdfNativeTokenizer* tokenizer = |
| 874 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
| 875 | if (tokenizer != NULL) { |
| 876 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 877 | looper.loop(); |
| 878 | delete tokenizer; |
| 879 | } |
| 880 | |
| 881 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 882 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 883 | canvas->restore(); |
| 884 | PdfOp_Q(pdfContext, canvas, NULL); |
| 885 | return kPartial_PdfResult; |
| 886 | } |
| 887 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 888 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 889 | pdfContext->fStateStack.push(pdfContext->fGraphicsState); |
| 890 | canvas->save(); |
| 891 | return kOK_PdfResult; |
| 892 | } |
| 893 | |
| 894 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 895 | pdfContext->fGraphicsState = pdfContext->fStateStack.top(); |
| 896 | pdfContext->fStateStack.pop(); |
| 897 | canvas->restore(); |
| 898 | return kOK_PdfResult; |
| 899 | } |
| 900 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 901 | static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 902 | double array[6]; |
| 903 | for (int i = 0 ; i < 6 ; i++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 904 | array[5 - i] = pdfContext->fObjectStack.top()->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 905 | pdfContext->fObjectStack.pop(); |
| 906 | } |
| 907 | |
| 908 | // a b |
| 909 | // c d |
| 910 | // e f |
| 911 | |
| 912 | // 0 1 |
| 913 | // 2 3 |
| 914 | // 4 5 |
| 915 | |
| 916 | // sx ky |
| 917 | // kx sy |
| 918 | // tx ty |
| 919 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 920 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 921 | pdfContext->fGraphicsState.fCTM.preConcat(matrix); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 922 | |
| 923 | #ifdef PDF_TRACE |
| 924 | printf("cm "); |
| 925 | for (int i = 0 ; i < 6 ; i++) { |
| 926 | printf("%f ", array[i]); |
| 927 | } |
| 928 | printf("\n"); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 929 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 930 | #endif |
| 931 | |
| 932 | return kOK_PdfResult; |
| 933 | } |
| 934 | |
| 935 | //leading TL Set the text leading, Tl |
| 936 | //, to leading, which is a number expressed in unscaled text |
| 937 | //space units. Text leading is used only by the T*, ', and " operators. Initial value: 0. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 938 | static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 939 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 940 | |
| 941 | pdfContext->fGraphicsState.fTextLeading = ty; |
| 942 | |
| 943 | return kOK_PdfResult; |
| 944 | } |
| 945 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 946 | static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 947 | #ifdef PDF_TRACE |
| 948 | printf("stack size = %i\n", (int)pdfContext->fObjectStack.size()); |
| 949 | #endif |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 950 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 951 | SkPdfObject* obj = pdfContext->fObjectStack.top(); |
| 952 | obj = obj; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 953 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 954 | |
| 955 | double array[6] = {1, 0, 0, 1, tx, ty}; |
| 956 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 957 | |
| 958 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 959 | pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix); |
| 960 | |
| 961 | return kPartial_PdfResult; |
| 962 | } |
| 963 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 964 | static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 965 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 966 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 967 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 968 | // TODO(edisonn): Create factory methods or constructors so native is hidden |
| 969 | SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 970 | pdfContext->fObjectStack.push(_ty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 971 | |
| 972 | PdfOp_TL(pdfContext, canvas, looper); |
| 973 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 974 | SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 975 | pdfContext->fObjectStack.push(vtx); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 976 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 977 | SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 978 | pdfContext->fObjectStack.push(vty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 979 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 980 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 981 | |
| 982 | // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster |
| 983 | // and the cleanup can happen while the user looks at the image |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 984 | |
| 985 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 986 | } |
| 987 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 988 | static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 989 | double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 990 | double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 991 | double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 992 | double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 993 | double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 994 | double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 995 | |
| 996 | double array[6]; |
| 997 | array[0] = a; |
| 998 | array[1] = b; |
| 999 | array[2] = c; |
| 1000 | array[3] = d; |
| 1001 | array[4] = e; |
| 1002 | array[5] = f; |
| 1003 | |
| 1004 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1005 | matrix.postConcat(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1006 | |
| 1007 | // TODO(edisonn): Text positioning. |
| 1008 | pdfContext->fGraphicsState.fMatrixTm = matrix; |
| 1009 | pdfContext->fGraphicsState.fMatrixTlm = matrix;; |
| 1010 | |
| 1011 | return kPartial_PdfResult; |
| 1012 | } |
| 1013 | |
| 1014 | //— T* Move to the start of the next line. This operator has the same effect as the code |
| 1015 | //0 Tl Td |
| 1016 | //where Tl is the current leading parameter in the text state |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1017 | static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1018 | SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0); |
| 1019 | SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1020 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1021 | pdfContext->fObjectStack.push(zero); |
| 1022 | pdfContext->fObjectStack.push(tl); |
| 1023 | |
| 1024 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 1025 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1026 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1029 | static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1030 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1031 | pdfContext->fGraphicsState.fPath.reset(); |
| 1032 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1033 | } |
| 1034 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1035 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1036 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1037 | |
| 1038 | pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 1039 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 1040 | |
| 1041 | return kOK_PdfResult; |
| 1042 | } |
| 1043 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1044 | static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1045 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1046 | pdfContext->fGraphicsState.fPath.reset(); |
| 1047 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1048 | } |
| 1049 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1050 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1051 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1052 | |
| 1053 | pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 1054 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 1055 | |
| 1056 | return kOK_PdfResult; |
| 1057 | } |
| 1058 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1059 | static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1060 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1061 | pdfContext->fGraphicsState.fPath.reset(); |
| 1062 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1063 | } |
| 1064 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1065 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1066 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1067 | double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1068 | double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1069 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1070 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1071 | |
| 1072 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1073 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1074 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1075 | |
| 1076 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1077 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1078 | |
| 1079 | return kOK_PdfResult; |
| 1080 | } |
| 1081 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1082 | static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1083 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1084 | pdfContext->fGraphicsState.fPath.reset(); |
| 1085 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1086 | } |
| 1087 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1088 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1089 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1090 | double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1091 | double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1092 | double y1 = pdfContext->fGraphicsState.fCurPosY; |
| 1093 | double x1 = pdfContext->fGraphicsState.fCurPosX; |
| 1094 | |
| 1095 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1096 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1097 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1098 | |
| 1099 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1100 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1101 | |
| 1102 | return kOK_PdfResult; |
| 1103 | } |
| 1104 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1105 | static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1106 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1107 | pdfContext->fGraphicsState.fPath.reset(); |
| 1108 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1109 | } |
| 1110 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1111 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1112 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1113 | double y2 = pdfContext->fGraphicsState.fCurPosY; |
| 1114 | double x2 = pdfContext->fGraphicsState.fCurPosX; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1115 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1116 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1117 | |
| 1118 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1119 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1120 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1121 | |
| 1122 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1123 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1124 | |
| 1125 | return kOK_PdfResult; |
| 1126 | } |
| 1127 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1128 | static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1129 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1130 | pdfContext->fGraphicsState.fPath.reset(); |
| 1131 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1132 | } |
| 1133 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1134 | double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1135 | double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1136 | double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1137 | double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1138 | |
| 1139 | pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y), |
| 1140 | SkDoubleToScalar(x + width), SkDoubleToScalar(y + height)); |
| 1141 | |
| 1142 | pdfContext->fGraphicsState.fCurPosX = x; |
| 1143 | pdfContext->fGraphicsState.fCurPosY = y + height; |
| 1144 | |
| 1145 | return kOK_PdfResult; |
| 1146 | } |
| 1147 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1148 | static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1149 | pdfContext->fGraphicsState.fPath.close(); |
| 1150 | return kOK_PdfResult; |
| 1151 | } |
| 1152 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1153 | static PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1154 | SkPath path = pdfContext->fGraphicsState.fPath; |
| 1155 | |
| 1156 | if (close) { |
| 1157 | path.close(); |
| 1158 | } |
| 1159 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1160 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1161 | |
| 1162 | SkPaint paint; |
| 1163 | |
| 1164 | SkPoint line[2]; |
| 1165 | if (fill && !stroke && path.isLine(line)) { |
| 1166 | paint.setStyle(SkPaint::kStroke_Style); |
| 1167 | |
| 1168 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 1169 | paint.setStrokeWidth(SkDoubleToScalar(0)); |
| 1170 | |
| 1171 | canvas->drawPath(path, paint); |
| 1172 | } else { |
| 1173 | if (fill) { |
| 1174 | paint.setStyle(SkPaint::kFill_Style); |
| 1175 | if (evenOdd) { |
| 1176 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1177 | } |
| 1178 | |
| 1179 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 1180 | |
| 1181 | canvas->drawPath(path, paint); |
| 1182 | } |
| 1183 | |
| 1184 | if (stroke) { |
| 1185 | paint.setStyle(SkPaint::kStroke_Style); |
| 1186 | |
| 1187 | pdfContext->fGraphicsState.applyGraphicsState(&paint, true); |
| 1188 | |
| 1189 | path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke |
| 1190 | canvas->drawPath(path, paint); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | pdfContext->fGraphicsState.fPath.reset(); |
| 1195 | // todo zoom ... other stuff ? |
| 1196 | |
| 1197 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1198 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1199 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1200 | #endif |
| 1201 | } |
| 1202 | |
| 1203 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1204 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1205 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1206 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1207 | |
| 1208 | } |
| 1209 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1210 | static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1211 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false); |
| 1212 | } |
| 1213 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1214 | static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1215 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false); |
| 1216 | } |
| 1217 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1218 | static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1219 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1220 | } |
| 1221 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1222 | static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1223 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1224 | } |
| 1225 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1226 | static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1227 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true); |
| 1228 | } |
| 1229 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1230 | static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1231 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false); |
| 1232 | } |
| 1233 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1234 | static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1235 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true); |
| 1236 | } |
| 1237 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1238 | static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1239 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false); |
| 1240 | } |
| 1241 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1242 | static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1243 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true); |
| 1244 | } |
| 1245 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1246 | static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1247 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1248 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1249 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1250 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1251 | #endif |
| 1252 | } |
| 1253 | |
| 1254 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1255 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1256 | |
| 1257 | pdfContext->fGraphicsState.fPathClosed = true; |
| 1258 | |
| 1259 | return kOK_PdfResult; |
| 1260 | } |
| 1261 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1262 | static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1263 | pdfContext->fGraphicsState.fTextBlock = true; |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1264 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM; |
| 1265 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1266 | |
| 1267 | return kPartial_PdfResult; |
| 1268 | } |
| 1269 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1270 | static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1271 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1272 | return kIgnoreError_PdfResult; |
| 1273 | } |
| 1274 | // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack? |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1275 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1278 | PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1279 | #ifdef PDF_TRACE |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1280 | printf("font name: %s\n", fontName->nameValue2().c_str()); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1281 | #endif |
| 1282 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1283 | if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) { |
| 1284 | // TODO(edisonn): try to recover and draw it any way? |
| 1285 | return kIgnoreError_PdfResult; |
| 1286 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1287 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1288 | SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName); |
| 1289 | objFont = pdfContext->fPdfDoc->resolveReference(objFont); |
| 1290 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) { |
| 1291 | // TODO(edisonn): try to recover and draw it any way? |
| 1292 | return kIgnoreError_PdfResult; |
| 1293 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1294 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1295 | SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont; |
| 1296 | |
| 1297 | SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd); |
| 1298 | |
| 1299 | if (skfont) { |
| 1300 | pdfContext->fGraphicsState.fSkFont = skfont; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1301 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1302 | pdfContext->fGraphicsState.fCurFontSize = fontSize; |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1303 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1306 | //font size Tf Set the text font, Tf |
| 1307 | //, to font and the text font size, Tfs, to size. font is the name of a |
| 1308 | //font resource in the Fontsubdictionary of the current resource dictionary; size is |
| 1309 | //a number representing a scale factor. There is no initial value for either font or |
| 1310 | //size; they must be specified explicitly using Tf before any text is shown. |
| 1311 | static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1312 | double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1313 | SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1314 | return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize); |
| 1315 | } |
| 1316 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1317 | static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1318 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1319 | // TODO(edisonn): try to recover and draw it any way? |
| 1320 | return kIgnoreError_PdfResult; |
| 1321 | } |
| 1322 | |
| 1323 | PdfResult ret = DrawText(pdfContext, |
| 1324 | pdfContext->fObjectStack.top(), |
| 1325 | canvas); |
| 1326 | pdfContext->fObjectStack.pop(); |
| 1327 | |
| 1328 | return ret; |
| 1329 | } |
| 1330 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1331 | static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1332 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1333 | // TODO(edisonn): try to recover and draw it any way? |
| 1334 | return kIgnoreError_PdfResult; |
| 1335 | } |
| 1336 | |
| 1337 | PdfOp_T_star(pdfContext, canvas, looper); |
| 1338 | // Do not pop, and push, just transfer the param to Tj |
| 1339 | return PdfOp_Tj(pdfContext, canvas, looper); |
| 1340 | } |
| 1341 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1342 | static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1343 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1344 | // TODO(edisonn): try to recover and draw it any way? |
| 1345 | return kIgnoreError_PdfResult; |
| 1346 | } |
| 1347 | |
| 1348 | SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1349 | SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1350 | SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1351 | |
| 1352 | pdfContext->fObjectStack.push(aw); |
| 1353 | PdfOp_Tw(pdfContext, canvas, looper); |
| 1354 | |
| 1355 | pdfContext->fObjectStack.push(ac); |
| 1356 | PdfOp_Tc(pdfContext, canvas, looper); |
| 1357 | |
| 1358 | pdfContext->fObjectStack.push(str); |
| 1359 | PdfOp_quote(pdfContext, canvas, looper); |
| 1360 | |
| 1361 | return kPartial_PdfResult; |
| 1362 | } |
| 1363 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1364 | static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1365 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1366 | // TODO(edisonn): try to recover and draw it any way? |
| 1367 | return kIgnoreError_PdfResult; |
| 1368 | } |
| 1369 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1370 | SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1371 | pdfContext->fObjectStack.pop(); |
| 1372 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1373 | if (!array->isArray()) { |
| 1374 | return kIgnoreError_PdfResult; |
| 1375 | } |
| 1376 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1377 | for( int i=0; i<static_cast<int>(array->size()); i++ ) |
| 1378 | { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1379 | if( (*array)[i]->isAnyString()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1380 | SkPdfObject* obj = (*array)[i]; |
| 1381 | DrawText(pdfContext, |
| 1382 | obj, |
| 1383 | canvas); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1384 | } else if ((*array)[i]->isNumber()) { |
| 1385 | double dx = (*array)[i]->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1386 | SkMatrix matrix; |
| 1387 | matrix.setAll(SkDoubleToScalar(1), |
| 1388 | SkDoubleToScalar(0), |
| 1389 | // TODO(edisonn): use writing mode, vertical/horizontal. |
| 1390 | SkDoubleToScalar(-dx), // amount is substracted!!! |
| 1391 | SkDoubleToScalar(0), |
| 1392 | SkDoubleToScalar(1), |
| 1393 | SkDoubleToScalar(0), |
| 1394 | SkDoubleToScalar(0), |
| 1395 | SkDoubleToScalar(0), |
| 1396 | SkDoubleToScalar(1)); |
| 1397 | |
| 1398 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 1399 | } |
| 1400 | } |
| 1401 | return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK. |
| 1402 | } |
| 1403 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1404 | static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1405 | colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1406 | return kOK_PdfResult; |
| 1407 | } |
| 1408 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1409 | static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1410 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1411 | } |
| 1412 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1413 | static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1414 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1415 | } |
| 1416 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1417 | static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1418 | double c[4]; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1419 | // int64_t v[4]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1420 | |
| 1421 | int n = GetColorSpaceComponents(colorOperator->fColorSpace); |
| 1422 | |
| 1423 | bool doubles = true; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1424 | if (colorOperator->fColorSpace.equals("Indexed")) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1425 | doubles = false; |
| 1426 | } |
| 1427 | |
| 1428 | #ifdef PDF_TRACE |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1429 | printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1430 | #endif |
| 1431 | |
| 1432 | for (int i = n - 1; i >= 0 ; i--) { |
| 1433 | if (doubles) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1434 | c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1435 | // } else { |
| 1436 | // v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | // TODO(edisonn): Now, set that color. Only DeviceRGB supported. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1441 | // TODO(edisonn): do possible field values to enum at parsing time! |
| 1442 | // TODO(edisonn): support also abreviations /DeviceRGB == /RGB |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1443 | if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) { |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1444 | colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*c[0]), (U8CPU)(255*c[1]), (U8CPU)(255*c[2]))); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1445 | } |
| 1446 | return kPartial_PdfResult; |
| 1447 | } |
| 1448 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1449 | static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1450 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1451 | } |
| 1452 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1453 | static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1454 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1455 | } |
| 1456 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1457 | static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1458 | //SkPdfString* name; |
| 1459 | if (pdfContext->fObjectStack.top()->isName()) { |
| 1460 | // TODO(edisonn): get name, pass it |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1461 | pdfContext->fObjectStack.pop(); |
| 1462 | } |
| 1463 | |
| 1464 | // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec. |
| 1465 | PdfOp_SC_sc(pdfContext, canvas, colorOperator); |
| 1466 | |
| 1467 | return kPartial_PdfResult; |
| 1468 | } |
| 1469 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1470 | static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1471 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1472 | } |
| 1473 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1474 | static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1475 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1476 | } |
| 1477 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1478 | static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1479 | /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1480 | return kNYI_PdfResult; |
| 1481 | } |
| 1482 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1483 | static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1484 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1485 | } |
| 1486 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1487 | static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1488 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1489 | } |
| 1490 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1491 | static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1492 | double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1493 | double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1494 | double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1495 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1496 | colorOperator->fColorSpace = strings_DeviceRGB; |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1497 | colorOperator->setRGBColor(SkColorSetRGB((U8CPU)(255*r), (U8CPU)(255*g), (U8CPU)(255*b))); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1498 | return kOK_PdfResult; |
| 1499 | } |
| 1500 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1501 | static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1502 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1503 | } |
| 1504 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1505 | static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1506 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1507 | } |
| 1508 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1509 | static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1510 | // TODO(edisonn): spec has some rules about overprint, implement them. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1511 | /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1512 | /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1513 | /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1514 | /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1515 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1516 | colorOperator->fColorSpace = strings_DeviceCMYK; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1517 | // TODO(edisonn): Set color. |
| 1518 | return kNYI_PdfResult; |
| 1519 | } |
| 1520 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1521 | static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1522 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1523 | } |
| 1524 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1525 | static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1526 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1527 | } |
| 1528 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1529 | static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1530 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1531 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1532 | |
| 1533 | return kOK_PdfResult; |
| 1534 | } |
| 1535 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1536 | static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1537 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1538 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1539 | pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType); |
| 1540 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1541 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1542 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1545 | static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1546 | *looper = new PdfCompatibilitySectionLooper(); |
| 1547 | return kOK_PdfResult; |
| 1548 | } |
| 1549 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1550 | static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1551 | #ifdef ASSERT_BAD_PDF_OPS |
| 1552 | SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's |
| 1553 | // have the assert when testing good pdfs. |
| 1554 | #endif |
| 1555 | return kIgnoreError_PdfResult; |
| 1556 | } |
| 1557 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1558 | static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1559 | *looper = new PdfInlineImageLooper(); |
| 1560 | return kOK_PdfResult; |
| 1561 | } |
| 1562 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1563 | static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1564 | #ifdef ASSERT_BAD_PDF_OPS |
| 1565 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 1566 | // have the assert when testing good pdfs. |
| 1567 | #endif |
| 1568 | return kIgnoreError_PdfResult; |
| 1569 | } |
| 1570 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1571 | static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1572 | #ifdef ASSERT_BAD_PDF_OPS |
| 1573 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 1574 | // have the assert when testing good pdfs. |
| 1575 | #endif |
| 1576 | return kIgnoreError_PdfResult; |
| 1577 | } |
| 1578 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1579 | |
| 1580 | // TODO(edisonn): security review here, make sure all parameters are valid, and safe. |
| 1581 | PdfResult skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) { |
| 1582 | pdfContext->fGraphicsState.fNonStroking.fOpacity = ca; |
| 1583 | return kOK_PdfResult; |
| 1584 | } |
| 1585 | |
| 1586 | PdfResult skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) { |
| 1587 | pdfContext->fGraphicsState.fStroking.fOpacity = CA; |
| 1588 | return kOK_PdfResult; |
| 1589 | } |
| 1590 | |
| 1591 | PdfResult skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1592 | pdfContext->fGraphicsState.fLineWidth = lineWidth; |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1593 | return kOK_PdfResult; |
| 1594 | } |
| 1595 | |
| 1596 | PdfResult skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) { |
| 1597 | pdfContext->fGraphicsState.fLineCap = (int)lineCap; |
| 1598 | return kOK_PdfResult; |
| 1599 | } |
| 1600 | |
| 1601 | PdfResult skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) { |
| 1602 | pdfContext->fGraphicsState.fLineJoin = (int)lineJoin; |
| 1603 | return kOK_PdfResult; |
| 1604 | } |
| 1605 | |
| 1606 | PdfResult skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) { |
| 1607 | pdfContext->fGraphicsState.fMiterLimit = miterLimit; |
| 1608 | return kOK_PdfResult; |
| 1609 | } |
| 1610 | |
| 1611 | // TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure |
| 1612 | /* |
| 1613 | 1) [ ] 0 No dash; solid, unbroken lines |
| 1614 | 2) [3] 0 3 units on, 3 units off, … |
| 1615 | 3) [2] 1 1 on, 2 off, 2 on, 2 off, … |
| 1616 | 4) [2 1] 0 2 on, 1 off, 2 on, 1 off, … |
| 1617 | 5) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, … |
| 1618 | 6) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, … |
| 1619 | */ |
| 1620 | |
| 1621 | PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* intervals, SkPdfObject* phase) { |
| 1622 | int cnt = intervals->size(); |
| 1623 | if (cnt >= 256) { |
| 1624 | // TODO(edisonn): report error/warning, unsuported; |
| 1625 | // TODO(edisonn): alloc memory |
| 1626 | return kIgnoreError_PdfResult; |
| 1627 | } |
| 1628 | for (int i = 0; i < cnt; i++) { |
| 1629 | if (!intervals->objAtAIndex(i)->isNumber()) { |
| 1630 | // TODO(edisonn): report error/warning |
| 1631 | return kIgnoreError_PdfResult; |
| 1632 | } |
| 1633 | } |
| 1634 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1635 | double total = 0; |
| 1636 | for (int i = 0 ; i < cnt; i++) { |
| 1637 | pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue(); |
| 1638 | total += pdfContext->fGraphicsState.fDashArray[i]; |
| 1639 | } |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 1640 | if (cnt & 1) { |
| 1641 | if (cnt == 1) { |
| 1642 | pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0]; |
| 1643 | cnt++; |
| 1644 | } else { |
| 1645 | // TODO(edisonn): report error/warning |
| 1646 | return kNYI_PdfResult; |
| 1647 | } |
| 1648 | } |
| 1649 | pdfContext->fGraphicsState.fDashArrayLength = cnt; |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1650 | pdfContext->fGraphicsState.fDashPhase = phase->scalarValue(); |
| 1651 | if (pdfContext->fGraphicsState.fDashPhase == 0) { |
| 1652 | // other rules, changes? |
| 1653 | pdfContext->fGraphicsState.fDashPhase = total; |
| 1654 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1655 | |
| 1656 | return kOK_PdfResult; |
| 1657 | } |
| 1658 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1659 | PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) { |
| 1660 | // TODO(edisonn): verify input |
| 1661 | if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) { |
| 1662 | // TODO(edisonn): report error/warning |
| 1663 | return kIgnoreError_PdfResult; |
| 1664 | } |
| 1665 | return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1)); |
| 1666 | } |
| 1667 | |
| 1668 | void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) { |
| 1669 | if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) { |
| 1670 | // TODO(edisonn): report error/warning |
| 1671 | return; |
| 1672 | } |
| 1673 | skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue()); |
| 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | //lineWidth w Set the line width in the graphics state (see “Line Width” on page 152). |
| 1678 | static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1679 | double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1680 | return skpdfGraphicsStateApplyLW(pdfContext, lw); |
| 1681 | } |
| 1682 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1683 | //lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1684 | static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1685 | int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1686 | return skpdfGraphicsStateApplyLC(pdfContext, lc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | //lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1690 | static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1691 | double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1692 | return skpdfGraphicsStateApplyLJ(pdfContext, lj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | //miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1696 | static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1697 | double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1698 | return skpdfGraphicsStateApplyML(pdfContext, ml); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | //dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on |
| 1702 | //page 155). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1703 | static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1704 | SkPdfObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1705 | SkPdfObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1706 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1707 | if (!array->isArray()) { |
| 1708 | return kIgnoreError_PdfResult; |
| 1709 | } |
| 1710 | |
| 1711 | return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | //intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1715 | static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1716 | pdfContext->fObjectStack.pop(); |
| 1717 | |
| 1718 | return kNYI_PdfResult; |
| 1719 | } |
| 1720 | |
| 1721 | //flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness |
| 1722 | //Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci- |
| 1723 | //fies the output device’s default flatness tolerance. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1724 | static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1725 | pdfContext->fObjectStack.pop(); |
| 1726 | |
| 1727 | return kNYI_PdfResult; |
| 1728 | } |
| 1729 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1730 | SkTDict<SkXfermode::Mode> gPdfBlendModes(20); |
| 1731 | |
| 1732 | class InitBlendModes { |
| 1733 | public: |
| 1734 | InitBlendModes() { |
| 1735 | // TODO(edisonn): use the python code generator? |
| 1736 | // TABLE 7.2 Standard separable blend modes |
| 1737 | gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode); |
| 1738 | gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode); |
| 1739 | gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode); |
| 1740 | gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode); |
| 1741 | gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode); |
| 1742 | gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode); |
| 1743 | gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode); |
| 1744 | gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode); |
| 1745 | gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode); |
| 1746 | gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode); |
| 1747 | gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode); |
| 1748 | gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode); |
| 1749 | |
| 1750 | // TABLE 7.3 Standard nonseparable blend modes |
| 1751 | gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode); |
| 1752 | gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode); |
| 1753 | gPdfBlendModes.set("Color", SkXfermode::kColor_Mode); |
| 1754 | gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode); |
| 1755 | } |
| 1756 | }; |
| 1757 | |
| 1758 | InitBlendModes _gDummyInniter; |
| 1759 | |
| 1760 | SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) { |
| 1761 | SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1); |
| 1762 | if (gPdfBlendModes.find(blendMode, len, &mode)) { |
| 1763 | return mode; |
| 1764 | } |
| 1765 | |
| 1766 | return (SkXfermode::Mode)(SkXfermode::kLastMode + 1); |
| 1767 | } |
| 1768 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1769 | void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) { |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1770 | SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length()); |
| 1771 | if (mode <= SkXfermode::kLastMode) { |
| 1772 | pdfContext->fGraphicsState.fBlendModesLength = 1; |
| 1773 | pdfContext->fGraphicsState.fBlendModes[0] = mode; |
| 1774 | } else { |
| 1775 | // TODO(edisonn): report unknown blend mode |
| 1776 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) { |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1780 | if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) { |
| 1781 | // TODO(edisonn): report error/warning |
| 1782 | return; |
| 1783 | } |
| 1784 | SkXfermode::Mode modes[256]; |
| 1785 | int cnt = blendModes->size(); |
| 1786 | for (int i = 0; i < cnt; i++) { |
| 1787 | SkPdfObject* name = blendModes->objAtAIndex(i); |
| 1788 | if (!name->isName()) { |
| 1789 | // TODO(edisonn): report error/warning |
| 1790 | return; |
| 1791 | } |
| 1792 | SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr()); |
| 1793 | if (mode > SkXfermode::kLastMode) { |
| 1794 | // TODO(edisonn): report error/warning |
| 1795 | return; |
| 1796 | } |
| 1797 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1798 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1799 | pdfContext->fGraphicsState.fBlendModesLength = cnt; |
| 1800 | for (int i = 0; i < cnt; i++) { |
| 1801 | pdfContext->fGraphicsState.fBlendModes[i] = modes[i]; |
| 1802 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) { |
| 1806 | // TODO(edisonn): verify input |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1807 | if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 1808 | pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask; |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1809 | } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) { |
| 1810 | SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask; |
| 1811 | pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true); |
| 1812 | } else { |
| 1813 | // TODO (edisonn): report error/warning |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 1818 | if (sMask == "None") { |
| 1819 | pdfContext->fGraphicsState.fSoftMaskDictionary = NULL; |
| 1820 | pdfContext->fGraphicsState.fSMask = SkBitmap(); |
| 1821 | return; |
| 1822 | } |
| 1823 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1824 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
| 1825 | SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc); |
| 1826 | |
| 1827 | if (extGStateDictionary == NULL) { |
| 1828 | #ifdef PDF_TRACE |
| 1829 | printf("ExtGState is NULL!\n"); |
| 1830 | #endif |
| 1831 | // TODO (edisonn): report error/warning |
| 1832 | return; |
| 1833 | } |
| 1834 | |
| 1835 | SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str())); |
| 1836 | if (!obj || !obj->isDictionary()) { |
| 1837 | // TODO (edisonn): report error/warning |
| 1838 | return; |
| 1839 | } |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 1840 | |
| 1841 | pdfContext->fGraphicsState.fSoftMaskDictionary = NULL; |
| 1842 | pdfContext->fGraphicsState.fSMask = SkBitmap(); |
| 1843 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1844 | skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary()); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) { |
| 1848 | pdfContext->fGraphicsState.fAlphaSource = alphaSource; |
| 1849 | } |
| 1850 | |
| 1851 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1852 | //dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is |
| 1853 | //the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1854 | static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1855 | SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1856 | |
| 1857 | #ifdef PDF_TRACE |
| 1858 | std::string str; |
| 1859 | #endif |
| 1860 | |
| 1861 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1862 | SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1863 | |
| 1864 | if (extGStateDictionary == NULL) { |
| 1865 | #ifdef PDF_TRACE |
| 1866 | printf("ExtGState is NULL!\n"); |
| 1867 | #endif |
| 1868 | return kIgnoreError_PdfResult; |
| 1869 | } |
| 1870 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1871 | SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1872 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1873 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) { |
| 1874 | return kIgnoreError_PdfResult; |
| 1875 | } |
| 1876 | SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1877 | |
| 1878 | // TODO(edisonn): now load all those properties in graphic state. |
| 1879 | if (gs == NULL) { |
| 1880 | return kIgnoreError_PdfResult; |
| 1881 | } |
| 1882 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1883 | if (gs->has_LW()) { |
| 1884 | skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc)); |
| 1885 | } |
| 1886 | |
| 1887 | if (gs->has_LC()) { |
| 1888 | skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc)); |
| 1889 | } |
| 1890 | |
| 1891 | if (gs->has_LJ()) { |
| 1892 | skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc)); |
| 1893 | } |
| 1894 | |
| 1895 | if (gs->has_ML()) { |
| 1896 | skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc)); |
| 1897 | } |
| 1898 | |
| 1899 | if (gs->has_D()) { |
| 1900 | skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc)); |
| 1901 | } |
| 1902 | |
| 1903 | if (gs->has_Font()) { |
| 1904 | skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc)); |
| 1905 | } |
| 1906 | |
| 1907 | if (gs->has_BM()) { |
| 1908 | if (gs->isBMAName(pdfContext->fPdfDoc)) { |
| 1909 | skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc)); |
| 1910 | } else if (gs->isBMAArray(pdfContext->fPdfDoc)) { |
| 1911 | skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc)); |
| 1912 | } else { |
| 1913 | // TODO(edisonn): report/warn |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | if (gs->has_SMask()) { |
| 1918 | if (gs->isSMaskAName(pdfContext->fPdfDoc)) { |
| 1919 | skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc)); |
| 1920 | } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) { |
| 1921 | skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc)); |
| 1922 | } else { |
| 1923 | // TODO(edisonn): report/warn |
| 1924 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | if (gs->has_ca()) { |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1928 | skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1931 | if (gs->has_CA()) { |
| 1932 | skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc)); |
| 1933 | } |
| 1934 | |
| 1935 | if (gs->has_AIS()) { |
| 1936 | skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1939 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | //charSpace Tc Set the character spacing, Tc |
| 1943 | //, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators. |
| 1944 | //Initial value: 0. |
| 1945 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1946 | double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1947 | pdfContext->fGraphicsState.fCharSpace = charSpace; |
| 1948 | |
| 1949 | return kOK_PdfResult; |
| 1950 | } |
| 1951 | |
| 1952 | //wordSpace Tw Set the word spacing, T |
| 1953 | //w |
| 1954 | //, to wordSpace, which is a number expressed in unscaled |
| 1955 | //text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial |
| 1956 | //value: 0. |
| 1957 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1958 | double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1959 | pdfContext->fGraphicsState.fWordSpace = wordSpace; |
| 1960 | |
| 1961 | return kOK_PdfResult; |
| 1962 | } |
| 1963 | |
| 1964 | //scale Tz Set the horizontal scaling, Th |
| 1965 | //, to (scale ˜ 100). scale is a number specifying the |
| 1966 | //percentage of the normal width. Initial value: 100 (normal width). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1967 | static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1968 | /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1969 | |
| 1970 | return kNYI_PdfResult; |
| 1971 | } |
| 1972 | |
| 1973 | //render Tr Set the text rendering mode, T |
| 1974 | //mode, to render, which is an integer. Initial value: 0. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1975 | static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1976 | /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1977 | |
| 1978 | return kNYI_PdfResult; |
| 1979 | } |
| 1980 | //rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space |
| 1981 | //units. Initial value: 0. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1982 | static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1983 | /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1984 | |
| 1985 | return kNYI_PdfResult; |
| 1986 | } |
| 1987 | |
| 1988 | //wx wy d0 |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1989 | static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1990 | pdfContext->fObjectStack.pop(); |
| 1991 | pdfContext->fObjectStack.pop(); |
| 1992 | |
| 1993 | return kNYI_PdfResult; |
| 1994 | } |
| 1995 | |
| 1996 | //wx wy llx lly urx ury d1 |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1997 | static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1998 | pdfContext->fObjectStack.pop(); |
| 1999 | pdfContext->fObjectStack.pop(); |
| 2000 | pdfContext->fObjectStack.pop(); |
| 2001 | pdfContext->fObjectStack.pop(); |
| 2002 | pdfContext->fObjectStack.pop(); |
| 2003 | pdfContext->fObjectStack.pop(); |
| 2004 | |
| 2005 | return kNYI_PdfResult; |
| 2006 | } |
| 2007 | |
| 2008 | //name sh |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2009 | static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2010 | pdfContext->fObjectStack.pop(); |
| 2011 | |
| 2012 | return kNYI_PdfResult; |
| 2013 | } |
| 2014 | |
| 2015 | //name Do |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2016 | static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2017 | SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2018 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2019 | SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2020 | |
| 2021 | if (xObject == NULL) { |
| 2022 | #ifdef PDF_TRACE |
| 2023 | printf("XObject is NULL!\n"); |
| 2024 | #endif |
| 2025 | return kIgnoreError_PdfResult; |
| 2026 | } |
| 2027 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2028 | SkPdfObject* value = xObject->get(name); |
| 2029 | value = pdfContext->fPdfDoc->resolveReference(value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2030 | |
| 2031 | #ifdef PDF_TRACE |
| 2032 | // value->ToString(str); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2033 | // printf("Do object value: %s\n", str); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2034 | #endif |
| 2035 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2036 | return doXObject(pdfContext, canvas, value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | //tag MP Designate a marked-content point. tag is a name object indicating the role or |
| 2040 | //significance of the point. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2041 | static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2042 | pdfContext->fObjectStack.pop(); |
| 2043 | |
| 2044 | return kNYI_PdfResult; |
| 2045 | } |
| 2046 | |
| 2047 | //tag properties DP Designate a marked-content point with an associated property list. tag is a |
| 2048 | //name object indicating the role or significance of the point; properties is |
| 2049 | //either an inline dictionary containing the property list or a name object |
| 2050 | //associated with it in the Properties subdictionary of the current resource |
| 2051 | //dictionary (see Section 9.5.1, “Property Lists”). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2052 | static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2053 | pdfContext->fObjectStack.pop(); |
| 2054 | pdfContext->fObjectStack.pop(); |
| 2055 | |
| 2056 | return kNYI_PdfResult; |
| 2057 | } |
| 2058 | |
| 2059 | //tag BMC Begin a marked-content sequence terminated by a balancing EMC operator. |
| 2060 | //tag is a name object indicating the role or significance of the sequence. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2061 | static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2062 | pdfContext->fObjectStack.pop(); |
| 2063 | |
| 2064 | return kNYI_PdfResult; |
| 2065 | } |
| 2066 | |
| 2067 | //tag properties BDC Begin a marked-content sequence with an associated property list, terminated |
| 2068 | //by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the |
| 2069 | //property list or a name object associated with it in the Properties subdictionary of the current resource dictionary (see Section 9.5.1, “Property Lists”). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2070 | static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2071 | pdfContext->fObjectStack.pop(); |
| 2072 | pdfContext->fObjectStack.pop(); |
| 2073 | |
| 2074 | return kNYI_PdfResult; |
| 2075 | } |
| 2076 | |
| 2077 | //— EMC End a marked-content sequence begun by a BMC or BDC operator. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2078 | static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2079 | return kNYI_PdfResult; |
| 2080 | } |
| 2081 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2082 | static void initPdfOperatorRenderes() { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2083 | static bool gInitialized = false; |
| 2084 | if (gInitialized) { |
| 2085 | return; |
| 2086 | } |
| 2087 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2088 | gPdfOps.set("q", PdfOp_q); |
| 2089 | gPdfOps.set("Q", PdfOp_Q); |
| 2090 | gPdfOps.set("cm", PdfOp_cm); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2091 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2092 | gPdfOps.set("TD", PdfOp_TD); |
| 2093 | gPdfOps.set("Td", PdfOp_Td); |
| 2094 | gPdfOps.set("Tm", PdfOp_Tm); |
| 2095 | gPdfOps.set("T*", PdfOp_T_star); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2096 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2097 | gPdfOps.set("m", PdfOp_m); |
| 2098 | gPdfOps.set("l", PdfOp_l); |
| 2099 | gPdfOps.set("c", PdfOp_c); |
| 2100 | gPdfOps.set("v", PdfOp_v); |
| 2101 | gPdfOps.set("y", PdfOp_y); |
| 2102 | gPdfOps.set("h", PdfOp_h); |
| 2103 | gPdfOps.set("re", PdfOp_re); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2104 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2105 | gPdfOps.set("S", PdfOp_S); |
| 2106 | gPdfOps.set("s", PdfOp_s); |
| 2107 | gPdfOps.set("f", PdfOp_f); |
| 2108 | gPdfOps.set("F", PdfOp_F); |
| 2109 | gPdfOps.set("f*", PdfOp_f_star); |
| 2110 | gPdfOps.set("B", PdfOp_B); |
| 2111 | gPdfOps.set("B*", PdfOp_B_star); |
| 2112 | gPdfOps.set("b", PdfOp_b); |
| 2113 | gPdfOps.set("b*", PdfOp_b_star); |
| 2114 | gPdfOps.set("n", PdfOp_n); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2115 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2116 | gPdfOps.set("BT", PdfOp_BT); |
| 2117 | gPdfOps.set("ET", PdfOp_ET); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2118 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2119 | gPdfOps.set("Tj", PdfOp_Tj); |
| 2120 | gPdfOps.set("'", PdfOp_quote); |
| 2121 | gPdfOps.set("\"", PdfOp_doublequote); |
| 2122 | gPdfOps.set("TJ", PdfOp_TJ); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2123 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2124 | gPdfOps.set("CS", PdfOp_CS); |
| 2125 | gPdfOps.set("cs", PdfOp_cs); |
| 2126 | gPdfOps.set("SC", PdfOp_SC); |
| 2127 | gPdfOps.set("SCN", PdfOp_SCN); |
| 2128 | gPdfOps.set("sc", PdfOp_sc); |
| 2129 | gPdfOps.set("scn", PdfOp_scn); |
| 2130 | gPdfOps.set("G", PdfOp_G); |
| 2131 | gPdfOps.set("g", PdfOp_g); |
| 2132 | gPdfOps.set("RG", PdfOp_RG); |
| 2133 | gPdfOps.set("rg", PdfOp_rg); |
| 2134 | gPdfOps.set("K", PdfOp_K); |
| 2135 | gPdfOps.set("k", PdfOp_k); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2136 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2137 | gPdfOps.set("W", PdfOp_W); |
| 2138 | gPdfOps.set("W*", PdfOp_W_star); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2139 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2140 | gPdfOps.set("BX", PdfOp_BX); |
| 2141 | gPdfOps.set("EX", PdfOp_EX); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2142 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2143 | gPdfOps.set("BI", PdfOp_BI); |
| 2144 | gPdfOps.set("ID", PdfOp_ID); |
| 2145 | gPdfOps.set("EI", PdfOp_EI); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2146 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2147 | gPdfOps.set("w", PdfOp_w); |
| 2148 | gPdfOps.set("J", PdfOp_J); |
| 2149 | gPdfOps.set("j", PdfOp_j); |
| 2150 | gPdfOps.set("M", PdfOp_M); |
| 2151 | gPdfOps.set("d", PdfOp_d); |
| 2152 | gPdfOps.set("ri", PdfOp_ri); |
| 2153 | gPdfOps.set("i", PdfOp_i); |
| 2154 | gPdfOps.set("gs", PdfOp_gs); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2155 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2156 | gPdfOps.set("Tc", PdfOp_Tc); |
| 2157 | gPdfOps.set("Tw", PdfOp_Tw); |
| 2158 | gPdfOps.set("Tz", PdfOp_Tz); |
| 2159 | gPdfOps.set("TL", PdfOp_TL); |
| 2160 | gPdfOps.set("Tf", PdfOp_Tf); |
| 2161 | gPdfOps.set("Tr", PdfOp_Tr); |
| 2162 | gPdfOps.set("Ts", PdfOp_Ts); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2163 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2164 | gPdfOps.set("d0", PdfOp_d0); |
| 2165 | gPdfOps.set("d1", PdfOp_d1); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2166 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2167 | gPdfOps.set("sh", PdfOp_sh); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2168 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2169 | gPdfOps.set("Do", PdfOp_Do); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2170 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2171 | gPdfOps.set("MP", PdfOp_MP); |
| 2172 | gPdfOps.set("DP", PdfOp_DP); |
| 2173 | gPdfOps.set("BMC", PdfOp_BMC); |
| 2174 | gPdfOps.set("BDC", PdfOp_BDC); |
| 2175 | gPdfOps.set("EMC", PdfOp_EMC); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2176 | |
| 2177 | gInitialized = true; |
| 2178 | } |
| 2179 | |
| 2180 | class InitPdfOps { |
| 2181 | public: |
| 2182 | InitPdfOps() { |
| 2183 | initPdfOperatorRenderes(); |
| 2184 | } |
| 2185 | }; |
| 2186 | |
| 2187 | InitPdfOps gInitPdfOps; |
| 2188 | |
| 2189 | void reportPdfRenderStats() { |
| 2190 | std::map<std::string, int>::iterator iter; |
| 2191 | |
| 2192 | for (int i = 0 ; i < kCount_PdfResult; i++) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2193 | SkTDict<int>::Iter iter(gRenderStats[i]); |
| 2194 | const char* key; |
| 2195 | int value = 0; |
| 2196 | while ((key = iter.next(&value)) != NULL) { |
| 2197 | printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | PdfResult PdfMainLooper::consumeToken(PdfToken& token) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2203 | if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2204 | { |
| 2205 | // TODO(edisonn): log trace flag (verbose, error, info, warning, ...) |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2206 | PdfOperatorRenderer pdfOperatorRenderer = NULL; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2207 | if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2208 | // caller, main work is done by pdfOperatorRenderer(...) |
| 2209 | PdfTokenLooper* childLooper = NULL; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2210 | PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper); |
| 2211 | |
| 2212 | int cnt = 0; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2213 | gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt); |
| 2214 | gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2215 | |
| 2216 | if (childLooper) { |
| 2217 | childLooper->setUp(this); |
| 2218 | childLooper->loop(); |
| 2219 | delete childLooper; |
| 2220 | } |
| 2221 | } else { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2222 | int cnt = 0; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2223 | gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt); |
| 2224 | gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | else if (token.fType == kObject_TokenType) |
| 2228 | { |
| 2229 | fPdfContext->fObjectStack.push( token.fObject ); |
| 2230 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2231 | else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2232 | // TODO(edisonn): deine or use assert not reached |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2233 | return kIgnoreError_PdfResult; |
| 2234 | } |
| 2235 | return kOK_PdfResult; |
| 2236 | } |
| 2237 | |
| 2238 | void PdfMainLooper::loop() { |
| 2239 | PdfToken token; |
| 2240 | while (readToken(fTokenizer, &token)) { |
| 2241 | consumeToken(token); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) { |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 2246 | SkASSERT(false); |
| 2247 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | void PdfInlineImageLooper::loop() { |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 2251 | doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage()); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | PdfResult PdfInlineImageLooper::done() { |
| 2255 | |
| 2256 | // TODO(edisonn): long to short names |
| 2257 | // TODO(edisonn): set properties in a map |
| 2258 | // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress |
| 2259 | // the stream. |
| 2260 | |
| 2261 | SkBitmap bitmap; |
| 2262 | setup_bitmap(&bitmap, 50, 50, SK_ColorRED); |
| 2263 | |
| 2264 | // TODO(edisonn): matrix use. |
| 2265 | // Draw dummy red square, to show the prezence of the inline image. |
| 2266 | fCanvas->drawBitmap(bitmap, |
| 2267 | SkDoubleToScalar(0), |
| 2268 | SkDoubleToScalar(0), |
| 2269 | NULL); |
| 2270 | return kNYI_PdfResult; |
| 2271 | } |
| 2272 | |
| 2273 | PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) { |
| 2274 | return fParent->consumeToken(token); |
| 2275 | } |
| 2276 | |
| 2277 | void PdfCompatibilitySectionLooper::loop() { |
| 2278 | // TODO(edisonn): save stacks position, or create a new stack? |
| 2279 | // TODO(edisonn): what happens if we pop out more variables then when we started? |
| 2280 | // restore them? fail? We could create a new operands stack for every new BX/EX section, |
| 2281 | // pop-ing too much will not affect outside the section. |
| 2282 | PdfToken token; |
| 2283 | while (readToken(fTokenizer, &token)) { |
| 2284 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) { |
| 2285 | PdfTokenLooper* looper = new PdfCompatibilitySectionLooper(); |
| 2286 | looper->setUp(this); |
| 2287 | looper->loop(); |
| 2288 | delete looper; |
| 2289 | } else { |
| 2290 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break; |
| 2291 | fParent->consumeToken(token); |
| 2292 | } |
| 2293 | } |
| 2294 | // TODO(edisonn): restore stack. |
| 2295 | } |
| 2296 | |
| 2297 | // TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf |
| 2298 | // TODO(edisonn): Add API for Forms viewing and editing |
| 2299 | // e.g. SkBitmap getPage(int page); |
| 2300 | // int formsCount(); |
| 2301 | // SkForm getForm(int formID); // SkForm(SkRect, .. other data) |
| 2302 | // TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ... |
| 2303 | // if we load the first page, and we zoom to fit to screen horizontally, then load only those |
| 2304 | // resources needed, so the preview is fast. |
| 2305 | // TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve |
| 2306 | // references automatically. |
| 2307 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2308 | PdfContext* gPdfContext = NULL; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 2309 | |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2310 | bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2311 | if (!fPdfDoc) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2312 | return false; |
| 2313 | } |
| 2314 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2315 | if (page < 0 || page >= pages()) { |
| 2316 | return false; |
| 2317 | } |
| 2318 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2319 | PdfContext pdfContext(fPdfDoc); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2320 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2321 | pdfContext.fOriginalMatrix = SkMatrix::I(); |
| 2322 | pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page); |
| 2323 | |
| 2324 | gPdfContext = &pdfContext; |
| 2325 | |
| 2326 | // TODO(edisonn): get matrix stuff right. |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2327 | SkScalar z = SkIntToScalar(0); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2328 | SkScalar w = dst.width(); |
| 2329 | SkScalar h = dst.height(); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2330 | |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2331 | SkScalar wp = fPdfDoc->MediaBox(page).width(); |
| 2332 | SkScalar hp = fPdfDoc->MediaBox(page).height(); |
| 2333 | |
| 2334 | SkPoint pdfSpace[4] = {SkPoint::Make(z, z), SkPoint::Make(wp, z), SkPoint::Make(wp, hp), SkPoint::Make(z, hp)}; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2335 | // SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 2336 | |
| 2337 | // TODO(edisonn): add flag for this app to create sourunding buffer zone |
| 2338 | // TODO(edisonn): add flagg for no clipping. |
| 2339 | // Use larger image to make sure we do not draw anything outside of page |
| 2340 | // could be used in tests. |
| 2341 | |
| 2342 | #ifdef PDF_DEBUG_3X |
| 2343 | 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)}; |
| 2344 | #else |
| 2345 | SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 2346 | #endif |
| 2347 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)}; |
| 2348 | //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)}; |
| 2349 | |
| 2350 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)}; |
| 2351 | //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)}; |
| 2352 | |
| 2353 | //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)}; |
| 2354 | //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)}; |
| 2355 | |
| 2356 | SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4)); |
| 2357 | SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix"); |
| 2358 | |
| 2359 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2360 | pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix; |
| 2361 | pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM; |
| 2362 | pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2363 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2364 | #ifndef PDF_DEBUG_NO_PAGE_CLIPING |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2365 | canvas->clipRect(dst, SkRegion::kIntersect_Op, true); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2366 | #endif |
| 2367 | |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2368 | canvas->setMatrix(pdfContext.fOriginalMatrix); |
| 2369 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 2370 | doPage(&pdfContext, canvas, fPdfDoc->page(page)); |
| 2371 | |
| 2372 | // TODO(edisonn:) erase with white before draw? |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2373 | // SkPaint paint; |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 2374 | // paint.setColor(SK_ColorWHITE); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2375 | // canvas->drawRect(rect, paint); |
| 2376 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2377 | |
| 2378 | canvas->flush(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2379 | return true; |
| 2380 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2381 | |
| 2382 | bool SkPdfRenderer::load(const SkString inputFileName) { |
| 2383 | unload(); |
| 2384 | |
| 2385 | // TODO(edisonn): create static function that could return NULL if there are errors |
| 2386 | fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str()); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 2387 | if (fPdfDoc->pages() == 0) { |
| 2388 | delete fPdfDoc; |
| 2389 | fPdfDoc = NULL; |
| 2390 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2391 | |
| 2392 | return fPdfDoc != NULL; |
| 2393 | } |
| 2394 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 2395 | bool SkPdfRenderer::load(SkStream* stream) { |
| 2396 | unload(); |
| 2397 | |
| 2398 | // TODO(edisonn): create static function that could return NULL if there are errors |
| 2399 | fPdfDoc = new SkNativeParsedPDF(stream); |
| 2400 | if (fPdfDoc->pages() == 0) { |
| 2401 | delete fPdfDoc; |
| 2402 | fPdfDoc = NULL; |
| 2403 | } |
| 2404 | |
| 2405 | return fPdfDoc != NULL; |
| 2406 | } |
| 2407 | |
| 2408 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2409 | int SkPdfRenderer::pages() const { |
| 2410 | return fPdfDoc != NULL ? fPdfDoc->pages() : 0; |
| 2411 | } |
| 2412 | |
| 2413 | void SkPdfRenderer::unload() { |
| 2414 | delete fPdfDoc; |
| 2415 | fPdfDoc = NULL; |
| 2416 | } |
| 2417 | |
| 2418 | SkRect SkPdfRenderer::MediaBox(int page) const { |
| 2419 | SkASSERT(fPdfDoc); |
| 2420 | return fPdfDoc->MediaBox(page); |
| 2421 | } |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 2422 | |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 2423 | size_t SkPdfRenderer::bytesUsed() const { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 2424 | return fPdfDoc ? fPdfDoc->bytesUsed() : 0; |
| 2425 | } |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 2426 | |
| 2427 | bool SkPDFNativeRenderToBitmap(SkStream* stream, |
| 2428 | SkBitmap* output, |
| 2429 | int page, |
| 2430 | SkPdfContent content, |
| 2431 | double dpi) { |
| 2432 | SkASSERT(page >= 0); |
| 2433 | SkPdfRenderer renderer; |
| 2434 | renderer.load(stream); |
| 2435 | if (!renderer.loaded() || page >= renderer.pages() || page < 0) { |
| 2436 | return false; |
| 2437 | } |
| 2438 | |
| 2439 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
| 2440 | |
| 2441 | SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0))); |
| 2442 | SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0))); |
| 2443 | |
| 2444 | rect = SkRect::MakeWH(width, height); |
| 2445 | |
| 2446 | setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height)); |
| 2447 | |
| 2448 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output))); |
| 2449 | SkCanvas canvas(device); |
| 2450 | |
| 2451 | return renderer.renderPage(page, &canvas, rect); |
| 2452 | } |