|
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/. */ |
|
5 |
|
6 #include "gfxPSSurface.h" |
|
7 |
|
8 #include "cairo.h" |
|
9 #include "cairo-ps.h" |
|
10 |
|
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 } |
|
24 |
|
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); |
|
29 |
|
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 } |
|
45 |
|
46 gfxPSSurface::~gfxPSSurface() |
|
47 { |
|
48 } |
|
49 |
|
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 } |
|
60 |
|
61 nsresult |
|
62 gfxPSSurface::EndPrinting() |
|
63 { |
|
64 return NS_OK; |
|
65 } |
|
66 |
|
67 nsresult |
|
68 gfxPSSurface::AbortPrinting() |
|
69 { |
|
70 return NS_OK; |
|
71 } |
|
72 |
|
73 nsresult |
|
74 gfxPSSurface::BeginPage() |
|
75 { |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 nsresult |
|
80 gfxPSSurface::EndPage() |
|
81 { |
|
82 cairo_surface_show_page(CairoSurface()); |
|
83 return NS_OK; |
|
84 } |
|
85 |
|
86 void |
|
87 gfxPSSurface::Finish() |
|
88 { |
|
89 gfxASurface::Finish(); |
|
90 mStream->Close(); |
|
91 } |
|
92 |
|
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 } |
|
100 |
|
101 void |
|
102 gfxPSSurface::GetDPI(double *xDPI, double *yDPI) |
|
103 { |
|
104 *xDPI = mXDPI; |
|
105 *yDPI = mYDPI; |
|
106 } |
|
107 |