No bug - tagging 2af34b4c9adf8c8defd3251b569af9c38cc0a429 with FIREFOX_BETA_124_BASE...
[gecko.git] / gfx / thebes / PrintTargetPDF.cpp
blob3b61696f4c3da28d5ef4f3ccabb7953c1b7775dc
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 "PrintTargetPDF.h"
8 #include "cairo.h"
9 #include "cairo-pdf.h"
10 #include "mozilla/AppShutdown.h"
11 #include "mozilla/StaticPrefs_print.h"
12 #include "nsContentUtils.h"
13 #include "nsString.h"
15 namespace mozilla::gfx {
17 static cairo_status_t write_func(void* closure, const unsigned char* data,
18 unsigned int length) {
19 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
20 return CAIRO_STATUS_SUCCESS;
22 nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
23 do {
24 uint32_t wrote = 0;
25 if (NS_FAILED(out->Write((const char*)data, length, &wrote))) {
26 break;
28 data += wrote;
29 length -= wrote;
30 } while (length > 0);
31 NS_ASSERTION(length == 0, "not everything was written to the file");
32 return CAIRO_STATUS_SUCCESS;
35 PrintTargetPDF::PrintTargetPDF(cairo_surface_t* aCairoSurface,
36 const IntSize& aSize, nsIOutputStream* aStream)
37 : PrintTarget(aCairoSurface, aSize), mStream(aStream) {}
39 PrintTargetPDF::~PrintTargetPDF() {
40 // We get called first, then PrintTarget's dtor. That means that mStream
41 // is destroyed before PrintTarget's dtor calls cairo_surface_destroy. This
42 // can be a problem if Finish() hasn't been called on us, since
43 // cairo_surface_destroy will then call cairo_surface_finish and that will
44 // end up invoking write_func above with the by now dangling pointer mStream
45 // that mCairoSurface stored. To prevent that from happening we must call
46 // Flush here before mStream is deleted.
47 Finish();
50 /* static */
51 already_AddRefed<PrintTargetPDF> PrintTargetPDF::CreateOrNull(
52 nsIOutputStream* aStream, const IntSize& aSizeInPoints) {
53 if (NS_WARN_IF(!aStream)) {
54 return nullptr;
57 cairo_surface_t* surface = cairo_pdf_surface_create_for_stream(
58 write_func, (void*)aStream, aSizeInPoints.width, aSizeInPoints.height);
59 if (cairo_surface_status(surface)) {
60 return nullptr;
63 nsAutoString creatorName;
64 if (NS_SUCCEEDED(nsContentUtils::GetLocalizedString(
65 nsContentUtils::eBRAND_PROPERTIES, "brandFullName", creatorName)) &&
66 !creatorName.IsEmpty()) {
67 creatorName.Append(u" " MOZILLA_VERSION);
68 cairo_pdf_surface_set_metadata(surface, CAIRO_PDF_METADATA_CREATOR,
69 NS_ConvertUTF16toUTF8(creatorName).get());
72 // The new object takes ownership of our surface reference.
73 RefPtr<PrintTargetPDF> target =
74 new PrintTargetPDF(surface, aSizeInPoints, aStream);
75 return target.forget();
78 nsresult PrintTargetPDF::BeginPage(const IntSize& aSizeInPoints) {
79 if (StaticPrefs::
80 print_save_as_pdf_use_page_rule_size_as_paper_size_enabled()) {
81 cairo_pdf_surface_set_size(mCairoSurface, aSizeInPoints.width,
82 aSizeInPoints.height);
83 if (cairo_surface_status(mCairoSurface)) {
84 return NS_ERROR_FAILURE;
87 return PrintTarget::BeginPage(aSizeInPoints);
90 nsresult PrintTargetPDF::EndPage() {
91 cairo_surface_show_page(mCairoSurface);
92 return PrintTarget::EndPage();
95 void PrintTargetPDF::Finish() {
96 if (mIsFinished ||
97 AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
98 // We don't want to call Close() on mStream more than once, and we don't
99 // want to block shutdown if for some reason the user shuts down the
100 // browser mid print.
101 return;
103 PrintTarget::Finish();
104 mStream->Close();
107 } // namespace mozilla::gfx