diff options
author | aayushkumar@chromium.org <aayushkumar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-17 22:04:58 +0000 |
---|---|---|
committer | aayushkumar@chromium.org <aayushkumar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-17 22:04:58 +0000 |
commit | 540ac80c83db3f4865d007288a4f1b8524e0aa86 (patch) | |
tree | 949f85a92cd501baef2827c063e3476ae27bb97c /printing | |
parent | 6895bf926cba01ef455bb7327703aa5378a46488 (diff) | |
download | chromium_src-540ac80c83db3f4865d007288a4f1b8524e0aa86.zip chromium_src-540ac80c83db3f4865d007288a4f1b8524e0aa86.tar.gz chromium_src-540ac80c83db3f4865d007288a4f1b8524e0aa86.tar.bz2 |
Added Header and Footer support in Linux, Windows and Mac for Skia
BUG=67514
TEST=
In the preview tab, note added options for printing headers and footers. Toggle with the checkbox and ensure that the correct headers and footers are displayed.
Review URL: http://codereview.chromium.org/7348010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97219 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r-- | printing/print_job_constants.cc | 31 | ||||
-rw-r--r-- | printing/print_job_constants.h | 22 | ||||
-rw-r--r-- | printing/print_settings.h | 10 | ||||
-rw-r--r-- | printing/print_settings_initializer.cc | 72 | ||||
-rw-r--r-- | printing/print_settings_initializer.h | 34 | ||||
-rw-r--r-- | printing/printing.gyp | 2 | ||||
-rw-r--r-- | printing/printing_context.cc | 10 | ||||
-rw-r--r-- | printing/printing_context.h | 14 | ||||
-rw-r--r-- | printing/printing_context_cairo.cc | 2 | ||||
-rw-r--r-- | printing/printing_context_cairo.h | 5 | ||||
-rw-r--r-- | printing/printing_context_mac.h | 5 | ||||
-rw-r--r-- | printing/printing_context_mac.mm | 2 | ||||
-rw-r--r-- | printing/printing_context_win.cc | 2 | ||||
-rw-r--r-- | printing/printing_context_win.h | 5 | ||||
-rw-r--r-- | printing/units.cc | 15 | ||||
-rw-r--r-- | printing/units.h | 7 |
16 files changed, 225 insertions, 13 deletions
diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index e5c67ec..436d656 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -30,6 +30,37 @@ const char kSettingDeviceName[] = "deviceName"; // Print job duplex mode. const char kSettingDuplexMode[] = "duplex"; +// Option to print headers and Footers: true if selected, false if not. +const char kSettingHeaderFooterEnabled[] = "headerFooterEnabled"; + +// Default font family name for printing the headers and footers. +const char kSettingHeaderFooterFontFamilyName[] = "sans"; + +// Default font name for printing the headers and footers. +const char kSettingHeaderFooterFontName[] = "Helvetica"; + +// Default font size for printing the headers and footers. +const int kSettingHeaderFooterFontSize = 8; + +// Number of horizontal regions for headers and footers. +const float kSettingHeaderFooterHorizontalRegions = 3; + +// Interstice or gap between different header footer components. +// Hardcoded to 0.25cm = 1/10" = 7.2points. +const float kSettingHeaderFooterInterstice = 7.2f; + +// Key that specifies the date of the page that will be printed in the headers +// and footers. +const char kSettingHeaderFooterDate[] = "date"; + +// Key that specifies the title of the page that will be printed in the headers +// and footers. +const char kSettingHeaderFooterTitle[] = "title"; + +// Key that specifies the URL of the page that will be printed in the headers +// and footers. +const char kSettingHeaderFooterURL[] = "url"; + // Page orientation: true for landscape, false for portrait. const char kSettingLandscape[] = "landscape"; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index 1997673..89a6e64 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -15,6 +15,15 @@ extern const char kSettingColor[]; extern const char kSettingCopies[]; extern const char kSettingDeviceName[]; extern const char kSettingDuplexMode[]; +extern const char kSettingHeaderFooterEnabled[]; +extern const char kSettingHeaderFooterFontFamilyName[]; +extern const char kSettingHeaderFooterFontName[]; +extern const int kSettingHeaderFooterFontSize; +extern const float kSettingHeaderFooterHorizontalRegions; +extern const float kSettingHeaderFooterInterstice; +extern const char kSettingHeaderFooterDate[]; +extern const char kSettingHeaderFooterTitle[]; +extern const char kSettingHeaderFooterURL[]; extern const char kSettingLandscape[]; extern const char kSettingPageRange[]; extern const char kSettingPageRangeFrom[]; @@ -33,6 +42,19 @@ enum DuplexMode { SHORT_EDGE, }; +// Specifies the horizontal alignment of the headers and footers. +enum HorizontalHeaderFooterPosition { + LEFT, + CENTER, + RIGHT +}; + +// Specifies the vertical alignment of the Headers and Footers. +enum VerticalHeaderFooterPosition { + TOP, + BOTTOM +}; + } // namespace printing #endif // PRINTING_PRINT_JOB_CONSTANTS_H_ diff --git a/printing/print_settings.h b/printing/print_settings.h index ee78556..8fea6dd 100644 --- a/printing/print_settings.h +++ b/printing/print_settings.h @@ -7,6 +7,8 @@ #include <string> +#include "base/memory/scoped_ptr.h" +#include "base/string16.h" #include "printing/page_range.h" #include "printing/page_setup.h" #include "ui/gfx/rect.h" @@ -95,6 +97,14 @@ class PrintSettings { // Updates the orientation and flip the page if needed. void SetOrientation(bool landscape); + // Strings to be printed as headers and footers if requested by the user. + string16 date; + string16 title; + string16 url; + + // True if the user wants headers and footers to be displayed. + bool display_header_footer; + private: ////////////////////////////////////////////////////////////////////////////// // Settings that can't be changed without side-effects. diff --git a/printing/print_settings_initializer.cc b/printing/print_settings_initializer.cc new file mode 100644 index 0000000..82d6955 --- /dev/null +++ b/printing/print_settings_initializer.cc @@ -0,0 +1,72 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/print_settings_initializer.h" + +#include <algorithm> +#include <cmath> + +#include "base/i18n/time_formatting.h" +#include "base/string_number_conversions.h" +#include "base/time.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" +#include "googleurl/src/gurl.h" +#include "printing/print_job_constants.h" +#include "printing/print_settings.h" +#include "printing/units.h" +#include "ui/base/text/text_elider.h" + +using base::DictionaryValue; +using base::Time; +using printing::ConvertPointsToPixelDouble; +using printing::ConvertUnitDouble; +using printing::GetHeaderFooterSegmentWidth; + +namespace printing { + +void PrintSettingsInitializer::InitHeaderFooterStrings( + const DictionaryValue& job_settings, + PrintSettings* print_settings) { + if (!job_settings.GetBoolean(printing::kSettingHeaderFooterEnabled, + &print_settings->display_header_footer)) { + NOTREACHED(); + } + if (!print_settings->display_header_footer) + return; + + string16 date = base::TimeFormatShortDateNumeric(Time::Now()); + string16 title; + std::string url; + if (!job_settings.GetString(printing::kSettingHeaderFooterTitle, &title) || + !job_settings.GetString(printing::kSettingHeaderFooterURL, &url)) { + NOTREACHED(); + } + + gfx::Font font(UTF8ToUTF16(printing::kSettingHeaderFooterFontName), + ceil(ConvertPointsToPixelDouble( + printing::kSettingHeaderFooterFontSize))); + double segment_width = GetHeaderFooterSegmentWidth(ConvertUnitDouble( + print_settings->page_setup_device_units().physical_size().width(), + print_settings->device_units_per_inch(), + printing::kPixelsPerInch)); + date = ui::ElideText(date, font, segment_width, false); + print_settings->date = date; + + // Calculate the available title width. If the date string is not long + // enough, increase the available space for the title. + // Assumes there is no header text to RIGHT of title. + double date_width = font.GetStringWidth(date); + double max_title_width = std::min(2 * segment_width, + 2 * (segment_width - date_width) + + segment_width); + print_settings->title = ui::ElideText(title, font, max_title_width, false); + + double max_url_width = 2 * segment_width; + GURL gurl(url); + print_settings->url = ui::ElideUrl(gurl, font, max_url_width, std::string()); +} + +} // namespace printing + diff --git a/printing/print_settings_initializer.h b/printing/print_settings_initializer.h new file mode 100644 index 0000000..de2e3cd --- /dev/null +++ b/printing/print_settings_initializer.h @@ -0,0 +1,34 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PRINTING_PRINT_SETTINGS_INITIALIZER_H_ +#define PRINTING_PRINT_SETTINGS_INITIALIZER_H_ + +#include "base/basictypes.h" +#include "base/logging.h" + +namespace base { +class DictionaryValue; +} + +namespace printing { + +class PrintSettings; + +// Initializes the header footer strings in the PrintSettings object from the +// provided |job_settings|. +class PrintSettingsInitializer { + public: + static void InitHeaderFooterStrings( + const base::DictionaryValue& job_settings, + PrintSettings* print_settings); + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(PrintSettingsInitializer); +}; + +} // namespace printing + +#endif // PRINTING_PRINT_SETTINGS_INITIALIZER_H_ + diff --git a/printing/printing.gyp b/printing/printing.gyp index eacf333..4f5483f 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -73,6 +73,8 @@ 'print_job_constants.h', 'print_settings.cc', 'print_settings.h', + 'print_settings_initializer.cc', + 'print_settings_initializer.h', 'print_settings_initializer_gtk.cc', 'print_settings_initializer_gtk.h', 'print_settings_initializer_mac.cc', diff --git a/printing/printing_context.cc b/printing/printing_context.cc index fb554e7..4e9d03a 100644 --- a/printing/printing_context.cc +++ b/printing/printing_context.cc @@ -5,6 +5,7 @@ #include "printing/printing_context.h" #include "base/values.h" +#include "printing/print_settings_initializer.h" namespace printing { @@ -33,4 +34,13 @@ PrintingContext::Result PrintingContext::OnError() { return abort_printing_ ? CANCEL : FAILED; } +PrintingContext::Result PrintingContext::UpdatePrintSettings( + const base::DictionaryValue& job_settings, + const PageRanges& ranges) { + PrintingContext::Result result = UpdatePrinterSettings(job_settings, ranges); + printing::PrintSettingsInitializer::InitHeaderFooterStrings(job_settings, + &settings_); + return result; +} + } // namespace printing diff --git a/printing/printing_context.h b/printing/printing_context.h index dd7e058..c7184e7 100644 --- a/printing/printing_context.h +++ b/printing/printing_context.h @@ -51,10 +51,16 @@ class PrintingContext { // default device settings. virtual Result UseDefaultSettings() = 0; - // Updates print settings. |job_settings| contains all print job settings - // information. |ranges| has the new page range settings. - virtual Result UpdatePrintSettings(const base::DictionaryValue& job_settings, - const PageRanges& ranges) = 0; + // Updates printer related settings. |job_settings| contains all print job + // settings information. |ranges| has the new page range settings. + virtual Result UpdatePrinterSettings( + const base::DictionaryValue& job_settings, + const PageRanges& ranges) = 0; + + // Updates Print Settings. |job_settings| contains all print job + // settings information. |ranges| has the new page range settings. + Result UpdatePrintSettings(const base::DictionaryValue& job_settings, + const PageRanges& ranges); // Initializes with predefined settings. virtual Result InitWithSettings(const PrintSettings& settings) = 0; diff --git a/printing/printing_context_cairo.cc b/printing/printing_context_cairo.cc index 3da44a1..16eb837 100644 --- a/printing/printing_context_cairo.cc +++ b/printing/printing_context_cairo.cc @@ -140,7 +140,7 @@ PrintingContext::Result PrintingContextCairo::UseDefaultSettings() { return OK; } -PrintingContext::Result PrintingContextCairo::UpdatePrintSettings( +PrintingContext::Result PrintingContextCairo::UpdatePrinterSettings( const DictionaryValue& job_settings, const PageRanges& ranges) { #if defined(OS_CHROMEOS) bool landscape = false; diff --git a/printing/printing_context_cairo.h b/printing/printing_context_cairo.h index 49d72b5..20c3b8c 100644 --- a/printing/printing_context_cairo.h +++ b/printing/printing_context_cairo.h @@ -39,8 +39,9 @@ class PrintingContextCairo : public PrintingContext { bool has_selection, PrintSettingsCallback* callback); virtual Result UseDefaultSettings(); - virtual Result UpdatePrintSettings(const base::DictionaryValue& job_settings, - const PageRanges& ranges); + virtual Result UpdatePrinterSettings( + const base::DictionaryValue& job_settings, + const PageRanges& ranges); virtual Result InitWithSettings(const PrintSettings& settings); virtual Result NewDocument(const string16& document_name); virtual Result NewPage(); diff --git a/printing/printing_context_mac.h b/printing/printing_context_mac.h index cb3e0e2..655ef6a 100644 --- a/printing/printing_context_mac.h +++ b/printing/printing_context_mac.h @@ -30,8 +30,9 @@ class PrintingContextMac : public PrintingContext { bool has_selection, PrintSettingsCallback* callback); virtual Result UseDefaultSettings(); - virtual Result UpdatePrintSettings(const base::DictionaryValue& job_settings, - const PageRanges& ranges); + virtual Result UpdatePrinterSettings( + const base::DictionaryValue& job_settings, + const PageRanges& ranges); virtual Result InitWithSettings(const PrintSettings& settings); virtual Result NewDocument(const string16& document_name); virtual Result NewPage(); diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm index 90654e2..f839802 100644 --- a/printing/printing_context_mac.mm +++ b/printing/printing_context_mac.mm @@ -98,7 +98,7 @@ PrintingContext::Result PrintingContextMac::UseDefaultSettings() { return OK; } -PrintingContext::Result PrintingContextMac::UpdatePrintSettings( +PrintingContext::Result PrintingContextMac::UpdatePrinterSettings( const DictionaryValue& job_settings, const PageRanges& ranges) { DCHECK(!in_print_job_); diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc index 17e5890..ef45060 100644 --- a/printing/printing_context_win.cc +++ b/printing/printing_context_win.cc @@ -302,7 +302,7 @@ PrintingContext::Result PrintingContextWin::UseDefaultSettings() { return FAILED; } -PrintingContext::Result PrintingContextWin::UpdatePrintSettings( +PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( const DictionaryValue& job_settings, const PageRanges& ranges) { DCHECK(!in_print_job_); diff --git a/printing/printing_context_win.h b/printing/printing_context_win.h index 81a61c3..c830736 100644 --- a/printing/printing_context_win.h +++ b/printing/printing_context_win.h @@ -28,8 +28,9 @@ class PrintingContextWin : public PrintingContext { bool has_selection, PrintSettingsCallback* callback); virtual Result UseDefaultSettings(); - virtual Result UpdatePrintSettings(const base::DictionaryValue& job_settings, - const PageRanges& ranges); + virtual Result UpdatePrinterSettings( + const base::DictionaryValue& job_settings, + const PageRanges& ranges); virtual Result InitWithSettings(const PrintSettings& settings); virtual Result NewDocument(const string16& document_name); virtual Result NewPage(); diff --git a/printing/units.cc b/printing/units.cc index 976fa80..84d78bb 100644 --- a/printing/units.cc +++ b/printing/units.cc @@ -5,6 +5,7 @@ #include "printing/units.h" #include "base/logging.h" +#include "printing/print_job_constants.h" namespace printing { @@ -47,4 +48,18 @@ double ConvertPixelsToPointDouble(double pixels) { return ConvertUnitDouble(pixels, kPixelsPerInch, kPointsPerInch); } +double ConvertPointsToPixelDouble(double points) { + return ConvertUnitDouble(points, kPointsPerInch, kPixelsPerInch); +} + +double GetHeaderFooterSegmentWidth(double page_width) { + // Interstice is left at both ends of the page as well as between + // each region, so 1 is added. + double total_interstice_width = + (printing::kSettingHeaderFooterHorizontalRegions + 1) * + printing::kSettingHeaderFooterInterstice; + return (page_width - total_interstice_width) / + printing::kSettingHeaderFooterHorizontalRegions; +} + } // namespace printing diff --git a/printing/units.h b/printing/units.h index cc61241..bd6d67a 100644 --- a/printing/units.h +++ b/printing/units.h @@ -36,6 +36,13 @@ int ConvertPixelsToPoint(int pixels); // Converts from 1 pixel to 1 point using doubles. double ConvertPixelsToPointDouble(double pixels); +// Converts from 1 point to 1 pixel using doubles. +double ConvertPointsToPixelDouble(double points); + +// Splits the horizontal width equally into segments with an interstice +// between each segment. Returns the width of a segment. +double GetHeaderFooterSegmentWidth(double page_width); + } // namespace printing #endif // PRINTING_UNITS_H_ |