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

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: Moved elide text to print_settings_initializer Created 9 years, 4 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 #if defined(USE_SKIA)
8 #include <algorithm>
9 #include <cmath>
10 #endif
7 #include <string> 11 #include <string>
8 12
9 #include "base/command_line.h" 13 #include "base/command_line.h"
10 #include "base/logging.h" 14 #include "base/logging.h"
11 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
12 #include "base/process_util.h" 16 #include "base/process_util.h"
13 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
14 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/print_messages.h" 19 #include "chrome/common/print_messages.h"
16 #include "chrome/common/render_messages.h" 20 #include "chrome/common/render_messages.h"
(...skipping 12 matching lines...) Expand all
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
33 #include "ui/base/l10n/l10n_util.h" 37 #include "ui/base/l10n/l10n_util.h"
34 38
35 #if defined(OS_POSIX) 39 #if defined(OS_POSIX)
36 #include "content/common/view_messages.h" 40 #include "content/common/view_messages.h"
37 #endif 41 #endif
38 42
43 #if defined(USE_SKIA)
44 #include "base/string_number_conversions.h"
45 #include "skia/ext/vector_canvas.h"
46 #include "skia/ext/vector_platform_device_skia.h"
47 #include "third_party/skia/include/core/SkTypeface.h"
48 #endif // USE_SKIA
49
50 using base::Time;
39 using printing::ConvertPixelsToPoint; 51 using printing::ConvertPixelsToPoint;
40 using printing::ConvertPixelsToPointDouble; 52 using printing::ConvertPixelsToPointDouble;
41 using printing::ConvertUnit; 53 using printing::ConvertUnit;
42 using printing::ConvertUnitDouble; 54 using printing::ConvertUnitDouble;
55 using printing::GetSegmentWidth;
43 using WebKit::WebConsoleMessage; 56 using WebKit::WebConsoleMessage;
44 using WebKit::WebDocument; 57 using WebKit::WebDocument;
45 using WebKit::WebElement; 58 using WebKit::WebElement;
46 using WebKit::WebFrame; 59 using WebKit::WebFrame;
47 using WebKit::WebNode; 60 using WebKit::WebNode;
48 using WebKit::WebSize; 61 using WebKit::WebSize;
49 using WebKit::WebString; 62 using WebKit::WebString;
50 using WebKit::WebURLRequest; 63 using WebKit::WebURLRequest;
51 using WebKit::WebView; 64 using WebKit::WebView;
52 65
(...skipping 27 matching lines...) Expand all
80 oldParams.params.min_shrink == newParams.params.min_shrink && 93 oldParams.params.min_shrink == newParams.params.min_shrink &&
81 oldParams.params.dpi == newParams.params.dpi && 94 oldParams.params.dpi == newParams.params.dpi &&
82 oldParams.params.printable_size == newParams.params.printable_size && 95 oldParams.params.printable_size == newParams.params.printable_size &&
83 oldParams.params.selection_only == newParams.params.selection_only && 96 oldParams.params.selection_only == newParams.params.selection_only &&
84 oldParams.params.page_size == newParams.params.page_size && 97 oldParams.params.page_size == newParams.params.page_size &&
85 oldParams.params.margin_top == newParams.params.margin_top && 98 oldParams.params.margin_top == newParams.params.margin_top &&
86 oldParams.params.margin_left == newParams.params.margin_left && 99 oldParams.params.margin_left == newParams.params.margin_left &&
87 oldParams.params.supports_alpha_blend == 100 oldParams.params.supports_alpha_blend ==
88 newParams.params.supports_alpha_blend && 101 newParams.params.supports_alpha_blend &&
89 oldParams.pages.size() == newParams.pages.size() && 102 oldParams.pages.size() == newParams.pages.size() &&
103 oldParams.params.display_header_footer ==
104 newParams.params.display_header_footer &&
kmadhusu 2011/08/12 17:07:00 Also check for individual values.
Aayush Kumar 2011/08/12 18:53:42 Is there a likely scenario when this would change
kmadhusu 2011/08/12 20:27:11 User can change the system date & time in between
90 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 105 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
91 newParams.pages.begin()); 106 newParams.pages.begin());
92 } 107 }
93 108
94 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 109 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
95 gfx::Size* result) { 110 gfx::Size* result) {
96 int dpi = GetDPI(&print_params); 111 int dpi = GetDPI(&print_params);
97 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, 112 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi,
98 print_params.desired_dpi)); 113 print_params.desired_dpi));
99 114
100 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, 115 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi,
101 print_params.desired_dpi)); 116 print_params.desired_dpi));
102 } 117 }
103 118
119 #if defined(USE_SKIA)
120 // Given a text, the positions, and the paint object, this method gets the
121 // coordinates and prints the text at those coordinates on the canvas.
122 void PrintHeaderFooterText(
123 string16 text,
124 skia::VectorCanvas* canvas,
125 SkPaint paint,
126 float webkit_scale_factor,
127 const PageSizeMargins& page_layout,
128 printing::HorizontalHeaderFooterPosition horizontal_position,
129 printing::VerticalHeaderFooterPosition vertical_position,
130 SkScalar offset_to_baseline) {
131 size_t text_byte_length = text.length() * sizeof(char16);
132 // Get the (x, y) coordinate from where printing of the current text should
133 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
134 // vertical alignment (TOP, BOTTOM).
135 SkScalar text_width_in_points = paint.measureText(text.c_str(),
136 text_byte_length);
137 SkScalar x = 0;
138 switch (horizontal_position) {
139 case printing::LEFT:
140 x = printing::kSettingHeaderFooterInterstice - page_layout.margin_left;
141 break;
142 case printing::RIGHT:
143 x = page_layout.content_width + page_layout.margin_right -
144 printing::kSettingHeaderFooterInterstice - text_width_in_points;
145 break;
146 case printing::CENTER: {
147 SkScalar available_width = GetSegmentWidth(
148 page_layout.margin_left + page_layout.margin_right +
149 page_layout.content_width);
150 x = available_width - page_layout.margin_left +
151 (available_width - text_width_in_points) / 2;
152 break;
153 }
154 default:
155 NOTREACHED();
156 }
157
158 SkScalar y = 0;
159 switch (vertical_position) {
160 case printing::TOP:
161 y = printing::kSettingHeaderFooterInterstice -
162 page_layout.margin_top - offset_to_baseline;
163 break;
164 case printing::BOTTOM:
165 y = page_layout.margin_bottom + page_layout.content_height -
166 printing::kSettingHeaderFooterInterstice - offset_to_baseline;
167 break;
168 default:
169 NOTREACHED();
170 }
171
172 x = x / webkit_scale_factor;
173 y = y / webkit_scale_factor;
174 paint.setTextSize(paint.getTextSize() / webkit_scale_factor);
175 canvas->drawText(text.c_str(), text_byte_length, x, y, paint);
176 }
177 #endif // USE_SKIA
178
104 } // namespace 179 } // namespace
105 180
181 #if defined(USE_SKIA)
182 // static - Not anonymous so that platform implementations can use it.
183 void PrintWebViewHelper::PrintHeaderAndFooter(
184 SkDevice* device,
185 skia::VectorCanvas* canvas,
186 int page_number,
187 int total_pages,
188 float webkit_scale_factor,
189 const PageSizeMargins& page_layout,
190 const DictionaryValue& header_footer_info) {
191 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea(
192 SkPDFDevice::kMargin_DrawingArea);
193
194 SkPaint paint;
195 paint.setColor(SK_ColorBLACK);
196 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
197 paint.setTextSize(printing::kSettingHeaderFooterFontSize);
198 paint.setTypeface(SkTypeface::CreateFromName(
199 printing::kSettingHeaderFooterFontName, SkTypeface::kNormal));
200
201 // Print the headers onto the |canvas| if there is enough space to print
202 // them.
203 string16 date;
204 string16 title;
205 if (!header_footer_info.GetString(printing::kSettingHeaderFooterTitle,
206 &title) ||
207 !header_footer_info.GetString(printing::kSettingHeaderFooterDate,
208 &date)) {
209 NOTREACHED();
210 }
211 string16 header_text = date + title;
212
213 SkRect header_bounds;
214 paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16),
215 &header_bounds, 0);
216 SkScalar text_height =
217 printing::kSettingHeaderFooterInterstice + header_bounds.height();
218 if (text_height <= page_layout.margin_top) {
219 PrintHeaderFooterText(date, canvas, paint, webkit_scale_factor, page_layout,
220 printing::LEFT, printing::TOP, header_bounds.top());
221 PrintHeaderFooterText(title, canvas, paint, webkit_scale_factor,
222 page_layout, printing::CENTER, printing::TOP,
223 header_bounds.top());
224 }
225
226 // Prints the footers onto the |canvas| if there is enough space to print
227 // them.
228 string16 page_of_total_pages = base::IntToString16(page_number) +
229 UTF8ToUTF16("/") +
230 base::IntToString16(total_pages);
231 string16 url;
232 if (!header_footer_info.GetString(printing::kSettingHeaderFooterURL,
233 &url)) {
234 NOTREACHED();
235 }
236 string16 footer_text = page_of_total_pages + url;
237
238 SkRect footer_bounds;
239 paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16),
240 &footer_bounds, 0);
241 text_height =
242 printing::kSettingHeaderFooterInterstice + footer_bounds.height();
243 if (text_height <= page_layout.margin_bottom) {
244 PrintHeaderFooterText(page_of_total_pages, canvas, paint,
245 webkit_scale_factor, page_layout, printing::RIGHT,
246 printing::BOTTOM, footer_bounds.bottom());
247 PrintHeaderFooterText(url, canvas, paint, webkit_scale_factor, page_layout,
248 printing::LEFT, printing::BOTTOM,
249 footer_bounds.bottom());
250 }
251
252 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea(
253 SkPDFDevice::kContent_DrawingArea);
254 }
255 #endif // USE_SKIA
256
106 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 257 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
107 const PrintMsg_Print_Params& print_params, 258 const PrintMsg_Print_Params& print_params,
108 WebFrame* frame, 259 WebFrame* frame,
109 WebNode* node) 260 WebNode* node)
110 : frame_(frame), 261 : frame_(frame),
111 web_view_(frame->view()), 262 web_view_(frame->view()),
112 dpi_(static_cast<int>(print_params.dpi)), 263 dpi_(static_cast<int>(print_params.dpi)),
113 expected_pages_count_(0), 264 expected_pages_count_(0),
114 use_browser_overlays_(true), 265 use_browser_overlays_(true),
115 finished_(false) { 266 finished_(false) {
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 return false; 909 return false;
759 } 910 }
760 return true; 911 return true;
761 } 912 }
762 913
763 bool PrintWebViewHelper::UpdatePrintSettings( 914 bool PrintWebViewHelper::UpdatePrintSettings(
764 const DictionaryValue& job_settings) { 915 const DictionaryValue& job_settings) {
765 PrintMsg_PrintPages_Params settings; 916 PrintMsg_PrintPages_Params settings;
766 917
767 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 918 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
768 print_pages_params_->params.document_cookie, job_settings, &settings)); 919 print_pages_params_->params.document_cookie, job_settings, &settings));
769 920
770 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) 921 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie)
771 return false; 922 return false;
772 923
773 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) 924 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params)))
774 return false; 925 return false;
775 926
776 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 927 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
928
929 // Getting Header and Footer settings.
930 bool display_header_footer = false;
931 if (!job_settings.GetBoolean(printing::kSettingHeaderFooterEnabled,
932 &display_header_footer)) {
933 NOTREACHED();
934 }
935 print_pages_params_->params.display_header_footer = display_header_footer;
936 if (display_header_footer) {
937 header_footer_info_.reset(new DictionaryValue());
938 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
939 print_pages_params_->params.date);
940 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
941 print_pages_params_->params.url);
942 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
943 print_pages_params_->params.title);
944 }
945
777 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 946 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
778 settings.params.document_cookie)); 947 settings.params.document_cookie));
779 return true; 948 return true;
780 } 949 }
781 950
782 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, 951 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame,
783 int expected_pages_count, 952 int expected_pages_count,
784 bool use_browser_overlays) { 953 bool use_browser_overlays) {
785 PrintHostMsg_ScriptedPrint_Params params; 954 PrintHostMsg_ScriptedPrint_Params params;
786 PrintMsg_PrintPages_Params print_settings; 955 PrintMsg_PrintPages_Params print_settings;
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 const gfx::Size& 1292 const gfx::Size&
1124 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1293 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1125 return prep_frame_view_->GetPrintCanvasSize(); 1294 return prep_frame_view_->GetPrintCanvasSize();
1126 } 1295 }
1127 1296
1128 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1297 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1129 prep_frame_view_.reset(); 1298 prep_frame_view_.reset();
1130 metafile_.reset(); 1299 metafile_.reset();
1131 rendered_pages_.clear(); 1300 rendered_pages_.clear();
1132 } 1301 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698