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 8ef7e832c5c797727e8872393cb841911e0b9a78..a454e913f2bb5ed5a7663c2cbeed56bce8f57765 100644 |
--- a/chrome/renderer/print_web_view_helper.cc |
+++ b/chrome/renderer/print_web_view_helper.cc |
@@ -109,6 +109,7 @@ bool PrintMsg_Print_Params_IsEmpty(const PrintMsg_Print_Params& params) { |
bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams, |
const PrintMsg_PrintPages_Params& newParams) { |
return oldParams.params.printable_size == newParams.params.printable_size && |
+ oldParams.params.printable_area == newParams.params.printable_area && |
oldParams.params.page_size == newParams.params.page_size && |
oldParams.params.margin_top == newParams.params.margin_top && |
oldParams.params.margin_left == newParams.params.margin_left && |
@@ -126,6 +127,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 +154,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 +448,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) { |
} |
@@ -560,6 +570,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(); |
@@ -620,8 +646,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(); |
@@ -842,7 +884,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) |
@@ -882,6 +925,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); |
@@ -905,41 +951,99 @@ void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
default_params.margin_left, |
dpi, printing::kPixelsPerInch); |
- if (frame) { |
+ if (frame && !(ignore_frame_margins_css && fit_to_page)) { |
+ 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_content_width = |
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); |
vandebo (ex-Chrome)
2011/12/04 22:20:34
When we are ignoring the css margins, I think thes
kmadhusu
2011/12/05 09:06:54
Done.
When the selected printer is "PRINT_TO_PDF
|
+ double new_content_height = |
ConvertPixelsToPoint(page_size_in_pixels.height - |
- margin_top_in_pixels - |
- margin_bottom_in_pixels); |
+ margin_top_in_pixels - margin_bottom_in_pixels); |
+ double new_page_box_width = |
+ ConvertPixelsToPoint(page_size_in_pixels.width); |
+ double new_page_box_height= |
+ ConvertPixelsToPoint(page_size_in_pixels.height); |
+ |
+ 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); |
// 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_content_width < 1.0 || new_content_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); |
+ |
+ if (fit_to_page && (default_page_size_height != new_page_box_height || |
+ default_page_size_width != new_page_box_width)) { |
+ double factor = 1.0f; |
+ if (default_page_size_width < new_page_box_width || |
+ default_page_size_height < new_page_box_height) { |
+ double minimum_printable_width = |
+ ConvertUnit(default_params.printable_area.width(), dpi, |
+ printing::kPointsPerInch); |
+ double minimum_printable_height = |
+ ConvertUnit(default_params.printable_area.height(), dpi, |
+ printing::kPointsPerInch); |
+ double ratio_width = minimum_printable_width / new_page_box_width; |
+ double ratio_height = minimum_printable_height / new_page_box_height; |
+ factor = ratio_width < ratio_height ? ratio_width : ratio_height; |
+ if (scale_factor) |
+ *scale_factor = factor; |
+ |
+ new_content_width *= factor; |
+ new_content_height *= factor; |
+ } |
+ margin_top_in_points = |
+ (default_page_size_height - new_page_box_height * factor)/2 + |
+ (margin_top_in_points * factor); |
+ margin_bottom_in_points = |
+ (default_page_size_height - new_page_box_height * factor)/2 + |
+ (margin_bottom_in_points * factor); |
+ margin_right_in_points = |
+ (default_page_size_width - new_page_box_width * factor)/2 + |
+ (margin_right_in_points * factor); |
+ margin_left_in_points = |
+ (default_page_size_width - new_page_box_width * factor)/2 + |
+ (margin_left_in_points * factor); |
+ } |
+ page_layout_in_points->content_width = new_content_width; |
+ page_layout_in_points->content_height = new_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. |
@@ -947,21 +1051,23 @@ void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
WebFrame* frame, |
const WebNode& node, |
PrepareFrameAndViewForPrint* prepare, |
- PrintMsg_Print_Params* params) { |
+ const PrintMsg_Print_Params& params, |
+ 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); |
- int dpi = GetDPI(params); |
- params->printable_size = gfx::Size( |
+ 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(¤t_params); |
+ 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 + |
@@ -971,18 +1077,17 @@ void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
page_layout_in_points.margin_top + |
page_layout_in_points.margin_bottom; |
- params->page_size = gfx::Size( |
+ 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( |
- page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
- params->margin_left = static_cast<int>(ConvertUnitDouble( |
+ current_params.margin_top = |
+ static_cast<int>(ConvertUnitDouble( |
+ page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
+ 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, |
@@ -1026,7 +1131,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; |
@@ -1127,15 +1234,9 @@ 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); |
+ UpdateFrameMarginsCssInfo(*job_settings); |
+ fit_to_page_ = source_is_html && !IsPrintToPdfRequested(*job_settings); |
// Header/Footer: Set |header_footer_info_|. |
if (settings.params.display_header_footer) { |
@@ -1373,7 +1474,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; |
@@ -1388,7 +1491,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)); |