OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkDocument_DEFINED | 8 #ifndef SkDocument_DEFINED |
9 #define SkDocument_DEFINED | 9 #define SkDocument_DEFINED |
10 | 10 |
11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
12 #include "SkPicture.h" | 12 #include "SkPicture.h" |
| 13 #include "SkPixelSerializer.h" |
13 #include "SkRect.h" | 14 #include "SkRect.h" |
14 #include "SkRefCnt.h" | 15 #include "SkRefCnt.h" |
15 #include "SkString.h" | 16 #include "SkString.h" |
16 #include "SkTime.h" | 17 #include "SkTime.h" |
17 | 18 |
18 class SkCanvas; | 19 class SkCanvas; |
19 class SkPixelSerializer; | |
20 class SkWStream; | 20 class SkWStream; |
21 | 21 |
| 22 #define SK_SUPPORT_LEGACY_DOCUMENT_API |
| 23 |
22 /** SK_ScalarDefaultDPI is 72 DPI. | 24 /** SK_ScalarDefaultDPI is 72 DPI. |
23 */ | 25 */ |
24 #define SK_ScalarDefaultRasterDPI 72.0f | 26 #define SK_ScalarDefaultRasterDPI 72.0f |
25 | 27 |
26 /** | 28 /** |
27 * High-level API for creating a document-based canvas. To use.. | 29 * High-level API for creating a document-based canvas. To use.. |
28 * | 30 * |
29 * 1. Create a document, specifying a stream to store the output. | 31 * 1. Create a document, specifying a stream to store the output. |
30 * 2. For each "page" of content: | 32 * 2. For each "page" of content: |
31 * a. canvas = doc->beginPage(...) | 33 * a. canvas = doc->beginPage(...) |
32 * b. draw_my_content(canvas); | 34 * b. draw_my_content(canvas); |
33 * c. doc->endPage(); | 35 * c. doc->endPage(); |
34 * 3. Close the document with doc->close(). | 36 * 3. Close the document with doc->close(). |
35 */ | 37 */ |
36 class SK_API SkDocument : public SkRefCnt { | 38 class SK_API SkDocument : public SkRefCnt { |
37 public: | 39 public: |
| 40 struct OptionalTimestamp { |
| 41 SkTime::DateTime fDateTime; |
| 42 bool fEnabled; |
| 43 OptionalTimestamp() : fEnabled(false) {} |
| 44 }; |
| 45 |
| 46 /** |
| 47 * Optional metadata to be passed into the PDF factory function. |
| 48 */ |
| 49 struct PDFMetadata { |
| 50 SkString fTitle; |
| 51 SkString fAuthor; |
| 52 SkString fSubject; |
| 53 SkString fKeywords; |
| 54 SkString fCreator; |
| 55 OptionalTimestamp fCreation; |
| 56 OptionalTimestamp fModified; |
| 57 }; |
| 58 |
| 59 /** |
| 60 * Create a PDF-backed document, writing the results into a |
| 61 * SkWStream. |
| 62 * |
| 63 * PDF pages are sized in point units. 1 pt == 1/72 inch == |
| 64 * 127/360 mm. |
| 65 * |
| 66 * @param stream A PDF document will be written to this |
| 67 * stream. The document may write to the stream at |
| 68 * anytime during its lifetime, until either close() is |
| 69 * called or the document is deleted. |
| 70 * @param dpi The DPI (pixels-per-inch) at which features without |
| 71 * native PDF support will be rasterized (e.g. draw image |
| 72 * with perspective, draw text with perspective, ...) A |
| 73 * larger DPI would create a PDF that reflects the |
| 74 * original intent with better fidelity, but it can make |
| 75 * for larger PDF files too, which would use more memory |
| 76 * while rendering, and it would be slower to be processed |
| 77 * or sent online or to printer. |
| 78 * @param metadata a PDFmetadata object. Any fields may be left |
| 79 * empty. |
| 80 * @param jpegEncoder For PDF documents, if a jpegEncoder is set, |
| 81 * use it to encode SkImages and SkBitmaps as [JFIF]JPEGs. |
| 82 * This feature is deprecated and is only supplied for |
| 83 * backwards compatability. |
| 84 * The prefered method to create PDFs with JPEG images is |
| 85 * to use SkImage::NewFromEncoded() and not jpegEncoder. |
| 86 * Chromium uses NewFromEncoded. |
| 87 * If the encoder is unset, or if jpegEncoder->onEncode() |
| 88 * returns NULL, fall back on encoding images losslessly |
| 89 * with Deflate. |
| 90 * @param pdfa Iff true, include XMP metadata, a document UUID, |
| 91 * and sRGB output intent information. This adds length |
| 92 * to the document and makes it non-reproducable, but are |
| 93 * necessary features for PDF/A-2b conformance |
| 94 * |
| 95 * @returns NULL if there is an error, otherwise a newly created |
| 96 * PDF-backed SkDocument. |
| 97 */ |
| 98 static sk_sp<SkDocument> MakePDF(SkWStream* stream, |
| 99 SkScalar dpi, |
| 100 const SkDocument::PDFMetadata& metadata, |
| 101 sk_sp<SkPixelSerializer> jpegEncoder, |
| 102 bool pdfa); |
| 103 |
| 104 static sk_sp<SkDocument> MakePDF(SkWStream* stream, |
| 105 SkScalar dpi = SK_ScalarDefaultRasterDPI) { |
| 106 return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(), |
| 107 nullptr, false); |
| 108 } |
| 109 |
| 110 /** |
| 111 * Create a PDF-backed document, writing the results into a file. |
| 112 */ |
| 113 static sk_sp<SkDocument> MakePDF(const char outputFilePath[], |
| 114 SkScalar dpi = SK_ScalarDefaultRasterDPI); |
| 115 |
| 116 /** |
| 117 * Create a XPS-backed document, writing the results into the stream. |
| 118 * Returns NULL if XPS is not supported. |
| 119 */ |
| 120 static sk_sp<SkDocument> MakeXPS(SkWStream* stream, |
| 121 SkScalar dpi = SK_ScalarDefaultRasterDPI); |
| 122 |
| 123 /** |
| 124 * Create a XPS-backed document, writing the results into a file. |
| 125 * Returns NULL if XPS is not supported. |
| 126 */ |
| 127 static sk_sp<SkDocument> MakeXPS(const char path[], |
| 128 SkScalar dpi = SK_ScalarDefaultRasterDPI); |
| 129 |
| 130 #ifdef SK_SUPPORT_LEGACY_DOCUMENT_API |
| 131 |
38 /** | 132 /** |
39 * Create a PDF-backed document, writing the results into a SkWStream. | 133 * Create a PDF-backed document, writing the results into a SkWStream. |
40 * | 134 * |
41 * PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm. | 135 * PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm. |
42 * | 136 * |
43 * @param SkWStream* A PDF document will be written to this | 137 * @param SkWStream* A PDF document will be written to this |
44 * stream. The document may write to the stream at | 138 * stream. The document may write to the stream at |
45 * anytime during its lifetime, until either close() is | 139 * anytime during its lifetime, until either close() is |
46 * called or the document is deleted. | 140 * called or the document is deleted. |
47 * @param dpi The DPI (pixels-per-inch) at which features without | 141 * @param dpi The DPI (pixels-per-inch) at which features without |
48 * native PDF support will be rasterized (e.g. draw image | 142 * native PDF support will be rasterized (e.g. draw image |
49 * with perspective, draw text with perspective, ...) A | 143 * with perspective, draw text with perspective, ...) A |
50 * larger DPI would create a PDF that reflects the | 144 * larger DPI would create a PDF that reflects the |
51 * original intent with better fidelity, but it can make | 145 * original intent with better fidelity, but it can make |
52 * for larger PDF files too, which would use more memory | 146 * for larger PDF files too, which would use more memory |
53 * while rendering, and it would be slower to be processed | 147 * while rendering, and it would be slower to be processed |
54 * or sent online or to printer. | 148 * or sent online or to printer. |
55 * @returns NULL if there is an error, otherwise a newly created | 149 * @returns NULL if there is an error, otherwise a newly created |
56 * PDF-backed SkDocument. | 150 * PDF-backed SkDocument. |
57 */ | 151 */ |
58 static SkDocument* CreatePDF(SkWStream*, | 152 static SkDocument* CreatePDF(SkWStream* stream, |
59 SkScalar dpi = SK_ScalarDefaultRasterDPI); | 153 SkScalar dpi = SK_ScalarDefaultRasterDPI) { |
| 154 return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(), |
| 155 nullptr, false).release(); |
| 156 } |
60 | 157 |
61 /** | 158 /** |
62 * @param jpegEncoder For PDF documents, if a jpegEncoder is set, | 159 * @param jpegEncoder For PDF documents, if a jpegEncoder is set, |
63 * use it to encode SkImages and SkBitmaps as [JFIF]JPEGs. | 160 * use it to encode SkImages and SkBitmaps as [JFIF]JPEGs. |
64 * This feature is deprecated and is only supplied for | 161 * This feature is deprecated and is only supplied for |
65 * backwards compatability. | 162 * backwards compatability. |
66 * | 163 * |
67 * The prefered method to create PDFs with JPEG images is | 164 * The prefered method to create PDFs with JPEG images is |
68 * to use SkImage::NewFromEncoded() and not jpegEncoder. | 165 * to use SkImage::NewFromEncoded() and not jpegEncoder. |
69 * Chromium uses NewFromEncoded. | 166 * Chromium uses NewFromEncoded. |
70 * | 167 * |
71 * If the encoder is unset, or if jpegEncoder->onEncode() | 168 * If the encoder is unset, or if jpegEncoder->onEncode() |
72 * returns NULL, fall back on encoding images losslessly | 169 * returns NULL, fall back on encoding images losslessly |
73 * with Deflate. | 170 * with Deflate. |
74 */ | 171 */ |
75 static SkDocument* CreatePDF(SkWStream*, | 172 static SkDocument* CreatePDF(SkWStream* stream, |
76 SkScalar dpi, | 173 SkScalar dpi, |
77 SkPixelSerializer* jpegEncoder); | 174 SkPixelSerializer* jpegEncoder) { |
| 175 return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(), |
| 176 sk_ref_sp(jpegEncoder), false).release(); |
| 177 } |
78 | 178 |
79 /** | 179 /** |
80 * Create a PDF-backed document, writing the results into a file. | 180 * Create a PDF-backed document, writing the results into a file. |
81 */ | 181 */ |
82 static SkDocument* CreatePDF(const char outputFilePath[], | 182 static SkDocument* CreatePDF(const char outputFilePath[], |
83 SkScalar dpi = SK_ScalarDefaultRasterDPI); | 183 SkScalar dpi = SK_ScalarDefaultRasterDPI) { |
| 184 return SkDocument::MakePDF(outputFilePath, dpi).release(); |
| 185 } |
84 | 186 |
85 /** | 187 /** |
86 * Create a XPS-backed document, writing the results into the stream. | 188 * Create a XPS-backed document, writing the results into the stream. |
87 * Returns NULL if XPS is not supported. | 189 * Returns NULL if XPS is not supported. |
88 */ | 190 */ |
89 static SkDocument* CreateXPS(SkWStream* stream, | 191 static SkDocument* CreateXPS(SkWStream* stream, |
90 SkScalar dpi = SK_ScalarDefaultRasterDPI); | 192 SkScalar dpi = SK_ScalarDefaultRasterDPI) { |
| 193 return SkDocument::MakeXPS(stream, dpi).release(); |
| 194 } |
91 | 195 |
92 /** | 196 /** |
93 * Create a XPS-backed document, writing the results into a file. | 197 * Create a XPS-backed document, writing the results into a file. |
94 * Returns NULL if XPS is not supported. | 198 * Returns NULL if XPS is not supported. |
95 */ | 199 */ |
96 static SkDocument* CreateXPS(const char path[], | 200 static SkDocument* CreateXPS(const char path[], |
97 SkScalar dpi = SK_ScalarDefaultRasterDPI); | 201 SkScalar dpi = SK_ScalarDefaultRasterDPI) { |
| 202 return SkDocument::MakeXPS(path, dpi).release(); |
| 203 } |
| 204 #endif // SK_SUPPORT_LEGACY_DOCUMENT_API |
| 205 |
98 /** | 206 /** |
99 * Begin a new page for the document, returning the canvas that will draw | 207 * Begin a new page for the document, returning the canvas that will draw |
100 * into the page. The document owns this canvas, and it will go out of | 208 * into the page. The document owns this canvas, and it will go out of |
101 * scope when endPage() or close() is called, or the document is deleted. | 209 * scope when endPage() or close() is called, or the document is deleted. |
102 */ | 210 */ |
103 SkCanvas* beginPage(SkScalar width, SkScalar height, | 211 SkCanvas* beginPage(SkScalar width, SkScalar height, |
104 const SkRect* content = NULL); | 212 const SkRect* content = NULL); |
105 | 213 |
106 /** | 214 /** |
107 * Call endPage() when the content for the current page has been drawn | 215 * Call endPage() when the content for the current page has been drawn |
(...skipping 10 matching lines...) Expand all Loading... |
118 * Returns true on success or false on failure. | 226 * Returns true on success or false on failure. |
119 */ | 227 */ |
120 bool close(); | 228 bool close(); |
121 | 229 |
122 /** | 230 /** |
123 * Call abort() to stop producing the document immediately. | 231 * Call abort() to stop producing the document immediately. |
124 * The stream output must be ignored, and should not be trusted. | 232 * The stream output must be ignored, and should not be trusted. |
125 */ | 233 */ |
126 void abort(); | 234 void abort(); |
127 | 235 |
| 236 #ifdef SK_SUPPORT_LEGACY_DOCUMENT_API |
128 /** | 237 /** |
129 * Set the document's metadata, if supported by the document | 238 * Set the document's metadata, if supported by the document |
130 * type. The creationDate and modifiedDate parameters can be | 239 * type. The creationDate and modifiedDate parameters can be |
131 * nullptr. For example: | 240 * nullptr. For example: |
132 * | 241 * |
133 * SkDocument* make_doc(SkWStream* output) { | 242 * SkDocument* make_doc(SkWStream* output) { |
134 * std::vector<SkDocument::Attribute> info; | 243 * std::vector<SkDocument::Attribute> info; |
135 * info.emplace_back(SkString("Title"), SkString("...")); | 244 * info.emplace_back(SkString("Title"), SkString("...")); |
136 * info.emplace_back(SkString("Author"), SkString("...")); | 245 * info.emplace_back(SkString("Author"), SkString("...")); |
137 * info.emplace_back(SkString("Subject"), SkString("...")); | 246 * info.emplace_back(SkString("Subject"), SkString("...")); |
138 * info.emplace_back(SkString("Keywords"), SkString("...")); | 247 * info.emplace_back(SkString("Keywords"), SkString("...")); |
139 * info.emplace_back(SkString("Creator"), SkString("...")); | 248 * info.emplace_back(SkString("Creator"), SkString("...")); |
140 * SkTime::DateTime now; | 249 * SkTime::DateTime now; |
141 * SkTime::GetDateTime(&now); | 250 * SkTime::GetDateTime(&now); |
142 * SkDocument* doc = SkDocument::CreatePDF(output); | 251 * SkDocument* doc = SkDocument::CreatePDF(output); |
143 * doc->setMetadata(&info[0], (int)info.size(), &now, &now); | 252 * doc->setMetadata(&info[0], (int)info.size(), &now, &now); |
144 * return doc; | 253 * return doc; |
145 * } | 254 * } |
146 */ | 255 */ |
147 struct Attribute { | 256 struct Attribute { |
148 SkString fKey, fValue; | 257 SkString fKey, fValue; |
149 Attribute(const SkString& k, const SkString& v) : fKey(k), fValue(v) {} | 258 Attribute(const SkString& k, const SkString& v) : fKey(k), fValue(v) {} |
150 }; | 259 }; |
151 virtual void setMetadata(const SkDocument::Attribute[], | 260 virtual void setMetadata(const SkDocument::Attribute[], |
152 int /* attributeCount */, | 261 int /* attributeCount */, |
153 const SkTime::DateTime* /* creationDate */, | 262 const SkTime::DateTime* /* creationDate */, |
154 const SkTime::DateTime* /* modifiedDate */) {} | 263 const SkTime::DateTime* /* modifiedDate */) {} |
| 264 #endif // SK_SUPPORT_LEGACY_DOCUMENT_API |
155 | 265 |
156 protected: | 266 protected: |
157 SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); | 267 SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); |
158 | 268 |
159 // note: subclasses must call close() in their destructor, as the base class | 269 // note: subclasses must call close() in their destructor, as the base class |
160 // cannot do this for them. | 270 // cannot do this for them. |
161 virtual ~SkDocument(); | 271 virtual ~SkDocument(); |
162 | 272 |
163 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, | 273 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
164 const SkRect& content) = 0; | 274 const SkRect& content) = 0; |
(...skipping 13 matching lines...) Expand all Loading... |
178 | 288 |
179 private: | 289 private: |
180 SkWStream* fStream; | 290 SkWStream* fStream; |
181 void (*fDoneProc)(SkWStream*, bool aborted); | 291 void (*fDoneProc)(SkWStream*, bool aborted); |
182 State fState; | 292 State fState; |
183 | 293 |
184 typedef SkRefCnt INHERITED; | 294 typedef SkRefCnt INHERITED; |
185 }; | 295 }; |
186 | 296 |
187 #endif | 297 #endif |
OLD | NEW |