OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkPdfNativeDoc.h" | 8 #include "SkPdfNativeDoc.h" |
9 #include "SkPdfNativeTokenizer.h" | 9 #include "SkPdfNativeTokenizer.h" |
10 #include "SkPdfNativeObject.h" | 10 #include "SkPdfNativeObject.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 void SkPdfNativeDoc::loadWithoutXRef() { | 170 void SkPdfNativeDoc::loadWithoutXRef() { |
171 const unsigned char* current = fFileContent; | 171 const unsigned char* current = fFileContent; |
172 const unsigned char* end = fFileContent + fContentLength; | 172 const unsigned char* end = fFileContent + fContentLength; |
173 | 173 |
174 // TODO(edisonn): read pdf version | 174 // TODO(edisonn): read pdf version |
175 current = ignoreLine(current, end); | 175 current = ignoreLine(current, end); |
176 | 176 |
177 current = skipPdfWhiteSpaces(0, current, end); | 177 current = skipPdfWhiteSpaces(0, current, end); |
178 while (current < end) { | 178 while (current < end) { |
179 SkPdfNativeObject token; | 179 SkPdfNativeObject token; |
180 current = nextObject(0, current, end, &token, NULL, NULL); | 180 current = nextObject(0, current, end, &token, NULL, NULL PUT_TRACK_STREA
M_ARGS_EXPL2(0, fFileContent)); |
181 if (token.isInteger()) { | 181 if (token.isInteger()) { |
182 int id = (int)token.intValue(); | 182 int id = (int)token.intValue(); |
183 | 183 |
184 token.reset(); | 184 token.reset(); |
185 current = nextObject(0, current, end, &token, NULL, NULL); | 185 current = nextObject(0, current, end, &token, NULL, NULL PUT_TRACK_S
TREAM_ARGS_EXPL2(0, fFileContent)); |
186 // int generation = (int)token.intValue(); // TODO(edisonn): ignore
d for now | 186 // int generation = (int)token.intValue(); // TODO(edisonn): ignore
d for now |
187 | 187 |
188 token.reset(); | 188 token.reset(); |
189 current = nextObject(0, current, end, &token, NULL, NULL); | 189 current = nextObject(0, current, end, &token, NULL, NULL PUT_TRACK_S
TREAM_ARGS_EXPL2(0, fFileContent)); |
190 // TODO(edisonn): must be obj, return error if not? ignore ? | 190 // TODO(edisonn): must be obj, return error if not? ignore ? |
191 if (!token.isKeyword("obj")) { | 191 if (!token.isKeyword("obj")) { |
192 continue; | 192 continue; |
193 } | 193 } |
194 | 194 |
195 while (fObjects.count() < id + 1) { | 195 while (fObjects.count() < id + 1) { |
196 reset(fObjects.append()); | 196 reset(fObjects.append()); |
197 } | 197 } |
198 | 198 |
199 fObjects[id].fOffset = current - fFileContent; | 199 fObjects[id].fOffset = current - fFileContent; |
200 | 200 |
201 SkPdfNativeObject* obj = fAllocator->allocObject(); | 201 SkPdfNativeObject* obj = fAllocator->allocObject(); |
202 current = nextObject(0, current, end, obj, fAllocator, this); | 202 current = nextObject(0, current, end, obj, fAllocator, this PUT_TRAC
K_STREAM_ARGS_EXPL2(0, fFileContent)); |
203 | 203 |
204 fObjects[id].fResolvedReference = obj; | 204 fObjects[id].fResolvedReference = obj; |
205 fObjects[id].fObj = obj; | 205 fObjects[id].fObj = obj; |
206 | 206 |
207 // set objects | 207 // set objects |
208 } else if (token.isKeyword("trailer")) { | 208 } else if (token.isKeyword("trailer")) { |
209 long dummy; | 209 long dummy; |
210 current = readTrailer(current, end, true, &dummy, true); | 210 current = readTrailer(current, end, true, &dummy, true); |
211 } else if (token.isKeyword("startxref")) { | 211 } else if (token.isKeyword("startxref")) { |
212 token.reset(); | 212 token.reset(); |
213 current = nextObject(0, current, end, &token, NULL, NULL); // ignor
e | 213 current = nextObject(0, current, end, &token, NULL, NULL PUT_TRACK_S
TREAM_ARGS_EXPL2(0, fFileContent)); // ignore |
214 } | 214 } |
215 | 215 |
216 current = skipPdfWhiteSpaces(0, current, end); | 216 current = skipPdfWhiteSpaces(0, current, end); |
217 } | 217 } |
218 | 218 |
219 // TODO(edisonn): hack, detect root catalog - we need to implement liniarize
d support, and remove this hack. | 219 // TODO(edisonn): hack, detect root catalog - we need to implement liniarize
d support, and remove this hack. |
220 if (!fRootCatalogRef) { | 220 if (!fRootCatalogRef) { |
221 for (unsigned int i = 0 ; i < objects(); i++) { | 221 for (unsigned int i = 0 ; i < objects(); i++) { |
222 SkPdfNativeObject* obj = object(i); | 222 SkPdfNativeObject* obj = object(i); |
223 SkPdfNativeObject* root = (obj && obj->isDictionary()) ? obj->get("R
oot") : NULL; | 223 SkPdfNativeObject* root = (obj && obj->isDictionary()) ? obj->get("R
oot") : NULL; |
(...skipping 18 matching lines...) Expand all Loading... |
242 } | 242 } |
243 | 243 |
244 // TODO(edisonn): NYI | 244 // TODO(edisonn): NYI |
245 SkPdfNativeDoc::~SkPdfNativeDoc() { | 245 SkPdfNativeDoc::~SkPdfNativeDoc() { |
246 sk_free((void*)fFileContent); | 246 sk_free((void*)fFileContent); |
247 delete fAllocator; | 247 delete fAllocator; |
248 } | 248 } |
249 | 249 |
250 const unsigned char* SkPdfNativeDoc::readCrossReferenceSection(const unsigned ch
ar* xrefStart, const unsigned char* trailerEnd) { | 250 const unsigned char* SkPdfNativeDoc::readCrossReferenceSection(const unsigned ch
ar* xrefStart, const unsigned char* trailerEnd) { |
251 SkPdfNativeObject xref; | 251 SkPdfNativeObject xref; |
252 const unsigned char* current = nextObject(0, xrefStart, trailerEnd, &xref, N
ULL, NULL); | 252 const unsigned char* current = nextObject(0, xrefStart, trailerEnd, &xref, N
ULL, NULL PUT_TRACK_STREAM_ARGS_EXPL2(0, fFileContent)); |
253 | 253 |
254 if (!xref.isKeyword("xref")) { | 254 if (!xref.isKeyword("xref")) { |
255 return trailerEnd; | 255 return trailerEnd; |
256 } | 256 } |
257 | 257 |
258 SkPdfNativeObject token; | 258 SkPdfNativeObject token; |
259 while (current < trailerEnd) { | 259 while (current < trailerEnd) { |
260 token.reset(); | 260 token.reset(); |
261 const unsigned char* previous = current; | 261 const unsigned char* previous = current; |
262 current = nextObject(0, current, trailerEnd, &token, NULL, NULL); | 262 current = nextObject(0, current, trailerEnd, &token, NULL, NULL PUT_TRAC
K_STREAM_ARGS_EXPL2(0, fFileContent)); |
263 if (!token.isInteger()) { | 263 if (!token.isInteger()) { |
264 return previous; | 264 return previous; |
265 } | 265 } |
266 | 266 |
267 int startId = (int)token.intValue(); | 267 int startId = (int)token.intValue(); |
268 token.reset(); | 268 token.reset(); |
269 current = nextObject(0, current, trailerEnd, &token, NULL, NULL); | 269 current = nextObject(0, current, trailerEnd, &token, NULL, NULL PUT_TRAC
K_STREAM_ARGS_EXPL2(0, fFileContent)); |
270 | 270 |
271 if (!token.isInteger()) { | 271 if (!token.isInteger()) { |
272 // TODO(edisonn): report/warning | 272 // TODO(edisonn): report/warning |
273 return current; | 273 return current; |
274 } | 274 } |
275 | 275 |
276 int entries = (int)token.intValue(); | 276 int entries = (int)token.intValue(); |
277 | 277 |
278 for (int i = 0; i < entries; i++) { | 278 for (int i = 0; i < entries; i++) { |
279 token.reset(); | 279 token.reset(); |
280 current = nextObject(0, current, trailerEnd, &token, NULL, NULL); | 280 current = nextObject(0, current, trailerEnd, &token, NULL, NULL PUT_
TRACK_STREAM_ARGS_EXPL2(0, fFileContent)); |
281 if (!token.isInteger()) { | 281 if (!token.isInteger()) { |
282 // TODO(edisonn): report/warning | 282 // TODO(edisonn): report/warning |
283 return current; | 283 return current; |
284 } | 284 } |
285 int offset = (int)token.intValue(); | 285 int offset = (int)token.intValue(); |
286 | 286 |
287 token.reset(); | 287 token.reset(); |
288 current = nextObject(0, current, trailerEnd, &token, NULL, NULL); | 288 current = nextObject(0, current, trailerEnd, &token, NULL, NULL PUT_
TRACK_STREAM_ARGS_EXPL2(0, fFileContent)); |
289 if (!token.isInteger()) { | 289 if (!token.isInteger()) { |
290 // TODO(edisonn): report/warning | 290 // TODO(edisonn): report/warning |
291 return current; | 291 return current; |
292 } | 292 } |
293 int generation = (int)token.intValue(); | 293 int generation = (int)token.intValue(); |
294 | 294 |
295 token.reset(); | 295 token.reset(); |
296 current = nextObject(0, current, trailerEnd, &token, NULL, NULL); | 296 current = nextObject(0, current, trailerEnd, &token, NULL, NULL PUT_
TRACK_STREAM_ARGS_EXPL2(0, fFileContent)); |
297 if (!token.isKeyword() || token.lenstr() != 1 || (*token.c_str() !=
'f' && *token.c_str() != 'n')) { | 297 if (!token.isKeyword() || token.lenstr() != 1 || (*token.c_str() !=
'f' && *token.c_str() != 'n')) { |
298 // TODO(edisonn): report/warning | 298 // TODO(edisonn): report/warning |
299 return current; | 299 return current; |
300 } | 300 } |
301 | 301 |
302 addCrossSectionInfo(startId + i, generation, offset, *token.c_str()
== 'f'); | 302 addCrossSectionInfo(startId + i, generation, offset, *token.c_str()
== 'f'); |
303 } | 303 } |
304 } | 304 } |
305 // TODO(edisonn): it should never get here? there is no trailer? | 305 // TODO(edisonn): it should never get here? there is no trailer? |
306 return current; | 306 return current; |
307 } | 307 } |
308 | 308 |
309 const unsigned char* SkPdfNativeDoc::readTrailer(const unsigned char* trailerSta
rt, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKey
word) { | 309 const unsigned char* SkPdfNativeDoc::readTrailer(const unsigned char* trailerSta
rt, const unsigned char* trailerEnd, bool storeCatalog, long* prev, bool skipKey
word) { |
310 *prev = -1; | 310 *prev = -1; |
311 | 311 |
312 const unsigned char* current = trailerStart; | 312 const unsigned char* current = trailerStart; |
313 if (!skipKeyword) { | 313 if (!skipKeyword) { |
314 SkPdfNativeObject trailerKeyword; | 314 SkPdfNativeObject trailerKeyword; |
315 // TODO(edisonn): use null allocator, and let it just fail if memory | 315 // TODO(edisonn): use null allocator, and let it just fail if memory |
316 // needs allocated (but no crash)! | 316 // needs allocated (but no crash)! |
317 current = nextObject(0, current, trailerEnd, &trailerKeyword, NULL, NULL
); | 317 current = nextObject(0, current, trailerEnd, &trailerKeyword, NULL, NULL
PUT_TRACK_STREAM_ARGS_EXPL2(0, fFileContent)); |
318 | 318 |
319 if (!trailerKeyword.isKeyword() || strlen("trailer") != trailerKeyword.l
enstr() || | 319 if (!trailerKeyword.isKeyword() || strlen("trailer") != trailerKeyword.l
enstr() || |
320 strncmp(trailerKeyword.c_str(), "trailer", strlen("trailer")) != 0)
{ | 320 strncmp(trailerKeyword.c_str(), "trailer", strlen("trailer")) != 0)
{ |
321 // TODO(edisonn): report warning, rebuild trailer from objects. | 321 // TODO(edisonn): report warning, rebuild trailer from objects. |
322 return current; | 322 return current; |
323 } | 323 } |
324 } | 324 } |
325 | 325 |
326 SkPdfNativeObject token; | 326 SkPdfNativeObject token; |
327 current = nextObject(0, current, trailerEnd, &token, fAllocator, NULL); | 327 current = nextObject(0, current, trailerEnd, &token, fAllocator, NULL PUT_TR
ACK_STREAM_ARGS_EXPL2(0, fFileContent)); |
328 if (!token.isDictionary()) { | 328 if (!token.isDictionary()) { |
329 return current; | 329 return current; |
330 } | 330 } |
331 SkPdfFileTrailerDictionary* trailer = (SkPdfFileTrailerDictionary*)&token; | 331 SkPdfFileTrailerDictionary* trailer = (SkPdfFileTrailerDictionary*)&token; |
332 if (!trailer->valid()) { | 332 if (!trailer->valid()) { |
333 return current; | 333 return current; |
334 } | 334 } |
335 | 335 |
336 if (storeCatalog) { | 336 if (storeCatalog) { |
337 SkPdfNativeObject* ref = trailer->Root(NULL); | 337 SkPdfNativeObject* ref = trailer->Root(NULL); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 const unsigned char* current = fFileContent + startOffset; | 372 const unsigned char* current = fFileContent + startOffset; |
373 const unsigned char* end = fFileContent + fContentLength; | 373 const unsigned char* end = fFileContent + fContentLength; |
374 | 374 |
375 SkPdfNativeTokenizer tokenizer(current, end - current, fAllocator, this); | 375 SkPdfNativeTokenizer tokenizer(current, end - current, fAllocator, this); |
376 | 376 |
377 SkPdfNativeObject idObj; | 377 SkPdfNativeObject idObj; |
378 SkPdfNativeObject generationObj; | 378 SkPdfNativeObject generationObj; |
379 SkPdfNativeObject objKeyword; | 379 SkPdfNativeObject objKeyword; |
380 SkPdfNativeObject* dict = fAllocator->allocObject(); | 380 SkPdfNativeObject* dict = fAllocator->allocObject(); |
381 | 381 |
382 current = nextObject(0, current, end, &idObj, NULL, NULL); | 382 current = nextObject(0, current, end, &idObj, NULL, NULL PUT_TRACK_STREAM_AR
GS_EXPL2(0, fFileContent)); |
383 if (current >= end) { | 383 if (current >= end) { |
384 // TODO(edisonn): report warning/error | 384 // TODO(edisonn): report warning/error |
385 return NULL; | 385 return NULL; |
386 } | 386 } |
387 | 387 |
388 current = nextObject(0, current, end, &generationObj, NULL, NULL); | 388 current = nextObject(0, current, end, &generationObj, NULL, NULL PUT_TRACK_S
TREAM_ARGS_EXPL2(0, fFileContent)); |
389 if (current >= end) { | 389 if (current >= end) { |
390 // TODO(edisonn): report warning/error | 390 // TODO(edisonn): report warning/error |
391 return NULL; | 391 return NULL; |
392 } | 392 } |
393 | 393 |
394 current = nextObject(0, current, end, &objKeyword, NULL, NULL); | 394 current = nextObject(0, current, end, &objKeyword, NULL, NULL PUT_TRACK_STRE
AM_ARGS_EXPL2(0, fFileContent)); |
395 if (current >= end) { | 395 if (current >= end) { |
396 // TODO(edisonn): report warning/error | 396 // TODO(edisonn): report warning/error |
397 return NULL; | 397 return NULL; |
398 } | 398 } |
399 | 399 |
400 if (!idObj.isInteger() || !generationObj.isInteger() || id != idObj.intValue
()/* || generation != generationObj.intValue()*/) { | 400 if (!idObj.isInteger() || !generationObj.isInteger() || id != idObj.intValue
()/* || generation != generationObj.intValue()*/) { |
401 // TODO(edisonn): report warning/error | 401 // TODO(edisonn): report warning/error |
402 } | 402 } |
403 | 403 |
404 if (!objKeyword.isKeyword() || strcmp(objKeyword.c_str(), "obj") != 0) { | 404 if (!objKeyword.isKeyword() || strcmp(objKeyword.c_str(), "obj") != 0) { |
405 // TODO(edisonn): report warning/error | 405 // TODO(edisonn): report warning/error |
406 } | 406 } |
407 | 407 |
408 current = nextObject(1, current, end, dict, fAllocator, this); | 408 current = nextObject(1, current, end, dict, fAllocator, this PUT_TRACK_STREA
M_ARGS_EXPL2(0, fFileContent)); |
409 | 409 |
410 // TODO(edisonn): report warning/error - verify last token is endobj | 410 // TODO(edisonn): report warning/error - verify last token is endobj |
411 | 411 |
412 return dict; | 412 return dict; |
413 } | 413 } |
414 | 414 |
415 void SkPdfNativeDoc::fillPages(SkPdfPageTreeNodeDictionary* tree) { | 415 void SkPdfNativeDoc::fillPages(SkPdfPageTreeNodeDictionary* tree) { |
416 SkPdfArray* kids = tree->Kids(this); | 416 SkPdfArray* kids = tree->Kids(this); |
417 if (kids == NULL) { | 417 if (kids == NULL) { |
418 *fPages.append() = (SkPdfPageObjectDictionary*)tree; | 418 *fPages.append() = (SkPdfPageObjectDictionary*)tree; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 | 506 |
507 return fObjects[i].fObj; | 507 return fObjects[i].fObj; |
508 } | 508 } |
509 | 509 |
510 const SkPdfMapper* SkPdfNativeDoc::mapper() const { | 510 const SkPdfMapper* SkPdfNativeDoc::mapper() const { |
511 return fMapper; | 511 return fMapper; |
512 } | 512 } |
513 | 513 |
514 SkPdfReal* SkPdfNativeDoc::createReal(double value) const { | 514 SkPdfReal* SkPdfNativeDoc::createReal(double value) const { |
515 SkPdfNativeObject* obj = fAllocator->allocObject(); | 515 SkPdfNativeObject* obj = fAllocator->allocObject(); |
516 SkPdfNativeObject::makeReal(value, obj); | 516 SkPdfNativeObject::makeReal(value, obj PUT_TRACK_PARAMETERS_SRC); |
517 return (SkPdfReal*)obj; | 517 return (SkPdfReal*)obj; |
518 } | 518 } |
519 | 519 |
520 SkPdfInteger* SkPdfNativeDoc::createInteger(int value) const { | 520 SkPdfInteger* SkPdfNativeDoc::createInteger(int value) const { |
521 SkPdfNativeObject* obj = fAllocator->allocObject(); | 521 SkPdfNativeObject* obj = fAllocator->allocObject(); |
522 SkPdfNativeObject::makeInteger(value, obj); | 522 SkPdfNativeObject::makeInteger(value, obj PUT_TRACK_PARAMETERS_SRC); |
523 return (SkPdfInteger*)obj; | 523 return (SkPdfInteger*)obj; |
524 } | 524 } |
525 | 525 |
526 SkPdfString* SkPdfNativeDoc::createString(const unsigned char* sz, size_t len) c
onst { | 526 SkPdfString* SkPdfNativeDoc::createString(const unsigned char* sz, size_t len) c
onst { |
527 SkPdfNativeObject* obj = fAllocator->allocObject(); | 527 SkPdfNativeObject* obj = fAllocator->allocObject(); |
528 SkPdfNativeObject::makeString(sz, len, obj); | 528 SkPdfNativeObject::makeString(sz, len, obj PUT_TRACK_PARAMETERS_SRC); |
529 return (SkPdfString*)obj; | 529 return (SkPdfString*)obj; |
530 } | 530 } |
531 | 531 |
532 SkPdfAllocator* SkPdfNativeDoc::allocator() const { | 532 SkPdfAllocator* SkPdfNativeDoc::allocator() const { |
533 return fAllocator; | 533 return fAllocator; |
534 } | 534 } |
535 | 535 |
536 // TODO(edisonn): fix infinite loop if ref to itself! | 536 // TODO(edisonn): fix infinite loop if ref to itself! |
537 // TODO(edisonn): perf, fix refs at load, and resolve will simply return fResolv
edReference? | 537 // TODO(edisonn): perf, fix refs at load, and resolve will simply return fResolv
edReference? |
538 SkPdfNativeObject* SkPdfNativeDoc::resolveReference(SkPdfNativeObject* ref) { | 538 SkPdfNativeObject* SkPdfNativeDoc::resolveReference(SkPdfNativeObject* ref) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 return (SkPdfNativeObject*)ref; | 585 return (SkPdfNativeObject*)ref; |
586 } | 586 } |
587 | 587 |
588 size_t SkPdfNativeDoc::bytesUsed() const { | 588 size_t SkPdfNativeDoc::bytesUsed() const { |
589 return fAllocator->bytesUsed() + | 589 return fAllocator->bytesUsed() + |
590 fContentLength + | 590 fContentLength + |
591 fObjects.count() * sizeof(PublicObjectEntry) + | 591 fObjects.count() * sizeof(PublicObjectEntry) + |
592 fPages.count() * sizeof(SkPdfPageObjectDictionary*) + | 592 fPages.count() * sizeof(SkPdfPageObjectDictionary*) + |
593 sizeof(*this); | 593 sizeof(*this); |
594 } | 594 } |
OLD | NEW |