michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gfxPSSurface.h" michael@0: michael@0: #include "cairo.h" michael@0: #include "cairo-ps.h" michael@0: michael@0: static cairo_status_t michael@0: write_func(void *closure, const unsigned char *data, unsigned int length) michael@0: { michael@0: nsCOMPtr out = reinterpret_cast(closure); michael@0: do { michael@0: uint32_t wrote = 0; michael@0: if (NS_FAILED(out->Write((const char*)data, length, &wrote))) michael@0: break; michael@0: data += wrote; length -= wrote; michael@0: } while (length > 0); michael@0: NS_ASSERTION(length == 0, "not everything was written to the file"); michael@0: return CAIRO_STATUS_SUCCESS; michael@0: } michael@0: michael@0: gfxPSSurface::gfxPSSurface(nsIOutputStream *aStream, const gfxSize& aSizeInPoints, PageOrientation aOrientation) michael@0: : mStream(aStream), mXDPI(-1), mYDPI(-1), mOrientation(aOrientation) michael@0: { michael@0: mSize = gfxIntSize(aSizeInPoints.width, aSizeInPoints.height); michael@0: michael@0: // The PS output does not specify the page size so to print michael@0: // landscape we need to rotate the drawing 90 degrees and print on michael@0: // portrait paper. If printing landscape, swap the width/height michael@0: // supplied to cairo to select a portrait print area. gfxContext michael@0: // will perform the rotation when GetRotateForLandscape() is TRUE. michael@0: gfxIntSize cairoSize; michael@0: if (mOrientation == PORTRAIT) { michael@0: cairoSize = mSize; michael@0: } else { michael@0: cairoSize = gfxIntSize(mSize.height, mSize.width); michael@0: } michael@0: cairo_surface_t* ps_surface = cairo_ps_surface_create_for_stream(write_func, (void*)mStream, cairoSize.width, cairoSize.height); michael@0: cairo_ps_surface_restrict_to_level(ps_surface, CAIRO_PS_LEVEL_2); michael@0: Init(ps_surface); michael@0: } michael@0: michael@0: gfxPSSurface::~gfxPSSurface() michael@0: { michael@0: } michael@0: michael@0: nsresult michael@0: gfxPSSurface::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName) michael@0: { michael@0: if (mOrientation == PORTRAIT) { michael@0: cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Portrait"); michael@0: } else { michael@0: cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Landscape"); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: gfxPSSurface::EndPrinting() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: gfxPSSurface::AbortPrinting() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: gfxPSSurface::BeginPage() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: gfxPSSurface::EndPage() michael@0: { michael@0: cairo_surface_show_page(CairoSurface()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: gfxPSSurface::Finish() michael@0: { michael@0: gfxASurface::Finish(); michael@0: mStream->Close(); michael@0: } michael@0: michael@0: void michael@0: gfxPSSurface::SetDPI(double xDPI, double yDPI) michael@0: { michael@0: mXDPI = xDPI; michael@0: mYDPI = yDPI; michael@0: cairo_surface_set_fallback_resolution(CairoSurface(), xDPI, yDPI); michael@0: } michael@0: michael@0: void michael@0: gfxPSSurface::GetDPI(double *xDPI, double *yDPI) michael@0: { michael@0: *xDPI = mXDPI; michael@0: *yDPI = mYDPI; michael@0: } michael@0: