| 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 "SkPixelSerializer.h" |
| 14 #include "SkRect.h" | 14 #include "SkRect.h" |
| 15 #include "SkRefCnt.h" | 15 #include "SkRefCnt.h" |
| 16 #include "SkString.h" | 16 #include "SkString.h" |
| 17 #include "SkTime.h" | 17 #include "SkTime.h" |
| 18 | 18 |
| 19 class SkCanvas; | 19 class SkCanvas; |
| 20 class SkWStream; | 20 class SkWStream; |
| 21 | 21 |
| 22 #define SK_SUPPORT_LEGACY_DOCUMENT_API | |
| 23 | |
| 24 /** SK_ScalarDefaultDPI is 72 DPI. | 22 /** SK_ScalarDefaultDPI is 72 DPI. |
| 25 */ | 23 */ |
| 26 #define SK_ScalarDefaultRasterDPI 72.0f | 24 #define SK_ScalarDefaultRasterDPI 72.0f |
| 27 | 25 |
| 28 /** | 26 /** |
| 29 * High-level API for creating a document-based canvas. To use.. | 27 * High-level API for creating a document-based canvas. To use.. |
| 30 * | 28 * |
| 31 * 1. Create a document, specifying a stream to store the output. | 29 * 1. Create a document, specifying a stream to store the output. |
| 32 * 2. For each "page" of content: | 30 * 2. For each "page" of content: |
| 33 * a. canvas = doc->beginPage(...) | 31 * a. canvas = doc->beginPage(...) |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 static sk_sp<SkDocument> MakeXPS(SkWStream* stream, | 148 static sk_sp<SkDocument> MakeXPS(SkWStream* stream, |
| 151 SkScalar dpi = SK_ScalarDefaultRasterDPI); | 149 SkScalar dpi = SK_ScalarDefaultRasterDPI); |
| 152 | 150 |
| 153 /** | 151 /** |
| 154 * Create a XPS-backed document, writing the results into a file. | 152 * Create a XPS-backed document, writing the results into a file. |
| 155 * Returns NULL if XPS is not supported. | 153 * Returns NULL if XPS is not supported. |
| 156 */ | 154 */ |
| 157 static sk_sp<SkDocument> MakeXPS(const char path[], | 155 static sk_sp<SkDocument> MakeXPS(const char path[], |
| 158 SkScalar dpi = SK_ScalarDefaultRasterDPI); | 156 SkScalar dpi = SK_ScalarDefaultRasterDPI); |
| 159 | 157 |
| 160 #ifdef SK_SUPPORT_LEGACY_DOCUMENT_API | |
| 161 | |
| 162 /** | |
| 163 * Create a PDF-backed document, writing the results into a SkWStream. | |
| 164 * | |
| 165 * PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm. | |
| 166 * | |
| 167 * @param SkWStream* A PDF document will be written to this | |
| 168 * stream. The document may write to the stream at | |
| 169 * anytime during its lifetime, until either close() is | |
| 170 * called or the document is deleted. | |
| 171 * @param dpi The DPI (pixels-per-inch) at which features without | |
| 172 * native PDF support will be rasterized (e.g. draw image | |
| 173 * with perspective, draw text with perspective, ...) A | |
| 174 * larger DPI would create a PDF that reflects the | |
| 175 * original intent with better fidelity, but it can make | |
| 176 * for larger PDF files too, which would use more memory | |
| 177 * while rendering, and it would be slower to be processed | |
| 178 * or sent online or to printer. | |
| 179 * @returns NULL if there is an error, otherwise a newly created | |
| 180 * PDF-backed SkDocument. | |
| 181 */ | |
| 182 static SkDocument* CreatePDF(SkWStream* stream, | |
| 183 SkScalar dpi = SK_ScalarDefaultRasterDPI) { | |
| 184 return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(), | |
| 185 nullptr, false).release(); | |
| 186 } | |
| 187 | |
| 188 /** | |
| 189 * @param jpegEncoder For PDF documents, if a jpegEncoder is set, | |
| 190 * use it to encode SkImages and SkBitmaps as [JFIF]JPEGs. | |
| 191 * This feature is deprecated and is only supplied for | |
| 192 * backwards compatability. | |
| 193 * | |
| 194 * The prefered method to create PDFs with JPEG images is | |
| 195 * to use SkImage::NewFromEncoded() and not jpegEncoder. | |
| 196 * Chromium uses NewFromEncoded. | |
| 197 * | |
| 198 * If the encoder is unset, or if jpegEncoder->onEncode() | |
| 199 * returns NULL, fall back on encoding images losslessly | |
| 200 * with Deflate. | |
| 201 */ | |
| 202 static SkDocument* CreatePDF(SkWStream* stream, | |
| 203 SkScalar dpi, | |
| 204 SkPixelSerializer* jpegEncoder) { | |
| 205 return SkDocument::MakePDF(stream, dpi, SkDocument::PDFMetadata(), | |
| 206 sk_ref_sp(jpegEncoder), false).release(); | |
| 207 } | |
| 208 | |
| 209 /** | |
| 210 * Create a PDF-backed document, writing the results into a file. | |
| 211 */ | |
| 212 static SkDocument* CreatePDF(const char outputFilePath[], | |
| 213 SkScalar dpi = SK_ScalarDefaultRasterDPI) { | |
| 214 return SkDocument::MakePDF(outputFilePath, dpi).release(); | |
| 215 } | |
| 216 | |
| 217 #endif // SK_SUPPORT_LEGACY_DOCUMENT_API | |
| 218 | |
| 219 /** | 158 /** |
| 220 * Begin a new page for the document, returning the canvas that will draw | 159 * Begin a new page for the document, returning the canvas that will draw |
| 221 * into the page. The document owns this canvas, and it will go out of | 160 * into the page. The document owns this canvas, and it will go out of |
| 222 * scope when endPage() or close() is called, or the document is deleted. | 161 * scope when endPage() or close() is called, or the document is deleted. |
| 223 */ | 162 */ |
| 224 SkCanvas* beginPage(SkScalar width, SkScalar height, | 163 SkCanvas* beginPage(SkScalar width, SkScalar height, |
| 225 const SkRect* content = NULL); | 164 const SkRect* content = NULL); |
| 226 | 165 |
| 227 /** | 166 /** |
| 228 * Call endPage() when the content for the current page has been drawn | 167 * Call endPage() when the content for the current page has been drawn |
| (...skipping 10 matching lines...) Expand all Loading... |
| 239 * Returns true on success or false on failure. | 178 * Returns true on success or false on failure. |
| 240 */ | 179 */ |
| 241 bool close(); | 180 bool close(); |
| 242 | 181 |
| 243 /** | 182 /** |
| 244 * Call abort() to stop producing the document immediately. | 183 * Call abort() to stop producing the document immediately. |
| 245 * The stream output must be ignored, and should not be trusted. | 184 * The stream output must be ignored, and should not be trusted. |
| 246 */ | 185 */ |
| 247 void abort(); | 186 void abort(); |
| 248 | 187 |
| 249 #ifdef SK_SUPPORT_LEGACY_DOCUMENT_API | |
| 250 /** | |
| 251 * Set the document's metadata, if supported by the document | |
| 252 * type. The creationDate and modifiedDate parameters can be | |
| 253 * nullptr. For example: | |
| 254 * | |
| 255 * SkDocument* make_doc(SkWStream* output) { | |
| 256 * std::vector<SkDocument::Attribute> info; | |
| 257 * info.emplace_back(SkString("Title"), SkString("...")); | |
| 258 * info.emplace_back(SkString("Author"), SkString("...")); | |
| 259 * info.emplace_back(SkString("Subject"), SkString("...")); | |
| 260 * info.emplace_back(SkString("Keywords"), SkString("...")); | |
| 261 * info.emplace_back(SkString("Creator"), SkString("...")); | |
| 262 * SkTime::DateTime now; | |
| 263 * SkTime::GetDateTime(&now); | |
| 264 * SkDocument* doc = SkDocument::CreatePDF(output); | |
| 265 * doc->setMetadata(&info[0], (int)info.size(), &now, &now); | |
| 266 * return doc; | |
| 267 * } | |
| 268 */ | |
| 269 struct Attribute { | |
| 270 SkString fKey, fValue; | |
| 271 Attribute(const SkString& k, const SkString& v) : fKey(k), fValue(v) {} | |
| 272 }; | |
| 273 virtual void setMetadata(const SkDocument::Attribute[], | |
| 274 int /* attributeCount */, | |
| 275 const SkTime::DateTime* /* creationDate */, | |
| 276 const SkTime::DateTime* /* modifiedDate */) {} | |
| 277 #endif // SK_SUPPORT_LEGACY_DOCUMENT_API | |
| 278 | |
| 279 protected: | 188 protected: |
| 280 SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); | 189 SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); |
| 281 | 190 |
| 282 // note: subclasses must call close() in their destructor, as the base class | 191 // note: subclasses must call close() in their destructor, as the base class |
| 283 // cannot do this for them. | 192 // cannot do this for them. |
| 284 virtual ~SkDocument(); | 193 virtual ~SkDocument(); |
| 285 | 194 |
| 286 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, | 195 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
| 287 const SkRect& content) = 0; | 196 const SkRect& content) = 0; |
| 288 virtual void onEndPage() = 0; | 197 virtual void onEndPage() = 0; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 301 | 210 |
| 302 private: | 211 private: |
| 303 SkWStream* fStream; | 212 SkWStream* fStream; |
| 304 void (*fDoneProc)(SkWStream*, bool aborted); | 213 void (*fDoneProc)(SkWStream*, bool aborted); |
| 305 State fState; | 214 State fState; |
| 306 | 215 |
| 307 typedef SkRefCnt INHERITED; | 216 typedef SkRefCnt INHERITED; |
| 308 }; | 217 }; |
| 309 | 218 |
| 310 #endif | 219 #endif |
| OLD | NEW |