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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 kDictionary_PdfObjectType, | 42 kDictionary_PdfObjectType, |
43 kNull_PdfObjectType, | 43 kNull_PdfObjectType, |
44 | 44 |
45 // TODO(edisonn): after the pdf has been loaded completely, resolve all
references | 45 // TODO(edisonn): after the pdf has been loaded completely, resolve all
references |
46 // try the same thing with delayed loaded ... | 46 // try the same thing with delayed loaded ... |
47 kReference_PdfObjectType, | 47 kReference_PdfObjectType, |
48 | 48 |
49 kUndefined_PdfObjectType, // per 1.4 spec, if the same key appear twic
e in the dictionary, the value is undefined | 49 kUndefined_PdfObjectType, // per 1.4 spec, if the same key appear twic
e in the dictionary, the value is undefined |
50 }; | 50 }; |
51 | 51 |
| 52 enum DataType { |
| 53 kEmpty_Data, |
| 54 kFont_Data, |
| 55 kBitmap_Data, |
| 56 }; |
| 57 |
52 private: | 58 private: |
53 struct Reference { | 59 struct Reference { |
54 unsigned int fId; | 60 unsigned int fId; |
55 unsigned int fGen; | 61 unsigned int fGen; |
56 }; | 62 }; |
57 | 63 |
58 // TODO(edisonn): add stream start, stream end, where stream is weither the
file | 64 // TODO(edisonn): add stream start, stream end, where stream is weither the
file |
59 // or decoded/filtered pdf stream | 65 // or decoded/filtered pdf stream |
60 | 66 |
61 // TODO(edisonn): add warning/report per object | 67 // TODO(edisonn): add warning/report per object |
62 // TODO(edisonn): add flag fUsed, to be used once the parsing is complete, | 68 // TODO(edisonn): add flag fUsed, to be used once the parsing is complete, |
63 // so we could show what parts have been proccessed, ignored, or generated e
rrors | 69 // so we could show what parts have been proccessed, ignored, or generated e
rrors |
64 | 70 |
65 ObjectType fObjectType; | 71 ObjectType fObjectType; |
66 | 72 |
67 union { | 73 union { |
68 bool fBooleanValue; | 74 bool fBooleanValue; |
69 int64_t fIntegerValue; | 75 int64_t fIntegerValue; |
70 // TODO(edisonn): double, float? typedefed | 76 // TODO(edisonn): double, float? typedefed |
71 double fRealValue; | 77 double fRealValue; |
72 NotOwnedString fStr; | 78 NotOwnedString fStr; |
73 | 79 |
74 // 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 | 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 |
75 SkTDArray<SkPdfObject*>* fArray; | 81 SkTDArray<SkPdfObject*>* fArray; |
76 Reference fRef; | 82 Reference fRef; |
77 }; | 83 }; |
78 SkTDict<SkPdfObject*>* fMap; | 84 SkTDict<SkPdfObject*>* fMap; |
| 85 |
| 86 // TODO(edisonn): rename data with cache |
79 void* fData; | 87 void* fData; |
| 88 DataType fDataType; |
80 | 89 |
81 | 90 |
82 public: | 91 public: |
83 | 92 |
84 SkPdfObject() : fObjectType(kInvalid_PdfObjectType), fMap(NULL), fData(NULL)
{} | 93 SkPdfObject() : fObjectType(kInvalid_PdfObjectType), fMap(NULL), fData(NULL)
, fDataType(kEmpty_Data) {} |
85 | 94 |
86 inline void* data() { | 95 |
87 return fData; | 96 inline bool hasData(DataType type) { |
| 97 return type == fDataType; |
88 } | 98 } |
89 | 99 |
90 inline void setData(void* data) { | 100 inline void* data(DataType type) { |
| 101 return type == fDataType ? fData : NULL; |
| 102 } |
| 103 |
| 104 inline void setData(void* data, DataType type) { |
| 105 releaseData(); |
| 106 fDataType = type; |
91 fData = data; | 107 fData = data; |
92 } | 108 } |
93 | 109 |
| 110 void releaseData(); |
| 111 |
94 // ~SkPdfObject() { | 112 // ~SkPdfObject() { |
95 // //reset(); must be called manually! | 113 // //reset(); must be called manually! |
96 // } | 114 // } |
97 | 115 |
98 void reset() { | 116 void reset() { |
99 switch (fObjectType) { | 117 switch (fObjectType) { |
100 case kArray_PdfObjectType: | 118 case kArray_PdfObjectType: |
101 delete fArray; | 119 delete fArray; |
102 break; | 120 break; |
103 | 121 |
104 case kDictionary_PdfObjectType: | 122 case kDictionary_PdfObjectType: |
105 delete fMap; | 123 delete fMap; |
106 if (isStreamOwned()) { | 124 if (isStreamOwned()) { |
107 delete[] fStr.fBuffer; | 125 delete[] fStr.fBuffer; |
108 fStr.fBuffer = NULL; | 126 fStr.fBuffer = NULL; |
109 fStr.fBytes = 0; | 127 fStr.fBytes = 0; |
110 } | 128 } |
111 break; | 129 break; |
112 | 130 |
113 default: | 131 default: |
114 break; | 132 break; |
115 } | 133 } |
116 fObjectType = kInvalid_PdfObjectType; | 134 fObjectType = kInvalid_PdfObjectType; |
| 135 releaseData(); |
117 } | 136 } |
118 | 137 |
119 ObjectType type() { return fObjectType; } | 138 ObjectType type() { return fObjectType; } |
120 | 139 |
121 const char* c_str() const { | 140 const char* c_str() const { |
122 switch (fObjectType) { | 141 switch (fObjectType) { |
123 case kString_PdfObjectType: | 142 case kString_PdfObjectType: |
124 case kHexString_PdfObjectType: | 143 case kHexString_PdfObjectType: |
125 case kKeyword_PdfObjectType: | 144 case kKeyword_PdfObjectType: |
126 case kName_PdfObjectType: | 145 case kName_PdfObjectType: |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 } | 616 } |
598 | 617 |
599 SkPdfStream* getStream() { | 618 SkPdfStream* getStream() { |
600 return hasStream() ? (SkPdfStream*)this : NULL; | 619 return hasStream() ? (SkPdfStream*)this : NULL; |
601 } | 620 } |
602 | 621 |
603 bool isAnyString() const { | 622 bool isAnyString() const { |
604 return fObjectType == kString_PdfObjectType || fObjectType == kHexString
_PdfObjectType; | 623 return fObjectType == kString_PdfObjectType || fObjectType == kHexString
_PdfObjectType; |
605 } | 624 } |
606 | 625 |
| 626 bool isHexString() const { |
| 627 return fObjectType == kHexString_PdfObjectType; |
| 628 } |
| 629 |
607 bool isMatrix() const { | 630 bool isMatrix() const { |
608 return fObjectType == kArray_PdfObjectType && fArray->count() == 6; // N
YI + and elems are numbers | 631 return fObjectType == kArray_PdfObjectType && fArray->count() == 6; // N
YI + and elems are numbers |
609 } | 632 } |
610 | 633 |
611 inline int64_t intValue() const { | 634 inline int64_t intValue() const { |
612 SkASSERT(fObjectType == kInteger_PdfObjectType); | 635 SkASSERT(fObjectType == kInteger_PdfObjectType); |
613 | 636 |
614 if (fObjectType != kInteger_PdfObjectType) { | 637 if (fObjectType != kInteger_PdfObjectType) { |
615 // TODO(edisonn): log err | 638 // TODO(edisonn): log err |
616 return 0; | 639 return 0; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 break; | 883 break; |
861 | 884 |
862 case kString_PdfObjectType: | 885 case kString_PdfObjectType: |
863 str.append("\""); | 886 str.append("\""); |
864 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 887 str.append((const char*)fStr.fBuffer, fStr.fBytes); |
865 str.append("\""); | 888 str.append("\""); |
866 break; | 889 break; |
867 | 890 |
868 case kHexString_PdfObjectType: | 891 case kHexString_PdfObjectType: |
869 str.append("<"); | 892 str.append("<"); |
870 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 893 for (unsigned int i = 0 ; i < fStr.fBytes; i++) { |
| 894 str.appendf("%02x", (unsigned int)fStr.fBuffer[i]); |
| 895 } |
871 str.append(">"); | 896 str.append(">"); |
872 break; | 897 break; |
873 | 898 |
874 case kName_PdfObjectType: | 899 case kName_PdfObjectType: |
875 str.append("/"); | 900 str.append("/"); |
876 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 901 str.append((const char*)fStr.fBuffer, fStr.fBytes); |
877 break; | 902 break; |
878 | 903 |
879 case kKeyword_PdfObjectType: | 904 case kKeyword_PdfObjectType: |
880 str.append((const char*)fStr.fBuffer, fStr.fBytes); | 905 str.append((const char*)fStr.fBuffer, fStr.fBytes); |
(...skipping 20 matching lines...) Expand all Loading... |
901 while ((key = iter.next(&obj)) != NULL) { | 926 while ((key = iter.next(&obj)) != NULL) { |
902 appendSpaces(&str, level + 2); | 927 appendSpaces(&str, level + 2); |
903 str.appendf("/%s %s\n", key, obj->toString(0, level + st
rlen(key) + 4).c_str()); | 928 str.appendf("/%s %s\n", key, obj->toString(0, level + st
rlen(key) + 4).c_str()); |
904 } | 929 } |
905 appendSpaces(&str, level); | 930 appendSpaces(&str, level); |
906 str.append(">>"); | 931 str.append(">>"); |
907 if (hasStream()) { | 932 if (hasStream()) { |
908 const unsigned char* stream = NULL; | 933 const unsigned char* stream = NULL; |
909 size_t length = 0; | 934 size_t length = 0; |
910 if (GetFilteredStreamRef(&stream, &length)) { | 935 if (GetFilteredStreamRef(&stream, &length)) { |
911 str.append("stream"); | 936 str.append("stream\n"); |
912 str.append((const char*)stream, length > 256 ? 256 :
length); | 937 str.append((const char*)stream, length > 256 ? 256 :
length); |
913 str.append("endstream"); | 938 str.append("\nendstream"); |
914 } else { | 939 } else { |
915 str.append("stream STREAM_ERROR endstream"); | 940 str.append("stream STREAM_ERROR endstream"); |
916 } | 941 } |
917 } | 942 } |
918 } | 943 } |
919 break; | 944 break; |
920 | 945 |
921 case kNull_PdfObjectType: | 946 case kNull_PdfObjectType: |
922 str = "NULL"; | 947 str = "NULL"; |
923 break; | 948 break; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
972 SkPdfName() : SkPdfObject() { | 997 SkPdfName() : SkPdfObject() { |
973 SkPdfObject::makeName((const unsigned char*)"", this); | 998 SkPdfObject::makeName((const unsigned char*)"", this); |
974 } | 999 } |
975 public: | 1000 public: |
976 SkPdfName(char* name) : SkPdfObject() { | 1001 SkPdfName(char* name) : SkPdfObject() { |
977 this->makeName((const unsigned char*)name, this); | 1002 this->makeName((const unsigned char*)name, this); |
978 } | 1003 } |
979 }; | 1004 }; |
980 | 1005 |
981 #endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ | 1006 #endif // EXPERIMENTAL_PDFVIEWER_PDFPARSER_NATIVE_SKPDFOBJECT_H_ |
OLD | NEW |