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 a1c33828f1de836325827570e92d90919b67c57d..c2194b8b1d44df255992c25509400b570e3d905c 100644 |
--- a/chrome/renderer/print_web_view_helper.cc |
+++ b/chrome/renderer/print_web_view_helper.cc |
@@ -7,9 +7,12 @@ |
#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/time.h" |
+#include "base/string_number_conversions.h" |
kmadhusu
2011/07/14 01:50:15
nit: ordering
Aayush Kumar
2011/07/19 01:20:30
Done.
|
#include "base/utf_string_conversions.h" |
#include "chrome/common/chrome_switches.h" |
#include "chrome/common/print_messages.h" |
@@ -21,6 +24,8 @@ |
#include "printing/metafile.h" |
#include "printing/print_job_constants.h" |
#include "printing/units.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,11 +37,13 @@ |
#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::ConvertUnit; |
@@ -88,6 +95,8 @@ bool PrintMsg_Print_Params_IsEqual( |
oldParams.params.supports_alpha_blend == |
newParams.params.supports_alpha_blend && |
oldParams.pages.size() == newParams.pages.size() && |
+ oldParams.params.header_footer == |
+ newParams.params.header_footer && |
std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
newParams.pages.begin()); |
} |
@@ -158,7 +167,8 @@ PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) |
context_menu_preview_node_(NULL), |
user_cancelled_scripted_print_count_(0), |
notify_browser_of_print_failure_(true), |
- preview_page_count_(0) { |
+ preview_page_count_(0), |
+ header_footer_interstice(printing::kSettingHeaderFooterInterstice) { |
is_preview_ = switches::IsPrintPreviewEnabled(); |
} |
@@ -266,10 +276,13 @@ void PrintWebViewHelper::OnPrintPages() { |
Print(frame, NULL); |
} |
-void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
+void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings, |
+ const DictionaryValue& header_footer_info) { |
DCHECK(is_preview_); |
DCHECK(!context_menu_preview_node_.get() || !script_initiated_preview_frame_); |
+ header_footer_info_ = header_footer_info.DeepCopy(); |
+ |
if (script_initiated_preview_frame_) { |
// Script initiated print preview. |
PrintPreview(script_initiated_preview_frame_, NULL, settings); |
@@ -499,13 +512,7 @@ void PrintWebViewHelper::didStopLoading() { |
void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
WebFrame* frame, |
int page_index, |
- const PrintMsg_Print_Params& default_params, |
- double* content_width_in_points, |
- double* content_height_in_points, |
- double* margin_top_in_points, |
- double* margin_right_in_points, |
- double* margin_bottom_in_points, |
- double* margin_left_in_points) { |
+ const PrintMsg_Print_Params& default_params) { |
int dpi = GetDPI(&default_params); |
WebSize page_size_in_pixels( |
@@ -537,39 +544,26 @@ void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
margin_left_in_pixels); |
} |
- *content_width_in_points = ConvertPixelsToPoint(page_size_in_pixels.width - |
- margin_left_in_pixels - |
- margin_right_in_pixels); |
- *content_height_in_points = ConvertPixelsToPoint(page_size_in_pixels.height - |
- margin_top_in_pixels - |
- margin_bottom_in_pixels); |
+ content_width_in_points = ConvertPixelsToPoint(page_size_in_pixels.width - |
+ margin_left_in_pixels - |
+ margin_right_in_pixels); |
+ content_height_in_points = ConvertPixelsToPoint(page_size_in_pixels.height - |
+ margin_top_in_pixels - |
+ margin_bottom_in_pixels); |
// Invalid page size and/or margins. We just use the default setting. |
- if (*content_width_in_points < 1.0 || *content_height_in_points < 1.0) { |
+ if (content_width_in_points < 1.0 || content_height_in_points < 1.0) { |
GetPageSizeAndMarginsInPoints(NULL, |
page_index, |
- default_params, |
- content_width_in_points, |
- content_height_in_points, |
- margin_top_in_points, |
- margin_right_in_points, |
- margin_bottom_in_points, |
- margin_left_in_points); |
+ default_params); |
return; |
} |
- if (margin_top_in_points) |
- *margin_top_in_points = |
- ConvertPixelsToPointDouble(margin_top_in_pixels); |
- if (margin_right_in_points) |
- *margin_right_in_points = |
- ConvertPixelsToPointDouble(margin_right_in_pixels); |
- if (margin_bottom_in_points) |
- *margin_bottom_in_points = |
+ margin_top_in_points = ConvertPixelsToPointDouble(margin_top_in_pixels); |
+ margin_right_in_points = ConvertPixelsToPointDouble(margin_right_in_pixels); |
+ margin_bottom_in_points = |
ConvertPixelsToPointDouble(margin_bottom_in_pixels); |
- if (margin_left_in_points) |
- *margin_left_in_points = |
- ConvertPixelsToPointDouble(margin_left_in_pixels); |
+ margin_left_in_points = ConvertPixelsToPointDouble(margin_left_in_pixels); |
} |
bool PrintWebViewHelper::IsModifiable(WebKit::WebFrame* frame, |
@@ -586,17 +580,8 @@ void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
WebFrame* frame, |
WebNode* node, |
PrintMsg_Print_Params* params) { |
- double content_width_in_points; |
- double content_height_in_points; |
- double margin_top_in_points; |
- double margin_right_in_points; |
- double margin_bottom_in_points; |
- double margin_left_in_points; |
PrepareFrameAndViewForPrint prepare(*params, frame, node, frame->view()); |
- PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, |
- &content_width_in_points, &content_height_in_points, |
- &margin_top_in_points, &margin_right_in_points, |
- &margin_bottom_in_points, &margin_left_in_points); |
+ PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params); |
int dpi = GetDPI(params); |
params->printable_size = gfx::Size( |
static_cast<int>(ConvertUnitDouble(content_width_in_points, |
@@ -687,6 +672,11 @@ bool PrintWebViewHelper::UpdatePrintSettingsLocal( |
return false; |
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
+ bool header_footer; |
+ if (!job_settings.GetBoolean(printing::kSettingHeaderFooter, &header_footer)) |
+ NOTREACHED(); |
+ print_pages_params_.get()->params.header_footer = header_footer; |
kmadhusu
2011/07/14 01:50:15
"print_pages_params_.get()" -> "print_pages_params
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ |
Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
settings.params.document_cookie)); |
return true; |
@@ -863,3 +853,118 @@ bool PrintWebViewHelper::PreviewPageRendered(int page_number) { |
notify_browser_of_print_failure_ = false; |
return !cancel; |
} |
+ |
+size_t GetString16ByteLength(string16 text) { |
+ return text.length()*sizeof(char16); |
+} |
+ |
+SkScalar PrintWebViewHelper::GetHorizontalCoordinate( |
+ string16 text, |
+ SkPaint paint, |
+ HorizontalHeaderFooterPosition pos) { |
+ SkScalar text_width_in_points = paint.measureText(text.c_str(), |
+ GetString16ByteLength(text)); |
+ |
+ switch (pos) { |
kmadhusu
2011/07/14 01:50:15
Why did you use switch() here and if() in GetVerti
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ case LEFT: |
kmadhusu
2011/07/14 01:50:15
nit: curly braces.
http://google-styleguide.googl
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ return margin_left_in_points*(-1) + header_footer_interstice; |
+ case RIGHT: |
+ return ((content_width_in_points+margin_right_in_points) - |
kmadhusu
2011/07/14 01:50:15
nit: add space before and after '+'
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ (header_footer_interstice + text_width_in_points)); |
+ case CENTER: |
+ SkScalar available_width = (margin_left_in_points + |
+ margin_right_in_points + |
+ content_width_in_points - |
+ 4*header_footer_interstice)/3; |
+ return (available_width - margin_left_in_points + |
+ (available_width - text_width_in_points)/2); |
+ } |
+ NOTREACHED(); |
+ return 0; |
+} |
+ |
+SkScalar PrintWebViewHelper::GetVerticalCoordinate( |
kmadhusu
2011/07/14 01:50:15
Are you planning to call GetHorizontalCoordinate()
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ SkPaint paint, |
+ VerticalHeaderFooterPosition pos) { |
+ if (pos == TOP) { |
+ return margin_top_in_points*(-1) + header_footer_interstice + |
+ paint.getTextSize(); |
+ } else if (pos == BOTTOM) { |
+ return margin_bottom_in_points+content_height_in_points - |
kmadhusu
2011/07/14 01:50:15
nit: add space before and after '+'
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ header_footer_interstice; |
+ } else { |
+ NOTREACHED(); |
+ return 0; |
+ } |
+} |
+ |
+void PrintWebViewHelper::PrintHeaderFooterText( |
+ string16 text, |
+ SkPaint paint, |
+ const SkRefPtr<skia::VectorCanvas>& canvas, |
+ HorizontalHeaderFooterPosition hor_pos, |
+ VerticalHeaderFooterPosition ver_pos, |
+ float webkit_scale_factor) { |
+ SkScalar x_cord = GetHorizontalCoordinate(text, paint, hor_pos) / |
+ webkit_scale_factor; |
+ SkScalar y_cord = GetVerticalCoordinate(paint, ver_pos) / |
+ webkit_scale_factor; |
+ size_t byte_length = GetString16ByteLength(text); |
+ |
+ canvas->drawText(text.c_str(), byte_length, x_cord, y_cord, paint); |
+} |
+ |
+void PrintWebViewHelper::PrintHeaderAndFooter(SkDevice *device, |
+ const SkRefPtr<skia::VectorCanvas>& canvas, |
+ int page_number, int total_pages, float webkit_scale_factor) { |
+ |
+ // Set the drawing area to draw in the margins. |
+ ((skia::VectorPlatformDeviceSkia* )device)-> |
kmadhusu
2011/07/14 01:50:15
Remove outer paranthesis "(skia::VectorPlatformDev
Aayush Kumar
2011/07/19 01:20:30
That doesn't work. It tries to call setDrawingAre
|
+ setDrawingArea(SkPDFDevice::kMargin_DrawingArea); |
+ |
+ // Setting up styles for the headers and footers text. |
+ SkPaint paint; |
+ paint.setColor(SK_ColorBLACK); |
+ paint.setTextSize(SkIntToScalar(8)/webkit_scale_factor); |
kmadhusu
2011/07/14 01:50:15
Is there any specific reason to use "8"?
dpapad
2011/07/14 15:59:35
Spaces around /.
Aayush Kumar
2011/07/19 01:20:30
It's what looked good on the page according to me
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ paint.setTypeface(SkTypeface::CreateFromName(NULL, SkTypeface::kNormal)); |
+ paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
+ |
+ // Printing the Date |
+ Time now = Time::Now(); |
+ string16 date = base::TimeFormatShortDateNumeric(now); |
kmadhusu
2011/07/14 01:50:15
|now| is used only once.
Change line 934 to base
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ PrintHeaderFooterText(date, paint, canvas, LEFT, TOP, webkit_scale_factor); |
+ |
+ // Printing the title |
+ string16 title; |
+ if (!header_footer_info_->GetString("title", &title)) |
kmadhusu
2011/07/14 01:50:15
"title" -> printing::kSettingHeaderFooterTitle
Aayush Kumar
2011/07/19 01:20:30
Thanks! :)
On 2011/07/14 01:50:15, kmadhusu wrote:
|
+ NOTREACHED(); |
+ |
+ SkScalar maxTitleSize = (margin_left_in_points + |
+ content_width_in_points + |
+ margin_right_in_points - |
+ 4*header_footer_interstice)/3; |
+ title = ui::ElideText(title, paint, maxTitleSize, false); |
+ PrintHeaderFooterText(title, paint, canvas, CENTER, TOP, |
+ webkit_scale_factor); |
kmadhusu
2011/07/14 01:50:15
Fix indentation.
PrintHeaderFooterText(title, pai
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ |
+ // Printing the URL |
+ std::string url; |
+ if (!header_footer_info_->GetString("url", &url)) |
kmadhusu
2011/07/14 01:50:15
"url" -> printing::kSettingHeaderFooterURL
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ NOTREACHED(); |
+ GURL gurl(url); |
+ string16 url_elided = ui::ElideUrl(gurl, paint, maxTitleSize, |
+ std::string()); |
+ PrintHeaderFooterText(url_elided, paint, canvas, LEFT, BOTTOM, |
+ webkit_scale_factor); |
kmadhusu
2011/07/14 01:50:15
Fix indentation
Aayush Kumar
2011/07/19 01:20:30
Done.
|
+ |
+ // Printing the page numbers at the bottom right corner of page. |
+ string16 page_on_page_total = base::IntToString16(page_number) + |
+ UTF8ToUTF16("/") + |
+ base::IntToString16(total_pages); |
+ PrintHeaderFooterText(page_on_page_total, paint, canvas, RIGHT, BOTTOM, |
+ webkit_scale_factor); |
+ |
+ // Restore the drawing area to draw in the content area. |
+ ((skia::VectorPlatformDeviceSkia* )device)-> |
+ setDrawingArea(SkPDFDevice::kContent_DrawingArea); |
+} |