Chromium Code Reviews| Index: chrome/renderer/print_web_view_helper.cc |
| diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc |
| index 8ee1c07110fa2236c31b2c9763f98faa391fb50b..afb12b5b1b2303a3fd3a631819eaca0293a8989b 100644 |
| --- a/chrome/renderer/print_web_view_helper.cc |
| +++ b/chrome/renderer/print_web_view_helper.cc |
| @@ -126,6 +126,7 @@ bool PrintMsg_Print_Params_IsEqual( |
| oldParams.params.supports_alpha_blend == |
| newParams.params.supports_alpha_blend && |
| oldParams.pages.size() == newParams.pages.size() && |
| + oldParams.params.print_to_pdf == newParams.params.print_to_pdf && |
| oldParams.params.display_header_footer == |
| newParams.params.display_header_footer && |
| oldParams.params.date == newParams.params.date && |
| @@ -152,6 +153,12 @@ bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
| return mime == "application/pdf"; |
| } |
| +bool PrintingFrameHasPageSizeStyle(WebFrame* frame) { |
| + if (!frame) |
| + return false; |
| + return frame->hasCustomPageSizeStyle(0); |
| +} |
| + |
| printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
| if (frame->isPrintScalingDisabledForPlugin(node)) |
| return printing::NO_MARGINS; |
| @@ -440,6 +447,8 @@ PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) |
| print_web_view_(NULL), |
| is_preview_enabled_(switches::IsPrintPreviewEnabled()), |
| is_print_ready_metafile_sent_(false), |
| + ignore_frame_margins_css_(false), |
| + fit_to_page_(true), |
| user_cancelled_scripted_print_count_(0), |
| notify_browser_of_print_failure_(true) { |
| } |
| @@ -558,6 +567,22 @@ void PrintWebViewHelper::OnPrintForSystemDialog() { |
| Print(frame, print_preview_context_.node()); |
| } |
| +void PrintWebViewHelper::UpdateFrameMarginsCssInfo( |
| + const DictionaryValue& settings) { |
| + int margins_type = 0; |
| + if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type)) |
| + margins_type = printing::DEFAULT_MARGINS; |
| + ignore_frame_margins_css_ = margins_type != printing::DEFAULT_MARGINS; |
| +} |
| + |
| +bool PrintWebViewHelper::IsPrintToPdfRequested( |
| + const DictionaryValue& job_settings) { |
| + bool print_to_pdf = false; |
| + if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
| + NOTREACHED(); |
| + return print_to_pdf; |
| +} |
| + |
| void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| DCHECK(is_preview_enabled_); |
| print_preview_context_.OnPrintPreview(); |
| @@ -618,8 +643,24 @@ void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| bool PrintWebViewHelper::CreatePreviewDocument() { |
| PrintMsg_Print_Params print_params = print_pages_params_->params; |
| const std::vector<int>& pages = print_pages_params_->pages; |
| - if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) |
| + if (!print_preview_context_.CreatePreviewDocument( |
| + &print_params, pages, ignore_frame_margins_css_, |
| + fit_to_page_)) { |
| return false; |
| + } |
| + |
| + PageSizeMargins default_page_layout; |
| + GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), 0, print_params, |
| + ignore_frame_margins_css_, fit_to_page_, NULL, &default_page_layout); |
| + if (!old_print_pages_params_.get() || |
| + !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { |
| + bool has_page_size_style = PrintingFrameHasPageSizeStyle( |
| + print_preview_context_.frame()); |
| + // Margins: Send default page layout to browser process. |
| + Send(new PrintHostMsg_DidGetDefaultPageLayout( |
| + routing_id(), default_page_layout, has_page_size_style)); |
| + } |
| + |
| PrintHostMsg_DidGetPreviewPageCount_Params params; |
| params.page_count = print_preview_context_.total_page_count(); |
| params.is_modifiable = print_preview_context_.IsModifiable(); |
| @@ -840,7 +881,8 @@ bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
| PrintMsg_Print_Params print_params = params.params; |
| PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); |
| UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, |
| - &print_params); |
| + &print_params, ignore_frame_margins_css_, |
| + fit_to_page_); |
| int page_count = prep_frame_view.GetExpectedPageCount(); |
| if (!page_count) |
| @@ -880,6 +922,9 @@ void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
| WebFrame* frame, |
| int page_index, |
| const PrintMsg_Print_Params& default_params, |
| + bool ignore_frame_margins_css, |
| + bool fit_to_page, |
| + double* scale_factor, |
| PageSizeMargins* page_layout_in_points) { |
| int dpi = GetDPI(&default_params); |
| @@ -904,40 +949,109 @@ void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
| dpi, printing::kPixelsPerInch); |
| if (frame) { |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
If I'm reading this correctly, if ignore_frame_mar
kmadhusu
2011/12/02 23:33:19
Done.
|
| + int old_margin_top_in_pixels = margin_top_in_pixels; |
| + int old_margin_bottom_in_pixels = margin_bottom_in_pixels; |
| + int old_margin_left_in_pixels = margin_left_in_pixels; |
| + int old_margin_right_in_pixels = margin_right_in_pixels; |
| + |
| frame->pageSizeAndMarginsInPixels(page_index, |
| page_size_in_pixels, |
| margin_top_in_pixels, |
| margin_right_in_pixels, |
| margin_bottom_in_pixels, |
| margin_left_in_pixels); |
| + if (ignore_frame_margins_css) { |
| + margin_top_in_pixels = old_margin_top_in_pixels; |
| + margin_bottom_in_pixels = old_margin_bottom_in_pixels; |
| + margin_left_in_pixels = old_margin_left_in_pixels; |
| + margin_right_in_pixels = old_margin_right_in_pixels; |
| + } |
| } |
| - page_layout_in_points->content_width = |
| + double new_printable_width = |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
is this printable width, page width, or content wi
kmadhusu
2011/12/02 23:33:19
Done.
|
| ConvertPixelsToPoint(page_size_in_pixels.width - |
| - margin_left_in_pixels - |
| - margin_right_in_pixels); |
| - page_layout_in_points->content_height = |
| + margin_left_in_pixels - margin_right_in_pixels); |
| + double new_printable_height = |
| ConvertPixelsToPoint(page_size_in_pixels.height - |
| - margin_top_in_pixels - |
| - margin_bottom_in_pixels); |
| + margin_top_in_pixels - margin_bottom_in_pixels); |
| + double default_printable_width = |
| + ConvertUnit(default_params.printable_size.width(), |
| + dpi, printing::kPointsPerInch); |
| + double default_printable_height = |
| + ConvertUnit(default_params.printable_size.height(), |
| + dpi, printing::kPointsPerInch); |
| + |
| + if (ignore_frame_margins_css && fit_to_page) { |
| + new_printable_width = default_printable_width; |
| + new_printable_height = default_printable_height; |
| + } |
| // Invalid page size and/or margins. We just use the default setting. |
| - if (page_layout_in_points->content_width < 1.0 || |
| - page_layout_in_points->content_height < 1.0) { |
| + if (new_printable_width < 1.0 || new_printable_height < 1.0) { |
| CHECK(frame != NULL); |
| - GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, |
| - page_layout_in_points); |
| + GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, false, |
| + false, NULL, page_layout_in_points); |
| return; |
| } |
| - |
| - page_layout_in_points->margin_top = |
| - ConvertPixelsToPointDouble(margin_top_in_pixels); |
| - page_layout_in_points->margin_right = |
| - ConvertPixelsToPointDouble(margin_right_in_pixels); |
| - page_layout_in_points->margin_bottom = |
| - ConvertPixelsToPointDouble(margin_bottom_in_pixels); |
| - page_layout_in_points->margin_left = |
| - ConvertPixelsToPointDouble(margin_left_in_pixels); |
| + double margin_top_in_points = |
| + ConvertPixelsToPointDouble(margin_top_in_pixels); |
| + double margin_right_in_points = |
| + ConvertPixelsToPointDouble(margin_right_in_pixels); |
| + double margin_left_in_points = |
| + ConvertPixelsToPointDouble(margin_left_in_pixels); |
| + double margin_bottom_in_points = |
| + ConvertPixelsToPointDouble(margin_bottom_in_pixels); |
| + double content_width = new_printable_width; |
| + double content_height = new_printable_height; |
| + |
| + if (fit_to_page && (default_printable_height != new_printable_height || |
| + default_printable_width != new_printable_width)) { |
| + double default_page_size_height = |
| + ConvertUnit(default_params.page_size.height(), dpi, |
| + printing::kPointsPerInch); |
| + double default_page_size_width = |
| + ConvertUnit(default_params.page_size.width(), dpi, |
| + printing::kPointsPerInch); |
| + if (default_printable_width < new_printable_width || |
| + default_printable_height < new_printable_height) { |
| + double ratio_width = default_printable_width / new_printable_width; |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
I think we want to scale the CSS page box into the
kmadhusu
2011/12/02 23:33:19
Done.
kmadhusu
2011/12/02 23:33:19
Done.
|
| + double ratio_height = default_printable_height / new_printable_height; |
| + double factor = ratio_width < ratio_height ? ratio_width : ratio_height; |
| + if (scale_factor) |
| + *scale_factor = factor; |
| + |
| + content_width *= factor; |
| + content_height *= factor; |
| + margin_top_in_points = default_page_size_height/2 - content_height/2; |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
(default_page_size_height - content_height) / 2;
vandebo (ex-Chrome)
2011/12/02 01:28:19
If we scale the CSS page box into the printable ar
kmadhusu
2011/12/02 23:33:19
Done.
|
| + margin_left_in_points = default_page_size_width/2 - content_width/2; |
| + margin_right_in_points = default_page_size_width - |
| + margin_left_in_points - content_width; |
| + margin_bottom_in_points = default_page_size_height - |
| + margin_top_in_points - content_height; |
| + } else { |
| + int margin_top_bottom = margin_top_in_points + margin_bottom_in_points; |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
This doesn't seem to work out. The formula we wor
kmadhusu
2011/12/02 23:33:19
This is the formula for scaling. I have also updat
|
| + int margin_left_right = margin_left_in_points + margin_right_in_points; |
| + double ratio_y = 1.0f, ratio_x = 1.0f; |
| + if (margin_top_bottom > 0) { |
| + ratio_y = (default_page_size_height - new_printable_height) / |
| + margin_top_bottom; |
| + } |
| + if (margin_left_right > 0) { |
| + ratio_x = (default_page_size_width - new_printable_width) / |
| + margin_left_right; |
| + } |
| + margin_top_in_points *= ratio_y; |
| + margin_left_in_points *= ratio_x; |
| + margin_right_in_points *= ratio_x; |
| + margin_bottom_in_points *= ratio_y; |
| + } |
| + } |
| + page_layout_in_points->content_width = content_width; |
| + page_layout_in_points->content_height = content_height; |
| + page_layout_in_points->margin_top = margin_top_in_points; |
| + page_layout_in_points->margin_right = margin_right_in_points; |
| + page_layout_in_points->margin_bottom = margin_bottom_in_points; |
| + page_layout_in_points->margin_left = margin_left_in_points; |
| } |
| // static - Not anonymous so that platform implementations can use it. |
| @@ -945,21 +1059,23 @@ void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
| WebFrame* frame, |
| const WebNode& node, |
| PrepareFrameAndViewForPrint* prepare, |
| - PrintMsg_Print_Params* params) { |
| + PrintMsg_Print_Params* params, |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
pass by value, since we want to change it in the b
kmadhusu
2011/12/02 23:33:19
Done.
|
| + bool ignore_frame_margins_css, |
| + bool fit_to_page) { |
| if (PrintingNodeOrPdfFrame(frame, node)) |
| return; |
| PageSizeMargins page_layout_in_points; |
| - PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, |
| - &page_layout_in_points); |
| + PrintMsg_Print_Params current_params = *params; |
| + PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, current_params, |
| + ignore_frame_margins_css, false, NULL, &page_layout_in_points); |
| int dpi = GetDPI(params); |
| - params->printable_size = gfx::Size( |
| + current_params.printable_size = gfx::Size( |
| static_cast<int>(ConvertUnitDouble( |
| page_layout_in_points.content_width, |
| printing::kPointsPerInch, dpi)), |
| static_cast<int>(ConvertUnitDouble( |
| page_layout_in_points.content_height, |
| printing::kPointsPerInch, dpi))); |
| - |
| double page_width_in_points = |
| page_layout_in_points.content_width + |
| page_layout_in_points.margin_left + |
| @@ -969,18 +1085,17 @@ void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
| page_layout_in_points.margin_top + |
| page_layout_in_points.margin_bottom; |
| - params->page_size = gfx::Size( |
| - static_cast<int>(ConvertUnitDouble( |
| + current_params.page_size = gfx::Size( |
| + static_cast<int>(ConvertUnitDouble( |
| page_width_in_points, printing::kPointsPerInch, dpi)), |
| static_cast<int>(ConvertUnitDouble( |
| page_height_in_points, printing::kPointsPerInch, dpi))); |
| - |
| - params->margin_top = static_cast<int>(ConvertUnitDouble( |
| + current_params.margin_top = |
| + static_cast<int>(ConvertUnitDouble( |
| page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
| - params->margin_left = static_cast<int>(ConvertUnitDouble( |
| + current_params.margin_left = static_cast<int>(ConvertUnitDouble( |
| page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); |
| - |
| - prepare->UpdatePrintParams(*params); |
| + prepare->UpdatePrintParams(current_params); |
| } |
| bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
| @@ -1024,7 +1139,9 @@ bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
| prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
| frame, node)); |
| UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
| - &print_pages_params_->params); |
| + &print_pages_params_->params, |
| + ignore_frame_margins_css_, |
| + fit_to_page_); |
| Send(new PrintHostMsg_DidGetDocumentCookie( |
| routing_id(), print_pages_params_->params.document_cookie)); |
| return true; |
| @@ -1078,6 +1195,9 @@ bool PrintWebViewHelper::UpdatePrintSettings( |
| margin_type); |
| job_settings = &modified_job_settings; |
| } |
| + UpdateFrameMarginsCssInfo(*job_settings); |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
You can put both of these lines in a !print_for_pr
kmadhusu
2011/12/02 23:33:19
Done.
|
| + fit_to_page_ = !print_for_preview && source_is_html && |
| + !IsPrintToPdfRequested(*job_settings); |
| // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
| // possible. |
| @@ -1125,15 +1245,7 @@ bool PrintWebViewHelper::UpdatePrintSettings( |
| return false; |
| } |
| - // Margins: Send default page layout to browser process. |
| - PageSizeMargins default_page_layout; |
| - GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, |
| - &default_page_layout); |
| - if (!old_print_pages_params_.get() || |
| - !PageLayoutIsEqual(*old_print_pages_params_, settings)) { |
| - Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), |
| - default_page_layout)); |
| - } |
| + settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
I think this can go inside the !print_for_preview
kmadhusu
2011/12/02 23:33:19
It is already within that block.
|
| // Header/Footer: Set |header_footer_info_|. |
| if (settings.params.display_header_footer) { |
| @@ -1371,7 +1483,9 @@ void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| PrintMsg_Print_Params* print_params, |
| - const std::vector<int>& pages) { |
| + const std::vector<int>& pages, |
| + bool ignore_frame_margins_css, |
| + bool fit_to_page) { |
| DCHECK_EQ(INITIALIZED, state_); |
| state_ = RENDERING; |
| @@ -1386,7 +1500,9 @@ bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
| node())); |
| UpdatePrintableSizeInPrintParameters(frame_, node_, |
| - prep_frame_view_.get(), print_params); |
| + prep_frame_view_.get(), print_params, |
| + ignore_frame_margins_css, |
| + fit_to_page); |
| print_params_.reset(new PrintMsg_Print_Params(*print_params)); |