| OLD | NEW |
| 1 // Copyright (c) 2009 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> | 11 #include <cairo-ps.h> |
| 12 | 12 |
| 13 #include "base/eintr_wrapper.h" | 13 #include "base/eintr_wrapper.h" |
| 14 #include "base/file_descriptor_posix.h" | 14 #include "base/file_descriptor_posix.h" |
| 15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "skia/ext/vector_platform_device_linux.h" | 17 #include "skia/ext/vector_platform_device_linux.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // The hardcoded margins, in points. These values are based on 72 dpi, |
| 22 // with approximately 0.25 margins on top, left, and right, and 0.56 on bottom. |
| 23 const double kTopMargin = 0.25 * 72.0; |
| 24 const double kBottomMargin = 0.56 * 72.0; |
| 25 const double kLeftMargin = 0.25 * 72.0; |
| 26 const double kRightMargin = 0.25 * 72.0; |
| 27 |
| 21 // Tests if |surface| is valid. | 28 // Tests if |surface| is valid. |
| 22 bool IsSurfaceValid(cairo_surface_t* surface) { | 29 bool IsSurfaceValid(cairo_surface_t* surface) { |
| 23 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; | 30 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; |
| 24 } | 31 } |
| 25 | 32 |
| 26 // Tests if |context| is valid. | 33 // Tests if |context| is valid. |
| 27 bool IsContextValid(cairo_t* context) { | 34 bool IsContextValid(cairo_t* context) { |
| 28 return cairo_status(context) == CAIRO_STATUS_SUCCESS; | 35 return cairo_status(context) == CAIRO_STATUS_SUCCESS; |
| 29 } | 36 } |
| 30 | 37 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 58 | 65 |
| 59 return CAIRO_STATUS_SUCCESS; | 66 return CAIRO_STATUS_SUCCESS; |
| 60 } | 67 } |
| 61 | 68 |
| 62 } // namespace | 69 } // namespace |
| 63 | 70 |
| 64 namespace printing { | 71 namespace printing { |
| 65 | 72 |
| 66 PdfPsMetafile::PdfPsMetafile(const FileFormat& format) | 73 PdfPsMetafile::PdfPsMetafile(const FileFormat& format) |
| 67 : format_(format), | 74 : format_(format), |
| 68 surface_(NULL), context_(NULL), | 75 surface_(NULL), context_(NULL) { |
| 69 page_surface_(NULL), page_context_(NULL) { | |
| 70 } | 76 } |
| 71 | 77 |
| 72 PdfPsMetafile::~PdfPsMetafile() { | 78 PdfPsMetafile::~PdfPsMetafile() { |
| 73 // Releases all resources if we forgot to do so. | 79 // Releases all resources if we forgot to do so. |
| 74 CleanUpAll(); | 80 CleanUpAll(); |
| 75 } | 81 } |
| 76 | 82 |
| 77 bool PdfPsMetafile::Init() { | 83 bool PdfPsMetafile::Init() { |
| 78 // We need to check at least these two members to ensure Init() has not been | 84 // 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_, | 85 // called before. |
| 80 // page_surface_, and page_context_ are NULL, and current_page_ is empty. | |
| 81 DCHECK(!context_); | 86 DCHECK(!context_); |
| 82 DCHECK(all_pages_.empty()); | 87 DCHECK(data_.empty()); |
| 83 | 88 |
| 84 // Creates an 1 by 1 Cairo surface for entire PDF/PS file. | 89 // Creates an 1 by 1 Cairo surface for entire PDF/PS file. |
| 85 // The size for each page will be overwritten later in StartPage(). | 90 // The size for each page will be overwritten later in StartPage(). |
| 86 switch (format_) { | 91 switch (format_) { |
| 87 case PDF: { | 92 case PDF: |
| 88 surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream, | 93 surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream, |
| 89 &all_pages_, 1, 1); | 94 &data_, 1, 1); |
| 90 } | 95 break; |
| 91 break; | |
| 92 | 96 |
| 93 case PS: { | 97 case PS: |
| 94 surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream, | 98 surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream, |
| 95 &all_pages_, 1, 1); | 99 &data_, 1, 1); |
| 96 } | 100 break; |
| 97 break; | |
| 98 | 101 |
| 99 default: | 102 default: |
| 100 NOTREACHED(); | 103 NOTREACHED(); |
| 101 return false; | 104 return false; |
| 102 } | 105 } |
| 103 | 106 |
| 107 // Don't let WebKit draw over the margins. |
| 108 cairo_surface_set_device_offset(surface_, |
| 109 static_cast<int>(kLeftMargin), |
| 110 static_cast<int>(kTopMargin)); |
| 111 |
| 104 // Cairo always returns a valid pointer. | 112 // Cairo always returns a valid pointer. |
| 105 // Hence, we have to check if it points to a "nil" object. | 113 // Hence, we have to check if it points to a "nil" object. |
| 106 if (!IsSurfaceValid(surface_)) { | 114 if (!IsSurfaceValid(surface_)) { |
| 107 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!"; | 115 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!"; |
| 108 CleanUpSurface(&surface_); | 116 CleanUpSurface(&surface_); |
| 109 return false; | 117 return false; |
| 110 } | 118 } |
| 111 | 119 |
| 112 // Creates a context. | 120 // Creates a context. |
| 113 context_ = cairo_create(surface_); | 121 context_ = cairo_create(surface_); |
| 114 if (!IsContextValid(context_)) { | 122 if (!IsContextValid(context_)) { |
| 115 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!"; | 123 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!"; |
| 116 CleanUpContext(&context_); | 124 CleanUpContext(&context_); |
| 117 CleanUpSurface(&surface_); | 125 CleanUpSurface(&surface_); |
| 118 return false; | 126 return false; |
| 119 } | 127 } |
| 120 | 128 |
| 121 return true; | 129 return true; |
| 122 } | 130 } |
| 123 | 131 |
| 124 bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { | 132 bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { |
| 125 // We need to check at least these two members to ensure Init() has not been | 133 // 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_, | 134 // called before |
| 127 // page_surface_, and page_context_ are NULL, and current_page_ is empty. | |
| 128 DCHECK(!context_); | 135 DCHECK(!context_); |
| 129 DCHECK(all_pages_.empty()); | 136 DCHECK(data_.empty()); |
| 130 | 137 |
| 131 if (src_buffer == NULL || src_buffer_size == 0) { | 138 if (src_buffer == NULL || src_buffer_size == 0) |
| 132 return false; | 139 return false; |
| 133 } | |
| 134 | 140 |
| 135 all_pages_ = std::string(reinterpret_cast<const char*>(src_buffer), | 141 data_ = std::string(reinterpret_cast<const char*>(src_buffer), |
| 136 src_buffer_size); | 142 src_buffer_size); |
| 137 | 143 |
| 138 return true; | 144 return true; |
| 139 } | 145 } |
| 140 | 146 |
| 141 cairo_t* PdfPsMetafile::StartPage(double width_in_points, | 147 cairo_t* PdfPsMetafile::StartPage(double width_in_points, |
| 142 double height_in_points) { | 148 double height_in_points) { |
| 143 DCHECK(IsSurfaceValid(surface_)); | 149 DCHECK(IsSurfaceValid(surface_)); |
| 144 DCHECK(IsContextValid(context_)); | 150 DCHECK(IsContextValid(context_)); |
| 145 // Passing this check implies page_surface_ is NULL, and current_page_ is | 151 // Passing this check implies page_surface_ is NULL, and current_page_ is |
| 146 // empty. | 152 // empty. |
| 147 DCHECK(!page_context_); | |
| 148 DCHECK_GT(width_in_points, 0.); | 153 DCHECK_GT(width_in_points, 0.); |
| 149 DCHECK_GT(height_in_points, 0.); | 154 DCHECK_GT(height_in_points, 0.); |
| 150 | 155 |
| 151 // Creates a target surface for the new page. | 156 // We build in extra room for the margins. The Cairo PDF backend will scale |
| 152 // Cairo 1.6.0 does NOT allow the first argument be NULL, | 157 // the output to fit a page. |
| 153 // but some newer versions do support NULL pointer. | 158 double width = width_in_points + kLeftMargin + kRightMargin; |
| 159 double height = height_in_points + kTopMargin + kBottomMargin; |
| 160 |
| 154 switch (format_) { | 161 switch (format_) { |
| 155 case PDF: { | 162 case PDF: |
| 156 page_surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream, | 163 cairo_pdf_surface_set_size(surface_, width, height); |
| 157 ¤t_page_, | 164 break; |
| 158 width_in_points, | |
| 159 height_in_points); | |
| 160 } | |
| 161 break; | |
| 162 | 165 |
| 163 case PS: { | 166 case PS: |
| 164 page_surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream, | 167 cairo_ps_surface_set_size(surface_, width, height); |
| 165 ¤t_page_, | 168 break; |
| 166 width_in_points, | |
| 167 height_in_points); | |
| 168 } | |
| 169 break; | |
| 170 | 169 |
| 171 default: | 170 default: |
| 172 NOTREACHED(); | 171 NOTREACHED(); |
| 173 CleanUpAll(); | 172 CleanUpAll(); |
| 174 return NULL; | 173 return NULL; |
| 175 } | 174 } |
| 176 | 175 |
| 177 // Cairo always returns a valid pointer. | 176 return context_; |
| 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 } | 177 } |
| 195 | 178 |
| 196 bool PdfPsMetafile::FinishPage(float shrink) { | 179 bool PdfPsMetafile::FinishPage() { |
| 197 DCHECK(IsSurfaceValid(surface_)); | 180 DCHECK(IsSurfaceValid(surface_)); |
| 198 DCHECK(IsContextValid(context_)); | 181 DCHECK(IsContextValid(context_)); |
| 199 DCHECK(IsSurfaceValid(page_surface_)); | |
| 200 DCHECK(IsContextValid(page_context_)); | |
| 201 DCHECK_GT(shrink, 0); | |
| 202 | 182 |
| 203 // Flushes all rendering for current page. | 183 // Flushes all rendering for current page. |
| 204 cairo_surface_flush(page_surface_); | 184 cairo_surface_flush(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_); | 185 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; | 186 return true; |
| 294 } | 187 } |
| 295 | 188 |
| 296 void PdfPsMetafile::Close() { | 189 void PdfPsMetafile::Close() { |
| 297 DCHECK(IsSurfaceValid(surface_)); | 190 DCHECK(IsSurfaceValid(surface_)); |
| 298 DCHECK(IsContextValid(context_)); | 191 DCHECK(IsContextValid(context_)); |
| 299 // Passing this check implies page_surface_ is NULL, and current_page_ is | |
| 300 // empty. | |
| 301 DCHECK(!page_context_); | |
| 302 | 192 |
| 303 cairo_surface_finish(surface_); | 193 cairo_surface_finish(surface_); |
| 304 DCHECK(!all_pages_.empty()); // Make sure we did get something. | 194 DCHECK(!data_.empty()); // Make sure we did get something. |
| 305 | 195 |
| 306 CleanUpContext(&context_); | 196 CleanUpContext(&context_); |
| 307 CleanUpSurface(&surface_); | 197 CleanUpSurface(&surface_); |
| 308 } | 198 } |
| 309 | 199 |
| 310 uint32 PdfPsMetafile::GetDataSize() const { | 200 uint32 PdfPsMetafile::GetDataSize() const { |
| 311 // We need to check at least these two members to ensure that either Init() | 201 // 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. | 202 // has been called to initialize |data_|, 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_); | 203 DCHECK(!context_); |
| 316 DCHECK(!all_pages_.empty()); | 204 DCHECK(!data_.empty()); |
| 317 | 205 |
| 318 return all_pages_.size(); | 206 return data_.size(); |
| 319 } | 207 } |
| 320 | 208 |
| 321 bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const { | 209 bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const { |
| 322 DCHECK(dst_buffer); | 210 DCHECK(dst_buffer); |
| 323 DCHECK_GT(dst_buffer_size, 0u); | 211 DCHECK_GT(dst_buffer_size, 0u); |
| 324 // We need to check at least these two members to ensure that either Init() | 212 memcpy(dst_buffer, data_.data(), dst_buffer_size); |
| 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 uint32 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 | 213 |
| 338 return true; | 214 return true; |
| 339 } | 215 } |
| 340 | 216 |
| 341 bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const { | 217 bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const { |
| 342 // We need to check at least these two members to ensure that either Init() | 218 // 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. | 219 // has been called to initialize |data_|, 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_); | 220 DCHECK(!context_); |
| 347 DCHECK(!all_pages_.empty()); | 221 DCHECK(!data_.empty()); |
| 348 | 222 |
| 349 if (fd.fd < 0) { | 223 if (fd.fd < 0) { |
| 350 DLOG(ERROR) << "Invalid file descriptor!"; | 224 DLOG(ERROR) << "Invalid file descriptor!"; |
| 351 return false; | 225 return false; |
| 352 } | 226 } |
| 353 | 227 |
| 354 bool success = true; | 228 bool success = true; |
| 355 if (file_util::WriteFileDescriptor(fd.fd, all_pages_.data(), | 229 if (file_util::WriteFileDescriptor(fd.fd, data_.data(), |
| 356 GetDataSize()) < 0) { | 230 GetDataSize()) < 0) { |
| 357 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; | 231 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; |
| 358 success = false; | 232 success = false; |
| 359 } | 233 } |
| 360 | 234 |
| 361 if (fd.auto_close) { | 235 if (fd.auto_close) { |
| 362 if (HANDLE_EINTR(close(fd.fd)) < 0) { | 236 if (HANDLE_EINTR(close(fd.fd)) < 0) { |
| 363 DPLOG(WARNING) << "close"; | 237 DPLOG(WARNING) << "close"; |
| 364 success = false; | 238 success = false; |
| 365 } | 239 } |
| 366 } | 240 } |
| 367 | 241 |
| 368 return success; | 242 return success; |
| 369 } | 243 } |
| 370 | 244 |
| 371 void PdfPsMetafile::CleanUpAll() { | 245 void PdfPsMetafile::CleanUpAll() { |
| 372 CleanUpContext(&context_); | 246 CleanUpContext(&context_); |
| 373 CleanUpSurface(&surface_); | 247 CleanUpSurface(&surface_); |
| 374 CleanUpContext(&page_context_); | 248 data_.clear(); |
| 375 CleanUpSurface(&page_surface_); | |
| 376 current_page_.clear(); | |
| 377 all_pages_.clear(); | |
| 378 skia::VectorPlatformDevice::ClearFontCache(); | 249 skia::VectorPlatformDevice::ClearFontCache(); |
| 379 } | 250 } |
| 380 | 251 |
| 381 } // namespace printing | 252 } // namespace printing |
| OLD | NEW |