OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/pdf_ps_metafile_cairo.h" | 5 #include "printing/pdf_ps_metafile_cairo.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include <cairo.h> | 9 #include <cairo.h> |
10 #include <cairo-pdf.h> | 10 #include <cairo-pdf.h> |
11 #include <cairo-ps.h> | |
12 | 11 |
13 #include "base/eintr_wrapper.h" | 12 #include "base/eintr_wrapper.h" |
14 #include "base/file_descriptor_posix.h" | 13 #include "base/file_descriptor_posix.h" |
15 #include "base/file_util.h" | 14 #include "base/file_util.h" |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "printing/units.h" | 16 #include "printing/units.h" |
18 #include "skia/ext/vector_platform_device_linux.h" | 17 #include "skia/ext/vector_platform_device_linux.h" |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 | 63 |
65 void DestroyContextData(void* data) { | 64 void DestroyContextData(void* data) { |
66 // Nothing to be done here. | 65 // Nothing to be done here. |
67 } | 66 } |
68 | 67 |
69 } // namespace | 68 } // namespace |
70 | 69 |
71 namespace printing { | 70 namespace printing { |
72 | 71 |
73 PdfPsMetafile::PdfPsMetafile() | 72 PdfPsMetafile::PdfPsMetafile() |
74 : format_(PDF), | 73 : surface_(NULL), context_(NULL) { |
vandebo (ex-Chrome)
2011/02/25 00:10:35
This should be either one two lines, or both on th
dpapad
2011/02/25 02:26:34
Done.
| |
75 surface_(NULL), context_(NULL) { | |
76 } | |
77 | |
78 PdfPsMetafile::PdfPsMetafile(const FileFormat& format) | |
79 : format_(format), | |
80 surface_(NULL), context_(NULL) { | |
81 } | 74 } |
82 | 75 |
83 PdfPsMetafile::~PdfPsMetafile() { | 76 PdfPsMetafile::~PdfPsMetafile() { |
84 // Releases all resources if we forgot to do so. | 77 // Releases all resources if we forgot to do so. |
85 CleanUpAll(); | 78 CleanUpAll(); |
86 } | 79 } |
87 | 80 |
88 bool PdfPsMetafile::Init() { | 81 bool PdfPsMetafile::Init() { |
89 // We need to check at least these two members to ensure Init() has not been | 82 // We need to check at least these two members to ensure Init() has not been |
90 // called before. | 83 // called before. |
91 DCHECK(!context_); | 84 DCHECK(!context_); |
92 DCHECK(data_.empty()); | 85 DCHECK(data_.empty()); |
93 | 86 |
94 // Creates an 1 by 1 Cairo surface for entire PDF/PS file. | 87 // Creates an 1 by 1 Cairo surface for the entire PDF file. |
95 // The size for each page will be overwritten later in StartPage(). | 88 // The size for each page will be overwritten later in StartPage(). |
96 switch (format_) { | 89 surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream, |
97 case PDF: | 90 &data_, 1, 1); |
98 surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream, | |
99 &data_, 1, 1); | |
100 break; | |
101 | |
102 case PS: | |
103 surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream, | |
104 &data_, 1, 1); | |
105 break; | |
106 | |
107 default: | |
108 NOTREACHED(); | |
109 return false; | |
110 } | |
111 | 91 |
112 // Cairo always returns a valid pointer. | 92 // Cairo always returns a valid pointer. |
113 // Hence, we have to check if it points to a "nil" object. | 93 // Hence, we have to check if it points to a "nil" object. |
114 if (!IsSurfaceValid(surface_)) { | 94 if (!IsSurfaceValid(surface_)) { |
115 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!"; | 95 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!"; |
116 CleanUpSurface(&surface_); | 96 CleanUpSurface(&surface_); |
117 return false; | 97 return false; |
118 } | 98 } |
119 | 99 |
120 // Creates a context. | 100 // Creates a context. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 double width = | 161 double width = |
182 width_in_points + margin_left_in_points + margin_right_in_points; | 162 width_in_points + margin_left_in_points + margin_right_in_points; |
183 double height = | 163 double height = |
184 height_in_points + margin_top_in_points + margin_bottom_in_points; | 164 height_in_points + margin_top_in_points + margin_bottom_in_points; |
185 | 165 |
186 // Don't let WebKit draw over the margins. | 166 // Don't let WebKit draw over the margins. |
187 cairo_surface_set_device_offset(surface_, | 167 cairo_surface_set_device_offset(surface_, |
188 margin_left_in_points, | 168 margin_left_in_points, |
189 margin_top_in_points); | 169 margin_top_in_points); |
190 | 170 |
191 switch (format_) { | 171 cairo_pdf_surface_set_size(surface_, width, height); |
192 case PDF: | |
193 cairo_pdf_surface_set_size(surface_, width, height); | |
194 break; | |
195 | |
196 case PS: | |
197 cairo_ps_surface_set_size(surface_, width, height); | |
198 break; | |
199 | |
200 default: | |
201 NOTREACHED(); | |
202 CleanUpAll(); | |
203 return NULL; | |
204 } | |
205 | |
206 return context_; | 172 return context_; |
207 } | 173 } |
208 | 174 |
209 bool PdfPsMetafile::FinishPage() { | 175 bool PdfPsMetafile::FinishPage() { |
210 DCHECK(IsSurfaceValid(surface_)); | 176 DCHECK(IsSurfaceValid(surface_)); |
211 DCHECK(IsContextValid(context_)); | 177 DCHECK(IsContextValid(context_)); |
212 | 178 |
213 // Flushes all rendering for current page. | 179 // Flushes all rendering for current page. |
214 cairo_surface_flush(surface_); | 180 cairo_surface_flush(surface_); |
215 cairo_show_page(context_); | 181 cairo_show_page(context_); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 data_.clear(); | 255 data_.clear(); |
290 skia::VectorPlatformDevice::ClearFontCache(); | 256 skia::VectorPlatformDevice::ClearFontCache(); |
291 } | 257 } |
292 | 258 |
293 const double PdfPsMetafile::kTopMarginInInch = 0.25; | 259 const double PdfPsMetafile::kTopMarginInInch = 0.25; |
294 const double PdfPsMetafile::kBottomMarginInInch = 0.56; | 260 const double PdfPsMetafile::kBottomMarginInInch = 0.56; |
295 const double PdfPsMetafile::kLeftMarginInInch = 0.25; | 261 const double PdfPsMetafile::kLeftMarginInInch = 0.25; |
296 const double PdfPsMetafile::kRightMarginInInch = 0.25; | 262 const double PdfPsMetafile::kRightMarginInInch = 0.25; |
297 | 263 |
298 } // namespace printing | 264 } // namespace printing |
OLD | NEW |