Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: chrome/renderer/print_web_view_helper.cc

Issue 8585017: PrintPreview: Honor the print media page size and margin values. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix conflicts Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 return !params.document_cookie && !params.desired_dpi && !params.max_shrink && 102 return !params.document_cookie && !params.desired_dpi && !params.max_shrink &&
103 !params.min_shrink && !params.dpi && params.content_size.IsEmpty() && 103 !params.min_shrink && !params.dpi && params.content_size.IsEmpty() &&
104 !params.selection_only && params.page_size.IsEmpty() && 104 !params.selection_only && params.page_size.IsEmpty() &&
105 !params.margin_top && !params.margin_left && 105 !params.margin_top && !params.margin_left &&
106 !params.supports_alpha_blend; 106 !params.supports_alpha_blend;
107 } 107 }
108 108
109 bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams, 109 bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams,
110 const PrintMsg_PrintPages_Params& newParams) { 110 const PrintMsg_PrintPages_Params& newParams) {
111 return oldParams.params.content_size == newParams.params.content_size && 111 return oldParams.params.content_size == newParams.params.content_size &&
112 oldParams.params.printable_area == newParams.params.printable_area &&
112 oldParams.params.page_size == newParams.params.page_size && 113 oldParams.params.page_size == newParams.params.page_size &&
113 oldParams.params.margin_top == newParams.params.margin_top && 114 oldParams.params.margin_top == newParams.params.margin_top &&
114 oldParams.params.margin_left == newParams.params.margin_left && 115 oldParams.params.margin_left == newParams.params.margin_left &&
115 oldParams.params.desired_dpi == newParams.params.desired_dpi && 116 oldParams.params.desired_dpi == newParams.params.desired_dpi &&
116 oldParams.params.dpi == newParams.params.dpi; 117 oldParams.params.dpi == newParams.params.dpi;
117 } 118 }
118 119
119 bool PrintMsg_Print_Params_IsEqual( 120 bool PrintMsg_Print_Params_IsEqual(
120 const PrintMsg_PrintPages_Params& oldParams, 121 const PrintMsg_PrintPages_Params& oldParams,
121 const PrintMsg_PrintPages_Params& newParams) { 122 const PrintMsg_PrintPages_Params& newParams) {
122 return PageLayoutIsEqual(oldParams, newParams) && 123 return PageLayoutIsEqual(oldParams, newParams) &&
123 oldParams.params.max_shrink == newParams.params.max_shrink && 124 oldParams.params.max_shrink == newParams.params.max_shrink &&
124 oldParams.params.min_shrink == newParams.params.min_shrink && 125 oldParams.params.min_shrink == newParams.params.min_shrink &&
125 oldParams.params.selection_only == newParams.params.selection_only && 126 oldParams.params.selection_only == newParams.params.selection_only &&
126 oldParams.params.supports_alpha_blend == 127 oldParams.params.supports_alpha_blend ==
127 newParams.params.supports_alpha_blend && 128 newParams.params.supports_alpha_blend &&
128 oldParams.pages.size() == newParams.pages.size() && 129 oldParams.pages.size() == newParams.pages.size() &&
130 oldParams.params.print_to_pdf == newParams.params.print_to_pdf &&
129 oldParams.params.display_header_footer == 131 oldParams.params.display_header_footer ==
130 newParams.params.display_header_footer && 132 newParams.params.display_header_footer &&
131 oldParams.params.date == newParams.params.date && 133 oldParams.params.date == newParams.params.date &&
132 oldParams.params.title == newParams.params.title && 134 oldParams.params.title == newParams.params.title &&
133 oldParams.params.url == newParams.params.url && 135 oldParams.params.url == newParams.params.url &&
134 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 136 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
135 newParams.pages.begin()); 137 newParams.pages.begin());
136 } 138 }
137 139
140 // Helper method to populate page layout information.
141 // Set |ignore_css_margins| to true to ignore print media margin css.
142 // If the page does not have any print media css, printer default values
143 // are used.
144 void PopulatePageLayoutInfo(
145 WebFrame* frame,
146 int page_index,
147 const PrintMsg_Print_Params& default_params,
148 bool ignore_css_margins,
vandebo (ex-Chrome) 2011/12/14 19:05:38 I was hoping to separate the fit/ignore logic from
kmadhusu 2011/12/19 09:52:19 Done.
149 bool fit_to_page,
150 PageSizeMargins* page_layout_in_points) {
151 int dpi = GetDPI(&default_params);
152
153 WebSize page_size_in_pixels(
154 ConvertUnit(default_params.page_size.width(),
155 dpi, printing::kPixelsPerInch),
156 ConvertUnit(default_params.page_size.height(),
157 dpi, printing::kPixelsPerInch));
158 int margin_top_in_pixels = ConvertUnit(
159 default_params.margin_top,
160 dpi, printing::kPixelsPerInch);
161 int margin_right_in_pixels = ConvertUnit(
162 default_params.page_size.width() -
163 default_params.content_size.width() - default_params.margin_left,
164 dpi, printing::kPixelsPerInch);
165 int margin_bottom_in_pixels = ConvertUnit(
166 default_params.page_size.height() -
167 default_params.content_size.height() - default_params.margin_top,
168 dpi, printing::kPixelsPerInch);
169 int margin_left_in_pixels = ConvertUnit(
170 default_params.margin_left,
171 dpi, printing::kPixelsPerInch);
172
173 if (frame && !(ignore_css_margins && fit_to_page)) {
174 int old_margin_top_in_pixels = margin_top_in_pixels;
175 int old_margin_bottom_in_pixels = margin_bottom_in_pixels;
176 int old_margin_left_in_pixels = margin_left_in_pixels;
177 int old_margin_right_in_pixels = margin_right_in_pixels;
178
179 frame->pageSizeAndMarginsInPixels(page_index,
180 page_size_in_pixels,
181 margin_top_in_pixels,
182 margin_right_in_pixels,
183 margin_bottom_in_pixels,
184 margin_left_in_pixels);
185 if (ignore_css_margins) {
186 margin_top_in_pixels = old_margin_top_in_pixels;
187 margin_bottom_in_pixels = old_margin_bottom_in_pixels;
188 margin_left_in_pixels = old_margin_left_in_pixels;
189 margin_right_in_pixels = old_margin_right_in_pixels;
190 }
191 }
192 double margin_top_in_points =
193 ConvertPixelsToPointDouble(margin_top_in_pixels);
194 double margin_right_in_points =
195 ConvertPixelsToPointDouble(margin_right_in_pixels);
196 double margin_left_in_points =
197 ConvertPixelsToPointDouble(margin_left_in_pixels);
198 double margin_bottom_in_points =
199 ConvertPixelsToPointDouble(margin_bottom_in_pixels);
200 double new_content_width =
201 ConvertPixelsToPoint(page_size_in_pixels.width);
202 double new_content_height =
203 ConvertPixelsToPoint(page_size_in_pixels.height);
204
205 bool page_has_custom_page_size_style =
206 frame && frame->hasCustomPageSizeStyle(page_index);
207
208 // Do not subtract the margins from |page_size_in_pixels| when the page has
209 // custom page size style and the user has requested a custom margins.
210 if ((!ignore_css_margins && page_has_custom_page_size_style) ||
211 (ignore_css_margins && page_has_custom_page_size_style && !fit_to_page)) {
212 new_content_width -= (margin_left_in_points + margin_right_in_points);
213 new_content_height -= (margin_top_in_points + margin_bottom_in_points);
214 } else {
215 double page_width_in_points =
216 ConvertUnit(default_params.page_size.width(),
217 dpi, printing::kPointsPerInch);
218 double page_height_in_points =
219 ConvertUnit(default_params.page_size.height(),
220 dpi, printing::kPointsPerInch);
221 new_content_width = page_width_in_points - margin_left_in_points -
222 margin_right_in_points;
223 new_content_height = page_height_in_points - margin_top_in_points -
224 margin_bottom_in_points;
225 }
226
227 // Invalid page size and/or margins. We just use the default setting.
228 if (new_content_width < 1.0 || new_content_height < 1.0) {
229 CHECK(frame != NULL);
230 PopulatePageLayoutInfo(NULL, page_index, default_params, false,
231 false, page_layout_in_points);
232 return;
233 }
234
235 page_layout_in_points->content_width = new_content_width;
236 page_layout_in_points->content_height = new_content_height;
237 page_layout_in_points->margin_top = margin_top_in_points;
238 page_layout_in_points->margin_right = margin_right_in_points;
239 page_layout_in_points->margin_bottom = margin_bottom_in_points;
240 page_layout_in_points->margin_left = margin_left_in_points;
241 }
242
243 // Helper method to fit |css_page_layout_in_points| to default paper size.
244 void FitCssPageSizeAndMarginsToPaperSize(
245 const PrintMsg_Print_Params& default_params,
246 const PageSizeMargins& css_page_layout_in_points,
vandebo (ex-Chrome) 2011/12/14 19:05:38 As is, I think you can pass a single PageSizeMargi
kmadhusu 2011/12/19 09:52:19 Done.
247 double* scale_factor,
248 PageSizeMargins* page_layout_in_points) {
249 int dpi = GetDPI(&default_params);
250 double margin_top_in_points = css_page_layout_in_points.margin_top;
251 double margin_bottom_in_points = css_page_layout_in_points.margin_bottom;
252 double margin_left_in_points = css_page_layout_in_points.margin_left;
253 double margin_right_in_points = css_page_layout_in_points.margin_right;
254 double content_width = css_page_layout_in_points.content_width;
255 double content_height = css_page_layout_in_points.content_height;
256
257 double new_page_box_width = content_width + margin_left_in_points +
258 margin_right_in_points;
259 double new_page_box_height = content_height + margin_top_in_points +
260 margin_bottom_in_points;
261 double default_page_size_height =
262 ConvertUnit(default_params.page_size.height(), dpi,
263 printing::kPointsPerInch);
264 double default_page_size_width =
265 ConvertUnit(default_params.page_size.width(), dpi,
266 printing::kPointsPerInch);
267
268 if (default_page_size_height != new_page_box_height ||
269 default_page_size_width != new_page_box_width) {
270 double factor = 1.0f;
271 if (default_page_size_width < new_page_box_width ||
272 default_page_size_height < new_page_box_height) {
273 double minimum_printable_width =
274 ConvertUnit(default_params.printable_area.width(), dpi,
275 printing::kPointsPerInch);
276 double minimum_printable_height =
277 ConvertUnit(default_params.printable_area.height(), dpi,
278 printing::kPointsPerInch);
279 double ratio_width = minimum_printable_width / new_page_box_width;
280 double ratio_height = minimum_printable_height / new_page_box_height;
281 factor = ratio_width < ratio_height ? ratio_width : ratio_height;
282 if (scale_factor)
283 *scale_factor = factor;
284
285 content_width *= factor;
286 content_height *= factor;
287 }
288 margin_top_in_points =
289 (default_page_size_height - new_page_box_height * factor)/2 +
290 (margin_top_in_points * factor);
291 margin_bottom_in_points =
292 (default_page_size_height - new_page_box_height * factor)/2 +
293 (margin_bottom_in_points * factor);
294 margin_right_in_points =
295 (default_page_size_width - new_page_box_width * factor)/2 +
296 (margin_right_in_points * factor);
297 margin_left_in_points =
298 (default_page_size_width - new_page_box_width * factor)/2 +
299 (margin_left_in_points * factor);
300 page_layout_in_points->content_width = content_width;
301 page_layout_in_points->content_height = content_height;
302 page_layout_in_points->margin_top = margin_top_in_points;
303 page_layout_in_points->margin_right = margin_right_in_points;
304 page_layout_in_points->margin_bottom = margin_bottom_in_points;
305 page_layout_in_points->margin_left = margin_left_in_points;
306 }
307 }
308
138 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 309 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
139 gfx::Size* result) { 310 gfx::Size* result) {
140 int dpi = GetDPI(&print_params); 311 int dpi = GetDPI(&print_params);
141 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, 312 result->set_width(ConvertUnit(print_params.content_size.width(), dpi,
142 print_params.desired_dpi)); 313 print_params.desired_dpi));
143 314
144 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, 315 result->set_height(ConvertUnit(print_params.content_size.height(), dpi,
145 print_params.desired_dpi)); 316 print_params.desired_dpi));
146 } 317 }
147 318
148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 319 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
149 if (!node.isNull()) 320 if (!node.isNull())
150 return true; 321 return true;
151 std::string mime(frame->dataSource()->response().mimeType().utf8()); 322 std::string mime(frame->dataSource()->response().mimeType().utf8());
152 return mime == "application/pdf"; 323 return mime == "application/pdf";
153 } 324 }
154 325
326 bool PrintingFrameHasPageSizeStyle(WebFrame* frame) {
327 if (!frame)
328 return false;
329 return frame->hasCustomPageSizeStyle(0);
330 }
331
155 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { 332 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) {
156 if (frame->isPrintScalingDisabledForPlugin(node)) 333 if (frame->isPrintScalingDisabledForPlugin(node))
157 return printing::NO_MARGINS; 334 return printing::NO_MARGINS;
158 else 335 else
159 return printing::PRINTABLE_AREA_MARGINS; 336 return printing::PRINTABLE_AREA_MARGINS;
160 } 337 }
161 338
162 // Get the (x, y) coordinate from where printing of the current text should 339 // Get the (x, y) coordinate from where printing of the current text should
163 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and 340 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
164 // vertical alignment (TOP, BOTTOM). 341 // vertical alignment (TOP, BOTTOM).
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 web_frame->setScrollOffset(prev_scroll_offset_); 610 web_frame->setScrollOffset(prev_scroll_offset_);
434 } 611 }
435 } 612 }
436 613
437 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 614 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
438 : content::RenderViewObserver(render_view), 615 : content::RenderViewObserver(render_view),
439 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 616 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
440 print_web_view_(NULL), 617 print_web_view_(NULL),
441 is_preview_enabled_(switches::IsPrintPreviewEnabled()), 618 is_preview_enabled_(switches::IsPrintPreviewEnabled()),
442 is_print_ready_metafile_sent_(false), 619 is_print_ready_metafile_sent_(false),
620 ignore_css_margins_(false),
621 fit_to_page_(true),
443 user_cancelled_scripted_print_count_(0), 622 user_cancelled_scripted_print_count_(0),
444 notify_browser_of_print_failure_(true) { 623 notify_browser_of_print_failure_(true) {
445 } 624 }
446 625
447 PrintWebViewHelper::~PrintWebViewHelper() {} 626 PrintWebViewHelper::~PrintWebViewHelper() {}
448 627
449 // Prints |frame| which called window.print(). 628 // Prints |frame| which called window.print().
450 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { 629 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) {
451 DCHECK(frame); 630 DCHECK(frame);
452 631
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 void PrintWebViewHelper::OnPrintForSystemDialog() { 732 void PrintWebViewHelper::OnPrintForSystemDialog() {
554 WebFrame* frame = print_preview_context_.frame(); 733 WebFrame* frame = print_preview_context_.frame();
555 if (!frame) { 734 if (!frame) {
556 NOTREACHED(); 735 NOTREACHED();
557 return; 736 return;
558 } 737 }
559 738
560 Print(frame, print_preview_context_.node()); 739 Print(frame, print_preview_context_.node());
561 } 740 }
562 741
742 void PrintWebViewHelper::UpdatePageSizeAndContentAreaFromPageLayout(
743 const printing::PageSizeMargins& page_layout_in_points,
744 gfx::Size* page_size,
745 gfx::Rect* content_area) {
746 *page_size = gfx::Size(
747 page_layout_in_points.content_width +
748 page_layout_in_points.margin_right +
749 page_layout_in_points.margin_left,
750 page_layout_in_points.content_height +
751 page_layout_in_points.margin_top +
752 page_layout_in_points.margin_bottom);
753 *content_area = gfx::Rect(page_layout_in_points.margin_left,
754 page_layout_in_points.margin_top,
755 page_layout_in_points.content_width,
756 page_layout_in_points.content_height);
757 }
758
759 void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
760 const DictionaryValue& settings) {
761 int margins_type = 0;
762 if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type))
763 margins_type = printing::DEFAULT_MARGINS;
764 ignore_css_margins_ = margins_type != printing::DEFAULT_MARGINS;
765 }
766
767 bool PrintWebViewHelper::IsPrintToPdfRequested(
768 const DictionaryValue& job_settings) {
769 bool print_to_pdf = false;
770 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf))
771 NOTREACHED();
772 return print_to_pdf;
773 }
774
563 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 775 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
564 DCHECK(is_preview_enabled_); 776 DCHECK(is_preview_enabled_);
565 print_preview_context_.OnPrintPreview(); 777 print_preview_context_.OnPrintPreview();
566 778
567 if (!UpdatePrintSettings(print_preview_context_.frame(), 779 if (!UpdatePrintSettings(print_preview_context_.frame(),
568 print_preview_context_.node(), settings, false)) { 780 print_preview_context_.node(), settings, false)) {
569 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 781 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
570 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 782 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
571 routing_id(), print_pages_params_->params.document_cookie)); 783 routing_id(), print_pages_params_->params.document_cookie));
572 notify_browser_of_print_failure_ = false; // Already sent. 784 notify_browser_of_print_failure_ = false; // Already sent.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } else { 825 } else {
614 if (notify_browser_of_print_failure_) 826 if (notify_browser_of_print_failure_)
615 LOG(ERROR) << "CreatePreviewDocument failed"; 827 LOG(ERROR) << "CreatePreviewDocument failed";
616 DidFinishPrinting(FAIL_PREVIEW); 828 DidFinishPrinting(FAIL_PREVIEW);
617 } 829 }
618 } 830 }
619 831
620 bool PrintWebViewHelper::CreatePreviewDocument() { 832 bool PrintWebViewHelper::CreatePreviewDocument() {
621 PrintMsg_Print_Params print_params = print_pages_params_->params; 833 PrintMsg_Print_Params print_params = print_pages_params_->params;
622 const std::vector<int>& pages = print_pages_params_->pages; 834 const std::vector<int>& pages = print_pages_params_->pages;
623 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) 835 if (!print_preview_context_.CreatePreviewDocument(
836 &print_params, pages, ignore_css_margins_,
837 fit_to_page_)) {
624 return false; 838 return false;
839 }
840
841 PageSizeMargins default_page_layout;
842 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), 0,
843 print_params, ignore_css_margins_, fit_to_page_, NULL,
844 &default_page_layout);
845 if (!old_print_pages_params_.get() ||
846 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) {
847 bool has_page_size_style = PrintingFrameHasPageSizeStyle(
848 print_preview_context_.frame());
849 // Margins: Send default page layout to browser process.
850 Send(new PrintHostMsg_DidGetDefaultPageLayout(
851 routing_id(), default_page_layout, has_page_size_style));
852 }
853
625 PrintHostMsg_DidGetPreviewPageCount_Params params; 854 PrintHostMsg_DidGetPreviewPageCount_Params params;
626 params.page_count = print_preview_context_.total_page_count(); 855 params.page_count = print_preview_context_.total_page_count();
627 params.is_modifiable = print_preview_context_.IsModifiable(); 856 params.is_modifiable = print_preview_context_.IsModifiable();
628 params.document_cookie = print_pages_params_->params.document_cookie; 857 params.document_cookie = print_pages_params_->params.document_cookie;
629 params.preview_request_id = print_pages_params_->params.preview_request_id; 858 params.preview_request_id = print_pages_params_->params.preview_request_id;
630 params.clear_preview_data = print_preview_context_.generate_draft_pages(); 859 params.clear_preview_data = print_preview_context_.generate_draft_pages();
631 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); 860 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params));
632 if (CheckForCancel()) 861 if (CheckForCancel())
633 return false; 862 return false;
634 863
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 1063
835 return true; 1064 return true;
836 } 1065 }
837 1066
838 #if defined(OS_MACOSX) || defined(OS_WIN) 1067 #if defined(OS_MACOSX) || defined(OS_WIN)
839 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, 1068 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
840 WebFrame* frame, 1069 WebFrame* frame,
841 const WebNode& node) { 1070 const WebNode& node) {
842 PrintMsg_Print_Params print_params = params.params; 1071 PrintMsg_Print_Params print_params = params.params;
843 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); 1072 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node);
844 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, 1073 UpdatePageSizeAndMarginsInFrameAndView(frame, node, &prep_frame_view,
845 &print_params); 1074 print_params, ignore_css_margins_,
1075 fit_to_page_);
846 1076
847 int page_count = prep_frame_view.GetExpectedPageCount(); 1077 int page_count = prep_frame_view.GetExpectedPageCount();
848 if (!page_count) 1078 if (!page_count)
849 return false; 1079 return false;
850 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), 1080 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(),
851 print_params.document_cookie, 1081 print_params.document_cookie,
852 page_count)); 1082 page_count));
853 1083
854 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); 1084 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize();
855 PrintMsg_PrintPage_Params page_params; 1085 PrintMsg_PrintPage_Params page_params;
(...skipping 19 matching lines...) Expand all
875 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); 1105 PrintMsg_PrintPages_Params* params = print_pages_params_.get();
876 DCHECK(params != NULL); 1106 DCHECK(params != NULL);
877 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); 1107 PrintPages(*params, print_web_view_->mainFrame(), WebNode());
878 } 1108 }
879 1109
880 // static - Not anonymous so that platform implementations can use it. 1110 // static - Not anonymous so that platform implementations can use it.
881 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( 1111 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints(
882 WebFrame* frame, 1112 WebFrame* frame,
883 int page_index, 1113 int page_index,
884 const PrintMsg_Print_Params& default_params, 1114 const PrintMsg_Print_Params& default_params,
1115 bool ignore_css_margins,
1116 bool fit_to_page,
1117 double* scale_factor,
885 PageSizeMargins* page_layout_in_points) { 1118 PageSizeMargins* page_layout_in_points) {
886 int dpi = GetDPI(&default_params); 1119 PageSizeMargins css_page_layout_in_points;
1120 PopulatePageLayoutInfo(frame, page_index, default_params,
1121 ignore_css_margins, fit_to_page, &css_page_layout_in_points);
1122 *page_layout_in_points = css_page_layout_in_points;
887 1123
888 WebSize page_size_in_pixels( 1124 if (fit_to_page) {
889 ConvertUnit(default_params.page_size.width(), 1125 FitCssPageSizeAndMarginsToPaperSize(default_params,
890 dpi, printing::kPixelsPerInch), 1126 css_page_layout_in_points, scale_factor, page_layout_in_points);
891 ConvertUnit(default_params.page_size.height(),
892 dpi, printing::kPixelsPerInch));
893 int margin_top_in_pixels = ConvertUnit(
894 default_params.margin_top,
895 dpi, printing::kPixelsPerInch);
896 int margin_right_in_pixels = ConvertUnit(
897 default_params.page_size.width() -
898 default_params.content_size.width() - default_params.margin_left,
899 dpi, printing::kPixelsPerInch);
900 int margin_bottom_in_pixels = ConvertUnit(
901 default_params.page_size.height() -
902 default_params.content_size.height() - default_params.margin_top,
903 dpi, printing::kPixelsPerInch);
904 int margin_left_in_pixels = ConvertUnit(
905 default_params.margin_left,
906 dpi, printing::kPixelsPerInch);
907
908 if (frame) {
909 frame->pageSizeAndMarginsInPixels(page_index,
910 page_size_in_pixels,
911 margin_top_in_pixels,
912 margin_right_in_pixels,
913 margin_bottom_in_pixels,
914 margin_left_in_pixels);
915 } 1127 }
916
917 page_layout_in_points->content_width =
918 ConvertPixelsToPoint(page_size_in_pixels.width -
919 margin_left_in_pixels -
920 margin_right_in_pixels);
921 page_layout_in_points->content_height =
922 ConvertPixelsToPoint(page_size_in_pixels.height -
923 margin_top_in_pixels -
924 margin_bottom_in_pixels);
925
926 // Invalid page size and/or margins. We just use the default setting.
927 if (page_layout_in_points->content_width < 1.0 ||
928 page_layout_in_points->content_height < 1.0) {
929 CHECK(frame != NULL);
930 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params,
931 page_layout_in_points);
932 return;
933 }
934
935 page_layout_in_points->margin_top =
936 ConvertPixelsToPointDouble(margin_top_in_pixels);
937 page_layout_in_points->margin_right =
938 ConvertPixelsToPointDouble(margin_right_in_pixels);
939 page_layout_in_points->margin_bottom =
940 ConvertPixelsToPointDouble(margin_bottom_in_pixels);
941 page_layout_in_points->margin_left =
942 ConvertPixelsToPointDouble(margin_left_in_pixels);
943 } 1128 }
944 1129
945 // static - Not anonymous so that platform implementations can use it. 1130 // static - Not anonymous so that platform implementations can use it.
946 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( 1131 void PrintWebViewHelper::UpdatePageSizeAndMarginsInFrameAndView(
947 WebFrame* frame, 1132 WebFrame* frame,
948 const WebNode& node, 1133 const WebNode& node,
949 PrepareFrameAndViewForPrint* prepare, 1134 PrepareFrameAndViewForPrint* prepare,
950 PrintMsg_Print_Params* params) { 1135 const PrintMsg_Print_Params& params,
1136 bool ignore_css_margins,
1137 bool fit_to_page) {
951 if (PrintingNodeOrPdfFrame(frame, node)) 1138 if (PrintingNodeOrPdfFrame(frame, node))
952 return; 1139 return;
953 PageSizeMargins page_layout_in_points; 1140 PageSizeMargins page_layout_in_points;
954 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, 1141 PrintMsg_Print_Params current_params = params;
955 &page_layout_in_points); 1142 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0,
956 int dpi = GetDPI(params); 1143 current_params, ignore_css_margins,
957 params->content_size = gfx::Size( 1144 ignore_css_margins && fit_to_page, NULL,
1145 &page_layout_in_points);
1146 int dpi = GetDPI(&current_params);
1147 current_params.content_size = gfx::Size(
958 static_cast<int>(ConvertUnitDouble( 1148 static_cast<int>(ConvertUnitDouble(
959 page_layout_in_points.content_width, 1149 page_layout_in_points.content_width,
960 printing::kPointsPerInch, dpi)), 1150 printing::kPointsPerInch, dpi)),
961 static_cast<int>(ConvertUnitDouble( 1151 static_cast<int>(ConvertUnitDouble(
962 page_layout_in_points.content_height, 1152 page_layout_in_points.content_height,
963 printing::kPointsPerInch, dpi))); 1153 printing::kPointsPerInch, dpi)));
964
965 double page_width_in_points = 1154 double page_width_in_points =
966 page_layout_in_points.content_width + 1155 page_layout_in_points.content_width +
967 page_layout_in_points.margin_left + 1156 page_layout_in_points.margin_left +
968 page_layout_in_points.margin_right; 1157 page_layout_in_points.margin_right;
969 double page_height_in_points = 1158 double page_height_in_points =
970 page_layout_in_points.content_height + 1159 page_layout_in_points.content_height +
971 page_layout_in_points.margin_top + 1160 page_layout_in_points.margin_top +
972 page_layout_in_points.margin_bottom; 1161 page_layout_in_points.margin_bottom;
973 1162
974 params->page_size = gfx::Size( 1163 current_params.page_size = gfx::Size(
975 static_cast<int>(ConvertUnitDouble( 1164 static_cast<int>(ConvertUnitDouble(
976 page_width_in_points, printing::kPointsPerInch, dpi)), 1165 page_width_in_points, printing::kPointsPerInch, dpi)),
977 static_cast<int>(ConvertUnitDouble( 1166 static_cast<int>(ConvertUnitDouble(
978 page_height_in_points, printing::kPointsPerInch, dpi))); 1167 page_height_in_points, printing::kPointsPerInch, dpi)));
979 1168 current_params.margin_top =
980 params->margin_top = static_cast<int>(ConvertUnitDouble( 1169 static_cast<int>(ConvertUnitDouble(
981 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); 1170 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi));
982 params->margin_left = static_cast<int>(ConvertUnitDouble( 1171 current_params.margin_left = static_cast<int>(ConvertUnitDouble(
983 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); 1172 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi));
984 1173 prepare->UpdatePrintParams(current_params);
985 prepare->UpdatePrintParams(*params);
986 } 1174 }
987 1175
988 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 1176 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame,
989 const WebKit::WebNode& node) { 1177 const WebKit::WebNode& node) {
990 DCHECK(frame); 1178 DCHECK(frame);
991 PrintMsg_PrintPages_Params settings; 1179 PrintMsg_PrintPages_Params settings;
992 1180
993 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1181 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
994 &settings.params)); 1182 &settings.params));
995 // Check if the printer returned any settings, if the settings is empty, we 1183 // Check if the printer returned any settings, if the settings is empty, we
(...skipping 22 matching lines...) Expand all
1018 1206
1019 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1207 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1020 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1208 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1021 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1209 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1022 if (!InitPrintSettings(frame, node)) 1210 if (!InitPrintSettings(frame, node))
1023 return false; 1211 return false;
1024 1212
1025 DCHECK(!prepare->get()); 1213 DCHECK(!prepare->get());
1026 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1214 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1027 frame, node)); 1215 frame, node));
1028 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1216 UpdatePageSizeAndMarginsInFrameAndView(frame, node, prepare->get(),
1029 &print_pages_params_->params); 1217 print_pages_params_->params,
1218 ignore_css_margins_,
vandebo (ex-Chrome) 2011/12/14 19:05:38 These two will always be false here, just use fals
kmadhusu 2011/12/19 09:52:19 ignore_css_margins_ is false and fit_to_page_ is t
1219 fit_to_page_);
1030 Send(new PrintHostMsg_DidGetDocumentCookie( 1220 Send(new PrintHostMsg_DidGetDocumentCookie(
1031 routing_id(), print_pages_params_->params.document_cookie)); 1221 routing_id(), print_pages_params_->params.document_cookie));
1032 return true; 1222 return true;
1033 } 1223 }
1034 1224
1035 bool PrintWebViewHelper::UpdatePrintSettings( 1225 bool PrintWebViewHelper::UpdatePrintSettings(
1036 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1226 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1037 const DictionaryValue& passed_job_settings, bool print_for_preview) { 1227 const DictionaryValue& passed_job_settings, bool print_for_preview) {
1038 DCHECK(is_preview_enabled_); 1228 DCHECK(is_preview_enabled_);
1039 const DictionaryValue* job_settings = &passed_job_settings; 1229 const DictionaryValue* job_settings = &passed_job_settings;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 &(settings.params.preview_ui_addr)) || 1310 &(settings.params.preview_ui_addr)) ||
1121 !job_settings->GetInteger(printing::kPreviewRequestID, 1311 !job_settings->GetInteger(printing::kPreviewRequestID,
1122 &(settings.params.preview_request_id)) || 1312 &(settings.params.preview_request_id)) ||
1123 !job_settings->GetBoolean(printing::kIsFirstRequest, 1313 !job_settings->GetBoolean(printing::kIsFirstRequest,
1124 &(settings.params.is_first_request))) { 1314 &(settings.params.is_first_request))) {
1125 NOTREACHED(); 1315 NOTREACHED();
1126 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1316 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1127 return false; 1317 return false;
1128 } 1318 }
1129 1319
1130 // Margins: Send default page layout to browser process. 1320 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1131 PageSizeMargins default_page_layout; 1321 UpdateFrameMarginsCssInfo(*job_settings);
1132 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, 1322 fit_to_page_ = source_is_html && !IsPrintToPdfRequested(*job_settings);
1133 &default_page_layout);
1134 if (!old_print_pages_params_.get() ||
1135 !PageLayoutIsEqual(*old_print_pages_params_, settings)) {
1136 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
1137 default_page_layout));
1138 }
1139 1323
1140 // Header/Footer: Set |header_footer_info_|. 1324 // Header/Footer: Set |header_footer_info_|.
1141 if (settings.params.display_header_footer) { 1325 if (settings.params.display_header_footer) {
1142 header_footer_info_.reset(new DictionaryValue()); 1326 header_footer_info_.reset(new DictionaryValue());
1143 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1327 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1144 settings.params.date); 1328 settings.params.date);
1145 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1329 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1146 settings.params.url); 1330 settings.params.url);
1147 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1331 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1148 settings.params.title); 1332 settings.params.title);
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 node_ = web_node; 1550 node_ = web_node;
1367 } 1551 }
1368 1552
1369 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { 1553 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
1370 DCHECK_EQ(INITIALIZED, state_); 1554 DCHECK_EQ(INITIALIZED, state_);
1371 ClearContext(); 1555 ClearContext();
1372 } 1556 }
1373 1557
1374 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( 1558 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
1375 PrintMsg_Print_Params* print_params, 1559 PrintMsg_Print_Params* print_params,
1376 const std::vector<int>& pages) { 1560 const std::vector<int>& pages,
1561 bool ignore_css_margins,
1562 bool fit_to_page) {
1377 DCHECK_EQ(INITIALIZED, state_); 1563 DCHECK_EQ(INITIALIZED, state_);
1378 state_ = RENDERING; 1564 state_ = RENDERING;
1379 1565
1380 metafile_.reset(new printing::PreviewMetafile); 1566 metafile_.reset(new printing::PreviewMetafile);
1381 if (!metafile_->Init()) { 1567 if (!metafile_->Init()) {
1382 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); 1568 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED);
1383 LOG(ERROR) << "PreviewMetafile Init failed"; 1569 LOG(ERROR) << "PreviewMetafile Init failed";
1384 return false; 1570 return false;
1385 } 1571 }
1386 1572
1387 // Need to make sure old object gets destroyed first. 1573 // Need to make sure old object gets destroyed first.
1388 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), 1574 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(),
1389 node())); 1575 node()));
1390 UpdatePrintableSizeInPrintParameters(frame_, node_, 1576 UpdatePageSizeAndMarginsInFrameAndView(frame_, node_,
1391 prep_frame_view_.get(), print_params); 1577 prep_frame_view_.get(), *print_params,
1578 ignore_css_margins,
1579 fit_to_page);
1392 1580
1393 print_params_.reset(new PrintMsg_Print_Params(*print_params)); 1581 print_params_.reset(new PrintMsg_Print_Params(*print_params));
1394 1582
1395 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); 1583 total_page_count_ = prep_frame_view_->GetExpectedPageCount();
1396 if (total_page_count_ == 0) { 1584 if (total_page_count_ == 0) {
1397 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; 1585 LOG(ERROR) << "CreatePreviewDocument got 0 page count";
1398 set_error(PREVIEW_ERROR_ZERO_PAGES); 1586 set_error(PREVIEW_ERROR_ZERO_PAGES);
1399 return false; 1587 return false;
1400 } 1588 }
1401 1589
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 DCHECK(IsRendering()); 1745 DCHECK(IsRendering());
1558 return prep_frame_view_->GetPrintCanvasSize(); 1746 return prep_frame_view_->GetPrintCanvasSize();
1559 } 1747 }
1560 1748
1561 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1749 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1562 prep_frame_view_.reset(); 1750 prep_frame_view_.reset();
1563 metafile_.reset(); 1751 metafile_.reset();
1564 pages_to_render_.clear(); 1752 pages_to_render_.clear();
1565 error_ = PREVIEW_ERROR_NONE; 1753 error_ = PREVIEW_ERROR_NONE;
1566 } 1754 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698