1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkDocument.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkDocument_DEFINED 1.12 +#define SkDocument_DEFINED 1.13 + 1.14 +#include "SkBitmap.h" 1.15 +#include "SkPicture.h" 1.16 +#include "SkRect.h" 1.17 +#include "SkRefCnt.h" 1.18 + 1.19 +class SkCanvas; 1.20 +class SkWStream; 1.21 + 1.22 +/** SK_ScalarDefaultDPI is 72 DPI. 1.23 +*/ 1.24 +#define SK_ScalarDefaultRasterDPI 72.0f 1.25 + 1.26 +/** 1.27 + * High-level API for creating a document-based canvas. To use.. 1.28 + * 1.29 + * 1. Create a document, specifying a stream to store the output. 1.30 + * 2. For each "page" of content: 1.31 + * a. canvas = doc->beginPage(...) 1.32 + * b. draw_my_content(canvas); 1.33 + * c. doc->endPage(); 1.34 + * 3. Close the document with doc->close(). 1.35 + */ 1.36 +class SkDocument : public SkRefCnt { 1.37 +public: 1.38 + SK_DECLARE_INST_COUNT(SkDocument) 1.39 + 1.40 + /** 1.41 + * Create a PDF-backed document, writing the results into a file. 1.42 + * If there is an error trying to create the doc, returns NULL. 1.43 + * encoder sets the DCTEncoder for images, to encode a bitmap 1.44 + * as JPEG (DCT). 1.45 + * rasterDpi - the DPI at which features without native PDF support 1.46 + * will be rasterized (e.g. draw image with perspective, 1.47 + * draw text with perspective, ...) 1.48 + * A larger DPI would create a PDF that reflects the original 1.49 + * intent with better fidelity, but it can make for larger 1.50 + * PDF files too, which would use more memory while rendering, 1.51 + * and it would be slower to be processed or sent online or 1.52 + * to printer. 1.53 + */ 1.54 + static SkDocument* CreatePDF( 1.55 + const char filename[], 1.56 + SkPicture::EncodeBitmap encoder = NULL, 1.57 + SkScalar rasterDpi = SK_ScalarDefaultRasterDPI); 1.58 + 1.59 + /** 1.60 + * Create a PDF-backed document, writing the results into a stream. 1.61 + * If there is an error trying to create the doc, returns NULL. 1.62 + * 1.63 + * The document may write to the stream at anytime during its lifetime, 1.64 + * until either close() is called or the document is deleted. Once close() 1.65 + * has been called, and all of the data has been written to the stream, 1.66 + * if there is a Done proc provided, it will be called with the stream. 1.67 + * The proc can delete the stream, or whatever it needs to do. 1.68 + * encoder sets the DCTEncoder for images, to encode a bitmap 1.69 + * as JPEG (DCT). 1.70 + * Done - clean up method intended to allow deletion of the stream. 1.71 + * Its aborted parameter is true if the cleanup is due to an abort 1.72 + * call. It is false otherwise. 1.73 + * rasterDpi - the DPI at which features without native PDF support 1.74 + * will be rasterized (e.g. draw image with perspective, 1.75 + * draw text with perspective, ...) 1.76 + * A larger DPI would create a PDF that reflects the original 1.77 + * intent with better fidelity, but it can make for larger 1.78 + * PDF files too, which would use more memory while rendering, 1.79 + * and it would be slower to be processed or sent online or 1.80 + * to printer. */ 1.81 + static SkDocument* CreatePDF( 1.82 + SkWStream*, void (*Done)(SkWStream*,bool aborted) = NULL, 1.83 + SkPicture::EncodeBitmap encoder = NULL, 1.84 + SkScalar rasterDpi = SK_ScalarDefaultRasterDPI); 1.85 + 1.86 + /** 1.87 + * Begin a new page for the document, returning the canvas that will draw 1.88 + * into the page. The document owns this canvas, and it will go out of 1.89 + * scope when endPage() or close() is called, or the document is deleted. 1.90 + */ 1.91 + SkCanvas* beginPage(SkScalar width, SkScalar height, 1.92 + const SkRect* content = NULL); 1.93 + 1.94 + /** 1.95 + * Call endPage() when the content for the current page has been drawn 1.96 + * (into the canvas returned by beginPage()). After this call the canvas 1.97 + * returned by beginPage() will be out-of-scope. 1.98 + */ 1.99 + void endPage(); 1.100 + 1.101 + /** 1.102 + * Call close() when all pages have been drawn. This will close the file 1.103 + * or stream holding the document's contents. After close() the document 1.104 + * can no longer add new pages. Deleting the document will automatically 1.105 + * call close() if need be. 1.106 + * Returns true on success or false on failure. 1.107 + */ 1.108 + bool close(); 1.109 + 1.110 + /** 1.111 + * Call abort() to stop producing the document immediately. 1.112 + * The stream output must be ignored, and should not be trusted. 1.113 + */ 1.114 + void abort(); 1.115 + 1.116 +protected: 1.117 + SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); 1.118 + // note: subclasses must call close() in their destructor, as the base class 1.119 + // cannot do this for them. 1.120 + virtual ~SkDocument(); 1.121 + 1.122 + virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, 1.123 + const SkRect& content) = 0; 1.124 + virtual void onEndPage() = 0; 1.125 + virtual bool onClose(SkWStream*) = 0; 1.126 + virtual void onAbort() = 0; 1.127 + 1.128 + enum State { 1.129 + kBetweenPages_State, 1.130 + kInPage_State, 1.131 + kClosed_State 1.132 + }; 1.133 + State getState() const { return fState; } 1.134 + 1.135 +private: 1.136 + SkWStream* fStream; 1.137 + void (*fDoneProc)(SkWStream*, bool aborted); 1.138 + State fState; 1.139 + 1.140 + typedef SkRefCnt INHERITED; 1.141 +}; 1.142 + 1.143 +#endif