Index: chrome/renderer/print_web_view_helper.cc |
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc |
index 1edc892a397aef33dd43754b5eb070d0da326bc5..50d815341a7782fc3340afa570e2f0046f484bf4 100644 |
--- a/chrome/renderer/print_web_view_helper.cc |
+++ b/chrome/renderer/print_web_view_helper.cc |
@@ -4,12 +4,17 @@ |
#include "chrome/renderer/print_web_view_helper.h" |
+#include <algorithm> |
+#include <cmath> |
#include <string> |
#include "base/command_line.h" |
+#include "base/i18n/time_formatting.h" |
#include "base/logging.h" |
#include "base/metrics/histogram.h" |
#include "base/process_util.h" |
+#include "base/string_number_conversions.h" |
+#include "base/time.h" |
#include "base/utf_string_conversions.h" |
#include "chrome/common/chrome_switches.h" |
#include "chrome/common/print_messages.h" |
@@ -21,6 +26,9 @@ |
#include "printing/metafile_impl.h" |
#include "printing/print_job_constants.h" |
#include "printing/units.h" |
+#include "skia/ext/vector_canvas.h" |
+#include "skia/ext/vector_platform_device_skia.h" |
+#include "third_party/skia/include/core/SkTypeface.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
@@ -32,13 +40,16 @@ |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
#include "ui/base/l10n/l10n_util.h" |
+#include "ui/base/text/text_elider.h" |
#if defined(OS_POSIX) |
#include "content/common/view_messages.h" |
#endif |
+using base::Time; |
using printing::ConvertPixelsToPoint; |
using printing::ConvertPixelsToPointDouble; |
+using printing::ConvertPointsToPixelDouble; |
using printing::ConvertUnit; |
using printing::ConvertUnitDouble; |
using WebKit::WebConsoleMessage; |
@@ -88,12 +99,209 @@ bool PrintMsg_Print_Params_IsEqual( |
oldParams.params.supports_alpha_blend == |
newParams.params.supports_alpha_blend && |
oldParams.pages.size() == newParams.pages.size() && |
+ oldParams.params.display_header_footer == |
+ newParams.params.display_header_footer && |
std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
newParams.pages.begin()); |
} |
+// Splits the horizontal width equally into three segments with an interstice |
Chris Guillory
2011/07/26 00:10:45
Nit: three segments -> segments
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+// between each segment. Returns the width of this segment. |
+SkScalar GetSegmentWidth(PageSizeMargins& page_layout) { |
Chris Guillory
2011/07/26 00:10:45
Make argument const.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ return (page_layout.margin_left + |
+ page_layout.content_width + |
+ page_layout.margin_right - |
+ (printing::kSettingHeaderFooterHorizontalRegions + 1) * |
Chris Guillory
2011/07/26 00:10:45
Can you explain why you have + 1 here?
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ printing::kSettingHeaderFooterInterstice) / |
+ printing::kSettingHeaderFooterHorizontalRegions; |
+} |
+ |
+// Given a text, the positions and the paint object, this method gets the |
+// coordinates and prints the text at those co-ordinates on the canvas. |
Chris Guillory
2011/07/26 00:10:45
Nit: co-ordinates -> coordinates
the positions and
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+void PrintHeaderFooterText( |
+ string16 text, |
+ SkPaint paint, |
+ const SkRefPtr<skia::VectorCanvas>& canvas, |
Chris Guillory
2011/07/26 00:10:45
Remove SkRefPtr usage if possible.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ printing::HorizontalHeaderFooterPosition hor_pos, |
Chris Guillory
2011/07/26 00:10:45
Optional: hor_pos -> horizontal_position
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ printing::VerticalHeaderFooterPosition ver_pos, |
Chris Guillory
2011/07/26 00:10:45
Optional: ver_pos -> vertical_position
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ float webkit_scale_factor, |
+ PageSizeMargins& page_layout, |
Chris Guillory
2011/07/26 00:10:45
const
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ SkScalar max_text_width, |
+ gfx::Font font, |
+ SkScalar height_to_baseline) { |
+ text = ui::ElideText(text, font, ConvertPointsToPixelDouble(max_text_width), |
+ false); |
+ |
+ SkPoint text_location; |
+ size_t text_byte_length = text.length() * sizeof(char16); |
+ // Gets the (x, y) coordinate from where we want to start printing the current |
Chris Guillory
2011/07/26 00:10:45
Nit: Gets -> Get
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ // text depending on the alignment horizontal alignment (LEFT, RIGHT, CENTER) |
+ // and vertical alignment (TOP, BOTTOM). |
+ { |
Chris Guillory
2011/07/26 00:10:45
Can you remove this nested block.
Aayush Kumar
2011/07/26 01:55:41
It tells us that that is a separate control block
|
+ SkScalar text_width_in_points = paint.measureText(text.c_str(), |
+ text_byte_length); |
+ |
+ SkScalar x = 0; |
+ switch (hor_pos) { |
+ case printing::LEFT: { |
+ x = page_layout.margin_left * (-1) + |
Chris Guillory
2011/07/26 00:10:45
page_layout.margin_left * (-1) -> -page_layout.mar
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ printing::kSettingHeaderFooterInterstice; |
+ break; |
+ } |
+ case printing::RIGHT: { |
+ x = (page_layout.content_width + page_layout.margin_right - |
+ (printing::kSettingHeaderFooterInterstice + text_width_in_points)); |
+ break; |
+ } |
+ case printing::CENTER: { |
Chris Guillory
2011/07/26 00:10:45
Looks like braces are only needed on this case sta
Aayush Kumar
2011/07/26 01:55:41
According to the google C++ style guidelines, it s
|
+ SkScalar available_width = GetSegmentWidth(page_layout); |
+ x = (available_width - page_layout.margin_left + |
Chris Guillory
2011/07/26 00:10:45
Don't need the outer parentheses.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ (available_width - text_width_in_points) / 2); |
+ break; |
+ } |
+ default: { |
+ NOTREACHED(); |
+ } |
+ } |
+ |
+ SkScalar y = 0; |
+ switch (ver_pos) { |
+ case printing::TOP: { |
+ y = page_layout.margin_top * (-1) + |
Chris Guillory
2011/07/26 00:10:45
page_layout.margin_top * (-1) -> -page_layout.marg
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ printing::kSettingHeaderFooterInterstice - height_to_baseline; |
+ break; |
+ } |
+ case printing::BOTTOM: { |
+ y = page_layout.margin_bottom + page_layout.content_height - |
+ printing::kSettingHeaderFooterInterstice - height_to_baseline; |
+ break; |
+ } |
+ default: { |
+ NOTREACHED(); |
+ } |
+ } |
+ |
+ text_location.set(x / webkit_scale_factor, y / webkit_scale_factor); |
+ } |
+ |
+ paint.setTextSize(paint.getTextSize()/webkit_scale_factor); |
Chris Guillory
2011/07/26 00:10:45
To be consistent, add space before and the /.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ canvas->drawText(text.c_str(), text_byte_length, text_location.x(), |
+ text_location.y(), paint); |
+} |
+ |
} // namespace |
+void PrintHeaderAndFooter(skia::VectorPlatformDeviceSkia* device, |
+ const SkRefPtr<skia::VectorCanvas>& canvas, |
+ int page_number, int total_pages, |
+ float webkit_scale_factor, |
+ PageSizeMargins& page_layout, |
Chris Guillory
2011/07/26 00:10:45
const
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ const DictionaryValue* header_footer_info) { |
+ // Set the drawing area to draw in the margins. |
+ device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); |
+ |
+ // Setting up styles for the headers and footers text. |
+ SkPaint paint; |
+ paint.setColor(SK_ColorBLACK); |
+ paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
+ paint.setTextSize(printing::kSettingHeaderFooterFontSize); |
+ gfx::Font font(UTF8ToUTF16(printing::kSettingHeaderFooterFontName), |
+ ceil(ConvertPointsToPixelDouble( |
+ printing::kSettingHeaderFooterFontSize))); |
+ paint.setTypeface(SkTypeface::CreateFromName( |
+ UTF16ToUTF8(font.GetFontName()).c_str(), |
+ SkTypeface::kNormal)); |
Chris Guillory
2011/07/26 00:10:45
This can fit on the previous line.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ |
+ // Ensuring we have enough space to print above and below the page to print |
Chris Guillory
2011/07/26 00:10:45
Nit: Ensuring -> Ensure
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ // headers. |
+ string16 date = base::TimeFormatShortDateNumeric(Time::Now()); |
+ string16 title; |
+ if (!header_footer_info->GetString(printing::kSettingHeaderFooterTitle, |
+ &title)) { |
+ NOTREACHED(); |
+ } |
+ |
+ SkRect header_bounds; |
+ string16 header_text = date + title; |
+ paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16), |
+ &header_bounds, 0); |
+ SkScalar text_height = printing::kSettingHeaderFooterInterstice + |
+ header_bounds.height(); |
+ |
+ if (text_height > page_layout.margin_top) |
+ return; |
+ |
+ // Ensuring we have enough space to print above and below the page to print |
Chris Guillory
2011/07/26 00:10:45
Nit: Ensuring -> Ensure
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ // footers. |
+ string16 page_on_page_total = base::IntToString16(page_number) + |
+ UTF8ToUTF16("/") + |
+ base::IntToString16(total_pages); |
+ std::string url; |
+ if (!header_footer_info->GetString(printing::kSettingHeaderFooterURL, |
+ &url)) { |
+ NOTREACHED(); |
+ } |
+ |
+ SkRect footer_bounds; |
+ string16 footer_text = page_on_page_total + UTF8ToUTF16(url); |
+ paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16), |
+ &footer_bounds, 0); |
+ text_height = printing::kSettingHeaderFooterInterstice + |
+ footer_bounds.height(); |
+ |
+ if (text_height > page_layout.margin_bottom) |
+ return; |
+ |
+ SkScalar segment_width = GetSegmentWidth(page_layout); |
+ // Printing the Date. |
Chris Guillory
2011/07/26 00:10:45
Nit: Printing -> Print
Here and below.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ PrintHeaderFooterText(date, paint, canvas, printing::LEFT, printing::TOP, |
+ webkit_scale_factor, page_layout, segment_width, |
+ font, header_bounds.top()); |
+ |
+ // Printing the title. |
+ SkScalar date_width = paint.measureText(date.c_str(), |
+ date.length() * sizeof(char16)); |
+ // Calculating the available title width. If the date string is not long |
Chris Guillory
2011/07/26 00:10:45
Nit: Calculating -> Calculate
Here and below.
Aayush Kumar
2011/07/26 01:55:41
Done.
|
+ // enough, then, we increase the available space we have for the title. |
+ // Assumes there is no header text to RIGHT of title. |
+ SkScalar max_title_width = std::min(2 * segment_width, |
+ 2 * (segment_width - date_width) + |
+ segment_width); |
+ PrintHeaderFooterText(title, paint, canvas, printing::CENTER, |
+ printing::TOP, webkit_scale_factor, page_layout, |
+ max_title_width, font, header_bounds.top()); |
+ |
+ // Printing the page numbers at the bottom right corner of page. |
+ PrintHeaderFooterText(page_on_page_total, paint, canvas, printing::RIGHT, |
+ printing::BOTTOM, webkit_scale_factor, |
+ page_layout, segment_width, font, |
+ footer_bounds.bottom()); |
+ |
+ // Printing the URL. |
+ GURL gurl(url); |
+ SkScalar page_width = paint.measureText( |
+ page_on_page_total.c_str(), |
+ page_on_page_total.length() * sizeof(char16)); |
+ |
+ // Calculating the available URL width. We increase the available URL width |
+ // if the |page_on_page_total| string isn't long enough. |
+ // Assumes no footer text being printed in the CENTER. |
+ SkScalar max_url_width = 3 * (page_layout.content_width + |
+ page_layout.margin_left + page_layout.margin_right) / 4; |
+ max_url_width = std::min(max_url_width, |
+ 2 * (segment_width - page_width) + segment_width); |
+ string16 url_elided = ui::ElideUrl(gurl, font, |
+ ConvertPointsToPixelDouble(max_url_width), |
+ std::string()); |
+ PrintHeaderFooterText(url_elided, paint, canvas, printing::LEFT, |
+ printing::BOTTOM, webkit_scale_factor, |
+ page_layout, max_url_width, font, |
+ footer_bounds.bottom()); |
+ |
+ // Restore the drawing area to draw in the content area. |
+ device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); |
+} |
+ |
PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
const PrintMsg_Print_Params& print_params, |
WebFrame* frame, |
@@ -156,6 +364,7 @@ PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) |
user_cancelled_scripted_print_count_(0), |
notify_browser_of_print_failure_(true) { |
is_preview_ = switches::IsPrintPreviewEnabled(); |
+ header_footer_info_ = new DictionaryValue(); |
} |
PrintWebViewHelper::~PrintWebViewHelper() {} |
@@ -738,6 +947,7 @@ bool PrintWebViewHelper::UpdatePrintSettingsCloud( |
return false; |
// TODO(abodenha@chromium.org) Parse page ranges from the job_settings. |
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
+ print_pages_params_->params.display_header_footer = false; |
Chris Guillory
2011/07/26 00:10:45
Can you set display_header_footer on the settings
Aayush Kumar
2011/07/26 01:55:41
Done.
|
return true; |
} |
@@ -755,6 +965,22 @@ bool PrintWebViewHelper::UpdatePrintSettingsLocal( |
return false; |
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
+ // Getting Header and Footer settings. |
+ bool display_header_footer; |
+ if (!job_settings.GetBoolean(printing::kSettingHeaderFooter, |
+ &display_header_footer)) { |
+ NOTREACHED(); |
+ } |
+ print_pages_params_->params.display_header_footer = display_header_footer; |
+ std::string url; |
+ if (!job_settings.GetString(printing::kSettingHeaderFooterURL, &url)) |
+ NOTREACHED(); |
+ header_footer_info_->SetString(printing::kSettingHeaderFooterURL, url); |
+ string16 title; |
+ if (!job_settings.GetString(printing::kSettingHeaderFooterTitle, &title)) |
+ NOTREACHED(); |
+ header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, title); |
+ |
Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
settings.params.document_cookie)); |
return true; |