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 "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 136 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, |
137 gfx::Size* result) { | 137 gfx::Size* result) { |
138 int dpi = GetDPI(&print_params); | 138 int dpi = GetDPI(&print_params); |
139 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, | 139 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, |
140 print_params.desired_dpi)); | 140 print_params.desired_dpi)); |
141 | 141 |
142 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, | 142 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, |
143 print_params.desired_dpi)); | 143 print_params.desired_dpi)); |
144 } | 144 } |
145 | 145 |
146 // Get the margins option selected and set custom margins appropriately. | |
147 void SetCustomMarginsIfSelected(const DictionaryValue& job_settings, | |
148 PrintMsg_PrintPages_Params* settings) { | |
149 bool default_margins_selected; | |
150 if (!job_settings.GetBoolean(printing::kSettingDefaultMarginsSelected, | |
151 &default_margins_selected)) { | |
152 NOTREACHED(); | |
153 default_margins_selected = true; | |
154 } | |
155 | |
156 if (default_margins_selected) | |
157 return; | |
158 | |
159 DictionaryValue* custom_margins; | |
160 if (!job_settings.GetDictionary(printing::kSettingMargins, | |
161 &custom_margins)) { | |
162 NOTREACHED(); | |
163 return; | |
164 } | |
165 | |
166 double custom_margin_top_in_points = 0; | |
167 double custom_margin_left_in_points = 0; | |
168 double custom_margin_right_in_points = 0; | |
169 double custom_margin_bottom_in_points = 0; | |
170 if (!custom_margins->GetDouble(printing::kSettingMarginTop, | |
171 &custom_margin_top_in_points) || | |
172 !custom_margins->GetDouble(printing::kSettingMarginLeft, | |
173 &custom_margin_left_in_points) || | |
174 !custom_margins->GetDouble(printing::kSettingMarginRight, | |
175 &custom_margin_right_in_points) || | |
176 !custom_margins->GetDouble(printing::kSettingMarginBottom, | |
177 &custom_margin_bottom_in_points)) { | |
178 NOTREACHED(); | |
179 return; | |
180 } | |
181 | |
182 int dpi = GetDPI(&settings->params); | |
183 double custom_margin_top_in_dots = ConvertUnitDouble( | |
184 custom_margin_top_in_points, printing::kPointsPerInch, dpi); | |
185 double custom_margin_left_in_dots = ConvertUnitDouble( | |
186 custom_margin_left_in_points, printing::kPointsPerInch, dpi); | |
187 double custom_margin_right_in_dots = ConvertUnitDouble( | |
188 custom_margin_right_in_points, printing::kPointsPerInch, dpi); | |
189 double custom_margin_bottom_in_dots = ConvertUnitDouble( | |
190 custom_margin_bottom_in_points, printing::kPointsPerInch, dpi); | |
191 | |
192 | |
193 if (custom_margin_left_in_dots < 0 || custom_margin_right_in_dots < 0 || | |
194 custom_margin_top_in_dots < 0 || custom_margin_bottom_in_dots < 0) { | |
195 NOTREACHED(); | |
196 return; | |
197 } | |
198 | |
199 if (settings->params.page_size.width() < custom_margin_left_in_dots + | |
200 custom_margin_right_in_dots || | |
201 settings->params.page_size.height() < custom_margin_top_in_dots + | |
202 custom_margin_bottom_in_dots) { | |
203 NOTREACHED(); | |
204 return; | |
205 } | |
206 | |
207 settings->params.margin_top = custom_margin_top_in_dots; | |
208 settings->params.margin_left = custom_margin_left_in_dots; | |
209 settings->params.printable_size.set_width( | |
210 settings->params.page_size.width() - custom_margin_right_in_dots - | |
211 custom_margin_left_in_dots); | |
212 settings->params.printable_size.set_height( | |
213 settings->params.page_size.height() - custom_margin_bottom_in_dots - | |
214 custom_margin_top_in_dots); | |
215 } | |
216 | |
217 // Get the (x, y) coordinate from where printing of the current text should | 146 // Get the (x, y) coordinate from where printing of the current text should |
218 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 147 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
219 // vertical alignment (TOP, BOTTOM). | 148 // vertical alignment (TOP, BOTTOM). |
220 SkPoint GetHeaderFooterPosition( | 149 SkPoint GetHeaderFooterPosition( |
221 float webkit_scale_factor, | 150 float webkit_scale_factor, |
222 const PageSizeMargins& page_layout, | 151 const PageSizeMargins& page_layout, |
223 printing::HorizontalHeaderFooterPosition horizontal_position, | 152 printing::HorizontalHeaderFooterPosition horizontal_position, |
224 printing::VerticalHeaderFooterPosition vertical_position, | 153 printing::VerticalHeaderFooterPosition vertical_position, |
225 double offset_to_baseline, | 154 double offset_to_baseline, |
226 double text_width_in_points) { | 155 double text_width_in_points) { |
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1150 | 1079 |
1151 // Margins: Send default page layout to browser process. | 1080 // Margins: Send default page layout to browser process. |
1152 PageSizeMargins default_page_layout; | 1081 PageSizeMargins default_page_layout; |
1153 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, | 1082 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, |
1154 &default_page_layout); | 1083 &default_page_layout); |
1155 if (!old_print_pages_params_.get() || | 1084 if (!old_print_pages_params_.get() || |
1156 !PageLayoutIsEqual(*old_print_pages_params_, settings)) { | 1085 !PageLayoutIsEqual(*old_print_pages_params_, settings)) { |
1157 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), | 1086 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), |
1158 default_page_layout)); | 1087 default_page_layout)); |
1159 } | 1088 } |
1160 SetCustomMarginsIfSelected(job_settings, &settings); | |
1161 | 1089 |
1162 // Header/Footer: Set |header_footer_info_|. | 1090 // Header/Footer: Set |header_footer_info_|. |
1163 if (settings.params.display_header_footer) { | 1091 if (settings.params.display_header_footer) { |
1164 header_footer_info_.reset(new DictionaryValue()); | 1092 header_footer_info_.reset(new DictionaryValue()); |
1165 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1093 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1166 settings.params.date); | 1094 settings.params.date); |
1167 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1095 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1168 settings.params.url); | 1096 settings.params.url); |
1169 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1097 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1170 settings.params.title); | 1098 settings.params.title); |
(...skipping 16 matching lines...) Expand all Loading... |
1187 // corresponding RenderViewHost instance to signal and reset the | 1115 // corresponding RenderViewHost instance to signal and reset the |
1188 // pump messages event. | 1116 // pump messages event. |
1189 params.routing_id = render_view()->routing_id(); | 1117 params.routing_id = render_view()->routing_id(); |
1190 // host_window_ may be NULL at this point if the current window is a | 1118 // host_window_ may be NULL at this point if the current window is a |
1191 // popup and the print() command has been issued from the parent. The | 1119 // popup and the print() command has been issued from the parent. The |
1192 // receiver of this message has to deal with this. | 1120 // receiver of this message has to deal with this. |
1193 params.host_window_id = render_view()->host_window(); | 1121 params.host_window_id = render_view()->host_window(); |
1194 params.cookie = print_pages_params_->params.document_cookie; | 1122 params.cookie = print_pages_params_->params.document_cookie; |
1195 params.has_selection = frame->hasSelection(); | 1123 params.has_selection = frame->hasSelection(); |
1196 params.expected_pages_count = expected_pages_count; | 1124 params.expected_pages_count = expected_pages_count; |
1197 params.use_overlays = use_browser_overlays; | 1125 params.margin_type = printing::DEFAULT_MARGINS; |
1198 | 1126 |
1199 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); | 1127 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
1200 | 1128 |
1201 print_pages_params_.reset(); | 1129 print_pages_params_.reset(); |
1202 IPC::SyncMessage* msg = | 1130 IPC::SyncMessage* msg = |
1203 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); | 1131 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); |
1204 msg->EnableMessagePumping(); | 1132 msg->EnableMessagePumping(); |
1205 Send(msg); | 1133 Send(msg); |
1206 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); | 1134 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
1207 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1135 return (print_settings.params.dpi && print_settings.params.document_cookie); |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1568 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1496 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1569 return prep_frame_view_->GetPrintCanvasSize(); | 1497 return prep_frame_view_->GetPrintCanvasSize(); |
1570 } | 1498 } |
1571 | 1499 |
1572 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1500 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1573 prep_frame_view_.reset(); | 1501 prep_frame_view_.reset(); |
1574 metafile_.reset(); | 1502 metafile_.reset(); |
1575 pages_to_render_.clear(); | 1503 pages_to_render_.clear(); |
1576 error_ = PREVIEW_ERROR_NONE; | 1504 error_ = PREVIEW_ERROR_NONE; |
1577 } | 1505 } |
OLD | NEW |