WebKit merge 129140:129167
[chromium-blink-merge.git] / printing / print_settings_initializer.cc
blobe82bbfcdc08664732123b6884878b2084d5920e8
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/print_settings_initializer.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <string>
11 #include "base/i18n/time_formatting.h"
12 #include "base/string_number_conversions.h"
13 #include "base/time.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "googleurl/src/gurl.h"
17 #include "printing/print_job_constants.h"
18 #include "printing/print_settings.h"
19 #include "printing/units.h"
20 #include "ui/base/text/text_elider.h"
22 using base::DictionaryValue;
24 namespace printing {
26 void PrintSettingsInitializer::InitHeaderFooterStrings(
27 const DictionaryValue& job_settings,
28 PrintSettings* print_settings) {
29 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled,
30 &print_settings->display_header_footer)) {
31 NOTREACHED();
33 if (!print_settings->display_header_footer)
34 return;
36 string16 date = base::TimeFormatShortDateNumeric(base::Time::Now());
37 string16 title;
38 std::string url;
39 if (!job_settings.GetString(kSettingHeaderFooterTitle, &title) ||
40 !job_settings.GetString(kSettingHeaderFooterURL, &url)) {
41 NOTREACHED();
44 gfx::Font font(
45 kSettingHeaderFooterFontName,
46 ceil(ConvertPointsToPixelDouble(kSettingHeaderFooterFontSize)));
47 double segment_width = GetHeaderFooterSegmentWidth(ConvertUnitDouble(
48 print_settings->page_setup_device_units().physical_size().width(),
49 print_settings->device_units_per_inch(), kPixelsPerInch));
50 date = ui::ElideText(date, font, segment_width, ui::ELIDE_AT_END);
51 print_settings->date = date;
53 // Calculate the available title width. If the date string is not long
54 // enough, increase the available space for the title.
55 // Assumes there is no header text to RIGHT of title.
56 double date_width = font.GetStringWidth(date);
57 double max_title_width = std::min(2 * segment_width,
58 2 * (segment_width - date_width) +
59 segment_width);
60 print_settings->title =
61 ui::ElideText(title, font, max_title_width, ui::ELIDE_AT_END);
63 double max_url_width = 2 * segment_width;
64 GURL gurl(url);
65 print_settings->url = ui::ElideUrl(gurl, font, max_url_width, std::string());
68 } // namespace printing