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

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

Powered by Google App Engine
This is Rietveld 408576698