Remove SkPdfNativeDoc::tokenizerOfStream.
Instead, initialize an SkPdfNativeTokenizer directly on the stack.
Fixes a memory leak in SkPdfFont.
R=mtklein@google.com
Review URL: https://codereview.chromium.org/100323003
git-svn-id: http://skia.googlecode.com/svn/trunk@12462 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/PdfViewer/SkPdfFont.cpp b/experimental/PdfViewer/SkPdfFont.cpp
index 72fd5b5..548a17e 100644
--- a/experimental/PdfViewer/SkPdfFont.cpp
+++ b/experimental/PdfViewer/SkPdfFont.cpp
@@ -336,7 +336,7 @@
if (stream) {
// Since font will be cached, the font has to sit in the per doc allocator, not to be
// freed after the page is done drawing.
- SkPdfNativeTokenizer* tokenizer = fParsed->tokenizerOfStream(stream, parsed->allocator());
+ SkPdfNativeTokenizer tokenizer(stream, parsed->allocator(), parsed);
PdfToken token;
fCMapEncoding = new unsigned short[256 * 256];
@@ -352,16 +352,16 @@
//<0000> <005E> <0020>
//<005F> <0061> [<00660066> <00660069> <00660066006C>]
- while (tokenizer->readToken(&token)) {
+ while (tokenizer.readToken(&token)) {
if (tokenIsKeyword(token, "begincodespacerange")) {
- while (tokenizer->readToken(&token) &&
+ while (tokenizer.readToken(&token) &&
!tokenIsKeyword(token, "endcodespacerange")) {
-// tokenizer->PutBack(token);
-// tokenizer->readToken(&token);
+// tokenizer.PutBack(token);
+// tokenizer.readToken(&token);
// TODO(edisonn): check token type! ignore/report errors.
int start = skstoi(token.fObject);
- tokenizer->readToken(&token);
+ tokenizer.readToken(&token);
int end = skstoi(token.fObject);
for (int i = start; i <= end; i++) {
fCMapEncodingFlag[i] |= 1;
@@ -370,11 +370,11 @@
}
if (tokenIsKeyword(token, "beginbfchar")) {
- while (tokenizer->readToken(&token) && !tokenIsKeyword(token, "endbfchar")) {
-// tokenizer->PutBack(token);
-// tokenizer->readToken(&token);
+ while (tokenizer.readToken(&token) && !tokenIsKeyword(token, "endbfchar")) {
+// tokenizer.PutBack(token);
+// tokenizer.readToken(&token);
int from = skstoi(token.fObject);
- tokenizer->readToken(&token);
+ tokenizer.readToken(&token);
int to = skstoi(token.fObject);
fCMapEncodingFlag[from] |= 2;
@@ -383,21 +383,21 @@
}
if (tokenIsKeyword(token, "beginbfrange")) {
- while (tokenizer->readToken(&token) && !tokenIsKeyword(token, "endbfrange")) {
-// tokenizer->PutBack(token);
-// tokenizer->readToken(&token);
+ while (tokenizer.readToken(&token) && !tokenIsKeyword(token, "endbfrange")) {
+// tokenizer.PutBack(token);
+// tokenizer.readToken(&token);
int start = skstoi(token.fObject);
- tokenizer->readToken(&token);
+ tokenizer.readToken(&token);
int end = skstoi(token.fObject);
- tokenizer->readToken(&token); // [ or just an array directly?
+ tokenizer.readToken(&token); // [ or just an array directly?
// do not putback, we will reuse the read. See next commented read.
-// tokenizer->PutBack(token);
+// tokenizer.PutBack(token);
// TODO(edisonn): read spec: any string or only hex string?
if (token.fType == kObject_TokenType && token.fObject->isAnyString()) {
-// tokenizer->readToken(&token);
+// tokenizer.readToken(&token);
int value = skstoi(token.fObject);
for (int i = start; i <= end; i++) {
@@ -409,14 +409,14 @@
// read one string
} else if (token.fType == kObject_TokenType && token.fObject->isArray()) {
-// tokenizer->readToken(&token);
+// tokenizer.readToken(&token);
// read array
for (unsigned int i = 0; i < token.fObject->size(); i++) {
fCMapEncodingFlag[start + i] |= 2;
fCMapEncoding[start + i] = skstoi((*token.fObject)[i]);
}
} else {
- tokenizer->PutBack(token);
+ tokenizer.PutBack(token);
}
}
}
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.cpp b/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.cpp
index 8c77637..2f0c18c 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.cpp
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.cpp
@@ -479,15 +479,6 @@
return SkRect::MakeEmpty();
}
-SkPdfNativeTokenizer* SkPdfNativeDoc::tokenizerOfStream(SkPdfNativeObject* stream,
- SkPdfAllocator* allocator) {
- if (stream == NULL) {
- return NULL;
- }
-
- return new SkPdfNativeTokenizer(stream, allocator, this);
-}
-
size_t SkPdfNativeDoc::objects() const {
return fObjects.count();
}
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.h b/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.h
index ea04616..b92c872 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.h
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfNativeDoc.h
@@ -82,10 +82,6 @@
// returns the page's mediabox i points - the page physical boundaries.
SkRect MediaBox(int page);
- // Returns a tokenizer of a pdf stream. The passed allocator will be used to allocate objects
- // that are parsed. It should be destroyed after the tokenizer.
- SkPdfNativeTokenizer* tokenizerOfStream(SkPdfNativeObject* stream, SkPdfAllocator* allocator);
-
//returns objects that are references and can be queried.
size_t objects() const;
diff --git a/experimental/PdfViewer/src/SkPdfContext.cpp b/experimental/PdfViewer/src/SkPdfContext.cpp
index b033e7a..670e6ce 100644
--- a/experimental/PdfViewer/src/SkPdfContext.cpp
+++ b/experimental/PdfViewer/src/SkPdfContext.cpp
@@ -35,15 +35,14 @@
}
void SkPdfContext::parseStream(SkPdfNativeObject* stream, SkCanvas* canvas) {
- SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfStream(stream, &fTmpPageAllocator);
- if (NULL == tokenizer) {
+ if (NULL == stream) {
// Nothing to parse.
return;
}
- PdfMainLooper looper(tokenizer, this, canvas);
+
+ SkPdfNativeTokenizer tokenizer(stream, &fTmpPageAllocator, fPdfDoc);
+ PdfMainLooper looper(&tokenizer, this, canvas);
looper.loop();
- // FIXME (scroggo): Will restructure to put tokenizer on the stack.
- delete tokenizer;
}
///////////////////////////////////////////////////////////////////////////////