halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | #if defined(SK_BUILD_FOR_WIN32) |
| 10 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 11 | #include "SkDocument.h" |
| 12 | #include "SkXPSDevice.h" |
| 13 | #include "SkStream.h" |
| 14 | |
| 15 | class SkDocument_XPS : public SkDocument { |
| 16 | public: |
| 17 | SkDocument_XPS(SkWStream* stream, |
| 18 | void (*doneProc)(SkWStream*, bool), |
| 19 | SkScalar dpi) |
| 20 | : SkDocument(stream, doneProc) { |
| 21 | const SkScalar kPointsPerMeter = SkDoubleToScalar(360000.0 / 127.0); |
| 22 | fUnitsPerMeter.set(kPointsPerMeter, kPointsPerMeter); |
| 23 | SkScalar pixelsPerMeterScale = SkDoubleToScalar(dpi * 5000.0 / 127.0); |
| 24 | fPixelsPerMeter.set(pixelsPerMeterScale, pixelsPerMeterScale); |
| 25 | fDevice.beginPortfolio(stream); |
| 26 | } |
| 27 | |
| 28 | virtual ~SkDocument_XPS() { |
| 29 | // subclasses must call close() in their destructors |
| 30 | this->close(); |
| 31 | } |
| 32 | |
| 33 | protected: |
tfarina | f4219dd | 2015-04-27 17:18:28 -0700 | [diff] [blame] | 34 | SkCanvas* onBeginPage(SkScalar width, |
| 35 | SkScalar height, |
| 36 | const SkRect& trimBox) override { |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 37 | fDevice.beginSheet(fUnitsPerMeter, fPixelsPerMeter, |
| 38 | SkSize::Make(width, height)); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 39 | fCanvas.reset(new SkCanvas(&fDevice)); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 40 | fCanvas->clipRect(trimBox); |
| 41 | fCanvas->translate(trimBox.x(), trimBox.y()); |
| 42 | return fCanvas.get(); |
| 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | void onEndPage() override { |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 46 | SkASSERT(fCanvas.get()); |
| 47 | fCanvas->flush(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | fCanvas.reset(nullptr); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 49 | fDevice.endSheet(); |
| 50 | } |
| 51 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame^] | 52 | void onClose(SkWStream*) override { |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 53 | SkASSERT(!fCanvas.get()); |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame^] | 54 | (void)fDevice.endPortfolio(); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | void onAbort() override {} |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | SkXPSDevice fDevice; |
| 61 | SkAutoTUnref<SkCanvas> fCanvas; |
| 62 | SkVector fUnitsPerMeter; |
| 63 | SkVector fPixelsPerMeter; |
| 64 | }; |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 68 | sk_sp<SkDocument> SkDocument::MakeXPS(SkWStream* stream, SkScalar dpi) { |
| 69 | return stream ? sk_make_sp<SkDocument_XPS>(stream, nullptr, dpi) : nullptr; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 70 | } |
| 71 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 72 | static void delete_wstream(SkWStream* stream, bool aborted) { delete stream; } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 73 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 74 | sk_sp<SkDocument> SkDocument::MakeXPS(const char path[], SkScalar dpi) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 75 | SkAutoTDelete<SkFILEWStream> stream(new SkFILEWStream(path)); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 76 | if (!stream->isValid()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | return nullptr; |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 78 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 79 | return sk_make_sp<SkDocument_XPS>(stream.release(), delete_wstream, dpi); |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 80 | } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 81 | |
| 82 | #endif//defined(SK_BUILD_FOR_WIN32) |