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