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

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: Updated as per Chris' 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_canvas.h"
30 #include "skia/ext/vector_platform_device_skia.h"
31 #include "third_party/skia/include/core/SkTypeface.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" 37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" 40 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 41 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
34 #include "ui/base/l10n/l10n_util.h" 42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/text/text_elider.h"
35 44
36 #if defined(OS_POSIX) 45 #if defined(OS_POSIX)
37 #include "content/common/view_messages.h" 46 #include "content/common/view_messages.h"
38 #endif 47 #endif
39 48
49 using base::Time;
40 using printing::ConvertPixelsToPoint; 50 using printing::ConvertPixelsToPoint;
41 using printing::ConvertPixelsToPointDouble; 51 using printing::ConvertPixelsToPointDouble;
52 using printing::ConvertPointsToPixelDouble;
42 using printing::ConvertUnit; 53 using printing::ConvertUnit;
43 using printing::ConvertUnitDouble; 54 using printing::ConvertUnitDouble;
44 using WebKit::WebConsoleMessage; 55 using WebKit::WebConsoleMessage;
45 using WebKit::WebDocument; 56 using WebKit::WebDocument;
46 using WebKit::WebElement; 57 using WebKit::WebElement;
47 using WebKit::WebFrame; 58 using WebKit::WebFrame;
48 using WebKit::WebNode; 59 using WebKit::WebNode;
49 using WebKit::WebSize; 60 using WebKit::WebSize;
50 using WebKit::WebString; 61 using WebKit::WebString;
51 using WebKit::WebURLRequest; 62 using WebKit::WebURLRequest;
(...skipping 29 matching lines...) Expand all
81 oldParams.params.min_shrink == newParams.params.min_shrink && 92 oldParams.params.min_shrink == newParams.params.min_shrink &&
82 oldParams.params.dpi == newParams.params.dpi && 93 oldParams.params.dpi == newParams.params.dpi &&
83 oldParams.params.printable_size == newParams.params.printable_size && 94 oldParams.params.printable_size == newParams.params.printable_size &&
84 oldParams.params.selection_only == newParams.params.selection_only && 95 oldParams.params.selection_only == newParams.params.selection_only &&
85 oldParams.params.page_size == newParams.params.page_size && 96 oldParams.params.page_size == newParams.params.page_size &&
86 oldParams.params.margin_top == newParams.params.margin_top && 97 oldParams.params.margin_top == newParams.params.margin_top &&
87 oldParams.params.margin_left == newParams.params.margin_left && 98 oldParams.params.margin_left == newParams.params.margin_left &&
88 oldParams.params.supports_alpha_blend == 99 oldParams.params.supports_alpha_blend ==
89 newParams.params.supports_alpha_blend && 100 newParams.params.supports_alpha_blend &&
90 oldParams.pages.size() == newParams.pages.size() && 101 oldParams.pages.size() == newParams.pages.size() &&
102 oldParams.params.display_header_footer ==
103 newParams.params.display_header_footer &&
91 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 104 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
92 newParams.pages.begin()); 105 newParams.pages.begin());
93 } 106 }
94 107
108 // Splits the horizontal width equally into segments with an interstice
109 // between each segment. Returns the width of this segment.
110 SkScalar GetSegmentWidth(const PageSizeMargins& page_layout) {
111 return (page_layout.margin_left +
112 page_layout.content_width +
vandebo (ex-Chrome) 2011/07/26 06:11:37 nit: indent
Aayush Kumar 2011/07/26 07:37:33 Done.
113 page_layout.margin_right -
114 // Interstice is left at both ends of the page as well as between each
115 // region, so 1 is added.
116 (printing::kSettingHeaderFooterHorizontalRegions + 1) *
117 printing::kSettingHeaderFooterInterstice) /
118 printing::kSettingHeaderFooterHorizontalRegions;
119 }
120
121 // Given a text, the positions, and the paint object, this method gets the
122 // coordinates and prints the text at those coordinates on the canvas.
123 void PrintHeaderFooterText(
124 string16 text,
125 SkPaint paint,
126 skia::VectorCanvas& canvas,
127 printing::HorizontalHeaderFooterPosition horizontal_position,
128 printing::VerticalHeaderFooterPosition vertical_position,
129 float webkit_scale_factor,
130 const PageSizeMargins& page_layout,
131 SkScalar max_text_width,
132 gfx::Font font,
133 SkScalar height_to_baseline) {
134 text = ui::ElideText(text, font, ConvertPointsToPixelDouble(max_text_width),
135 false);
136
137 SkPoint text_location;
138 size_t text_byte_length = text.length() * sizeof(char16);
139 // Get the (x, y) coordinate from where printing of the current text should
140 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
141 // vertical alignment (TOP, BOTTOM).
142 {
vandebo (ex-Chrome) 2011/07/26 06:11:37 Don't use an extra scope.
Aayush Kumar 2011/07/26 07:37:33 Done.
143 SkScalar text_width_in_points = paint.measureText(text.c_str(),
144 text_byte_length);
145
146 SkScalar x = 0;
147 switch (horizontal_position) {
148 case printing::LEFT: {
149 x = -page_layout.margin_left +
150 printing::kSettingHeaderFooterInterstice;
151 break;
152 }
153 case printing::RIGHT: {
154 x = (page_layout.content_width + page_layout.margin_right -
155 (printing::kSettingHeaderFooterInterstice + text_width_in_points));
156 break;
157 }
158 case printing::CENTER: {
159 SkScalar available_width = GetSegmentWidth(page_layout);
160 x = available_width - page_layout.margin_left +
161 (available_width - text_width_in_points) / 2;
162 break;
163 }
164 default: {
165 NOTREACHED();
166 }
167 }
168
169 SkScalar y = 0;
170 switch (vertical_position) {
171 case printing::TOP: {
172 y = -page_layout.margin_top - height_to_baseline +
173 printing::kSettingHeaderFooterInterstice;
174 break;
175 }
176 case printing::BOTTOM: {
177 y = page_layout.margin_bottom + page_layout.content_height -
178 printing::kSettingHeaderFooterInterstice - height_to_baseline;
179 break;
180 }
181 default: {
182 NOTREACHED();
183 }
184 }
185
186 text_location.set(x / webkit_scale_factor, y / webkit_scale_factor);
187 }
188
189 paint.setTextSize(paint.getTextSize() / webkit_scale_factor);
190 canvas.drawText(text.c_str(), text_byte_length, text_location.x(),
191 text_location.y(), paint);
192 }
193
95 } // namespace 194 } // namespace
96 195
196 void PrintHeaderAndFooter(skia::VectorPlatformDeviceSkia* device,
197 skia::VectorCanvas& canvas,
198 int page_number, int total_pages,
199 float webkit_scale_factor,
200 const PageSizeMargins& page_layout,
201 const DictionaryValue* header_footer_info) {
202 // Set the drawing area to draw in the margins.
203 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea);
204
205 // Setting up styles for the headers and footers text.
206 SkPaint paint;
207 paint.setColor(SK_ColorBLACK);
208 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
209 paint.setTextSize(printing::kSettingHeaderFooterFontSize);
210 gfx::Font font(UTF8ToUTF16(printing::kSettingHeaderFooterFontName),
211 ceil(ConvertPointsToPixelDouble(
212 printing::kSettingHeaderFooterFontSize)));
213 paint.setTypeface(SkTypeface::CreateFromName(
214 UTF16ToUTF8(font.GetFontName()).c_str(), SkTypeface::kNormal));
215
216 // Ensure we have enough space to print above and below the page to print
217 // headers.
218 string16 date = base::TimeFormatShortDateNumeric(Time::Now());
219 string16 title;
220 if (!header_footer_info->GetString(printing::kSettingHeaderFooterTitle,
221 &title)) {
222 NOTREACHED();
223 }
224
225 SkRect header_bounds;
226 string16 header_text = date + title;
227 paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16),
228 &header_bounds, 0);
229 SkScalar text_height = printing::kSettingHeaderFooterInterstice +
vandebo (ex-Chrome) 2011/07/26 06:11:37 nit: = <newline>
Aayush Kumar 2011/07/26 07:37:33 Done.
230 header_bounds.height();
231
232 if (text_height > page_layout.margin_top)
233 return;
234
235 // Ensure we have enough space to print above and below the page to print
236 // footers.
237 string16 page_on_page_total = base::IntToString16(page_number) +
vandebo (ex-Chrome) 2011/07/26 06:11:37 page_of_total_pages ?
Aayush Kumar 2011/07/26 07:37:33 Done.
238 UTF8ToUTF16("/") +
239 base::IntToString16(total_pages);
240 std::string url;
241 if (!header_footer_info->GetString(printing::kSettingHeaderFooterURL,
242 &url)) {
243 NOTREACHED();
244 }
245
246 SkRect footer_bounds;
247 string16 footer_text = page_on_page_total + UTF8ToUTF16(url);
248 paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16),
249 &footer_bounds, 0);
250 text_height = printing::kSettingHeaderFooterInterstice +
vandebo (ex-Chrome) 2011/07/26 06:11:37 nit: = <newline>
Aayush Kumar 2011/07/26 07:37:33 Done.
251 footer_bounds.height();
252
253 if (text_height > page_layout.margin_bottom)
254 return;
255
256 SkScalar segment_width = GetSegmentWidth(page_layout);
257 // Print the Date.
258 PrintHeaderFooterText(date, paint, canvas, printing::LEFT, printing::TOP,
259 webkit_scale_factor, page_layout, segment_width,
260 font, header_bounds.top());
261
262 // Print the title.
263 SkScalar date_width = paint.measureText(date.c_str(),
264 date.length() * sizeof(char16));
265 // Calculate the available title width. If the date string is not long
266 // enough, then, we increase the available space we have for the title.
267 // Assumes there is no header text to RIGHT of title.
268 SkScalar max_title_width = std::min(2 * segment_width,
269 2 * (segment_width - date_width) +
270 segment_width);
271 PrintHeaderFooterText(title, paint, canvas, printing::CENTER,
272 printing::TOP, webkit_scale_factor, page_layout,
273 max_title_width, font, header_bounds.top());
274
275 // Print the page numbers at the bottom right corner of page.
276 PrintHeaderFooterText(page_on_page_total, paint, canvas, printing::RIGHT,
277 printing::BOTTOM, webkit_scale_factor,
278 page_layout, segment_width, font,
279 footer_bounds.bottom());
280
281 // Print the URL.
282 GURL gurl(url);
283 SkScalar page_width = paint.measureText(
284 page_on_page_total.c_str(),
285 page_on_page_total.length() * sizeof(char16));
286
287 // Calculate the available URL width. We increase the available URL width
288 // if the |page_on_page_total| string isn't long enough.
289 // Assumes no footer text being printed in the CENTER.
290 SkScalar max_url_width = 3 * (page_layout.content_width +
vandebo (ex-Chrome) 2011/07/26 06:11:37 nit: newline foo = (foo + bar + baz) * 3
Aayush Kumar 2011/07/26 07:37:33 Done.
291 page_layout.margin_left + page_layout.margin_right) / 4;
292 max_url_width = std::min(max_url_width,
293 2 * (segment_width - page_width) + segment_width);
294 string16 url_elided = ui::ElideUrl(gurl, font,
295 ConvertPointsToPixelDouble(max_url_width),
296 std::string());
297 PrintHeaderFooterText(url_elided, paint, canvas, printing::LEFT,
298 printing::BOTTOM, webkit_scale_factor,
299 page_layout, max_url_width, font,
300 footer_bounds.bottom());
301
302 // Restore the drawing area to draw in the content area.
303 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea);
304 }
305
97 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 306 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
98 const PrintMsg_Print_Params& print_params, 307 const PrintMsg_Print_Params& print_params,
99 WebFrame* frame, 308 WebFrame* frame,
100 WebNode* node) 309 WebNode* node)
101 : frame_(frame), 310 : frame_(frame),
102 web_view_(frame->view()), 311 web_view_(frame->view()),
103 expected_pages_count_(0), 312 expected_pages_count_(0),
104 use_browser_overlays_(true), 313 use_browser_overlays_(true),
105 finished_(false) { 314 finished_(false) {
106 int dpi = GetDPI(&print_params); 315 int dpi = GetDPI(&print_params);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 } 358 }
150 } 359 }
151 360
152 PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) 361 PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view)
153 : RenderViewObserver(render_view), 362 : RenderViewObserver(render_view),
154 RenderViewObserverTracker<PrintWebViewHelper>(render_view), 363 RenderViewObserverTracker<PrintWebViewHelper>(render_view),
155 print_web_view_(NULL), 364 print_web_view_(NULL),
156 user_cancelled_scripted_print_count_(0), 365 user_cancelled_scripted_print_count_(0),
157 notify_browser_of_print_failure_(true) { 366 notify_browser_of_print_failure_(true) {
158 is_preview_ = switches::IsPrintPreviewEnabled(); 367 is_preview_ = switches::IsPrintPreviewEnabled();
368 header_footer_info_ = new DictionaryValue();
vandebo (ex-Chrome) 2011/07/26 06:11:37 put in init list
Aayush Kumar 2011/07/26 07:37:33 Done.
159 } 369 }
160 370
161 PrintWebViewHelper::~PrintWebViewHelper() {} 371 PrintWebViewHelper::~PrintWebViewHelper() {}
162 372
163 // Prints |frame| which called window.print(). 373 // Prints |frame| which called window.print().
164 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { 374 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) {
165 DCHECK(frame); 375 DCHECK(frame);
166 376
167 // Allow Prerendering to cancel this print request if necessary. 377 // Allow Prerendering to cancel this print request if necessary.
168 if (prerender::PrerenderHelper::IsPrerendering(render_view())) { 378 if (prerender::PrerenderHelper::IsPrerendering(render_view())) {
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 settings.params.page_size = gfx::Size(2550, 3300); 937 settings.params.page_size = gfx::Size(2550, 3300);
728 settings.params.printable_size = gfx::Size(2250, 3000); 938 settings.params.printable_size = gfx::Size(2250, 3000);
729 settings.params.margin_top = 150; 939 settings.params.margin_top = 150;
730 settings.params.margin_left = 150; 940 settings.params.margin_left = 150;
731 settings.params.dpi = 300.0; 941 settings.params.dpi = 300.0;
732 settings.params.min_shrink = 1.25; 942 settings.params.min_shrink = 1.25;
733 settings.params.max_shrink = 2.0; 943 settings.params.max_shrink = 2.0;
734 settings.params.desired_dpi = 72; 944 settings.params.desired_dpi = 72;
735 settings.params.selection_only = false; 945 settings.params.selection_only = false;
736 settings.params.supports_alpha_blend = false; 946 settings.params.supports_alpha_blend = false;
947 settings.params.display_header_footer = false;
737 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) 948 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params)))
738 return false; 949 return false;
739 // TODO(abodenha@chromium.org) Parse page ranges from the job_settings. 950 // TODO(abodenha@chromium.org) Parse page ranges from the job_settings.
740 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 951 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
741 return true; 952 return true;
742 } 953 }
743 954
744 bool PrintWebViewHelper::UpdatePrintSettingsLocal( 955 bool PrintWebViewHelper::UpdatePrintSettingsLocal(
745 const DictionaryValue& job_settings) { 956 const DictionaryValue& job_settings) {
746 PrintMsg_PrintPages_Params settings; 957 PrintMsg_PrintPages_Params settings;
747 958
748 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 959 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
749 print_pages_params_->params.document_cookie, job_settings, &settings)); 960 print_pages_params_->params.document_cookie, job_settings, &settings));
750 961
751 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) 962 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie)
752 return false; 963 return false;
753 964
754 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) 965 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params)))
755 return false; 966 return false;
756 967
757 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 968 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
969 // Getting Header and Footer settings.
vandebo (ex-Chrome) 2011/07/26 06:11:37 Should this go into UpdatePrintSettings?
Aayush Kumar 2011/07/26 07:37:33 I just rebased and it seems that UpdatePrintSettin
970 bool display_header_footer;
971 if (!job_settings.GetBoolean(printing::kSettingHeaderFooter,
972 &display_header_footer)) {
973 NOTREACHED();
974 }
975 print_pages_params_->params.display_header_footer = display_header_footer;
976 std::string url;
977 if (!job_settings.GetString(printing::kSettingHeaderFooterURL, &url))
978 NOTREACHED();
979 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, url);
980 string16 title;
981 if (!job_settings.GetString(printing::kSettingHeaderFooterTitle, &title))
982 NOTREACHED();
983 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, title);
984
758 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 985 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
759 settings.params.document_cookie)); 986 settings.params.document_cookie));
760 return true; 987 return true;
761 } 988 }
762 989
763 bool PrintWebViewHelper::UpdatePrintSettings( 990 bool PrintWebViewHelper::UpdatePrintSettings(
764 const DictionaryValue& job_settings) { 991 const DictionaryValue& job_settings) {
765 if (job_settings.HasKey(printing::kSettingCloudPrintId)) { 992 if (job_settings.HasKey(printing::kSettingCloudPrintId)) {
766 return UpdatePrintSettingsCloud(job_settings); 993 return UpdatePrintSettingsCloud(job_settings);
767 } else { 994 } else {
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 const gfx::Size& 1296 const gfx::Size&
1070 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1297 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1071 return prep_frame_view_->GetPrintCanvasSize(); 1298 return prep_frame_view_->GetPrintCanvasSize();
1072 } 1299 }
1073 1300
1074 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1301 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1075 prep_frame_view_.reset(); 1302 prep_frame_view_.reset();
1076 metafile_.reset(); 1303 metafile_.reset();
1077 rendered_pages_.clear(); 1304 rendered_pages_.clear();
1078 } 1305 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698