1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 "gfxPSSurface.h"
12 write_func(void *closure
, const unsigned char *data
, unsigned int length
)
14 nsCOMPtr
<nsIOutputStream
> out
= reinterpret_cast<nsIOutputStream
*>(closure
);
17 if (NS_FAILED(out
->Write((const char*)data
, length
, &wrote
)))
19 data
+= wrote
; length
-= wrote
;
21 NS_ASSERTION(length
== 0, "not everything was written to the file");
22 return CAIRO_STATUS_SUCCESS
;
25 gfxPSSurface::gfxPSSurface(nsIOutputStream
*aStream
, const gfxSize
& aSizeInPoints
, PageOrientation aOrientation
)
26 : mStream(aStream
), mXDPI(-1), mYDPI(-1), mOrientation(aOrientation
)
28 mSize
= gfxIntSize(aSizeInPoints
.width
, aSizeInPoints
.height
);
30 // The PS output does not specify the page size so to print
31 // landscape we need to rotate the drawing 90 degrees and print on
32 // portrait paper. If printing landscape, swap the width/height
33 // supplied to cairo to select a portrait print area. gfxContext
34 // will perform the rotation when GetRotateForLandscape() is TRUE.
36 if (mOrientation
== PORTRAIT
) {
39 cairoSize
= gfxIntSize(mSize
.height
, mSize
.width
);
41 cairo_surface_t
* ps_surface
= cairo_ps_surface_create_for_stream(write_func
, (void*)mStream
, cairoSize
.width
, cairoSize
.height
);
42 cairo_ps_surface_restrict_to_level(ps_surface
, CAIRO_PS_LEVEL_2
);
46 gfxPSSurface::~gfxPSSurface()
51 gfxPSSurface::BeginPrinting(const nsAString
& aTitle
, const nsAString
& aPrintToFileName
)
53 if (mOrientation
== PORTRAIT
) {
54 cairo_ps_surface_dsc_comment (mSurface
, "%%Orientation: Portrait");
56 cairo_ps_surface_dsc_comment (mSurface
, "%%Orientation: Landscape");
62 gfxPSSurface::EndPrinting()
68 gfxPSSurface::AbortPrinting()
74 gfxPSSurface::BeginPage()
80 gfxPSSurface::EndPage()
82 cairo_surface_show_page(CairoSurface());
87 gfxPSSurface::Finish()
89 gfxASurface::Finish();
94 gfxPSSurface::SetDPI(double xDPI
, double yDPI
)
98 cairo_surface_set_fallback_resolution(CairoSurface(), xDPI
, yDPI
);
102 gfxPSSurface::GetDPI(double *xDPI
, double *yDPI
)