| 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> | 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 "printing/units.h" | 17 #include "printing/units.h" |
| 18 #include "skia/ext/vector_platform_device_linux.h" | 18 #include "skia/ext/vector_platform_device_linux.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const cairo_user_data_key_t kPdfMetafileKey = {0}; |
| 23 |
| 22 // Tests if |surface| is valid. | 24 // Tests if |surface| is valid. |
| 23 bool IsSurfaceValid(cairo_surface_t* surface) { | 25 bool IsSurfaceValid(cairo_surface_t* surface) { |
| 24 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; | 26 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; |
| 25 } | 27 } |
| 26 | 28 |
| 27 // Tests if |context| is valid. | 29 // Tests if |context| is valid. |
| 28 bool IsContextValid(cairo_t* context) { | 30 bool IsContextValid(cairo_t* context) { |
| 29 return cairo_status(context) == CAIRO_STATUS_SUCCESS; | 31 return cairo_status(context) == CAIRO_STATUS_SUCCESS; |
| 30 } | 32 } |
| 31 | 33 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 53 DCHECK(dst_buffer); | 55 DCHECK(dst_buffer); |
| 54 DCHECK(src_data); | 56 DCHECK(src_data); |
| 55 DCHECK_GT(src_data_length, 0u); | 57 DCHECK_GT(src_data_length, 0u); |
| 56 | 58 |
| 57 std::string* buffer = reinterpret_cast<std::string*>(dst_buffer); | 59 std::string* buffer = reinterpret_cast<std::string*>(dst_buffer); |
| 58 buffer->append(reinterpret_cast<const char*>(src_data), src_data_length); | 60 buffer->append(reinterpret_cast<const char*>(src_data), src_data_length); |
| 59 | 61 |
| 60 return CAIRO_STATUS_SUCCESS; | 62 return CAIRO_STATUS_SUCCESS; |
| 61 } | 63 } |
| 62 | 64 |
| 65 void DestroyContextData(void* data) { |
| 66 // Nothing to be done here. |
| 67 } |
| 68 |
| 63 } // namespace | 69 } // namespace |
| 64 | 70 |
| 65 namespace printing { | 71 namespace printing { |
| 66 | 72 |
| 67 PdfPsMetafile::PdfPsMetafile(const FileFormat& format) | 73 PdfPsMetafile::PdfPsMetafile(const FileFormat& format) |
| 68 : format_(format), | 74 : format_(format), |
| 69 surface_(NULL), context_(NULL) { | 75 surface_(NULL), context_(NULL) { |
| 70 } | 76 } |
| 71 | 77 |
| 72 PdfPsMetafile::~PdfPsMetafile() { | 78 PdfPsMetafile::~PdfPsMetafile() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 114 |
| 109 // Creates a context. | 115 // Creates a context. |
| 110 context_ = cairo_create(surface_); | 116 context_ = cairo_create(surface_); |
| 111 if (!IsContextValid(context_)) { | 117 if (!IsContextValid(context_)) { |
| 112 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!"; | 118 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!"; |
| 113 CleanUpContext(&context_); | 119 CleanUpContext(&context_); |
| 114 CleanUpSurface(&surface_); | 120 CleanUpSurface(&surface_); |
| 115 return false; | 121 return false; |
| 116 } | 122 } |
| 117 | 123 |
| 124 cairo_set_user_data(context_, &kPdfMetafileKey, this, DestroyContextData); |
| 125 |
| 118 return true; | 126 return true; |
| 119 } | 127 } |
| 120 | 128 |
| 121 bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { | 129 bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { |
| 122 // We need to check at least these two members to ensure Init() has not been | 130 // We need to check at least these two members to ensure Init() has not been |
| 123 // called before | 131 // called before |
| 124 DCHECK(!context_); | 132 DCHECK(!context_); |
| 125 DCHECK(data_.empty()); | 133 DCHECK(data_.empty()); |
| 126 | 134 |
| 127 if (src_buffer == NULL || src_buffer_size == 0) | 135 if (src_buffer == NULL || src_buffer_size == 0) |
| 128 return false; | 136 return false; |
| 129 | 137 |
| 130 data_ = std::string(reinterpret_cast<const char*>(src_buffer), | 138 data_ = std::string(reinterpret_cast<const char*>(src_buffer), |
| 131 src_buffer_size); | 139 src_buffer_size); |
| 132 | 140 |
| 133 return true; | 141 return true; |
| 134 } | 142 } |
| 135 | 143 |
| 144 bool PdfPsMetafile::SetRawData(const void* src_buffer, |
| 145 uint32 src_buffer_size) { |
| 146 if (!context_) { |
| 147 // If Init has not already been called, just call Init() |
| 148 return Init(src_buffer, src_buffer_size); |
| 149 } |
| 150 // If a context has already been created, remember this data in |
| 151 // raw_override_data_ |
| 152 if (src_buffer == NULL || src_buffer_size == 0) |
| 153 return false; |
| 154 |
| 155 raw_override_data_ = std::string(reinterpret_cast<const char*>(src_buffer), |
| 156 src_buffer_size); |
| 157 |
| 158 return true; |
| 159 } |
| 160 |
| 136 cairo_t* PdfPsMetafile::StartPage(double width_in_points, | 161 cairo_t* PdfPsMetafile::StartPage(double width_in_points, |
| 137 double height_in_points, | 162 double height_in_points, |
| 138 double margin_top_in_points, | 163 double margin_top_in_points, |
| 139 double margin_right_in_points, | 164 double margin_right_in_points, |
| 140 double margin_bottom_in_points, | 165 double margin_bottom_in_points, |
| 141 double margin_left_in_points) { | 166 double margin_left_in_points) { |
| 142 DCHECK(IsSurfaceValid(surface_)); | 167 DCHECK(IsSurfaceValid(surface_)); |
| 143 DCHECK(IsContextValid(context_)); | 168 DCHECK(IsContextValid(context_)); |
| 144 // Passing this check implies page_surface_ is NULL, and current_page_ is | 169 // Passing this check implies page_surface_ is NULL, and current_page_ is |
| 145 // empty. | 170 // empty. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 cairo_surface_flush(surface_); | 209 cairo_surface_flush(surface_); |
| 185 cairo_show_page(context_); | 210 cairo_show_page(context_); |
| 186 return true; | 211 return true; |
| 187 } | 212 } |
| 188 | 213 |
| 189 void PdfPsMetafile::Close() { | 214 void PdfPsMetafile::Close() { |
| 190 DCHECK(IsSurfaceValid(surface_)); | 215 DCHECK(IsSurfaceValid(surface_)); |
| 191 DCHECK(IsContextValid(context_)); | 216 DCHECK(IsContextValid(context_)); |
| 192 | 217 |
| 193 cairo_surface_finish(surface_); | 218 cairo_surface_finish(surface_); |
| 219 |
| 220 // If we have raw PDF/PS data set use that instead of what was drawn. |
| 221 if (!raw_override_data_.empty()) { |
| 222 data_ = raw_override_data_; |
| 223 raw_override_data_.clear(); |
| 224 } |
| 194 DCHECK(!data_.empty()); // Make sure we did get something. | 225 DCHECK(!data_.empty()); // Make sure we did get something. |
| 195 | 226 |
| 196 CleanUpContext(&context_); | 227 CleanUpContext(&context_); |
| 197 CleanUpSurface(&surface_); | 228 CleanUpSurface(&surface_); |
| 198 } | 229 } |
| 199 | 230 |
| 200 uint32 PdfPsMetafile::GetDataSize() const { | 231 uint32 PdfPsMetafile::GetDataSize() const { |
| 201 // We need to check at least these two members to ensure that either Init() | 232 // We need to check at least these two members to ensure that either Init() |
| 202 // has been called to initialize |data_|, or metafile has been closed. | 233 // has been called to initialize |data_|, or metafile has been closed. |
| 203 DCHECK(!context_); | 234 DCHECK(!context_); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 if (fd.auto_close) { | 266 if (fd.auto_close) { |
| 236 if (HANDLE_EINTR(close(fd.fd)) < 0) { | 267 if (HANDLE_EINTR(close(fd.fd)) < 0) { |
| 237 DPLOG(WARNING) << "close"; | 268 DPLOG(WARNING) << "close"; |
| 238 success = false; | 269 success = false; |
| 239 } | 270 } |
| 240 } | 271 } |
| 241 | 272 |
| 242 return success; | 273 return success; |
| 243 } | 274 } |
| 244 | 275 |
| 276 PdfPsMetafile* PdfPsMetafile::FromCairoContext(cairo_t* context) { |
| 277 return reinterpret_cast<PdfPsMetafile*>( |
| 278 cairo_get_user_data(context, &kPdfMetafileKey)); |
| 279 } |
| 280 |
| 245 void PdfPsMetafile::CleanUpAll() { | 281 void PdfPsMetafile::CleanUpAll() { |
| 246 CleanUpContext(&context_); | 282 CleanUpContext(&context_); |
| 247 CleanUpSurface(&surface_); | 283 CleanUpSurface(&surface_); |
| 248 data_.clear(); | 284 data_.clear(); |
| 249 skia::VectorPlatformDevice::ClearFontCache(); | 285 skia::VectorPlatformDevice::ClearFontCache(); |
| 250 } | 286 } |
| 251 | 287 |
| 252 const double PdfPsMetafile::kTopMarginInInch = 0.25; | 288 const double PdfPsMetafile::kTopMarginInInch = 0.25; |
| 253 const double PdfPsMetafile::kBottomMarginInInch = 0.56; | 289 const double PdfPsMetafile::kBottomMarginInInch = 0.56; |
| 254 const double PdfPsMetafile::kLeftMarginInInch = 0.25; | 290 const double PdfPsMetafile::kLeftMarginInInch = 0.25; |
| 255 const double PdfPsMetafile::kRightMarginInInch = 0.25; | 291 const double PdfPsMetafile::kRightMarginInInch = 0.25; |
| 256 | 292 |
| 257 } // namespace printing | 293 } // namespace printing |
| OLD | NEW |