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