Bug 1728955: part 6) Log result of Windows' `OleSetClipboardResult`. r=masayuki
[gecko.git] / gfx / thebes / PrintTargetPS.cpp
blob06bb25377a9e05d47f3e65746f4cc6525dcb9e09
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 "PrintTargetPS.h"
8 #include "cairo.h"
9 #include "cairo-ps.h"
11 namespace mozilla::gfx {
13 static cairo_status_t write_func(void* closure, const unsigned char* data,
14 unsigned int length) {
15 nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
16 do {
17 uint32_t wrote = 0;
18 if (NS_FAILED(out->Write((const char*)data, length, &wrote))) {
19 break;
21 data += wrote;
22 length -= wrote;
23 } while (length > 0);
24 NS_ASSERTION(length == 0, "not everything was written to the file");
25 return CAIRO_STATUS_SUCCESS;
28 PrintTargetPS::PrintTargetPS(cairo_surface_t* aCairoSurface,
29 const IntSize& aSize, nsIOutputStream* aStream,
30 PageOrientation aOrientation)
31 : PrintTarget(aCairoSurface, aSize),
32 mStream(aStream),
33 mOrientation(aOrientation) {}
35 PrintTargetPS::~PrintTargetPS() {
36 // We get called first, then PrintTarget's dtor. That means that mStream
37 // is destroyed before PrintTarget's dtor calls cairo_surface_destroy. This
38 // can be a problem if Finish() hasn't been called on us, since
39 // cairo_surface_destroy will then call cairo_surface_finish and that will
40 // end up invoking write_func above with the by now dangling pointer mStream
41 // that mCairoSurface stored. To prevent that from happening we must call
42 // Flush here before mStream is deleted.
43 Finish();
46 /* static */
47 already_AddRefed<PrintTargetPS> PrintTargetPS::CreateOrNull(
48 nsIOutputStream* aStream, IntSize aSizeInPoints,
49 PageOrientation aOrientation) {
50 // The PS output does not specify the page size so to print landscape we need
51 // to rotate the drawing 90 degrees and print on portrait paper. If printing
52 // landscape, swap the width/height supplied to cairo to select a portrait
53 // print area. Our consumers are responsible for checking
54 // RotateForLandscape() and applying a rotation transform if true.
55 if (aOrientation == LANDSCAPE) {
56 std::swap(aSizeInPoints.width, aSizeInPoints.height);
59 cairo_surface_t* surface = cairo_ps_surface_create_for_stream(
60 write_func, (void*)aStream, aSizeInPoints.width, aSizeInPoints.height);
61 if (cairo_surface_status(surface)) {
62 return nullptr;
64 cairo_ps_surface_restrict_to_level(surface, CAIRO_PS_LEVEL_2);
66 // The new object takes ownership of our surface reference.
67 RefPtr<PrintTargetPS> target =
68 new PrintTargetPS(surface, aSizeInPoints, aStream, aOrientation);
69 return target.forget();
72 nsresult PrintTargetPS::BeginPrinting(const nsAString& aTitle,
73 const nsAString& aPrintToFileName,
74 int32_t aStartPage, int32_t aEndPage) {
75 if (mOrientation == PORTRAIT) {
76 cairo_ps_surface_dsc_comment(mCairoSurface, "%%Orientation: Portrait");
77 } else {
78 cairo_ps_surface_dsc_comment(mCairoSurface, "%%Orientation: Landscape");
80 return NS_OK;
83 nsresult PrintTargetPS::EndPage() {
84 cairo_surface_show_page(mCairoSurface);
85 return NS_OK;
88 void PrintTargetPS::Finish() {
89 if (mIsFinished) {
90 return; // We don't want to call Close() on mStream more than once
92 PrintTarget::Finish();
93 mStream->Close();
96 } // namespace mozilla::gfx