aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/xps/SkDocument_XPS.cpp
blob: 33edf789e4355fa56e3d9c1ded88f70f31b3aab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkTypes.h"
#if defined(SK_BUILD_FOR_WIN32)

#include "SkDocument.h"
#include "SkXPSDevice.h"
#include "SkStream.h"

class SkDocument_XPS : public SkDocument {
public:
    SkDocument_XPS(SkWStream* stream,
                   void (*doneProc)(SkWStream*, bool),
                   SkScalar dpi)
        : SkDocument(stream, doneProc) {
        const SkScalar kPointsPerMeter = SkDoubleToScalar(360000.0 / 127.0);
        fUnitsPerMeter.set(kPointsPerMeter, kPointsPerMeter);
        SkScalar pixelsPerMeterScale = SkDoubleToScalar(dpi * 5000.0 / 127.0);
        fPixelsPerMeter.set(pixelsPerMeterScale, pixelsPerMeterScale);
        fDevice.beginPortfolio(stream);
    }

    virtual ~SkDocument_XPS() {
        // subclasses must call close() in their destructors
        this->close();
    }

protected:
    SkCanvas* onBeginPage(SkScalar width,
                          SkScalar height,
                          const SkRect& trimBox) override {
        fDevice.beginSheet(fUnitsPerMeter, fPixelsPerMeter,
                           SkSize::Make(width, height));
        fCanvas.reset(new SkCanvas(&fDevice));
        fCanvas->clipRect(trimBox);
        fCanvas->translate(trimBox.x(), trimBox.y());
        return fCanvas.get();
    }

    void onEndPage() override {
        SkASSERT(fCanvas.get());
        fCanvas->flush();
        fCanvas.reset(nullptr);
        fDevice.endSheet();
    }

    bool onClose(SkWStream*) override {
        SkASSERT(!fCanvas.get());
        return fDevice.endPortfolio();
    }

    void onAbort() override {}

private:
    SkXPSDevice fDevice;
    SkAutoTUnref<SkCanvas> fCanvas;
    SkVector fUnitsPerMeter;
    SkVector fPixelsPerMeter;
};

///////////////////////////////////////////////////////////////////////////////

SkDocument* SkDocument::CreateXPS(SkWStream* stream, SkScalar dpi) {
    return stream ? new SkDocument_XPS(stream, nullptr, dpi) : nullptr;
}

static void delete_wstream(SkWStream* stream, bool aborted) { delete stream; }

SkDocument* SkDocument::CreateXPS(const char path[], SkScalar dpi) {
    SkAutoTDelete<SkFILEWStream> stream(new SkFILEWStream(path));
    if (!stream->isValid()) {
        return nullptr;
    }
    return new SkDocument_XPS(stream.release(), delete_wstream, dpi);
}

#endif//defined(SK_BUILD_FOR_WIN32)