OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 WebFrame* frame) { | 101 WebFrame* frame) { |
102 // Generate a memory-based metafile. It will use the current screen's DPI. | 102 // Generate a memory-based metafile. It will use the current screen's DPI. |
103 // Each metafile contains a single page. | 103 // Each metafile contains a single page. |
104 scoped_ptr<Metafile> metafile(new printing::NativeMetafile); | 104 scoped_ptr<Metafile> metafile(new printing::NativeMetafile); |
105 metafile->Init(); | 105 metafile->Init(); |
106 DCHECK(metafile->context()); | 106 DCHECK(metafile->context()); |
107 skia::InitializeDC(metafile->context()); | 107 skia::InitializeDC(metafile->context()); |
108 | 108 |
109 int page_number = params.page_number; | 109 int page_number = params.page_number; |
110 | 110 |
111 // Calculate scaling. | 111 // Calculate the dpi adjustment. |
112 double actual_shrink = gfx::CalculatePageScale( | 112 // Browser will render context using desired_dpi, so we need to calculate |
113 metafile->context(), | 113 // adjustment factor to play content on the printer DC later during the |
114 params.params.content_size.width(), | 114 // actual printing. |
115 params.params.content_size.height()); | 115 double actual_shrink = static_cast<float>(params.params.desired_dpi / |
| 116 params.params.dpi); |
| 117 if (print_for_preview_) { |
| 118 // While printing from the preview, we are using PDF to print. |
| 119 // It creates a temp metafile based on screen DC. The standard scale factor |
| 120 // may not fit an entire content of the PDF to the metafile. It will cause |
| 121 // output to be cutoff. |
| 122 // (See http://code.google.com/p/chromium-os/issues/detail?id=16088 and |
| 123 // related Chrome bugs) |
| 124 // In such case we need to calculate the same scale ratio PDF plugin will |
| 125 // calculate during printing. |
| 126 // TODO(gene): Revisit current implementation and address comments below. |
| 127 // (http://code.google.com/p/chromium/issues/detail?id=123408) |
| 128 // Ideally, we should return this parameter from the plugin to avoid code |
| 129 // duplication. However, currently the call stack involves WebKit and at |
| 130 // some point shrink factor was cutoff and always returns 1.0. |
| 131 // webkit::ppapi::PluginInstance::PrintPDFOutput - ratio calculated here |
| 132 // webkit::ppapi::PluginInstance::PrintPageHelper |
| 133 // webkit::ppapi::PluginInstance::PrintPage |
| 134 // webkit::ppapi::WebPluginImpl::printPage |
| 135 // WebKit::WebPluginContainerImpl::printPage |
| 136 // WebKit::ChromePluginPrintContext::spoolPage - always return 1.0 scale |
| 137 // WebKit::WebFrameImpl::printPage |
| 138 // PrintWebViewHelper::RenderPage |
| 139 // PrintWebViewHelper::PrintPageInternal |
| 140 // |
| 141 // Another solution is to build in scaling factor into metafile itself |
| 142 // (for example, GDI comments), and make metafile playback to take care of |
| 143 // scaling automatically. |
| 144 actual_shrink = gfx::CalculatePageScale( |
| 145 metafile->context(), |
| 146 params.params.content_size.width(), |
| 147 params.params.content_size.height()); |
| 148 } |
116 | 149 |
117 gfx::Size page_size_in_dpi; | 150 gfx::Size page_size_in_dpi; |
118 gfx::Rect content_area_in_dpi; | 151 gfx::Rect content_area_in_dpi; |
119 // Render page for printing. | 152 // Render page for printing. |
120 metafile.reset(RenderPage(params.params, page_number, frame, false, | 153 metafile.reset(RenderPage(params.params, page_number, frame, false, |
121 metafile.get(), &actual_shrink, &page_size_in_dpi, | 154 metafile.get(), &actual_shrink, &page_size_in_dpi, |
122 &content_area_in_dpi)); | 155 &content_area_in_dpi)); |
123 | 156 |
124 // Close the device context to retrieve the compiled metafile. | 157 // Close the device context to retrieve the compiled metafile. |
125 if (!metafile->FinishDocument()) | 158 if (!metafile->FinishDocument()) |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 shared_buf.Unmap(); | 375 shared_buf.Unmap(); |
343 return false; | 376 return false; |
344 } | 377 } |
345 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 378 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
346 shared_buf.Unmap(); | 379 shared_buf.Unmap(); |
347 | 380 |
348 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 381 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
349 shared_mem_handle)); | 382 shared_mem_handle)); |
350 return true; | 383 return true; |
351 } | 384 } |
OLD | NEW |