Flutter Engine
The Flutter Engine
SkXPSDocument.cpp
Go to the documentation of this file.
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
9#if defined(SK_BUILD_FOR_WIN)
10
12
16#include "src/xps/SkXPSDevice.h"
17
18#include <XpsObjectModel.h>
19
20namespace {
21struct 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;
31 void onEndPage() override;
32 void onClose(SkWStream*) override;
33 void onAbort() override;
34};
35}
36
37SkXPSDocument::SkXPSDocument(SkWStream* stream,
38 SkScalar dpi,
39 SkTScopedComPtr<IXpsOMObjectFactory> xpsFactory)
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
52SkXPSDocument::~SkXPSDocument() {
53 // subclasses must call close() in their destructors
54 this->close();
55}
56
57SkCanvas* 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
63void SkXPSDocument::onEndPage() {
64 SkASSERT(fCanvas.get());
65 fCanvas.reset(nullptr);
66 fDevice.endSheet();
67}
68
69void SkXPSDocument::onClose(SkWStream*) {
70 SkASSERT(!fCanvas.get());
71 (void)fDevice.endPortfolio();
72}
73
74void SkXPSDocument::onAbort() {}
75
76///////////////////////////////////////////////////////////////////////////////
77
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)
#define SkASSERT(cond)
Definition: SkAssert.h:116
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
#define SkDoubleToScalar(x)
Definition: SkScalar.h:64
virtual void onClose(SkWStream *)=0
virtual void onAbort()=0
virtual SkCanvas * onBeginPage(SkScalar width, SkScalar height)=0
virtual void onEndPage()=0
float SkScalar
Definition: extension.cpp:12
SK_API sk_sp< SkDocument > MakeDocument(SkWStream *stream, const Metadata &metadata)
Definition: ref_ptr.h:256
SkScalar w
SkScalar h
int32_t height
int32_t width
Definition: SkSize.h:16