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