Bug 1744135 Part 2: Annotate macOS font loading with font name if a crash happens...
[gecko.git] / gfx / thebes / PrintTargetSkPDF.cpp
blob6cf7d3729e1f350b0e69adca2ab7b3a1d17f34cd
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() {
58 mPageCanvas = mPDFDoc->beginPage(mSize.width, mSize.height);
60 return !mPageCanvas ? NS_ERROR_FAILURE : PrintTarget::BeginPage();
63 nsresult PrintTargetSkPDF::EndPage() {
64 mPageCanvas = nullptr;
65 mPageDT = nullptr;
66 return PrintTarget::EndPage();
69 nsresult PrintTargetSkPDF::EndPrinting() {
70 mPDFDoc->close();
71 if (mRefPDFDoc) {
72 mRefPDFDoc->close();
74 mPageCanvas = nullptr;
75 mPageDT = nullptr;
76 return NS_OK;
79 void PrintTargetSkPDF::Finish() {
80 if (mIsFinished) {
81 return;
83 mOStream->flush();
84 PrintTarget::Finish();
87 already_AddRefed<DrawTarget> PrintTargetSkPDF::MakeDrawTarget(
88 const IntSize& aSize, DrawEventRecorder* aRecorder) {
89 if (aRecorder) {
90 return PrintTarget::MakeDrawTarget(aSize, aRecorder);
92 // MOZ_ASSERT(aSize == mSize, "Should mPageCanvas size match?");
93 if (!mPageCanvas) {
94 return nullptr;
96 mPageDT = Factory::CreateDrawTargetWithSkCanvas(mPageCanvas);
97 if (!mPageDT) {
98 mPageCanvas = nullptr;
99 return nullptr;
101 return do_AddRef(mPageDT);
104 already_AddRefed<DrawTarget> PrintTargetSkPDF::GetReferenceDrawTarget() {
105 if (!mRefDT) {
106 SkPDF::Metadata metadata;
107 // SkDocument stores a non-owning raw pointer to aStream
108 mRefPDFDoc = SkPDF::MakeDocument(&mRefOStream, metadata);
109 if (!mRefPDFDoc) {
110 return nullptr;
112 mRefCanvas = mRefPDFDoc->beginPage(mSize.width, mSize.height);
113 if (!mRefCanvas) {
114 return nullptr;
116 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetWithSkCanvas(mRefCanvas);
117 if (!dt) {
118 return nullptr;
120 mRefDT = std::move(dt);
123 return do_AddRef(mRefDT);
126 } // namespace mozilla::gfx