OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/print_settings_initializer.h" | 5 #include "printing/print_settings_conversion.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "printing/page_size_margins.h" | 15 #include "printing/page_size_margins.h" |
16 #include "printing/print_job_constants.h" | 16 #include "printing/print_job_constants.h" |
17 #include "printing/print_settings.h" | 17 #include "printing/print_settings.h" |
18 #include "printing/units.h" | 18 #include "printing/units.h" |
19 | 19 |
20 namespace printing { | 20 namespace printing { |
21 | 21 |
22 bool PrintSettingsInitializer::InitSettings( | 22 namespace { |
23 const base::DictionaryValue& job_settings, | 23 |
24 const PageRanges& ranges, | 24 void GetCustomMarginsFromJobSettings(const base::DictionaryValue& settings, |
25 PrintSettings* settings) { | 25 PageSizeMargins* page_size_margins) { |
| 26 const base::DictionaryValue* custom_margins; |
| 27 if (!settings.GetDictionary(kSettingMarginsCustom, &custom_margins) || |
| 28 !custom_margins->GetDouble(kSettingMarginTop, |
| 29 &page_size_margins->margin_top) || |
| 30 !custom_margins->GetDouble(kSettingMarginBottom, |
| 31 &page_size_margins->margin_bottom) || |
| 32 !custom_margins->GetDouble(kSettingMarginLeft, |
| 33 &page_size_margins->margin_left) || |
| 34 !custom_margins->GetDouble(kSettingMarginRight, |
| 35 &page_size_margins->margin_right)) { |
| 36 NOTREACHED(); |
| 37 } |
| 38 } |
| 39 |
| 40 void SetMarginsToJobSettings(const std::string& json_path, |
| 41 const PageMargins& margins, |
| 42 base::DictionaryValue* job_settings) { |
| 43 base::DictionaryValue* dict = new base::DictionaryValue; |
| 44 job_settings->Set(json_path, dict); |
| 45 dict->SetInteger(kSettingMarginTop, margins.top); |
| 46 dict->SetInteger(kSettingMarginBottom, margins.bottom); |
| 47 dict->SetInteger(kSettingMarginLeft, margins.left); |
| 48 dict->SetInteger(kSettingMarginRight, margins.right); |
| 49 } |
| 50 |
| 51 void SetSizeToJobSettings(const std::string& json_path, |
| 52 const gfx::Size& size, |
| 53 base::DictionaryValue* job_settings) { |
| 54 base::DictionaryValue* dict = new base::DictionaryValue; |
| 55 job_settings->Set(json_path, dict); |
| 56 dict->SetInteger("width", size.width()); |
| 57 dict->SetInteger("height", size.height()); |
| 58 } |
| 59 |
| 60 void SetRectToJobSettings(const std::string& json_path, |
| 61 const gfx::Rect& rect, |
| 62 base::DictionaryValue* job_settings) { |
| 63 base::DictionaryValue* dict = new base::DictionaryValue; |
| 64 job_settings->Set(json_path, dict); |
| 65 dict->SetInteger("x", rect.x()); |
| 66 dict->SetInteger("y", rect.y()); |
| 67 dict->SetInteger("width", rect.width()); |
| 68 dict->SetInteger("height", rect.height()); |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 bool PrintSettingsFromJobSettings(const base::DictionaryValue& job_settings, |
| 74 PrintSettings* settings) { |
26 bool display_header_footer = false; | 75 bool display_header_footer = false; |
27 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, | 76 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, |
28 &display_header_footer)) { | 77 &display_header_footer)) { |
29 return false; | 78 return false; |
30 } | 79 } |
31 settings->set_display_header_footer(display_header_footer); | 80 settings->set_display_header_footer(display_header_footer); |
32 | 81 |
33 if (settings->display_header_footer()) { | 82 if (settings->display_header_footer()) { |
34 base::string16 title; | 83 base::string16 title; |
35 base::string16 url; | 84 base::string16 url; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 PageMargins margins_in_points; | 117 PageMargins margins_in_points; |
69 margins_in_points.Clear(); | 118 margins_in_points.Clear(); |
70 margins_in_points.top = page_size_margins.margin_top; | 119 margins_in_points.top = page_size_margins.margin_top; |
71 margins_in_points.bottom = page_size_margins.margin_bottom; | 120 margins_in_points.bottom = page_size_margins.margin_bottom; |
72 margins_in_points.left = page_size_margins.margin_left; | 121 margins_in_points.left = page_size_margins.margin_left; |
73 margins_in_points.right = page_size_margins.margin_right; | 122 margins_in_points.right = page_size_margins.margin_right; |
74 | 123 |
75 settings->SetCustomMargins(margins_in_points); | 124 settings->SetCustomMargins(margins_in_points); |
76 } | 125 } |
77 | 126 |
78 settings->set_ranges(ranges); | 127 PageRanges new_ranges; |
| 128 const base::ListValue* page_range_array = NULL; |
| 129 if (job_settings.GetList(kSettingPageRange, &page_range_array)) { |
| 130 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { |
| 131 const base::DictionaryValue* dict; |
| 132 if (!page_range_array->GetDictionary(index, &dict)) |
| 133 continue; |
| 134 |
| 135 PageRange range; |
| 136 if (!dict->GetInteger(kSettingPageRangeFrom, &range.from) || |
| 137 !dict->GetInteger(kSettingPageRangeTo, &range.to)) { |
| 138 continue; |
| 139 } |
| 140 |
| 141 // Page numbers are 1-based in the dictionary. |
| 142 // Page numbers are 0-based for the printing context. |
| 143 range.from--; |
| 144 range.to--; |
| 145 new_ranges.push_back(range); |
| 146 } |
| 147 } |
| 148 settings->set_ranges(new_ranges); |
79 | 149 |
80 int color = 0; | 150 int color = 0; |
81 bool landscape = false; | 151 bool landscape = false; |
82 int duplex_mode = 0; | 152 int duplex_mode = 0; |
83 base::string16 device_name; | 153 base::string16 device_name; |
84 bool collate = false; | 154 bool collate = false; |
85 int copies = 1; | 155 int copies = 1; |
86 | 156 |
87 if (!job_settings.GetBoolean(kSettingCollate, &collate) || | 157 if (!job_settings.GetBoolean(kSettingCollate, &collate) || |
88 !job_settings.GetInteger(kSettingCopies, &copies) || | 158 !job_settings.GetInteger(kSettingCopies, &copies) || |
89 !job_settings.GetInteger(kSettingColor, &color) || | 159 !job_settings.GetInteger(kSettingColor, &color) || |
90 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || | 160 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || |
91 !job_settings.GetBoolean(kSettingLandscape, &landscape) || | 161 !job_settings.GetBoolean(kSettingLandscape, &landscape) || |
92 !job_settings.GetString(kSettingDeviceName, &device_name)) { | 162 !job_settings.GetString(kSettingDeviceName, &device_name)) { |
93 return false; | 163 return false; |
94 } | 164 } |
95 | 165 |
96 settings->set_collate(collate); | 166 settings->set_collate(collate); |
97 settings->set_copies(copies); | 167 settings->set_copies(copies); |
98 settings->SetOrientation(landscape); | 168 settings->SetOrientation(landscape); |
99 settings->set_device_name(device_name); | 169 settings->set_device_name(device_name); |
100 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); | 170 settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); |
101 settings->set_color(static_cast<ColorModel>(color)); | 171 settings->set_color(static_cast<ColorModel>(color)); |
102 | 172 |
103 return true; | 173 return true; |
104 } | 174 } |
105 | 175 |
| 176 void PrintSettingsToJobSettingsDebug(const PrintSettings& settings, |
| 177 base::DictionaryValue* job_settings) { |
| 178 job_settings->SetBoolean(kSettingHeaderFooterEnabled, |
| 179 settings.display_header_footer()); |
| 180 job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); |
| 181 job_settings->SetString(kSettingHeaderFooterURL, settings.url()); |
| 182 job_settings->SetBoolean(kSettingShouldPrintBackgrounds, |
| 183 settings.should_print_backgrounds()); |
| 184 job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, |
| 185 settings.selection_only()); |
| 186 job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); |
| 187 if (!settings.ranges().empty()) { |
| 188 base::ListValue* page_range_array = new base::ListValue; |
| 189 job_settings->Set(kSettingPageRange, page_range_array); |
| 190 for (size_t i = 0; i < settings.ranges().size(); ++i) { |
| 191 base::DictionaryValue* dict = new base::DictionaryValue; |
| 192 page_range_array->Append(dict); |
| 193 dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); |
| 194 dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); |
| 195 } |
| 196 } |
| 197 |
| 198 job_settings->SetBoolean(kSettingCollate, settings.collate()); |
| 199 job_settings->SetInteger(kSettingCopies, settings.copies()); |
| 200 job_settings->SetInteger(kSettingColor, settings.color()); |
| 201 job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); |
| 202 job_settings->SetBoolean(kSettingLandscape, settings.landscape()); |
| 203 job_settings->SetString(kSettingDeviceName, settings.device_name()); |
| 204 |
| 205 // Following values are not read form JSON by InitSettings, so do not have |
| 206 // common public constants. So just serialize in "debug" section. |
| 207 base::DictionaryValue* debug = new base::DictionaryValue; |
| 208 job_settings->Set("debug", debug); |
| 209 debug->SetDouble("minShrink", settings.min_shrink()); |
| 210 debug->SetDouble("maxShrink", settings.max_shrink()); |
| 211 debug->SetInteger("desiredDpi", settings.desired_dpi()); |
| 212 debug->SetInteger("dpi", settings.dpi()); |
| 213 debug->SetInteger("deviceUnitsPerInch", settings.device_units_per_inch()); |
| 214 debug->SetBoolean("support_alpha_blend", settings.should_print_backgrounds()); |
| 215 |
| 216 SetMarginsToJobSettings("requested_custom_margins_in_points", |
| 217 settings.requested_custom_margins_in_points(), |
| 218 debug); |
| 219 const PageSetup& page_setup = settings.page_setup_device_units(); |
| 220 SetMarginsToJobSettings( |
| 221 "effective_margins", page_setup.effective_margins(), debug); |
| 222 SetSizeToJobSettings("physical_size", page_setup.physical_size(), debug); |
| 223 SetRectToJobSettings("overlay_area", page_setup.overlay_area(), debug); |
| 224 SetRectToJobSettings("content_area", page_setup.content_area(), debug); |
| 225 SetRectToJobSettings("printable_area", page_setup.printable_area(), debug); |
| 226 } |
| 227 |
106 } // namespace printing | 228 } // namespace printing |
OLD | NEW |