gfx/thebes/gfxPSSurface.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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)
    13 {
    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;
    23 }
    25 gfxPSSurface::gfxPSSurface(nsIOutputStream *aStream, const gfxSize& aSizeInPoints, PageOrientation aOrientation)
    26     : mStream(aStream), mXDPI(-1), mYDPI(-1), mOrientation(aOrientation)
    27 {
    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);
    40     }
    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);
    44 }
    46 gfxPSSurface::~gfxPSSurface()
    47 {
    48 }
    50 nsresult
    51 gfxPSSurface::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName)
    52 {
    53     if (mOrientation == PORTRAIT) {
    54       cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Portrait");
    55     } else {
    56       cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Landscape");
    57     }
    58     return NS_OK;
    59 }
    61 nsresult
    62 gfxPSSurface::EndPrinting()
    63 {
    64     return NS_OK;
    65 }
    67 nsresult
    68 gfxPSSurface::AbortPrinting()
    69 {
    70     return NS_OK;
    71 }
    73 nsresult
    74 gfxPSSurface::BeginPage()
    75 {
    76     return NS_OK;
    77 }
    79 nsresult
    80 gfxPSSurface::EndPage()
    81 {
    82     cairo_surface_show_page(CairoSurface());
    83     return NS_OK;
    84 }
    86 void
    87 gfxPSSurface::Finish()
    88 {
    89     gfxASurface::Finish();
    90     mStream->Close();
    91 }
    93 void
    94 gfxPSSurface::SetDPI(double xDPI, double yDPI)
    95 {
    96     mXDPI = xDPI;
    97     mYDPI = yDPI;
    98     cairo_surface_set_fallback_resolution(CairoSurface(), xDPI, yDPI);
    99 }
   101 void
   102 gfxPSSurface::GetDPI(double *xDPI, double *yDPI)
   103 {
   104     *xDPI = mXDPI;
   105     *yDPI = mYDPI;
   106 }

mercurial