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 | b0145ce | 2013-08-05 16:23:23 +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) { |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 442 | SkBitmap* bitmap = new SkBitmap(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 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 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 465 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 466 | bitmap->setPixels(uncompressedStreamArgb); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 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 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 481 | bitmap->setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 482 | width, height); |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 483 | bitmap->setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable()); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 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 | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 499 | static SkBitmap* getImageFromObjectCore(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. |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 502 | return NULL; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 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 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 510 | bool indexed = false; |
| 511 | SkPMColor colors[256]; |
| 512 | int cnt = 0; |
| 513 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 514 | // TODO(edisonn): color space can be an array too! |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 515 | if (image->isColorSpaceAName(pdfContext->fPdfDoc)) { |
| 516 | colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc); |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 517 | } else if (image->isColorSpaceAArray(pdfContext->fPdfDoc)) { |
| 518 | SkPdfArray* array = image->getColorSpaceAsArray(pdfContext->fPdfDoc); |
| 519 | if (array && array->size() == 4 && array->objAtAIndex(0)->isName("Indexed") && |
| 520 | (array->objAtAIndex(1)->isName("DeviceRGB") || array->objAtAIndex(1)->isName("RGB")) && |
| 521 | array->objAtAIndex(2)->isInteger() && |
| 522 | array->objAtAIndex(3)->isHexString() |
| 523 | ) { |
| 524 | // TODO(edisonn): suport only DeviceRGB for now. |
| 525 | indexed = true; |
| 526 | cnt = array->objAtAIndex(2)->intValue() + 1; |
| 527 | if (cnt > 256) { |
| 528 | // TODO(edionn): report NYIs |
| 529 | return NULL; |
| 530 | } |
| 531 | SkColorTable colorTable(cnt); |
| 532 | NotOwnedString data = array->objAtAIndex(3)->strRef(); |
| 533 | if (data.fBytes != (unsigned int)cnt * 3) { |
| 534 | // TODO(edionn): report error/warning |
| 535 | return NULL; |
| 536 | } |
| 537 | for (int i = 0 ; i < cnt; i++) { |
| 538 | colors[i] = SkPreMultiplyARGB(0xff, data.fBuffer[3 * i], data.fBuffer[3 * i + 1], data.fBuffer[3 * i + 2]); |
| 539 | } |
| 540 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| 544 | bool imageMask = image->imageMask(); |
| 545 | |
| 546 | if (imageMask) { |
| 547 | if (bpc != 0 && bpc != 1) { |
| 548 | // TODO(edisonn): report warning to be used in testing. |
| 549 | return SkBitmap(); |
| 550 | } |
| 551 | bpc = 1; |
| 552 | } |
| 553 | */ |
| 554 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 555 | const unsigned char* uncompressedStream = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 556 | size_t uncompressedStreamLength = 0; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 557 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 558 | SkPdfStream* stream = (SkPdfStream*)image; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 559 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 560 | if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength) || |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 561 | uncompressedStream == NULL || uncompressedStreamLength == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 562 | // TODO(edisonn): report warning to be used in testing. |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 563 | return NULL; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 564 | } |
| 565 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 566 | SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream; |
| 567 | |
| 568 | if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) && |
| 569 | streamDict->getFilterAsName(NULL) == "DCTDecode") || |
| 570 | (streamDict->isFilterAArray(NULL) && |
| 571 | streamDict->getFilterAsArray(NULL)->size() > 0 && |
| 572 | streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() && |
| 573 | streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) { |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 574 | SkBitmap* bitmap = new SkBitmap(); |
| 575 | SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, bitmap); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 576 | return bitmap; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ... |
| 582 | // PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 583 | // obj.GetDictionary().GetKey(PdfName("Filter"))); |
| 584 | // if (value && value->IsArray() && value->GetArray().GetSize() == 1) { |
| 585 | // value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 586 | // &value->GetArray()[0]); |
| 587 | // } |
| 588 | // if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") { |
| 589 | // SkStream stream = SkStream:: |
| 590 | // SkImageDecoder::Factory() |
| 591 | // } |
| 592 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 593 | // TODO(edisonn): assumes RGB for now, since it is the only onwe implemented |
| 594 | if (indexed) { |
| 595 | SkBitmap* bitmap = new SkBitmap(); |
| 596 | bitmap->setConfig(SkBitmap::kIndex8_Config, width, height); |
| 597 | SkColorTable* colorTable = new SkColorTable(colors, cnt); |
| 598 | bitmap->setPixels((void*)uncompressedStream, colorTable); |
| 599 | return bitmap; |
| 600 | } |
| 601 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 602 | int bytesPerLine = (int)(uncompressedStreamLength / height); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 603 | #ifdef PDF_TRACE |
| 604 | if (uncompressedStreamLength % height != 0) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 605 | printf("Warning uncompressedStreamLength modulo height != 0 !!!\n"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 606 | } |
| 607 | #endif |
| 608 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 609 | SkBitmap* bitmap = transferImageStreamToBitmap( |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 610 | (unsigned char*)uncompressedStream, uncompressedStreamLength, |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 611 | (int)width, (int)height, bytesPerLine, |
| 612 | (int)bpc, colorSpace, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 613 | transparencyMask); |
| 614 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 615 | return bitmap; |
| 616 | } |
| 617 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 618 | static SkBitmap* getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) { |
| 619 | if (!transparencyMask) { |
| 620 | if (!image->hasData(SkPdfObject::kBitmap_Data)) { |
| 621 | SkBitmap* bitmap = getImageFromObjectCore(pdfContext, image, transparencyMask); |
| 622 | image->setData(bitmap, SkPdfObject::kBitmap_Data); |
| 623 | } |
| 624 | return (SkBitmap*) image->data(SkPdfObject::kBitmap_Data); |
| 625 | } else { |
| 626 | return getImageFromObjectCore(pdfContext, image, transparencyMask); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | static SkBitmap* getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 631 | SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 632 | |
| 633 | if (sMask) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 634 | return getImageFromObject(pdfContext, sMask, true); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // TODO(edisonn): implement GS SMask. Default to empty right now. |
| 638 | return pdfContext->fGraphicsState.fSMask; |
| 639 | } |
| 640 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 641 | static PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 642 | if (skpdfimage == NULL) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 643 | return kIgnoreError_PdfResult; |
| 644 | } |
| 645 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 646 | SkBitmap* image = getImageFromObject(pdfContext, skpdfimage, false); |
| 647 | SkBitmap* sMask = getSmaskFromObject(pdfContext, skpdfimage); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 648 | |
| 649 | canvas->save(); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 650 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 651 | |
| 652 | #if 1 |
| 653 | SkScalar z = SkIntToScalar(0); |
| 654 | SkScalar one = SkIntToScalar(1); |
| 655 | |
| 656 | SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)}; |
| 657 | SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)}; |
| 658 | SkMatrix flip; |
| 659 | SkAssertResult(flip.setPolyToPoly(from, to, 4)); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 660 | SkMatrix solveImageFlip = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 661 | solveImageFlip.preConcat(flip); |
| 662 | canvas->setMatrix(solveImageFlip); |
| 663 | #endif |
| 664 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 665 | SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0)); |
| 666 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 667 | // TODO(edisonn): soft mask type? alpha/luminosity. |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 668 | if (!sMask || sMask->empty()) { |
| 669 | canvas->drawBitmapRect(*image, dst, NULL); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 670 | } else { |
| 671 | canvas->saveLayer(&dst, NULL); |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 672 | canvas->drawBitmapRect(*image, dst, NULL); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 673 | SkPaint xfer; |
| 674 | pdfContext->fGraphicsState.applyGraphicsState(&xfer, false); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 675 | // 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] | 676 | xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 677 | canvas->drawBitmapRect(*sMask, dst, &xfer); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 678 | canvas->restore(); |
| 679 | } |
| 680 | |
| 681 | canvas->restore(); |
| 682 | |
| 683 | return kPartial_PdfResult; |
| 684 | } |
| 685 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 686 | //TODO(edisonn): options for implementing isolation and knockout |
| 687 | // 1) emulate them (current solution) |
| 688 | // PRO: simple |
| 689 | // CON: will need to use readPixels, which means serious perf issues |
| 690 | // 2) Compile a plan for an array of matrixes, compose the result at the end |
| 691 | // PRO: might be faster then 1, no need to readPixels |
| 692 | // CON: multiple drawings (but on smaller areas), pay a price at loading pdf to compute a pdf draw plan |
| 693 | // on average, a load with empty draw is 100ms on all the skps we have, for complete sites |
| 694 | // 3) support them natively in SkCanvas |
| 695 | // PRO: simple |
| 696 | // CON: we would still need to use a form of readPixels anyway, so perf might be the same as 1) |
| 697 | // 4) compile a plan using pathops, and render once without any fancy rules with backdrop |
| 698 | // PRO: simple, fast |
| 699 | // CON: pathops must be bug free first + time to compute new paths |
| 700 | // pay a price at loading pdf to compute a pdf draw plan |
| 701 | // 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] | 702 | // 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] | 703 | |
| 704 | |
| 705 | // TODO(edisonn): draw plan from point! - list of draw ops of a point, like a tree! |
| 706 | // TODO(edisonn): Minimal PDF to draw some points - remove everything that it is not needed, save pdf uncompressed |
| 707 | |
| 708 | |
| 709 | |
| 710 | static void doGroup_before(PdfContext* pdfContext, SkCanvas* canvas, SkRect bbox, SkPdfTransparencyGroupDictionary* tgroup, bool page) { |
| 711 | SkRect bboxOrig = bbox; |
| 712 | SkBitmap backdrop; |
| 713 | bool isolatedGroup = tgroup->I(pdfContext->fPdfDoc); |
| 714 | // bool knockoutGroup = tgroup->K(pdfContext->fPdfDoc); |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 715 | SkPaint paint; |
| 716 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 717 | canvas->saveLayer(&bboxOrig, isolatedGroup ? &paint : NULL); |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 718 | } |
| 719 | |
edisonn@google.com | 251176e | 2013-08-01 13:24:00 +0000 | [diff] [blame] | 720 | // TODO(edisonn): non isolation implemented in skia |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 721 | //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] | 722 | // if not isolated |
| 723 | // canvas->drawBitmapRect(backdrop, bboxOrig, NULL); |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 724 | //} |
| 725 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 726 | static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 727 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 728 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 729 | } |
| 730 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 731 | if (!skobj->has_BBox()) { |
| 732 | return kIgnoreError_PdfResult; |
| 733 | } |
| 734 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 735 | PdfOp_q(pdfContext, canvas, NULL); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 736 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 737 | canvas->save(); |
| 738 | |
| 739 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 740 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 741 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 742 | } |
| 743 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 744 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix"); |
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 | if (skobj->has_Matrix()) { |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 747 | pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc)); |
| 748 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM; |
| 749 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 750 | // TODO(edisonn) reset matrixTm and matricTlm also? |
| 751 | } |
| 752 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 753 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 754 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 755 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 756 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 757 | SkRect bbox = skobj->BBox(pdfContext->fPdfDoc); |
| 758 | 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] | 759 | |
| 760 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 761 | // For this PdfContentsTokenizer needs to be extended. |
| 762 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 763 | // This is a group? |
| 764 | if (skobj->has_Group()) { |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 765 | SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc); |
| 766 | doGroup_before(pdfContext, canvas, bbox, tgroup, false); |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 767 | } |
| 768 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 769 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 770 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 771 | SkPdfNativeTokenizer* tokenizer = |
| 772 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 773 | if (tokenizer != NULL) { |
| 774 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 775 | looper.loop(); |
| 776 | delete tokenizer; |
| 777 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 778 | |
| 779 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 780 | // 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] | 781 | |
| 782 | if (skobj->has_Group()) { |
| 783 | canvas->restore(); |
| 784 | } |
| 785 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 786 | canvas->restore(); |
| 787 | PdfOp_Q(pdfContext, canvas, NULL); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 788 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 789 | } |
| 790 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 791 | |
| 792 | // TODO(edisonn): Extract a class like ObjWithStream |
| 793 | static PdfResult doXObject_Pattern(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1PatternDictionary* skobj) { |
| 794 | if (!skobj || !skobj->hasStream()) { |
| 795 | return kIgnoreError_PdfResult; |
| 796 | } |
| 797 | |
| 798 | if (!skobj->has_BBox()) { |
| 799 | return kIgnoreError_PdfResult; |
| 800 | } |
| 801 | |
| 802 | PdfOp_q(pdfContext, canvas, NULL); |
| 803 | |
| 804 | canvas->save(); |
| 805 | |
| 806 | |
| 807 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 808 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
| 809 | } |
| 810 | |
| 811 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Current matrix"); |
| 812 | |
| 813 | if (skobj->has_Matrix()) { |
| 814 | pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc)); |
| 815 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM; |
| 816 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
| 817 | // TODO(edisonn) reset matrixTm and matricTlm also? |
| 818 | } |
| 819 | |
| 820 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix"); |
| 821 | |
| 822 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
| 823 | |
| 824 | SkRect bbox = skobj->BBox(pdfContext->fPdfDoc); |
| 825 | canvas->clipRect(bbox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
| 826 | |
| 827 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 828 | // For this PdfContentsTokenizer needs to be extended. |
| 829 | |
| 830 | SkPdfStream* stream = (SkPdfStream*)skobj; |
| 831 | |
| 832 | SkPdfNativeTokenizer* tokenizer = |
| 833 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
| 834 | if (tokenizer != NULL) { |
| 835 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 836 | looper.loop(); |
| 837 | delete tokenizer; |
| 838 | } |
| 839 | |
| 840 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 841 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 842 | |
| 843 | canvas->restore(); |
| 844 | PdfOp_Q(pdfContext, canvas, NULL); |
| 845 | return kPartial_PdfResult; |
| 846 | } |
| 847 | |
| 848 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 849 | //static PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
| 850 | // return kNYI_PdfResult; |
| 851 | //} |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 852 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 853 | PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) { |
| 854 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 855 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | PdfOp_q(pdfContext, canvas, NULL); |
| 859 | canvas->save(); |
| 860 | |
| 861 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 862 | pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize)); |
| 863 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 864 | pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm; |
| 865 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 866 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 867 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 868 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 869 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 870 | |
| 871 | SkRect rm = bBox; |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 872 | pdfContext->fGraphicsState.fCTM.mapRect(&rm); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 873 | |
| 874 | SkTraceRect(rm, "bbox mapped"); |
| 875 | |
| 876 | canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
| 877 | |
| 878 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 879 | // For this PdfContentsTokenizer needs to be extended. |
| 880 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 881 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 882 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 883 | SkPdfNativeTokenizer* tokenizer = |
| 884 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 885 | if (tokenizer != NULL) { |
| 886 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 887 | looper.loop(); |
| 888 | delete tokenizer; |
| 889 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 890 | |
| 891 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 892 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 893 | canvas->restore(); |
| 894 | PdfOp_Q(pdfContext, canvas, NULL); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 895 | |
| 896 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 900 | // TODO(edisonn): make sure the pointer is unique |
| 901 | std::set<const SkPdfObject*> gInRendering; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 902 | |
| 903 | class CheckRecursiveRendering { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 904 | const SkPdfObject* fUniqueData; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 905 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 906 | CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 907 | gInRendering.insert(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | ~CheckRecursiveRendering() { |
| 911 | //SkASSERT(fObj.fInRendering); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 912 | gInRendering.erase(fUniqueData); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 913 | } |
| 914 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 915 | static bool IsInRendering(const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 916 | return gInRendering.find(obj) != gInRendering.end(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 917 | } |
| 918 | }; |
| 919 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 920 | static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 921 | if (CheckRecursiveRendering::IsInRendering(obj)) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 922 | // Oops, corrupt PDF! |
| 923 | return kIgnoreError_PdfResult; |
| 924 | } |
| 925 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 926 | CheckRecursiveRendering checkRecursion(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 927 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 928 | switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj)) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 929 | { |
| 930 | case kImageDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 931 | return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 932 | case kType1FormDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 933 | return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 934 | //case kObjectDictionaryXObjectPS_SkPdfObjectType: |
| 935 | //return doXObject_PS(skxobj.asPS()); |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 936 | default: { |
| 937 | if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(obj) != kNone_SkPdfObjectType) { |
| 938 | SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)obj; |
| 939 | return doXObject_Pattern(pdfContext, canvas, pattern); |
| 940 | } |
| 941 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 942 | } |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 943 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 944 | } |
| 945 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 946 | static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) { |
| 947 | if (!skobj) { |
| 948 | return kIgnoreError_PdfResult; |
| 949 | } |
| 950 | |
| 951 | if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) { |
| 952 | return kNYI_PdfResult; |
| 953 | } |
| 954 | |
| 955 | SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc); |
| 956 | |
| 957 | if (!stream) { |
| 958 | return kIgnoreError_PdfResult; |
| 959 | } |
| 960 | |
| 961 | if (CheckRecursiveRendering::IsInRendering(skobj)) { |
| 962 | // Oops, corrupt PDF! |
| 963 | return kIgnoreError_PdfResult; |
| 964 | } |
| 965 | CheckRecursiveRendering checkRecursion(skobj); |
| 966 | |
| 967 | |
| 968 | PdfOp_q(pdfContext, canvas, NULL); |
| 969 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 970 | |
| 971 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 972 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
| 973 | } |
| 974 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 975 | // TODO(edisonn): MediaBox can be inherited!!!! |
| 976 | SkRect bbox = skobj->MediaBox(pdfContext->fPdfDoc); |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 977 | if (skobj->has_Group()) { |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 978 | SkPdfTransparencyGroupDictionary* tgroup = skobj->Group(pdfContext->fPdfDoc); |
| 979 | doGroup_before(pdfContext, canvas, bbox, tgroup, true); |
| 980 | } else { |
| 981 | canvas->save(); |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 982 | } |
| 983 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 984 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 985 | SkPdfNativeTokenizer* tokenizer = |
| 986 | pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator); |
| 987 | if (tokenizer != NULL) { |
| 988 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 989 | looper.loop(); |
| 990 | delete tokenizer; |
| 991 | } |
| 992 | |
| 993 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 994 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 995 | canvas->restore(); |
| 996 | PdfOp_Q(pdfContext, canvas, NULL); |
| 997 | return kPartial_PdfResult; |
| 998 | } |
| 999 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1000 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1001 | pdfContext->fStateStack.push(pdfContext->fGraphicsState); |
| 1002 | canvas->save(); |
| 1003 | return kOK_PdfResult; |
| 1004 | } |
| 1005 | |
| 1006 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1007 | pdfContext->fGraphicsState = pdfContext->fStateStack.top(); |
| 1008 | pdfContext->fStateStack.pop(); |
| 1009 | canvas->restore(); |
| 1010 | return kOK_PdfResult; |
| 1011 | } |
| 1012 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1013 | static PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1014 | double array[6]; |
| 1015 | for (int i = 0 ; i < 6 ; i++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1016 | array[5 - i] = pdfContext->fObjectStack.top()->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1017 | pdfContext->fObjectStack.pop(); |
| 1018 | } |
| 1019 | |
| 1020 | // a b |
| 1021 | // c d |
| 1022 | // e f |
| 1023 | |
| 1024 | // 0 1 |
| 1025 | // 2 3 |
| 1026 | // 4 5 |
| 1027 | |
| 1028 | // sx ky |
| 1029 | // kx sy |
| 1030 | // tx ty |
| 1031 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 1032 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1033 | pdfContext->fGraphicsState.fCTM.preConcat(matrix); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1034 | |
| 1035 | #ifdef PDF_TRACE |
| 1036 | printf("cm "); |
| 1037 | for (int i = 0 ; i < 6 ; i++) { |
| 1038 | printf("%f ", array[i]); |
| 1039 | } |
| 1040 | printf("\n"); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1041 | SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "cm"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1042 | #endif |
| 1043 | |
| 1044 | return kOK_PdfResult; |
| 1045 | } |
| 1046 | |
| 1047 | //leading TL Set the text leading, Tl |
| 1048 | //, to leading, which is a number expressed in unscaled text |
| 1049 | //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] | 1050 | static PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1051 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1052 | |
| 1053 | pdfContext->fGraphicsState.fTextLeading = ty; |
| 1054 | |
| 1055 | return kOK_PdfResult; |
| 1056 | } |
| 1057 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1058 | static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 1059 | #ifdef PDF_TRACE |
| 1060 | printf("stack size = %i\n", (int)pdfContext->fObjectStack.size()); |
| 1061 | #endif |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1062 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 1063 | SkPdfObject* obj = pdfContext->fObjectStack.top(); |
| 1064 | obj = obj; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1065 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1066 | |
| 1067 | double array[6] = {1, 0, 0, 1, tx, ty}; |
| 1068 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 1069 | |
| 1070 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 1071 | pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix); |
| 1072 | |
| 1073 | return kPartial_PdfResult; |
| 1074 | } |
| 1075 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1076 | static PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1077 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1078 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1079 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1080 | // TODO(edisonn): Create factory methods or constructors so native is hidden |
| 1081 | SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1082 | pdfContext->fObjectStack.push(_ty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1083 | |
| 1084 | PdfOp_TL(pdfContext, canvas, looper); |
| 1085 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1086 | SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1087 | pdfContext->fObjectStack.push(vtx); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1088 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1089 | SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1090 | pdfContext->fObjectStack.push(vty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1091 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1092 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 1093 | |
| 1094 | // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster |
| 1095 | // 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] | 1096 | |
| 1097 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1100 | static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1101 | double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1102 | double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1103 | double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1104 | double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1105 | double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1106 | double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1107 | |
| 1108 | double array[6]; |
| 1109 | array[0] = a; |
| 1110 | array[1] = b; |
| 1111 | array[2] = c; |
| 1112 | array[3] = d; |
| 1113 | array[4] = e; |
| 1114 | array[5] = f; |
| 1115 | |
| 1116 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1117 | matrix.postConcat(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1118 | |
| 1119 | // TODO(edisonn): Text positioning. |
| 1120 | pdfContext->fGraphicsState.fMatrixTm = matrix; |
| 1121 | pdfContext->fGraphicsState.fMatrixTlm = matrix;; |
| 1122 | |
| 1123 | return kPartial_PdfResult; |
| 1124 | } |
| 1125 | |
| 1126 | //— T* Move to the start of the next line. This operator has the same effect as the code |
| 1127 | //0 Tl Td |
| 1128 | //where Tl is the current leading parameter in the text state |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1129 | static PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1130 | SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0); |
| 1131 | SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1132 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1133 | pdfContext->fObjectStack.push(zero); |
| 1134 | pdfContext->fObjectStack.push(tl); |
| 1135 | |
| 1136 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 1137 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1138 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1141 | static PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1142 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1143 | pdfContext->fGraphicsState.fPath.reset(); |
| 1144 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1145 | } |
| 1146 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1147 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1148 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1149 | |
| 1150 | pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 1151 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 1152 | |
| 1153 | return kOK_PdfResult; |
| 1154 | } |
| 1155 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1156 | static PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1157 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1158 | pdfContext->fGraphicsState.fPath.reset(); |
| 1159 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1160 | } |
| 1161 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1162 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1163 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1164 | |
| 1165 | pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 1166 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 1167 | |
| 1168 | return kOK_PdfResult; |
| 1169 | } |
| 1170 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1171 | static PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1172 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1173 | pdfContext->fGraphicsState.fPath.reset(); |
| 1174 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1175 | } |
| 1176 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1177 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1178 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1179 | double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1180 | double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1181 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1182 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1183 | |
| 1184 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1185 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1186 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1187 | |
| 1188 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1189 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1190 | |
| 1191 | return kOK_PdfResult; |
| 1192 | } |
| 1193 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1194 | static PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1195 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1196 | pdfContext->fGraphicsState.fPath.reset(); |
| 1197 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1198 | } |
| 1199 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1200 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1201 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1202 | double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1203 | double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1204 | double y1 = pdfContext->fGraphicsState.fCurPosY; |
| 1205 | double x1 = pdfContext->fGraphicsState.fCurPosX; |
| 1206 | |
| 1207 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1208 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1209 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1210 | |
| 1211 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1212 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1213 | |
| 1214 | return kOK_PdfResult; |
| 1215 | } |
| 1216 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1217 | static PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1218 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1219 | pdfContext->fGraphicsState.fPath.reset(); |
| 1220 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1221 | } |
| 1222 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1223 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1224 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1225 | double y2 = pdfContext->fGraphicsState.fCurPosY; |
| 1226 | double x2 = pdfContext->fGraphicsState.fCurPosX; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1227 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1228 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1229 | |
| 1230 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 1231 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 1232 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 1233 | |
| 1234 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 1235 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 1236 | |
| 1237 | return kOK_PdfResult; |
| 1238 | } |
| 1239 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1240 | static PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1241 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 1242 | pdfContext->fGraphicsState.fPath.reset(); |
| 1243 | pdfContext->fGraphicsState.fPathClosed = false; |
| 1244 | } |
| 1245 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1246 | double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1247 | double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1248 | double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1249 | double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1250 | |
| 1251 | pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y), |
| 1252 | SkDoubleToScalar(x + width), SkDoubleToScalar(y + height)); |
| 1253 | |
| 1254 | pdfContext->fGraphicsState.fCurPosX = x; |
| 1255 | pdfContext->fGraphicsState.fCurPosY = y + height; |
| 1256 | |
| 1257 | return kOK_PdfResult; |
| 1258 | } |
| 1259 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1260 | static PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1261 | pdfContext->fGraphicsState.fPath.close(); |
| 1262 | return kOK_PdfResult; |
| 1263 | } |
| 1264 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1265 | 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] | 1266 | SkPath path = pdfContext->fGraphicsState.fPath; |
| 1267 | |
| 1268 | if (close) { |
| 1269 | path.close(); |
| 1270 | } |
| 1271 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1272 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1273 | |
| 1274 | SkPaint paint; |
| 1275 | |
| 1276 | SkPoint line[2]; |
| 1277 | if (fill && !stroke && path.isLine(line)) { |
| 1278 | paint.setStyle(SkPaint::kStroke_Style); |
| 1279 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1280 | // TODO(edisonn): implement this with patterns |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1281 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 1282 | paint.setStrokeWidth(SkDoubleToScalar(0)); |
| 1283 | |
| 1284 | canvas->drawPath(path, paint); |
| 1285 | } else { |
| 1286 | if (fill) { |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1287 | if (strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0 && |
| 1288 | pdfContext->fGraphicsState.fNonStroking.fPattern != NULL) { |
| 1289 | |
| 1290 | // TODO(edisonn): we can use a shader here, like imageshader to draw fast. ultimately, |
| 1291 | // if this is not possible, and we are in rasper mode, and the cells don't intersect, we could even have multiple cpus. |
| 1292 | |
| 1293 | canvas->save(); |
| 1294 | PdfOp_q(pdfContext, canvas, NULL); |
| 1295 | |
| 1296 | if (evenOdd) { |
| 1297 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1298 | } |
| 1299 | canvas->clipPath(path); |
| 1300 | |
| 1301 | if (pdfContext->fPdfDoc->mapper()->mapType1PatternDictionary(pdfContext->fGraphicsState.fNonStroking.fPattern) != kNone_SkPdfObjectType) { |
| 1302 | SkPdfType1PatternDictionary* pattern = (SkPdfType1PatternDictionary*)pdfContext->fGraphicsState.fNonStroking.fPattern; |
| 1303 | |
| 1304 | // TODO(edisonn): constants |
| 1305 | // TODO(edisonn): colored |
| 1306 | if (pattern->PaintType(pdfContext->fPdfDoc) == 1) { |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 1307 | // TODO(edisonn): don't use abs, iterate as asked, if the cells intersect |
| 1308 | // it will change the result iterating in reverse |
| 1309 | int xStep = abs((int)pattern->XStep(pdfContext->fPdfDoc)); |
| 1310 | int yStep = abs((int)pattern->YStep(pdfContext->fPdfDoc)); |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1311 | |
| 1312 | SkRect bounds = path.getBounds(); |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1313 | |
| 1314 | // TODO(edisonn): xstep and ystep can be negative, and we need to iterate in reverse |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 1315 | // TODO(edisonn): don't do that! |
| 1316 | bounds.sort(); |
| 1317 | |
| 1318 | SkScalar x; |
| 1319 | SkScalar y; |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1320 | |
| 1321 | y = bounds.top(); |
| 1322 | int totalx = 0; |
| 1323 | int totaly = 0; |
| 1324 | while (y < bounds.bottom()) { |
| 1325 | x = bounds.left(); |
| 1326 | totalx = 0; |
| 1327 | |
| 1328 | while (x < bounds.right()) { |
| 1329 | doXObject(pdfContext, canvas, pattern); |
| 1330 | |
| 1331 | pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(xStep), SkIntToScalar(0)); |
| 1332 | totalx += xStep; |
| 1333 | x += SkIntToScalar(xStep); |
| 1334 | } |
| 1335 | pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(-totalx), SkIntToScalar(0)); |
| 1336 | |
| 1337 | pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(0), SkIntToScalar(-yStep)); |
| 1338 | totaly += yStep; |
| 1339 | y += SkIntToScalar(yStep); |
| 1340 | } |
| 1341 | pdfContext->fGraphicsState.fCTM.preTranslate(SkIntToScalar(0), SkIntToScalar(totaly)); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | // apply matrix |
| 1346 | // get xstep, y step, bbox ... for cliping, and bos of the path |
| 1347 | |
| 1348 | PdfOp_Q(pdfContext, canvas, NULL); |
| 1349 | canvas->restore(); |
| 1350 | } else { |
| 1351 | paint.setStyle(SkPaint::kFill_Style); |
| 1352 | if (evenOdd) { |
| 1353 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1354 | } |
| 1355 | |
| 1356 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 1357 | |
| 1358 | canvas->drawPath(path, paint); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1359 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | if (stroke) { |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1363 | if (false && strncmp((char*)pdfContext->fGraphicsState.fNonStroking.fColorSpace.fBuffer, "Pattern", strlen("Pattern")) == 0) { |
| 1364 | // TODO(edisonn): implement Pattern for strokes |
| 1365 | paint.setStyle(SkPaint::kStroke_Style); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1366 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1367 | paint.setColor(SK_ColorGREEN); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1368 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1369 | path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke |
| 1370 | canvas->drawPath(path, paint); |
| 1371 | } else { |
| 1372 | paint.setStyle(SkPaint::kStroke_Style); |
| 1373 | |
| 1374 | pdfContext->fGraphicsState.applyGraphicsState(&paint, true); |
| 1375 | |
| 1376 | path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke |
| 1377 | canvas->drawPath(path, paint); |
| 1378 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | pdfContext->fGraphicsState.fPath.reset(); |
| 1383 | // todo zoom ... other stuff ? |
| 1384 | |
| 1385 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1386 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1387 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1388 | #endif |
| 1389 | } |
| 1390 | |
| 1391 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1392 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1393 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1394 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1395 | |
| 1396 | } |
| 1397 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1398 | static PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1399 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false); |
| 1400 | } |
| 1401 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1402 | static PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1403 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false); |
| 1404 | } |
| 1405 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1406 | static PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1407 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1408 | } |
| 1409 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1410 | static PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1411 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1412 | } |
| 1413 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1414 | static PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1415 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true); |
| 1416 | } |
| 1417 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1418 | static PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1419 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false); |
| 1420 | } |
| 1421 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1422 | static PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1423 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true); |
| 1424 | } |
| 1425 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1426 | static PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1427 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false); |
| 1428 | } |
| 1429 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1430 | static PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1431 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true); |
| 1432 | } |
| 1433 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1434 | static PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1435 | canvas->setMatrix(pdfContext->fGraphicsState.fCTM); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1436 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1437 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1438 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1439 | #endif |
| 1440 | } |
| 1441 | |
| 1442 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1443 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1444 | |
| 1445 | pdfContext->fGraphicsState.fPathClosed = true; |
| 1446 | |
| 1447 | return kOK_PdfResult; |
| 1448 | } |
| 1449 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1450 | static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1451 | pdfContext->fGraphicsState.fTextBlock = true; |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1452 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM; |
| 1453 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1454 | |
| 1455 | return kPartial_PdfResult; |
| 1456 | } |
| 1457 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1458 | static PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1459 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1460 | return kIgnoreError_PdfResult; |
| 1461 | } |
| 1462 | // 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] | 1463 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1466 | PdfResult skpdfGraphicsStateApplyFontCore(PdfContext* pdfContext, const SkPdfObject* fontName, double fontSize) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1467 | #ifdef PDF_TRACE |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1468 | printf("font name: %s\n", fontName->nameValue2().c_str()); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1469 | #endif |
| 1470 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1471 | if (!pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) { |
| 1472 | // TODO(edisonn): try to recover and draw it any way? |
| 1473 | return kIgnoreError_PdfResult; |
| 1474 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1475 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1476 | SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName); |
| 1477 | objFont = pdfContext->fPdfDoc->resolveReference(objFont); |
| 1478 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) { |
| 1479 | // TODO(edisonn): try to recover and draw it any way? |
| 1480 | return kIgnoreError_PdfResult; |
| 1481 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1482 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1483 | SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont; |
| 1484 | |
| 1485 | SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd); |
| 1486 | |
| 1487 | if (skfont) { |
| 1488 | pdfContext->fGraphicsState.fSkFont = skfont; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1489 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1490 | pdfContext->fGraphicsState.fCurFontSize = fontSize; |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1491 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1494 | //font size Tf Set the text font, Tf |
| 1495 | //, to font and the text font size, Tfs, to size. font is the name of a |
| 1496 | //font resource in the Fontsubdictionary of the current resource dictionary; size is |
| 1497 | //a number representing a scale factor. There is no initial value for either font or |
| 1498 | //size; they must be specified explicitly using Tf before any text is shown. |
| 1499 | static PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1500 | double fontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1501 | SkPdfObject* fontName = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1502 | return skpdfGraphicsStateApplyFontCore(pdfContext, fontName, fontSize); |
| 1503 | } |
| 1504 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1505 | static PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1506 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1507 | // TODO(edisonn): try to recover and draw it any way? |
| 1508 | return kIgnoreError_PdfResult; |
| 1509 | } |
| 1510 | |
| 1511 | PdfResult ret = DrawText(pdfContext, |
| 1512 | pdfContext->fObjectStack.top(), |
| 1513 | canvas); |
| 1514 | pdfContext->fObjectStack.pop(); |
| 1515 | |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1519 | static PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1520 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1521 | // TODO(edisonn): try to recover and draw it any way? |
| 1522 | return kIgnoreError_PdfResult; |
| 1523 | } |
| 1524 | |
| 1525 | PdfOp_T_star(pdfContext, canvas, looper); |
| 1526 | // Do not pop, and push, just transfer the param to Tj |
| 1527 | return PdfOp_Tj(pdfContext, canvas, looper); |
| 1528 | } |
| 1529 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1530 | static PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1531 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1532 | // TODO(edisonn): try to recover and draw it any way? |
| 1533 | return kIgnoreError_PdfResult; |
| 1534 | } |
| 1535 | |
| 1536 | SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1537 | SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1538 | SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1539 | |
| 1540 | pdfContext->fObjectStack.push(aw); |
| 1541 | PdfOp_Tw(pdfContext, canvas, looper); |
| 1542 | |
| 1543 | pdfContext->fObjectStack.push(ac); |
| 1544 | PdfOp_Tc(pdfContext, canvas, looper); |
| 1545 | |
| 1546 | pdfContext->fObjectStack.push(str); |
| 1547 | PdfOp_quote(pdfContext, canvas, looper); |
| 1548 | |
| 1549 | return kPartial_PdfResult; |
| 1550 | } |
| 1551 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1552 | static PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1553 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1554 | // TODO(edisonn): try to recover and draw it any way? |
| 1555 | return kIgnoreError_PdfResult; |
| 1556 | } |
| 1557 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1558 | SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1559 | pdfContext->fObjectStack.pop(); |
| 1560 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1561 | if (!array->isArray()) { |
| 1562 | return kIgnoreError_PdfResult; |
| 1563 | } |
| 1564 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1565 | for( int i=0; i<static_cast<int>(array->size()); i++ ) |
| 1566 | { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1567 | if( (*array)[i]->isAnyString()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1568 | SkPdfObject* obj = (*array)[i]; |
| 1569 | DrawText(pdfContext, |
| 1570 | obj, |
| 1571 | canvas); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1572 | } else if ((*array)[i]->isNumber()) { |
| 1573 | double dx = (*array)[i]->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1574 | SkMatrix matrix; |
| 1575 | matrix.setAll(SkDoubleToScalar(1), |
| 1576 | SkDoubleToScalar(0), |
| 1577 | // TODO(edisonn): use writing mode, vertical/horizontal. |
| 1578 | SkDoubleToScalar(-dx), // amount is substracted!!! |
| 1579 | SkDoubleToScalar(0), |
| 1580 | SkDoubleToScalar(1), |
| 1581 | SkDoubleToScalar(0), |
| 1582 | SkDoubleToScalar(0), |
| 1583 | SkDoubleToScalar(0), |
| 1584 | SkDoubleToScalar(1)); |
| 1585 | |
| 1586 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 1587 | } |
| 1588 | } |
| 1589 | return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK. |
| 1590 | } |
| 1591 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1592 | static PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1593 | colorOperator->fColorSpace = pdfContext->fObjectStack.top()->strRef(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1594 | return kOK_PdfResult; |
| 1595 | } |
| 1596 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1597 | static PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1598 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1599 | } |
| 1600 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1601 | static PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1602 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1603 | } |
| 1604 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1605 | static PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1606 | double c[4]; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1607 | // int64_t v[4]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1608 | |
| 1609 | int n = GetColorSpaceComponents(colorOperator->fColorSpace); |
| 1610 | |
| 1611 | bool doubles = true; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1612 | if (colorOperator->fColorSpace.equals("Indexed")) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1613 | doubles = false; |
| 1614 | } |
| 1615 | |
| 1616 | #ifdef PDF_TRACE |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1617 | printf("color space = %s, N = %i\n", colorOperator->fColorSpace.fBuffer, n); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1618 | #endif |
| 1619 | |
| 1620 | for (int i = n - 1; i >= 0 ; i--) { |
| 1621 | if (doubles) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1622 | c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1623 | // } else { |
| 1624 | // v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // TODO(edisonn): Now, set that color. Only DeviceRGB supported. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1629 | // TODO(edisonn): do possible field values to enum at parsing time! |
| 1630 | // TODO(edisonn): support also abreviations /DeviceRGB == /RGB |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1631 | if (colorOperator->fColorSpace.equals("DeviceRGB") || colorOperator->fColorSpace.equals("RGB")) { |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1632 | 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] | 1633 | } |
| 1634 | return kPartial_PdfResult; |
| 1635 | } |
| 1636 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1637 | static PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1638 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1639 | } |
| 1640 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1641 | static PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1642 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1643 | } |
| 1644 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1645 | static PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1646 | if (pdfContext->fObjectStack.top()->isName()) { |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 1647 | SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1648 | |
| 1649 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1650 | SkPdfDictionary* patternResources = pdfContext->fGraphicsState.fResources->Pattern(pdfContext->fPdfDoc); |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 1651 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1652 | if (patternResources == NULL) { |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 1653 | #ifdef PDF_TRACE |
| 1654 | printf("ExtGState is NULL!\n"); |
| 1655 | #endif |
| 1656 | return kIgnoreError_PdfResult; |
| 1657 | } |
| 1658 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1659 | colorOperator->setPatternColorSpace(pdfContext->fPdfDoc->resolveReference(patternResources->get(name))); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec. |
| 1663 | PdfOp_SC_sc(pdfContext, canvas, colorOperator); |
| 1664 | |
| 1665 | return kPartial_PdfResult; |
| 1666 | } |
| 1667 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1668 | static PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1669 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1670 | } |
| 1671 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1672 | static PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1673 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1674 | } |
| 1675 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1676 | static PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1677 | /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1678 | return kNYI_PdfResult; |
| 1679 | } |
| 1680 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1681 | static PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1682 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1683 | } |
| 1684 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1685 | static PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1686 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1687 | } |
| 1688 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1689 | static PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1690 | double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1691 | double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1692 | double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1693 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1694 | colorOperator->fColorSpace = strings_DeviceRGB; |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1695 | 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] | 1696 | return kOK_PdfResult; |
| 1697 | } |
| 1698 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1699 | static PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1700 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1701 | } |
| 1702 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1703 | static PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1704 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1705 | } |
| 1706 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1707 | static PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1708 | // TODO(edisonn): spec has some rules about overprint, implement them. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1709 | /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1710 | /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1711 | /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1712 | /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1713 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1714 | colorOperator->fColorSpace = strings_DeviceCMYK; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1715 | // TODO(edisonn): Set color. |
| 1716 | return kNYI_PdfResult; |
| 1717 | } |
| 1718 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1719 | static PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1720 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1721 | } |
| 1722 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1723 | static PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1724 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1725 | } |
| 1726 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1727 | static PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1728 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1729 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1730 | |
| 1731 | return kOK_PdfResult; |
| 1732 | } |
| 1733 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1734 | static PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1735 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1736 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1737 | pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType); |
| 1738 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1739 | |
edisonn@google.com | 96ba3aa | 2013-07-28 20:04:35 +0000 | [diff] [blame] | 1740 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1743 | static PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1744 | *looper = new PdfCompatibilitySectionLooper(); |
| 1745 | return kOK_PdfResult; |
| 1746 | } |
| 1747 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1748 | static PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1749 | #ifdef ASSERT_BAD_PDF_OPS |
| 1750 | SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's |
| 1751 | // have the assert when testing good pdfs. |
| 1752 | #endif |
| 1753 | return kIgnoreError_PdfResult; |
| 1754 | } |
| 1755 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1756 | static PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1757 | *looper = new PdfInlineImageLooper(); |
| 1758 | return kOK_PdfResult; |
| 1759 | } |
| 1760 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1761 | static PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1762 | #ifdef ASSERT_BAD_PDF_OPS |
| 1763 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 1764 | // have the assert when testing good pdfs. |
| 1765 | #endif |
| 1766 | return kIgnoreError_PdfResult; |
| 1767 | } |
| 1768 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1769 | static PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1770 | #ifdef ASSERT_BAD_PDF_OPS |
| 1771 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 1772 | // have the assert when testing good pdfs. |
| 1773 | #endif |
| 1774 | return kIgnoreError_PdfResult; |
| 1775 | } |
| 1776 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1777 | |
| 1778 | // TODO(edisonn): security review here, make sure all parameters are valid, and safe. |
| 1779 | PdfResult skpdfGraphicsStateApply_ca(PdfContext* pdfContext, double ca) { |
| 1780 | pdfContext->fGraphicsState.fNonStroking.fOpacity = ca; |
| 1781 | return kOK_PdfResult; |
| 1782 | } |
| 1783 | |
| 1784 | PdfResult skpdfGraphicsStateApply_CA(PdfContext* pdfContext, double CA) { |
| 1785 | pdfContext->fGraphicsState.fStroking.fOpacity = CA; |
| 1786 | return kOK_PdfResult; |
| 1787 | } |
| 1788 | |
| 1789 | PdfResult skpdfGraphicsStateApplyLW(PdfContext* pdfContext, double lineWidth) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1790 | pdfContext->fGraphicsState.fLineWidth = lineWidth; |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1791 | return kOK_PdfResult; |
| 1792 | } |
| 1793 | |
| 1794 | PdfResult skpdfGraphicsStateApplyLC(PdfContext* pdfContext, int64_t lineCap) { |
| 1795 | pdfContext->fGraphicsState.fLineCap = (int)lineCap; |
| 1796 | return kOK_PdfResult; |
| 1797 | } |
| 1798 | |
| 1799 | PdfResult skpdfGraphicsStateApplyLJ(PdfContext* pdfContext, int64_t lineJoin) { |
| 1800 | pdfContext->fGraphicsState.fLineJoin = (int)lineJoin; |
| 1801 | return kOK_PdfResult; |
| 1802 | } |
| 1803 | |
| 1804 | PdfResult skpdfGraphicsStateApplyML(PdfContext* pdfContext, double miterLimit) { |
| 1805 | pdfContext->fGraphicsState.fMiterLimit = miterLimit; |
| 1806 | return kOK_PdfResult; |
| 1807 | } |
| 1808 | |
| 1809 | // TODO(edisonn): implement all rules, as of now 3) and 5) and 6) do not seem suported by skia, but I am not sure |
| 1810 | /* |
| 1811 | 1) [ ] 0 No dash; solid, unbroken lines |
| 1812 | 2) [3] 0 3 units on, 3 units off, … |
| 1813 | 3) [2] 1 1 on, 2 off, 2 on, 2 off, … |
| 1814 | 4) [2 1] 0 2 on, 1 off, 2 on, 1 off, … |
| 1815 | 5) [3 5] 6 2 off, 3 on, 5 off, 3 on, 5 off, … |
| 1816 | 6) [2 3] 11 1 on, 3 off, 2 on, 3 off, 2 on, … |
| 1817 | */ |
| 1818 | |
| 1819 | PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* intervals, SkPdfObject* phase) { |
| 1820 | int cnt = intervals->size(); |
| 1821 | if (cnt >= 256) { |
| 1822 | // TODO(edisonn): report error/warning, unsuported; |
| 1823 | // TODO(edisonn): alloc memory |
| 1824 | return kIgnoreError_PdfResult; |
| 1825 | } |
| 1826 | for (int i = 0; i < cnt; i++) { |
| 1827 | if (!intervals->objAtAIndex(i)->isNumber()) { |
| 1828 | // TODO(edisonn): report error/warning |
| 1829 | return kIgnoreError_PdfResult; |
| 1830 | } |
| 1831 | } |
| 1832 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1833 | double total = 0; |
| 1834 | for (int i = 0 ; i < cnt; i++) { |
| 1835 | pdfContext->fGraphicsState.fDashArray[i] = intervals->objAtAIndex(i)->scalarValue(); |
| 1836 | total += pdfContext->fGraphicsState.fDashArray[i]; |
| 1837 | } |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 1838 | if (cnt & 1) { |
| 1839 | if (cnt == 1) { |
| 1840 | pdfContext->fGraphicsState.fDashArray[1] = pdfContext->fGraphicsState.fDashArray[0]; |
| 1841 | cnt++; |
| 1842 | } else { |
| 1843 | // TODO(edisonn): report error/warning |
| 1844 | return kNYI_PdfResult; |
| 1845 | } |
| 1846 | } |
| 1847 | pdfContext->fGraphicsState.fDashArrayLength = cnt; |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1848 | pdfContext->fGraphicsState.fDashPhase = phase->scalarValue(); |
| 1849 | if (pdfContext->fGraphicsState.fDashPhase == 0) { |
| 1850 | // other rules, changes? |
| 1851 | pdfContext->fGraphicsState.fDashPhase = total; |
| 1852 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1853 | |
| 1854 | return kOK_PdfResult; |
| 1855 | } |
| 1856 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1857 | PdfResult skpdfGraphicsStateApplyD(PdfContext* pdfContext, SkPdfArray* dash) { |
| 1858 | // TODO(edisonn): verify input |
| 1859 | if (!dash || dash->isArray() || dash->size() != 2 || !dash->objAtAIndex(0)->isArray() || !dash->objAtAIndex(1)->isNumber()) { |
| 1860 | // TODO(edisonn): report error/warning |
| 1861 | return kIgnoreError_PdfResult; |
| 1862 | } |
| 1863 | return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)dash->objAtAIndex(0), dash->objAtAIndex(1)); |
| 1864 | } |
| 1865 | |
| 1866 | void skpdfGraphicsStateApplyFont(PdfContext* pdfContext, SkPdfArray* fontAndSize) { |
| 1867 | if (!fontAndSize || fontAndSize->isArray() || fontAndSize->size() != 2 || !fontAndSize->objAtAIndex(0)->isName() || !fontAndSize->objAtAIndex(1)->isNumber()) { |
| 1868 | // TODO(edisonn): report error/warning |
| 1869 | return; |
| 1870 | } |
| 1871 | skpdfGraphicsStateApplyFontCore(pdfContext, fontAndSize->objAtAIndex(0), fontAndSize->objAtAIndex(1)->numberValue()); |
| 1872 | } |
| 1873 | |
| 1874 | |
| 1875 | //lineWidth w Set the line width in the graphics state (see “Line Width” on page 152). |
| 1876 | static PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1877 | double lw = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1878 | return skpdfGraphicsStateApplyLW(pdfContext, lw); |
| 1879 | } |
| 1880 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1881 | //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] | 1882 | static PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1883 | int64_t lc = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1884 | return skpdfGraphicsStateApplyLC(pdfContext, lc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | //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] | 1888 | static PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1889 | double lj = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1890 | return skpdfGraphicsStateApplyLJ(pdfContext, lj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | //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] | 1894 | static PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1895 | double ml = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1896 | return skpdfGraphicsStateApplyML(pdfContext, ml); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | //dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on |
| 1900 | //page 155). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1901 | static PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1902 | SkPdfObject* phase = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1903 | SkPdfObject* array = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1904 | |
edisonn@google.com | 24cdf13 | 2013-07-30 16:06:12 +0000 | [diff] [blame] | 1905 | if (!array->isArray()) { |
| 1906 | return kIgnoreError_PdfResult; |
| 1907 | } |
| 1908 | |
| 1909 | return skpdfGraphicsStateApplyD(pdfContext, (SkPdfArray*)array, phase); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | //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] | 1913 | static PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1914 | pdfContext->fObjectStack.pop(); |
| 1915 | |
| 1916 | return kNYI_PdfResult; |
| 1917 | } |
| 1918 | |
| 1919 | //flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness |
| 1920 | //Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci- |
| 1921 | //fies the output device’s default flatness tolerance. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 1922 | static PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1923 | pdfContext->fObjectStack.pop(); |
| 1924 | |
| 1925 | return kNYI_PdfResult; |
| 1926 | } |
| 1927 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1928 | SkTDict<SkXfermode::Mode> gPdfBlendModes(20); |
| 1929 | |
| 1930 | class InitBlendModes { |
| 1931 | public: |
| 1932 | InitBlendModes() { |
| 1933 | // TODO(edisonn): use the python code generator? |
| 1934 | // TABLE 7.2 Standard separable blend modes |
| 1935 | gPdfBlendModes.set("Normal", SkXfermode::kSrc_Mode); |
| 1936 | gPdfBlendModes.set("Multiply", SkXfermode::kMultiply_Mode); |
| 1937 | gPdfBlendModes.set("Screen", SkXfermode::kScreen_Mode); |
| 1938 | gPdfBlendModes.set("Overlay", SkXfermode::kOverlay_Mode); |
| 1939 | gPdfBlendModes.set("Darken", SkXfermode::kDarken_Mode); |
| 1940 | gPdfBlendModes.set("Lighten", SkXfermode::kLighten_Mode); |
| 1941 | gPdfBlendModes.set("ColorDodge", SkXfermode::kColorDodge_Mode); |
| 1942 | gPdfBlendModes.set("ColorBurn", SkXfermode::kColorBurn_Mode); |
| 1943 | gPdfBlendModes.set("HardLight", SkXfermode::kHardLight_Mode); |
| 1944 | gPdfBlendModes.set("SoftLight", SkXfermode::kSoftLight_Mode); |
| 1945 | gPdfBlendModes.set("Difference", SkXfermode::kDifference_Mode); |
| 1946 | gPdfBlendModes.set("Exclusion", SkXfermode::kExclusion_Mode); |
| 1947 | |
| 1948 | // TABLE 7.3 Standard nonseparable blend modes |
| 1949 | gPdfBlendModes.set("Hue", SkXfermode::kHue_Mode); |
| 1950 | gPdfBlendModes.set("Saturation", SkXfermode::kSaturation_Mode); |
| 1951 | gPdfBlendModes.set("Color", SkXfermode::kColor_Mode); |
| 1952 | gPdfBlendModes.set("Luminosity", SkXfermode::kLuminosity_Mode); |
| 1953 | } |
| 1954 | }; |
| 1955 | |
| 1956 | InitBlendModes _gDummyInniter; |
| 1957 | |
| 1958 | SkXfermode::Mode xferModeFromBlendMode(const char* blendMode, size_t len) { |
| 1959 | SkXfermode::Mode mode = (SkXfermode::Mode)(SkXfermode::kLastMode + 1); |
| 1960 | if (gPdfBlendModes.find(blendMode, len, &mode)) { |
| 1961 | return mode; |
| 1962 | } |
| 1963 | |
| 1964 | return (SkXfermode::Mode)(SkXfermode::kLastMode + 1); |
| 1965 | } |
| 1966 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1967 | void skpdfGraphicsStateApplyBM_name(PdfContext* pdfContext, const std::string& blendMode) { |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1968 | SkXfermode::Mode mode = xferModeFromBlendMode(blendMode.c_str(), blendMode.length()); |
| 1969 | if (mode <= SkXfermode::kLastMode) { |
| 1970 | pdfContext->fGraphicsState.fBlendModesLength = 1; |
| 1971 | pdfContext->fGraphicsState.fBlendModes[0] = mode; |
| 1972 | } else { |
| 1973 | // TODO(edisonn): report unknown blend mode |
| 1974 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | void skpdfGraphicsStateApplyBM_array(PdfContext* pdfContext, SkPdfArray* blendModes) { |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1978 | if (!blendModes || blendModes->isArray() || blendModes->size() == 0 || blendModes->size() > 256) { |
| 1979 | // TODO(edisonn): report error/warning |
| 1980 | return; |
| 1981 | } |
| 1982 | SkXfermode::Mode modes[256]; |
| 1983 | int cnt = blendModes->size(); |
| 1984 | for (int i = 0; i < cnt; i++) { |
| 1985 | SkPdfObject* name = blendModes->objAtAIndex(i); |
| 1986 | if (!name->isName()) { |
| 1987 | // TODO(edisonn): report error/warning |
| 1988 | return; |
| 1989 | } |
| 1990 | SkXfermode::Mode mode = xferModeFromBlendMode(name->c_str(), name->lenstr()); |
| 1991 | if (mode > SkXfermode::kLastMode) { |
| 1992 | // TODO(edisonn): report error/warning |
| 1993 | return; |
| 1994 | } |
| 1995 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 1996 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1997 | pdfContext->fGraphicsState.fBlendModesLength = cnt; |
| 1998 | for (int i = 0; i < cnt; i++) { |
| 1999 | pdfContext->fGraphicsState.fBlendModes[i] = modes[i]; |
| 2000 | } |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | void skpdfGraphicsStateApplySMask_dict(PdfContext* pdfContext, SkPdfDictionary* sMask) { |
| 2004 | // TODO(edisonn): verify input |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 2005 | if (pdfContext->fPdfDoc->mapper()->mapSoftMaskDictionary(sMask)) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2006 | pdfContext->fGraphicsState.fSoftMaskDictionary = (SkPdfSoftMaskDictionary*)sMask; |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 2007 | } else if (pdfContext->fPdfDoc->mapper()->mapSoftMaskImageDictionary(sMask)) { |
| 2008 | SkPdfSoftMaskImageDictionary* smid = (SkPdfSoftMaskImageDictionary*)sMask; |
| 2009 | pdfContext->fGraphicsState.fSMask = getImageFromObject(pdfContext, smid, true); |
| 2010 | } else { |
| 2011 | // TODO (edisonn): report error/warning |
| 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | void skpdfGraphicsStateApplySMask_name(PdfContext* pdfContext, const std::string& sMask) { |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2016 | if (sMask == "None") { |
| 2017 | pdfContext->fGraphicsState.fSoftMaskDictionary = NULL; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 2018 | pdfContext->fGraphicsState.fSMask = NULL; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 2022 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
| 2023 | SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc); |
| 2024 | |
| 2025 | if (extGStateDictionary == NULL) { |
| 2026 | #ifdef PDF_TRACE |
| 2027 | printf("ExtGState is NULL!\n"); |
| 2028 | #endif |
| 2029 | // TODO (edisonn): report error/warning |
| 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | SkPdfObject* obj = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(sMask.c_str())); |
| 2034 | if (!obj || !obj->isDictionary()) { |
| 2035 | // TODO (edisonn): report error/warning |
| 2036 | return; |
| 2037 | } |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2038 | |
| 2039 | pdfContext->fGraphicsState.fSoftMaskDictionary = NULL; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame^] | 2040 | pdfContext->fGraphicsState.fSMask = NULL; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2041 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 2042 | skpdfGraphicsStateApplySMask_dict(pdfContext, obj->asDictionary()); |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | void skpdfGraphicsStateApplyAIS(PdfContext* pdfContext, bool alphaSource) { |
| 2046 | pdfContext->fGraphicsState.fAlphaSource = alphaSource; |
| 2047 | } |
| 2048 | |
| 2049 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2050 | //dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is |
| 2051 | //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] | 2052 | static PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2053 | SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2054 | |
| 2055 | #ifdef PDF_TRACE |
| 2056 | std::string str; |
| 2057 | #endif |
| 2058 | |
| 2059 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2060 | SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2061 | |
| 2062 | if (extGStateDictionary == NULL) { |
| 2063 | #ifdef PDF_TRACE |
| 2064 | printf("ExtGState is NULL!\n"); |
| 2065 | #endif |
| 2066 | return kIgnoreError_PdfResult; |
| 2067 | } |
| 2068 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2069 | SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2070 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2071 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) { |
| 2072 | return kIgnoreError_PdfResult; |
| 2073 | } |
| 2074 | SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2075 | |
| 2076 | // TODO(edisonn): now load all those properties in graphic state. |
| 2077 | if (gs == NULL) { |
| 2078 | return kIgnoreError_PdfResult; |
| 2079 | } |
| 2080 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2081 | if (gs->has_LW()) { |
| 2082 | skpdfGraphicsStateApplyLW(pdfContext, gs->LW(pdfContext->fPdfDoc)); |
| 2083 | } |
| 2084 | |
| 2085 | if (gs->has_LC()) { |
| 2086 | skpdfGraphicsStateApplyLC(pdfContext, gs->LC(pdfContext->fPdfDoc)); |
| 2087 | } |
| 2088 | |
| 2089 | if (gs->has_LJ()) { |
| 2090 | skpdfGraphicsStateApplyLJ(pdfContext, gs->LJ(pdfContext->fPdfDoc)); |
| 2091 | } |
| 2092 | |
| 2093 | if (gs->has_ML()) { |
| 2094 | skpdfGraphicsStateApplyML(pdfContext, gs->ML(pdfContext->fPdfDoc)); |
| 2095 | } |
| 2096 | |
| 2097 | if (gs->has_D()) { |
| 2098 | skpdfGraphicsStateApplyD(pdfContext, gs->D(pdfContext->fPdfDoc)); |
| 2099 | } |
| 2100 | |
| 2101 | if (gs->has_Font()) { |
| 2102 | skpdfGraphicsStateApplyFont(pdfContext, gs->Font(pdfContext->fPdfDoc)); |
| 2103 | } |
| 2104 | |
| 2105 | if (gs->has_BM()) { |
| 2106 | if (gs->isBMAName(pdfContext->fPdfDoc)) { |
| 2107 | skpdfGraphicsStateApplyBM_name(pdfContext, gs->getBMAsName(pdfContext->fPdfDoc)); |
| 2108 | } else if (gs->isBMAArray(pdfContext->fPdfDoc)) { |
| 2109 | skpdfGraphicsStateApplyBM_array(pdfContext, gs->getBMAsArray(pdfContext->fPdfDoc)); |
| 2110 | } else { |
| 2111 | // TODO(edisonn): report/warn |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | if (gs->has_SMask()) { |
| 2116 | if (gs->isSMaskAName(pdfContext->fPdfDoc)) { |
| 2117 | skpdfGraphicsStateApplySMask_name(pdfContext, gs->getSMaskAsName(pdfContext->fPdfDoc)); |
| 2118 | } else if (gs->isSMaskADictionary(pdfContext->fPdfDoc)) { |
| 2119 | skpdfGraphicsStateApplySMask_dict(pdfContext, gs->getSMaskAsDictionary(pdfContext->fPdfDoc)); |
| 2120 | } else { |
| 2121 | // TODO(edisonn): report/warn |
| 2122 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | if (gs->has_ca()) { |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2126 | skpdfGraphicsStateApply_ca(pdfContext, gs->ca(pdfContext->fPdfDoc)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2129 | if (gs->has_CA()) { |
| 2130 | skpdfGraphicsStateApply_CA(pdfContext, gs->CA(pdfContext->fPdfDoc)); |
| 2131 | } |
| 2132 | |
| 2133 | if (gs->has_AIS()) { |
| 2134 | skpdfGraphicsStateApplyAIS(pdfContext, gs->AIS(pdfContext->fPdfDoc)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 2137 | return kOK_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | //charSpace Tc Set the character spacing, Tc |
| 2141 | //, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators. |
| 2142 | //Initial value: 0. |
| 2143 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2144 | double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2145 | pdfContext->fGraphicsState.fCharSpace = charSpace; |
| 2146 | |
| 2147 | return kOK_PdfResult; |
| 2148 | } |
| 2149 | |
| 2150 | //wordSpace Tw Set the word spacing, T |
| 2151 | //w |
| 2152 | //, to wordSpace, which is a number expressed in unscaled |
| 2153 | //text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial |
| 2154 | //value: 0. |
| 2155 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2156 | double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2157 | pdfContext->fGraphicsState.fWordSpace = wordSpace; |
| 2158 | |
| 2159 | return kOK_PdfResult; |
| 2160 | } |
| 2161 | |
| 2162 | //scale Tz Set the horizontal scaling, Th |
| 2163 | //, to (scale ˜ 100). scale is a number specifying the |
| 2164 | //percentage of the normal width. Initial value: 100 (normal width). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2165 | static PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2166 | /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2167 | |
| 2168 | return kNYI_PdfResult; |
| 2169 | } |
| 2170 | |
| 2171 | //render Tr Set the text rendering mode, T |
| 2172 | //mode, to render, which is an integer. Initial value: 0. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2173 | static PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2174 | /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2175 | |
| 2176 | return kNYI_PdfResult; |
| 2177 | } |
| 2178 | //rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space |
| 2179 | //units. Initial value: 0. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2180 | static PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2181 | /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2182 | |
| 2183 | return kNYI_PdfResult; |
| 2184 | } |
| 2185 | |
| 2186 | //wx wy d0 |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2187 | static PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2188 | pdfContext->fObjectStack.pop(); |
| 2189 | pdfContext->fObjectStack.pop(); |
| 2190 | |
| 2191 | return kNYI_PdfResult; |
| 2192 | } |
| 2193 | |
| 2194 | //wx wy llx lly urx ury d1 |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2195 | static PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2196 | pdfContext->fObjectStack.pop(); |
| 2197 | pdfContext->fObjectStack.pop(); |
| 2198 | pdfContext->fObjectStack.pop(); |
| 2199 | pdfContext->fObjectStack.pop(); |
| 2200 | pdfContext->fObjectStack.pop(); |
| 2201 | pdfContext->fObjectStack.pop(); |
| 2202 | |
| 2203 | return kNYI_PdfResult; |
| 2204 | } |
| 2205 | |
| 2206 | //name sh |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2207 | static PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2208 | pdfContext->fObjectStack.pop(); |
| 2209 | |
| 2210 | return kNYI_PdfResult; |
| 2211 | } |
| 2212 | |
| 2213 | //name Do |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2214 | static PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2215 | SkPdfObject* name = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2216 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2217 | SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2218 | |
| 2219 | if (xObject == NULL) { |
| 2220 | #ifdef PDF_TRACE |
| 2221 | printf("XObject is NULL!\n"); |
| 2222 | #endif |
| 2223 | return kIgnoreError_PdfResult; |
| 2224 | } |
| 2225 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2226 | SkPdfObject* value = xObject->get(name); |
| 2227 | value = pdfContext->fPdfDoc->resolveReference(value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2228 | |
| 2229 | #ifdef PDF_TRACE |
| 2230 | // value->ToString(str); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2231 | // printf("Do object value: %s\n", str); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2232 | #endif |
| 2233 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2234 | return doXObject(pdfContext, canvas, value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | //tag MP Designate a marked-content point. tag is a name object indicating the role or |
| 2238 | //significance of the point. |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2239 | static PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2240 | pdfContext->fObjectStack.pop(); |
| 2241 | |
| 2242 | return kNYI_PdfResult; |
| 2243 | } |
| 2244 | |
| 2245 | //tag properties DP Designate a marked-content point with an associated property list. tag is a |
| 2246 | //name object indicating the role or significance of the point; properties is |
| 2247 | //either an inline dictionary containing the property list or a name object |
| 2248 | //associated with it in the Properties subdictionary of the current resource |
| 2249 | //dictionary (see Section 9.5.1, “Property Lists”). |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2250 | static PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2251 | pdfContext->fObjectStack.pop(); |
| 2252 | pdfContext->fObjectStack.pop(); |
| 2253 | |
| 2254 | return kNYI_PdfResult; |
| 2255 | } |
| 2256 | |
| 2257 | //tag BMC Begin a marked-content sequence terminated by a balancing EMC operator. |
| 2258 | //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] | 2259 | static PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2260 | pdfContext->fObjectStack.pop(); |
| 2261 | |
| 2262 | return kNYI_PdfResult; |
| 2263 | } |
| 2264 | |
| 2265 | //tag properties BDC Begin a marked-content sequence with an associated property list, terminated |
| 2266 | //by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the |
| 2267 | //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] | 2268 | static PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2269 | pdfContext->fObjectStack.pop(); |
| 2270 | pdfContext->fObjectStack.pop(); |
| 2271 | |
| 2272 | return kNYI_PdfResult; |
| 2273 | } |
| 2274 | |
| 2275 | //— 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] | 2276 | static PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2277 | return kNYI_PdfResult; |
| 2278 | } |
| 2279 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 2280 | static void initPdfOperatorRenderes() { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2281 | static bool gInitialized = false; |
| 2282 | if (gInitialized) { |
| 2283 | return; |
| 2284 | } |
| 2285 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2286 | gPdfOps.set("q", PdfOp_q); |
| 2287 | gPdfOps.set("Q", PdfOp_Q); |
| 2288 | gPdfOps.set("cm", PdfOp_cm); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2289 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2290 | gPdfOps.set("TD", PdfOp_TD); |
| 2291 | gPdfOps.set("Td", PdfOp_Td); |
| 2292 | gPdfOps.set("Tm", PdfOp_Tm); |
| 2293 | gPdfOps.set("T*", PdfOp_T_star); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2294 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2295 | gPdfOps.set("m", PdfOp_m); |
| 2296 | gPdfOps.set("l", PdfOp_l); |
| 2297 | gPdfOps.set("c", PdfOp_c); |
| 2298 | gPdfOps.set("v", PdfOp_v); |
| 2299 | gPdfOps.set("y", PdfOp_y); |
| 2300 | gPdfOps.set("h", PdfOp_h); |
| 2301 | gPdfOps.set("re", PdfOp_re); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2302 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2303 | gPdfOps.set("S", PdfOp_S); |
| 2304 | gPdfOps.set("s", PdfOp_s); |
| 2305 | gPdfOps.set("f", PdfOp_f); |
| 2306 | gPdfOps.set("F", PdfOp_F); |
| 2307 | gPdfOps.set("f*", PdfOp_f_star); |
| 2308 | gPdfOps.set("B", PdfOp_B); |
| 2309 | gPdfOps.set("B*", PdfOp_B_star); |
| 2310 | gPdfOps.set("b", PdfOp_b); |
| 2311 | gPdfOps.set("b*", PdfOp_b_star); |
| 2312 | gPdfOps.set("n", PdfOp_n); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2313 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2314 | gPdfOps.set("BT", PdfOp_BT); |
| 2315 | gPdfOps.set("ET", PdfOp_ET); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2316 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2317 | gPdfOps.set("Tj", PdfOp_Tj); |
| 2318 | gPdfOps.set("'", PdfOp_quote); |
| 2319 | gPdfOps.set("\"", PdfOp_doublequote); |
| 2320 | gPdfOps.set("TJ", PdfOp_TJ); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2321 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2322 | gPdfOps.set("CS", PdfOp_CS); |
| 2323 | gPdfOps.set("cs", PdfOp_cs); |
| 2324 | gPdfOps.set("SC", PdfOp_SC); |
| 2325 | gPdfOps.set("SCN", PdfOp_SCN); |
| 2326 | gPdfOps.set("sc", PdfOp_sc); |
| 2327 | gPdfOps.set("scn", PdfOp_scn); |
| 2328 | gPdfOps.set("G", PdfOp_G); |
| 2329 | gPdfOps.set("g", PdfOp_g); |
| 2330 | gPdfOps.set("RG", PdfOp_RG); |
| 2331 | gPdfOps.set("rg", PdfOp_rg); |
| 2332 | gPdfOps.set("K", PdfOp_K); |
| 2333 | gPdfOps.set("k", PdfOp_k); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2334 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2335 | gPdfOps.set("W", PdfOp_W); |
| 2336 | gPdfOps.set("W*", PdfOp_W_star); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2337 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2338 | gPdfOps.set("BX", PdfOp_BX); |
| 2339 | gPdfOps.set("EX", PdfOp_EX); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2340 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2341 | gPdfOps.set("BI", PdfOp_BI); |
| 2342 | gPdfOps.set("ID", PdfOp_ID); |
| 2343 | gPdfOps.set("EI", PdfOp_EI); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2344 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2345 | gPdfOps.set("w", PdfOp_w); |
| 2346 | gPdfOps.set("J", PdfOp_J); |
| 2347 | gPdfOps.set("j", PdfOp_j); |
| 2348 | gPdfOps.set("M", PdfOp_M); |
| 2349 | gPdfOps.set("d", PdfOp_d); |
| 2350 | gPdfOps.set("ri", PdfOp_ri); |
| 2351 | gPdfOps.set("i", PdfOp_i); |
| 2352 | gPdfOps.set("gs", PdfOp_gs); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2353 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2354 | gPdfOps.set("Tc", PdfOp_Tc); |
| 2355 | gPdfOps.set("Tw", PdfOp_Tw); |
| 2356 | gPdfOps.set("Tz", PdfOp_Tz); |
| 2357 | gPdfOps.set("TL", PdfOp_TL); |
| 2358 | gPdfOps.set("Tf", PdfOp_Tf); |
| 2359 | gPdfOps.set("Tr", PdfOp_Tr); |
| 2360 | gPdfOps.set("Ts", PdfOp_Ts); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2361 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2362 | gPdfOps.set("d0", PdfOp_d0); |
| 2363 | gPdfOps.set("d1", PdfOp_d1); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2364 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2365 | gPdfOps.set("sh", PdfOp_sh); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2366 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2367 | gPdfOps.set("Do", PdfOp_Do); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2368 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2369 | gPdfOps.set("MP", PdfOp_MP); |
| 2370 | gPdfOps.set("DP", PdfOp_DP); |
| 2371 | gPdfOps.set("BMC", PdfOp_BMC); |
| 2372 | gPdfOps.set("BDC", PdfOp_BDC); |
| 2373 | gPdfOps.set("EMC", PdfOp_EMC); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2374 | |
| 2375 | gInitialized = true; |
| 2376 | } |
| 2377 | |
| 2378 | class InitPdfOps { |
| 2379 | public: |
| 2380 | InitPdfOps() { |
| 2381 | initPdfOperatorRenderes(); |
| 2382 | } |
| 2383 | }; |
| 2384 | |
| 2385 | InitPdfOps gInitPdfOps; |
| 2386 | |
| 2387 | void reportPdfRenderStats() { |
| 2388 | std::map<std::string, int>::iterator iter; |
| 2389 | |
| 2390 | for (int i = 0 ; i < kCount_PdfResult; i++) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2391 | SkTDict<int>::Iter iter(gRenderStats[i]); |
| 2392 | const char* key; |
| 2393 | int value = 0; |
| 2394 | while ((key = iter.next(&value)) != NULL) { |
| 2395 | printf("%s: %s -> count %i\n", gRenderStatsNames[i], key, value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | PdfResult PdfMainLooper::consumeToken(PdfToken& token) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2401 | if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2402 | { |
| 2403 | // TODO(edisonn): log trace flag (verbose, error, info, warning, ...) |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2404 | PdfOperatorRenderer pdfOperatorRenderer = NULL; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2405 | if (gPdfOps.find(token.fKeyword, token.fKeywordLength, &pdfOperatorRenderer) && pdfOperatorRenderer) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2406 | // caller, main work is done by pdfOperatorRenderer(...) |
| 2407 | PdfTokenLooper* childLooper = NULL; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2408 | PdfResult result = pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper); |
| 2409 | |
| 2410 | int cnt = 0; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2411 | gRenderStats[result].find(token.fKeyword, token.fKeywordLength, &cnt); |
| 2412 | gRenderStats[result].set(token.fKeyword, token.fKeywordLength, cnt + 1); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2413 | |
| 2414 | if (childLooper) { |
| 2415 | childLooper->setUp(this); |
| 2416 | childLooper->loop(); |
| 2417 | delete childLooper; |
| 2418 | } |
| 2419 | } else { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2420 | int cnt = 0; |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 2421 | gRenderStats[kUnsupported_PdfResult].find(token.fKeyword, token.fKeywordLength, &cnt); |
| 2422 | gRenderStats[kUnsupported_PdfResult].set(token.fKeyword, token.fKeywordLength, cnt + 1); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | else if (token.fType == kObject_TokenType) |
| 2426 | { |
| 2427 | fPdfContext->fObjectStack.push( token.fObject ); |
| 2428 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2429 | else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 2430 | // TODO(edisonn): deine or use assert not reached |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2431 | return kIgnoreError_PdfResult; |
| 2432 | } |
| 2433 | return kOK_PdfResult; |
| 2434 | } |
| 2435 | |
| 2436 | void PdfMainLooper::loop() { |
| 2437 | PdfToken token; |
| 2438 | while (readToken(fTokenizer, &token)) { |
| 2439 | consumeToken(token); |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) { |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 2444 | SkASSERT(false); |
| 2445 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | void PdfInlineImageLooper::loop() { |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 2449 | doXObject_Image(fPdfContext, fCanvas, fTokenizer->readInlineImage()); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | PdfResult PdfInlineImageLooper::done() { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2453 | return kNYI_PdfResult; |
| 2454 | } |
| 2455 | |
| 2456 | PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) { |
| 2457 | return fParent->consumeToken(token); |
| 2458 | } |
| 2459 | |
| 2460 | void PdfCompatibilitySectionLooper::loop() { |
| 2461 | // TODO(edisonn): save stacks position, or create a new stack? |
| 2462 | // TODO(edisonn): what happens if we pop out more variables then when we started? |
| 2463 | // restore them? fail? We could create a new operands stack for every new BX/EX section, |
| 2464 | // pop-ing too much will not affect outside the section. |
| 2465 | PdfToken token; |
| 2466 | while (readToken(fTokenizer, &token)) { |
| 2467 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) { |
| 2468 | PdfTokenLooper* looper = new PdfCompatibilitySectionLooper(); |
| 2469 | looper->setUp(this); |
| 2470 | looper->loop(); |
| 2471 | delete looper; |
| 2472 | } else { |
| 2473 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break; |
| 2474 | fParent->consumeToken(token); |
| 2475 | } |
| 2476 | } |
| 2477 | // TODO(edisonn): restore stack. |
| 2478 | } |
| 2479 | |
| 2480 | // TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf |
| 2481 | // TODO(edisonn): Add API for Forms viewing and editing |
| 2482 | // e.g. SkBitmap getPage(int page); |
| 2483 | // int formsCount(); |
| 2484 | // SkForm getForm(int formID); // SkForm(SkRect, .. other data) |
| 2485 | // TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ... |
| 2486 | // if we load the first page, and we zoom to fit to screen horizontally, then load only those |
| 2487 | // resources needed, so the preview is fast. |
| 2488 | // TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve |
| 2489 | // references automatically. |
| 2490 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2491 | PdfContext* gPdfContext = NULL; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 2492 | |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2493 | bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) const { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2494 | if (!fPdfDoc) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2495 | return false; |
| 2496 | } |
| 2497 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2498 | if (page < 0 || page >= pages()) { |
| 2499 | return false; |
| 2500 | } |
| 2501 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2502 | PdfContext pdfContext(fPdfDoc); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 2503 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2504 | pdfContext.fOriginalMatrix = SkMatrix::I(); |
| 2505 | pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page); |
| 2506 | |
| 2507 | gPdfContext = &pdfContext; |
| 2508 | |
| 2509 | // TODO(edisonn): get matrix stuff right. |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2510 | SkScalar z = SkIntToScalar(0); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2511 | SkScalar w = dst.width(); |
| 2512 | SkScalar h = dst.height(); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2513 | |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2514 | SkScalar wp = fPdfDoc->MediaBox(page).width(); |
| 2515 | SkScalar hp = fPdfDoc->MediaBox(page).height(); |
| 2516 | |
| 2517 | 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] | 2518 | // SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 2519 | |
| 2520 | // TODO(edisonn): add flag for this app to create sourunding buffer zone |
| 2521 | // TODO(edisonn): add flagg for no clipping. |
| 2522 | // Use larger image to make sure we do not draw anything outside of page |
| 2523 | // could be used in tests. |
| 2524 | |
| 2525 | #ifdef PDF_DEBUG_3X |
| 2526 | 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)}; |
| 2527 | #else |
| 2528 | SkPoint skiaSpace[4] = {SkPoint::Make(z, h), SkPoint::Make(w, h), SkPoint::Make(w, z), SkPoint::Make(z, z)}; |
| 2529 | #endif |
| 2530 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(w, h)}; |
| 2531 | //SkPoint skiaSpace[2] = {SkPoint::Make(w, z), SkPoint::Make(z, h)}; |
| 2532 | |
| 2533 | //SkPoint pdfSpace[2] = {SkPoint::Make(z, z), SkPoint::Make(z, h)}; |
| 2534 | //SkPoint skiaSpace[2] = {SkPoint::Make(z, h), SkPoint::Make(z, z)}; |
| 2535 | |
| 2536 | //SkPoint pdfSpace[3] = {SkPoint::Make(z, z), SkPoint::Make(z, h), SkPoint::Make(w, h)}; |
| 2537 | //SkPoint skiaSpace[3] = {SkPoint::Make(z, h), SkPoint::Make(z, z), SkPoint::Make(w, 0)}; |
| 2538 | |
| 2539 | SkAssertResult(pdfContext.fOriginalMatrix.setPolyToPoly(pdfSpace, skiaSpace, 4)); |
| 2540 | SkTraceMatrix(pdfContext.fOriginalMatrix, "Original matrix"); |
| 2541 | |
| 2542 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 2543 | pdfContext.fGraphicsState.fCTM = pdfContext.fOriginalMatrix; |
| 2544 | pdfContext.fGraphicsState.fMatrixTm = pdfContext.fGraphicsState.fCTM; |
| 2545 | pdfContext.fGraphicsState.fMatrixTlm = pdfContext.fGraphicsState.fCTM; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2546 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2547 | #ifndef PDF_DEBUG_NO_PAGE_CLIPING |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2548 | canvas->clipRect(dst, SkRegion::kIntersect_Op, true); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2549 | #endif |
| 2550 | |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 2551 | canvas->setMatrix(pdfContext.fOriginalMatrix); |
| 2552 | |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 2553 | doPage(&pdfContext, canvas, fPdfDoc->page(page)); |
| 2554 | |
| 2555 | // TODO(edisonn:) erase with white before draw? |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2556 | // SkPaint paint; |
edisonn@google.com | 88fc03d | 2013-07-30 13:34:10 +0000 | [diff] [blame] | 2557 | // paint.setColor(SK_ColorWHITE); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2558 | // canvas->drawRect(rect, paint); |
| 2559 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2560 | |
| 2561 | canvas->flush(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2562 | return true; |
| 2563 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2564 | |
| 2565 | bool SkPdfRenderer::load(const SkString inputFileName) { |
| 2566 | unload(); |
| 2567 | |
| 2568 | // TODO(edisonn): create static function that could return NULL if there are errors |
| 2569 | fPdfDoc = new SkNativeParsedPDF(inputFileName.c_str()); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 2570 | if (fPdfDoc->pages() == 0) { |
| 2571 | delete fPdfDoc; |
| 2572 | fPdfDoc = NULL; |
| 2573 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2574 | |
| 2575 | return fPdfDoc != NULL; |
| 2576 | } |
| 2577 | |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 2578 | bool SkPdfRenderer::load(SkStream* stream) { |
| 2579 | unload(); |
| 2580 | |
| 2581 | // TODO(edisonn): create static function that could return NULL if there are errors |
| 2582 | fPdfDoc = new SkNativeParsedPDF(stream); |
| 2583 | if (fPdfDoc->pages() == 0) { |
| 2584 | delete fPdfDoc; |
| 2585 | fPdfDoc = NULL; |
| 2586 | } |
| 2587 | |
| 2588 | return fPdfDoc != NULL; |
| 2589 | } |
| 2590 | |
| 2591 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 2592 | int SkPdfRenderer::pages() const { |
| 2593 | return fPdfDoc != NULL ? fPdfDoc->pages() : 0; |
| 2594 | } |
| 2595 | |
| 2596 | void SkPdfRenderer::unload() { |
| 2597 | delete fPdfDoc; |
| 2598 | fPdfDoc = NULL; |
| 2599 | } |
| 2600 | |
| 2601 | SkRect SkPdfRenderer::MediaBox(int page) const { |
| 2602 | SkASSERT(fPdfDoc); |
| 2603 | return fPdfDoc->MediaBox(page); |
| 2604 | } |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 2605 | |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 2606 | size_t SkPdfRenderer::bytesUsed() const { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 2607 | return fPdfDoc ? fPdfDoc->bytesUsed() : 0; |
| 2608 | } |
edisonn@google.com | 147adb1 | 2013-07-24 15:56:19 +0000 | [diff] [blame] | 2609 | |
| 2610 | bool SkPDFNativeRenderToBitmap(SkStream* stream, |
| 2611 | SkBitmap* output, |
| 2612 | int page, |
| 2613 | SkPdfContent content, |
| 2614 | double dpi) { |
| 2615 | SkASSERT(page >= 0); |
| 2616 | SkPdfRenderer renderer; |
| 2617 | renderer.load(stream); |
| 2618 | if (!renderer.loaded() || page >= renderer.pages() || page < 0) { |
| 2619 | return false; |
| 2620 | } |
| 2621 | |
| 2622 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
| 2623 | |
| 2624 | SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(dpi / 72.0))); |
| 2625 | SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(dpi / 72.0))); |
| 2626 | |
| 2627 | rect = SkRect::MakeWH(width, height); |
| 2628 | |
| 2629 | setup_bitmap(output, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height)); |
| 2630 | |
| 2631 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*output))); |
| 2632 | SkCanvas canvas(device); |
| 2633 | |
| 2634 | return renderer.renderPage(page, &canvas, rect); |
| 2635 | } |