• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/core/SkTypes.h"
9 #if defined(SK_BUILD_FOR_WIN)
10 
11 #include "include/docs/SkXPSDocument.h"
12 
13 #include "include/core/SkStream.h"
14 #include "src/utils/win/SkHRESULT.h"
15 #include "src/utils/win/SkTScopedComPtr.h"
16 #include "src/xps/SkXPSDevice.h"
17 
18 #include <XpsObjectModel.h>
19 
20 namespace {
21 struct SkXPSDocument final : public SkDocument {
22     SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
23     SkXPSDevice fDevice;
24     std::unique_ptr<SkCanvas> fCanvas;
25     SkVector fUnitsPerMeter;
26     SkVector fPixelsPerMeter;
27 
28     SkXPSDocument(SkWStream*, SkScalar dpi, SkTScopedComPtr<IXpsOMObjectFactory>);
29     ~SkXPSDocument() override;
30     SkCanvas* onBeginPage(SkScalar w, SkScalar h) override;
31     void onEndPage() override;
32     void onClose(SkWStream*) override;
33     void onAbort() override;
34 };
35 }
36 
SkXPSDocument(SkWStream * stream,SkScalar dpi,SkTScopedComPtr<IXpsOMObjectFactory> xpsFactory)37 SkXPSDocument::SkXPSDocument(SkWStream* stream,
38                    SkScalar dpi,
39                    SkTScopedComPtr<IXpsOMObjectFactory> xpsFactory)
40         : SkDocument(stream)
41         , fXpsFactory(std::move(xpsFactory))
42         , fDevice(SkISize{10000, 10000})
43 {
44     const SkScalar kPointsPerMeter = SkDoubleToScalar(360000.0 / 127.0);
45     fUnitsPerMeter.set(kPointsPerMeter, kPointsPerMeter);
46     SkScalar pixelsPerMeterScale = SkDoubleToScalar(dpi * 5000.0 / 127.0);
47     fPixelsPerMeter.set(pixelsPerMeterScale, pixelsPerMeterScale);
48     SkASSERT(fXpsFactory);
49     fDevice.beginPortfolio(stream, fXpsFactory.get());
50 }
51 
~SkXPSDocument()52 SkXPSDocument::~SkXPSDocument() {
53     // subclasses must call close() in their destructors
54     this->close();
55 }
56 
onBeginPage(SkScalar width,SkScalar height)57 SkCanvas* SkXPSDocument::onBeginPage(SkScalar width, SkScalar height) {
58     fDevice.beginSheet(fUnitsPerMeter, fPixelsPerMeter, {width, height});
59     fCanvas.reset(new SkCanvas(sk_ref_sp(&fDevice)));
60     return fCanvas.get();
61 }
62 
onEndPage()63 void SkXPSDocument::onEndPage() {
64     SkASSERT(fCanvas.get());
65     fCanvas.reset(nullptr);
66     fDevice.endSheet();
67 }
68 
onClose(SkWStream *)69 void SkXPSDocument::onClose(SkWStream*) {
70     SkASSERT(!fCanvas.get());
71     (void)fDevice.endPortfolio();
72 }
73 
onAbort()74 void SkXPSDocument::onAbort() {}
75 
76 ///////////////////////////////////////////////////////////////////////////////
77 
MakeDocument(SkWStream * stream,IXpsOMObjectFactory * factoryPtr,SkScalar dpi)78 sk_sp<SkDocument> SkXPS::MakeDocument(SkWStream* stream,
79                                       IXpsOMObjectFactory* factoryPtr,
80                                       SkScalar dpi) {
81     SkTScopedComPtr<IXpsOMObjectFactory> factory(SkSafeRefComPtr(factoryPtr));
82     return stream && factory
83            ? sk_make_sp<SkXPSDocument>(stream, dpi, std::move(factory))
84            : nullptr;
85 }
86 #endif  // defined(SK_BUILD_FOR_WIN)
87