no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / gfx / thebes / PrintTargetSkPDF.cpp
blob2834877407b640bc89ccfcdb756915b66a00e290
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"
9 #include "nsString.h"
10 #include <vector>
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)),
18 mPageCanvas(nullptr),
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:
26 mPDFDoc = nullptr;
27 mRefPDFDoc = nullptr;
30 /* static */
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
41 // PDF metadata.
43 SkPDF::Metadata metadata;
44 metadata.fTitle = NS_ConvertUTF16toUTF8(aTitle).get();
45 metadata.fCreator = "Firefox";
46 SkTime::DateTime now;
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;
66 mPageDT = nullptr;
67 return PrintTarget::EndPage();
70 nsresult PrintTargetSkPDF::EndPrinting() {
71 mPDFDoc->close();
72 if (mRefPDFDoc) {
73 mRefPDFDoc->close();
75 mPageCanvas = nullptr;
76 mPageDT = nullptr;
77 return NS_OK;
80 void PrintTargetSkPDF::Finish() {
81 if (mIsFinished) {
82 return;
84 mOStream->flush();
85 PrintTarget::Finish();
88 already_AddRefed<DrawTarget> PrintTargetSkPDF::MakeDrawTarget(
89 const IntSize& aSize, DrawEventRecorder* aRecorder) {
90 if (aRecorder) {
91 return PrintTarget::MakeDrawTarget(aSize, aRecorder);
93 // MOZ_ASSERT(aSize == mSize, "Should mPageCanvas size match?");
94 if (!mPageCanvas) {
95 return nullptr;
97 mPageDT = Factory::CreateDrawTargetWithSkCanvas(mPageCanvas);
98 if (!mPageDT) {
99 mPageCanvas = nullptr;
100 return nullptr;
102 return do_AddRef(mPageDT);
105 already_AddRefed<DrawTarget> PrintTargetSkPDF::GetReferenceDrawTarget() {
106 if (!mRefDT) {
107 SkPDF::Metadata metadata;
108 // SkDocument stores a non-owning raw pointer to aStream
109 mRefPDFDoc = SkPDF::MakeDocument(&mRefOStream, metadata);
110 if (!mRefPDFDoc) {
111 return nullptr;
113 mRefCanvas = mRefPDFDoc->beginPage(mSize.width, mSize.height);
114 if (!mRefCanvas) {
115 return nullptr;
117 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetWithSkCanvas(mRefCanvas);
118 if (!dt) {
119 return nullptr;
121 mRefDT = std::move(dt);
124 return do_AddRef(mRefDT);
127 } // namespace mozilla::gfx