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 | */ |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 7 | |
| 8 | #include "SkPdfNativeTokenizer.h" |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 9 | #include "SkPdfNativeObject.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 10 | #include "SkPdfConfig.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 11 | |
edisonn@google.com | 33f11b6 | 2013-08-14 21:35:27 +0000 | [diff] [blame] | 12 | // TODO(edisonn): mac builder does not find the header ... but from headers is ok |
| 13 | //#include "SkPdfStreamCommonDictionary_autogen.h" |
| 14 | //#include "SkPdfImageDictionary_autogen.h" |
| 15 | #include "SkPdfHeaders_autogen.h" |
| 16 | |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 17 | |
| 18 | // TODO(edisonn): perf!!! |
| 19 | // there could be 0s between start and end! but not in the needle. |
| 20 | static char* strrstrk(char* hayStart, char* hayEnd, const char* needle) { |
| 21 | int needleLen = strlen(needle); |
| 22 | if ((isPdfWhiteSpaceOrPdfDelimiter(*(hayStart+needleLen)) || (hayStart+needleLen == hayEnd)) && |
| 23 | strncmp(hayStart, needle, needleLen) == 0) { |
| 24 | return hayStart; |
| 25 | } |
| 26 | |
| 27 | hayStart++; |
| 28 | |
| 29 | while (hayStart < hayEnd) { |
| 30 | if (isPdfWhiteSpaceOrPdfDelimiter(*(hayStart-1)) && |
| 31 | (isPdfWhiteSpaceOrPdfDelimiter(*(hayStart+needleLen)) || (hayStart+needleLen == hayEnd)) && |
| 32 | strncmp(hayStart, needle, needleLen) == 0) { |
| 33 | return hayStart; |
| 34 | } |
| 35 | hayStart++; |
| 36 | } |
| 37 | return NULL; |
| 38 | } |
| 39 | |
edisonn@google.com | e2e01ff | 2013-08-02 20:24:48 +0000 | [diff] [blame] | 40 | #ifdef PDF_TRACE_TOKENIZER |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 41 | static void TRACE_INDENT(int level, const char* type) { |
| 42 | static int id = 0; |
| 43 | id++; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 44 | #if 0 |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 45 | if (478613 == id) { |
| 46 | printf("break;\n"); |
| 47 | } |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 48 | #endif |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 49 | // all types should have 2 letters, so the text is alligned nicely |
| 50 | printf("\n%10i %15s: ", id, type); |
| 51 | for (int i = 0 ; i < level; i++) { |
| 52 | printf(" "); |
| 53 | } |
| 54 | } |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 55 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 56 | static void TRACE_COMMENT(char ch) { |
| 57 | printf("%c", ch); |
| 58 | } |
| 59 | |
| 60 | static void TRACE_TK(char ch) { |
| 61 | printf("%c", ch); |
| 62 | } |
| 63 | |
| 64 | static void TRACE_NAME(const unsigned char* start, const unsigned char* end) { |
| 65 | while (start < end) { |
| 66 | printf("%c", *start); |
| 67 | start++; |
| 68 | } |
| 69 | printf("\n"); |
| 70 | } |
| 71 | |
| 72 | static void TRACE_STRING(const unsigned char* start, const unsigned char* end) { |
| 73 | while (start < end) { |
| 74 | printf("%c", *start); |
| 75 | start++; |
| 76 | } |
| 77 | printf("\n"); |
| 78 | } |
| 79 | |
| 80 | static void TRACE_HEXSTRING(const unsigned char* start, const unsigned char* end) { |
| 81 | while (start < end) { |
| 82 | printf("%c", *start); |
| 83 | start++; |
| 84 | } |
| 85 | printf("\n"); |
| 86 | } |
| 87 | |
| 88 | #else |
| 89 | #define TRACE_INDENT(level,type) |
| 90 | #define TRACE_COMMENT(ch) |
| 91 | #define TRACE_TK(ch) |
| 92 | #define TRACE_NAME(start,end) |
| 93 | #define TRACE_STRING(start,end) |
| 94 | #define TRACE_HEXSTRING(start,end) |
| 95 | #endif |
| 96 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 97 | const unsigned char* skipPdfWhiteSpaces(int level, const unsigned char* start, const unsigned char* end) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 98 | TRACE_INDENT(level, "White Space"); |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 99 | while (start < end && (isPdfWhiteSpace(*start) || *start == kComment_PdfDelimiter)) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 100 | TRACE_COMMENT(*start); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 101 | if (*start == kComment_PdfDelimiter) { |
| 102 | // skip the comment until end of line |
| 103 | while (start < end && !isPdfEOL(*start)) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 104 | //*start = '\0'; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 105 | start++; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 106 | TRACE_COMMENT(*start); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 107 | } |
| 108 | } else { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 109 | //*start = '\0'; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 110 | start++; |
| 111 | } |
| 112 | } |
| 113 | return start; |
| 114 | } |
| 115 | |
| 116 | // TODO(edisonn) '(' can be used, will it break the string a delimiter or space inside () ? |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 117 | const unsigned char* endOfPdfToken(int level, const unsigned char* start, const unsigned char* end) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 118 | //int opened brackets |
| 119 | //TODO(edisonn): what out for special chars, like \n, \032 |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 120 | TRACE_INDENT(level, "Token"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 121 | |
| 122 | SkASSERT(!isPdfWhiteSpace(*start)); |
| 123 | |
| 124 | if (start < end && isPdfDelimiter(*start)) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 125 | TRACE_TK(*start); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 126 | start++; |
| 127 | return start; |
| 128 | } |
| 129 | |
| 130 | while (start < end && !isPdfWhiteSpaceOrPdfDelimiter(*start)) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 131 | TRACE_TK(*start); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 132 | start++; |
| 133 | } |
| 134 | return start; |
| 135 | } |
| 136 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 137 | // last elem has to be ] |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 138 | static const unsigned char* readArray(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* array, SkPdfAllocator* allocator, SkPdfNativeDoc* doc GET_TRACK_STREAM) { |
| 139 | SkPdfNativeObject::makeEmptyArray(array PUT_TRACK_STREAM(start, start)); |
| 140 | |
edisonn@google.com | 1f08016 | 2013-07-23 21:05:49 +0000 | [diff] [blame] | 141 | if (allocator == NULL) { |
| 142 | // TODO(edisonn): report/warning error |
| 143 | return end; |
| 144 | } |
| 145 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 146 | TRACE_INDENT(level, "Array"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 147 | while (start < end) { |
| 148 | // skip white spaces |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 149 | start = skipPdfWhiteSpaces(level + 1, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 150 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 151 | const unsigned char* endOfToken = endOfPdfToken(level + 1, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 152 | |
| 153 | if (endOfToken == start) { |
| 154 | // TODO(edisonn): report error in pdf file (end of stream with ] for end of aray |
| 155 | return start; |
| 156 | } |
| 157 | |
| 158 | if (endOfToken == start + 1 && *start == kClosedSquareBracket_PdfDelimiter) { |
| 159 | return endOfToken; |
| 160 | } |
| 161 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 162 | SkPdfNativeObject* newObj = allocator->allocObject(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 163 | start = nextObject(level + 1, start, end, newObj, allocator, doc PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 164 | // TODO(edisonn): perf/memory: put the variables on the stack, and flush them on the array only when |
| 165 | // we are sure they are not references! |
| 166 | if (newObj->isKeywordReference() && array->size() >= 2 && array->objAtAIndex(array->size() - 1)->isInteger() && array->objAtAIndex(array->size() - 2)->isInteger()) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 167 | SkPdfNativeObject* gen = array->removeLastInArray(); |
| 168 | SkPdfNativeObject* id = array->removeLastInArray(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 169 | |
| 170 | SkPdfNativeObject::resetAndMakeReference((unsigned int)id->intValue(), (unsigned int)gen->intValue(), newObj PUT_TRACK_PARAMETERS_OBJ2(id, newObj)); |
| 171 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 172 | } |
| 173 | array->appendInArray(newObj); |
| 174 | } |
| 175 | // TODO(edisonn): report not reached, we should never get here |
edisonn@google.com | 8bad737 | 2013-07-10 23:36:56 +0000 | [diff] [blame] | 176 | // TODO(edisonn): there might be a bug here, enable an assert and run it on files |
| 177 | // or it might be that the files were actually corrupted |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 178 | return start; |
| 179 | } |
| 180 | |
| 181 | // When we read strings we will rewrite the string so we will reuse the memory |
| 182 | // when we start to read the string, we already consumed the opened bracket |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 183 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 184 | // TODO(edisonn): space: add paramater, taht would report if we need to allocate new buffer, or we can reuse the one we have |
| 185 | |
| 186 | static const unsigned char* readString(int level, const unsigned char* start, const unsigned char* end, unsigned char* out) { |
| 187 | TRACE_INDENT(level, "String"); |
| 188 | const unsigned char* in = start; |
| 189 | bool hasOut = (out != NULL); |
| 190 | |
| 191 | int openRoundBrackets = 1; |
| 192 | while (in < end) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 193 | openRoundBrackets += ((*in) == kOpenedRoundBracket_PdfDelimiter); |
| 194 | openRoundBrackets -= ((*in) == kClosedRoundBracket_PdfDelimiter); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 195 | if (openRoundBrackets == 0) { |
| 196 | in++; // consumed ) |
| 197 | break; |
| 198 | } |
| 199 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 200 | if (*in == kEscape_PdfSpecial) { |
| 201 | if (in + 1 < end) { |
| 202 | switch (in[1]) { |
| 203 | case 'n': |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 204 | if (hasOut) { *out = kLF_PdfWhiteSpace; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 205 | out++; |
| 206 | in += 2; |
| 207 | break; |
| 208 | |
| 209 | case 'r': |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 210 | if (hasOut) { *out = kCR_PdfWhiteSpace; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 211 | out++; |
| 212 | in += 2; |
| 213 | break; |
| 214 | |
| 215 | case 't': |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 216 | if (hasOut) { *out = kHT_PdfWhiteSpace; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 217 | out++; |
| 218 | in += 2; |
| 219 | break; |
| 220 | |
| 221 | case 'b': |
| 222 | // TODO(edisonn): any special meaning to backspace? |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 223 | if (hasOut) { *out = kBackspace_PdfSpecial; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 224 | out++; |
| 225 | in += 2; |
| 226 | break; |
| 227 | |
| 228 | case 'f': |
edisonn@google.com | 1f08016 | 2013-07-23 21:05:49 +0000 | [diff] [blame] | 229 | if (hasOut) { *out = kFF_PdfWhiteSpace; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 230 | out++; |
| 231 | in += 2; |
| 232 | break; |
| 233 | |
| 234 | case kOpenedRoundBracket_PdfDelimiter: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 235 | if (hasOut) { *out = kOpenedRoundBracket_PdfDelimiter; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 236 | out++; |
| 237 | in += 2; |
| 238 | break; |
| 239 | |
| 240 | case kClosedRoundBracket_PdfDelimiter: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 241 | if (hasOut) { *out = kClosedRoundBracket_PdfDelimiter; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 242 | out++; |
| 243 | in += 2; |
| 244 | break; |
| 245 | |
| 246 | case kEscape_PdfSpecial: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 247 | if (hasOut) { *out = kEscape_PdfSpecial; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 248 | out++; |
| 249 | in += 2; |
| 250 | break; |
| 251 | |
| 252 | case '0': |
| 253 | case '1': |
| 254 | case '2': |
| 255 | case '3': |
| 256 | case '4': |
| 257 | case '5': |
| 258 | case '6': |
| 259 | case '7': { |
| 260 | //read octals |
| 261 | in++; // consume backslash |
| 262 | |
| 263 | int code = 0; |
| 264 | int i = 0; |
| 265 | while (in < end && *in >= '0' && *in < '8') { |
| 266 | code = (code << 3) + ((*in) - '0'); // code * 8 + d |
| 267 | i++; |
| 268 | in++; |
| 269 | if (i == 3) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 270 | if (hasOut) { *out = code & 0xff; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 271 | out++; |
| 272 | i = 0; |
| 273 | } |
| 274 | } |
| 275 | if (i > 0) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 276 | if (hasOut) { *out = code & 0xff; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 277 | out++; |
| 278 | } |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | default: |
| 283 | // Per spec, backslash is ignored is escaped ch is unknown |
| 284 | in++; |
| 285 | break; |
| 286 | } |
edisonn@google.com | 8bad737 | 2013-07-10 23:36:56 +0000 | [diff] [blame] | 287 | } else { |
| 288 | in++; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 289 | } |
| 290 | } else { |
| 291 | // TODO(edisonn): perf, avoid copy into itself, maybe first do a simple scan until found backslash ? |
| 292 | // we could have one look that first just inc current, and when we find the backslash |
| 293 | // we go to this loop |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 294 | if (hasOut) { *out = *in; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 295 | in++; |
| 296 | out++; |
| 297 | } |
| 298 | } |
| 299 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 300 | if (hasOut) { |
| 301 | return in; // consumed already ) at the end of the string |
| 302 | } else { |
| 303 | return start + (out - (const unsigned char*)NULL); // return where the string would end if we reuse the string |
| 304 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 305 | } |
| 306 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 307 | static int readStringLength(int level, const unsigned char* start, const unsigned char* end) { |
| 308 | return readString(level, start, end, NULL) - start; |
| 309 | } |
| 310 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 311 | static const unsigned char* readString(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* str, SkPdfAllocator* allocator GET_TRACK_STREAM) { |
edisonn@google.com | b44334c | 2013-07-23 20:47:05 +0000 | [diff] [blame] | 312 | if (!allocator) { |
| 313 | return end; |
| 314 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 315 | int outLength = readStringLength(level, start, end); |
| 316 | // TODO(edisonn): optimize the allocation, don't allocate new string, but put it in a preallocated buffer |
| 317 | unsigned char* out = (unsigned char*)allocator->alloc(outLength); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 318 | const unsigned char* now = readString(level, start, end, out); |
| 319 | SkPdfNativeObject::makeString(out, out + outLength, str PUT_TRACK_STREAM(start, now)); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 320 | TRACE_STRING(out, out + outLength); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 321 | return now; // consumed already ) at the end of the string |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static const unsigned char* readHexString(int level, const unsigned char* start, const unsigned char* end, unsigned char* out) { |
| 325 | TRACE_INDENT(level, "HexString"); |
| 326 | bool hasOut = (out != NULL); |
| 327 | const unsigned char* in = start; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 328 | |
| 329 | unsigned char code = 0; |
| 330 | |
| 331 | while (in < end) { |
| 332 | while (in < end && isPdfWhiteSpace(*in)) { |
| 333 | in++; |
| 334 | } |
| 335 | |
| 336 | if (*in == kClosedInequityBracket_PdfDelimiter) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 337 | //*in = '\0'; |
| 338 | in++; // consume > |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 339 | // normal exit |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | if (in >= end) { |
| 344 | // end too soon |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | switch (*in) { |
| 349 | case '0': |
| 350 | case '1': |
| 351 | case '2': |
| 352 | case '3': |
| 353 | case '4': |
| 354 | case '5': |
| 355 | case '6': |
| 356 | case '7': |
| 357 | case '8': |
| 358 | case '9': |
| 359 | code = (*in - '0') << 4; |
| 360 | break; |
| 361 | |
| 362 | case 'a': |
| 363 | case 'b': |
| 364 | case 'c': |
| 365 | case 'd': |
| 366 | case 'e': |
| 367 | case 'f': |
| 368 | code = (*in - 'a' + 10) << 4; |
| 369 | break; |
| 370 | |
| 371 | case 'A': |
| 372 | case 'B': |
| 373 | case 'C': |
| 374 | case 'D': |
| 375 | case 'E': |
| 376 | case 'F': |
| 377 | code = (*in - 'A' + 10) << 4; |
| 378 | break; |
| 379 | |
| 380 | // TODO(edisonn): spec does not say how to handle this error |
| 381 | default: |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | in++; // advance |
| 386 | |
| 387 | while (in < end && isPdfWhiteSpace(*in)) { |
| 388 | in++; |
| 389 | } |
| 390 | |
| 391 | // TODO(edisonn): report error |
| 392 | if (in >= end) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 393 | if (hasOut) { *out = code; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 394 | out++; |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | if (*in == kClosedInequityBracket_PdfDelimiter) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 399 | if (hasOut) { *out = code; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 400 | out++; |
edisonn@google.com | 1acab36 | 2013-07-25 22:03:22 +0000 | [diff] [blame] | 401 | in++; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | |
| 405 | switch (*in) { |
| 406 | case '0': |
| 407 | case '1': |
| 408 | case '2': |
| 409 | case '3': |
| 410 | case '4': |
| 411 | case '5': |
| 412 | case '6': |
| 413 | case '7': |
| 414 | case '8': |
| 415 | case '9': |
| 416 | code += (*in - '0'); |
| 417 | break; |
| 418 | |
| 419 | case 'a': |
| 420 | case 'b': |
| 421 | case 'c': |
| 422 | case 'd': |
| 423 | case 'e': |
| 424 | case 'f': |
| 425 | code += (*in - 'a' + 10); |
| 426 | break; |
| 427 | |
| 428 | case 'A': |
| 429 | case 'B': |
| 430 | case 'C': |
| 431 | case 'D': |
| 432 | case 'E': |
| 433 | case 'F': |
| 434 | code += (*in - 'A' + 10); |
| 435 | break; |
| 436 | |
| 437 | // TODO(edisonn): spec does not say how to handle this error |
| 438 | default: |
| 439 | break; |
| 440 | } |
| 441 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 442 | if (hasOut) { *out = code; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 443 | out++; |
| 444 | in++; |
| 445 | } |
| 446 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 447 | if (hasOut) { |
| 448 | return in; // consumed already > at the end of the string |
| 449 | } else { |
| 450 | return start + (out - (const unsigned char*)NULL); // return where the string would end if we reuse the string |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 451 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 452 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 453 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 454 | static int readHexStringLength(int level, const unsigned char* start, const unsigned char* end) { |
| 455 | return readHexString(level, start, end, NULL) - start; |
| 456 | } |
| 457 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 458 | static const unsigned char* readHexString(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* str, SkPdfAllocator* allocator GET_TRACK_STREAM) { |
edisonn@google.com | b44334c | 2013-07-23 20:47:05 +0000 | [diff] [blame] | 459 | if (!allocator) { |
| 460 | return end; |
| 461 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 462 | int outLength = readHexStringLength(level, start, end); |
| 463 | // TODO(edisonn): optimize the allocation, don't allocate new string, but put it in a preallocated buffer |
| 464 | unsigned char* out = (unsigned char*)allocator->alloc(outLength); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 465 | const unsigned char* now = readHexString(level, start, end, out); |
| 466 | SkPdfNativeObject::makeHexString(out, out + outLength, str PUT_TRACK_STREAM(start, now)); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 467 | TRACE_HEXSTRING(out, out + outLength); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 468 | return now; // consumed already > at the end of the string |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // TODO(edisonn): before PDF 1.2 name could not have special characters, add version parameter |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 472 | static const unsigned char* readName(int level, const unsigned char* start, const unsigned char* end, unsigned char* out) { |
| 473 | TRACE_INDENT(level, "Name"); |
| 474 | bool hasOut = (out != NULL); |
| 475 | const unsigned char* in = start; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 476 | |
| 477 | unsigned char code = 0; |
| 478 | |
| 479 | while (in < end) { |
| 480 | if (isPdfWhiteSpaceOrPdfDelimiter(*in)) { |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | if (*in == '#' && in + 2 < end) { |
| 485 | in++; |
| 486 | switch (*in) { |
| 487 | case '0': |
| 488 | case '1': |
| 489 | case '2': |
| 490 | case '3': |
| 491 | case '4': |
| 492 | case '5': |
| 493 | case '6': |
| 494 | case '7': |
| 495 | case '8': |
| 496 | case '9': |
| 497 | code = (*in - '0') << 4; |
| 498 | break; |
| 499 | |
| 500 | case 'a': |
| 501 | case 'b': |
| 502 | case 'c': |
| 503 | case 'd': |
| 504 | case 'e': |
| 505 | case 'f': |
| 506 | code = (*in - 'a' + 10) << 4; |
| 507 | break; |
| 508 | |
| 509 | case 'A': |
| 510 | case 'B': |
| 511 | case 'C': |
| 512 | case 'D': |
| 513 | case 'E': |
| 514 | case 'F': |
| 515 | code = (*in - 'A' + 10) << 4; |
| 516 | break; |
| 517 | |
| 518 | // TODO(edisonn): spec does not say how to handle this error |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | in++; // advance |
| 524 | |
| 525 | switch (*in) { |
| 526 | case '0': |
| 527 | case '1': |
| 528 | case '2': |
| 529 | case '3': |
| 530 | case '4': |
| 531 | case '5': |
| 532 | case '6': |
| 533 | case '7': |
| 534 | case '8': |
| 535 | case '9': |
| 536 | code += (*in - '0'); |
| 537 | break; |
| 538 | |
| 539 | case 'a': |
| 540 | case 'b': |
| 541 | case 'c': |
| 542 | case 'd': |
| 543 | case 'e': |
| 544 | case 'f': |
| 545 | code += (*in - 'a' + 10); |
| 546 | break; |
| 547 | |
| 548 | case 'A': |
| 549 | case 'B': |
| 550 | case 'C': |
| 551 | case 'D': |
| 552 | case 'E': |
| 553 | case 'F': |
| 554 | code += (*in - 'A' + 10); |
| 555 | break; |
| 556 | |
| 557 | // TODO(edisonn): spec does not say how to handle this error |
| 558 | default: |
| 559 | break; |
| 560 | } |
| 561 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 562 | if (hasOut) { *out = code; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 563 | out++; |
| 564 | in++; |
| 565 | } else { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 566 | if (hasOut) { *out = *in; } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 567 | out++; |
| 568 | in++; |
| 569 | } |
| 570 | } |
| 571 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 572 | if (hasOut) { |
| 573 | return in; |
| 574 | } else { |
| 575 | return start + (out - (const unsigned char*)NULL); // return where the string would end if we reuse the string |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static int readNameLength(int level, const unsigned char* start, const unsigned char* end) { |
| 580 | return readName(level, start, end, NULL) - start; |
| 581 | } |
| 582 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 583 | static const unsigned char* readName(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* name, SkPdfAllocator* allocator GET_TRACK_STREAM) { |
edisonn@google.com | b44334c | 2013-07-23 20:47:05 +0000 | [diff] [blame] | 584 | if (!allocator) { |
| 585 | return end; |
| 586 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 587 | int outLength = readNameLength(level, start, end); |
| 588 | // TODO(edisonn): optimize the allocation, don't allocate new string, but put it in a preallocated buffer |
| 589 | unsigned char* out = (unsigned char*)allocator->alloc(outLength); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 590 | const unsigned char* now = readName(level, start, end, out); |
| 591 | SkPdfNativeObject::makeName(out, out + outLength, name PUT_TRACK_STREAM(start, now)); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 592 | TRACE_NAME(out, out + outLength); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 593 | return now; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // TODO(edisonn): pdf spec let Length to be an indirect object define after the stream |
| 597 | // that makes for an interesting scenario, where the stream itself contains endstream, together |
| 598 | // with a reference object with the length, but the real length object would be somewhere else |
| 599 | // it could confuse the parser |
| 600 | /*example: |
| 601 | |
| 602 | 7 0 obj |
| 603 | << /length 8 0 R>> |
| 604 | stream |
| 605 | ............... |
| 606 | endstream |
| 607 | 8 0 obj #we are in stream actually, not a real object |
| 608 | << 10 >> #we are in stream actually, not a real object |
| 609 | endobj |
| 610 | endstream |
| 611 | 8 0 obj #real obj |
| 612 | << 100 >> #real obj |
| 613 | endobj |
| 614 | and it could get worse, with multiple object like this |
| 615 | */ |
| 616 | |
| 617 | // right now implement the silly algorithm that assumes endstream is finishing the stream |
| 618 | |
| 619 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 620 | static const unsigned char* readStream(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* dict, SkPdfNativeDoc* doc) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 621 | TRACE_INDENT(level, "Stream"); |
| 622 | start = skipPdfWhiteSpaces(level, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 623 | if (!(start[0] == 's' && start[1] == 't' && start[2] == 'r' && start[3] == 'e' && start[4] == 'a' && start[5] == 'm')) { |
| 624 | // no stream. return. |
| 625 | return start; |
| 626 | } |
| 627 | |
| 628 | start += 6; // strlen("stream") |
| 629 | if (start[0] == kCR_PdfWhiteSpace && start[1] == kLF_PdfWhiteSpace) { |
| 630 | start += 2; |
| 631 | } else if (start[0] == kLF_PdfWhiteSpace) { |
| 632 | start += 1; |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 633 | } else if (isPdfWhiteSpace(start[0])) { |
| 634 | start += 1; |
| 635 | } else { |
| 636 | // TODO(edisonn): warn it should be isPdfDelimiter(start[0])) ? |
| 637 | // TODO(edisonn): warning? |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | SkPdfStreamCommonDictionary* stream = (SkPdfStreamCommonDictionary*) dict; |
| 641 | // TODO(edisonn): load Length |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 642 | int64_t length = -1; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 643 | |
| 644 | // TODO(edisonn): very basic implementation |
edisonn@google.com | 951d653 | 2013-07-10 23:17:31 +0000 | [diff] [blame] | 645 | if (stream->has_Length() && stream->Length(doc) > 0) { |
| 646 | length = stream->Length(doc); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // TODO(edisonn): laod external streams |
| 650 | // TODO(edisonn): look at the last filter, to determione how to deal with possible issue |
| 651 | |
edisonn@google.com | 4ef4bed | 2013-07-29 22:14:45 +0000 | [diff] [blame] | 652 | |
| 653 | if (length >= 0) { |
| 654 | const unsigned char* endstream = start + length; |
| 655 | |
| 656 | if (endstream[0] == kCR_PdfWhiteSpace && endstream[1] == kLF_PdfWhiteSpace) { |
| 657 | endstream += 2; |
| 658 | } else if (endstream[0] == kLF_PdfWhiteSpace) { |
| 659 | endstream += 1; |
| 660 | } |
| 661 | |
| 662 | if (strncmp((const char*)endstream, "endstream", strlen("endstream")) != 0) { |
| 663 | length = -1; |
| 664 | } |
| 665 | } |
| 666 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 667 | if (length < 0) { |
| 668 | // scan the buffer, until we find first endstream |
| 669 | // TODO(edisonn): all buffers must have a 0 at the end now, |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 670 | const unsigned char* endstream = (const unsigned char*)strrstrk((char*)start, (char*)end, "endstream"); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 671 | |
| 672 | if (endstream) { |
| 673 | length = endstream - start; |
| 674 | if (*(endstream-1) == kLF_PdfWhiteSpace) length--; |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 675 | if (*(endstream-2) == kCR_PdfWhiteSpace) length--; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | if (length >= 0) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 679 | const unsigned char* endstream = start + length; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 680 | |
| 681 | if (endstream[0] == kCR_PdfWhiteSpace && endstream[1] == kLF_PdfWhiteSpace) { |
| 682 | endstream += 2; |
| 683 | } else if (endstream[0] == kLF_PdfWhiteSpace) { |
| 684 | endstream += 1; |
| 685 | } |
| 686 | |
| 687 | // TODO(edisonn): verify the next bytes are "endstream" |
| 688 | |
| 689 | endstream += strlen("endstream"); |
| 690 | // TODO(edisonn): Assert? report error/warning? |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 691 | dict->addStream(start, (size_t)length); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 692 | return endstream; |
| 693 | } |
| 694 | return start; |
| 695 | } |
| 696 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 697 | static const unsigned char* readInlineImageStream(int level, const unsigned char* start, const unsigned char* end, SkPdfImageDictionary* inlineImage, SkPdfNativeDoc* doc) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 698 | TRACE_INDENT(level, "Inline Image"); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 699 | // We already processed ID keyword, and we should be positioned immediately after it |
| 700 | |
| 701 | // TODO(edisonn): security: read after end check, or make buffers with extra 2 bytes |
| 702 | if (start[0] == kCR_PdfWhiteSpace && start[1] == kLF_PdfWhiteSpace) { |
| 703 | start += 2; |
| 704 | } else if (start[0] == kLF_PdfWhiteSpace) { |
| 705 | start += 1; |
| 706 | } else if (isPdfWhiteSpace(start[0])) { |
| 707 | start += 1; |
| 708 | } else { |
| 709 | SkASSERT(isPdfDelimiter(start[0])); |
| 710 | // TODO(edisonn): warning? |
| 711 | } |
| 712 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 713 | const unsigned char* endstream = (const unsigned char*)strrstrk((char*)start, (char*)end, "EI"); |
| 714 | const unsigned char* endEI = endstream ? endstream + 2 : NULL; // 2 == strlen("EI") |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 715 | |
| 716 | if (endstream) { |
| 717 | int length = endstream - start; |
| 718 | if (*(endstream-1) == kLF_PdfWhiteSpace) length--; |
| 719 | if (*(endstream-2) == kCR_PdfWhiteSpace) length--; |
| 720 | inlineImage->addStream(start, (size_t)length); |
| 721 | } else { |
| 722 | // TODO(edisonn): report error in inline image stream (ID-EI) section |
| 723 | // TODO(edisonn): based on filter, try to ignore a missing EI, and read data properly |
| 724 | return end; |
| 725 | } |
| 726 | return endEI; |
| 727 | } |
| 728 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 729 | static const unsigned char* readDictionary(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* dict, SkPdfAllocator* allocator, SkPdfNativeDoc* doc GET_TRACK_STREAM) { |
edisonn@google.com | 1f08016 | 2013-07-23 21:05:49 +0000 | [diff] [blame] | 730 | if (allocator == NULL) { |
| 731 | // TODO(edisonn): report/warning error |
| 732 | return end; |
| 733 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 734 | TRACE_INDENT(level, "Dictionary"); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 735 | SkPdfNativeObject::makeEmptyDictionary(dict PUT_TRACK_STREAM(start, start)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 736 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 737 | start = skipPdfWhiteSpaces(level, start, end); |
| 738 | SkPdfAllocator tmpStorage; // keys will be stored in dict, we can free them immediately after set. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 739 | |
| 740 | while (start < end && *start == kNamed_PdfDelimiter) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 741 | SkPdfNativeObject key; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 742 | //*start = '\0'; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 743 | start++; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 744 | start = readName(level + 1, start, end, &key, &tmpStorage PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 745 | start = skipPdfWhiteSpaces(level + 1, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 746 | |
| 747 | if (start < end) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 748 | SkPdfNativeObject* value = allocator->allocObject(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 749 | start = nextObject(level + 1, start, end, value, allocator, doc PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 750 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 751 | start = skipPdfWhiteSpaces(level + 1, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 752 | |
| 753 | if (start < end) { |
| 754 | // seems we have an indirect reference |
| 755 | if (isPdfDigit(*start)) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 756 | SkPdfNativeObject generation; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 757 | start = nextObject(level + 1, start, end, &generation, allocator, doc PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 758 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 759 | SkPdfNativeObject keywordR; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 760 | start = nextObject(level + 1, start, end, &keywordR, allocator, doc PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 761 | |
| 762 | if (value->isInteger() && generation.isInteger() && keywordR.isKeywordReference()) { |
| 763 | int64_t id = value->intValue(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 764 | SkPdfNativeObject::resetAndMakeReference((unsigned int)id, (unsigned int)generation.intValue(), value PUT_TRACK_PARAMETERS_OBJ2(value, &generation)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 765 | dict->set(&key, value); |
| 766 | } else { |
| 767 | // error, ignore |
| 768 | dict->set(&key, value); |
| 769 | } |
| 770 | } else { |
| 771 | // next elem is not a digit, but it might not be / either! |
| 772 | dict->set(&key, value); |
| 773 | } |
| 774 | } else { |
| 775 | // /key >> |
| 776 | dict->set(&key, value); |
| 777 | return end; |
| 778 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 779 | start = skipPdfWhiteSpaces(level + 1, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 780 | } else { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 781 | dict->set(&key, &SkPdfNativeObject::kNull); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 782 | return end; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // TODO(edisonn): options to ignore these errors |
| 787 | |
| 788 | // now we should expect >> |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 789 | start = skipPdfWhiteSpaces(level, start, end); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 790 | if (*start != kClosedInequityBracket_PdfDelimiter) { |
| 791 | // TODO(edisonn): report/warning |
| 792 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 793 | //*start = '\0'; |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 794 | start++; // skip > |
| 795 | if (*start != kClosedInequityBracket_PdfDelimiter) { |
| 796 | // TODO(edisonn): report/warning |
| 797 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 798 | //*start = '\0'; |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 799 | start++; // skip > |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 800 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 801 | STORE_TRACK_PARAMETER_OFFSET_END(dict,start); |
| 802 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 803 | start = readStream(level, start, end, dict, doc); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 804 | |
| 805 | return start; |
| 806 | } |
| 807 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 808 | const unsigned char* nextObject(int level, const unsigned char* start, const unsigned char* end, SkPdfNativeObject* token, SkPdfAllocator* allocator, SkPdfNativeDoc* doc GET_TRACK_STREAM) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 809 | const unsigned char* current; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 810 | |
| 811 | // skip white spaces |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 812 | start = skipPdfWhiteSpaces(level, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 813 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 814 | current = endOfPdfToken(level, start, end); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 815 | |
| 816 | // no token, len would be 0 |
| 817 | if (current == start) { |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | int tokenLen = current - start; |
| 822 | |
| 823 | if (tokenLen == 1) { |
| 824 | // start array |
| 825 | switch (*start) { |
| 826 | case kOpenedSquareBracket_PdfDelimiter: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 827 | //*start = '\0'; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 828 | return readArray(level + 1, current, end, token, allocator, doc PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 829 | |
| 830 | case kOpenedRoundBracket_PdfDelimiter: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 831 | //*start = '\0'; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 832 | return readString(level, start + 1, end, token, allocator PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 833 | |
| 834 | case kOpenedInequityBracket_PdfDelimiter: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 835 | //*start = '\0'; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 836 | if (end > start + 1 && start[1] == kOpenedInequityBracket_PdfDelimiter) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 837 | //start[1] = '\0'; // optional |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 838 | // TODO(edisonn): pass here the length somehow? |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 839 | return readDictionary(level + 1, start + 2, end, token, allocator, doc PUT_TRACK_STREAM_ARGS); // skip << |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 840 | } else { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 841 | return readHexString(level, start + 1, end, token, allocator PUT_TRACK_STREAM_ARGS); // skip < |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | case kNamed_PdfDelimiter: |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 845 | //*start = '\0'; |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 846 | return readName(level, start + 1, end, token, allocator PUT_TRACK_STREAM_ARGS); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 847 | |
| 848 | // TODO(edisonn): what to do curly brackets? read spec! |
| 849 | case kOpenedCurlyBracket_PdfDelimiter: |
| 850 | default: |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | SkASSERT(!isPdfWhiteSpace(*start)); |
| 855 | if (isPdfDelimiter(*start)) { |
| 856 | // TODO(edisonn): how stream ] } > ) will be handled? |
| 857 | // for now ignore, and it will become a keyword to be ignored |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | if (tokenLen == 4 && start[0] == 'n' && start[1] == 'u' && start[2] == 'l' && start[3] == 'l') { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 862 | SkPdfNativeObject::makeNull(token PUT_TRACK_STREAM(start, start + 4)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 863 | return current; |
| 864 | } |
| 865 | |
| 866 | if (tokenLen == 4 && start[0] == 't' && start[1] == 'r' && start[2] == 'u' && start[3] == 'e') { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 867 | SkPdfNativeObject::makeBoolean(true, token PUT_TRACK_STREAM(start, start + 4)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 868 | return current; |
| 869 | } |
| 870 | |
edisonn@google.com | f111a4b | 2013-07-31 18:22:36 +0000 | [diff] [blame] | 871 | if (tokenLen == 5 && start[0] == 'f' && start[1] == 'a' && start[2] == 'l' && start[3] == 's' && start[4] == 'e') { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 872 | SkPdfNativeObject::makeBoolean(false, token PUT_TRACK_STREAM(start, start + 5)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 873 | return current; |
| 874 | } |
| 875 | |
| 876 | if (isPdfNumeric(*start)) { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 877 | SkPdfNativeObject::makeNumeric(start, current, token PUT_TRACK_STREAM(start, current)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 878 | } else { |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 879 | SkPdfNativeObject::makeKeyword(start, current, token PUT_TRACK_STREAM(start, current)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 880 | } |
| 881 | return current; |
| 882 | } |
| 883 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 884 | SkPdfNativeObject* SkPdfAllocator::allocBlock() { |
| 885 | fSizeInBytes += BUFFER_SIZE * sizeof(SkPdfNativeObject); |
| 886 | return new SkPdfNativeObject[BUFFER_SIZE]; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | SkPdfAllocator::~SkPdfAllocator() { |
| 890 | for (int i = 0 ; i < fHandles.count(); i++) { |
| 891 | free(fHandles[i]); |
| 892 | } |
| 893 | for (int i = 0 ; i < fHistory.count(); i++) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 894 | for (int j = 0 ; j < BUFFER_SIZE; j++) { |
| 895 | fHistory[i][j].reset(); |
| 896 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 897 | delete[] fHistory[i]; |
| 898 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 899 | for (int j = 0 ; j < BUFFER_SIZE; j++) { |
| 900 | fCurrent[j].reset(); |
| 901 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 902 | delete[] fCurrent; |
| 903 | } |
| 904 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 905 | SkPdfNativeObject* SkPdfAllocator::allocObject() { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 906 | if (fCurrentUsed >= BUFFER_SIZE) { |
| 907 | fHistory.push(fCurrent); |
| 908 | fCurrent = allocBlock(); |
| 909 | fCurrentUsed = 0; |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 910 | fSizeInBytes += sizeof(SkPdfNativeObject*); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 911 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 912 | fCurrentUsed++; |
| 913 | return &fCurrent[fCurrentUsed - 1]; |
| 914 | } |
| 915 | |
| 916 | // TODO(edisonn): perf: do no copy the buffers, but use them, and mark cache the result, so there is no need of a second pass |
edisonn@google.com | 33f11b6 | 2013-08-14 21:35:27 +0000 | [diff] [blame] | 917 | SkPdfNativeTokenizer::SkPdfNativeTokenizer(SkPdfNativeObject* objWithStream, SkPdfAllocator* allocator, SkPdfNativeDoc* doc) : fDoc(doc), fAllocator(allocator), fUncompressedStream(NULL), fUncompressedStreamEnd(NULL), fEmpty(false), fHasPutBack(false) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 918 | const unsigned char* buffer = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 919 | size_t len = 0; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 920 | objWithStream->GetFilteredStreamRef(&buffer, &len); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 921 | // TODO(edisonn): hack, find end of object |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 922 | char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj"); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 923 | if (endobj) { |
| 924 | len = endobj - (char*)buffer + strlen("endobj"); |
| 925 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 926 | fUncompressedStreamStart = fUncompressedStream = buffer; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 927 | fUncompressedStreamEnd = fUncompressedStream + len; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 928 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 929 | |
edisonn@google.com | 33f11b6 | 2013-08-14 21:35:27 +0000 | [diff] [blame] | 930 | SkPdfNativeTokenizer::SkPdfNativeTokenizer(const unsigned char* buffer, int len, SkPdfAllocator* allocator, SkPdfNativeDoc* doc) : fDoc(doc), fAllocator(allocator), fEmpty(false), fHasPutBack(false) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 931 | // TODO(edisonn): hack, find end of object |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 932 | char* endobj = strrstrk((char*)buffer, (char*)buffer + len, "endobj"); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 933 | if (endobj) { |
| 934 | len = endobj - (char*)buffer + strlen("endobj"); |
| 935 | } |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 936 | fUncompressedStreamStart = fUncompressedStream = buffer; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 937 | fUncompressedStreamEnd = fUncompressedStream + len; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | SkPdfNativeTokenizer::~SkPdfNativeTokenizer() { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 944 | SkPdfNativeObject obj; |
edisonn@google.com | 91ce698 | 2013-08-05 20:45:40 +0000 | [diff] [blame] | 945 | #ifdef PDF_TRACE_READ_TOKEN |
| 946 | static int read_op = 0; |
edisonn@google.com | 91ce698 | 2013-08-05 20:45:40 +0000 | [diff] [blame] | 947 | #endif |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 948 | token->fKeyword = NULL; |
| 949 | token->fObject = NULL; |
| 950 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 951 | fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompressedStreamEnd); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 952 | if (fUncompressedStream >= fUncompressedStreamEnd) { |
| 953 | return false; |
| 954 | } |
| 955 | |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 956 | fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStreamEnd, &obj, fAllocator, fDoc PUT_TRACK_STREAM_ARGS_EXPL2(fStreamId, fUncompressedStreamStart)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 957 | |
| 958 | // If it is a keyword, we will only get the pointer of the string |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 959 | if (obj.type() == SkPdfNativeObject::kKeyword_PdfObjectType) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 960 | token->fKeyword = obj.c_str(); |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 961 | token->fKeywordLength = obj.lenstr(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 962 | token->fType = kKeyword_TokenType; |
| 963 | } else { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 964 | SkPdfNativeObject* pobj = fAllocator->allocObject(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 965 | *pobj = obj; |
| 966 | token->fObject = pobj; |
| 967 | token->fType = kObject_TokenType; |
| 968 | } |
| 969 | |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 970 | #ifdef PDF_TRACE_READ_TOKEN |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 971 | read_op++; |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 972 | #if 0 |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 973 | if (548 == read_op) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 974 | printf("break;\n"); |
| 975 | } |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 976 | #endif |
edisonn@google.com | e91260c | 2013-09-04 17:29:06 +0000 | [diff] [blame] | 977 | printf("%i READ %s %s\n", read_op, token->fType == kKeyword_TokenType ? "Keyword" : "Object", token->fKeyword ? SkString(token->fKeyword, token->fKeywordLength).c_str() : token->fObject->toString().c_str()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 978 | #endif |
| 979 | |
| 980 | return true; |
| 981 | } |
| 982 | |
| 983 | void SkPdfNativeTokenizer::PutBack(PdfToken token) { |
| 984 | SkASSERT(!fHasPutBack); |
| 985 | fHasPutBack = true; |
| 986 | fPutBack = token; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 987 | #ifdef PDF_TRACE_READ_TOKEN |
edisonn@google.com | e91260c | 2013-09-04 17:29:06 +0000 | [diff] [blame] | 988 | printf("PUT_BACK %s %s\n", token.fType == kKeyword_TokenType ? "Keyword" : "Object", token.fKeyword ? SkString(token.fKeyword, token.fKeywordLength).c_str(): token.fObject->toString().c_str()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 989 | #endif |
| 990 | } |
| 991 | |
| 992 | bool SkPdfNativeTokenizer::readToken(PdfToken* token) { |
| 993 | if (fHasPutBack) { |
| 994 | *token = fPutBack; |
| 995 | fHasPutBack = false; |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 996 | #ifdef PDF_TRACE_READ_TOKEN |
edisonn@google.com | e91260c | 2013-09-04 17:29:06 +0000 | [diff] [blame] | 997 | printf("READ_BACK %s %s\n", token->fType == kKeyword_TokenType ? "Keyword" : "Object", token->fKeyword ? SkString(token->fKeyword, token->fKeywordLength).c_str() : token->fObject->toString().c_str()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 998 | #endif |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | if (fEmpty) { |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1003 | #ifdef PDF_TRACE_READ_TOKEN |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1004 | printf("EMPTY TOKENIZER\n"); |
| 1005 | #endif |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | return readTokenCore(token); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1010 | } |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1011 | |
| 1012 | #define DECLARE_PDF_NAME(longName) SkPdfName longName((char*)#longName) |
| 1013 | |
| 1014 | // keys |
| 1015 | DECLARE_PDF_NAME(BitsPerComponent); |
| 1016 | DECLARE_PDF_NAME(ColorSpace); |
| 1017 | DECLARE_PDF_NAME(Decode); |
| 1018 | DECLARE_PDF_NAME(DecodeParms); |
| 1019 | DECLARE_PDF_NAME(Filter); |
| 1020 | DECLARE_PDF_NAME(Height); |
| 1021 | DECLARE_PDF_NAME(ImageMask); |
| 1022 | DECLARE_PDF_NAME(Intent); // PDF 1.1 - the key, or the abreviations? |
| 1023 | DECLARE_PDF_NAME(Interpolate); |
| 1024 | DECLARE_PDF_NAME(Width); |
| 1025 | |
| 1026 | // values |
| 1027 | DECLARE_PDF_NAME(DeviceGray); |
| 1028 | DECLARE_PDF_NAME(DeviceRGB); |
| 1029 | DECLARE_PDF_NAME(DeviceCMYK); |
| 1030 | DECLARE_PDF_NAME(Indexed); |
| 1031 | DECLARE_PDF_NAME(ASCIIHexDecode); |
| 1032 | DECLARE_PDF_NAME(ASCII85Decode); |
| 1033 | DECLARE_PDF_NAME(LZWDecode); |
| 1034 | DECLARE_PDF_NAME(FlateDecode); // PDF 1.2 |
| 1035 | DECLARE_PDF_NAME(RunLengthDecode); |
| 1036 | DECLARE_PDF_NAME(CCITTFaxDecode); |
| 1037 | DECLARE_PDF_NAME(DCTDecode); |
| 1038 | |
| 1039 | #define HANDLE_NAME_ABBR(obj,longName,shortName) if (obj->isName(#shortName)) return &longName; |
| 1040 | |
| 1041 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1042 | static SkPdfNativeObject* inlineImageKeyAbbreviationExpand(SkPdfNativeObject* key) { |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1043 | if (!key || !key->isName()) { |
| 1044 | return key; |
| 1045 | } |
| 1046 | |
| 1047 | // TODO(edisonn): use autogenerated code! |
| 1048 | HANDLE_NAME_ABBR(key, BitsPerComponent, BPC); |
| 1049 | HANDLE_NAME_ABBR(key, ColorSpace, CS); |
| 1050 | HANDLE_NAME_ABBR(key, Decode, D); |
| 1051 | HANDLE_NAME_ABBR(key, DecodeParms, DP); |
| 1052 | HANDLE_NAME_ABBR(key, Filter, F); |
| 1053 | HANDLE_NAME_ABBR(key, Height, H); |
| 1054 | HANDLE_NAME_ABBR(key, ImageMask, IM); |
| 1055 | // HANDLE_NAME_ABBR(key, Intent, ); |
| 1056 | HANDLE_NAME_ABBR(key, Interpolate, I); |
| 1057 | HANDLE_NAME_ABBR(key, Width, W); |
| 1058 | |
| 1059 | return key; |
| 1060 | } |
| 1061 | |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1062 | static SkPdfNativeObject* inlineImageValueAbbreviationExpand(SkPdfNativeObject* value) { |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1063 | if (!value || !value->isName()) { |
| 1064 | return value; |
| 1065 | } |
| 1066 | |
| 1067 | // TODO(edisonn): use autogenerated code! |
| 1068 | HANDLE_NAME_ABBR(value, DeviceGray, G); |
| 1069 | HANDLE_NAME_ABBR(value, DeviceRGB, RGB); |
| 1070 | HANDLE_NAME_ABBR(value, DeviceCMYK, CMYK); |
| 1071 | HANDLE_NAME_ABBR(value, Indexed, I); |
| 1072 | HANDLE_NAME_ABBR(value, ASCIIHexDecode, AHx); |
| 1073 | HANDLE_NAME_ABBR(value, ASCII85Decode, A85); |
| 1074 | HANDLE_NAME_ABBR(value, LZWDecode, LZW); |
| 1075 | HANDLE_NAME_ABBR(value, FlateDecode, Fl); // (PDF 1.2) |
| 1076 | HANDLE_NAME_ABBR(value, RunLengthDecode, RL); |
| 1077 | HANDLE_NAME_ABBR(value, CCITTFaxDecode, CCF); |
| 1078 | HANDLE_NAME_ABBR(value, DCTDecode, DCT); |
| 1079 | |
| 1080 | return value; |
| 1081 | } |
| 1082 | |
| 1083 | SkPdfImageDictionary* SkPdfNativeTokenizer::readInlineImage() { |
| 1084 | // BI already processed |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1085 | fUncompressedStream = skipPdfWhiteSpaces(0, fUncompressedStream, fUncompressedStreamEnd); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1086 | if (fUncompressedStream >= fUncompressedStreamEnd) { |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | SkPdfImageDictionary* inlineImage = (SkPdfImageDictionary*)fAllocator->allocObject(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 1091 | SkPdfNativeObject::makeEmptyDictionary(inlineImage PUT_TRACK_STREAM_ARGS_EXPL(fStreamId, fUncompressedStream - fUncompressedStreamStart, fUncompressedStream - fUncompressedStreamStart)); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1092 | |
| 1093 | while (fUncompressedStream < fUncompressedStreamEnd) { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1094 | SkPdfNativeObject* key = fAllocator->allocObject(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 1095 | fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStreamEnd, key, fAllocator, fDoc PUT_TRACK_STREAM_ARGS_EXPL2(fStreamId, fUncompressedStreamStart)); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1096 | |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 1097 | if (key->isKeyword() && key->lenstr() == 2 && key->c_str()[0] == 'I' && key->c_str()[1] == 'D') { // ID |
edisonn@google.com | 2ccc3af | 2013-07-23 17:43:18 +0000 | [diff] [blame] | 1098 | fUncompressedStream = readInlineImageStream(0, fUncompressedStream, fUncompressedStreamEnd, inlineImage, fDoc); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1099 | return inlineImage; |
| 1100 | } else { |
edisonn@google.com | 3aa3555 | 2013-08-14 18:26:20 +0000 | [diff] [blame] | 1101 | SkPdfNativeObject* obj = fAllocator->allocObject(); |
edisonn@google.com | bca421b | 2013-09-05 20:00:21 +0000 | [diff] [blame^] | 1102 | fUncompressedStream = nextObject(0, fUncompressedStream, fUncompressedStreamEnd, obj, fAllocator, fDoc PUT_TRACK_STREAM_ARGS_EXPL2(fStreamId, fUncompressedStreamStart)); |
edisonn@google.com | 78b38b1 | 2013-07-15 18:20:58 +0000 | [diff] [blame] | 1103 | // TODO(edisonn): perf maybe we should not expand abreviation like this |
| 1104 | inlineImage->set(inlineImageKeyAbbreviationExpand(key), |
| 1105 | inlineImageValueAbbreviationExpand(obj)); |
| 1106 | } |
| 1107 | } |
| 1108 | // TODO(edisonn): report end of data with inline image without an EI |
| 1109 | return inlineImage; |
| 1110 | } |