Bumping manifests a=b2g-bump
[gecko.git] / gfx / thebes / gfxPSSurface.cpp
blob250e79f63cee09683c4db09aeeaf58ffb10b5e8a
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"
8 #include "cairo.h"
9 #include "cairo-ps.h"
11 static cairo_status_t
12 write_func(void *closure, const unsigned char *data, unsigned int length)
14 nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
15 do {
16 uint32_t wrote = 0;
17 if (NS_FAILED(out->Write((const char*)data, length, &wrote)))
18 break;
19 data += wrote; length -= wrote;
20 } while (length > 0);
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.
35 gfxIntSize cairoSize;
36 if (mOrientation == PORTRAIT) {
37 cairoSize = mSize;
38 } else {
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);
43 Init(ps_surface);
46 gfxPSSurface::~gfxPSSurface()
50 nsresult
51 gfxPSSurface::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName)
53 if (mOrientation == PORTRAIT) {
54 cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Portrait");
55 } else {
56 cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Landscape");
58 return NS_OK;
61 nsresult
62 gfxPSSurface::EndPrinting()
64 return NS_OK;
67 nsresult
68 gfxPSSurface::AbortPrinting()
70 return NS_OK;
73 nsresult
74 gfxPSSurface::BeginPage()
76 return NS_OK;
79 nsresult
80 gfxPSSurface::EndPage()
82 cairo_surface_show_page(CairoSurface());
83 return NS_OK;
86 void
87 gfxPSSurface::Finish()
89 gfxASurface::Finish();
90 mStream->Close();
93 void
94 gfxPSSurface::SetDPI(double xDPI, double yDPI)
96 mXDPI = xDPI;
97 mYDPI = yDPI;
98 cairo_surface_set_fallback_resolution(CairoSurface(), xDPI, yDPI);
101 void
102 gfxPSSurface::GetDPI(double *xDPI, double *yDPI)
104 *xDPI = mXDPI;
105 *yDPI = mYDPI;