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