2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkDocument.h"
9 #include "SkXPSDevice.h"
12 class SkDocument_XPS : public SkDocument {
14 SkDocument_XPS(SkWStream* stream,
15 void (*doneProc)(SkWStream*, bool),
17 : SkDocument(stream, doneProc) {
18 const SkScalar kPointsPerMeter = SkDoubleToScalar(360000.0 / 127.0);
19 fUnitsPerMeter.set(kPointsPerMeter, kPointsPerMeter);
20 SkScalar pixelsPerMeterScale = SkDoubleToScalar(dpi * 5000.0 / 127.0);
21 fPixelsPerMeter.set(pixelsPerMeterScale, pixelsPerMeterScale);
22 fDevice.beginPortfolio(stream);
25 virtual ~SkDocument_XPS() {
26 // subclasses must call close() in their destructors
31 virtual SkCanvas* onBeginPage(SkScalar width,
33 const SkRect& trimBox) SK_OVERRIDE {
34 fDevice.beginSheet(fUnitsPerMeter, fPixelsPerMeter,
35 SkSize::Make(width, height));
36 fCanvas.reset(SkNEW_ARGS(SkCanvas, (&fDevice)));
37 fCanvas->clipRect(trimBox);
38 fCanvas->translate(trimBox.x(), trimBox.y());
42 void onEndPage() SK_OVERRIDE {
43 SkASSERT(fCanvas.get());
49 bool onClose(SkWStream*) SK_OVERRIDE {
50 SkASSERT(!fCanvas.get());
51 return fDevice.endPortfolio();
54 void onAbort() SK_OVERRIDE {}
58 SkAutoTUnref<SkCanvas> fCanvas;
59 SkVector fUnitsPerMeter;
60 SkVector fPixelsPerMeter;
63 ///////////////////////////////////////////////////////////////////////////////
65 SkDocument* SkDocument::CreateXPS(SkWStream* stream, SkScalar dpi) {
66 return stream ? SkNEW_ARGS(SkDocument_XPS, (stream, NULL, dpi)) : NULL;
69 static void delete_wstream(SkWStream* stream, bool aborted) {
73 SkDocument* SkDocument::CreateXPS(const char path[], SkScalar dpi) {
74 SkAutoTDelete<SkFILEWStream> stream(SkNEW_ARGS(SkFILEWStream, (path)));
75 if (!stream->isValid()) {
78 return SkNEW_ARGS(SkDocument_XPS, (stream.detach(), delete_wstream, dpi));