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