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