diff options
author | vandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-15 22:30:48 +0000 |
---|---|---|
committer | vandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-15 22:30:48 +0000 |
commit | 1c23b4e86cf43bf86c591840bba6be39ad49b0c9 (patch) | |
tree | 5e3c268f4544a27d74fc81d0820e0a3fd82949ae /printing | |
parent | 0374b165987b53354edca740082640315091fd95 (diff) | |
download | chromium_src-1c23b4e86cf43bf86c591840bba6be39ad49b0c9.zip chromium_src-1c23b4e86cf43bf86c591840bba6be39ad49b0c9.tar.gz chromium_src-1c23b4e86cf43bf86c591840bba6be39ad49b0c9.tar.bz2 |
Move margin processing code to the browser process.
It seems that this is where it is supposed to live and it was erroneously added to PrintWebViewHelper.
BUG=67091, 92045, 91880, 92000, 92218, 95905
TEST=NONE
Review URL: http://codereview.chromium.org/8201027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105688 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r-- | printing/page_setup.cc | 83 | ||||
-rw-r--r-- | printing/page_setup.h | 8 | ||||
-rw-r--r-- | printing/print_dialog_gtk_interface.h | 10 | ||||
-rw-r--r-- | printing/print_settings.cc | 68 | ||||
-rw-r--r-- | printing/print_settings.h | 12 | ||||
-rw-r--r-- | printing/printing_context.cc | 52 | ||||
-rw-r--r-- | printing/printing_context.h | 4 | ||||
-rw-r--r-- | printing/printing_context_cairo.cc | 2 | ||||
-rw-r--r-- | printing/printing_context_win.cc | 5 |
9 files changed, 174 insertions, 70 deletions
diff --git a/printing/page_setup.cc b/printing/page_setup.cc index 9ac3e520..2411f6f 100644 --- a/printing/page_setup.cc +++ b/printing/page_setup.cc @@ -37,7 +37,8 @@ bool PageMargins::Equals(const PageMargins& rhs) const { bottom == rhs.bottom; } -PageSetup::PageSetup() : text_height_(0) { +PageSetup::PageSetup() { + Clear(); } PageSetup::~PageSetup() {} @@ -75,35 +76,64 @@ void PageSetup::Init(const gfx::Size& physical_size, printable_area_ = printable_area; text_height_ = text_height; + CalculateSizesWithinRect(printable_area_); +} + +void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { + requested_margins_ = requested_margins; + if (printable_area_.width() && printable_area_.height()) + CalculateSizesWithinRect(printable_area_); +} + +void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { + requested_margins_ = requested_margins; + if (physical_size_.width() && physical_size_.height()) + CalculateSizesWithinRect(gfx::Rect(physical_size_)); +} + +void PageSetup::FlipOrientation() { + if (physical_size_.width() && physical_size_.height()) { + gfx::Size new_size(physical_size_.height(), physical_size_.width()); + int new_y = physical_size_.width() - + (printable_area_.width() + printable_area_.x()); + gfx::Rect new_printable_area(printable_area_.y(), + new_y, + printable_area_.height(), + printable_area_.width()); + Init(new_size, new_printable_area, text_height_); + } +} + +void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds) { // Calculate the effective margins. The tricky part. effective_margins_.header = std::max(requested_margins_.header, - printable_area_.y()); + bounds.y()); effective_margins_.footer = std::max(requested_margins_.footer, - physical_size.height() - - printable_area_.bottom()); + physical_size_.height() - + bounds.bottom()); effective_margins_.left = std::max(requested_margins_.left, - printable_area_.x()); + bounds.x()); effective_margins_.top = std::max(std::max(requested_margins_.top, - printable_area_.y()), - effective_margins_.header + text_height); + bounds.y()), + effective_margins_.header + text_height_); effective_margins_.right = std::max(requested_margins_.right, - physical_size.width() - - printable_area_.right()); - effective_margins_.bottom = std::max(std::max(requested_margins_.bottom, - physical_size.height() - - printable_area_.bottom()), - effective_margins_.footer + text_height); + physical_size_.width() - + bounds.right()); + effective_margins_.bottom = + std::max(std::max(requested_margins_.bottom, + physical_size_.height() - bounds.bottom()), + effective_margins_.footer + text_height_); // Calculate the overlay area. If the margins are excessive, the overlay_area // size will be (0, 0). overlay_area_.set_x(effective_margins_.left); overlay_area_.set_y(effective_margins_.header); overlay_area_.set_width(std::max(0, - physical_size.width() - + physical_size_.width() - effective_margins_.right - overlay_area_.x())); overlay_area_.set_height(std::max(0, - physical_size.height() - + physical_size_.height() - effective_margins_.footer - overlay_area_.y())); @@ -112,32 +142,13 @@ void PageSetup::Init(const gfx::Size& physical_size, content_area_.set_x(effective_margins_.left); content_area_.set_y(effective_margins_.top); content_area_.set_width(std::max(0, - physical_size.width() - + physical_size_.width() - effective_margins_.right - content_area_.x())); content_area_.set_height(std::max(0, - physical_size.height() - + physical_size_.height() - effective_margins_.bottom - content_area_.y())); } -void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { - requested_margins_ = requested_margins; - if (physical_size_.width() && physical_size_.height()) - Init(physical_size_, printable_area_, text_height_); -} - -void PageSetup::FlipOrientation() { - if (physical_size_.width() && physical_size_.height()) { - gfx::Size new_size(physical_size_.height(), physical_size_.width()); - int new_y = physical_size_.width() - - (printable_area_.width() + printable_area_.x()); - gfx::Rect new_printable_area(printable_area_.y(), - new_y, - printable_area_.height(), - printable_area_.width()); - Init(new_size, new_printable_area, text_height_); - } -} - } // namespace printing diff --git a/printing/page_setup.h b/printing/page_setup.h index 7f33893..ef1552d 100644 --- a/printing/page_setup.h +++ b/printing/page_setup.h @@ -46,8 +46,12 @@ class PRINTING_EXPORT PageSetup { void Init(const gfx::Size& physical_size, const gfx::Rect& printable_area, int text_height); + // Use |requested_margins| as long as they fall inside the printable area. void SetRequestedMargins(const PageMargins& requested_margins); + // Ignore the printable area, and set the margins to |requested_margins|. + void ForceRequestedMargins(const PageMargins& requested_margins); + // Flips the orientation of the page and recalculates all page areas. void FlipOrientation(); @@ -60,6 +64,10 @@ class PRINTING_EXPORT PageSetup { } private: + // Calculate overlay_area_, effective_margins_, and content_area_, based on + // a constraint of |bounds|. + void CalculateSizesWithinRect(const gfx::Rect& bounds); + // Physical size of the page, including non-printable margins. gfx::Size physical_size_; diff --git a/printing/print_dialog_gtk_interface.h b/printing/print_dialog_gtk_interface.h index 4fb9341..22de95c 100644 --- a/printing/print_dialog_gtk_interface.h +++ b/printing/print_dialog_gtk_interface.h @@ -11,6 +11,7 @@ namespace printing { class Metafile; +class PrintSettings; // An interface for GTK printing dialogs. Classes that live outside of // printing/ can implement this interface and get threading requirements @@ -20,12 +21,13 @@ class PrintDialogGtkInterface { // Tell the dialog to use the default print setting. virtual void UseDefaultSettings() = 0; - // Update the dialog to use |settings| and |ranges|, where |settings| is a - // dictionary of settings with possible keys from + // Update the dialog to use |job_settings| and |ranges|, where |job_settings| + // is a dictionary of settings with possible keys from // printing/print_job_constants.h. Only used when printing without the system // print dialog. E.g. for Print Preview. Returns false on error. - virtual bool UpdateSettings(const base::DictionaryValue& settings, - const PageRanges& ranges) = 0; + virtual bool UpdateSettings(const base::DictionaryValue& job_settings, + const PageRanges& ranges, + PrintSettings* settings) = 0; // Shows the dialog and handles the response with |callback|. Only used when // printing with the native print dialog. diff --git a/printing/print_settings.cc b/printing/print_settings.cc index 06aac453d..0e1a743 100644 --- a/printing/print_settings.cc +++ b/printing/print_settings.cc @@ -5,6 +5,7 @@ #include "printing/print_settings.h" #include "base/atomic_sequence_num.h" +#include "base/logging.h" #include "printing/print_job_constants.h" #include "printing/units.h" @@ -114,7 +115,7 @@ PrintSettings::PrintSettings() max_shrink(2.0), desired_dpi(72), selection_only(false), - use_overlays(true), + margin_type(DEFAULT_MARGINS), display_header_footer(false), dpi_(0), landscape_(false), @@ -146,34 +147,67 @@ void PrintSettings::SetPrinterPrintableArea( gfx::Size const& physical_size_device_units, gfx::Rect const& printable_area_device_units, int units_per_inch) { - int header_footer_text_height = 0; - int margin_printer_units = 0; - if (use_overlays) { + if (display_header_footer) { // Hard-code text_height = 0.5cm = ~1/5 of inch. header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice, kPointsPerInch, units_per_inch); - // Default margins 1.0cm = ~2/5 of an inch. - margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch, - units_per_inch); } - // Start by setting the user configuration page_setup_device_units_.Init(physical_size_device_units, printable_area_device_units, header_footer_text_height); - - // Apply default margins (not user configurable just yet). - // Since the font height is half the margin we put the header and footers at - // the font height from the margins. PageMargins margins; margins.header = header_footer_text_height; margins.footer = header_footer_text_height; - margins.left = margin_printer_units; - margins.top = margin_printer_units; - margins.right = margin_printer_units; - margins.bottom = margin_printer_units; - page_setup_device_units_.SetRequestedMargins(margins); + switch (margin_type) { + case DEFAULT_MARGINS: { + // Default margins 1.0cm = ~2/5 of an inch. + int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch, + units_per_inch); + margins.top = margin_printer_units; + margins.bottom = margin_printer_units; + margins.left = margin_printer_units; + margins.right = margin_printer_units; + break; + } + case NO_MARGINS: + case PRINTABLE_AREA_MARGINS: { + margins.top = 0; + margins.bottom = 0; + margins.left = 0; + margins.right = 0; + break; + } + case CUSTOM_MARGINS: { + margins.top = ConvertUnitDouble(custom_margins_in_points_.top, + printing::kPointsPerInch, + units_per_inch); + margins.bottom = ConvertUnitDouble(custom_margins_in_points_.bottom, + printing::kPointsPerInch, + units_per_inch); + margins.left = ConvertUnitDouble(custom_margins_in_points_.left, + printing::kPointsPerInch, + units_per_inch); + margins.right = ConvertUnitDouble(custom_margins_in_points_.right, + printing::kPointsPerInch, + units_per_inch); + break; + } + default: { + NOTREACHED(); + } + } + + if (margin_type == DEFAULT_MARGINS || margin_type == PRINTABLE_AREA_MARGINS) + page_setup_device_units_.SetRequestedMargins(margins); + else + page_setup_device_units_.ForceRequestedMargins(margins); +} + +void PrintSettings::SetCustomMargins(const PageMargins& margins_in_points) { + custom_margins_in_points_ = margins_in_points; + margin_type = CUSTOM_MARGINS; } bool PrintSettings::Equals(const PrintSettings& rhs) const { diff --git a/printing/print_settings.h b/printing/print_settings.h index d73fd66..31417de 100644 --- a/printing/print_settings.h +++ b/printing/print_settings.h @@ -11,6 +11,7 @@ #include "base/string16.h" #include "printing/page_range.h" #include "printing/page_setup.h" +#include "printing/print_job_constants.h" #include "printing/printing_export.h" #include "ui/gfx/rect.h" @@ -40,6 +41,8 @@ class PRINTING_EXPORT PrintSettings { gfx::Rect const& printable_area_device_units, int units_per_inch); + void SetCustomMargins(const PageMargins& margins_in_points); + // Equality operator. // NOTE: printer_name is NOT tested for equality since it doesn't affect the // output. @@ -95,10 +98,8 @@ class PRINTING_EXPORT PrintSettings { // Indicates if the user only wants to print the current selection. bool selection_only; - // Indicates whether we should use browser-controlled page overlays - // (header, footer, margins etc). If it is false, the overlays are - // controlled by the renderer. - bool use_overlays; + // Indicates what kind of margins should be applied to the printable area. + MarginType margin_type; // Cookie generator. It is used to initialize PrintedDocument with its // associated PrintSettings, to be sure that each generated PrintedPage is @@ -137,6 +138,9 @@ class PRINTING_EXPORT PrintSettings { // True if this printer supports AlphaBlend. bool supports_alpha_blend_; + + // If margin type is custom, these are the margins. + PageMargins custom_margins_in_points_; }; } // namespace printing diff --git a/printing/printing_context.cc b/printing/printing_context.cc index 4e9d03a..46c0e6e 100644 --- a/printing/printing_context.cc +++ b/printing/printing_context.cc @@ -4,7 +4,9 @@ #include "printing/printing_context.h" +#include "base/logging.h" #include "base/values.h" +#include "printing/page_setup.h" #include "printing/print_settings_initializer.h" namespace printing { @@ -19,6 +21,11 @@ PrintingContext::PrintingContext(const std::string& app_locale) PrintingContext::~PrintingContext() { } +void PrintingContext::set_margin_type(MarginType type) { + DCHECK(type != CUSTOM_MARGINS); + settings_.margin_type = type; +} + void PrintingContext::ResetSettings() { ReleaseContext(); @@ -37,6 +44,51 @@ PrintingContext::Result PrintingContext::OnError() { PrintingContext::Result PrintingContext::UpdatePrintSettings( const base::DictionaryValue& job_settings, const PageRanges& ranges) { + ResetSettings(); + + if (!job_settings.GetBoolean(printing::kSettingHeaderFooterEnabled, + &settings_.display_header_footer)) { + NOTREACHED(); + } + + int margin_type = DEFAULT_MARGINS; + if (!job_settings.GetInteger(printing::kSettingMarginsType, &margin_type) || + (margin_type != DEFAULT_MARGINS && + margin_type != NO_MARGINS && + margin_type != CUSTOM_MARGINS && + margin_type != PRINTABLE_AREA_MARGINS)) { + NOTREACHED(); + } + settings_.margin_type = static_cast<MarginType>(margin_type); + + if (margin_type == CUSTOM_MARGINS) { + double top_margin_in_points = 0; + double bottom_margin_in_points = 0; + double left_margin_in_points = 0; + double right_margin_in_points = 0; + DictionaryValue* custom_margins; + if (!job_settings.GetDictionary(printing::kSettingMarginsCustom, + &custom_margins) || + !custom_margins->GetDouble(printing::kSettingMarginTop, + &top_margin_in_points) || + !custom_margins->GetDouble(printing::kSettingMarginBottom, + &bottom_margin_in_points) || + !custom_margins->GetDouble(printing::kSettingMarginLeft, + &left_margin_in_points) || + !custom_margins->GetDouble(printing::kSettingMarginRight, + &right_margin_in_points)) { + NOTREACHED(); + } + PageMargins margins_in_points; + margins_in_points.Clear(); + margins_in_points.top = top_margin_in_points; + margins_in_points.bottom = bottom_margin_in_points; + margins_in_points.left = left_margin_in_points; + margins_in_points.right = right_margin_in_points; + + settings_.SetCustomMargins(margins_in_points); + } + PrintingContext::Result result = UpdatePrinterSettings(job_settings, ranges); printing::PrintSettingsInitializer::InitHeaderFooterStrings(job_settings, &settings_); diff --git a/printing/printing_context.h b/printing/printing_context.h index 46e9f57..5dbe70d 100644 --- a/printing/printing_context.h +++ b/printing/printing_context.h @@ -98,9 +98,7 @@ class PRINTING_EXPORT PrintingContext { // caller owns the returned object. static PrintingContext* Create(const std::string& app_locale); - void set_use_overlays(bool use_overlays) { - settings_.use_overlays = use_overlays; - } + void set_margin_type(MarginType type); const PrintSettings& settings() const { return settings_; diff --git a/printing/printing_context_cairo.cc b/printing/printing_context_cairo.cc index a000d48..81d8d92 100644 --- a/printing/printing_context_cairo.cc +++ b/printing/printing_context_cairo.cc @@ -163,7 +163,7 @@ PrintingContext::Result PrintingContextCairo::UpdatePrinterSettings( print_dialog_->AddRefToDialog(); } - if (!print_dialog_->UpdateSettings(job_settings, ranges)) + if (!print_dialog_->UpdateSettings(job_settings, ranges, &settings_)) return OnError(); return OK; diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc index 3a1da66..e7c0f56 100644 --- a/printing/printing_context_win.cc +++ b/printing/printing_context_win.cc @@ -383,11 +383,6 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( return OK; } - // Underlying |settings_| do not have these attributes, so we need to - // operate on printer directly, which involves reloading settings. - // Therefore, reset the settings anyway. - ResetSettings(); - HANDLE printer; LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); if (!OpenPrinter(device_name_wide, &printer, NULL)) |