Chromium Code Reviews| 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. | |
|
Albert Bodenhamer
2012/05/02 23:49:18
In my dream world we can just create a DC that mat
| |
| 128 // Ideal way would be to return scale factor all the way from the plugin: | |
| 129 // webkit::ppapi::PluginInstance::PrintPDFOutput - scale calculated here | |
| 130 // webkit::ppapi::PluginInstance::PrintPageHelper | |
| 131 // webkit::ppapi::PluginInstance::PrintPage | |
| 132 // webkit::ppapi::WebPluginImpl::printPage | |
| 133 // WebKit::WebPluginContainerImpl::printPage | |
| 134 // WebKit::ChromePluginPrintContext::spoolPage - always return 1.0 scale | |
| 135 // WebKit::WebFrameImpl::printPage | |
| 136 // PrintWebViewHelper::RenderPage | |
| 137 // PrintWebViewHelper::PrintPageInternal | |
| 138 | |
| 139 printing::ClearCustomPrintingPageScale(); | |
| 140 | |
| 152 // Render page for printing. | 141 // Render page for printing. |
| 153 metafile.reset(RenderPage(params.params, page_number, frame, false, | 142 metafile.reset(RenderPage(params.params, page_number, frame, false, |
| 154 metafile.get(), &actual_shrink, &page_size_in_dpi, | 143 metafile.get(), &actual_shrink, &page_size_in_dpi, |
| 155 &content_area_in_dpi)); | 144 &content_area_in_dpi)); |
| 156 | 145 |
| 146 double custom_scale; | |
| 147 if (printing::GetCustomPrintingPageScale(&custom_scale)) { | |
| 148 actual_shrink = custom_scale; | |
| 149 printing::ClearCustomPrintingPageScale(); | |
| 150 } | |
| 151 | |
| 157 // Close the device context to retrieve the compiled metafile. | 152 // Close the device context to retrieve the compiled metafile. |
| 158 if (!metafile->FinishDocument()) | 153 if (!metafile->FinishDocument()) |
| 159 NOTREACHED(); | 154 NOTREACHED(); |
| 160 | 155 |
| 161 // Get the size of the compiled metafile. | 156 // Get the size of the compiled metafile. |
| 162 uint32 buf_size = metafile->GetDataSize(); | 157 uint32 buf_size = metafile->GetDataSize(); |
| 163 DCHECK_GT(buf_size, 128u); | 158 DCHECK_GT(buf_size, 128u); |
| 164 | 159 |
| 165 PrintHostMsg_DidPrintPage_Params page_params; | 160 PrintHostMsg_DidPrintPage_Params page_params; |
| 166 page_params.data_size = buf_size; | 161 page_params.data_size = buf_size; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 shared_buf.Unmap(); | 370 shared_buf.Unmap(); |
| 376 return false; | 371 return false; |
| 377 } | 372 } |
| 378 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 373 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 379 shared_buf.Unmap(); | 374 shared_buf.Unmap(); |
| 380 | 375 |
| 381 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 376 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
| 382 shared_mem_handle)); | 377 shared_mem_handle)); |
| 383 return true; | 378 return true; |
| 384 } | 379 } |
| OLD | NEW |