pdfviewer: add doPage function, cet stream from Contents
authoredisonn@google.com <edisonn@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 30 Jul 2013 13:34:10 +0000 (13:34 +0000)
committeredisonn@google.com <edisonn@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 30 Jul 2013 13:34:10 +0000 (13:34 +0000)
Review URL: https://codereview.chromium.org/21049009

git-svn-id: http://skia.googlecode.com/svn/trunk@10433 2bbb7eff-a529-9590-31e7-b0007b416f81

experimental/PdfViewer/SkPdfRenderer.cpp
experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h

index b18167ce9f944fc05502f62dfd2f1e3788a0a017..eb3695d78194e0e8ebd7a6f541b9817b88d9bccc 100644 (file)
@@ -784,6 +784,57 @@ static PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdf
     }
 }
 
+static PdfResult doPage(PdfContext* pdfContext, SkCanvas* canvas, SkPdfPageObjectDictionary* skobj) {
+    if (!skobj) {
+        return kIgnoreError_PdfResult;
+    }
+
+    if (!skobj->isContentsAStream(pdfContext->fPdfDoc)) {
+        return kNYI_PdfResult;
+    }
+
+    SkPdfStream* stream = skobj->getContentsAsStream(pdfContext->fPdfDoc);
+
+    if (!stream) {
+        return kIgnoreError_PdfResult;
+    }
+
+    if (CheckRecursiveRendering::IsInRendering(skobj)) {
+        // Oops, corrupt PDF!
+        return kIgnoreError_PdfResult;
+    }
+    CheckRecursiveRendering checkRecursion(skobj);
+
+
+    PdfOp_q(pdfContext, canvas, NULL);
+
+    canvas->save();
+
+    if (skobj->Resources(pdfContext->fPdfDoc)) {
+        pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc);
+    }
+
+    // TODO(edisonn): refactor common path with doXObject()
+    // This is a group?
+    if (skobj->has_Group()) {
+        //TransparencyGroupDictionary* ...
+    }
+
+    SkPdfNativeTokenizer* tokenizer =
+            pdfContext->fPdfDoc->tokenizerOfStream(stream, pdfContext->fTmpPageAllocator);
+    if (tokenizer != NULL) {
+        PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas);
+        looper.loop();
+        delete tokenizer;
+    }
+
+    // TODO(edisonn): should we restore the variable stack at the same state?
+    // There could be operands left, that could be consumed by a parent tokenizer when we pop.
+    canvas->restore();
+    PdfOp_Q(pdfContext, canvas, NULL);
+    return kPartial_PdfResult;
+}
+
 PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
     pdfContext->fStateStack.push(pdfContext->fGraphicsState);
     canvas->save();
@@ -2185,12 +2236,6 @@ bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) co
 
     PdfContext pdfContext(fPdfDoc);
 
-    SkPdfNativeTokenizer* tokenizer = fPdfDoc->tokenizerOfPage(page, pdfContext.fTmpPageAllocator);
-    if (!tokenizer) {
-        // TODO(edisonn): report/warning/debug
-        return false;
-    }
-
     pdfContext.fOriginalMatrix = SkMatrix::I();
     pdfContext.fGraphicsState.fResources = fPdfDoc->pageResources(page);
 
@@ -2240,15 +2285,13 @@ bool SkPdfRenderer::renderPage(int page, SkCanvas* canvas, const SkRect& dst) co
 
     canvas->setMatrix(pdfContext.fOriginalMatrix);
 
-// erase with red before?
+    doPage(&pdfContext, canvas, fPdfDoc->page(page));
+
+          // TODO(edisonn:) erase with white before draw?
 //        SkPaint paint;
-//        paint.setColor(SK_ColorRED);
+//        paint.setColor(SK_ColorWHITE);
 //        canvas->drawRect(rect, paint);
 
-    PdfMainLooper looper(NULL, tokenizer, &pdfContext, canvas);
-    looper.loop();
-
-    delete tokenizer;
 
     canvas->flush();
     return true;
index 9e89b84424c49254b004545312aeb129d51faf32..3273182269c7e070cf3013bb225fe8c7da04c10d 100644 (file)
@@ -403,7 +403,14 @@ int SkNativeParsedPDF::pages() const {
     return fPages.count();
 }
 
+SkPdfPageObjectDictionary* SkNativeParsedPDF::page(int page) {
+    SkASSERT(page >= 0 && page < fPages.count());
+    return fPages[page];
+}
+
+
 SkPdfResourceDictionary* SkNativeParsedPDF::pageResources(int page) {
+    SkASSERT(page >= 0 && page < fPages.count());
     return fPages[page]->Resources(this);
 }
 
@@ -502,8 +509,6 @@ SkPdfObject* SkNativeParsedPDF::resolveReference(const SkPdfObject* ref) {
         // TODO(edisonn): generation/updates not supported now
         //int gen = ref->referenceGeneration();
 
-        SkASSERT(!(id < 0 || id > fObjects.count()));
-
         // TODO(edisonn): verify id and gen expected
         if (id < 0 || id >= fObjects.count()) {
             // TODO(edisonn): report error/warning
index 77a98c7d04a684302a1a4d0fd3ba2abd476def0e..d073f00a30457e00267b02283ce1409c969f2512 100644 (file)
@@ -54,6 +54,7 @@ public:
 
     size_t objects() const;
     SkPdfObject* object(int i);
+    SkPdfPageObjectDictionary* page(int page);
 
     const SkPdfMapper* mapper() const;
     SkPdfAllocator* allocator() const;