gfx/thebes/gfxPSSurface.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/thebes/gfxPSSurface.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "gfxPSSurface.h"
    1.10 +
    1.11 +#include "cairo.h"
    1.12 +#include "cairo-ps.h"
    1.13 +
    1.14 +static cairo_status_t
    1.15 +write_func(void *closure, const unsigned char *data, unsigned int length)
    1.16 +{
    1.17 +    nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
    1.18 +    do {
    1.19 +        uint32_t wrote = 0;
    1.20 +        if (NS_FAILED(out->Write((const char*)data, length, &wrote)))
    1.21 +            break;
    1.22 +        data += wrote; length -= wrote;
    1.23 +    } while (length > 0);
    1.24 +    NS_ASSERTION(length == 0, "not everything was written to the file");
    1.25 +    return CAIRO_STATUS_SUCCESS;
    1.26 +}
    1.27 +
    1.28 +gfxPSSurface::gfxPSSurface(nsIOutputStream *aStream, const gfxSize& aSizeInPoints, PageOrientation aOrientation)
    1.29 +    : mStream(aStream), mXDPI(-1), mYDPI(-1), mOrientation(aOrientation)
    1.30 +{
    1.31 +    mSize = gfxIntSize(aSizeInPoints.width, aSizeInPoints.height);
    1.32 +
    1.33 +    // The PS output does not specify the page size so to print
    1.34 +    // landscape we need to rotate the drawing 90 degrees and print on
    1.35 +    // portrait paper. If printing landscape, swap the width/height
    1.36 +    // supplied to cairo to select a portrait print area. gfxContext
    1.37 +    // will perform the rotation when GetRotateForLandscape() is TRUE.
    1.38 +    gfxIntSize cairoSize;
    1.39 +    if (mOrientation == PORTRAIT) {
    1.40 +        cairoSize = mSize;
    1.41 +    } else {
    1.42 +        cairoSize = gfxIntSize(mSize.height, mSize.width);
    1.43 +    }
    1.44 +    cairo_surface_t* ps_surface = cairo_ps_surface_create_for_stream(write_func, (void*)mStream, cairoSize.width, cairoSize.height);
    1.45 +    cairo_ps_surface_restrict_to_level(ps_surface, CAIRO_PS_LEVEL_2);
    1.46 +    Init(ps_surface);
    1.47 +}
    1.48 +
    1.49 +gfxPSSurface::~gfxPSSurface()
    1.50 +{
    1.51 +}
    1.52 +
    1.53 +nsresult
    1.54 +gfxPSSurface::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName)
    1.55 +{
    1.56 +    if (mOrientation == PORTRAIT) {
    1.57 +      cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Portrait");
    1.58 +    } else {
    1.59 +      cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Landscape");
    1.60 +    }
    1.61 +    return NS_OK;
    1.62 +}
    1.63 +
    1.64 +nsresult
    1.65 +gfxPSSurface::EndPrinting()
    1.66 +{
    1.67 +    return NS_OK;
    1.68 +}
    1.69 +
    1.70 +nsresult
    1.71 +gfxPSSurface::AbortPrinting()
    1.72 +{
    1.73 +    return NS_OK;
    1.74 +}
    1.75 +
    1.76 +nsresult
    1.77 +gfxPSSurface::BeginPage()
    1.78 +{
    1.79 +    return NS_OK;
    1.80 +}
    1.81 +
    1.82 +nsresult
    1.83 +gfxPSSurface::EndPage()
    1.84 +{
    1.85 +    cairo_surface_show_page(CairoSurface());
    1.86 +    return NS_OK;
    1.87 +}
    1.88 +
    1.89 +void
    1.90 +gfxPSSurface::Finish()
    1.91 +{
    1.92 +    gfxASurface::Finish();
    1.93 +    mStream->Close();
    1.94 +}
    1.95 +
    1.96 +void
    1.97 +gfxPSSurface::SetDPI(double xDPI, double yDPI)
    1.98 +{
    1.99 +    mXDPI = xDPI;
   1.100 +    mYDPI = yDPI;
   1.101 +    cairo_surface_set_fallback_resolution(CairoSurface(), xDPI, yDPI);
   1.102 +}
   1.103 +
   1.104 +void
   1.105 +gfxPSSurface::GetDPI(double *xDPI, double *yDPI)
   1.106 +{
   1.107 +    *xDPI = mXDPI;
   1.108 +    *yDPI = mYDPI;
   1.109 +}
   1.110 +

mercurial