OLD | NEW |
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 Loading... |
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; |
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::GetHeaderFooterSegmentWidth; |
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 Loading... |
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 && |
| 101 oldParams.params.date == newParams.params.date && |
| 102 oldParams.params.title == newParams.params.title && |
| 103 oldParams.params.url == newParams.params.url && |
90 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 104 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
91 newParams.pages.begin()); | 105 newParams.pages.begin()); |
92 } | 106 } |
93 | 107 |
94 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 108 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, |
95 gfx::Size* result) { | 109 gfx::Size* result) { |
96 int dpi = GetDPI(&print_params); | 110 int dpi = GetDPI(&print_params); |
97 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, | 111 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, |
98 print_params.desired_dpi)); | 112 print_params.desired_dpi)); |
99 | 113 |
100 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, | 114 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, |
101 print_params.desired_dpi)); | 115 print_params.desired_dpi)); |
102 } | 116 } |
103 | 117 |
| 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 |
104 } // namespace | 181 } // namespace |
105 | 182 |
| 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 |
106 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 259 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
107 const PrintMsg_Print_Params& print_params, | 260 const PrintMsg_Print_Params& print_params, |
108 WebFrame* frame, | 261 WebFrame* frame, |
109 WebNode* node) | 262 WebNode* node) |
110 : frame_(frame), | 263 : frame_(frame), |
111 web_view_(frame->view()), | 264 web_view_(frame->view()), |
112 dpi_(static_cast<int>(print_params.dpi)), | 265 dpi_(static_cast<int>(print_params.dpi)), |
113 expected_pages_count_(0), | 266 expected_pages_count_(0), |
114 use_browser_overlays_(true), | 267 use_browser_overlays_(true), |
115 finished_(false) { | 268 finished_(false) { |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 return false; | 912 return false; |
760 } | 913 } |
761 return true; | 914 return true; |
762 } | 915 } |
763 | 916 |
764 bool PrintWebViewHelper::UpdatePrintSettings( | 917 bool PrintWebViewHelper::UpdatePrintSettings( |
765 const DictionaryValue& job_settings) { | 918 const DictionaryValue& job_settings) { |
766 PrintMsg_PrintPages_Params settings; | 919 PrintMsg_PrintPages_Params settings; |
767 | 920 |
768 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 921 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
769 print_pages_params_->params.document_cookie, job_settings, &settings)); | 922 print_pages_params_->params.document_cookie, job_settings, &settings)); |
770 | 923 |
771 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 924 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
772 return false; | 925 return false; |
773 | 926 |
774 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) | 927 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) |
775 return false; | 928 return false; |
776 | 929 |
777 if (!job_settings.GetBoolean(printing::kIsFirstRequest, | 930 if (!job_settings.GetBoolean(printing::kIsFirstRequest, |
778 &(settings.params.is_first_request))) { | 931 &(settings.params.is_first_request))) { |
779 NOTREACHED(); | 932 NOTREACHED(); |
780 } | 933 } |
781 | 934 |
782 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 935 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 |
783 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 947 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
784 settings.params.document_cookie)); | 948 settings.params.document_cookie)); |
785 return true; | 949 return true; |
786 } | 950 } |
787 | 951 |
788 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 952 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
789 int expected_pages_count, | 953 int expected_pages_count, |
790 bool use_browser_overlays) { | 954 bool use_browser_overlays) { |
791 PrintHostMsg_ScriptedPrint_Params params; | 955 PrintHostMsg_ScriptedPrint_Params params; |
792 PrintMsg_PrintPages_Params print_settings; | 956 PrintMsg_PrintPages_Params print_settings; |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1131 const gfx::Size& | 1295 const gfx::Size& |
1132 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1296 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1133 return prep_frame_view_->GetPrintCanvasSize(); | 1297 return prep_frame_view_->GetPrintCanvasSize(); |
1134 } | 1298 } |
1135 | 1299 |
1136 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1300 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1137 prep_frame_view_.reset(); | 1301 prep_frame_view_.reset(); |
1138 metafile_.reset(); | 1302 metafile_.reset(); |
1139 rendered_pages_.clear(); | 1303 rendered_pages_.clear(); |
1140 } | 1304 } |
OLD | NEW |