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 "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/process_util.h" | 8 #include "base/process_util.h" |
9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
10 #include "chrome/common/render_messages_params.h" | 10 #include "chrome/common/render_messages_params.h" |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 preview_params.metafile_data_handle, | 190 preview_params.metafile_data_handle, |
191 &preview_params.metafile_data_handle))) { | 191 &preview_params.metafile_data_handle))) { |
192 NOTREACHED() << "Send message failed."; | 192 NOTREACHED() << "Send message failed."; |
193 } | 193 } |
194 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 194 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
195 } | 195 } |
196 | 196 |
197 void PrintWebViewHelper::RenderPage( | 197 void PrintWebViewHelper::RenderPage( |
198 const ViewMsg_Print_Params& params, float* scale_factor, int page_number, | 198 const ViewMsg_Print_Params& params, float* scale_factor, int page_number, |
199 WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) { | 199 WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) { |
200 HDC hdc = (*metafile)->context(); | 200 DCHECK(metafile->get()->context()); |
201 DCHECK(hdc); | |
202 | 201 |
203 double content_width_in_points; | 202 double content_width_in_points; |
204 double content_height_in_points; | 203 double content_height_in_points; |
205 double margin_top_in_points; | 204 double margin_top_in_points; |
206 double margin_left_in_points; | 205 double margin_left_in_points; |
207 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 206 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
208 &content_width_in_points, | 207 &content_width_in_points, |
209 &content_height_in_points, | 208 &content_height_in_points, |
210 &margin_top_in_points, NULL, NULL, | 209 &margin_top_in_points, NULL, NULL, |
211 &margin_left_in_points); | 210 &margin_left_in_points); |
212 | 211 |
213 // Since WebKit extends the page width depending on the magical scale factor | 212 // Since WebKit extends the page width depending on the magical scale factor |
214 // we make sure the canvas covers the worst case scenario (x2.0 currently). | 213 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
215 // PrintContext will then set the correct clipping region. | 214 // PrintContext will then set the correct clipping region. |
216 int width = static_cast<int>(content_width_in_points * params.max_shrink); | 215 int width = static_cast<int>(content_width_in_points * params.max_shrink); |
217 int height = static_cast<int>(content_height_in_points * params.max_shrink); | 216 int height = static_cast<int>(content_height_in_points * params.max_shrink); |
218 | 217 |
219 bool result = (*metafile)->StartPage( | 218 gfx::Size page_size(width, height); |
220 gfx::Size(width, height), | 219 gfx::Point(static_cast<int>(margin_left_in_points), |
221 gfx::Point(static_cast<int>(margin_top_in_points), | 220 static_cast<int>(margin_top_in_points)); |
222 static_cast<int>(margin_left_in_points)), | 221 skia::PlatformDevice* device = (*metafile)->StartPageForVectorCanvas( |
223 *scale_factor); | 222 page_size, content_origin, 1.0f); |
224 DCHECK(result); | 223 DCHECK(device); |
| 224 skia::VectorCanvas canvas(device); |
225 | 225 |
226 #if 0 | |
227 // TODO(maruel): This code is kept for testing until the 100% GDI drawing | |
228 // code is stable. maruels use this code's output as a reference when the | |
229 // GDI drawing code fails. | |
230 | |
231 // Mix of Skia and GDI based. | |
232 skia::PlatformCanvas canvas(width, height, true); | |
233 canvas.drawARGB(255, 255, 255, 255, SkXfermode::kSrc_Mode); | |
234 float webkit_scale_factor = frame->printPage(page_number, &canvas); | 226 float webkit_scale_factor = frame->printPage(page_number, &canvas); |
235 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { | 227 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { |
236 NOTREACHED() << "Printing page " << page_number << " failed."; | 228 NOTREACHED() << "Printing page " << page_number << " failed."; |
237 } else { | |
238 // Update the dpi adjustment with the "page |scale_factor|" calculated in | |
239 // webkit. | |
240 *scale_factor /= webkit_scale_factor; | |
241 } | |
242 | |
243 // Create a BMP v4 header that we can serialize. | |
244 BITMAPV4HEADER bitmap_header; | |
245 gfx::CreateBitmapV4Header(width, height, &bitmap_header); | |
246 const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true); | |
247 SkAutoLockPixels src_lock(src_bmp); | |
248 int retval = StretchDIBits(hdc, | |
249 0, | |
250 0, | |
251 width, height, | |
252 0, 0, | |
253 width, height, | |
254 src_bmp.getPixels(), | |
255 reinterpret_cast<BITMAPINFO*>(&bitmap_header), | |
256 DIB_RGB_COLORS, | |
257 SRCCOPY); | |
258 DCHECK(retval != GDI_ERROR); | |
259 #else | |
260 // 100% GDI based. | |
261 skia::VectorCanvas canvas(hdc, width, height); | |
262 float webkit_scale_factor = frame->printPage(page_number, &canvas); | |
263 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { | |
264 NOTREACHED() << "Printing page " << page_number << " failed."; | |
265 } else { | 229 } else { |
266 // Update the dpi adjustment with the "page |scale_factor|" calculated in | 230 // Update the dpi adjustment with the "page |scale_factor|" calculated in |
267 // webkit. | 231 // webkit. |
268 *scale_factor /= webkit_scale_factor; | 232 *scale_factor /= webkit_scale_factor; |
269 } | 233 } |
270 #endif | |
271 | 234 |
272 result = (*metafile)->FinishPage(); | 235 bool result = (*metafile)->FinishPage(); |
273 DCHECK(result); | 236 DCHECK(result); |
274 | 237 |
275 skia::VectorPlatformDevice* platform_device = | 238 skia::VectorPlatformDevice* platform_device = |
276 static_cast<skia::VectorPlatformDevice*>(canvas.getDevice()); | 239 static_cast<skia::VectorPlatformDevice*>(device); |
277 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { | 240 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { |
278 // Close the device context to retrieve the compiled metafile. | 241 // Close the device context to retrieve the compiled metafile. |
279 if (!(*metafile)->Close()) | 242 if (!(*metafile)->Close()) |
280 NOTREACHED(); | 243 NOTREACHED(); |
281 | 244 |
282 scoped_ptr<printing::NativeMetafile> metafile2( | 245 scoped_ptr<printing::NativeMetafile> metafile2( |
283 printing::NativeMetafileFactory::CreateMetafile()); | 246 printing::NativeMetafileFactory::CreateMetafile()); |
284 // Page used alpha blend, but printer doesn't support it. Rewrite the | 247 // Page used alpha blend, but printer doesn't support it. Rewrite the |
285 // metafile and flatten out the transparency. | 248 // metafile and flatten out the transparency. |
286 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); | 249 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // Copy the bits into shared memory. | 306 // Copy the bits into shared memory. |
344 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | 307 if (!metafile->GetData(shared_buf.memory(), buf_size)) { |
345 NOTREACHED() << "GetData() failed"; | 308 NOTREACHED() << "GetData() failed"; |
346 shared_buf.Unmap(); | 309 shared_buf.Unmap(); |
347 return false; | 310 return false; |
348 } | 311 } |
349 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 312 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
350 shared_buf.Unmap(); | 313 shared_buf.Unmap(); |
351 return true; | 314 return true; |
352 } | 315 } |
OLD | NEW |