OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 11 |
12 #include "base/eintr_wrapper.h" | 12 #include "base/eintr_wrapper.h" |
13 #include "base/file_descriptor_posix.h" | 13 #include "base/file_descriptor_posix.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "printing/units.h" | 16 #include "printing/units.h" |
17 #include "skia/ext/vector_platform_device_linux.h" | 17 #include "skia/ext/vector_platform_device_linux.h" |
18 #include "ui/gfx/rect.h" | |
19 #include "ui/gfx/size.h" | |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 const cairo_user_data_key_t kPdfMetafileKey = {0}; | 23 const cairo_user_data_key_t kPdfMetafileKey = {0}; |
22 | 24 |
23 // Tests if |surface| is valid. | 25 // Tests if |surface| is valid. |
24 bool IsSurfaceValid(cairo_surface_t* surface) { | 26 bool IsSurfaceValid(cairo_surface_t* surface) { |
25 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; | 27 return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; |
26 } | 28 } |
27 | 29 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 // Creates a context. | 103 // Creates a context. |
102 context_ = cairo_create(surface_); | 104 context_ = cairo_create(surface_); |
103 if (!IsContextValid(context_)) { | 105 if (!IsContextValid(context_)) { |
104 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!"; | 106 DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!"; |
105 CleanUpContext(&context_); | 107 CleanUpContext(&context_); |
106 CleanUpSurface(&surface_); | 108 CleanUpSurface(&surface_); |
107 return false; | 109 return false; |
108 } | 110 } |
109 | 111 |
110 cairo_set_user_data(context_, &kPdfMetafileKey, this, DestroyContextData); | 112 cairo_set_user_data(context_, &kPdfMetafileKey, this, DestroyContextData); |
111 | |
112 return true; | 113 return true; |
113 } | 114 } |
114 | 115 |
115 bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { | 116 bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { |
116 // We need to check at least these two members to ensure Init() has not been | 117 // We need to check at least these two members to ensure Init() has not been |
117 // called before | 118 // called before |
118 DCHECK(!context_); | 119 DCHECK(!context_); |
119 DCHECK(data_.empty()); | 120 DCHECK(data_.empty()); |
120 | 121 |
121 if (src_buffer == NULL || src_buffer_size == 0) | 122 if (src_buffer == NULL || src_buffer_size == 0) |
122 return false; | 123 return false; |
123 | 124 |
124 data_ = std::string(reinterpret_cast<const char*>(src_buffer), | 125 data_ = std::string(reinterpret_cast<const char*>(src_buffer), |
125 src_buffer_size); | 126 src_buffer_size); |
126 | |
127 return true; | 127 return true; |
128 } | 128 } |
129 | 129 |
130 bool PdfPsMetafile::SetRawData(const void* src_buffer, | 130 bool PdfPsMetafile::SetRawData(const void* src_buffer, |
131 uint32 src_buffer_size) { | 131 uint32 src_buffer_size) { |
132 if (!context_) { | 132 if (!context_) { |
133 // If Init has not already been called, just call Init() | 133 // If Init has not already been called, just call Init() |
134 return Init(src_buffer, src_buffer_size); | 134 return Init(src_buffer, src_buffer_size); |
135 } | 135 } |
136 // If a context has already been created, remember this data in | 136 // If a context has already been created, remember this data in |
137 // raw_override_data_ | 137 // raw_override_data_ |
138 if (src_buffer == NULL || src_buffer_size == 0) | 138 if (src_buffer == NULL || src_buffer_size == 0) |
139 return false; | 139 return false; |
140 | 140 |
141 raw_override_data_ = std::string(reinterpret_cast<const char*>(src_buffer), | 141 raw_override_data_ = std::string(reinterpret_cast<const char*>(src_buffer), |
142 src_buffer_size); | 142 src_buffer_size); |
143 | 143 |
144 return true; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 cairo_t* PdfPsMetafile::StartPage(double width_in_points, | 147 cairo_t* PdfPsMetafile::StartPage(const gfx::Size& page_size, |
148 double height_in_points, | |
149 double margin_top_in_points, | 148 double margin_top_in_points, |
150 double margin_right_in_points, | 149 double margin_right_in_points, |
151 double margin_bottom_in_points, | 150 double margin_bottom_in_points, |
152 double margin_left_in_points) { | 151 double margin_left_in_points) { |
153 DCHECK(IsSurfaceValid(surface_)); | 152 DCHECK(IsSurfaceValid(surface_)); |
154 DCHECK(IsContextValid(context_)); | 153 DCHECK(IsContextValid(context_)); |
155 // Passing this check implies page_surface_ is NULL, and current_page_ is | 154 // Passing this check implies page_surface_ is NULL, and current_page_ is |
156 // empty. | 155 // empty. |
157 DCHECK_GT(width_in_points, 0.); | 156 DCHECK_GT(page_size.width(), 0.); |
158 DCHECK_GT(height_in_points, 0.); | 157 DCHECK_GT(page_size.height(), 0.); |
159 | 158 |
160 // We build in extra room for the margins. The Cairo PDF backend will scale | 159 // We build in extra room for the margins. The Cairo PDF backend will scale |
161 // the output to fit a page. | 160 // the output to fit a page. |
162 double width = | 161 double width = |
163 width_in_points + margin_left_in_points + margin_right_in_points; | 162 page_size.width() + margin_left_in_points + margin_right_in_points; |
vandebo (ex-Chrome)
2011/03/14 20:24:25
The argument obviously isn't page size, it's conte
dpapad
2011/03/14 22:15:12
Done. Also I removed unused args. The remaining ar
| |
164 double height = | 163 double height = |
165 height_in_points + margin_top_in_points + margin_bottom_in_points; | 164 page_size.height() + margin_top_in_points + margin_bottom_in_points; |
166 | 165 |
167 // Don't let WebKit draw over the margins. | 166 // Don't let WebKit draw over the margins. |
168 cairo_surface_set_device_offset(surface_, | 167 cairo_surface_set_device_offset(surface_, |
169 margin_left_in_points, | 168 margin_left_in_points, |
170 margin_top_in_points); | 169 margin_top_in_points); |
171 | 170 |
172 cairo_pdf_surface_set_size(surface_, width, height); | 171 cairo_pdf_surface_set_size(surface_, width, height); |
173 return context_; | 172 return context_; |
174 } | 173 } |
175 | 174 |
176 bool PdfPsMetafile::FinishPage() { | 175 bool PdfPsMetafile::FinishPage() { |
177 DCHECK(IsSurfaceValid(surface_)); | 176 DCHECK(IsSurfaceValid(surface_)); |
178 DCHECK(IsContextValid(context_)); | 177 DCHECK(IsContextValid(context_)); |
179 | 178 |
180 // Flushes all rendering for current page. | 179 // Flushes all rendering for current page. |
181 cairo_surface_flush(surface_); | 180 cairo_surface_flush(surface_); |
182 cairo_show_page(context_); | 181 cairo_show_page(context_); |
183 return true; | 182 return true; |
184 } | 183 } |
185 | 184 |
186 void PdfPsMetafile::Close() { | 185 bool PdfPsMetafile::Close() { |
187 DCHECK(IsSurfaceValid(surface_)); | 186 DCHECK(IsSurfaceValid(surface_)); |
188 DCHECK(IsContextValid(context_)); | 187 DCHECK(IsContextValid(context_)); |
189 | 188 |
190 cairo_surface_finish(surface_); | 189 cairo_surface_finish(surface_); |
191 | 190 |
192 // If we have raw PDF data set use that instead of what was drawn. | 191 // If we have raw PDF data set use that instead of what was drawn. |
193 if (!raw_override_data_.empty()) { | 192 if (!raw_override_data_.empty()) { |
194 data_ = raw_override_data_; | 193 data_ = raw_override_data_; |
195 raw_override_data_.clear(); | 194 raw_override_data_.clear(); |
196 } | 195 } |
197 DCHECK(!data_.empty()); // Make sure we did get something. | 196 DCHECK(!data_.empty()); // Make sure we did get something. |
198 | 197 |
199 CleanUpContext(&context_); | 198 CleanUpContext(&context_); |
200 CleanUpSurface(&surface_); | 199 CleanUpSurface(&surface_); |
200 return true; | |
201 } | 201 } |
202 | 202 |
203 uint32 PdfPsMetafile::GetDataSize() const { | 203 uint32 PdfPsMetafile::GetDataSize() const { |
204 // We need to check at least these two members to ensure that either Init() | 204 // We need to check at least these two members to ensure that either Init() |
205 // has been called to initialize |data_|, or metafile has been closed. | 205 // has been called to initialize |data_|, or metafile has been closed. |
206 DCHECK(!context_); | 206 DCHECK(!context_); |
207 DCHECK(!data_.empty()); | 207 DCHECK(!data_.empty()); |
208 | 208 |
209 return data_.size(); | 209 return data_.size(); |
210 } | 210 } |
211 | 211 |
212 bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const { | 212 bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const { |
213 DCHECK(dst_buffer); | 213 DCHECK(dst_buffer); |
214 DCHECK_GT(dst_buffer_size, 0u); | 214 DCHECK_GT(dst_buffer_size, 0u); |
215 memcpy(dst_buffer, data_.data(), dst_buffer_size); | 215 memcpy(dst_buffer, data_.data(), dst_buffer_size); |
216 | 216 |
217 return true; | 217 return true; |
218 } | 218 } |
219 | 219 |
220 bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const { | 220 bool PdfPsMetafile::SaveTo(const FilePath& file_path) const { |
221 // We need to check at least these two members to ensure that either Init() | 221 // We need to check at least these two members to ensure that either Init() |
222 // has been called to initialize |data_|, or metafile has been closed. | 222 // has been called to initialize |data_|, or metafile has been closed. |
223 DCHECK(!context_); | 223 DCHECK(!context_); |
224 DCHECK(!data_.empty()); | |
225 | |
226 bool success = true; | |
227 if (file_util::WriteFile(file_path, data_.data(), GetDataSize()) < 0) { | |
228 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); | |
229 success = false; | |
230 } | |
231 return success; | |
232 } | |
233 | |
234 gfx::Rect PdfPsMetafile::GetPageBounds(unsigned int page_number) const { | |
235 NOTIMPLEMENTED(); | |
236 return gfx::Rect(); | |
237 } | |
238 | |
239 #if defined(OS_CHROMEOS) | |
240 bool PdfPsMetafile::SaveToFD(const base::FileDescriptor& fd) const { | |
241 // We need to check at least these two members to ensure that either Init() | |
242 // has been called to initialize |data_|, or metafile has been closed. | |
243 DCHECK(!context_); | |
224 DCHECK(!data_.empty()); | 244 DCHECK(!data_.empty()); |
225 | 245 |
226 if (fd.fd < 0) { | 246 if (fd.fd < 0) { |
227 DLOG(ERROR) << "Invalid file descriptor!"; | 247 DLOG(ERROR) << "Invalid file descriptor!"; |
228 return false; | 248 return false; |
229 } | 249 } |
230 | 250 |
231 bool success = true; | 251 bool success = true; |
232 if (file_util::WriteFileDescriptor(fd.fd, data_.data(), | 252 if (file_util::WriteFileDescriptor(fd.fd, data_.data(), |
233 GetDataSize()) < 0) { | 253 GetDataSize()) < 0) { |
234 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; | 254 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; |
235 success = false; | 255 success = false; |
236 } | 256 } |
237 | 257 |
238 if (fd.auto_close) { | 258 if (fd.auto_close) { |
239 if (HANDLE_EINTR(close(fd.fd)) < 0) { | 259 if (HANDLE_EINTR(close(fd.fd)) < 0) { |
240 DPLOG(WARNING) << "close"; | 260 DPLOG(WARNING) << "close"; |
241 success = false; | 261 success = false; |
242 } | 262 } |
243 } | 263 } |
244 | 264 |
245 return success; | 265 return success; |
246 } | 266 } |
267 #endif // if defined(OS_CHROMEOS) | |
247 | 268 |
248 PdfPsMetafile* PdfPsMetafile::FromCairoContext(cairo_t* context) { | 269 PdfPsMetafile* PdfPsMetafile::FromCairoContext(cairo_t* context) { |
249 return reinterpret_cast<PdfPsMetafile*>( | 270 return reinterpret_cast<PdfPsMetafile*>( |
250 cairo_get_user_data(context, &kPdfMetafileKey)); | 271 cairo_get_user_data(context, &kPdfMetafileKey)); |
251 } | 272 } |
252 | 273 |
253 void PdfPsMetafile::CleanUpAll() { | 274 void PdfPsMetafile::CleanUpAll() { |
254 CleanUpContext(&context_); | 275 CleanUpContext(&context_); |
255 CleanUpSurface(&surface_); | 276 CleanUpSurface(&surface_); |
256 data_.clear(); | 277 data_.clear(); |
257 skia::VectorPlatformDevice::ClearFontCache(); | 278 skia::VectorPlatformDevice::ClearFontCache(); |
258 } | 279 } |
259 | 280 |
260 const double PdfPsMetafile::kTopMarginInInch = 0.25; | 281 const double PdfPsMetafile::kTopMarginInInch = 0.25; |
261 const double PdfPsMetafile::kBottomMarginInInch = 0.56; | 282 const double PdfPsMetafile::kBottomMarginInInch = 0.56; |
262 const double PdfPsMetafile::kLeftMarginInInch = 0.25; | 283 const double PdfPsMetafile::kLeftMarginInInch = 0.25; |
263 const double PdfPsMetafile::kRightMarginInInch = 0.25; | 284 const double PdfPsMetafile::kRightMarginInInch = 0.25; |
264 | 285 |
265 } // namespace printing | 286 } // namespace printing |
OLD | NEW |