OLD | NEW |
1 #ifndef EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ | 1 #ifndef EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ |
2 #define EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ | 2 #define EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ |
3 | 3 |
4 #include <stdint.h> | 4 #include <stdint.h> |
5 #include <string.h> | 5 #include <string.h> |
6 #include <string> | 6 #include <string> |
7 #include "SkTDArray.h" | 7 #include "SkTDArray.h" |
8 #include "SkTDict.h" | 8 #include "SkTDict.h" |
9 #include "SkRect.h" | 9 #include "SkRect.h" |
10 #include "SkMatrix.h" | 10 #include "SkMatrix.h" |
11 #include "SkString.h" | 11 #include "SkString.h" |
12 | 12 |
13 #include "SkPdfNYI.h" | 13 #include "SkPdfNYI.h" |
14 #include "SkPdfConfig.h" | 14 #include "SkPdfConfig.h" |
| 15 #include "SkPdfUtils.h" |
| 16 |
| 17 #include "SkPdfNativeTokenizer.h" |
15 | 18 |
16 class SkPdfDictionary; | 19 class SkPdfDictionary; |
17 class SkPdfStream; | 20 class SkPdfStream; |
18 class SkPdfAllocator; | 21 class SkPdfAllocator; |
19 | 22 |
20 // TODO(edisonn): macro it and move it to utils | 23 // TODO(edisonn): macro it and move it to utils |
21 SkMatrix SkMatrixFromPdfMatrix(double array[6]); | 24 SkMatrix SkMatrixFromPdfMatrix(double array[6]); |
22 | 25 |
23 | 26 |
24 #define kFilteredStreamBit 0 | 27 #define kFilteredStreamBit 0 |
25 #define kUnfilteredStreamBit 1 | 28 #define kUnfilteredStreamBit 1 |
26 #define kOwnedStreamBit 2 | 29 #define kOwnedStreamBit 2 |
27 | 30 |
28 class SkPdfObject { | 31 class SkPdfNativeObject { |
29 public: | 32 public: |
30 enum ObjectType { | 33 enum ObjectType { |
31 kInvalid_PdfObjectType, | 34 kInvalid_PdfObjectType, |
32 | 35 |
33 kBoolean_PdfObjectType, | 36 kBoolean_PdfObjectType, |
34 kInteger_PdfObjectType, | 37 kInteger_PdfObjectType, |
35 kReal_PdfObjectType, | 38 kReal_PdfObjectType, |
36 kString_PdfObjectType, | 39 kString_PdfObjectType, |
37 kHexString_PdfObjectType, | 40 kHexString_PdfObjectType, |
38 kName_PdfObjectType, | 41 kName_PdfObjectType, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 ObjectType fObjectType; | 74 ObjectType fObjectType; |
72 | 75 |
73 union { | 76 union { |
74 bool fBooleanValue; | 77 bool fBooleanValue; |
75 int64_t fIntegerValue; | 78 int64_t fIntegerValue; |
76 // TODO(edisonn): double, float? typedefed | 79 // TODO(edisonn): double, float? typedefed |
77 double fRealValue; | 80 double fRealValue; |
78 NotOwnedString fStr; | 81 NotOwnedString fStr; |
79 | 82 |
80 // TODO(edisonn): make sure the foorprint of fArray and fMap is small, o
therwise, use pointers, or classes with up to 8 bytes in footprint | 83 // TODO(edisonn): make sure the foorprint of fArray and fMap is small, o
therwise, use pointers, or classes with up to 8 bytes in footprint |
81 SkTDArray<SkPdfObject*>* fArray; | 84 SkTDArray<SkPdfNativeObject*>* fArray; |
82 Reference fRef; | 85 Reference fRef; |
83 }; | 86 }; |
84 SkTDict<SkPdfObject*>* fMap; | 87 SkTDict<SkPdfNativeObject*>* fMap; |
85 | 88 |
86 // TODO(edisonn): rename data with cache | 89 // TODO(edisonn): rename data with cache |
87 void* fData; | 90 void* fData; |
88 DataType fDataType; | 91 DataType fDataType; |
89 | 92 |
90 | 93 |
91 public: | 94 public: |
92 | 95 |
93 SkPdfObject() : fObjectType(kInvalid_PdfObjectType), fMap(NULL), fData(NULL)
, fDataType(kEmpty_Data) {} | 96 SkPdfNativeObject() : fObjectType(kInvalid_PdfObjectType), fMap(NULL), fData
(NULL), fDataType(kEmpty_Data) {} |
94 | 97 |
95 | 98 |
96 inline bool hasData(DataType type) { | 99 inline bool hasData(DataType type) { |
97 return type == fDataType; | 100 return type == fDataType; |
98 } | 101 } |
99 | 102 |
100 inline void* data(DataType type) { | 103 inline void* data(DataType type) { |
101 return type == fDataType ? fData : NULL; | 104 return type == fDataType ? fData : NULL; |
102 } | 105 } |
103 | 106 |
104 inline void setData(void* data, DataType type) { | 107 inline void setData(void* data, DataType type) { |
105 releaseData(); | 108 releaseData(); |
106 fDataType = type; | 109 fDataType = type; |
107 fData = data; | 110 fData = data; |
108 } | 111 } |
109 | 112 |
110 void releaseData(); | 113 void releaseData(); |
111 | 114 |
112 // ~SkPdfObject() { | 115 // ~SkPdfNativeObject() { |
113 // //reset(); must be called manually! | 116 // //reset(); must be called manually! |
114 // } | 117 // } |
115 | 118 |
116 void reset() { | 119 void reset() { |
117 switch (fObjectType) { | 120 switch (fObjectType) { |
118 case kArray_PdfObjectType: | 121 case kArray_PdfObjectType: |
119 delete fArray; | 122 delete fArray; |
120 break; | 123 break; |
121 | 124 |
122 case kDictionary_PdfObjectType: | 125 case kDictionary_PdfObjectType: |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 static SkPdfFileSpec nyi; | 186 static SkPdfFileSpec nyi; |
184 return nyi; | 187 return nyi; |
185 } | 188 } |
186 | 189 |
187 // TODO(edisonn): NYI | 190 // TODO(edisonn): NYI |
188 SkPdfTree& treeValue() const { | 191 SkPdfTree& treeValue() const { |
189 static SkPdfTree nyi; | 192 static SkPdfTree nyi; |
190 return nyi; | 193 return nyi; |
191 } | 194 } |
192 | 195 |
193 static void makeBoolean(bool value, SkPdfObject* obj) { | 196 static void makeBoolean(bool value, SkPdfNativeObject* obj) { |
194 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 197 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
195 | 198 |
196 obj->fObjectType = kBoolean_PdfObjectType; | 199 obj->fObjectType = kBoolean_PdfObjectType; |
197 obj->fBooleanValue = value; | 200 obj->fBooleanValue = value; |
198 } | 201 } |
199 | 202 |
200 static SkPdfObject makeBoolean(bool value) { | 203 static SkPdfNativeObject makeBoolean(bool value) { |
201 SkPdfObject obj; | 204 SkPdfNativeObject obj; |
202 obj.fObjectType = kBoolean_PdfObjectType; | 205 obj.fObjectType = kBoolean_PdfObjectType; |
203 obj.fBooleanValue = value; | 206 obj.fBooleanValue = value; |
204 return obj; | 207 return obj; |
205 } | 208 } |
206 | 209 |
207 static void makeInteger(int64_t value, SkPdfObject* obj) { | 210 static void makeInteger(int64_t value, SkPdfNativeObject* obj) { |
208 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 211 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
209 | 212 |
210 obj->fObjectType = kInteger_PdfObjectType; | 213 obj->fObjectType = kInteger_PdfObjectType; |
211 obj->fIntegerValue = value; | 214 obj->fIntegerValue = value; |
212 } | 215 } |
213 | 216 |
214 static void makeReal(double value, SkPdfObject* obj) { | 217 static void makeReal(double value, SkPdfNativeObject* obj) { |
215 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 218 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
216 | 219 |
217 obj->fObjectType = kReal_PdfObjectType; | 220 obj->fObjectType = kReal_PdfObjectType; |
218 obj->fRealValue = value; | 221 obj->fRealValue = value; |
219 } | 222 } |
220 | 223 |
221 static void makeNull(SkPdfObject* obj) { | 224 static void makeNull(SkPdfNativeObject* obj) { |
222 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 225 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
223 | 226 |
224 obj->fObjectType = kNull_PdfObjectType; | 227 obj->fObjectType = kNull_PdfObjectType; |
225 } | 228 } |
226 | 229 |
227 static SkPdfObject makeNull() { | 230 static SkPdfNativeObject makeNull() { |
228 SkPdfObject obj; | 231 SkPdfNativeObject obj; |
229 obj.fObjectType = kNull_PdfObjectType; | 232 obj.fObjectType = kNull_PdfObjectType; |
230 return obj; | 233 return obj; |
231 } | 234 } |
232 | 235 |
233 static SkPdfObject kNull; | 236 static SkPdfNativeObject kNull; |
234 | 237 |
235 static void makeNumeric(const unsigned char* start, const unsigned char* end
, SkPdfObject* obj) { | 238 static void makeNumeric(const unsigned char* start, const unsigned char* end
, SkPdfNativeObject* obj) { |
236 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 239 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
237 | 240 |
238 // TODO(edisonn): NYI properly | 241 // TODO(edisonn): NYI properly |
239 // if has dot (impl), or exceeds max int, is real, otherwise is int | 242 // if has dot (impl), or exceeds max int, is real, otherwise is int |
240 bool isInt = true; | 243 bool isInt = true; |
241 for (const unsigned char* current = start; current < end; current++) { | 244 for (const unsigned char* current = start; current < end; current++) { |
242 if (*current == '.') { | 245 if (*current == '.') { |
243 isInt = false; | 246 isInt = false; |
244 break; | 247 break; |
245 } | 248 } |
246 // TODO(edisonn): report parse issue with numbers like "24asdasd123" | 249 // TODO(edisonn): report parse issue with numbers like "24asdasd123" |
247 } | 250 } |
248 if (isInt) { | 251 if (isInt) { |
249 makeInteger(atol((const char*)start), obj); | 252 makeInteger(atol((const char*)start), obj); |
250 } else { | 253 } else { |
251 makeReal(atof((const char*)start), obj); | 254 makeReal(atof((const char*)start), obj); |
252 } | 255 } |
253 } | 256 } |
254 | 257 |
255 static void makeReference(unsigned int id, unsigned int gen, SkPdfObject* ob
j) { | 258 static void makeReference(unsigned int id, unsigned int gen, SkPdfNativeObje
ct* obj) { |
256 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 259 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
257 | 260 |
258 obj->fObjectType = kReference_PdfObjectType; | 261 obj->fObjectType = kReference_PdfObjectType; |
259 obj->fRef.fId = id; | 262 obj->fRef.fId = id; |
260 obj->fRef.fGen = gen; | 263 obj->fRef.fGen = gen; |
261 } | 264 } |
262 | 265 |
263 | 266 |
264 static void makeString(const unsigned char* start, SkPdfObject* obj) { | 267 static void makeString(const unsigned char* start, SkPdfNativeObject* obj) { |
265 makeStringCore(start, strlen((const char*)start), obj, kString_PdfObject
Type); | 268 makeStringCore(start, strlen((const char*)start), obj, kString_PdfObject
Type); |
266 } | 269 } |
267 | 270 |
268 static void makeString(const unsigned char* start, const unsigned char* end,
SkPdfObject* obj) { | 271 static void makeString(const unsigned char* start, const unsigned char* end,
SkPdfNativeObject* obj) { |
269 makeStringCore(start, end - start, obj, kString_PdfObjectType); | 272 makeStringCore(start, end - start, obj, kString_PdfObjectType); |
270 } | 273 } |
271 | 274 |
272 static void makeString(const unsigned char* start, size_t bytes, SkPdfObject
* obj) { | 275 static void makeString(const unsigned char* start, size_t bytes, SkPdfNative
Object* obj) { |
273 makeStringCore(start, bytes, obj, kString_PdfObjectType); | 276 makeStringCore(start, bytes, obj, kString_PdfObjectType); |
274 } | 277 } |
275 | 278 |
276 | 279 |
277 static void makeHexString(const unsigned char* start, SkPdfObject* obj) { | 280 static void makeHexString(const unsigned char* start, SkPdfNativeObject* obj
) { |
278 makeStringCore(start, strlen((const char*)start), obj, kHexString_PdfObj
ectType); | 281 makeStringCore(start, strlen((const char*)start), obj, kHexString_PdfObj
ectType); |
279 } | 282 } |
280 | 283 |
281 static void makeHexString(const unsigned char* start, const unsigned char* e
nd, SkPdfObject* obj) { | 284 static void makeHexString(const unsigned char* start, const unsigned char* e
nd, SkPdfNativeObject* obj) { |
282 makeStringCore(start, end - start, obj, kHexString_PdfObjectType); | 285 makeStringCore(start, end - start, obj, kHexString_PdfObjectType); |
283 } | 286 } |
284 | 287 |
285 static void makeHexString(const unsigned char* start, size_t bytes, SkPdfObj
ect* obj) { | 288 static void makeHexString(const unsigned char* start, size_t bytes, SkPdfNat
iveObject* obj) { |
286 makeStringCore(start, bytes, obj, kHexString_PdfObjectType); | 289 makeStringCore(start, bytes, obj, kHexString_PdfObjectType); |
287 } | 290 } |
288 | 291 |
289 | 292 |
290 static void makeName(const unsigned char* start, SkPdfObject* obj) { | 293 static void makeName(const unsigned char* start, SkPdfNativeObject* obj) { |
291 makeStringCore(start, strlen((const char*)start), obj, kName_PdfObjectTy
pe); | 294 makeStringCore(start, strlen((const char*)start), obj, kName_PdfObjectTy
pe); |
292 } | 295 } |
293 | 296 |
294 static void makeName(const unsigned char* start, const unsigned char* end, S
kPdfObject* obj) { | 297 static void makeName(const unsigned char* start, const unsigned char* end, S
kPdfNativeObject* obj) { |
295 makeStringCore(start, end - start, obj, kName_PdfObjectType); | 298 makeStringCore(start, end - start, obj, kName_PdfObjectType); |
296 } | 299 } |
297 | 300 |
298 static void makeName(const unsigned char* start, size_t bytes, SkPdfObject*
obj) { | 301 static void makeName(const unsigned char* start, size_t bytes, SkPdfNativeOb
ject* obj) { |
299 makeStringCore(start, bytes, obj, kName_PdfObjectType); | 302 makeStringCore(start, bytes, obj, kName_PdfObjectType); |
300 } | 303 } |
301 | 304 |
302 | 305 |
303 static void makeKeyword(const unsigned char* start, SkPdfObject* obj) { | 306 static void makeKeyword(const unsigned char* start, SkPdfNativeObject* obj)
{ |
304 makeStringCore(start, strlen((const char*)start), obj, kKeyword_PdfObjec
tType); | 307 makeStringCore(start, strlen((const char*)start), obj, kKeyword_PdfObjec
tType); |
305 } | 308 } |
306 | 309 |
307 static void makeKeyword(const unsigned char* start, const unsigned char* end
, SkPdfObject* obj) { | 310 static void makeKeyword(const unsigned char* start, const unsigned char* end
, SkPdfNativeObject* obj) { |
308 makeStringCore(start, end - start, obj, kKeyword_PdfObjectType); | 311 makeStringCore(start, end - start, obj, kKeyword_PdfObjectType); |
309 } | 312 } |
310 | 313 |
311 static void makeKeyword(const unsigned char* start, size_t bytes, SkPdfObjec
t* obj) { | 314 static void makeKeyword(const unsigned char* start, size_t bytes, SkPdfNativ
eObject* obj) { |
312 makeStringCore(start, bytes, obj, kKeyword_PdfObjectType); | 315 makeStringCore(start, bytes, obj, kKeyword_PdfObjectType); |
313 } | 316 } |
314 | 317 |
315 | 318 |
316 | 319 |
317 // TODO(edisonn): make the functions to return SkPdfArray, move these functi
ons in SkPdfArray | 320 // TODO(edisonn): make the functions to return SkPdfArray, move these functi
ons in SkPdfArray |
318 static void makeEmptyArray(SkPdfObject* obj) { | 321 static void makeEmptyArray(SkPdfNativeObject* obj) { |
319 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 322 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
320 | 323 |
321 obj->fObjectType = kArray_PdfObjectType; | 324 obj->fObjectType = kArray_PdfObjectType; |
322 obj->fArray = new SkTDArray<SkPdfObject*>(); | 325 obj->fArray = new SkTDArray<SkPdfNativeObject*>(); |
323 // return (SkPdfArray*)obj; | 326 // return (SkPdfArray*)obj; |
324 } | 327 } |
325 | 328 |
326 bool appendInArray(SkPdfObject* obj) { | 329 bool appendInArray(SkPdfNativeObject* obj) { |
327 SkASSERT(fObjectType == kArray_PdfObjectType); | 330 SkASSERT(fObjectType == kArray_PdfObjectType); |
328 if (fObjectType != kArray_PdfObjectType) { | 331 if (fObjectType != kArray_PdfObjectType) { |
329 // TODO(edisonn): report err | 332 // TODO(edisonn): report err |
330 return false; | 333 return false; |
331 } | 334 } |
332 | 335 |
333 fArray->push(obj); | 336 fArray->push(obj); |
334 return true; | 337 return true; |
335 } | 338 } |
336 | 339 |
337 size_t size() const { | 340 size_t size() const { |
338 SkASSERT(fObjectType == kArray_PdfObjectType); | 341 SkASSERT(fObjectType == kArray_PdfObjectType); |
339 | 342 |
340 return fArray->count(); | 343 return fArray->count(); |
341 } | 344 } |
342 | 345 |
343 SkPdfObject* objAtAIndex(int i) { | 346 SkPdfNativeObject* objAtAIndex(int i) { |
344 SkASSERT(fObjectType == kArray_PdfObjectType); | 347 SkASSERT(fObjectType == kArray_PdfObjectType); |
345 | 348 |
346 return (*fArray)[i]; | 349 return (*fArray)[i]; |
347 } | 350 } |
348 | 351 |
349 SkPdfObject* removeLastInArray() { | 352 SkPdfNativeObject* removeLastInArray() { |
350 SkASSERT(fObjectType == kArray_PdfObjectType); | 353 SkASSERT(fObjectType == kArray_PdfObjectType); |
351 | 354 |
352 SkPdfObject* ret = NULL; | 355 SkPdfNativeObject* ret = NULL; |
353 fArray->pop(&ret); | 356 fArray->pop(&ret); |
354 | 357 |
355 return ret; | 358 return ret; |
356 } | 359 } |
357 | 360 |
358 | 361 |
359 const SkPdfObject* objAtAIndex(int i) const { | 362 const SkPdfNativeObject* objAtAIndex(int i) const { |
360 SkASSERT(fObjectType == kArray_PdfObjectType); | 363 SkASSERT(fObjectType == kArray_PdfObjectType); |
361 | 364 |
362 return (*fArray)[i]; | 365 return (*fArray)[i]; |
363 } | 366 } |
364 | 367 |
365 SkPdfObject* operator[](int i) { | 368 SkPdfNativeObject* operator[](int i) { |
366 SkASSERT(fObjectType == kArray_PdfObjectType); | 369 SkASSERT(fObjectType == kArray_PdfObjectType); |
367 | 370 |
368 return (*fArray)[i]; | 371 return (*fArray)[i]; |
369 } | 372 } |
370 | 373 |
371 const SkPdfObject* operator[](int i) const { | 374 const SkPdfNativeObject* operator[](int i) const { |
372 SkASSERT(fObjectType == kArray_PdfObjectType); | 375 SkASSERT(fObjectType == kArray_PdfObjectType); |
373 | 376 |
374 return (*fArray)[i]; | 377 return (*fArray)[i]; |
375 } | 378 } |
376 | 379 |
377 | 380 |
378 // TODO(edisonn): make the functions to return SkPdfDictionary, move these f
unctions in SkPdfDictionary | 381 // TODO(edisonn): make the functions to return SkPdfDictionary, move these f
unctions in SkPdfDictionary |
379 static void makeEmptyDictionary(SkPdfObject* obj) { | 382 static void makeEmptyDictionary(SkPdfNativeObject* obj) { |
380 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 383 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
381 | 384 |
382 obj->fObjectType = kDictionary_PdfObjectType; | 385 obj->fObjectType = kDictionary_PdfObjectType; |
383 obj->fMap = new SkTDict<SkPdfObject*>(1); | 386 obj->fMap = new SkTDict<SkPdfNativeObject*>(1); |
384 obj->fStr.fBuffer = NULL; | 387 obj->fStr.fBuffer = NULL; |
385 obj->fStr.fBytes = 0; | 388 obj->fStr.fBytes = 0; |
386 } | 389 } |
387 | 390 |
388 // TODO(edisonn): get all the possible names from spec, and compute a hash f
unction | 391 // TODO(edisonn): get all the possible names from spec, and compute a hash f
unction |
389 // that would create no overlaps in the same dictionary | 392 // that would create no overlaps in the same dictionary |
390 // or build a tree of chars that when followed goes to a unique id/index/has
h | 393 // or build a tree of chars that when followed goes to a unique id/index/has
h |
391 // TODO(edisonn): generate constants like kDictFoo, kNameDict_name | 394 // TODO(edisonn): generate constants like kDictFoo, kNameDict_name |
392 // which will be used in code | 395 // which will be used in code |
393 // add function SkPdfFastNameKey key(const char* key); | 396 // add function SkPdfFastNameKey key(const char* key); |
394 // TODO(edisonn): setting the same key twike, will make the value undefined! | 397 // TODO(edisonn): setting the same key twike, will make the value undefined! |
395 bool set(const SkPdfObject* key, SkPdfObject* value) { | 398 bool set(const SkPdfNativeObject* key, SkPdfNativeObject* value) { |
396 SkASSERT(fObjectType == kDictionary_PdfObjectType); | 399 SkASSERT(fObjectType == kDictionary_PdfObjectType); |
397 SkASSERT(key->fObjectType == kName_PdfObjectType); | 400 SkASSERT(key->fObjectType == kName_PdfObjectType); |
398 | 401 |
399 if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionar
y_PdfObjectType) { | 402 if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionar
y_PdfObjectType) { |
400 // TODO(edisonn): report err | 403 // TODO(edisonn): report err |
401 return false; | 404 return false; |
402 } | 405 } |
403 | 406 |
404 //// we rewrite all delimiters and white spaces with '\0', so we expect
the end of name to be '\0' | 407 //// we rewrite all delimiters and white spaces with '\0', so we expect
the end of name to be '\0' |
405 //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); | 408 //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); |
406 | 409 |
407 return set(key->fStr.fBuffer, key->fStr.fBytes, value); | 410 return set(key->fStr.fBuffer, key->fStr.fBytes, value); |
408 } | 411 } |
409 | 412 |
410 bool set(const char* key, SkPdfObject* value) { | 413 bool set(const char* key, SkPdfNativeObject* value) { |
411 return set((const unsigned char*)key, strlen(key), value); | 414 return set((const unsigned char*)key, strlen(key), value); |
412 } | 415 } |
413 | 416 |
414 bool set(const unsigned char* key, size_t len, SkPdfObject* value) { | 417 bool set(const unsigned char* key, size_t len, SkPdfNativeObject* value) { |
415 SkASSERT(fObjectType == kDictionary_PdfObjectType); | 418 SkASSERT(fObjectType == kDictionary_PdfObjectType); |
416 | 419 |
417 if (fObjectType != kDictionary_PdfObjectType) { | 420 if (fObjectType != kDictionary_PdfObjectType) { |
418 // TODO(edisonn): report err | 421 // TODO(edisonn): report err |
419 return false; | 422 return false; |
420 } | 423 } |
421 | 424 |
422 return fMap->set((const char*)key, len, value); | 425 return fMap->set((const char*)key, len, value); |
423 } | 426 } |
424 | 427 |
425 SkPdfObject* get(const SkPdfObject* key) { | 428 SkPdfNativeObject* get(const SkPdfNativeObject* key) { |
426 SkASSERT(fObjectType == kDictionary_PdfObjectType); | 429 SkASSERT(fObjectType == kDictionary_PdfObjectType); |
427 SkASSERT(key->fObjectType == kName_PdfObjectType); | 430 SkASSERT(key->fObjectType == kName_PdfObjectType); |
428 | 431 |
429 if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionar
y_PdfObjectType) { | 432 if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionar
y_PdfObjectType) { |
430 // TODO(edisonn): report err | 433 // TODO(edisonn): report err |
431 return NULL; | 434 return NULL; |
432 } | 435 } |
433 | 436 |
434 //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); | 437 //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); |
435 | 438 |
436 return get(key->fStr.fBuffer, key->fStr.fBytes); | 439 return get(key->fStr.fBuffer, key->fStr.fBytes); |
437 } | 440 } |
438 | 441 |
439 SkPdfObject* get(const char* key) { | 442 SkPdfNativeObject* get(const char* key) { |
440 return get((const unsigned char*)key, strlen(key)); | 443 return get((const unsigned char*)key, strlen(key)); |
441 } | 444 } |
442 | 445 |
443 SkPdfObject* get(const unsigned char* key, size_t len) { | 446 SkPdfNativeObject* get(const unsigned char* key, size_t len) { |
444 SkASSERT(fObjectType == kDictionary_PdfObjectType); | 447 SkASSERT(fObjectType == kDictionary_PdfObjectType); |
445 SkASSERT(key); | 448 SkASSERT(key); |
446 if (fObjectType != kDictionary_PdfObjectType) { | 449 if (fObjectType != kDictionary_PdfObjectType) { |
447 // TODO(edisonn): report err | 450 // TODO(edisonn): report err |
448 return NULL; | 451 return NULL; |
449 } | 452 } |
450 SkPdfObject* ret = NULL; | 453 SkPdfNativeObject* ret = NULL; |
451 fMap->find((const char*)key, len, &ret); | 454 fMap->find((const char*)key, len, &ret); |
452 | 455 |
453 #ifdef PDF_TRACE | 456 #ifdef PDF_TRACE |
454 SkString _key; | 457 SkString _key; |
455 _key.append((const char*)key, len); | 458 _key.append((const char*)key, len); |
456 printf("\nget(/%s) = %s\n", _key.c_str(), ret ? ret->toString(0, len + 9
).c_str() : "_NOT_FOUND"); | 459 printf("\nget(/%s) = %s\n", _key.c_str(), ret ? ret->toString(0, len + 9
).c_str() : "_NOT_FOUND"); |
457 #endif | 460 #endif |
458 | 461 |
459 return ret; | 462 return ret; |
460 } | 463 } |
461 | 464 |
462 const SkPdfObject* get(const SkPdfObject* key) const { | 465 const SkPdfNativeObject* get(const SkPdfNativeObject* key) const { |
463 SkASSERT(fObjectType == kDictionary_PdfObjectType); | 466 SkASSERT(fObjectType == kDictionary_PdfObjectType); |
464 SkASSERT(key->fObjectType == kName_PdfObjectType); | 467 SkASSERT(key->fObjectType == kName_PdfObjectType); |
465 | 468 |
466 if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionar
y_PdfObjectType) { | 469 if (key->fObjectType != kName_PdfObjectType || fObjectType != kDictionar
y_PdfObjectType) { |
467 // TODO(edisonn): report err | 470 // TODO(edisonn): report err |
468 return NULL; | 471 return NULL; |
469 } | 472 } |
470 | 473 |
471 //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); | 474 //SkASSERT(key->fStr.fBuffer[key->fStr.fBytes] == '\0'); |
472 | 475 |
473 return get(key->fStr.fBuffer, key->fStr.fBytes); | 476 return get(key->fStr.fBuffer, key->fStr.fBytes); |
474 } | 477 } |
475 | 478 |
476 const SkPdfObject* get(const char* key) const { | 479 const SkPdfNativeObject* get(const char* key) const { |
477 return get((const unsigned char*)key, strlen(key)); | 480 return get((const unsigned char*)key, strlen(key)); |
478 } | 481 } |
479 | 482 |
480 const SkPdfObject* get(const unsigned char* key, size_t len) const { | 483 const SkPdfNativeObject* get(const unsigned char* key, size_t len) const { |
481 SkASSERT(fObjectType == kDictionary_PdfObjectType); | 484 SkASSERT(fObjectType == kDictionary_PdfObjectType); |
482 SkASSERT(key); | 485 SkASSERT(key); |
483 if (fObjectType != kDictionary_PdfObjectType) { | 486 if (fObjectType != kDictionary_PdfObjectType) { |
484 // TODO(edisonn): report err | 487 // TODO(edisonn): report err |
485 return NULL; | 488 return NULL; |
486 } | 489 } |
487 SkPdfObject* ret = NULL; | 490 SkPdfNativeObject* ret = NULL; |
488 fMap->find((const char*)key, len, &ret); | 491 fMap->find((const char*)key, len, &ret); |
489 | 492 |
490 #ifdef PDF_TRACE | 493 #ifdef PDF_TRACE |
491 SkString _key; | 494 SkString _key; |
492 _key.append((const char*)key, len); | 495 _key.append((const char*)key, len); |
493 printf("\nget(/%s) = %s\n", _key.c_str(), ret ? ret->toString(0, len + 9
).c_str() : "_NOT_FOUND"); | 496 printf("\nget(/%s) = %s\n", _key.c_str(), ret ? ret->toString(0, len + 9
).c_str() : "_NOT_FOUND"); |
494 #endif | 497 #endif |
495 | 498 |
496 return ret; | 499 return ret; |
497 } | 500 } |
498 | 501 |
499 const SkPdfObject* get(const char* key, const char* abr) const { | 502 const SkPdfNativeObject* get(const char* key, const char* abr) const { |
500 const SkPdfObject* ret = get(key); | 503 const SkPdfNativeObject* ret = get(key); |
501 // TODO(edisonn): / is a valid name, and it might be an abreviation, so
"" should not be like NULL | 504 // TODO(edisonn): / is a valid name, and it might be an abreviation, so
"" should not be like NULL |
502 // make this distiontion in generator, and remove "" from condition | 505 // make this distiontion in generator, and remove "" from condition |
503 if (ret != NULL || abr == NULL || *abr == '\0') { | 506 if (ret != NULL || abr == NULL || *abr == '\0') { |
504 return ret; | 507 return ret; |
505 } | 508 } |
506 return get(abr); | 509 return get(abr); |
507 } | 510 } |
508 | 511 |
509 SkPdfObject* get(const char* key, const char* abr) { | 512 SkPdfNativeObject* get(const char* key, const char* abr) { |
510 SkPdfObject* ret = get(key); | 513 SkPdfNativeObject* ret = get(key); |
511 // TODO(edisonn): / is a valid name, and it might be an abreviation, so
"" should not be like NULL | 514 // TODO(edisonn): / is a valid name, and it might be an abreviation, so
"" should not be like NULL |
512 // make this distiontion in generator, and remove "" from condition | 515 // make this distiontion in generator, and remove "" from condition |
513 if (ret != NULL || abr == NULL || *abr == '\0') { | 516 if (ret != NULL || abr == NULL || *abr == '\0') { |
514 return ret; | 517 return ret; |
515 } | 518 } |
516 return get(abr); | 519 return get(abr); |
517 } | 520 } |
518 | 521 |
519 SkPdfDictionary* asDictionary() { | 522 SkPdfDictionary* asDictionary() { |
520 SkASSERT(isDictionary()); | 523 SkASSERT(isDictionary()); |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 | 754 |
752 SkRect rectangleValue() const { | 755 SkRect rectangleValue() const { |
753 SkASSERT(isRectangle()); | 756 SkASSERT(isRectangle()); |
754 if (!isRectangle()) { | 757 if (!isRectangle()) { |
755 return SkRect::MakeEmpty(); | 758 return SkRect::MakeEmpty(); |
756 } | 759 } |
757 | 760 |
758 double array[4]; | 761 double array[4]; |
759 for (int i = 0; i < 4; i++) { | 762 for (int i = 0; i < 4; i++) { |
760 // TODO(edisonn): version where we could resolve references? | 763 // TODO(edisonn): version where we could resolve references? |
761 const SkPdfObject* elem = objAtAIndex(i); | 764 const SkPdfNativeObject* elem = objAtAIndex(i); |
762 if (elem == NULL || !elem->isNumber()) { | 765 if (elem == NULL || !elem->isNumber()) { |
763 // TODO(edisonn): report error | 766 // TODO(edisonn): report error |
764 return SkRect::MakeEmpty(); | 767 return SkRect::MakeEmpty(); |
765 } | 768 } |
766 array[i] = elem->numberValue(); | 769 array[i] = elem->numberValue(); |
767 } | 770 } |
768 | 771 |
769 return SkRect::MakeLTRB(SkDoubleToScalar(array[0]), | 772 return SkRect::MakeLTRB(SkDoubleToScalar(array[0]), |
770 SkDoubleToScalar(array[1]), | 773 SkDoubleToScalar(array[1]), |
771 SkDoubleToScalar(array[2]), | 774 SkDoubleToScalar(array[2]), |
772 SkDoubleToScalar(array[3])); | 775 SkDoubleToScalar(array[3])); |
773 } | 776 } |
774 | 777 |
775 SkMatrix matrixValue() const { | 778 SkMatrix matrixValue() const { |
776 SkASSERT(isMatrix()); | 779 SkASSERT(isMatrix()); |
777 if (!isMatrix()) { | 780 if (!isMatrix()) { |
778 return SkMatrix::I(); | 781 return SkMatrix::I(); |
779 } | 782 } |
780 | 783 |
781 double array[6]; | 784 double array[6]; |
782 for (int i = 0; i < 6; i++) { | 785 for (int i = 0; i < 6; i++) { |
783 // TODO(edisonn): version where we could resolve references? | 786 // TODO(edisonn): version where we could resolve references? |
784 const SkPdfObject* elem = objAtAIndex(i); | 787 const SkPdfNativeObject* elem = objAtAIndex(i); |
785 if (elem == NULL || !elem->isNumber()) { | 788 if (elem == NULL || !elem->isNumber()) { |
786 // TODO(edisonn): report error | 789 // TODO(edisonn): report error |
787 return SkMatrix::I(); | 790 return SkMatrix::I(); |
788 } | 791 } |
789 array[i] = elem->numberValue(); | 792 array[i] = elem->numberValue(); |
790 } | 793 } |
791 | 794 |
792 return SkMatrixFromPdfMatrix(array); | 795 return SkMatrixFromPdfMatrix(array); |
793 } | 796 } |
794 | 797 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 | 858 |
856 return true; | 859 return true; |
857 } | 860 } |
858 | 861 |
859 static void appendSpaces(SkString* str, int level) { | 862 static void appendSpaces(SkString* str, int level) { |
860 for (int i = 0 ; i < level; i++) { | 863 for (int i = 0 ; i < level; i++) { |
861 str->append(" "); | 864 str->append(" "); |
862 } | 865 } |
863 } | 866 } |
864 | 867 |
| 868 static void append(SkString* str, const char* data, size_t len, const char*
prefix = "\\x") { |
| 869 for (unsigned int i = 0 ; i < len; i++) { |
| 870 if (data[i] == kNUL_PdfWhiteSpace) { |
| 871 str->append(prefix); |
| 872 str->append("00"); |
| 873 } else if (data[i] == kHT_PdfWhiteSpace) { |
| 874 str->append(prefix); |
| 875 str->append("09"); |
| 876 } else if (data[i] == kLF_PdfWhiteSpace) { |
| 877 str->append(prefix); |
| 878 str->append("0A"); |
| 879 } else if (data[i] == kFF_PdfWhiteSpace) { |
| 880 str->append(prefix); |
| 881 str->append("0C"); |
| 882 } else if (data[i] == kCR_PdfWhiteSpace) { |
| 883 str->append(prefix); |
| 884 str->append("0D"); |
| 885 } else { |
| 886 str->append(data + i, 1); |
| 887 } |
| 888 } |
| 889 } |
| 890 |
865 SkString toString(int firstRowLevel = 0, int level = 0) { | 891 SkString toString(int firstRowLevel = 0, int level = 0) { |
866 SkString str; | 892 SkString str; |
867 appendSpaces(&str, firstRowLevel); | 893 appendSpaces(&str, firstRowLevel); |
868 switch (fObjectType) { | 894 switch (fObjectType) { |
869 case kInvalid_PdfObjectType: | 895 case kInvalid_PdfObjectType: |
870 str.append("__Invalid"); | 896 str.append("__Invalid"); |
871 break; | 897 break; |
872 | 898 |
873 case kBoolean_PdfObjectType: | 899 case kBoolean_PdfObjectType: |
874 str.appendf("%s", fBooleanValue ? "true" : "false"); | 900 str.appendf("%s", fBooleanValue ? "true" : "false"); |
875 break; | 901 break; |
876 | 902 |
877 case kInteger_PdfObjectType: | 903 case kInteger_PdfObjectType: |
878 str.appendf("%i", (int)fIntegerValue); | 904 str.appendf("%i", (int)fIntegerValue); |
879 break; | 905 break; |
880 | 906 |
881 case kReal_PdfObjectType: | 907 case kReal_PdfObjectType: |
882 str.appendf("%f", fRealValue); | 908 str.appendf("%f", fRealValue); |
883 break; | 909 break; |
884 | 910 |
885 case kString_PdfObjectType: | 911 case kString_PdfObjectType: |
886 str.append("\""); | 912 str.append("\""); |
887 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 913 append(&str, (const char*)fStr.fBuffer, fStr.fBytes); |
888 str.append("\""); | 914 str.append("\""); |
889 break; | 915 break; |
890 | 916 |
891 case kHexString_PdfObjectType: | 917 case kHexString_PdfObjectType: |
892 str.append("<"); | 918 str.append("<"); |
893 for (unsigned int i = 0 ; i < fStr.fBytes; i++) { | 919 for (unsigned int i = 0 ; i < fStr.fBytes; i++) { |
894 str.appendf("%02x", (unsigned int)fStr.fBuffer[i]); | 920 str.appendf("%02x", (unsigned int)fStr.fBuffer[i]); |
895 } | 921 } |
896 str.append(">"); | 922 str.append(">"); |
897 break; | 923 break; |
898 | 924 |
899 case kName_PdfObjectType: | 925 case kName_PdfObjectType: |
900 str.append("/"); | 926 str.append("/"); |
901 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 927 append(&str, (const char*)fStr.fBuffer, fStr.fBytes, "#"); |
902 break; | 928 break; |
903 | 929 |
904 case kKeyword_PdfObjectType: | 930 case kKeyword_PdfObjectType: |
905 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 931 append(&str, (const char*)fStr.fBuffer, fStr.fBytes); |
906 break; | 932 break; |
907 | 933 |
908 case kArray_PdfObjectType: | 934 case kArray_PdfObjectType: |
909 str.append("[\n"); | 935 str.append("[\n"); |
910 for (unsigned int i = 0; i < size(); i++) { | 936 for (unsigned int i = 0; i < size(); i++) { |
911 str.append(objAtAIndex(i)->toString(level + 1, level + 1)); | 937 str.append(objAtAIndex(i)->toString(level + 1, level + 1)); |
912 if (i < size() - 1) { | 938 if (i < size() - 1) { |
913 str.append(","); | 939 str.append(","); |
914 } | 940 } |
915 str.append("\n"); | 941 str.append("\n"); |
916 } | 942 } |
917 appendSpaces(&str, level); | 943 appendSpaces(&str, level); |
918 str.append("]"); | 944 str.append("]"); |
919 break; | 945 break; |
920 | 946 |
921 case kDictionary_PdfObjectType: { | 947 case kDictionary_PdfObjectType: { |
922 SkTDict<SkPdfObject*>::Iter iter(*fMap); | 948 SkTDict<SkPdfNativeObject*>::Iter iter(*fMap); |
923 SkPdfObject* obj = NULL; | 949 SkPdfNativeObject* obj = NULL; |
924 const char* key = NULL; | 950 const char* key = NULL; |
925 str.append("<<\n"); | 951 str.append("<<\n"); |
926 while ((key = iter.next(&obj)) != NULL) { | 952 while ((key = iter.next(&obj)) != NULL) { |
927 appendSpaces(&str, level + 2); | 953 appendSpaces(&str, level + 2); |
928 str.appendf("/%s %s\n", key, obj->toString(0, level + st
rlen(key) + 4).c_str()); | 954 str.appendf("/%s %s\n", key, obj->toString(0, level + st
rlen(key) + 4).c_str()); |
929 } | 955 } |
930 appendSpaces(&str, level); | 956 appendSpaces(&str, level); |
931 str.append(">>"); | 957 str.append(">>"); |
932 if (hasStream()) { | 958 if (hasStream()) { |
933 const unsigned char* stream = NULL; | 959 const unsigned char* stream = NULL; |
934 size_t length = 0; | 960 size_t length = 0; |
935 if (GetFilteredStreamRef(&stream, &length)) { | 961 if (GetFilteredStreamRef(&stream, &length)) { |
936 str.append("stream\n"); | 962 str.append("stream\n"); |
937 str.append((const char*)stream, length > 256 ? 256 :
length); | 963 append(&str, (const char*)stream, length > 256 ? 256
: length); |
938 str.append("\nendstream"); | 964 str.append("\nendstream"); |
939 } else { | 965 } else { |
940 str.append("stream STREAM_ERROR endstream"); | 966 str.append("stream STREAM_ERROR endstream"); |
941 } | 967 } |
942 } | 968 } |
943 } | 969 } |
944 break; | 970 break; |
945 | 971 |
946 case kNull_PdfObjectType: | 972 case kNull_PdfObjectType: |
947 str = "NULL"; | 973 str = "NULL"; |
948 break; | 974 break; |
949 | 975 |
950 case kReference_PdfObjectType: | 976 case kReference_PdfObjectType: |
951 str.appendf("%i %i R", fRef.fId, fRef.fGen); | 977 str.appendf("%i %i R", fRef.fId, fRef.fGen); |
952 break; | 978 break; |
953 | 979 |
954 case kUndefined_PdfObjectType: | 980 case kUndefined_PdfObjectType: |
955 str = "Undefined"; | 981 str = "Undefined"; |
956 break; | 982 break; |
957 | 983 |
958 default: | 984 default: |
959 str = "Error"; | 985 str = "Error"; |
960 break; | 986 break; |
961 } | 987 } |
962 | 988 |
963 return str; | 989 return str; |
964 } | 990 } |
965 | 991 |
966 private: | 992 private: |
967 static void makeStringCore(const unsigned char* start, SkPdfObject* obj, Obj
ectType type) { | 993 static void makeStringCore(const unsigned char* start, SkPdfNativeObject* ob
j, ObjectType type) { |
968 makeStringCore(start, strlen((const char*)start), obj, type); | 994 makeStringCore(start, strlen((const char*)start), obj, type); |
969 } | 995 } |
970 | 996 |
971 static void makeStringCore(const unsigned char* start, const unsigned char*
end, SkPdfObject* obj, ObjectType type) { | 997 static void makeStringCore(const unsigned char* start, const unsigned char*
end, SkPdfNativeObject* obj, ObjectType type) { |
972 makeStringCore(start, end - start, obj, type); | 998 makeStringCore(start, end - start, obj, type); |
973 } | 999 } |
974 | 1000 |
975 static void makeStringCore(const unsigned char* start, size_t bytes, SkPdfOb
ject* obj, ObjectType type) { | 1001 static void makeStringCore(const unsigned char* start, size_t bytes, SkPdfNa
tiveObject* obj, ObjectType type) { |
976 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); | 1002 SkASSERT(obj->fObjectType == kInvalid_PdfObjectType); |
977 | 1003 |
978 obj->fObjectType = type; | 1004 obj->fObjectType = type; |
979 obj->fStr.fBuffer = start; | 1005 obj->fStr.fBuffer = start; |
980 obj->fStr.fBytes = bytes; | 1006 obj->fStr.fBytes = bytes; |
981 } | 1007 } |
982 | 1008 |
983 bool applyFilter(const char* name); | 1009 bool applyFilter(const char* name); |
984 bool applyFlateDecodeFilter(); | 1010 bool applyFlateDecodeFilter(); |
985 bool applyDCTDecodeFilter(); | 1011 bool applyDCTDecodeFilter(); |
986 }; | 1012 }; |
987 | 1013 |
988 class SkPdfStream : public SkPdfObject {}; | 1014 class SkPdfStream : public SkPdfNativeObject {}; |
989 class SkPdfArray : public SkPdfObject {}; | 1015 class SkPdfArray : public SkPdfNativeObject {}; |
990 class SkPdfString : public SkPdfObject {}; | 1016 class SkPdfString : public SkPdfNativeObject {}; |
991 class SkPdfHexString : public SkPdfObject {}; | 1017 class SkPdfHexString : public SkPdfNativeObject {}; |
992 class SkPdfInteger : public SkPdfObject {}; | 1018 class SkPdfInteger : public SkPdfNativeObject {}; |
993 class SkPdfReal : public SkPdfObject {}; | 1019 class SkPdfReal : public SkPdfNativeObject {}; |
994 class SkPdfNumber : public SkPdfObject {}; | 1020 class SkPdfNumber : public SkPdfNativeObject {}; |
995 | 1021 |
996 class SkPdfName : public SkPdfObject { | 1022 class SkPdfName : public SkPdfNativeObject { |
997 SkPdfName() : SkPdfObject() { | 1023 SkPdfName() : SkPdfNativeObject() { |
998 SkPdfObject::makeName((const unsigned char*)"", this); | 1024 SkPdfNativeObject::makeName((const unsigned char*)"", this); |
999 } | 1025 } |
1000 public: | 1026 public: |
1001 SkPdfName(char* name) : SkPdfObject() { | 1027 SkPdfName(char* name) : SkPdfNativeObject() { |
1002 this->makeName((const unsigned char*)name, this); | 1028 this->makeName((const unsigned char*)name, this); |
1003 } | 1029 } |
1004 }; | 1030 }; |
1005 | 1031 |
1006 #endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ | 1032 #endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ |
OLD | NEW |