edisonn@google.com | cf2cfa1 | 2013-08-21 16:31:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkPdfNativeObject_DEFINED |
| 9 | #define SkPdfNativeObject_DEFINED |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 10 | |
| 11 | #include <stdint.h> |
| 12 | #include <string.h> |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
| 15 | #include "SkTDict.h" |
| 16 | #include "SkRect.h" |
| 17 | #include "SkMatrix.h" |
| 18 | #include "SkString.h" |
| 19 | |
| 20 | #include "SkPdfNYI.h" |
| 21 | #include "SkPdfConfig.h" |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 22 | #include "SkPdfUtils.h" |
| 23 | |
| 24 | #include "SkPdfNativeTokenizer.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 25 | |
| 26 | class SkPdfDictionary; |
| 27 | class SkPdfStream; |
| 28 | class SkPdfAllocator; |
| 29 | |
| 30 | // TODO(edisonn): macro it and move it to utils |
| 31 | SkMatrix SkMatrixFromPdfMatrix(double array[6]); |
| 32 | |
| 33 | |
| 34 | #define kFilteredStreamBit 0 |
| 35 | #define kUnfilteredStreamBit 1 |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 36 | #define kOwnedStreamBit 2 |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 37 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 38 | class SkPdfNativeObject { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 39 | public: |
| 40 | enum ObjectType { |
edisonn@google.com | af54a51 | 2013-09-13 19:33:42 +0000 | [diff] [blame^] | 41 | // The type will have only one of these values, but for error reporting, we make it an enum |
| 42 | // so it can easily report that something was expected to be one of a few types |
| 43 | kInvalid_PdfObjectType = 1 << 1, |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 44 | |
edisonn@google.com | af54a51 | 2013-09-13 19:33:42 +0000 | [diff] [blame^] | 45 | kBoolean_PdfObjectType = 1 << 2, |
| 46 | kInteger_PdfObjectType = 1 << 3, |
| 47 | kReal_PdfObjectType = 1 << 4, |
| 48 | _kNumber_PdfObjectType = kInteger_PdfObjectType | kReal_PdfObjectType, |
| 49 | kString_PdfObjectType = 1 << 5, |
| 50 | kHexString_PdfObjectType = 1 << 6, |
| 51 | _kAnyString_PdfObjectType = kString_PdfObjectType | kHexString_PdfObjectType, |
| 52 | kName_PdfObjectType = 1 << 7, |
| 53 | kKeyword_PdfObjectType = 1 << 8, |
| 54 | _kStream_PdfObjectType = 1 << 9, // attached to a Dictionary, do not use |
| 55 | kArray_PdfObjectType = 1 << 10, |
| 56 | kDictionary_PdfObjectType = 1 << 11, |
| 57 | kNull_PdfObjectType = 1 << 12, |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 58 | |
| 59 | // TODO(edisonn): after the pdf has been loaded completely, resolve all references |
| 60 | // try the same thing with delayed loaded ... |
edisonn@google.com | af54a51 | 2013-09-13 19:33:42 +0000 | [diff] [blame^] | 61 | kReference_PdfObjectType = 1 << 13, |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 62 | |
edisonn@google.com | af54a51 | 2013-09-13 19:33:42 +0000 | [diff] [blame^] | 63 | kUndefined_PdfObjectType = 1 << 14, // per 1.4 spec, if the same key appear twice in the dictionary, the value is undefined |
| 64 | |
| 65 | _kObject_PdfObjectType = -1, |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 68 | enum DataType { |
| 69 | kEmpty_Data, |
| 70 | kFont_Data, |
| 71 | kBitmap_Data, |
| 72 | }; |
| 73 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 74 | private: |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 75 | // TODO(edisonn): assert reset operations while in rendering! |
| 76 | uint32_t fInRendering : 1; |
| 77 | uint32_t fUnused : 31; |
| 78 | |
| 79 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 80 | struct Reference { |
| 81 | unsigned int fId; |
| 82 | unsigned int fGen; |
| 83 | }; |
| 84 | |
| 85 | // TODO(edisonn): add stream start, stream end, where stream is weither the file |
| 86 | // or decoded/filtered pdf stream |
| 87 | |
| 88 | // TODO(edisonn): add warning/report per object |
| 89 | // TODO(edisonn): add flag fUsed, to be used once the parsing is complete, |
| 90 | // so we could show what parts have been proccessed, ignored, or generated errors |
| 91 | |
| 92 | ObjectType fObjectType; |
| 93 | |
| 94 | union { |
| 95 | bool fBooleanValue; |
| 96 | int64_t fIntegerValue; |
| 97 | // TODO(edisonn): double, float? typedefed |
| 98 | double fRealValue; |
| 99 | NotOwnedString fStr; |
| 100 | |
| 101 | // TODO(edisonn): make sure the foorprint of fArray and fMap is small, otherwise, use pointers, or classes with up to 8 bytes in footprint |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 102 | SkTDArray<SkPdfNativeObject*>* fArray; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 103 | Reference fRef; |
| 104 | }; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 105 | SkTDict<SkPdfNativeObject*>* fMap; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 106 | |
| 107 | // TODO(edisonn): rename data with cache |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 108 | void* fData; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 109 | DataType fDataType; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 110 | |
| 111 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 112 | // Keep this the last entries |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 113 | #ifdef PDF_TRACK_OBJECT_USAGE |
| 114 | mutable bool fUsed; |
| 115 | #endif // PDF_TRACK_OBJECT_USAGE |
| 116 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 117 | #ifdef PDF_TRACK_STREAM_OFFSETS |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 118 | public: |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 119 | int fStreamId; |
| 120 | int fOffsetStart; |
| 121 | int fOffsetEnd; |
| 122 | #endif // PDF_TRACK_STREAM_OFFSETS |
| 123 | |
| 124 | public: |
| 125 | |
| 126 | #ifdef PDF_TRACK_STREAM_OFFSETS |
| 127 | int streamId() const { return fStreamId; } |
| 128 | int offsetStart() const { return fOffsetStart; } |
| 129 | int offsetEnd() const { return fOffsetEnd; } |
| 130 | #endif // PDF_TRACK_STREAM_OFFSETS |
| 131 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 132 | |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 133 | SkPdfNativeObject() : fInRendering(0) |
| 134 | , fObjectType(kInvalid_PdfObjectType) |
| 135 | , fMap(NULL) |
| 136 | , fData(NULL) |
| 137 | , fDataType(kEmpty_Data) |
| 138 | #ifdef PDF_TRACK_OBJECT_USAGE |
| 139 | , fUsed(false) |
| 140 | #endif // PDF_TRACK_OBJECT_USAGE |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 141 | |
| 142 | #ifdef PDF_TRACK_STREAM_OFFSETS |
| 143 | , fStreamId(-1) |
| 144 | , fOffsetStart(-1) |
| 145 | , fOffsetEnd(-1) |
| 146 | #endif // PDF_TRACK_STREAM_OFFSETS |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 147 | {} |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 148 | |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 149 | bool inRendering() const { return fInRendering != 0; } |
| 150 | void startRendering() {fInRendering = 1;} |
| 151 | void doneRendering() {fInRendering = 0;} |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 152 | |
| 153 | inline bool hasData(DataType type) { |
| 154 | return type == fDataType; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 157 | inline void* data(DataType type) { |
| 158 | return type == fDataType ? fData : NULL; |
| 159 | } |
| 160 | |
| 161 | inline void setData(void* data, DataType type) { |
| 162 | releaseData(); |
| 163 | fDataType = type; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 164 | fData = data; |
| 165 | } |
| 166 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 167 | void releaseData(); |
| 168 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 169 | // ~SkPdfNativeObject() { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 170 | // //reset(); must be called manually! |
| 171 | // } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 172 | |
| 173 | void reset() { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 174 | SkPdfMarkObjectUnused(); |
| 175 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 176 | switch (fObjectType) { |
| 177 | case kArray_PdfObjectType: |
| 178 | delete fArray; |
| 179 | break; |
| 180 | |
| 181 | case kDictionary_PdfObjectType: |
| 182 | delete fMap; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 183 | if (isStreamOwned()) { |
| 184 | delete[] fStr.fBuffer; |
| 185 | fStr.fBuffer = NULL; |
| 186 | fStr.fBytes = 0; |
| 187 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 188 | break; |
| 189 | |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | fObjectType = kInvalid_PdfObjectType; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 194 | releaseData(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 195 | } |
| 196 | |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 197 | ObjectType type() { |
| 198 | SkPdfMarkObjectUsed(); |
| 199 | |
| 200 | return fObjectType; |
| 201 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 202 | |
| 203 | const char* c_str() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 204 | SkPdfMarkObjectUsed(); |
| 205 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 206 | switch (fObjectType) { |
| 207 | case kString_PdfObjectType: |
| 208 | case kHexString_PdfObjectType: |
| 209 | case kKeyword_PdfObjectType: |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 210 | case kName_PdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 211 | return (const char*)fStr.fBuffer; |
| 212 | |
| 213 | default: |
| 214 | // TODO(edisonn): report/warning |
| 215 | return NULL; |
| 216 | } |
| 217 | } |
| 218 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 219 | size_t lenstr() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 220 | SkPdfMarkObjectUsed(); |
| 221 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 222 | switch (fObjectType) { |
| 223 | case kString_PdfObjectType: |
| 224 | case kHexString_PdfObjectType: |
| 225 | case kKeyword_PdfObjectType: |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 226 | case kName_PdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 227 | return fStr.fBytes; |
| 228 | |
| 229 | default: |
| 230 | // TODO(edisonn): report/warning |
| 231 | return 0; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | |
| 236 | // TODO(edisonn): NYI |
| 237 | SkPdfDate& dateValue() const { |
| 238 | static SkPdfDate nyi; |
| 239 | return nyi; |
| 240 | } |
| 241 | |
| 242 | // TODO(edisonn): NYI |
| 243 | SkPdfFunction& functionValue() const { |
| 244 | static SkPdfFunction nyi; |
| 245 | return nyi; |
| 246 | } |
| 247 | |
| 248 | // TODO(edisonn): NYI |
| 249 | SkPdfFileSpec& fileSpecValue() const { |
| 250 | static SkPdfFileSpec nyi; |
| 251 | return nyi; |
| 252 | } |
| 253 | |
| 254 | // TODO(edisonn): NYI |
| 255 | SkPdfTree& treeValue() const { |
| 256 | static SkPdfTree nyi; |
| 257 | return nyi; |
| 258 | } |
| 259 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 260 | static void makeBoolean(bool value, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 261 | STORE_TRACK_PARAMETERS(obj); |
| 262 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 263 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 264 | |
| 265 | obj->fObjectType = kBoolean_PdfObjectType; |
| 266 | obj->fBooleanValue = value; |
| 267 | } |
| 268 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 269 | static SkPdfNativeObject makeBoolean(bool value GET_TRACK_PARAMETERS) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 270 | SkPdfNativeObject obj; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 271 | |
| 272 | STORE_TRACK_PARAMETERS(&obj); |
| 273 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 274 | obj.fObjectType = kBoolean_PdfObjectType; |
| 275 | obj.fBooleanValue = value; |
| 276 | return obj; |
| 277 | } |
| 278 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 279 | static void makeInteger(int64_t value, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 280 | STORE_TRACK_PARAMETERS(obj); |
| 281 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 282 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 283 | |
| 284 | obj->fObjectType = kInteger_PdfObjectType; |
| 285 | obj->fIntegerValue = value; |
| 286 | } |
| 287 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 288 | static void makeReal(double value, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 289 | STORE_TRACK_PARAMETERS(obj); |
| 290 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 291 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 292 | |
| 293 | obj->fObjectType = kReal_PdfObjectType; |
| 294 | obj->fRealValue = value; |
| 295 | } |
| 296 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 297 | static void makeNull(SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 298 | STORE_TRACK_PARAMETERS(obj); |
| 299 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 300 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 301 | |
| 302 | obj->fObjectType = kNull_PdfObjectType; |
| 303 | } |
| 304 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 305 | static SkPdfNativeObject makeNull(GET_TRACK_PARAMETERS0) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 306 | SkPdfNativeObject obj; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 307 | |
| 308 | STORE_TRACK_PARAMETERS(&obj); |
| 309 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 310 | obj.fObjectType = kNull_PdfObjectType; |
| 311 | return obj; |
| 312 | } |
| 313 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 314 | static SkPdfNativeObject kNull; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 315 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 316 | static void makeNumeric(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 317 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 318 | |
| 319 | // TODO(edisonn): NYI properly |
| 320 | // if has dot (impl), or exceeds max int, is real, otherwise is int |
| 321 | bool isInt = true; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 322 | for (const unsigned char* current = start; current < end; current++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 323 | if (*current == '.') { |
| 324 | isInt = false; |
| 325 | break; |
| 326 | } |
| 327 | // TODO(edisonn): report parse issue with numbers like "24asdasd123" |
| 328 | } |
| 329 | if (isInt) { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 330 | makeInteger(atol((const char*)start), obj PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 331 | } else { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 332 | makeReal(atof((const char*)start), obj PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 336 | static void makeReference(unsigned int id, unsigned int gen, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 337 | STORE_TRACK_PARAMETERS(obj); |
| 338 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 339 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 340 | |
| 341 | obj->fObjectType = kReference_PdfObjectType; |
| 342 | obj->fRef.fId = id; |
| 343 | obj->fRef.fGen = gen; |
| 344 | } |
| 345 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 346 | static void resetAndMakeReference(unsigned int id, unsigned int gen, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 347 | obj->reset(); |
| 348 | makeReference(id, gen, obj PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 352 | static void makeString(const unsigned char* start, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 353 | makeStringCore(start, strlen((const char*)start), obj, kString_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 354 | } |
| 355 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 356 | static void makeString(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 357 | makeStringCore(start, end - start, obj, kString_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 358 | } |
| 359 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 360 | static void makeString(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 361 | makeStringCore(start, bytes, obj, kString_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 365 | static void makeHexString(const unsigned char* start, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 366 | makeStringCore(start, strlen((const char*)start), obj, kHexString_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 367 | } |
| 368 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 369 | static void makeHexString(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 370 | makeStringCore(start, end - start, obj, kHexString_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 371 | } |
| 372 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 373 | static void makeHexString(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 374 | makeStringCore(start, bytes, obj, kHexString_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 378 | static void makeName(const unsigned char* start, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 379 | makeStringCore(start, strlen((const char*)start), obj, kName_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 380 | } |
| 381 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 382 | static void makeName(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 383 | makeStringCore(start, end - start, obj, kName_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 384 | } |
| 385 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 386 | static void makeName(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 387 | makeStringCore(start, bytes, obj, kName_PdfObjectType PUT_TRACK_PARAMETERS); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | static void makeKeyword(const unsigned char* start, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 392 | makeStringCore(start, strlen((const char*)start), obj, kKeyword_PdfObjectType PUT_TRACK_PARAMETERS); |
| 393 | } |
| 394 | |
| 395 | static void makeKeyword(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 396 | makeStringCore(start, end - start, obj, kKeyword_PdfObjectType PUT_TRACK_PARAMETERS); |
| 397 | } |
| 398 | |
| 399 | static void makeKeyword(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 400 | makeStringCore(start, bytes, obj, kKeyword_PdfObjectType PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | // TODO(edisonn): make the functions to return SkPdfArray, move these functions in SkPdfArray |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 406 | static void makeEmptyArray(SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 407 | STORE_TRACK_PARAMETERS(obj); |
| 408 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 409 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 410 | |
| 411 | obj->fObjectType = kArray_PdfObjectType; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 412 | obj->fArray = new SkTDArray<SkPdfNativeObject*>(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 413 | // return (SkPdfArray*)obj; |
| 414 | } |
| 415 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 416 | bool appendInArray(SkPdfNativeObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 417 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 418 | if (fObjectType != kArray_PdfObjectType) { |
| 419 | // TODO(edisonn): report err |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | fArray->push(obj); |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | size_t size() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 428 | SkPdfMarkObjectUsed(); |
| 429 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 430 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 431 | |
| 432 | return fArray->count(); |
| 433 | } |
| 434 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 435 | SkPdfNativeObject* objAtAIndex(int i) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 436 | SkPdfMarkObjectUsed(); |
| 437 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 438 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 439 | |
| 440 | return (*fArray)[i]; |
| 441 | } |
| 442 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 443 | SkPdfNativeObject* removeLastInArray() { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 444 | // SkPdfMarkObjectUsed(); |
| 445 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 446 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 447 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 448 | SkPdfNativeObject* ret = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 449 | fArray->pop(&ret); |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 455 | const SkPdfNativeObject* objAtAIndex(int i) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 456 | SkPdfMarkObjectUsed(); |
| 457 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 458 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 459 | |
| 460 | return (*fArray)[i]; |
| 461 | } |
| 462 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 463 | SkPdfNativeObject* operator[](int i) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 464 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 465 | |
| 466 | return (*fArray)[i]; |
| 467 | } |
| 468 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 469 | const SkPdfNativeObject* operator[](int i) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 470 | SkPdfMarkObjectUsed(); |
| 471 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 472 | SkASSERT(fObjectType == kArray_PdfObjectType); |
| 473 | |
| 474 | return (*fArray)[i]; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | // TODO(edisonn): make the functions to return SkPdfDictionary, move these functions in SkPdfDictionary |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 479 | static void makeEmptyDictionary(SkPdfNativeObject* obj GET_TRACK_PARAMETERS) { |
| 480 | STORE_TRACK_PARAMETERS(obj); |
| 481 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 482 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 483 | |
| 484 | obj->fObjectType = kDictionary_PdfObjectType; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 485 | obj->fMap = new SkTDict<SkPdfNativeObject*>(1); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 486 | obj->fStr.fBuffer = NULL; |
| 487 | obj->fStr.fBytes = 0; |
| 488 | } |
| 489 | |
| 490 | // TODO(edisonn): get all the possible names from spec, and compute a hash function |
| 491 | // that would create no overlaps in the same dictionary |
| 492 | // or build a tree of chars that when followed goes to a unique id/index/hash |
| 493 | // TODO(edisonn): generate constants like kDictFoo, kNameDict_name |
| 494 | // which will be used in code |
| 495 | // add function SkPdfFastNameKey key(const char* key); |
| 496 | // TODO(edisonn): setting the same key twike, will make the value undefined! |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 497 | bool set(const SkPdfNativeObject* key, SkPdfNativeObject* value) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 498 | //SkPdfMarkObjectUsed(); |
| 499 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 500 | SkASSERT(fObjectType == kDictionary_PdfObjectType); |
| 501 | SkASSERT(key->fObjectType == kName_PdfObjectType); |
| 502 | |
| 503 | if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionary_PdfObjectType) { |
| 504 | // TODO(edisonn): report err |
| 505 | return false; |
| 506 | } |
| 507 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 508 | //// we rewrite all delimiters and white spaces with '\0', so we expect the end of name to be '\0' |
| 509 | //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 510 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 511 | return set(key->fStr.fBuffer, key->fStr.fBytes, value); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 512 | } |
| 513 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 514 | bool set(const char* key, SkPdfNativeObject* value) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 515 | //SkPdfMarkObjectUsed(); |
| 516 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 517 | return set((const unsigned char*)key, strlen(key), value); |
| 518 | } |
| 519 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 520 | bool set(const unsigned char* key, size_t len, SkPdfNativeObject* value) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 521 | //SkPdfMarkObjectUsed(); |
| 522 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 523 | SkASSERT(fObjectType == kDictionary_PdfObjectType); |
| 524 | |
| 525 | if (fObjectType != kDictionary_PdfObjectType) { |
| 526 | // TODO(edisonn): report err |
| 527 | return false; |
| 528 | } |
| 529 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 530 | return fMap->set((const char*)key, len, value); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 531 | } |
| 532 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 533 | SkPdfNativeObject* get(const SkPdfNativeObject* key) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 534 | SkPdfMarkObjectUsed(); |
| 535 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 536 | SkASSERT(fObjectType == kDictionary_PdfObjectType); |
| 537 | SkASSERT(key->fObjectType == kName_PdfObjectType); |
| 538 | |
| 539 | if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionary_PdfObjectType) { |
| 540 | // TODO(edisonn): report err |
edisonn@google.com | 3fc4826 | 2013-07-22 15:29:55 +0000 | [diff] [blame] | 541 | return NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 542 | } |
| 543 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 544 | //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 545 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 546 | return get(key->fStr.fBuffer, key->fStr.fBytes); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 547 | } |
| 548 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 549 | SkPdfNativeObject* get(const char* key) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 550 | SkPdfMarkObjectUsed(); |
| 551 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 552 | return get((const unsigned char*)key, strlen(key)); |
| 553 | } |
| 554 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 555 | SkPdfNativeObject* get(const unsigned char* key, size_t len) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 556 | SkPdfMarkObjectUsed(); |
| 557 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 558 | SkASSERT(fObjectType == kDictionary_PdfObjectType); |
| 559 | SkASSERT(key); |
| 560 | if (fObjectType != kDictionary_PdfObjectType) { |
| 561 | // TODO(edisonn): report err |
| 562 | return NULL; |
| 563 | } |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 564 | SkPdfNativeObject* ret = NULL; |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 565 | fMap->find((const char*)key, len, &ret); |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 566 | |
| 567 | #ifdef PDF_TRACE |
| 568 | SkString _key; |
| 569 | _key.append((const char*)key, len); |
| 570 | printf("\nget(/%s) = %s\n", _key.c_str(), ret ? ret->toString(0, len + 9).c_str() : "_NOT_FOUND"); |
| 571 | #endif |
| 572 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 573 | return ret; |
| 574 | } |
| 575 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 576 | const SkPdfNativeObject* get(const SkPdfNativeObject* key) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 577 | SkPdfMarkObjectUsed(); |
| 578 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 579 | SkASSERT(fObjectType == kDictionary_PdfObjectType); |
| 580 | SkASSERT(key->fObjectType == kName_PdfObjectType); |
| 581 | |
| 582 | if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionary_PdfObjectType) { |
| 583 | // TODO(edisonn): report err |
edisonn@google.com | 3fc4826 | 2013-07-22 15:29:55 +0000 | [diff] [blame] | 584 | return NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 585 | } |
| 586 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 587 | //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 588 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 589 | return get(key->fStr.fBuffer, key->fStr.fBytes); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 590 | } |
| 591 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 592 | const SkPdfNativeObject* get(const char* key) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 593 | SkPdfMarkObjectUsed(); |
| 594 | |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 595 | return get((const unsigned char*)key, strlen(key)); |
| 596 | } |
| 597 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 598 | const SkPdfNativeObject* get(const unsigned char* key, size_t len) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 599 | SkPdfMarkObjectUsed(); |
| 600 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 601 | SkASSERT(fObjectType == kDictionary_PdfObjectType); |
| 602 | SkASSERT(key); |
| 603 | if (fObjectType != kDictionary_PdfObjectType) { |
| 604 | // TODO(edisonn): report err |
| 605 | return NULL; |
| 606 | } |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 607 | SkPdfNativeObject* ret = NULL; |
edisonn@google.com | d761e32 | 2013-07-22 17:29:43 +0000 | [diff] [blame] | 608 | fMap->find((const char*)key, len, &ret); |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 609 | |
| 610 | #ifdef PDF_TRACE |
| 611 | SkString _key; |
| 612 | _key.append((const char*)key, len); |
| 613 | printf("\nget(/%s) = %s\n", _key.c_str(), ret ? ret->toString(0, len + 9).c_str() : "_NOT_FOUND"); |
| 614 | #endif |
| 615 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 616 | return ret; |
| 617 | } |
| 618 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 619 | const SkPdfNativeObject* get(const char* key, const char* abr) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 620 | SkPdfMarkObjectUsed(); |
| 621 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 622 | const SkPdfNativeObject* ret = get(key); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 623 | // TODO(edisonn): / is a valid name, and it might be an abreviation, so "" should not be like NULL |
| 624 | // make this distiontion in generator, and remove "" from condition |
| 625 | if (ret != NULL || abr == NULL || *abr == '\0') { |
| 626 | return ret; |
| 627 | } |
| 628 | return get(abr); |
| 629 | } |
| 630 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 631 | SkPdfNativeObject* get(const char* key, const char* abr) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 632 | SkPdfMarkObjectUsed(); |
| 633 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 634 | SkPdfNativeObject* ret = get(key); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 635 | // TODO(edisonn): / is a valid name, and it might be an abreviation, so "" should not be like NULL |
| 636 | // make this distiontion in generator, and remove "" from condition |
| 637 | if (ret != NULL || abr == NULL || *abr == '\0') { |
| 638 | return ret; |
| 639 | } |
| 640 | return get(abr); |
| 641 | } |
| 642 | |
| 643 | SkPdfDictionary* asDictionary() { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 644 | SkPdfMarkObjectUsed(); |
| 645 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 646 | SkASSERT(isDictionary()); |
| 647 | if (!isDictionary()) { |
| 648 | return NULL; |
| 649 | } |
| 650 | return (SkPdfDictionary*) this; |
| 651 | } |
| 652 | |
| 653 | const SkPdfDictionary* asDictionary() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 654 | SkPdfMarkObjectUsed(); |
| 655 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 656 | SkASSERT(isDictionary()); |
| 657 | if (!isDictionary()) { |
| 658 | return NULL; |
| 659 | } |
| 660 | return (SkPdfDictionary*) this; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | bool isReference() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 665 | SkPdfMarkObjectUsed(); |
| 666 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 667 | return fObjectType == kReference_PdfObjectType; |
| 668 | } |
| 669 | |
| 670 | bool isBoolean() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 671 | SkPdfMarkObjectUsed(); |
| 672 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 673 | return fObjectType == kBoolean_PdfObjectType; |
| 674 | } |
| 675 | |
| 676 | bool isInteger() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 677 | SkPdfMarkObjectUsed(); |
| 678 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 679 | return fObjectType == kInteger_PdfObjectType; |
| 680 | } |
| 681 | private: |
| 682 | bool isReal() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 683 | SkPdfMarkObjectUsed(); |
| 684 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 685 | return fObjectType == kReal_PdfObjectType; |
| 686 | } |
| 687 | public: |
| 688 | bool isNumber() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 689 | SkPdfMarkObjectUsed(); |
| 690 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 691 | return fObjectType == kInteger_PdfObjectType || fObjectType == kReal_PdfObjectType; |
| 692 | } |
| 693 | |
| 694 | bool isKeywordReference() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 695 | SkPdfMarkObjectUsed(); |
| 696 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 697 | return fObjectType == kKeyword_PdfObjectType && fStr.fBytes == 1 && fStr.fBuffer[0] == 'R'; |
| 698 | } |
| 699 | |
| 700 | bool isKeyword() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 701 | SkPdfMarkObjectUsed(); |
| 702 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 703 | return fObjectType == kKeyword_PdfObjectType; |
| 704 | } |
| 705 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 706 | bool isKeyword(const char* keyword) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 707 | SkPdfMarkObjectUsed(); |
| 708 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 709 | if (!isKeyword()) { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | if (strlen(keyword) != fStr.fBytes) { |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | if (strncmp(keyword, (const char*)fStr.fBuffer, fStr.fBytes) != 0) { |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | return true; |
| 722 | } |
| 723 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 724 | bool isName() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 725 | SkPdfMarkObjectUsed(); |
| 726 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 727 | return fObjectType == kName_PdfObjectType; |
| 728 | } |
| 729 | |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 730 | bool isName(const char* name) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 731 | SkPdfMarkObjectUsed(); |
| 732 | |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 733 | return fObjectType == kName_PdfObjectType && fStr.fBytes == strlen(name) && strncmp((const char*)fStr.fBuffer, name, fStr.fBytes) == 0; |
| 734 | } |
| 735 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 736 | bool isArray() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 737 | SkPdfMarkObjectUsed(); |
| 738 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 739 | return fObjectType == kArray_PdfObjectType; |
| 740 | } |
| 741 | |
| 742 | bool isDate() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 743 | SkPdfMarkObjectUsed(); |
| 744 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 745 | return fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType; |
| 746 | } |
| 747 | |
| 748 | bool isDictionary() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 749 | SkPdfMarkObjectUsed(); |
| 750 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 751 | return fObjectType == kDictionary_PdfObjectType; |
| 752 | } |
| 753 | |
| 754 | bool isFunction() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 755 | SkPdfMarkObjectUsed(); |
| 756 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 757 | return false; // NYI |
| 758 | } |
| 759 | |
| 760 | bool isRectangle() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 761 | SkPdfMarkObjectUsed(); |
| 762 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 763 | return fObjectType == kArray_PdfObjectType && fArray->count() == 4; // NYI + and elems are numbers |
| 764 | } |
| 765 | |
| 766 | // TODO(edisonn): has stream .. or is stream ... TBD |
| 767 | bool hasStream() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 768 | SkPdfMarkObjectUsed(); |
| 769 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 770 | return isDictionary() && fStr.fBuffer != NULL; |
| 771 | } |
| 772 | |
| 773 | // TODO(edisonn): has stream .. or is stream ... TBD |
| 774 | const SkPdfStream* getStream() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 775 | SkPdfMarkObjectUsed(); |
| 776 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 777 | return hasStream() ? (const SkPdfStream*)this : NULL; |
| 778 | } |
| 779 | |
| 780 | SkPdfStream* getStream() { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 781 | SkPdfMarkObjectUsed(); |
| 782 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 783 | return hasStream() ? (SkPdfStream*)this : NULL; |
| 784 | } |
| 785 | |
| 786 | bool isAnyString() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 787 | SkPdfMarkObjectUsed(); |
| 788 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 789 | return fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType; |
| 790 | } |
| 791 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 792 | bool isHexString() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 793 | SkPdfMarkObjectUsed(); |
| 794 | |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 795 | return fObjectType == kHexString_PdfObjectType; |
| 796 | } |
| 797 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 798 | bool isMatrix() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 799 | SkPdfMarkObjectUsed(); |
| 800 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 801 | return fObjectType == kArray_PdfObjectType && fArray->count() == 6; // NYI + and elems are numbers |
| 802 | } |
| 803 | |
| 804 | inline int64_t intValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 805 | SkPdfMarkObjectUsed(); |
| 806 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 807 | SkASSERT(fObjectType == kInteger_PdfObjectType); |
| 808 | |
| 809 | if (fObjectType != kInteger_PdfObjectType) { |
| 810 | // TODO(edisonn): log err |
| 811 | return 0; |
| 812 | } |
| 813 | return fIntegerValue; |
| 814 | } |
| 815 | private: |
| 816 | inline double realValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 817 | SkPdfMarkObjectUsed(); |
| 818 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 819 | SkASSERT(fObjectType == kReal_PdfObjectType); |
| 820 | |
| 821 | if (fObjectType != kReal_PdfObjectType) { |
| 822 | // TODO(edisonn): log err |
| 823 | return 0; |
| 824 | } |
| 825 | return fRealValue; |
| 826 | } |
| 827 | public: |
| 828 | inline double numberValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 829 | SkPdfMarkObjectUsed(); |
| 830 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 831 | SkASSERT(isNumber()); |
| 832 | |
| 833 | if (!isNumber()) { |
| 834 | // TODO(edisonn): log err |
| 835 | return 0; |
| 836 | } |
| 837 | return fObjectType == kReal_PdfObjectType ? fRealValue : fIntegerValue; |
| 838 | } |
| 839 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 840 | inline SkScalar scalarValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 841 | SkPdfMarkObjectUsed(); |
| 842 | |
edisonn@google.com | a0cefa1 | 2013-07-28 18:34:14 +0000 | [diff] [blame] | 843 | SkASSERT(isNumber()); |
| 844 | |
| 845 | if (!isNumber()) { |
| 846 | // TODO(edisonn): log err |
| 847 | return SkIntToScalar(0); |
| 848 | } |
| 849 | return fObjectType == kReal_PdfObjectType ? SkDoubleToScalar(fRealValue) : |
| 850 | SkIntToScalar(fIntegerValue); |
| 851 | } |
| 852 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 853 | int referenceId() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 854 | SkPdfMarkObjectUsed(); |
| 855 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 856 | SkASSERT(fObjectType == kReference_PdfObjectType); |
| 857 | return fRef.fId; |
| 858 | } |
| 859 | |
| 860 | int referenceGeneration() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 861 | SkPdfMarkObjectUsed(); |
| 862 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 863 | SkASSERT(fObjectType == kReference_PdfObjectType); |
| 864 | return fRef.fGen; |
| 865 | } |
| 866 | |
| 867 | inline const char* nameValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 868 | SkPdfMarkObjectUsed(); |
| 869 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 870 | SkASSERT(fObjectType == kName_PdfObjectType); |
| 871 | |
| 872 | if (fObjectType != kName_PdfObjectType) { |
| 873 | // TODO(edisonn): log err |
| 874 | return ""; |
| 875 | } |
| 876 | return (const char*)fStr.fBuffer; |
| 877 | } |
| 878 | |
| 879 | inline const char* stringValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 880 | SkPdfMarkObjectUsed(); |
| 881 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 882 | SkASSERT(fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType); |
| 883 | |
| 884 | if (fObjectType != kString_PdfObjectType && fObjectType != kHexString_PdfObjectType) { |
| 885 | // TODO(edisonn): log err |
| 886 | return ""; |
| 887 | } |
| 888 | return (const char*)fStr.fBuffer; |
| 889 | } |
| 890 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 891 | inline NotOwnedString strRef() { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 892 | SkPdfMarkObjectUsed(); |
| 893 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 894 | switch (fObjectType) { |
| 895 | case kString_PdfObjectType: |
| 896 | case kHexString_PdfObjectType: |
| 897 | case kKeyword_PdfObjectType: |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 898 | case kName_PdfObjectType: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 899 | return fStr; |
| 900 | |
| 901 | default: |
| 902 | // TODO(edisonn): report/warning |
| 903 | return NotOwnedString(); |
| 904 | } |
| 905 | } |
| 906 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 907 | // TODO(edisonn): nameValue2 and stringValue2 are used to make code generation easy, |
| 908 | // but it is not a performat way to do it, since it will create an extra copy |
| 909 | // remove these functions and make code generated faster |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 910 | inline SkString nameValue2() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 911 | SkPdfMarkObjectUsed(); |
| 912 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 913 | SkASSERT(fObjectType == kName_PdfObjectType); |
| 914 | |
| 915 | if (fObjectType != kName_PdfObjectType) { |
| 916 | // TODO(edisonn): log err |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 917 | return SkString(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 918 | } |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 919 | return SkString((const char*)fStr.fBuffer, fStr.fBytes); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 920 | } |
| 921 | |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 922 | inline SkString stringValue2() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 923 | SkPdfMarkObjectUsed(); |
| 924 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 925 | SkASSERT(fObjectType == kString_PdfObjectType || fObjectType == kHexString_PdfObjectType); |
| 926 | |
| 927 | if (fObjectType != kString_PdfObjectType && fObjectType != kHexString_PdfObjectType) { |
| 928 | // TODO(edisonn): log err |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 929 | return SkString(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 930 | } |
edisonn@google.com | 063d707 | 2013-08-16 15:05:08 +0000 | [diff] [blame] | 931 | return SkString((const char*)fStr.fBuffer, fStr.fBytes); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | inline bool boolValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 935 | SkPdfMarkObjectUsed(); |
| 936 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 937 | SkASSERT(fObjectType == kBoolean_PdfObjectType); |
| 938 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 939 | if (fObjectType != kBoolean_PdfObjectType) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 940 | // TODO(edisonn): log err |
| 941 | return false; |
| 942 | } |
| 943 | return fBooleanValue; |
| 944 | } |
| 945 | |
| 946 | SkRect rectangleValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 947 | SkPdfMarkObjectUsed(); |
| 948 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 949 | SkASSERT(isRectangle()); |
| 950 | if (!isRectangle()) { |
| 951 | return SkRect::MakeEmpty(); |
| 952 | } |
| 953 | |
| 954 | double array[4]; |
| 955 | for (int i = 0; i < 4; i++) { |
| 956 | // TODO(edisonn): version where we could resolve references? |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 957 | const SkPdfNativeObject* elem = objAtAIndex(i); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 958 | if (elem == NULL || !elem->isNumber()) { |
| 959 | // TODO(edisonn): report error |
| 960 | return SkRect::MakeEmpty(); |
| 961 | } |
| 962 | array[i] = elem->numberValue(); |
| 963 | } |
| 964 | |
| 965 | return SkRect::MakeLTRB(SkDoubleToScalar(array[0]), |
| 966 | SkDoubleToScalar(array[1]), |
| 967 | SkDoubleToScalar(array[2]), |
| 968 | SkDoubleToScalar(array[3])); |
| 969 | } |
| 970 | |
| 971 | SkMatrix matrixValue() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 972 | SkPdfMarkObjectUsed(); |
| 973 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 974 | SkASSERT(isMatrix()); |
| 975 | if (!isMatrix()) { |
| 976 | return SkMatrix::I(); |
| 977 | } |
| 978 | |
| 979 | double array[6]; |
| 980 | for (int i = 0; i < 6; i++) { |
| 981 | // TODO(edisonn): version where we could resolve references? |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 982 | const SkPdfNativeObject* elem = objAtAIndex(i); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 983 | if (elem == NULL || !elem->isNumber()) { |
| 984 | // TODO(edisonn): report error |
| 985 | return SkMatrix::I(); |
| 986 | } |
| 987 | array[i] = elem->numberValue(); |
| 988 | } |
| 989 | |
| 990 | return SkMatrixFromPdfMatrix(array); |
| 991 | } |
| 992 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 993 | bool filterStream(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 994 | |
| 995 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 996 | bool GetFilteredStreamRef(unsigned char const** buffer, size_t* len) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 997 | SkPdfMarkObjectUsed(); |
| 998 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 999 | // TODO(edisonn): add params that couls let the last filter in place if it is jpeg or png to fast load images |
| 1000 | if (!hasStream()) { |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1004 | filterStream(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (buffer) { |
| 1007 | *buffer = fStr.fBuffer; |
| 1008 | } |
| 1009 | |
| 1010 | if (len) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1011 | *len = fStr.fBytes >> 2; // last 2 bits |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | bool isStreamFiltered() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 1018 | SkPdfMarkObjectUsed(); |
| 1019 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1020 | return hasStream() && ((fStr.fBytes & 1) == kFilteredStreamBit); |
| 1021 | } |
| 1022 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1023 | bool isStreamOwned() const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 1024 | SkPdfMarkObjectUsed(); |
| 1025 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1026 | return hasStream() && ((fStr.fBytes & 2) == kOwnedStreamBit); |
| 1027 | } |
| 1028 | |
| 1029 | bool GetUnfilteredStreamRef(unsigned char const** buffer, size_t* len) const { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 1030 | SkPdfMarkObjectUsed(); |
| 1031 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1032 | if (isStreamFiltered()) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | if (!hasStream()) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | if (buffer) { |
| 1041 | *buffer = fStr.fBuffer; |
| 1042 | } |
| 1043 | |
| 1044 | if (len) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1045 | *len = fStr.fBytes >> 2; // remove last 2 bits |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1051 | bool addStream(const unsigned char* buffer, size_t len) { |
edisonn@google.com | 0fd25d8 | 2013-09-05 16:40:34 +0000 | [diff] [blame] | 1052 | //SkPdfMarkObjectUsed(); |
| 1053 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1054 | SkASSERT(!hasStream()); |
| 1055 | SkASSERT(isDictionary()); |
| 1056 | |
| 1057 | if (!isDictionary() || hasStream()) { |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | fStr.fBuffer = buffer; |
| 1062 | fStr.fBytes = (len << 2) + kUnfilteredStreamBit; |
| 1063 | |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
edisonn@google.com | 276fed9 | 2013-08-01 21:20:47 +0000 | [diff] [blame] | 1067 | static void appendSpaces(SkString* str, int level) { |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1068 | for (int i = 0 ; i < level; i++) { |
| 1069 | str->append(" "); |
| 1070 | } |
| 1071 | } |
| 1072 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1073 | static void append(SkString* str, const char* data, size_t len, const char* prefix = "\\x") { |
| 1074 | for (unsigned int i = 0 ; i < len; i++) { |
| 1075 | if (data[i] == kNUL_PdfWhiteSpace) { |
| 1076 | str->append(prefix); |
| 1077 | str->append("00"); |
| 1078 | } else if (data[i] == kHT_PdfWhiteSpace) { |
| 1079 | str->append(prefix); |
| 1080 | str->append("09"); |
| 1081 | } else if (data[i] == kLF_PdfWhiteSpace) { |
| 1082 | str->append(prefix); |
| 1083 | str->append("0A"); |
| 1084 | } else if (data[i] == kFF_PdfWhiteSpace) { |
| 1085 | str->append(prefix); |
| 1086 | str->append("0C"); |
| 1087 | } else if (data[i] == kCR_PdfWhiteSpace) { |
| 1088 | str->append(prefix); |
| 1089 | str->append("0D"); |
| 1090 | } else { |
| 1091 | str->append(data + i, 1); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1096 | SkString toString(int firstRowLevel = 0, int level = 0) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1097 | SkString str; |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1098 | appendSpaces(&str, firstRowLevel); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1099 | switch (fObjectType) { |
| 1100 | case kInvalid_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1101 | str.append("__Invalid"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1102 | break; |
| 1103 | |
| 1104 | case kBoolean_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1105 | str.appendf("%s", fBooleanValue ? "true" : "false"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1106 | break; |
| 1107 | |
| 1108 | case kInteger_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1109 | str.appendf("%i", (int)fIntegerValue); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1110 | break; |
| 1111 | |
| 1112 | case kReal_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1113 | str.appendf("%f", fRealValue); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1114 | break; |
| 1115 | |
| 1116 | case kString_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1117 | str.append("\""); |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1118 | append(&str, (const char*)fStr.fBuffer, fStr.fBytes); |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1119 | str.append("\""); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1120 | break; |
| 1121 | |
| 1122 | case kHexString_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1123 | str.append("<"); |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 1124 | for (unsigned int i = 0 ; i < fStr.fBytes; i++) { |
| 1125 | str.appendf("%02x", (unsigned int)fStr.fBuffer[i]); |
| 1126 | } |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1127 | str.append(">"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1128 | break; |
| 1129 | |
| 1130 | case kName_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1131 | str.append("/"); |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1132 | append(&str, (const char*)fStr.fBuffer, fStr.fBytes, "#"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | |
| 1135 | case kKeyword_PdfObjectType: |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1136 | append(&str, (const char*)fStr.fBuffer, fStr.fBytes); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | |
| 1139 | case kArray_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1140 | str.append("[\n"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1141 | for (unsigned int i = 0; i < size(); i++) { |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1142 | str.append(objAtAIndex(i)->toString(level + 1, level + 1)); |
| 1143 | if (i < size() - 1) { |
| 1144 | str.append(","); |
| 1145 | } |
| 1146 | str.append("\n"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1147 | } |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1148 | appendSpaces(&str, level); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1149 | str.append("]"); |
| 1150 | break; |
| 1151 | |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1152 | case kDictionary_PdfObjectType: { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1153 | SkTDict<SkPdfNativeObject*>::Iter iter(*fMap); |
| 1154 | SkPdfNativeObject* obj = NULL; |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1155 | const char* key = NULL; |
| 1156 | str.append("<<\n"); |
| 1157 | while ((key = iter.next(&obj)) != NULL) { |
| 1158 | appendSpaces(&str, level + 2); |
| 1159 | str.appendf("/%s %s\n", key, obj->toString(0, level + strlen(key) + 4).c_str()); |
| 1160 | } |
| 1161 | appendSpaces(&str, level); |
| 1162 | str.append(">>"); |
| 1163 | if (hasStream()) { |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1164 | const unsigned char* stream = NULL; |
| 1165 | size_t length = 0; |
| 1166 | if (GetFilteredStreamRef(&stream, &length)) { |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 1167 | str.append("stream\n"); |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1168 | append(&str, (const char*)stream, length > 256 ? 256 : length); |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 1169 | str.append("\nendstream"); |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | str.append("stream STREAM_ERROR endstream"); |
| 1172 | } |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1173 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1174 | } |
| 1175 | break; |
| 1176 | |
| 1177 | case kNull_PdfObjectType: |
| 1178 | str = "NULL"; |
| 1179 | break; |
| 1180 | |
| 1181 | case kReference_PdfObjectType: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1182 | str.appendf("%i %i R", fRef.fId, fRef.fGen); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | |
| 1185 | case kUndefined_PdfObjectType: |
| 1186 | str = "Undefined"; |
| 1187 | break; |
| 1188 | |
| 1189 | default: |
edisonn@google.com | 9a43c18 | 2013-08-01 20:06:42 +0000 | [diff] [blame] | 1190 | str = "Error"; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | return str; |
| 1195 | } |
| 1196 | |
| 1197 | private: |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 1198 | static void makeStringCore(const unsigned char* start, SkPdfNativeObject* obj, ObjectType type GET_TRACK_PARAMETERS) { |
| 1199 | makeStringCore(start, strlen((const char*)start), obj, type PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 1202 | static void makeStringCore(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* obj, ObjectType type GET_TRACK_PARAMETERS) { |
| 1203 | makeStringCore(start, end - start, obj, type PUT_TRACK_PARAMETERS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 1206 | static void makeStringCore(const unsigned char* start, size_t bytes, SkPdfNativeObject* obj, ObjectType type GET_TRACK_PARAMETERS) { |
| 1207 | STORE_TRACK_PARAMETERS(obj); |
| 1208 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1209 | SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
| 1210 | |
| 1211 | obj->fObjectType = type; |
| 1212 | obj->fStr.fBuffer = start; |
| 1213 | obj->fStr.fBytes = bytes; |
| 1214 | } |
| 1215 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1216 | bool applyFilter(const char* name); |
| 1217 | bool applyFlateDecodeFilter(); |
| 1218 | bool applyDCTDecodeFilter(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1219 | }; |
| 1220 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1221 | class SkPdfStream : public SkPdfNativeObject {}; |
| 1222 | class SkPdfArray : public SkPdfNativeObject {}; |
| 1223 | class SkPdfString : public SkPdfNativeObject {}; |
| 1224 | class SkPdfHexString : public SkPdfNativeObject {}; |
| 1225 | class SkPdfInteger : public SkPdfNativeObject {}; |
| 1226 | class SkPdfReal : public SkPdfNativeObject {}; |
| 1227 | class SkPdfNumber : public SkPdfNativeObject {}; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1228 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1229 | class SkPdfName : public SkPdfNativeObject { |
| 1230 | SkPdfName() : SkPdfNativeObject() { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 1231 | SkPdfNativeObject::makeName((const unsigned char*)"", this PUT_TRACK_PARAMETERS_SRC); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1232 | } |
| 1233 | public: |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1234 | SkPdfName(char* name) : SkPdfNativeObject() { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame] | 1235 | this->makeName((const unsigned char*)name, this PUT_TRACK_PARAMETERS_SRC); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1236 | } |
| 1237 | }; |
| 1238 | |
edisonn@google.com | cf2cfa1 | 2013-08-21 16:31:37 +0000 | [diff] [blame] | 1239 | #endif // SkPdfNativeObject |