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