Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: experimental/PdfViewer/pdfparser/native/SkPdfNativeTokenizer.h

Issue 27043002: pdfviewer: (more code cleanup): class documentation and comments Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « experimental/PdfViewer/pdfparser/native/SkPdfNativeObject.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkPdfNativeTokenizer_DEFINED 8 #ifndef SkPdfNativeTokenizer_DEFINED
9 #define SkPdfNativeTokenizer_DEFINED 9 #define SkPdfNativeTokenizer_DEFINED
10 10
11 #include "SkPdfConfig.h"
12
13 #include "SkTDArray.h"
14 #include "SkTDict.h"
15 #include <math.h> 11 #include <math.h>
16 #include <string.h> 12 #include <string.h>
17 13
14 #include "SkPdfConfig.h"
15 #include "SkTDArray.h"
16 #include "SkTDict.h"
17
18 // All these constants are defined by the PDF 1.4 Spec.
19
18 class SkPdfDictionary; 20 class SkPdfDictionary;
19 class SkPdfImageDictionary; 21 class SkPdfImageDictionary;
22 class SkPdfNativeDoc;
23 class SkPdfNativeObject;
24
20 25
21 // White Spaces 26 // White Spaces
22 #define kNUL_PdfWhiteSpace '\x00' 27 #define kNUL_PdfWhiteSpace '\x00'
23 #define kHT_PdfWhiteSpace '\x09' 28 #define kHT_PdfWhiteSpace '\x09'
24 #define kLF_PdfWhiteSpace '\x0A' 29 #define kLF_PdfWhiteSpace '\x0A'
25 #define kFF_PdfWhiteSpace '\x0C' 30 #define kFF_PdfWhiteSpace '\x0C'
26 #define kCR_PdfWhiteSpace '\x0D' 31 #define kCR_PdfWhiteSpace '\x0D'
27 #define kSP_PdfWhiteSpace '\x20' 32 #define kSP_PdfWhiteSpace '\x20'
28 33
29 // PdfDelimiters 34 // PdfDelimiters
(...skipping 12 matching lines...) Expand all
42 #define kBackspace_PdfSpecial '\x08' 47 #define kBackspace_PdfSpecial '\x08'
43 48
44 // TODO(edisonn): what is the faster way for compiler/machine type to evaluate t his expressions? 49 // TODO(edisonn): what is the faster way for compiler/machine type to evaluate t his expressions?
45 // we should evaluate all options. might be even different from one machine to a nother 50 // we should evaluate all options. might be even different from one machine to a nother
46 // 1) expand expression, let compiler optimize it 51 // 1) expand expression, let compiler optimize it
47 // 2) binary search 52 // 2) binary search
48 // 3) linear search in array 53 // 3) linear search in array
49 // 4) vector (e.f. T type[256] .. return type[ch] ... 54 // 4) vector (e.f. T type[256] .. return type[ch] ...
50 // 5) manually build the expression with least number of operators, e.g. for con secutive 55 // 5) manually build the expression with least number of operators, e.g. for con secutive
51 // chars, we can use an binary equal ignoring last bit 56 // chars, we can use an binary equal ignoring last bit
52 #define isPdfWhiteSpace(ch) (((ch)==kNUL_PdfWhiteSpace)||((ch)==kHT_PdfWhiteSpac e)||((ch)==kLF_PdfWhiteSpace)||((ch)==kFF_PdfWhiteSpace)||((ch)==kCR_PdfWhiteSpa ce)||((ch)==kSP_PdfWhiteSpace)) 57 #define isPdfWhiteSpace(ch) (((ch)==kNUL_PdfWhiteSpace)|| \
58 ((ch)==kHT_PdfWhiteSpace)|| \
59 ((ch)==kLF_PdfWhiteSpace)|| \
60 ((ch)==kFF_PdfWhiteSpace)|| \
61 ((ch)==kCR_PdfWhiteSpace)|| \
62 ((ch)==kSP_PdfWhiteSpace))
53 63
54 #define isPdfEOL(ch) (((ch)==kLF_PdfWhiteSpace)||((ch)==kCR_PdfWhiteSpace)) 64 #define isPdfEOL(ch) (((ch)==kLF_PdfWhiteSpace)||((ch)==kCR_PdfWhiteSpace))
55 65
56 66
57 #define isPdfDelimiter(ch) (((ch)==kOpenedRoundBracket_PdfDelimiter)||\ 67 #define isPdfDelimiter(ch) (((ch)==kOpenedRoundBracket_PdfDelimiter)||\
58 ((ch)==kClosedRoundBracket_PdfDelimiter)||\ 68 ((ch)==kClosedRoundBracket_PdfDelimiter)||\
59 ((ch)==kOpenedInequityBracket_PdfDelimiter)||\ 69 ((ch)==kOpenedInequityBracket_PdfDelimiter)||\
60 ((ch)==kClosedInequityBracket_PdfDelimiter)||\ 70 ((ch)==kClosedInequityBracket_PdfDelimiter)||\
61 ((ch)==kOpenedSquareBracket_PdfDelimiter)||\ 71 ((ch)==kOpenedSquareBracket_PdfDelimiter)||\
62 ((ch)==kClosedSquareBracket_PdfDelimiter)||\ 72 ((ch)==kClosedSquareBracket_PdfDelimiter)||\
63 ((ch)==kOpenedCurlyBracket_PdfDelimiter)||\ 73 ((ch)==kOpenedCurlyBracket_PdfDelimiter)||\
64 ((ch)==kClosedCurlyBracket_PdfDelimiter)||\ 74 ((ch)==kClosedCurlyBracket_PdfDelimiter)||\
65 ((ch)==kNamed_PdfDelimiter)||\ 75 ((ch)==kNamed_PdfDelimiter)||\
66 ((ch)==kComment_PdfDelimiter)) 76 ((ch)==kComment_PdfDelimiter))
67 77
68 #define isPdfWhiteSpaceOrPdfDelimiter(ch) (isPdfWhiteSpace(ch)||isPdfDelimiter(c h)) 78 #define isPdfWhiteSpaceOrPdfDelimiter(ch) (isPdfWhiteSpace(ch)||isPdfDelimiter(c h))
69 79
70 #define isPdfDigit(ch) ((ch)>='0'&&(ch)<='9') 80 #define isPdfDigit(ch) ((ch)>='0'&&(ch)<='9')
71 #define isPdfNumeric(ch) (isPdfDigit(ch)||(ch)=='+'||(ch)=='-'||(ch)=='.') 81 #define isPdfNumeric(ch) (isPdfDigit(ch)||(ch)=='+'||(ch)=='-'||(ch)=='.')
72 82
73 const unsigned char* skipPdfWhiteSpaces(const unsigned char* buffer, const unsig ned char* end); 83 const unsigned char* skipPdfWhiteSpaces(const unsigned char* buffer, const unsig ned char* end);
74 const unsigned char* endOfPdfToken(const unsigned char* start, const unsigned ch ar* end); 84 const unsigned char* endOfPdfToken(const unsigned char* start, const unsigned ch ar* end);
75 85
76 // TODO(edisonn): typedef read and integer tyepes? make less readable... 86 #define BUFFER_SIZE 1024
77 //typedef double SkPdfReal;
78 //typedef int64_t SkPdfInteger;
79 87
80 // an allocator only allocates memory, and it deletes it all when the allocator is destroyed 88 /** \class SkPdfAllocator
81 // this would allow us not to do any garbage collection while we parse or draw a pdf, and defere it 89 *
82 // while the user is looking at the image 90 * An allocator only allocates memory, and it deletes it all when the allocato r is destroyed.
83 91 * This strategy would allow us not to do any garbage collection while we pars e and/or render
84 class SkPdfNativeObject; 92 * a pdf.
85 93 *
94 */
86 class SkPdfAllocator { 95 class SkPdfAllocator {
87 #define BUFFER_SIZE 1024
88 SkTDArray<SkPdfNativeObject*> fHistory;
89 SkTDArray<void*> fHandles;
90 SkPdfNativeObject* fCurrent;
91 int fCurrentUsed;
92
93 SkPdfNativeObject* allocBlock();
94 size_t fSizeInBytes;
95
96 public: 96 public:
97 SkPdfAllocator() { 97 SkPdfAllocator() {
98 fSizeInBytes = sizeof(*this); 98 fSizeInBytes = sizeof(*this);
99 fCurrent = allocBlock(); 99 fCurrent = allocBlock();
100 fCurrentUsed = 0; 100 fCurrentUsed = 0;
101 } 101 }
102 102
103 ~SkPdfAllocator(); 103 ~SkPdfAllocator();
104 104
105 // Allocates an object. It will be reset automatically when ~SkPdfAllocator( ) is called.
105 SkPdfNativeObject* allocObject(); 106 SkPdfNativeObject* allocObject();
106 107
107 // TODO(edisonn): free this memory in destructor, track the usage? 108 // Allocates a buffer. It will be freed automatically when ~SkPdfAllocator() is called.
108 void* alloc(size_t bytes) { 109 void* alloc(size_t bytes) {
109 void* data = malloc(bytes); 110 void* data = malloc(bytes);
110 fHandles.push(data); 111 fHandles.push(data);
111 fSizeInBytes += bytes; 112 fSizeInBytes += bytes;
112 return data; 113 return data;
113 } 114 }
114 115
116 // Returns the number of bytes used in this allocator.
115 size_t bytesUsed() const { 117 size_t bytesUsed() const {
116 return fSizeInBytes; 118 return fSizeInBytes;
117 } 119 }
120
121 private:
122 SkTDArray<SkPdfNativeObject*> fHistory;
123 SkTDArray<void*> fHandles;
124 SkPdfNativeObject* fCurrent;
125 int fCurrentUsed;
126
127 SkPdfNativeObject* allocBlock();
128 size_t fSizeInBytes;
118 }; 129 };
119 130
120 class SkPdfNativeDoc; 131 // Type of a parsed token.
121 const unsigned char* nextObject(const unsigned char* start, const unsigned char* end, SkPdfNativeObject* token, SkPdfAllocator* allocator, SkPdfNativeDoc* doc);
122
123 enum SkPdfTokenType { 132 enum SkPdfTokenType {
124 kKeyword_TokenType, 133 kKeyword_TokenType,
125 kObject_TokenType, 134 kObject_TokenType,
126 }; 135 };
127 136
137
138 /** \struct PdfToken
139 *
140 * Stores the result of the parsing - a keyword or an object.
141 *
142 */
128 struct PdfToken { 143 struct PdfToken {
129 const char* fKeyword; 144 const char* fKeyword;
130 size_t fKeywordLength; 145 size_t fKeywordLength;
131 SkPdfNativeObject* fObject; 146 SkPdfNativeObject* fObject;
132 SkPdfTokenType fType; 147 SkPdfTokenType fType;
133 148
134 PdfToken() : fKeyword(NULL), fKeywordLength(0), fObject(NULL) {} 149 PdfToken() : fKeyword(NULL), fKeywordLength(0), fObject(NULL) {}
135 }; 150 };
136 151
152 /** \class SkPdfNativeTokenizer
153 *
154 * Responsible to tokenize a stream in small tokens, eityh a keyword or an obj ect.
155 * A renderer can feed on the tokens and render a pdf.
156 *
157 */
137 class SkPdfNativeTokenizer { 158 class SkPdfNativeTokenizer {
138 public: 159 public:
139 SkPdfNativeTokenizer(SkPdfNativeObject* objWithStream, SkPdfAllocator* alloc ator, SkPdfNativeDoc* doc); 160 SkPdfNativeTokenizer(SkPdfNativeObject* objWithStream,
140 SkPdfNativeTokenizer(const unsigned char* buffer, int len, SkPdfAllocator* a llocator, SkPdfNativeDoc* doc); 161 SkPdfAllocator* allocator, SkPdfNativeDoc* doc);
162 SkPdfNativeTokenizer(const unsigned char* buffer, int len,
163 SkPdfAllocator* allocator, SkPdfNativeDoc* doc);
141 164
142 virtual ~SkPdfNativeTokenizer(); 165 virtual ~SkPdfNativeTokenizer();
143 166
167 // Reads one token. Returns false if there are no more tokens.
144 bool readToken(PdfToken* token); 168 bool readToken(PdfToken* token);
145 bool readTokenCore(PdfToken* token); 169
170 // Put back a token to be read in the nextToken read. Only one token is allo wed to be put
171 // back. Must not necesaarely be the last token read.
146 void PutBack(PdfToken token); 172 void PutBack(PdfToken token);
173
174 // Reads the inline image that is present in the stream. At this point we ju st consumed the ID
175 // token already.
147 SkPdfImageDictionary* readInlineImage(); 176 SkPdfImageDictionary* readInlineImage();
148 177
149 private: 178 private:
179 bool readTokenCore(PdfToken* token);
180
150 SkPdfNativeDoc* fDoc; 181 SkPdfNativeDoc* fDoc;
151 SkPdfAllocator* fAllocator; 182 SkPdfAllocator* fAllocator;
152 183
153 const unsigned char* fUncompressedStreamStart; 184 const unsigned char* fUncompressedStreamStart;
154 const unsigned char* fUncompressedStream; 185 const unsigned char* fUncompressedStream;
155 const unsigned char* fUncompressedStreamEnd; 186 const unsigned char* fUncompressedStreamEnd;
156 187
157 bool fEmpty; 188 bool fEmpty;
158 bool fHasPutBack; 189 bool fHasPutBack;
159 PdfToken fPutBack; 190 PdfToken fPutBack;
160 }; 191 };
161 192
193 const unsigned char* nextObject(const unsigned char* start, const unsigned char* end,
194 SkPdfNativeObject* token,
195 SkPdfAllocator* allocator,
196 SkPdfNativeDoc* doc);
197
162 #endif // SkPdfNativeTokenizer_DEFINED 198 #endif // SkPdfNativeTokenizer_DEFINED
OLDNEW
« no previous file with comments | « experimental/PdfViewer/pdfparser/native/SkPdfNativeObject.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698