2013-06-07 20:30:16 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
* found in the LICENSE file.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SkDocument_DEFINED
|
|
|
|
|
#define SkDocument_DEFINED
|
|
|
|
|
|
2013-10-09 21:09:00 +00:00
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
|
#include "SkPicture.h"
|
2016-04-27 14:45:18 +00:00
|
|
|
|
#include "SkPixelSerializer.h"
|
2013-06-07 20:30:16 +00:00
|
|
|
|
#include "SkRect.h"
|
|
|
|
|
#include "SkRefCnt.h"
|
2015-09-23 19:45:49 +00:00
|
|
|
|
#include "SkString.h"
|
|
|
|
|
#include "SkTime.h"
|
2013-06-07 20:30:16 +00:00
|
|
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
|
class SkWStream;
|
|
|
|
|
|
2016-04-27 14:45:18 +00:00
|
|
|
|
#define SK_SUPPORT_LEGACY_DOCUMENT_API
|
|
|
|
|
|
2013-10-21 17:14:37 +00:00
|
|
|
|
/** SK_ScalarDefaultDPI is 72 DPI.
|
|
|
|
|
*/
|
2013-11-25 19:44:07 +00:00
|
|
|
|
#define SK_ScalarDefaultRasterDPI 72.0f
|
2013-10-21 17:14:37 +00:00
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* High-level API for creating a document-based canvas. To use..
|
|
|
|
|
*
|
|
|
|
|
* 1. Create a document, specifying a stream to store the output.
|
|
|
|
|
* 2. For each "page" of content:
|
|
|
|
|
* a. canvas = doc->beginPage(...)
|
|
|
|
|
* b. draw_my_content(canvas);
|
|
|
|
|
* c. doc->endPage();
|
|
|
|
|
* 3. Close the document with doc->close().
|
|
|
|
|
*/
|
2014-11-14 21:24:18 +00:00
|
|
|
|
class SK_API SkDocument : public SkRefCnt {
|
2013-06-07 20:30:16 +00:00
|
|
|
|
public:
|
2016-04-27 14:45:18 +00:00
|
|
|
|
struct OptionalTimestamp {
|
|
|
|
|
SkTime::DateTime fDateTime;
|
|
|
|
|
bool fEnabled;
|
|
|
|
|
OptionalTimestamp() : fEnabled(false) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Optional metadata to be passed into the PDF factory function.
|
|
|
|
|
*/
|
|
|
|
|
struct PDFMetadata {
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The document’s title.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkString fTitle;
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The name of the person who created the document.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkString fAuthor;
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The subject of the document.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkString fSubject;
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Keywords associated with the document. Commas may be used
|
|
|
|
|
* to delineate keywords within the string.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkString fKeywords;
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* If the document was converted to PDF from another format,
|
|
|
|
|
* the name of the conforming product that created the
|
|
|
|
|
* original document from which it was converted.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkString fCreator;
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The product that is converting this document to PDF.
|
|
|
|
|
*
|
|
|
|
|
* Leave fProducer empty to get the default, correct value.
|
|
|
|
|
*/
|
|
|
|
|
SkString fProducer;
|
|
|
|
|
/**
|
|
|
|
|
* The date and time the document was created.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
OptionalTimestamp fCreation;
|
2016-06-30 15:22:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The date and time the document was most recently modified.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
OptionalTimestamp fModified;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a PDF-backed document, writing the results into a
|
|
|
|
|
* SkWStream.
|
|
|
|
|
*
|
|
|
|
|
* PDF pages are sized in point units. 1 pt == 1/72 inch ==
|
|
|
|
|
* 127/360 mm.
|
|
|
|
|
*
|
|
|
|
|
* @param stream A PDF document will be written to this
|
|
|
|
|
* stream. The document may write to the stream at
|
|
|
|
|
* anytime during its lifetime, until either close() is
|
|
|
|
|
* called or the document is deleted.
|
|
|
|
|
* @param dpi The DPI (pixels-per-inch) at which features without
|
|
|
|
|
* native PDF support will be rasterized (e.g. draw image
|
|
|
|
|
* with perspective, draw text with perspective, ...) A
|
|
|
|
|
* larger DPI would create a PDF that reflects the
|
|
|
|
|
* original intent with better fidelity, but it can make
|
|
|
|
|
* for larger PDF files too, which would use more memory
|
|
|
|
|
* while rendering, and it would be slower to be processed
|
|
|
|
|
* or sent online or to printer.
|
|
|
|
|
* @param metadata a PDFmetadata object. Any fields may be left
|
|
|
|
|
* empty.
|
|
|
|
|
* @param jpegEncoder For PDF documents, if a jpegEncoder is set,
|
|
|
|
|
* use it to encode SkImages and SkBitmaps as [JFIF]JPEGs.
|
|
|
|
|
* This feature is deprecated and is only supplied for
|
|
|
|
|
* backwards compatability.
|
|
|
|
|
* The prefered method to create PDFs with JPEG images is
|
|
|
|
|
* to use SkImage::NewFromEncoded() and not jpegEncoder.
|
|
|
|
|
* Chromium uses NewFromEncoded.
|
|
|
|
|
* If the encoder is unset, or if jpegEncoder->onEncode()
|
|
|
|
|
* returns NULL, fall back on encoding images losslessly
|
|
|
|
|
* with Deflate.
|
|
|
|
|
* @param pdfa Iff true, include XMP metadata, a document UUID,
|
|
|
|
|
* and sRGB output intent information. This adds length
|
|
|
|
|
* to the document and makes it non-reproducable, but are
|
|
|
|
|
* necessary features for PDF/A-2b conformance
|
|
|
|
|
*
|
|
|
|
|
* @returns NULL if there is an error, otherwise a newly created
|
|
|
|
|
* PDF-backed SkDocument.
|
|
|
|
|
*/
|
|
|
|
|
static sk_sp<SkDocument> MakePDF(SkWStream* stream,
|
|
|
|
|
SkScalar dpi,
|
|
|
|
|
const SkDocument::PDFMetadata& metadata,
|
|
|
|
|
sk_sp<SkPixelSerializer> jpegEncoder,
|
|
|
|
|
bool pdfa);
|
|
|
|
|
|
|
|
|
|
static sk_sp<SkDocument> MakePDF(SkWStream* stream,
|
|
|
|
|
SkScalar dpi = SK_ScalarDefaultRasterDPI) {
|
|
|
|
|
return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(),
|
|
|
|
|
nullptr, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a PDF-backed document, writing the results into a file.
|
|
|
|
|
*/
|
|
|
|
|
static sk_sp<SkDocument> MakePDF(const char outputFilePath[],
|
|
|
|
|
SkScalar dpi = SK_ScalarDefaultRasterDPI);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a XPS-backed document, writing the results into the stream.
|
|
|
|
|
* Returns NULL if XPS is not supported.
|
|
|
|
|
*/
|
|
|
|
|
static sk_sp<SkDocument> MakeXPS(SkWStream* stream,
|
|
|
|
|
SkScalar dpi = SK_ScalarDefaultRasterDPI);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a XPS-backed document, writing the results into a file.
|
|
|
|
|
* Returns NULL if XPS is not supported.
|
|
|
|
|
*/
|
|
|
|
|
static sk_sp<SkDocument> MakeXPS(const char path[],
|
|
|
|
|
SkScalar dpi = SK_ScalarDefaultRasterDPI);
|
|
|
|
|
|
|
|
|
|
#ifdef SK_SUPPORT_LEGACY_DOCUMENT_API
|
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
/**
|
2015-02-20 02:50:05 +00:00
|
|
|
|
* Create a PDF-backed document, writing the results into a SkWStream.
|
|
|
|
|
*
|
|
|
|
|
* PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm.
|
|
|
|
|
*
|
|
|
|
|
* @param SkWStream* A PDF document will be written to this
|
|
|
|
|
* stream. The document may write to the stream at
|
|
|
|
|
* anytime during its lifetime, until either close() is
|
|
|
|
|
* called or the document is deleted.
|
|
|
|
|
* @param dpi The DPI (pixels-per-inch) at which features without
|
|
|
|
|
* native PDF support will be rasterized (e.g. draw image
|
|
|
|
|
* with perspective, draw text with perspective, ...) A
|
|
|
|
|
* larger DPI would create a PDF that reflects the
|
|
|
|
|
* original intent with better fidelity, but it can make
|
|
|
|
|
* for larger PDF files too, which would use more memory
|
|
|
|
|
* while rendering, and it would be slower to be processed
|
|
|
|
|
* or sent online or to printer.
|
|
|
|
|
* @returns NULL if there is an error, otherwise a newly created
|
|
|
|
|
* PDF-backed SkDocument.
|
2013-06-07 20:30:16 +00:00
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
static SkDocument* CreatePDF(SkWStream* stream,
|
|
|
|
|
SkScalar dpi = SK_ScalarDefaultRasterDPI) {
|
|
|
|
|
return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(),
|
|
|
|
|
nullptr, false).release();
|
|
|
|
|
}
|
2013-06-07 20:30:16 +00:00
|
|
|
|
|
2015-12-10 16:59:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param jpegEncoder For PDF documents, if a jpegEncoder is set,
|
|
|
|
|
* use it to encode SkImages and SkBitmaps as [JFIF]JPEGs.
|
|
|
|
|
* This feature is deprecated and is only supplied for
|
|
|
|
|
* backwards compatability.
|
|
|
|
|
*
|
|
|
|
|
* The prefered method to create PDFs with JPEG images is
|
|
|
|
|
* to use SkImage::NewFromEncoded() and not jpegEncoder.
|
|
|
|
|
* Chromium uses NewFromEncoded.
|
|
|
|
|
*
|
|
|
|
|
* If the encoder is unset, or if jpegEncoder->onEncode()
|
|
|
|
|
* returns NULL, fall back on encoding images losslessly
|
|
|
|
|
* with Deflate.
|
|
|
|
|
*/
|
2016-04-27 14:45:18 +00:00
|
|
|
|
static SkDocument* CreatePDF(SkWStream* stream,
|
2015-12-10 16:59:43 +00:00
|
|
|
|
SkScalar dpi,
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkPixelSerializer* jpegEncoder) {
|
|
|
|
|
return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(),
|
|
|
|
|
sk_ref_sp(jpegEncoder), false).release();
|
|
|
|
|
}
|
2015-12-10 16:59:43 +00:00
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
/**
|
2015-02-20 02:50:05 +00:00
|
|
|
|
* Create a PDF-backed document, writing the results into a file.
|
|
|
|
|
*/
|
|
|
|
|
static SkDocument* CreatePDF(const char outputFilePath[],
|
2016-04-27 14:45:18 +00:00
|
|
|
|
SkScalar dpi = SK_ScalarDefaultRasterDPI) {
|
|
|
|
|
return SkDocument::MakePDF(outputFilePath, dpi).release();
|
|
|
|
|
}
|
2013-06-07 20:30:16 +00:00
|
|
|
|
|
2016-04-27 14:45:18 +00:00
|
|
|
|
#endif // SK_SUPPORT_LEGACY_DOCUMENT_API
|
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* Begin a new page for the document, returning the canvas that will draw
|
|
|
|
|
* into the page. The document owns this canvas, and it will go out of
|
|
|
|
|
* scope when endPage() or close() is called, or the document is deleted.
|
|
|
|
|
*/
|
|
|
|
|
SkCanvas* beginPage(SkScalar width, SkScalar height,
|
|
|
|
|
const SkRect* content = NULL);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call endPage() when the content for the current page has been drawn
|
|
|
|
|
* (into the canvas returned by beginPage()). After this call the canvas
|
|
|
|
|
* returned by beginPage() will be out-of-scope.
|
|
|
|
|
*/
|
|
|
|
|
void endPage();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call close() when all pages have been drawn. This will close the file
|
|
|
|
|
* or stream holding the document's contents. After close() the document
|
|
|
|
|
* can no longer add new pages. Deleting the document will automatically
|
|
|
|
|
* call close() if need be.
|
2013-10-09 21:09:00 +00:00
|
|
|
|
* Returns true on success or false on failure.
|
2013-06-07 20:30:16 +00:00
|
|
|
|
*/
|
2013-10-09 21:09:00 +00:00
|
|
|
|
bool close();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call abort() to stop producing the document immediately.
|
|
|
|
|
* The stream output must be ignored, and should not be trusted.
|
|
|
|
|
*/
|
|
|
|
|
void abort();
|
2013-06-07 20:30:16 +00:00
|
|
|
|
|
2016-04-27 14:45:18 +00:00
|
|
|
|
#ifdef SK_SUPPORT_LEGACY_DOCUMENT_API
|
2015-09-23 19:45:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set the document's metadata, if supported by the document
|
|
|
|
|
* type. The creationDate and modifiedDate parameters can be
|
|
|
|
|
* nullptr. For example:
|
|
|
|
|
*
|
|
|
|
|
* SkDocument* make_doc(SkWStream* output) {
|
2016-02-11 15:59:59 +00:00
|
|
|
|
* std::vector<SkDocument::Attribute> info;
|
2015-09-23 19:45:49 +00:00
|
|
|
|
* info.emplace_back(SkString("Title"), SkString("..."));
|
|
|
|
|
* info.emplace_back(SkString("Author"), SkString("..."));
|
|
|
|
|
* info.emplace_back(SkString("Subject"), SkString("..."));
|
|
|
|
|
* info.emplace_back(SkString("Keywords"), SkString("..."));
|
|
|
|
|
* info.emplace_back(SkString("Creator"), SkString("..."));
|
|
|
|
|
* SkTime::DateTime now;
|
|
|
|
|
* SkTime::GetDateTime(&now);
|
|
|
|
|
* SkDocument* doc = SkDocument::CreatePDF(output);
|
2016-02-11 15:59:59 +00:00
|
|
|
|
* doc->setMetadata(&info[0], (int)info.size(), &now, &now);
|
2015-09-23 19:45:49 +00:00
|
|
|
|
* return doc;
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
struct Attribute {
|
|
|
|
|
SkString fKey, fValue;
|
|
|
|
|
Attribute(const SkString& k, const SkString& v) : fKey(k), fValue(v) {}
|
|
|
|
|
};
|
2016-02-11 15:59:59 +00:00
|
|
|
|
virtual void setMetadata(const SkDocument::Attribute[],
|
|
|
|
|
int /* attributeCount */,
|
2015-09-23 19:45:49 +00:00
|
|
|
|
const SkTime::DateTime* /* creationDate */,
|
|
|
|
|
const SkTime::DateTime* /* modifiedDate */) {}
|
2016-04-27 14:45:18 +00:00
|
|
|
|
#endif // SK_SUPPORT_LEGACY_DOCUMENT_API
|
2015-09-23 19:45:49 +00:00
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
protected:
|
2013-11-18 16:26:25 +00:00
|
|
|
|
SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted));
|
2015-02-20 02:50:05 +00:00
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
// note: subclasses must call close() in their destructor, as the base class
|
|
|
|
|
// cannot do this for them.
|
|
|
|
|
virtual ~SkDocument();
|
|
|
|
|
|
|
|
|
|
virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
|
|
|
|
|
const SkRect& content) = 0;
|
|
|
|
|
virtual void onEndPage() = 0;
|
2013-10-09 21:09:00 +00:00
|
|
|
|
virtual bool onClose(SkWStream*) = 0;
|
|
|
|
|
virtual void onAbort() = 0;
|
2013-06-07 20:30:16 +00:00
|
|
|
|
|
2014-10-30 18:29:00 +00:00
|
|
|
|
// Allows subclasses to write to the stream as pages are written.
|
|
|
|
|
SkWStream* getStream() { return fStream; }
|
|
|
|
|
|
2013-06-07 20:30:16 +00:00
|
|
|
|
enum State {
|
|
|
|
|
kBetweenPages_State,
|
|
|
|
|
kInPage_State,
|
|
|
|
|
kClosed_State
|
|
|
|
|
};
|
|
|
|
|
State getState() const { return fState; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
SkWStream* fStream;
|
2013-11-18 16:26:25 +00:00
|
|
|
|
void (*fDoneProc)(SkWStream*, bool aborted);
|
2013-06-08 07:01:13 +00:00
|
|
|
|
State fState;
|
2013-07-11 22:29:29 +00:00
|
|
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
2013-06-07 20:30:16 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|