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" |
11 #include "base/win/scoped_gdi_object.h" | 11 #include "base/win/scoped_gdi_object.h" |
12 #include "base/win/scoped_hdc.h" | 12 #include "base/win/scoped_hdc.h" |
13 #include "base/win/scoped_select_object.h" | 13 #include "base/win/scoped_select_object.h" |
14 #include "chrome/common/print_messages.h" | 14 #include "chrome/common/print_messages.h" |
| 15 #include "printing/custom_scaling.h" |
15 #include "printing/metafile.h" | 16 #include "printing/metafile.h" |
16 #include "printing/metafile_impl.h" | 17 #include "printing/metafile_impl.h" |
17 #include "printing/metafile_skia_wrapper.h" | 18 #include "printing/metafile_skia_wrapper.h" |
18 #include "printing/page_size_margins.h" | 19 #include "printing/page_size_margins.h" |
19 #include "printing/units.h" | 20 #include "printing/units.h" |
20 #include "skia/ext/vector_canvas.h" | 21 #include "skia/ext/vector_canvas.h" |
21 #include "skia/ext/platform_device.h" | 22 #include "skia/ext/platform_device.h" |
22 #include "third_party/skia/include/core/SkRefCnt.h" | 23 #include "third_party/skia/include/core/SkRefCnt.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
24 #include "ui/gfx/gdi_util.h" | 25 #include "ui/gfx/gdi_util.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 skia::InitializeDC(metafile->context()); | 108 skia::InitializeDC(metafile->context()); |
108 | 109 |
109 int page_number = params.page_number; | 110 int page_number = params.page_number; |
110 | 111 |
111 // Calculate the dpi adjustment. | 112 // Calculate the dpi adjustment. |
112 // Browser will render context using desired_dpi, so we need to calculate | 113 // Browser will render context using desired_dpi, so we need to calculate |
113 // adjustment factor to play content on the printer DC later during the | 114 // adjustment factor to play content on the printer DC later during the |
114 // actual printing. | 115 // actual printing. |
115 double actual_shrink = static_cast<float>(params.params.desired_dpi / | 116 double actual_shrink = static_cast<float>(params.params.desired_dpi / |
116 params.params.dpi); | 117 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 } | |
149 | |
150 gfx::Size page_size_in_dpi; | 118 gfx::Size page_size_in_dpi; |
151 gfx::Rect content_area_in_dpi; | 119 gfx::Rect content_area_in_dpi; |
| 120 |
| 121 // If we are printing PDF, it may not fit into metafile using 72dpi. |
| 122 // (Metafile is based on screen resolution here.) |
| 123 // (See http://code.google.com/p/chromium-os/issues/detail?id=16088) |
| 124 // If PDF plugin encounter this issue it will save custom scale in TLS, |
| 125 // so we can apply the same scaling factor here. |
| 126 // If will do so ONLY if default scaling does not work. |
| 127 // TODO(gene): We should revisit this solution for the next versions. |
| 128 // Two possible solutions: |
| 129 // We can create metafile of the right size (or resizable) |
| 130 // https://code.google.com/p/chromium/issues/detail?id=126037 |
| 131 // or |
| 132 // We should return scale factor all the way from the plugin: |
| 133 // webkit::ppapi::PluginInstance::PrintPDFOutput - scale calculated here |
| 134 // webkit::ppapi::PluginInstance::PrintPageHelper |
| 135 // webkit::ppapi::PluginInstance::PrintPage |
| 136 // webkit::ppapi::WebPluginImpl::printPage |
| 137 // WebKit::WebPluginContainerImpl::printPage |
| 138 // WebKit::ChromePluginPrintContext::spoolPage - always return 1.0 scale |
| 139 // WebKit::WebFrameImpl::printPage |
| 140 // PrintWebViewHelper::RenderPage |
| 141 // PrintWebViewHelper::PrintPageInternal |
| 142 |
| 143 printing::ClearCustomPrintingPageScale(); |
| 144 |
152 // Render page for printing. | 145 // Render page for printing. |
153 metafile.reset(RenderPage(params.params, page_number, frame, false, | 146 metafile.reset(RenderPage(params.params, page_number, frame, false, |
154 metafile.get(), &actual_shrink, &page_size_in_dpi, | 147 metafile.get(), &actual_shrink, &page_size_in_dpi, |
155 &content_area_in_dpi)); | 148 &content_area_in_dpi)); |
156 | 149 |
| 150 double custom_scale; |
| 151 if (printing::GetCustomPrintingPageScale(&custom_scale)) { |
| 152 actual_shrink = custom_scale; |
| 153 printing::ClearCustomPrintingPageScale(); |
| 154 } |
| 155 |
157 // Close the device context to retrieve the compiled metafile. | 156 // Close the device context to retrieve the compiled metafile. |
158 if (!metafile->FinishDocument()) | 157 if (!metafile->FinishDocument()) |
159 NOTREACHED(); | 158 NOTREACHED(); |
160 | 159 |
161 // Get the size of the compiled metafile. | 160 // Get the size of the compiled metafile. |
162 uint32 buf_size = metafile->GetDataSize(); | 161 uint32 buf_size = metafile->GetDataSize(); |
163 DCHECK_GT(buf_size, 128u); | 162 DCHECK_GT(buf_size, 128u); |
164 | 163 |
165 PrintHostMsg_DidPrintPage_Params page_params; | 164 PrintHostMsg_DidPrintPage_Params page_params; |
166 page_params.data_size = buf_size; | 165 page_params.data_size = buf_size; |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 shared_buf.Unmap(); | 374 shared_buf.Unmap(); |
376 return false; | 375 return false; |
377 } | 376 } |
378 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 377 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
379 shared_buf.Unmap(); | 378 shared_buf.Unmap(); |
380 | 379 |
381 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 380 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
382 shared_mem_handle)); | 381 shared_mem_handle)); |
383 return true; | 382 return true; |
384 } | 383 } |
OLD | NEW |