Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "gfxPSSurface.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "cairo.h" |
michael@0 | 9 | #include "cairo-ps.h" |
michael@0 | 10 | |
michael@0 | 11 | static cairo_status_t |
michael@0 | 12 | write_func(void *closure, const unsigned char *data, unsigned int length) |
michael@0 | 13 | { |
michael@0 | 14 | nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure); |
michael@0 | 15 | do { |
michael@0 | 16 | uint32_t wrote = 0; |
michael@0 | 17 | if (NS_FAILED(out->Write((const char*)data, length, &wrote))) |
michael@0 | 18 | break; |
michael@0 | 19 | data += wrote; length -= wrote; |
michael@0 | 20 | } while (length > 0); |
michael@0 | 21 | NS_ASSERTION(length == 0, "not everything was written to the file"); |
michael@0 | 22 | return CAIRO_STATUS_SUCCESS; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | gfxPSSurface::gfxPSSurface(nsIOutputStream *aStream, const gfxSize& aSizeInPoints, PageOrientation aOrientation) |
michael@0 | 26 | : mStream(aStream), mXDPI(-1), mYDPI(-1), mOrientation(aOrientation) |
michael@0 | 27 | { |
michael@0 | 28 | mSize = gfxIntSize(aSizeInPoints.width, aSizeInPoints.height); |
michael@0 | 29 | |
michael@0 | 30 | // The PS output does not specify the page size so to print |
michael@0 | 31 | // landscape we need to rotate the drawing 90 degrees and print on |
michael@0 | 32 | // portrait paper. If printing landscape, swap the width/height |
michael@0 | 33 | // supplied to cairo to select a portrait print area. gfxContext |
michael@0 | 34 | // will perform the rotation when GetRotateForLandscape() is TRUE. |
michael@0 | 35 | gfxIntSize cairoSize; |
michael@0 | 36 | if (mOrientation == PORTRAIT) { |
michael@0 | 37 | cairoSize = mSize; |
michael@0 | 38 | } else { |
michael@0 | 39 | cairoSize = gfxIntSize(mSize.height, mSize.width); |
michael@0 | 40 | } |
michael@0 | 41 | cairo_surface_t* ps_surface = cairo_ps_surface_create_for_stream(write_func, (void*)mStream, cairoSize.width, cairoSize.height); |
michael@0 | 42 | cairo_ps_surface_restrict_to_level(ps_surface, CAIRO_PS_LEVEL_2); |
michael@0 | 43 | Init(ps_surface); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | gfxPSSurface::~gfxPSSurface() |
michael@0 | 47 | { |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | nsresult |
michael@0 | 51 | gfxPSSurface::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName) |
michael@0 | 52 | { |
michael@0 | 53 | if (mOrientation == PORTRAIT) { |
michael@0 | 54 | cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Portrait"); |
michael@0 | 55 | } else { |
michael@0 | 56 | cairo_ps_surface_dsc_comment (mSurface, "%%Orientation: Landscape"); |
michael@0 | 57 | } |
michael@0 | 58 | return NS_OK; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | nsresult |
michael@0 | 62 | gfxPSSurface::EndPrinting() |
michael@0 | 63 | { |
michael@0 | 64 | return NS_OK; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | nsresult |
michael@0 | 68 | gfxPSSurface::AbortPrinting() |
michael@0 | 69 | { |
michael@0 | 70 | return NS_OK; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | nsresult |
michael@0 | 74 | gfxPSSurface::BeginPage() |
michael@0 | 75 | { |
michael@0 | 76 | return NS_OK; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | nsresult |
michael@0 | 80 | gfxPSSurface::EndPage() |
michael@0 | 81 | { |
michael@0 | 82 | cairo_surface_show_page(CairoSurface()); |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | void |
michael@0 | 87 | gfxPSSurface::Finish() |
michael@0 | 88 | { |
michael@0 | 89 | gfxASurface::Finish(); |
michael@0 | 90 | mStream->Close(); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | void |
michael@0 | 94 | gfxPSSurface::SetDPI(double xDPI, double yDPI) |
michael@0 | 95 | { |
michael@0 | 96 | mXDPI = xDPI; |
michael@0 | 97 | mYDPI = yDPI; |
michael@0 | 98 | cairo_surface_set_fallback_resolution(CairoSurface(), xDPI, yDPI); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | void |
michael@0 | 102 | gfxPSSurface::GetDPI(double *xDPI, double *yDPI) |
michael@0 | 103 | { |
michael@0 | 104 | *xDPI = mXDPI; |
michael@0 | 105 | *yDPI = mYDPI; |
michael@0 | 106 | } |
michael@0 | 107 |