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