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(const IntSize
& aSizeInPoints
) {
58 mPageCanvas
= mPDFDoc
->beginPage(mSize
.width
, mSize
.height
);
60 return !mPageCanvas
? NS_ERROR_FAILURE
61 : PrintTarget::BeginPage(aSizeInPoints
);
64 nsresult
PrintTargetSkPDF::EndPage() {
65 mPageCanvas
= nullptr;
67 return PrintTarget::EndPage();
70 nsresult
PrintTargetSkPDF::EndPrinting() {
75 mPageCanvas
= nullptr;
80 void PrintTargetSkPDF::Finish() {
85 PrintTarget::Finish();
88 already_AddRefed
<DrawTarget
> PrintTargetSkPDF::MakeDrawTarget(
89 const IntSize
& aSize
, DrawEventRecorder
* aRecorder
) {
91 return PrintTarget::MakeDrawTarget(aSize
, aRecorder
);
93 // MOZ_ASSERT(aSize == mSize, "Should mPageCanvas size match?");
97 mPageDT
= Factory::CreateDrawTargetWithSkCanvas(mPageCanvas
);
99 mPageCanvas
= nullptr;
102 return do_AddRef(mPageDT
);
105 already_AddRefed
<DrawTarget
> PrintTargetSkPDF::GetReferenceDrawTarget() {
107 SkPDF::Metadata metadata
;
108 // SkDocument stores a non-owning raw pointer to aStream
109 mRefPDFDoc
= SkPDF::MakeDocument(&mRefOStream
, metadata
);
113 mRefCanvas
= mRefPDFDoc
->beginPage(mSize
.width
, mSize
.height
);
117 RefPtr
<DrawTarget
> dt
= Factory::CreateDrawTargetWithSkCanvas(mRefCanvas
);
121 mRefDT
= std::move(dt
);
124 return do_AddRef(mRefDT
);
127 } // namespace mozilla::gfx