199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com/* 299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Copyright 2013 Google Inc. 399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * 499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Use of this source code is governed by a BSD-style license that can be 599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * found in the LICENSE file. 699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */ 799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#ifndef SkDocument_DEFINED 999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#define SkDocument_DEFINED 1099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 11b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org#include "SkBitmap.h" 12b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org#include "SkPicture.h" 1399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#include "SkRect.h" 1499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#include "SkRefCnt.h" 15f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary#include "SkString.h" 16f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary#include "SkTime.h" 1799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 1899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comclass SkCanvas; 19712fdf7603c62820b21174da9b0a2071c174936bhalcanaryclass SkPixelSerializer; 2099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comclass SkWStream; 2199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 228c294900f31d8d032b62182f011019276d27d5d0commit-bot@chromium.org/** SK_ScalarDefaultDPI is 72 DPI. 238c294900f31d8d032b62182f011019276d27d5d0commit-bot@chromium.org*/ 244b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org#define SK_ScalarDefaultRasterDPI 72.0f 258c294900f31d8d032b62182f011019276d27d5d0commit-bot@chromium.org 2699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com/** 2799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * High-level API for creating a document-based canvas. To use.. 2899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * 2999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * 1. Create a document, specifying a stream to store the output. 3099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * 2. For each "page" of content: 3199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * a. canvas = doc->beginPage(...) 3299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * b. draw_my_content(canvas); 3399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * c. doc->endPage(); 3499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * 3. Close the document with doc->close(). 3599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */ 3685588344c3ef43768e27881f71079edb372119b5halcanaryclass SK_API SkDocument : public SkRefCnt { 3799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.compublic: 3899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com /** 398c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * Create a PDF-backed document, writing the results into a SkWStream. 408c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * 418c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm. 428c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * 438c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * @param SkWStream* A PDF document will be written to this 448c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * stream. The document may write to the stream at 458c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * anytime during its lifetime, until either close() is 468c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * called or the document is deleted. 478c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * @param dpi The DPI (pixels-per-inch) at which features without 488c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * native PDF support will be rasterized (e.g. draw image 498c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * with perspective, draw text with perspective, ...) A 508c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * larger DPI would create a PDF that reflects the 518c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * original intent with better fidelity, but it can make 528c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * for larger PDF files too, which would use more memory 538c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * while rendering, and it would be slower to be processed 548c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * or sent online or to printer. 558c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * @returns NULL if there is an error, otherwise a newly created 568c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * PDF-backed SkDocument. 5799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */ 588c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary static SkDocument* CreatePDF(SkWStream*, 598c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary SkScalar dpi = SK_ScalarDefaultRasterDPI); 6099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 6199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com /** 62712fdf7603c62820b21174da9b0a2071c174936bhalcanary * @param jpegEncoder For PDF documents, if a jpegEncoder is set, 63712fdf7603c62820b21174da9b0a2071c174936bhalcanary * use it to encode SkImages and SkBitmaps as [JFIF]JPEGs. 64712fdf7603c62820b21174da9b0a2071c174936bhalcanary * This feature is deprecated and is only supplied for 65712fdf7603c62820b21174da9b0a2071c174936bhalcanary * backwards compatability. 66712fdf7603c62820b21174da9b0a2071c174936bhalcanary * 67712fdf7603c62820b21174da9b0a2071c174936bhalcanary * The prefered method to create PDFs with JPEG images is 68712fdf7603c62820b21174da9b0a2071c174936bhalcanary * to use SkImage::NewFromEncoded() and not jpegEncoder. 69712fdf7603c62820b21174da9b0a2071c174936bhalcanary * Chromium uses NewFromEncoded. 70712fdf7603c62820b21174da9b0a2071c174936bhalcanary * 71712fdf7603c62820b21174da9b0a2071c174936bhalcanary * If the encoder is unset, or if jpegEncoder->onEncode() 72712fdf7603c62820b21174da9b0a2071c174936bhalcanary * returns NULL, fall back on encoding images losslessly 73712fdf7603c62820b21174da9b0a2071c174936bhalcanary * with Deflate. 74712fdf7603c62820b21174da9b0a2071c174936bhalcanary */ 75712fdf7603c62820b21174da9b0a2071c174936bhalcanary static SkDocument* CreatePDF(SkWStream*, 76712fdf7603c62820b21174da9b0a2071c174936bhalcanary SkScalar dpi, 77712fdf7603c62820b21174da9b0a2071c174936bhalcanary SkPixelSerializer* jpegEncoder); 78712fdf7603c62820b21174da9b0a2071c174936bhalcanary 79712fdf7603c62820b21174da9b0a2071c174936bhalcanary /** 808c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary * Create a PDF-backed document, writing the results into a file. 818c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary */ 828c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary static SkDocument* CreatePDF(const char outputFilePath[], 838c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary SkScalar dpi = SK_ScalarDefaultRasterDPI); 8499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 8599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com /** 8647ef4d5d934bba86848aa238efab21f54a160c1ahalcanary * Create a XPS-backed document, writing the results into the stream. 8747ef4d5d934bba86848aa238efab21f54a160c1ahalcanary * Returns NULL if XPS is not supported. 8847ef4d5d934bba86848aa238efab21f54a160c1ahalcanary */ 8947ef4d5d934bba86848aa238efab21f54a160c1ahalcanary static SkDocument* CreateXPS(SkWStream* stream, 9047ef4d5d934bba86848aa238efab21f54a160c1ahalcanary SkScalar dpi = SK_ScalarDefaultRasterDPI); 9147ef4d5d934bba86848aa238efab21f54a160c1ahalcanary 9247ef4d5d934bba86848aa238efab21f54a160c1ahalcanary /** 9347ef4d5d934bba86848aa238efab21f54a160c1ahalcanary * Create a XPS-backed document, writing the results into a file. 9447ef4d5d934bba86848aa238efab21f54a160c1ahalcanary * Returns NULL if XPS is not supported. 9547ef4d5d934bba86848aa238efab21f54a160c1ahalcanary */ 9647ef4d5d934bba86848aa238efab21f54a160c1ahalcanary static SkDocument* CreateXPS(const char path[], 9747ef4d5d934bba86848aa238efab21f54a160c1ahalcanary SkScalar dpi = SK_ScalarDefaultRasterDPI); 9847ef4d5d934bba86848aa238efab21f54a160c1ahalcanary /** 9999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Begin a new page for the document, returning the canvas that will draw 10099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * into the page. The document owns this canvas, and it will go out of 10199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * scope when endPage() or close() is called, or the document is deleted. 10299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */ 10399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com SkCanvas* beginPage(SkScalar width, SkScalar height, 10499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com const SkRect* content = NULL); 10599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 10699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com /** 10799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Call endPage() when the content for the current page has been drawn 10899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * (into the canvas returned by beginPage()). After this call the canvas 10999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * returned by beginPage() will be out-of-scope. 11099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */ 11199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com void endPage(); 11299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 11399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com /** 11499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Call close() when all pages have been drawn. This will close the file 11599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * or stream holding the document's contents. After close() the document 11699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * can no longer add new pages. Deleting the document will automatically 11799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * call close() if need be. 118b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org * Returns true on success or false on failure. 11999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */ 120b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org bool close(); 121b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org 122b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org /** 123b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org * Call abort() to stop producing the document immediately. 124b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org * The stream output must be ignored, and should not be trusted. 125b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org */ 126b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org void abort(); 12799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 128f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary /** 129f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * Set the document's metadata, if supported by the document 130f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * type. The creationDate and modifiedDate parameters can be 131f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * nullptr. For example: 132f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * 133f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * SkDocument* make_doc(SkWStream* output) { 1347001576c7160e20c9c9db2fbe947408989027d66halcanary * std::vector<SkDocument::Attribute> info; 135f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * info.emplace_back(SkString("Title"), SkString("...")); 136f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * info.emplace_back(SkString("Author"), SkString("...")); 137f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * info.emplace_back(SkString("Subject"), SkString("...")); 138f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * info.emplace_back(SkString("Keywords"), SkString("...")); 139f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * info.emplace_back(SkString("Creator"), SkString("...")); 140f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * SkTime::DateTime now; 141f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * SkTime::GetDateTime(&now); 142f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * SkDocument* doc = SkDocument::CreatePDF(output); 1437001576c7160e20c9c9db2fbe947408989027d66halcanary * doc->setMetadata(&info[0], (int)info.size(), &now, &now); 144f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * return doc; 145f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary * } 146f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary */ 147f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary struct Attribute { 148f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary SkString fKey, fValue; 149f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary Attribute(const SkString& k, const SkString& v) : fKey(k), fValue(v) {} 150f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary }; 1517001576c7160e20c9c9db2fbe947408989027d66halcanary virtual void setMetadata(const SkDocument::Attribute[], 1527001576c7160e20c9c9db2fbe947408989027d66halcanary int /* attributeCount */, 153f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary const SkTime::DateTime* /* creationDate */, 154f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary const SkTime::DateTime* /* modifiedDate */) {} 155f12a1673f024d30d30f06b9f88b5cc072b8a2d1ehalcanary 15699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comprotected: 157701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); 1588c92dc1dc281649f9e6b0ff534c25bc89dded3eahalcanary 15999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com // note: subclasses must call close() in their destructor, as the base class 16099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com // cannot do this for them. 16199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com virtual ~SkDocument(); 16299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 16399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, 16499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com const SkRect& content) = 0; 16599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com virtual void onEndPage() = 0; 166b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org virtual bool onClose(SkWStream*) = 0; 167b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org virtual void onAbort() = 0; 16899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 169902d4d18dec27b6991516a235fc04a7af7290287halcanary // Allows subclasses to write to the stream as pages are written. 170902d4d18dec27b6991516a235fc04a7af7290287halcanary SkWStream* getStream() { return fStream; } 171902d4d18dec27b6991516a235fc04a7af7290287halcanary 17299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com enum State { 17399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com kBetweenPages_State, 17499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com kInPage_State, 17599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com kClosed_State 17699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com }; 17799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com State getState() const { return fState; } 17899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 17999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comprivate: 18099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com SkWStream* fStream; 181701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com void (*fDoneProc)(SkWStream*, bool aborted); 1826319367bdcbf5e5050632ab97973f5035d0dd8faskia.committer@gmail.com State fState; 183ef284a84f503adfd08ee52b5aee142c548698ea4commit-bot@chromium.org 184ef284a84f503adfd08ee52b5aee142c548698ea4commit-bot@chromium.org typedef SkRefCnt INHERITED; 18599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}; 18699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com 18799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#endif 188