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