Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(658)

Side by Side Diff: printing/pdf_ps_metafile_linux.cc

Issue 565043: OpenBSD/FreeBSD GYP changes (most of the remaining ones) (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "printing/pdf_ps_metafile_linux.h"
6
7 #include <stdio.h>
8
9 #include <cairo.h>
10 #include <cairo-pdf.h>
11 #include <cairo-ps.h>
12
13 #include "base/eintr_wrapper.h"
14 #include "base/file_descriptor_posix.h"
15 #include "base/file_util.h"
16 #include "base/logging.h"
17 #include "skia/ext/vector_platform_device_linux.h"
18
19 namespace {
20
21 // Tests if |surface| is valid.
22 bool IsSurfaceValid(cairo_surface_t* surface) {
23 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS;
24 }
25
26 // Tests if |context| is valid.
27 bool IsContextValid(cairo_t* context) {
28 return cairo_status(context) == CAIRO_STATUS_SUCCESS;
29 }
30
31 // Destroys and resets |surface|.
32 void CleanUpSurface(cairo_surface_t** surface) {
33 if (*surface) {
34 cairo_surface_destroy(*surface);
35 *surface = NULL;
36 }
37 }
38
39 // Destroys and resets |context|.
40 void CleanUpContext(cairo_t** context) {
41 if (*context) {
42 cairo_destroy(*context);
43 *context = NULL;
44 }
45 }
46
47 // Callback function for Cairo to write PDF/PS stream.
48 // |dst_buffer| is actually a pointer of type `std::string*`.
49 cairo_status_t WriteCairoStream(void* dst_buffer,
50 const unsigned char* src_data,
51 unsigned int src_data_length) {
52 DCHECK(dst_buffer);
53 DCHECK(src_data);
54 DCHECK_GT(src_data_length, 0u);
55
56 std::string* buffer = reinterpret_cast<std::string*>(dst_buffer);
57 buffer->append(reinterpret_cast<const char*>(src_data), src_data_length);
58
59 return CAIRO_STATUS_SUCCESS;
60 }
61
62 } // namespace
63
64 namespace printing {
65
66 PdfPsMetafile::PdfPsMetafile(const FileFormat& format)
67 : format_(format),
68 surface_(NULL), context_(NULL),
69 page_surface_(NULL), page_context_(NULL) {
70 }
71
72 PdfPsMetafile::~PdfPsMetafile() {
73 // Releases all resources if we forgot to do so.
74 CleanUpAll();
75 }
76
77 bool PdfPsMetafile::Init() {
78 // We need to check at least these two members to ensure Init() has not been
79 // called before. Passing these two checks also implies that surface_,
80 // page_surface_, and page_context_ are NULL, and current_page_ is empty.
81 DCHECK(!context_);
82 DCHECK(all_pages_.empty());
83
84 // Creates an 1 by 1 Cairo surface for entire PDF/PS file.
85 // The size for each page will be overwritten later in StartPage().
86 switch (format_) {
87 case PDF: {
88 surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
89 &all_pages_, 1, 1);
90 }
91 break;
92
93 case PS: {
94 surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
95 &all_pages_, 1, 1);
96 }
97 break;
98
99 default:
100 NOTREACHED();
101 return false;
102 }
103
104 // Cairo always returns a valid pointer.
105 // Hence, we have to check if it points to a "nil" object.
106 if (!IsSurfaceValid(surface_)) {
107 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!";
108 CleanUpSurface(&surface_);
109 return false;
110 }
111
112 // Creates a context.
113 context_ = cairo_create(surface_);
114 if (!IsContextValid(context_)) {
115 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!";
116 CleanUpContext(&context_);
117 CleanUpSurface(&surface_);
118 return false;
119 }
120
121 return true;
122 }
123
124 bool PdfPsMetafile::Init(const void* src_buffer, size_t src_buffer_size) {
125 // We need to check at least these two members to ensure Init() has not been
126 // called before. Passing these two checks also implies that surface_,
127 // page_surface_, and page_context_ are NULL, and current_page_ is empty.
128 DCHECK(!context_);
129 DCHECK(all_pages_.empty());
130
131 if (src_buffer == NULL || src_buffer_size == 0) {
132 return false;
133 }
134
135 all_pages_ = std::string(reinterpret_cast<const char*>(src_buffer),
136 src_buffer_size);
137
138 return true;
139 }
140
141 cairo_t* PdfPsMetafile::StartPage(double width_in_points,
142 double height_in_points) {
143 DCHECK(IsSurfaceValid(surface_));
144 DCHECK(IsContextValid(context_));
145 // Passing this check implies page_surface_ is NULL, and current_page_ is
146 // empty.
147 DCHECK(!page_context_);
148 DCHECK_GT(width_in_points, 0.);
149 DCHECK_GT(height_in_points, 0.);
150
151 // Creates a target surface for the new page.
152 // Cairo 1.6.0 does NOT allow the first argument be NULL,
153 // but some newer versions do support NULL pointer.
154 switch (format_) {
155 case PDF: {
156 page_surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
157 &current_page_,
158 width_in_points,
159 height_in_points);
160 }
161 break;
162
163 case PS: {
164 page_surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
165 &current_page_,
166 width_in_points,
167 height_in_points);
168 }
169 break;
170
171 default:
172 NOTREACHED();
173 CleanUpAll();
174 return NULL;
175 }
176
177 // Cairo always returns a valid pointer.
178 // Hence, we have to check if it points to a "nil" object.
179 if (!IsSurfaceValid(page_surface_)) {
180 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!";
181 CleanUpAll();
182 return NULL;
183 }
184
185 // Creates a context.
186 page_context_ = cairo_create(page_surface_);
187 if (!IsContextValid(page_context_)) {
188 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!";
189 CleanUpAll();
190 return NULL;
191 }
192
193 return page_context_;
194 }
195
196 bool PdfPsMetafile::FinishPage(float shrink) {
197 DCHECK(IsSurfaceValid(surface_));
198 DCHECK(IsContextValid(context_));
199 DCHECK(IsSurfaceValid(page_surface_));
200 DCHECK(IsContextValid(page_context_));
201 DCHECK_GT(shrink, 0);
202
203 // Flushes all rendering for current page.
204 cairo_surface_flush(page_surface_);
205
206 // TODO(myhuang): Use real page settings.
207 // We hard-coded page settings here for testing purpose.
208 // The paper size is US Letter (8.5 in. by 11 in.).
209 // The default margins are:
210 // Left = 0.25 in.
211 // Right = 0.25 in.
212 // Top = 0.25 in.
213 // Bottom = 0.56 in.
214 const double kDPI = 72.0; // Dots (points) per inch.
215 const double kWidthInInch = 8.5;
216 const double kHeightInInch = 11.0;
217 const double kWidthInPoint = kWidthInInch * kDPI;
218 const double kHeightInPoint = kHeightInInch * kDPI;
219 switch (format_) {
220 case PDF: {
221 cairo_pdf_surface_set_size(surface_, kWidthInPoint, kHeightInPoint);
222 }
223 break;
224
225 case PS: {
226 cairo_ps_surface_set_size(surface_, kWidthInPoint, kHeightInPoint);
227 }
228 break;
229
230 default:
231 NOTREACHED();
232 CleanUpAll();
233 return false;
234 }
235
236 // Checks if our surface is still valid after resizing.
237 if (!IsSurfaceValid(surface_)) {
238 DLOG(ERROR) << "Cannot resize Cairo surface for PdfPsMetafile!";
239 CleanUpAll();
240 return false;
241 }
242
243 // Saves context's states.
244 cairo_save(context_);
245 // Copies current page onto the surface of final result.
246 // Margins are done by coordinates transformation.
247 // Please NOTE that we have to call cairo_scale() before we call
248 // cairo_set_source_surface().
249 const double scale_factor = 1. / shrink;
250 cairo_scale(context_, scale_factor, scale_factor);
251 const double kLeftMarginInInch = 0.25;
252 const double kTopMarginInInch = 0.25;
253 const double kLeftMarginInPoint = kLeftMarginInInch * kDPI;
254 const double kTopMarginInPoint = kTopMarginInInch * kDPI;
255 const double kScaledLeftMarginInPoint = kLeftMarginInPoint * shrink;
256 const double kScaledTopMarginInPoint = kTopMarginInPoint * shrink;
257 cairo_set_source_surface(context_,
258 page_surface_,
259 kScaledLeftMarginInPoint,
260 kScaledTopMarginInPoint);
261 // In Cairo 1.6.0, if we use the following API, either the renderer will
262 // crash, or we will get an empty page. This might be a bug in Cairo.
263 // cairo_set_operator(context_, CAIRO_OPERATOR_SOURCE);
264 const double kRightMarginInInch = 0.25;
265 const double kBottomMarginInInch = 0.56;
266 const double kPrintableWidthInInch =
267 kWidthInInch - kLeftMarginInInch - kRightMarginInInch;
268 const double kPrintableHeightInInch =
269 kHeightInInch - kTopMarginInInch - kBottomMarginInInch;
270 const double kScaledPrintableWidthInPoint =
271 kPrintableWidthInInch * kDPI * shrink;
272 const double kScaledPrintableHeightInPoint =
273 kPrintableHeightInInch * kDPI * shrink;
274 cairo_rectangle(context_,
275 kScaledLeftMarginInPoint,
276 kScaledTopMarginInPoint,
277 kScaledPrintableWidthInPoint,
278 kScaledPrintableHeightInPoint);
279 cairo_fill(context_);
280
281 // Finishes the duplication of current page.
282 cairo_show_page(context_);
283 cairo_surface_flush(surface_);
284
285 // Destroys resources for current page.
286 CleanUpContext(&page_context_);
287 CleanUpSurface(&page_surface_);
288 current_page_.clear();
289
290 // Restores context's states.
291 cairo_restore(context_);
292
293 return true;
294 }
295
296 void PdfPsMetafile::Close() {
297 DCHECK(IsSurfaceValid(surface_));
298 DCHECK(IsContextValid(context_));
299 // Passing this check implies page_surface_ is NULL, and current_page_ is
300 // empty.
301 DCHECK(!page_context_);
302
303 cairo_surface_finish(surface_);
304 DCHECK(!all_pages_.empty()); // Make sure we did get something.
305
306 CleanUpContext(&context_);
307 CleanUpSurface(&surface_);
308 }
309
310 unsigned int PdfPsMetafile::GetDataSize() const {
311 // We need to check at least these two members to ensure that either Init()
312 // has been called to initialize |all_pages_|, or metafile has been closed.
313 // Passing these two checks also implies that surface_, page_surface_, and
314 // page_context_ are NULL, and current_page_ is empty.
315 DCHECK(!context_);
316 DCHECK(!all_pages_.empty());
317
318 return all_pages_.size();
319 }
320
321 bool PdfPsMetafile::GetData(void* dst_buffer, size_t dst_buffer_size) const {
322 DCHECK(dst_buffer);
323 DCHECK_GT(dst_buffer_size, 0u);
324 // We need to check at least these two members to ensure that either Init()
325 // has been called to initialize |all_pages_|, or metafile has been closed.
326 // Passing these two checks also implies that surface_, page_surface_, and
327 // page_context_ are NULL, and current_page_ is empty.
328 DCHECK(!context_);
329 DCHECK(!all_pages_.empty());
330
331 size_t data_size = GetDataSize();
332 if (dst_buffer_size > data_size) {
333 return false;
334 }
335
336 memcpy(dst_buffer, all_pages_.data(), dst_buffer_size);
337
338 return true;
339 }
340
341 bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const {
342 // We need to check at least these two members to ensure that either Init()
343 // has been called to initialize |all_pages_|, or metafile has been closed.
344 // Passing these two checks also implies that surface_, page_surface_, and
345 // page_context_ are NULL, and current_page_ is empty.
346 DCHECK(!context_);
347 DCHECK(!all_pages_.empty());
348
349 if (fd.fd < 0) {
350 DLOG(ERROR) << "Invalid file descriptor!";
351 return false;
352 }
353
354 bool success = true;
355 if (file_util::WriteFileDescriptor(fd.fd, all_pages_.data(),
356 GetDataSize()) < 0) {
357 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
358 success = false;
359 }
360
361 if (fd.auto_close)
362 HANDLE_EINTR(close(fd.fd));
363 return success;
364 }
365
366 void PdfPsMetafile::CleanUpAll() {
367 CleanUpContext(&context_);
368 CleanUpSurface(&surface_);
369 CleanUpContext(&page_context_);
370 CleanUpSurface(&page_surface_);
371 current_page_.clear();
372 all_pages_.clear();
373 skia::VectorPlatformDevice::ClearFontCache();
374 }
375
376 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698