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

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

Issue 7348010: Added Header and Footer support using Skia (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: style changes as per demetrios comments Created 9 years, 5 months 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 <algorithm>
8 #include <cmath>
7 #include <string> 9 #include <string>
8 10
9 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/i18n/time_formatting.h"
10 #include "base/logging.h" 13 #include "base/logging.h"
11 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
12 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/string_number_conversions.h"
17 #include "base/time.h"
13 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
14 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/print_messages.h" 20 #include "chrome/common/print_messages.h"
16 #include "chrome/common/render_messages.h" 21 #include "chrome/common/render_messages.h"
17 #include "chrome/common/url_constants.h" 22 #include "chrome/common/url_constants.h"
18 #include "chrome/renderer/prerender/prerender_helper.h" 23 #include "chrome/renderer/prerender/prerender_helper.h"
19 #include "content/renderer/render_view.h" 24 #include "content/renderer/render_view.h"
20 #include "grit/generated_resources.h" 25 #include "grit/generated_resources.h"
21 #include "printing/metafile_impl.h" 26 #include "printing/metafile_impl.h"
22 #include "printing/print_job_constants.h" 27 #include "printing/print_job_constants.h"
23 #include "printing/units.h" 28 #include "printing/units.h"
29 #include "skia/ext/vector_platform_device_skia.h"
30 #include "third_party/skia/include/core/SkTypeface.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" 37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 40 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
34 #include "ui/base/l10n/l10n_util.h" 41 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/base/text/text_elider.h"
35 43
36 #if defined(OS_POSIX) 44 #if defined(OS_POSIX)
37 #include "content/common/view_messages.h" 45 #include "content/common/view_messages.h"
38 #endif 46 #endif
39 47
48 using base::Time;
40 using printing::ConvertPixelsToPoint; 49 using printing::ConvertPixelsToPoint;
41 using printing::ConvertPixelsToPointDouble; 50 using printing::ConvertPixelsToPointDouble;
51 using printing::ConvertPointsToPixelDouble;
42 using printing::ConvertUnit; 52 using printing::ConvertUnit;
43 using printing::ConvertUnitDouble; 53 using printing::ConvertUnitDouble;
44 using WebKit::WebConsoleMessage; 54 using WebKit::WebConsoleMessage;
45 using WebKit::WebDocument; 55 using WebKit::WebDocument;
46 using WebKit::WebElement; 56 using WebKit::WebElement;
47 using WebKit::WebFrame; 57 using WebKit::WebFrame;
48 using WebKit::WebNode; 58 using WebKit::WebNode;
49 using WebKit::WebSize; 59 using WebKit::WebSize;
50 using WebKit::WebString; 60 using WebKit::WebString;
51 using WebKit::WebURLRequest; 61 using WebKit::WebURLRequest;
(...skipping 29 matching lines...) Expand all
81 oldParams.params.min_shrink == newParams.params.min_shrink && 91 oldParams.params.min_shrink == newParams.params.min_shrink &&
82 oldParams.params.dpi == newParams.params.dpi && 92 oldParams.params.dpi == newParams.params.dpi &&
83 oldParams.params.printable_size == newParams.params.printable_size && 93 oldParams.params.printable_size == newParams.params.printable_size &&
84 oldParams.params.selection_only == newParams.params.selection_only && 94 oldParams.params.selection_only == newParams.params.selection_only &&
85 oldParams.params.page_size == newParams.params.page_size && 95 oldParams.params.page_size == newParams.params.page_size &&
86 oldParams.params.margin_top == newParams.params.margin_top && 96 oldParams.params.margin_top == newParams.params.margin_top &&
87 oldParams.params.margin_left == newParams.params.margin_left && 97 oldParams.params.margin_left == newParams.params.margin_left &&
88 oldParams.params.supports_alpha_blend == 98 oldParams.params.supports_alpha_blend ==
89 newParams.params.supports_alpha_blend && 99 newParams.params.supports_alpha_blend &&
90 oldParams.pages.size() == newParams.pages.size() && 100 oldParams.pages.size() == newParams.pages.size() &&
101 oldParams.params.display_header_footer ==
102 newParams.params.display_header_footer &&
91 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 103 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
92 newParams.pages.begin()); 104 newParams.pages.begin());
93 } 105 }
94 106
107 // Gets the byte length of the |text|.
108 size_t GetString16ByteLength(string16 text) {
vandebo (ex-Chrome) 2011/07/22 22:58:33 I don't think this needs to be a function, go ahea
Aayush Kumar 2011/07/24 02:09:02 Done.
109 return text.length() * sizeof(char16);
110 }
111
112 // Splits the horizontal width equally into three segments with an interstice
113 // between each segment. Returns the width of this segment.
114 SkScalar GetSegmentWidth(PageSizeMargins& page_size_margins) {
115 return (page_size_margins.margin_left +
116 page_size_margins.content_width +
117 page_size_margins.margin_right -
118 (printing::kSettingHeaderFooterHorizontalRegions + 1) *
119 printing::kSettingHeaderFooterInterstice) /
120 printing::kSettingHeaderFooterHorizontalRegions;
121 }
122
123 // Given a SkPaint object, gets the maximum possible height of any glyph.
124 SkScalar GetMaxTextHeight(SkPaint paint) {
125 SkPaint::FontMetrics metrics;
126 paint.getFontMetrics(&metrics);
127 return metrics.fBottom - metrics.fTop;
128 }
129
130 // Gets the (x, y) coordinate from where we want to start printing the current
131 // text depending on the alignment horizontal alignment (LEFT, RIGHT, CENTER)
132 // and vertical alignment (TOP, BOTTOM).
133 SkPoint GetHeaderFooterPosition(
134 string16 text,
135 SkPaint paint,
136 printing::HorizontalHeaderFooterPosition hor_pos,
137 printing::VerticalHeaderFooterPosition ver_pos,
138 PageSizeMargins& page_size_margins) {
139
140 SkScalar text_width_in_points = paint.measureText(
141 text.c_str(), GetString16ByteLength(text));
142
143 SkScalar x = 0;
144 switch (hor_pos) {
145 case printing::LEFT: {
146 x = page_size_margins.margin_left * (-1) +
147 printing::kSettingHeaderFooterInterstice;
148 break;
149 }
150 case printing::RIGHT: {
151 x = ((page_size_margins.content_width + page_size_margins.margin_right) -
152 (printing::kSettingHeaderFooterInterstice + text_width_in_points));
153 break;
154 }
155 case printing::CENTER: {
156 SkScalar available_width = GetSegmentWidth(page_size_margins);
157 x = (available_width - page_size_margins.margin_left +
158 (available_width - text_width_in_points) / 2);
159 break;
160 }
161 default: {
162 NOTREACHED();
163 }
164 }
165
166 SkScalar y = 0;
167 SkPaint::FontMetrics metrics;
168 paint.getFontMetrics(&metrics);
169 switch (ver_pos) {
170 case printing::TOP: {
171 y = page_size_margins.margin_top * (-1) +
172 printing::kSettingHeaderFooterInterstice - metrics.fTop;
173 break;
174 }
175 case printing::BOTTOM: {
176 y = page_size_margins.margin_bottom + page_size_margins.content_height -
177 printing::kSettingHeaderFooterInterstice - metrics.fBottom;
178 break;
179 }
180 default: {
181 NOTREACHED();
182 }
183 }
184
185 SkPoint point;
186 point.set(x, y);
187 return point;
188 }
189
190 // Given a text, the positions and the paint object, this method gets the
191 // coordinates and prints the text at those co-ordinates on the canvas.
192 void PrintHeaderFooterText(
193 string16 text,
194 SkPaint paint,
195 const SkRefPtr<skia::VectorCanvas>& canvas,
196 printing::HorizontalHeaderFooterPosition hor_pos,
197 printing::VerticalHeaderFooterPosition ver_pos,
198 float webkit_scale_factor,
199 PageSizeMargins& page_size_margins) {
200 SkPoint point = GetHeaderFooterPosition(text, paint, hor_pos, ver_pos,
vandebo (ex-Chrome) 2011/07/22 22:58:33 Maybe just inline getheaderfooterposition here, si
Aayush Kumar 2011/07/24 02:09:02 Done.
201 page_size_margins);
202 point.set(point.x() / webkit_scale_factor, point.y() / webkit_scale_factor);
203 size_t byte_length = GetString16ByteLength(text);
204
205 canvas->drawText(text.c_str(), byte_length, point.x(), point.y(), paint);
206 }
207
95 } // namespace 208 } // namespace
96 209
210 void PrintHeaderAndFooter(skia::VectorPlatformDeviceSkia* device,
211 const SkRefPtr<skia::VectorCanvas>& canvas,
212 int page_number, int total_pages,
213 float webkit_scale_factor,
214 PageSizeMargins& page_size_margins,
215 const DictionaryValue* header_footer_info) {
216 // Set the drawing area to draw in the margins.
217 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea);
218
219 // Setting up styles for the headers and footers text.
220 SkPaint paint;
221 paint.setColor(SK_ColorBLACK);
222 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
223
224 SkScalar font_size = printing::kSettingHeaderFooterFontSize /
225 webkit_scale_factor;
226 paint.setTextSize(font_size);
227 gfx::Font font(UTF8ToUTF16(printing::kSettingHeaderFooterFontName),
228 ceil(ConvertPointsToPixelDouble(font_size)));
229 paint.setTypeface(SkTypeface::CreateFromName(
230 UTF16ToUTF8(font.GetFontName()).c_str(),
231 SkTypeface::kNormal));
232
233 // Ensuring we have enough space to print above and below the page to print
234 // headers and footers.
235 SkScalar text_height = printing::kSettingHeaderFooterInterstice +
236 GetMaxTextHeight(paint);
vandebo (ex-Chrome) 2011/07/22 22:58:33 Why not measure the actual text height here, inste
Aayush Kumar 2011/07/24 02:09:02 Done.
237 if (text_height > page_size_margins.margin_top ||
238 text_height > page_size_margins.margin_bottom) {
239 return;
240 }
241
242 SkScalar segment_width = GetSegmentWidth(page_size_margins);
243 // Printing the Date.
244 string16 date = base::TimeFormatShortDateNumeric(Time::Now());
245 date = date;
246 date = ui::ElideText(date, font, ConvertPointsToPixelDouble(segment_width),
vandebo (ex-Chrome) 2011/07/22 22:58:33 Can you pull the ElideText into the PrintHeaderFoo
Aayush Kumar 2011/07/24 02:09:02 Done.
247 false);
248 PrintHeaderFooterText(date, paint, canvas, printing::LEFT, printing::TOP,
249 webkit_scale_factor, page_size_margins);
250
251 // Printing the title.
252 string16 title;
253 if (!header_footer_info->GetString(printing::kSettingHeaderFooterTitle,
254 &title)) {
255 NOTREACHED();
256 }
257
258 SkScalar date_width = paint.measureText(date.c_str(),
259 GetString16ByteLength(date));
260 // Calculating the available title width. If the date string is not long
261 // enough, then, we increase the available space we have for the title.
262 // Assumes there is no header text to RIGHT of title.
263 SkScalar max_title_width = std::min(2 * segment_width,
264 2 * (segment_width - date_width) +
265 segment_width);
266 title = ui::ElideText(title, font,
267 ConvertPointsToPixelDouble(max_title_width), false);
268 PrintHeaderFooterText(title, paint, canvas, printing::CENTER,
269 printing::TOP, webkit_scale_factor,
270 page_size_margins);
271
272 // Printing the page numbers at the bottom right corner of page.
273 string16 page_on_page_total = base::IntToString16(page_number) +
274 UTF8ToUTF16("/") +
275 base::IntToString16(total_pages);
276 page_on_page_total = ui::ElideText(page_on_page_total, font,
277 ConvertPointsToPixelDouble(segment_width),
278 false);
279 PrintHeaderFooterText(page_on_page_total, paint, canvas, printing::RIGHT,
280 printing::BOTTOM, webkit_scale_factor,
281 page_size_margins);
282
283 // Printing the URL.
284 std::string url;
285 if (!header_footer_info->GetString(printing::kSettingHeaderFooterURL,
286 &url)) {
287 NOTREACHED();
288 }
289 GURL gurl(url);
290 SkScalar page_width = paint.measureText(
291 page_on_page_total.c_str(),
292 GetString16ByteLength(page_on_page_total));
293
294 // Calculating the available URL width. We increase the available URL width
295 // if the |page_on_page_total| string isn't long enough.
296 // Assumes no footer text being printed in the CENTER.
297 SkScalar max_url_width = 3 * (page_size_margins.content_width +
298 page_size_margins.margin_left + page_size_margins.margin_right) / 4;
299 max_url_width = std::min(max_url_width,
300 2 * (segment_width - page_width) + segment_width);
301 string16 url_elided = ui::ElideUrl(gurl, font,
302 ConvertPointsToPixelDouble(max_url_width),
303 std::string());
304 PrintHeaderFooterText(url_elided, paint, canvas, printing::LEFT,
305 printing::BOTTOM, webkit_scale_factor,
306 page_size_margins);
307
308 // Restore the drawing area to draw in the content area.
309 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea);
310 }
311
97 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 312 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
98 const PrintMsg_Print_Params& print_params, 313 const PrintMsg_Print_Params& print_params,
99 WebFrame* frame, 314 WebFrame* frame,
100 WebNode* node) 315 WebNode* node)
101 : frame_(frame), 316 : frame_(frame),
102 web_view_(frame->view()), 317 web_view_(frame->view()),
103 expected_pages_count_(0), 318 expected_pages_count_(0),
104 use_browser_overlays_(true), 319 use_browser_overlays_(true),
105 finished_(false) { 320 finished_(false) {
106 int dpi = GetDPI(&print_params); 321 int dpi = GetDPI(&print_params);
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 render_view()->webview()->mainFrame(); 469 render_view()->webview()->mainFrame();
255 return true; 470 return true;
256 } 471 }
257 472
258 void PrintWebViewHelper::OnPrintPages() { 473 void PrintWebViewHelper::OnPrintPages() {
259 WebFrame* frame; 474 WebFrame* frame;
260 if (GetPrintFrame(&frame)) 475 if (GetPrintFrame(&frame))
261 Print(frame, NULL); 476 Print(frame, NULL);
262 } 477 }
263 478
264 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 479 void PrintWebViewHelper::OnPrintPreview(
480 const DictionaryValue& settings,
481 const DictionaryValue& header_footer_info) {
265 DCHECK(is_preview_); 482 DCHECK(is_preview_);
266 print_preview_context_.OnPrintPreview(); 483 print_preview_context_.OnPrintPreview();
484 header_footer_info_ = header_footer_info.DeepCopy();
267 485
268 if (!InitPrintSettings(print_preview_context_.frame(), 486 if (!InitPrintSettings(print_preview_context_.frame(),
269 print_preview_context_.node())) { 487 print_preview_context_.node())) {
270 NOTREACHED(); 488 NOTREACHED();
271 return; 489 return;
272 } 490 }
273 491
274 if (!UpdatePrintSettings(settings)) { 492 if (!UpdatePrintSettings(settings)) {
275 DidFinishPrinting(FAIL_PREVIEW); 493 DidFinishPrinting(FAIL_PREVIEW);
276 return; 494 return;
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 return true; 763 return true;
546 } 764 }
547 #endif // OS_MACOSX || OS_WIN 765 #endif // OS_MACOSX || OS_WIN
548 766
549 void PrintWebViewHelper::didStopLoading() { 767 void PrintWebViewHelper::didStopLoading() {
550 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); 768 PrintMsg_PrintPages_Params* params = print_pages_params_.get();
551 DCHECK(params != NULL); 769 DCHECK(params != NULL);
552 PrintPages(*params, print_web_view_->mainFrame(), NULL); 770 PrintPages(*params, print_web_view_->mainFrame(), NULL);
553 } 771 }
554 772
555 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( 773 PageSizeMargins PrintWebViewHelper::GetPageSizeAndMarginsInPoints(
556 WebFrame* frame, 774 WebFrame* frame,
557 int page_index, 775 int page_index,
558 const PrintMsg_Print_Params& default_params, 776 const PrintMsg_Print_Params& default_params) {
559 double* content_width_in_points,
560 double* content_height_in_points,
561 double* margin_top_in_points,
562 double* margin_right_in_points,
563 double* margin_bottom_in_points,
564 double* margin_left_in_points) {
565 int dpi = GetDPI(&default_params); 777 int dpi = GetDPI(&default_params);
566 778
567 WebSize page_size_in_pixels( 779 WebSize page_size_in_pixels(
568 ConvertUnit(default_params.page_size.width(), 780 ConvertUnit(default_params.page_size.width(),
569 dpi, printing::kPixelsPerInch), 781 dpi, printing::kPixelsPerInch),
570 ConvertUnit(default_params.page_size.height(), 782 ConvertUnit(default_params.page_size.height(),
571 dpi, printing::kPixelsPerInch)); 783 dpi, printing::kPixelsPerInch));
572 int margin_top_in_pixels = ConvertUnit( 784 int margin_top_in_pixels = ConvertUnit(
573 default_params.margin_top, 785 default_params.margin_top,
574 dpi, printing::kPixelsPerInch); 786 dpi, printing::kPixelsPerInch);
(...skipping 11 matching lines...) Expand all
586 798
587 if (frame) { 799 if (frame) {
588 frame->pageSizeAndMarginsInPixels(page_index, 800 frame->pageSizeAndMarginsInPixels(page_index,
589 page_size_in_pixels, 801 page_size_in_pixels,
590 margin_top_in_pixels, 802 margin_top_in_pixels,
591 margin_right_in_pixels, 803 margin_right_in_pixels,
592 margin_bottom_in_pixels, 804 margin_bottom_in_pixels,
593 margin_left_in_pixels); 805 margin_left_in_pixels);
594 } 806 }
595 807
596 *content_width_in_points = ConvertPixelsToPoint(page_size_in_pixels.width - 808 PageSizeMargins page_size_margins_in_points;
597 margin_left_in_pixels - 809 page_size_margins_in_points.content_width = ConvertPixelsToPoint(
598 margin_right_in_pixels); 810 page_size_in_pixels.width - margin_left_in_pixels -
599 *content_height_in_points = ConvertPixelsToPoint(page_size_in_pixels.height - 811 margin_right_in_pixels);
600 margin_top_in_pixels - 812 page_size_margins_in_points.content_height = ConvertPixelsToPoint(
601 margin_bottom_in_pixels); 813 page_size_in_pixels.height - margin_top_in_pixels -
814 margin_bottom_in_pixels);
602 815
603 // Invalid page size and/or margins. We just use the default setting. 816 // Invalid page size and/or margins. We just use the default setting.
604 if (*content_width_in_points < 1.0 || *content_height_in_points < 1.0) { 817 if (page_size_margins_in_points.content_width < 1.0 ||
605 GetPageSizeAndMarginsInPoints(NULL, 818 page_size_margins_in_points.content_height < 1.0) {
606 page_index, 819 return GetPageSizeAndMarginsInPoints(NULL, page_index, default_params);
607 default_params,
608 content_width_in_points,
609 content_height_in_points,
610 margin_top_in_points,
611 margin_right_in_points,
612 margin_bottom_in_points,
613 margin_left_in_points);
614 return;
615 } 820 }
616 821
617 if (margin_top_in_points) 822 page_size_margins_in_points.margin_top =
618 *margin_top_in_points = 823 ConvertPixelsToPointDouble(margin_top_in_pixels);
619 ConvertPixelsToPointDouble(margin_top_in_pixels); 824 page_size_margins_in_points.margin_right =
620 if (margin_right_in_points) 825 ConvertPixelsToPointDouble(margin_right_in_pixels);
621 *margin_right_in_points = 826 page_size_margins_in_points.margin_bottom =
622 ConvertPixelsToPointDouble(margin_right_in_pixels); 827 ConvertPixelsToPointDouble(margin_bottom_in_pixels);
623 if (margin_bottom_in_points) 828 page_size_margins_in_points.margin_left =
624 *margin_bottom_in_points = 829 ConvertPixelsToPointDouble(margin_left_in_pixels);
625 ConvertPixelsToPointDouble(margin_bottom_in_pixels); 830 return page_size_margins_in_points;
626 if (margin_left_in_points)
627 *margin_left_in_points =
628 ConvertPixelsToPointDouble(margin_left_in_pixels);
629 } 831 }
630 832
631 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( 833 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters(
632 WebFrame* frame, 834 WebFrame* frame,
633 WebNode* node, 835 WebNode* node,
634 PrintMsg_Print_Params* params) { 836 PrintMsg_Print_Params* params) {
635 double content_width_in_points;
636 double content_height_in_points;
637 double margin_top_in_points;
638 double margin_right_in_points;
639 double margin_bottom_in_points;
640 double margin_left_in_points;
641 PrepareFrameAndViewForPrint prepare(*params, frame, node); 837 PrepareFrameAndViewForPrint prepare(*params, frame, node);
642 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, 838 PageSizeMargins page_size_margins_in_points =
643 &content_width_in_points, &content_height_in_points, 839 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params);
644 &margin_top_in_points, &margin_right_in_points,
645 &margin_bottom_in_points, &margin_left_in_points);
646 int dpi = GetDPI(params); 840 int dpi = GetDPI(params);
647 params->printable_size = gfx::Size( 841 params->printable_size = gfx::Size(
648 static_cast<int>(ConvertUnitDouble(content_width_in_points, 842 static_cast<int>(ConvertUnitDouble(
843 page_size_margins_in_points.content_width,
649 printing::kPointsPerInch, dpi)), 844 printing::kPointsPerInch, dpi)),
650 static_cast<int>(ConvertUnitDouble(content_height_in_points, 845 static_cast<int>(ConvertUnitDouble(
846 page_size_margins_in_points.content_height,
651 printing::kPointsPerInch, dpi))); 847 printing::kPointsPerInch, dpi)));
652 848
653 double page_width_in_points = content_width_in_points + 849 double page_width_in_points = page_size_margins_in_points.content_width +
654 margin_left_in_points + margin_right_in_points; 850 page_size_margins_in_points.margin_left +
655 double page_height_in_points = content_height_in_points + 851 page_size_margins_in_points.margin_right;
656 margin_top_in_points + margin_bottom_in_points; 852 double page_height_in_points = page_size_margins_in_points.content_height +
853 page_size_margins_in_points.margin_top +
854 page_size_margins_in_points.margin_bottom;
657 855
658 params->page_size = gfx::Size( 856 params->page_size = gfx::Size(
659 static_cast<int>(ConvertUnitDouble( 857 static_cast<int>(ConvertUnitDouble(
660 page_width_in_points, printing::kPointsPerInch, dpi)), 858 page_width_in_points, printing::kPointsPerInch, dpi)),
661 static_cast<int>(ConvertUnitDouble( 859 static_cast<int>(ConvertUnitDouble(
662 page_height_in_points, printing::kPointsPerInch, dpi))); 860 page_height_in_points, printing::kPointsPerInch, dpi)));
663 861
664 params->margin_top = static_cast<int>(ConvertUnitDouble( 862 params->margin_top = static_cast<int>(ConvertUnitDouble(
665 margin_top_in_points, printing::kPointsPerInch, dpi)); 863 page_size_margins_in_points.margin_top, printing::kPointsPerInch, dpi));
666 params->margin_left = static_cast<int>(ConvertUnitDouble( 864 params->margin_left = static_cast<int>(ConvertUnitDouble(
667 margin_left_in_points, printing::kPointsPerInch, dpi)); 865 page_size_margins_in_points.margin_left, printing::kPointsPerInch, dpi));
668 } 866 }
669 867
670 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 868 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame,
671 WebKit::WebNode* node) { 869 WebKit::WebNode* node) {
672 DCHECK(frame); 870 DCHECK(frame);
673 PrintMsg_PrintPages_Params settings; 871 PrintMsg_PrintPages_Params settings;
674 872
675 // TODO(abodenha@chromium.org) It doesn't make sense to do this if our 873 // TODO(abodenha@chromium.org) It doesn't make sense to do this if our
676 // "default" is a cloud based printer. Split InitPrintSettings up 874 // "default" is a cloud based printer. Split InitPrintSettings up
677 // so that we can avoid the overhead of unneeded calls into the native 875 // so that we can avoid the overhead of unneeded calls into the native
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 settings.params.margin_top = 150; 911 settings.params.margin_top = 150;
714 settings.params.margin_left = 150; 912 settings.params.margin_left = 150;
715 settings.params.dpi = 300.0; 913 settings.params.dpi = 300.0;
716 settings.params.min_shrink = 1.25; 914 settings.params.min_shrink = 1.25;
717 settings.params.max_shrink = 2.0; 915 settings.params.max_shrink = 2.0;
718 settings.params.desired_dpi = 72; 916 settings.params.desired_dpi = 72;
719 settings.params.selection_only = false; 917 settings.params.selection_only = false;
720 settings.params.supports_alpha_blend = false; 918 settings.params.supports_alpha_blend = false;
721 // TODO(abodenha@chromium.org) Parse page ranges from the job_settings. 919 // TODO(abodenha@chromium.org) Parse page ranges from the job_settings.
722 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 920 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
921 print_pages_params_->params.display_header_footer = false;
723 return true; 922 return true;
724 } 923 }
725 924
726 bool PrintWebViewHelper::UpdatePrintSettingsLocal( 925 bool PrintWebViewHelper::UpdatePrintSettingsLocal(
727 const DictionaryValue& job_settings) { 926 const DictionaryValue& job_settings) {
728 PrintMsg_PrintPages_Params settings; 927 PrintMsg_PrintPages_Params settings;
729 928
730 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 929 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
731 print_pages_params_->params.document_cookie, job_settings, &settings)); 930 print_pages_params_->params.document_cookie, job_settings, &settings));
732 931
733 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) 932 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie)
734 return false; 933 return false;
735 934
736 if (!job_settings.GetInteger(printing::kPreviewRequestID, 935 if (!job_settings.GetInteger(printing::kPreviewRequestID,
737 &settings.params.preview_request_id)) { 936 &settings.params.preview_request_id)) {
738 NOTREACHED(); 937 NOTREACHED();
739 return false; 938 return false;
740 } 939 }
741 940
742 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 941 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
942 bool header_footer;
943 if (!job_settings.GetBoolean(printing::kSettingHeaderFooter, &header_footer))
944 NOTREACHED();
945 print_pages_params_->params.display_header_footer = header_footer;
946
743 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 947 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
744 settings.params.document_cookie)); 948 settings.params.document_cookie));
745 return true; 949 return true;
746 } 950 }
747 951
748 bool PrintWebViewHelper::UpdatePrintSettings( 952 bool PrintWebViewHelper::UpdatePrintSettings(
749 const DictionaryValue& job_settings) { 953 const DictionaryValue& job_settings) {
750 if (job_settings.HasKey(printing::kSettingCloudPrintId)) { 954 if (job_settings.HasKey(printing::kSettingCloudPrintId)) {
751 return UpdatePrintSettingsCloud(job_settings); 955 return UpdatePrintSettingsCloud(job_settings);
752 } else { 956 } else {
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 const gfx::Size& 1258 const gfx::Size&
1055 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1259 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1056 return prep_frame_view_->GetPrintCanvasSize(); 1260 return prep_frame_view_->GetPrintCanvasSize();
1057 } 1261 }
1058 1262
1059 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1263 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1060 prep_frame_view_.reset(); 1264 prep_frame_view_.reset();
1061 metafile_.reset(); 1265 metafile_.reset();
1062 rendered_pages_.clear(); 1266 rendered_pages_.clear();
1063 } 1267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698