Skip to content

Commit

Permalink
PrintPreview: Honor the print media page size and margin values.
Browse files Browse the repository at this point in the history
BUG=104210, 100819
TEST=Added PrintWebViewHelperPreviewTests.

Review URL: http://codereview.chromium.org/8585017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117102 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kmadhusu@chromium.org committed Jan 10, 2012
1 parent 66122f4 commit 732b813
Show file tree
Hide file tree
Showing 26 changed files with 734 additions and 261 deletions.
2 changes: 2 additions & 0 deletions chrome/browser/printing/print_dialog_cloud.cc
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) {

PrintMsg_Print_Params default_settings;
default_settings.content_size = gfx::Size(kWidth, kHeight);
default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
default_settings.dpi = kDPI;
default_settings.min_shrink = kMinPageShrink;
default_settings.max_shrink = kMaxPageShrink;
Expand All @@ -419,6 +420,7 @@ void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) {
default_settings.selection_only = false;
default_settings.preview_request_id = 0;
default_settings.is_first_request = true;
default_settings.print_to_pdf = false;

if (!GetPageSetupParameters(json, default_settings)) {
NOTREACHED();
Expand Down
6 changes: 4 additions & 2 deletions chrome/browser/printing/print_preview_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,16 @@ void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) {
}

void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout(
const PageSizeMargins& page_layout_in_points) {
const PageSizeMargins& page_layout_in_points,
bool has_custom_page_size_style) {
TabContentsWrapper* print_preview_tab = GetPrintPreviewTab();
if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI())
return;

PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
print_preview_tab->web_contents()->GetWebUI());
print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points);
print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points,
has_custom_page_size_style);
}

void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) {
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/printing/print_preview_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class PrintPreviewMessageHandler : public content::WebContentsObserver {
// Message handlers.
void OnRequestPrintPreview(bool source_is_modifiable);
void OnDidGetDefaultPageLayout(
const printing::PageSizeMargins& page_layout_in_points);
const printing::PageSizeMargins& page_layout_in_points,
bool has_custom_page_size_style);
void OnDidGetPreviewPageCount(
const PrintHostMsg_DidGetPreviewPageCount_Params& params);
void OnDidPreviewPage(const PrintHostMsg_DidPreviewPage_Params& params);
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/printing/printing_message_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ void RenderParamsFromPrintSettings(const printing::PrintSettings& settings,
params->content_size.SetSize(
settings.page_setup_device_units().content_area().width(),
settings.page_setup_device_units().content_area().height());
params->printable_area.SetRect(
settings.page_setup_device_units().printable_area().x(),
settings.page_setup_device_units().printable_area().y(),
settings.page_setup_device_units().printable_area().width(),
settings.page_setup_device_units().printable_area().height());
params->margin_top = settings.page_setup_device_units().content_area().y();
params->margin_left = settings.page_setup_device_units().content_area().x();
params->dpi = settings.dpi();
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/resources/print_preview/layout_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ cr.define('print_preview', function() {
* @private
*/
onPDFLoaded_: function() {
this.fadeInOut_(!previewModifiable);
this.fadeInOut_(!previewModifiable || hasPageSizeStyle);
},

/**
Expand Down
10 changes: 8 additions & 2 deletions chrome/browser/resources/print_preview/print_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var lastSelectedPrinterIndex = 0;
// Used to disable some printing options when the preview is not modifiable.
var previewModifiable = false;

// Used to identify whether the printing frame has specific page size style.
var hasPageSizeStyle = false;

// Destination list special value constants.
const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters';
const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters';
Expand Down Expand Up @@ -835,7 +838,7 @@ function setPluginPreviewPageCount() {
* Called from PrintPreviewUI::OnDidGetPreviewPageCount().
* @param {number} pageCount The number of pages.
* @param {number} previewResponseId The preview request id that resulted in
* this response.
* this response.
*/
function onDidGetPreviewPageCount(pageCount, previewResponseId) {
if (!isExpectedPreviewResponse(previewResponseId))
Expand All @@ -850,8 +853,11 @@ function onDidGetPreviewPageCount(pageCount, previewResponseId) {
/**
* @param {printing::PageSizeMargins} pageLayout The default layout of the page
* in points.
* @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
* document has a custom page size style.
*/
function onDidGetDefaultPageLayout(pageLayout) {
function onDidGetDefaultPageLayout(pageLayout, hasCustomPageSizeStyle) {
hasPageSizeStyle = hasCustomPageSizeStyle;
marginSettings.currentDefaultPageLayout = new print_preview.PageLayout(
pageLayout.contentWidth,
pageLayout.contentHeight,
Expand Down
6 changes: 4 additions & 2 deletions chrome/browser/ui/webui/print_preview_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void PrintPreviewUI::OnDidGetPreviewPageCount(
}

void PrintPreviewUI::OnDidGetDefaultPageLayout(
const PageSizeMargins& page_layout) {
const PageSizeMargins& page_layout, bool has_custom_page_size_style) {
if (page_layout.margin_top < 0 || page_layout.margin_left < 0 ||
page_layout.margin_bottom < 0 || page_layout.margin_right < 0 ||
page_layout.content_width < 0 || page_layout.content_height < 0) {
Expand All @@ -218,7 +218,9 @@ void PrintPreviewUI::OnDidGetDefaultPageLayout(
layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);

CallJavascriptFunction("onDidGetDefaultPageLayout", layout);
base::FundamentalValue has_page_size_style(has_custom_page_size_style);
CallJavascriptFunction("onDidGetDefaultPageLayout", layout,
has_page_size_style);
}

void PrintPreviewUI::OnDidPreviewPage(int page_number,
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/ui/webui/print_preview_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class PrintPreviewUI : public ConstrainedHtmlUI {
// Notifies the Web UI of the default page layout according to the currently
// selected printer and page size.
void OnDidGetDefaultPageLayout(
const printing::PageSizeMargins& page_layout);
const printing::PageSizeMargins& page_layout,
bool has_custom_page_size_style);

// Notifies the Web UI that the 0-based page |page_number| has been rendered.
// |preview_request_id| indicates wich request resulted in this response.
Expand Down
4 changes: 4 additions & 0 deletions chrome/common/print_messages.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
PrintMsg_Print_Params::PrintMsg_Print_Params()
: page_size(),
content_size(),
printable_area(),
margin_top(0),
margin_left(0),
dpi(0),
Expand All @@ -23,6 +24,7 @@ PrintMsg_Print_Params::PrintMsg_Print_Params()
preview_ui_addr(),
preview_request_id(0),
is_first_request(false),
print_to_pdf(false),
display_header_footer(false),
date(),
title(),
Expand All @@ -34,6 +36,7 @@ PrintMsg_Print_Params::~PrintMsg_Print_Params() {}
void PrintMsg_Print_Params::Reset() {
page_size = gfx::Size();
content_size = gfx::Size();
printable_area = gfx::Rect();
margin_top = 0;
margin_left = 0;
dpi = 0;
Expand All @@ -46,6 +49,7 @@ void PrintMsg_Print_Params::Reset() {
preview_ui_addr = std::string();
preview_request_id = 0;
is_first_request = false;
print_to_pdf = false;
display_header_footer = false;
date = string16();
title = string16();
Expand Down
15 changes: 13 additions & 2 deletions chrome/common/print_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct PrintMsg_Print_Params {

gfx::Size page_size;
gfx::Size content_size;
gfx::Rect printable_area;
int margin_top;
int margin_left;
double dpi;
Expand All @@ -40,6 +41,7 @@ struct PrintMsg_Print_Params {
std::string preview_ui_addr;
int preview_request_id;
bool is_first_request;
bool print_to_pdf;
bool display_header_footer;
string16 date;
string16 title;
Expand Down Expand Up @@ -72,6 +74,9 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params)
// In pixels according to dpi_x and dpi_y.
IPC_STRUCT_TRAITS_MEMBER(content_size)

// Physical printable area of the page in pixels according to dpi.
IPC_STRUCT_TRAITS_MEMBER(printable_area)

// The y-offset of the printable area, in pixels according to dpi.
IPC_STRUCT_TRAITS_MEMBER(margin_top)

Expand Down Expand Up @@ -110,6 +115,9 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params)
// True if this is the first preview request.
IPC_STRUCT_TRAITS_MEMBER(is_first_request)

// True if print to pdf is requested.
IPC_STRUCT_TRAITS_MEMBER(print_to_pdf)

// Specifies if the header and footer should be rendered.
IPC_STRUCT_TRAITS_MEMBER(display_header_footer)

Expand Down Expand Up @@ -351,8 +359,11 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetPreviewPageCount,

// Notify the browser of the default page layout according to the currently
// selected printer and page size.
IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetDefaultPageLayout,
printing::PageSizeMargins /* page layout in points */)
// |has_custom_page_size_style| is true when the printing frame has a custom
// page size css otherwise false.
IPC_MESSAGE_ROUTED2(PrintHostMsg_DidGetDefaultPageLayout,
printing::PageSizeMargins /* page layout in points */,
bool /* has custom page size style */)

// Notify the browser a print preview page has been rendered.
IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPreviewPage,
Expand Down
6 changes: 4 additions & 2 deletions chrome/renderer/chrome_mock_render_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ void ChromeMockRenderThread::OnUpdatePrintSettings(
// Check and make sure the required settings are all there.
// We don't actually care about the values.
std::string dummy_string;
int margins_type = 0;
if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
!job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
!job_settings.GetInteger(printing::kSettingColor, NULL) ||
Expand All @@ -154,7 +155,8 @@ void ChromeMockRenderThread::OnUpdatePrintSettings(
!job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
!job_settings.GetInteger(printing::kSettingCopies, NULL) ||
!job_settings.GetString(printing::kPreviewUIAddr, &dummy_string) ||
!job_settings.GetInteger(printing::kPreviewRequestID, NULL)) {
!job_settings.GetInteger(printing::kPreviewRequestID, NULL) ||
!job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) {
return;
}

Expand All @@ -180,7 +182,7 @@ void ChromeMockRenderThread::OnUpdatePrintSettings(
}
}
std::vector<int> pages(printing::PageRange::GetPages(new_ranges));
printer_->UpdateSettings(document_cookie, params, pages);
printer_->UpdateSettings(document_cookie, params, pages, margins_type);
}
}

Expand Down
37 changes: 36 additions & 1 deletion chrome/renderer/mock_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@
#include "printing/units.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

void UpdateMargins(int margins_type, int dpi, PrintMsg_Print_Params* params) {
if (margins_type == printing::NO_MARGINS) {
params->content_size.SetSize(static_cast<int>((8.5 * dpi)),
static_cast<int>((11.0 * dpi)));
params->margin_left = 0;
params->margin_top = 0;
} else if (margins_type == printing::PRINTABLE_AREA_MARGINS) {
params->content_size.SetSize(static_cast<int>((8.0 * dpi)),
static_cast<int>((10.5 * dpi)));
params->margin_left = static_cast<int>(0.25 * dpi);
params->margin_top = static_cast<int>(0.25 * dpi);
} else if (margins_type == printing::CUSTOM_MARGINS) {
params->content_size.SetSize(static_cast<int>((7.9 * dpi)),
static_cast<int>((10.4 * dpi)));
params->margin_left = static_cast<int>(0.30 * dpi);
params->margin_top = static_cast<int>(0.30 * dpi);
}
}

} // end

MockPrinterPage::MockPrinterPage(const void* source_data,
uint32 source_size,
const printing::Image& image)
Expand All @@ -40,6 +63,7 @@ MockPrinter::MockPrinter()
number_pages_(0),
page_number_(0),
is_first_request_(true),
print_to_pdf_(false),
preview_request_id_(0),
display_header_footer_(false),
date_(ASCIIToUTF16("date")),
Expand All @@ -51,6 +75,10 @@ MockPrinter::MockPrinter()
content_size_.SetSize(static_cast<int>((7.5 * dpi_)),
static_cast<int>((10.0 * dpi_)));
margin_left_ = margin_top_ = static_cast<int>(0.5 * dpi_);
printable_area_.SetRect(static_cast<int>(0.25 * dpi_),
static_cast<int>(0.25 *dpi_),
static_cast<int>(8 * dpi_),
static_cast<int>(10.5 * dpi_));
}

MockPrinter::~MockPrinter() {
Expand Down Expand Up @@ -80,6 +108,7 @@ void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) {
selection_only_ = params.selection_only;
page_size_ = params.page_size;
content_size_ = params.content_size;
printable_area_ = params.printable_area;
margin_left_ = params.margin_left;
margin_top_ = params.margin_top;
display_header_footer_ = params.display_header_footer;
Expand Down Expand Up @@ -111,7 +140,9 @@ void MockPrinter::ScriptedPrint(int cookie,
settings->params.document_cookie = document_cookie_;
settings->params.page_size = page_size_;
settings->params.content_size = content_size_;
settings->params.printable_area = printable_area_;
settings->params.is_first_request = is_first_request_;
settings->params.print_to_pdf = print_to_pdf_;
settings->params.preview_request_id = preview_request_id_;
settings->params.display_header_footer = display_header_footer_;
settings->params.date = date_;
Expand All @@ -122,13 +153,15 @@ void MockPrinter::ScriptedPrint(int cookie,

void MockPrinter::UpdateSettings(int cookie,
PrintMsg_PrintPages_Params* params,
const std::vector<int>& pages) {
const std::vector<int>& pages,
int margins_type) {
if (document_cookie_ == -1) {
document_cookie_ = CreateDocumentCookie();
}
params->Reset();
params->pages = pages;
SetPrintParams(&(params->params));
UpdateMargins(margins_type, dpi_, &(params->params));
printer_status_ = PRINTER_PRINTING;
}

Expand Down Expand Up @@ -258,9 +291,11 @@ void MockPrinter::SetPrintParams(PrintMsg_Print_Params* params) {
params->document_cookie = document_cookie_;
params->page_size = page_size_;
params->content_size = content_size_;
params->printable_area = printable_area_;
params->margin_left = margin_left_;
params->margin_top = margin_top_;
params->is_first_request = is_first_request_;
params->print_to_pdf = print_to_pdf_;
params->preview_request_id = preview_request_id_;
params->display_header_footer = display_header_footer_;
params->date = date_;
Expand Down
6 changes: 5 additions & 1 deletion chrome/renderer/mock_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "printing/image.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"

struct PrintMsg_Print_Params;
Expand Down Expand Up @@ -78,7 +79,8 @@ class MockPrinter {
bool has_selection,
PrintMsg_PrintPages_Params* settings);
void UpdateSettings(int cookie, PrintMsg_PrintPages_Params* params,
const std::vector<int>& page_range_array);
const std::vector<int>& page_range_array,
int margins_type);
void SetPrintedPagesCount(int cookie, int number_pages);
void PrintPage(const PrintHostMsg_DidPrintPage_Params& params);

Expand Down Expand Up @@ -110,6 +112,7 @@ class MockPrinter {
gfx::Size content_size_;
int margin_left_;
int margin_top_;
gfx::Rect printable_area_;

// Specifies dots per inch.
double dpi_;
Expand All @@ -135,6 +138,7 @@ class MockPrinter {

// Used only in the preview sequence.
bool is_first_request_;
bool print_to_pdf_;
int preview_request_id_;

// Used for displaying headers and footers.
Expand Down
Loading

0 comments on commit 732b813

Please sign in to comment.