1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "PrintTargetSkPDF.h"
8 #include "mozilla/gfx/2D.h"
12 namespace mozilla::gfx
{
14 PrintTargetSkPDF::PrintTargetSkPDF(const IntSize
& aSize
,
15 UniquePtr
<SkWStream
> aStream
)
16 : PrintTarget(/* not using cairo_surface_t */ nullptr, aSize
),
17 mOStream(std::move(aStream
)),
19 mRefCanvas(nullptr) {}
21 PrintTargetSkPDF::~PrintTargetSkPDF() {
22 Finish(); // ensure stream is flushed
24 // Make sure mPDFDoc and mRefPDFDoc are destroyed before our member streams
25 // (which they wrap) are destroyed:
31 already_AddRefed
<PrintTargetSkPDF
> PrintTargetSkPDF::CreateOrNull(
32 UniquePtr
<SkWStream
> aStream
, const IntSize
& aSizeInPoints
) {
33 return do_AddRef(new PrintTargetSkPDF(aSizeInPoints
, std::move(aStream
)));
36 nsresult
PrintTargetSkPDF::BeginPrinting(const nsAString
& aTitle
,
37 const nsAString
& aPrintToFileName
,
38 int32_t aStartPage
, int32_t aEndPage
) {
39 // We need to create the SkPDFDocument here rather than in CreateOrNull
40 // because it's only now that we are given aTitle which we want for the
43 SkPDF::Metadata metadata
;
44 metadata
.fTitle
= NS_ConvertUTF16toUTF8(aTitle
).get();
45 metadata
.fCreator
= "Firefox";
47 SkTime::GetDateTime(&now
);
48 metadata
.fCreation
= now
;
49 metadata
.fModified
= now
;
51 // SkDocument stores a non-owning raw pointer to aStream
52 mPDFDoc
= SkPDF::MakeDocument(mOStream
.get(), metadata
);
54 return mPDFDoc
? NS_OK
: NS_ERROR_FAILURE
;
57 nsresult
PrintTargetSkPDF::BeginPage() {
58 mPageCanvas
= mPDFDoc
->beginPage(mSize
.width
, mSize
.height
);
60 return !mPageCanvas
? NS_ERROR_FAILURE
: PrintTarget::BeginPage();
63 nsresult
PrintTargetSkPDF::EndPage() {
64 mPageCanvas
= nullptr;
66 return PrintTarget::EndPage();
69 nsresult
PrintTargetSkPDF::EndPrinting() {
74 mPageCanvas
= nullptr;
79 void PrintTargetSkPDF::Finish() {
84 PrintTarget::Finish();
87 already_AddRefed
<DrawTarget
> PrintTargetSkPDF::MakeDrawTarget(
88 const IntSize
& aSize
, DrawEventRecorder
* aRecorder
) {
90 return PrintTarget::MakeDrawTarget(aSize
, aRecorder
);
92 // MOZ_ASSERT(aSize == mSize, "Should mPageCanvas size match?");
96 mPageDT
= Factory::CreateDrawTargetWithSkCanvas(mPageCanvas
);
98 mPageCanvas
= nullptr;
101 return do_AddRef(mPageDT
);
104 already_AddRefed
<DrawTarget
> PrintTargetSkPDF::GetReferenceDrawTarget() {
106 SkPDF::Metadata metadata
;
107 // SkDocument stores a non-owning raw pointer to aStream
108 mRefPDFDoc
= SkPDF::MakeDocument(&mRefOStream
, metadata
);
112 mRefCanvas
= mRefPDFDoc
->beginPage(mSize
.width
, mSize
.height
);
116 RefPtr
<DrawTarget
> dt
= Factory::CreateDrawTargetWithSkCanvas(mRefCanvas
);
120 mRefDT
= std::move(dt
);
123 return do_AddRef(mRefDT
);
126 } // namespace mozilla::gfx