Home | History | Annotate | Download | only in doc
      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  */
      7 
      8 #include "SkDocument.h"
      9 #include "SkPDFDocument.h"
     10 #include "SkPDFDeviceFlattener.h"
     11 
     12 class SkDocument_PDF : public SkDocument {
     13 public:
     14     SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*))
     15             : SkDocument(stream, doneProc) {
     16         fDoc = SkNEW(SkPDFDocument);
     17         fCanvas = NULL;
     18         fDevice = NULL;
     19     }
     20 
     21     virtual ~SkDocument_PDF() {
     22         // subclasses must call close() in their destructors
     23         this->close();
     24     }
     25 
     26 protected:
     27     virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
     28                                   const SkRect& trimBox) SK_OVERRIDE {
     29         SkASSERT(NULL == fCanvas);
     30         SkASSERT(NULL == fDevice);
     31 
     32         SkSize mediaBoxSize;
     33         mediaBoxSize.set(width, height);
     34 
     35         fDevice = SkNEW_ARGS(SkPDFDeviceFlattener, (mediaBoxSize, &trimBox));
     36         fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
     37         return fCanvas;
     38     }
     39 
     40     virtual void onEndPage() SK_OVERRIDE {
     41         SkASSERT(fCanvas);
     42         SkASSERT(fDevice);
     43 
     44         fCanvas->flush();
     45         fDoc->appendPage(fDevice);
     46 
     47         fCanvas->unref();
     48         fDevice->unref();
     49 
     50         fCanvas = NULL;
     51         fDevice = NULL;
     52     }
     53 
     54     virtual void onClose(SkWStream* stream) SK_OVERRIDE {
     55         SkASSERT(NULL == fCanvas);
     56         SkASSERT(NULL == fDevice);
     57 
     58         fDoc->emitPDF(stream);
     59         SkDELETE(fDoc);
     60         fDoc = NULL;
     61     }
     62 
     63 private:
     64     SkPDFDocument*  fDoc;
     65     SkPDFDeviceFlattener* fDevice;
     66     SkCanvas*       fCanvas;
     67 };
     68 
     69 ///////////////////////////////////////////////////////////////////////////////
     70 
     71 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*)) {
     72     return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done)) : NULL;
     73 }
     74 
     75 static void delete_wstream(SkWStream* stream) {
     76     SkDELETE(stream);
     77 }
     78 
     79 SkDocument* SkDocument::CreatePDF(const char path[]) {
     80     SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
     81     if (!stream->isValid()) {
     82         SkDELETE(stream);
     83         return NULL;
     84     }
     85     return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream));
     86 }
     87