OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include "SkDocument.h" | |
9 #include "SkPDFCanon.h" | 8 #include "SkPDFCanon.h" |
10 #include "SkPDFDevice.h" | 9 #include "SkPDFDevice.h" |
| 10 #include "SkPDFDocument.h" |
11 #include "SkPDFFont.h" | 11 #include "SkPDFFont.h" |
| 12 #include "SkPDFMetadata.h" |
12 #include "SkPDFStream.h" | 13 #include "SkPDFStream.h" |
13 #include "SkPDFTypes.h" | 14 #include "SkPDFTypes.h" |
14 #include "SkPDFUtils.h" | 15 #include "SkPDFUtils.h" |
15 #include "SkStream.h" | 16 #include "SkStream.h" |
16 #include "SkPDFMetadata.h" | |
17 | |
18 class SkPDFDict; | |
19 | 17 |
20 static void emit_pdf_header(SkWStream* stream) { | 18 static void emit_pdf_header(SkWStream* stream) { |
21 stream->writeText("%PDF-1.4\n%"); | 19 stream->writeText("%PDF-1.4\n%"); |
22 // The PDF spec recommends including a comment with four bytes, all | 20 // The PDF spec recommends including a comment with four bytes, all |
23 // with their high bits set. This is "Skia" with the high bits set. | 21 // with their high bits set. This is "Skia" with the high bits set. |
24 stream->write32(0xD3EBE9E1); | 22 stream->write32(0xD3EBE9E1); |
25 stream->writeText("\n"); | 23 stream->writeText("\n"); |
26 } | 24 } |
27 | 25 |
28 static void emit_pdf_footer(SkWStream* stream, | 26 static void emit_pdf_footer(SkWStream* stream, |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 if (notEmbeddableCount) { | 300 if (notEmbeddableCount) { |
303 *notEmbeddableCount = notEmbeddable; | 301 *notEmbeddableCount = notEmbeddable; |
304 } | 302 } |
305 } | 303 } |
306 #endif | 304 #endif |
307 | 305 |
308 template <typename T> static T* clone(const T* o) { return o ? new T(*o) : nullp
tr; } | 306 template <typename T> static T* clone(const T* o) { return o ? new T(*o) : nullp
tr; } |
309 //////////////////////////////////////////////////////////////////////////////// | 307 //////////////////////////////////////////////////////////////////////////////// |
310 | 308 |
311 namespace { | 309 namespace { |
312 class SkDocument_PDF : public SkDocument { | 310 class SkPDFDocument : public SkDocument { |
313 public: | 311 public: |
314 SkDocument_PDF(SkWStream* stream, | 312 SkPDFDocument(SkWStream* stream, |
315 void (*doneProc)(SkWStream*, bool), | 313 void (*doneProc)(SkWStream*, bool), |
316 SkScalar rasterDpi, | 314 SkScalar rasterDpi, |
317 SkPixelSerializer* jpegEncoder) | 315 SkPixelSerializer* jpegEncoder) |
318 : SkDocument(stream, doneProc) | 316 : SkDocument(stream, doneProc) |
319 , fRasterDpi(rasterDpi) { | 317 , fRasterDpi(rasterDpi) { |
320 fCanon.setPixelSerializer(SkSafeRef(jpegEncoder)); | 318 fCanon.setPixelSerializer(SkSafeRef(jpegEncoder)); |
321 } | 319 } |
322 | 320 |
323 virtual ~SkDocument_PDF() { | 321 virtual ~SkPDFDocument() { |
324 // subclasses must call close() in their destructors | 322 // subclasses must call close() in their destructors |
325 this->close(); | 323 this->close(); |
326 } | 324 } |
327 | 325 |
328 protected: | 326 protected: |
329 SkCanvas* onBeginPage(SkScalar width, SkScalar height, | 327 SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
330 const SkRect& trimBox) override { | 328 const SkRect& trimBox) override { |
331 SkASSERT(!fCanvas.get()); | 329 SkASSERT(!fCanvas.get()); |
332 | 330 |
333 SkISize pageSize = SkISize::Make( | 331 SkISize pageSize = SkISize::Make( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 private: | 371 private: |
374 SkPDFCanon fCanon; | 372 SkPDFCanon fCanon; |
375 SkTArray<sk_sp<const SkPDFDevice>> fPageDevices; | 373 SkTArray<sk_sp<const SkPDFDevice>> fPageDevices; |
376 sk_sp<SkCanvas> fCanvas; | 374 sk_sp<SkCanvas> fCanvas; |
377 SkScalar fRasterDpi; | 375 SkScalar fRasterDpi; |
378 SkPDFMetadata fMetadata; | 376 SkPDFMetadata fMetadata; |
379 }; | 377 }; |
380 } // namespace | 378 } // namespace |
381 /////////////////////////////////////////////////////////////////////////////// | 379 /////////////////////////////////////////////////////////////////////////////// |
382 | 380 |
| 381 sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream, |
| 382 void (*proc)(SkWStream*, bool), |
| 383 SkScalar dpi, |
| 384 SkPixelSerializer* jpeg) { |
| 385 return stream ? sk_make_sp<SkPDFDocument>(stream, proc, dpi, jpeg) : nullptr
; |
| 386 } |
| 387 |
383 SkDocument* SkDocument::CreatePDF(SkWStream* stream, SkScalar dpi) { | 388 SkDocument* SkDocument::CreatePDF(SkWStream* stream, SkScalar dpi) { |
384 return stream ? new SkDocument_PDF(stream, nullptr, dpi, nullptr) : nullptr; | 389 return SkPDFMakeDocument(stream, nullptr, dpi, nullptr).release(); |
385 } | 390 } |
386 | 391 |
387 SkDocument* SkDocument::CreatePDF(SkWStream* stream, | 392 SkDocument* SkDocument::CreatePDF(SkWStream* stream, |
388 SkScalar dpi, | 393 SkScalar dpi, |
389 SkPixelSerializer* jpegEncoder) { | 394 SkPixelSerializer* jpegEncoder) { |
390 return stream | 395 return SkPDFMakeDocument(stream, nullptr, dpi, jpegEncoder).release(); |
391 ? new SkDocument_PDF(stream, nullptr, dpi, jpegEncoder) | |
392 : nullptr; | |
393 } | 396 } |
394 | 397 |
395 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { | 398 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { |
396 SkFILEWStream* stream = new SkFILEWStream(path); | |
397 if (!stream->isValid()) { | |
398 delete stream; | |
399 return nullptr; | |
400 } | |
401 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; | 399 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; |
402 return new SkDocument_PDF(stream, delete_wstream, dpi, nullptr); | 400 SkAutoTDelete<SkFILEWStream> stream(new SkFILEWStream(path)); |
| 401 return stream->isValid() |
| 402 ? SkPDFMakeDocument(stream.detach(), delete_wstream, dpi, nullptr).relea
se() |
| 403 : nullptr; |
403 } | 404 } |
OLD | NEW |