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

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

Issue 7670045: Revert 97219 - Added Header and Footer support in Linux, Windows and Mac for Skia (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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 // defined(USE_SKIA)
45
46 using base::Time;
47 using printing::ConvertPixelsToPoint; 39 using printing::ConvertPixelsToPoint;
48 using printing::ConvertPixelsToPointDouble; 40 using printing::ConvertPixelsToPointDouble;
49 using printing::ConvertUnit; 41 using printing::ConvertUnit;
50 using printing::ConvertUnitDouble; 42 using printing::ConvertUnitDouble;
51 using printing::GetHeaderFooterSegmentWidth;
52 using WebKit::WebConsoleMessage; 43 using WebKit::WebConsoleMessage;
53 using WebKit::WebDocument; 44 using WebKit::WebDocument;
54 using WebKit::WebElement; 45 using WebKit::WebElement;
55 using WebKit::WebFrame; 46 using WebKit::WebFrame;
56 using WebKit::WebNode; 47 using WebKit::WebNode;
57 using WebKit::WebSize; 48 using WebKit::WebSize;
58 using WebKit::WebString; 49 using WebKit::WebString;
59 using WebKit::WebURLRequest; 50 using WebKit::WebURLRequest;
60 using WebKit::WebView; 51 using WebKit::WebView;
61 52
(...skipping 27 matching lines...) Expand all
89 oldParams.params.min_shrink == newParams.params.min_shrink && 80 oldParams.params.min_shrink == newParams.params.min_shrink &&
90 oldParams.params.dpi == newParams.params.dpi && 81 oldParams.params.dpi == newParams.params.dpi &&
91 oldParams.params.printable_size == newParams.params.printable_size && 82 oldParams.params.printable_size == newParams.params.printable_size &&
92 oldParams.params.selection_only == newParams.params.selection_only && 83 oldParams.params.selection_only == newParams.params.selection_only &&
93 oldParams.params.page_size == newParams.params.page_size && 84 oldParams.params.page_size == newParams.params.page_size &&
94 oldParams.params.margin_top == newParams.params.margin_top && 85 oldParams.params.margin_top == newParams.params.margin_top &&
95 oldParams.params.margin_left == newParams.params.margin_left && 86 oldParams.params.margin_left == newParams.params.margin_left &&
96 oldParams.params.supports_alpha_blend == 87 oldParams.params.supports_alpha_blend ==
97 newParams.params.supports_alpha_blend && 88 newParams.params.supports_alpha_blend &&
98 oldParams.pages.size() == newParams.pages.size() && 89 oldParams.pages.size() == newParams.pages.size() &&
99 oldParams.params.display_header_footer ==
100 newParams.params.display_header_footer &&
101 oldParams.params.date == newParams.params.date &&
102 oldParams.params.title == newParams.params.title &&
103 oldParams.params.url == newParams.params.url &&
104 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 90 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
105 newParams.pages.begin()); 91 newParams.pages.begin());
106 } 92 }
107 93
108 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 94 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
109 gfx::Size* result) { 95 gfx::Size* result) {
110 int dpi = GetDPI(&print_params); 96 int dpi = GetDPI(&print_params);
111 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, 97 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi,
112 print_params.desired_dpi)); 98 print_params.desired_dpi));
113 99
114 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, 100 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi,
115 print_params.desired_dpi)); 101 print_params.desired_dpi));
116 } 102 }
117 103
118 #if defined(USE_SKIA)
119 // Given a text, the positions, and the paint object, this method gets the
120 // coordinates and prints the text at those coordinates on the canvas.
121 void PrintHeaderFooterText(
122 string16 text,
123 skia::VectorCanvas* canvas,
124 SkPaint paint,
125 float webkit_scale_factor,
126 const PageSizeMargins& page_layout,
127 printing::HorizontalHeaderFooterPosition horizontal_position,
128 printing::VerticalHeaderFooterPosition vertical_position,
129 SkScalar offset_to_baseline) {
130 size_t text_byte_length = text.length() * sizeof(char16);
131 // Get the (x, y) coordinate from where printing of the current text should
132 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
133 // vertical alignment (TOP, BOTTOM).
134 SkScalar text_width_in_points = paint.measureText(text.c_str(),
135 text_byte_length);
136 SkScalar x = 0;
137 switch (horizontal_position) {
138 case printing::LEFT: {
139 x = printing::kSettingHeaderFooterInterstice - page_layout.margin_left;
140 break;
141 }
142 case printing::RIGHT: {
143 x = page_layout.content_width + page_layout.margin_right -
144 printing::kSettingHeaderFooterInterstice - text_width_in_points;
145 break;
146 }
147 case printing::CENTER: {
148 SkScalar available_width = GetHeaderFooterSegmentWidth(
149 page_layout.margin_left + page_layout.margin_right +
150 page_layout.content_width);
151 x = available_width - page_layout.margin_left +
152 (available_width - text_width_in_points) / 2;
153 break;
154 }
155 default: {
156 NOTREACHED();
157 }
158 }
159
160 SkScalar y = 0;
161 switch (vertical_position) {
162 case printing::TOP:
163 y = printing::kSettingHeaderFooterInterstice -
164 page_layout.margin_top - offset_to_baseline;
165 break;
166 case printing::BOTTOM:
167 y = page_layout.margin_bottom + page_layout.content_height -
168 printing::kSettingHeaderFooterInterstice - offset_to_baseline;
169 break;
170 default:
171 NOTREACHED();
172 }
173
174 x = x / webkit_scale_factor;
175 y = y / webkit_scale_factor;
176 paint.setTextSize(paint.getTextSize() / webkit_scale_factor);
177 canvas->drawText(text.c_str(), text_byte_length, x, y, paint);
178 }
179 #endif // defined(USE_SKIA)
180
181 } // namespace 104 } // namespace
182 105
183 #if defined(USE_SKIA)
184 // static - Not anonymous so that platform implementations can use it.
185 void PrintWebViewHelper::PrintHeaderAndFooter(
186 SkDevice* device,
187 skia::VectorCanvas* canvas,
188 int page_number,
189 int total_pages,
190 float webkit_scale_factor,
191 const PageSizeMargins& page_layout,
192 const DictionaryValue& header_footer_info) {
193 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea(
194 SkPDFDevice::kMargin_DrawingArea);
195
196 SkPaint paint;
197 paint.setColor(SK_ColorBLACK);
198 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
199 paint.setTextSize(printing::kSettingHeaderFooterFontSize);
200 paint.setTypeface(SkTypeface::CreateFromName(
201 printing::kSettingHeaderFooterFontFamilyName, SkTypeface::kNormal));
202
203 // Print the headers onto the |canvas| if there is enough space to print
204 // them.
205 string16 date;
206 string16 title;
207 if (!header_footer_info.GetString(printing::kSettingHeaderFooterTitle,
208 &title) ||
209 !header_footer_info.GetString(printing::kSettingHeaderFooterDate,
210 &date)) {
211 NOTREACHED();
212 }
213 string16 header_text = date + title;
214
215 SkRect header_bounds;
216 paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16),
217 &header_bounds, 0);
218 SkScalar text_height =
219 printing::kSettingHeaderFooterInterstice + header_bounds.height();
220 if (text_height <= page_layout.margin_top) {
221 PrintHeaderFooterText(date, canvas, paint, webkit_scale_factor, page_layout,
222 printing::LEFT, printing::TOP, header_bounds.top());
223 PrintHeaderFooterText(title, canvas, paint, webkit_scale_factor,
224 page_layout, printing::CENTER, printing::TOP,
225 header_bounds.top());
226 }
227
228 // Prints the footers onto the |canvas| if there is enough space to print
229 // them.
230 string16 page_of_total_pages = base::IntToString16(page_number) +
231 UTF8ToUTF16("/") +
232 base::IntToString16(total_pages);
233 string16 url;
234 if (!header_footer_info.GetString(printing::kSettingHeaderFooterURL,
235 &url)) {
236 NOTREACHED();
237 }
238 string16 footer_text = page_of_total_pages + url;
239
240 SkRect footer_bounds;
241 paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16),
242 &footer_bounds, 0);
243 text_height =
244 printing::kSettingHeaderFooterInterstice + footer_bounds.height();
245 if (text_height <= page_layout.margin_bottom) {
246 PrintHeaderFooterText(page_of_total_pages, canvas, paint,
247 webkit_scale_factor, page_layout, printing::RIGHT,
248 printing::BOTTOM, footer_bounds.bottom());
249 PrintHeaderFooterText(url, canvas, paint, webkit_scale_factor, page_layout,
250 printing::LEFT, printing::BOTTOM,
251 footer_bounds.bottom());
252 }
253
254 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea(
255 SkPDFDevice::kContent_DrawingArea);
256 }
257 #endif // defined(USE_SKIA)
258
259 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 106 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
260 const PrintMsg_Print_Params& print_params, 107 const PrintMsg_Print_Params& print_params,
261 WebFrame* frame, 108 WebFrame* frame,
262 WebNode* node) 109 WebNode* node)
263 : frame_(frame), 110 : frame_(frame),
264 web_view_(frame->view()), 111 web_view_(frame->view()),
265 dpi_(static_cast<int>(print_params.dpi)), 112 dpi_(static_cast<int>(print_params.dpi)),
266 expected_pages_count_(0), 113 expected_pages_count_(0),
267 use_browser_overlays_(true), 114 use_browser_overlays_(true),
268 finished_(false) { 115 finished_(false) {
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 return false; 759 return false;
913 } 760 }
914 return true; 761 return true;
915 } 762 }
916 763
917 bool PrintWebViewHelper::UpdatePrintSettings( 764 bool PrintWebViewHelper::UpdatePrintSettings(
918 const DictionaryValue& job_settings) { 765 const DictionaryValue& job_settings) {
919 PrintMsg_PrintPages_Params settings; 766 PrintMsg_PrintPages_Params settings;
920 767
921 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 768 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
922 print_pages_params_->params.document_cookie, job_settings, &settings)); 769 print_pages_params_->params.document_cookie, job_settings, &settings));
923 770
924 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) 771 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie)
925 return false; 772 return false;
926 773
927 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) 774 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params)))
928 return false; 775 return false;
929 776
930 if (!job_settings.GetBoolean(printing::kIsFirstRequest, 777 if (!job_settings.GetBoolean(printing::kIsFirstRequest,
931 &(settings.params.is_first_request))) { 778 &(settings.params.is_first_request))) {
932 NOTREACHED(); 779 NOTREACHED();
933 } 780 }
934 781
935 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 782 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
936
937 if (print_pages_params_->params.display_header_footer) {
938 header_footer_info_.reset(new DictionaryValue());
939 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
940 print_pages_params_->params.date);
941 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
942 print_pages_params_->params.url);
943 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
944 print_pages_params_->params.title);
945 }
946
947 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 783 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
948 settings.params.document_cookie)); 784 settings.params.document_cookie));
949 return true; 785 return true;
950 } 786 }
951 787
952 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, 788 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame,
953 int expected_pages_count, 789 int expected_pages_count,
954 bool use_browser_overlays) { 790 bool use_browser_overlays) {
955 PrintHostMsg_ScriptedPrint_Params params; 791 PrintHostMsg_ScriptedPrint_Params params;
956 PrintMsg_PrintPages_Params print_settings; 792 PrintMsg_PrintPages_Params print_settings;
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1295 const gfx::Size& 1131 const gfx::Size&
1296 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1132 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1297 return prep_frame_view_->GetPrintCanvasSize(); 1133 return prep_frame_view_->GetPrintCanvasSize();
1298 } 1134 }
1299 1135
1300 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1136 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1301 prep_frame_view_.reset(); 1137 prep_frame_view_.reset();
1302 metafile_.reset(); 1138 metafile_.reset();
1303 rendered_pages_.clear(); 1139 rendered_pages_.clear();
1304 } 1140 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | chrome/renderer/print_web_view_helper_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698