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