Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [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 | |
| 8 | |
| 9 | #include "SkTypes.h" |
| 10 | #if defined(SK_BUILD_FOR_WIN32) |
| 11 | |
| 12 | #include "SkXPSDocument.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkHRESULT.h" |
| 15 | |
| 16 | SkXPSDocument::SkXPSDocument(SkWStream* stream, |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 17 | SkScalar dpi, |
| 18 | SkTScopedComPtr<IXpsOMObjectFactory> xpsFactory) |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 19 | : SkDocument(stream, nullptr) |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 20 | , fXpsFactory(std::move(xpsFactory)) |
| 21 | , fDevice(SkISize{10000, 10000}) |
| 22 | { |
| 23 | const SkScalar kPointsPerMeter = SkDoubleToScalar(360000.0 / 127.0); |
| 24 | fUnitsPerMeter.set(kPointsPerMeter, kPointsPerMeter); |
| 25 | SkScalar pixelsPerMeterScale = SkDoubleToScalar(dpi * 5000.0 / 127.0); |
| 26 | fPixelsPerMeter.set(pixelsPerMeterScale, pixelsPerMeterScale); |
| 27 | SkASSERT(fXpsFactory); |
| 28 | fDevice.beginPortfolio(stream, fXpsFactory.get()); |
| 29 | } |
| 30 | |
| 31 | SkXPSDocument::~SkXPSDocument() { |
| 32 | // subclasses must call close() in their destructors |
| 33 | this->close(); |
| 34 | } |
| 35 | |
Hal Canary | b1de5f9 | 2017-07-01 22:17:15 -0400 | [diff] [blame] | 36 | SkCanvas* SkXPSDocument::onBeginPage(SkScalar width, SkScalar height) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 37 | fDevice.beginSheet(fUnitsPerMeter, fPixelsPerMeter, {width, height}); |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 38 | fCanvas.reset(new SkCanvas(&fDevice)); |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 39 | return fCanvas.get(); |
| 40 | } |
| 41 | |
| 42 | void SkXPSDocument::onEndPage() { |
| 43 | SkASSERT(fCanvas.get()); |
| 44 | fCanvas->flush(); |
| 45 | fCanvas.reset(nullptr); |
| 46 | fDevice.endSheet(); |
| 47 | } |
| 48 | |
| 49 | void SkXPSDocument::onClose(SkWStream*) { |
| 50 | SkASSERT(!fCanvas.get()); |
| 51 | (void)fDevice.endPortfolio(); |
| 52 | } |
| 53 | |
| 54 | void SkXPSDocument::onAbort() {} |
| 55 | |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 58 | sk_sp<SkDocument> SkDocument::MakeXPS(SkWStream* stream, |
| 59 | IXpsOMObjectFactory* factoryPtr, |
| 60 | SkScalar dpi) { |
| 61 | SkTScopedComPtr<IXpsOMObjectFactory> factory(SkSafeRefComPtr(factoryPtr)); |
| 62 | return stream && factory |
Hal Canary | 5e221e7 | 2017-02-06 09:51:42 -0500 | [diff] [blame] | 63 | ? sk_make_sp<SkXPSDocument>(stream, dpi, std::move(factory)) |
Hal Canary | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 64 | : nullptr; |
| 65 | } |
| 66 | |
| 67 | #endif//defined(SK_BUILD_FOR_WIN32) |