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

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

Issue 18435007: pdf viewer: refactor and fix a bug (SkPdfobject should not reset on destruct) (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 #include "SkPdfNativeTokenizer.h" 2 #include "SkPdfNativeTokenizer.h"
3 #include "SkPdfObject.h" 3 #include "SkPdfObject.h"
4 #include "SkPdfConfig.h" 4 #include "SkPdfConfig.h"
5 5
6 #include "SkPdfStreamCommonDictionary_autogen.h" 6 #include "SkPdfStreamCommonDictionary_autogen.h"
7 7
8 static unsigned char* skipPdfWhiteSpaces(unsigned char* start, unsigned char* en d) { 8 static unsigned char* skipPdfWhiteSpaces(unsigned char* start, unsigned char* en d) {
9 while (start < end && isPdfWhiteSpace(*start)) { 9 while (start < end && isPdfWhiteSpace(*start)) {
10 if (*start == kComment_PdfDelimiter) { 10 if (*start == kComment_PdfDelimiter) {
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 649
650 SkPdfObject* SkPdfAllocator::allocBlock() { 650 SkPdfObject* SkPdfAllocator::allocBlock() {
651 return new SkPdfObject[BUFFER_SIZE]; 651 return new SkPdfObject[BUFFER_SIZE];
652 } 652 }
653 653
654 SkPdfAllocator::~SkPdfAllocator() { 654 SkPdfAllocator::~SkPdfAllocator() {
655 for (int i = 0 ; i < fHandles.count(); i++) { 655 for (int i = 0 ; i < fHandles.count(); i++) {
656 free(fHandles[i]); 656 free(fHandles[i]);
657 } 657 }
658 for (int i = 0 ; i < fHistory.count(); i++) { 658 for (int i = 0 ; i < fHistory.count(); i++) {
659 for (int j = 0 ; j < BUFFER_SIZE; j++) {
660 fHistory[i][j].reset();
661 }
659 delete[] fHistory[i]; 662 delete[] fHistory[i];
660 } 663 }
664 for (int j = 0 ; j < BUFFER_SIZE; j++) {
665 fCurrent[j].reset();
666 }
661 delete[] fCurrent; 667 delete[] fCurrent;
662 } 668 }
663 669
664 SkPdfObject* SkPdfAllocator::allocObject() { 670 SkPdfObject* SkPdfAllocator::allocObject() {
665 if (fCurrentUsed >= BUFFER_SIZE) { 671 if (fCurrentUsed >= BUFFER_SIZE) {
666 fHistory.push(fCurrent); 672 fHistory.push(fCurrent);
667 fCurrent = allocBlock(); 673 fCurrent = allocBlock();
668 fCurrentUsed = 0; 674 fCurrentUsed = 0;
669 } 675 }
670
671 fCurrentUsed++; 676 fCurrentUsed++;
672 return &fCurrent[fCurrentUsed - 1]; 677 return &fCurrent[fCurrentUsed - 1];
673 } 678 }
674 679
675 // 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 680 // 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
676 SkPdfNativeTokenizer::SkPdfNativeTokenizer(SkPdfObject* objWithStream, const SkP dfMapper* mapper, SkPdfAllocator* allocator) : fMapper(mapper), fAllocator(alloc ator), fUncompressedStream(NULL), fUncompressedStreamEnd(NULL), fEmpty(false), f HasPutBack(false) { 681 SkPdfNativeTokenizer::SkPdfNativeTokenizer(SkPdfObject* objWithStream, const SkP dfMapper* mapper, SkPdfAllocator* allocator) : fMapper(mapper), fAllocator(alloc ator), fUncompressedStream(NULL), fUncompressedStreamEnd(NULL), fEmpty(false), f HasPutBack(false) {
677 unsigned char* buffer = NULL; 682 unsigned char* buffer = NULL;
678 size_t len = 0; 683 size_t len = 0;
679 objWithStream->GetFilteredStreamRef(&buffer, &len, fAllocator); 684 objWithStream->GetFilteredStreamRef(&buffer, &len, fAllocator);
685 // TODO(edisonn): hack, find end of object
686 // char* endobj = strstr((char*)buffer, "endobj");
687 // if (endobj) {
688 // len = endobj - (char*)buffer + strlen("endobj");
689 // }
680 fUncompressedStreamStart = fUncompressedStream = (unsigned char*)fAllocator- >alloc(len); 690 fUncompressedStreamStart = fUncompressedStream = (unsigned char*)fAllocator- >alloc(len);
681 fUncompressedStreamEnd = fUncompressedStream + len; 691 fUncompressedStreamEnd = fUncompressedStream + len;
682 memcpy(fUncompressedStream, buffer, len);} 692 memcpy(fUncompressedStream, buffer, len);
693 }
683 694
684 SkPdfNativeTokenizer::SkPdfNativeTokenizer(unsigned char* buffer, int len, const SkPdfMapper* mapper, SkPdfAllocator* allocator) : fMapper(mapper), fAllocator(a llocator), fEmpty(false), fHasPutBack(false) { 695 SkPdfNativeTokenizer::SkPdfNativeTokenizer(unsigned char* buffer, int len, const SkPdfMapper* mapper, SkPdfAllocator* allocator) : fMapper(mapper), fAllocator(a llocator), fEmpty(false), fHasPutBack(false) {
696 // TODO(edisonn): hack, find end of object
697 // char* endobj = strstr((char*)buffer, "endobj");
698 // if (endobj) {
699 // len = endobj - (char*)buffer + strlen("endobj");
700 // }
685 fUncompressedStreamStart = fUncompressedStream = (unsigned char*)fAllocator- >alloc(len); 701 fUncompressedStreamStart = fUncompressedStream = (unsigned char*)fAllocator- >alloc(len);
686 fUncompressedStreamEnd = fUncompressedStream + len; 702 fUncompressedStreamEnd = fUncompressedStream + len;
687 memcpy(fUncompressedStream, buffer, len); 703 memcpy(fUncompressedStream, buffer, len);
688 } 704 }
689 705
690 SkPdfNativeTokenizer::~SkPdfNativeTokenizer() { 706 SkPdfNativeTokenizer::~SkPdfNativeTokenizer() {
691 // free the unparsed stream, we don't need it.
692 // the parsed one is locked as it contains the strings and keywords referenc ed in objects
693 if (fUncompressedStream) {
694 void* dummy = realloc(fUncompressedStreamStart, fUncompressedStream - fU ncompressedStreamStart);
695 //SkASSERT(dummy == fUncompressedStreamStart);
696 fUncompressedStreamStart = (unsigned char*)dummy; // suppress compiler warning
697 } else {
698 SkASSERT(false);
699 }
700 } 707 }
701 708
702 bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) { 709 bool SkPdfNativeTokenizer::readTokenCore(PdfToken* token) {
703 token->fKeyword = NULL; 710 token->fKeyword = NULL;
704 token->fObject = NULL; 711 token->fObject = NULL;
705 712
706 fUncompressedStream = skipPdfWhiteSpaces(fUncompressedStream, fUncompressedS treamEnd); 713 fUncompressedStream = skipPdfWhiteSpaces(fUncompressedStream, fUncompressedS treamEnd);
707 if (fUncompressedStream >= fUncompressedStreamEnd) { 714 if (fUncompressedStream >= fUncompressedStreamEnd) {
708 return false; 715 return false;
709 } 716 }
710 717
711 SkPdfObject obj; 718 SkPdfObject obj;
712 fUncompressedStream = nextObject(fUncompressedStream, fUncompressedStreamEnd , &obj, fAllocator); 719 fUncompressedStream = nextObject(fUncompressedStream, fUncompressedStreamEnd , &obj, fAllocator);
713 720
714 // If it is a keyword, we will only get the pointer of the string 721 // If it is a keyword, we will only get the pointer of the string
715 if (obj.type() == SkPdfObject::kKeyword_PdfObjectType) { 722 if (obj.type() == SkPdfObject::kKeyword_PdfObjectType) {
716 token->fKeyword = obj.c_str(); 723 token->fKeyword = obj.c_str();
717 token->fKeywordLength = obj.len(); 724 token->fKeywordLength = obj.len();
718 token->fType = kKeyword_TokenType; 725 token->fType = kKeyword_TokenType;
719 } else { 726 } else {
720 SkPdfObject* pobj = fAllocator->allocObject(); 727 SkPdfObject* pobj = fAllocator->allocObject();
721 *pobj = obj; 728 *pobj = obj;
722 token->fObject = pobj; 729 token->fObject = pobj;
723 token->fType = kObject_TokenType; 730 token->fType = kObject_TokenType;
724 } 731 }
725 732
726 #ifdef PDF_TRACE 733 #ifdef PDF_TRACE
727 static int read_op = 0; 734 static int read_op = 0;
728 read_op++; 735 read_op++;
729 if (182749 == read_op) { 736 if (548 == read_op) {
730 printf("break;\n"); 737 printf("break;\n");
731 } 738 }
732 printf("%i READ %s %s\n", read_op, token->fType == kKeyword_TokenType ? "Key word" : "Object", token->fKeyword ? std::string(token->fKeyword, token->fKeyword Length).c_str() : token->fObject->toString().c_str()); 739 printf("%i READ %s %s\n", read_op, token->fType == kKeyword_TokenType ? "Key word" : "Object", token->fKeyword ? std::string(token->fKeyword, token->fKeyword Length).c_str() : token->fObject->toString().c_str());
733 #endif 740 #endif
734 741
735 return true; 742 return true;
736 } 743 }
737 744
738 void SkPdfNativeTokenizer::PutBack(PdfToken token) { 745 void SkPdfNativeTokenizer::PutBack(PdfToken token) {
739 SkASSERT(!fHasPutBack); 746 SkASSERT(!fHasPutBack);
(...skipping 16 matching lines...) Expand all
756 763
757 if (fEmpty) { 764 if (fEmpty) {
758 #ifdef PDF_TRACE 765 #ifdef PDF_TRACE
759 printf("EMPTY TOKENIZER\n"); 766 printf("EMPTY TOKENIZER\n");
760 #endif 767 #endif
761 return false; 768 return false;
762 } 769 }
763 770
764 return readTokenCore(token); 771 return readTokenCore(token);
765 } 772 }
773
OLDNEW
« no previous file with comments | « experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp ('k') | experimental/PdfViewer/pdfparser/native/SkPdfObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698