Index: chrome/renderer/print_web_view_helper_linux.cc |
diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc |
index 9255a007bfc51867ec19bdadb47c67e446c21ee6..5ab53feebf0139a3575a9f58c9f8451e9bbbb6e1 100644 |
--- a/chrome/renderer/print_web_view_helper_linux.cc |
+++ b/chrome/renderer/print_web_view_helper_linux.cc |
@@ -5,23 +5,33 @@ |
#include "chrome/renderer/print_web_view_helper.h" |
#include "base/file_descriptor_posix.h" |
+#include "base/i18n/time_formatting.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/metrics/histogram.h" |
+#include "base/string_number_conversions.h" |
+#include "base/time.h" |
+#include "base/utf_string_conversions.h" |
#include "chrome/common/print_messages.h" |
#include "content/common/view_messages.h" |
#include "printing/metafile.h" |
#include "printing/metafile_impl.h" |
#include "printing/metafile_skia_wrapper.h" |
#include "skia/ext/vector_canvas.h" |
+#include "skia/ext/vector_platform_device_skia.h" |
+#include "third_party/skia/include/core/SkColor.h" |
+#include "third_party/skia/include/core/SkDraw.h" |
#include "third_party/skia/include/core/SkRefCnt.h" |
+#include "third_party/skia/include/core/SkTypeface.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
+#include "ui/base/text/text_elider.h" |
#include "ui/gfx/point.h" |
#if !defined(OS_CHROMEOS) |
#include "base/process_util.h" |
#endif // !defined(OS_CHROMEOS) |
+using base::Time; |
using WebKit::WebFrame; |
using WebKit::WebNode; |
@@ -164,6 +174,7 @@ bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
*page_count)); |
#endif |
} |
+ int total_pages = *page_count; |
base::TimeTicks begin_time = base::TimeTicks::Now(); |
base::TimeTicks page_begin_time = begin_time; |
@@ -174,7 +185,8 @@ bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
if (params.pages.empty()) { |
for (int i = 0; i < *page_count; ++i) { |
page_params.page_number = i; |
- PrintPageInternal(page_params, canvas_size, frame, metafile); |
+ PrintPageInternal(page_params, canvas_size, frame, metafile, is_preview, |
+ total_pages); |
if (is_preview) { |
page_begin_time = ReportPreviewPageRenderTime(page_begin_time); |
if (!PreviewPageRendered(i)) |
@@ -184,7 +196,8 @@ bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
} else { |
for (size_t i = 0; i < params.pages.size(); ++i) { |
page_params.page_number = params.pages[i]; |
- PrintPageInternal(page_params, canvas_size, frame, metafile); |
+ PrintPageInternal(page_params, canvas_size, frame, metafile, is_preview, |
+ total_pages); |
if (is_preview) { |
page_begin_time = ReportPreviewPageRenderTime(page_begin_time); |
if (!PreviewPageRendered(params.pages[i])) |
@@ -206,11 +219,87 @@ bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
return true; |
} |
+ |
+size_t GetString16ByteLength(string16 text) { |
+ return text.length()*sizeof(char16); |
+} |
+ |
+SkScalar PrintWebViewHelper::GetHorizontalCoordinate( |
+ string16 text, |
+ SkPaint paint, |
+ HorizontalHeaderFooterPosition pos, |
+ SkScalar margin_left_in_points, |
+ SkScalar margin_right_in_points, |
+ SkScalar content_width_in_points) { |
+ SkScalar text_width_in_points = paint.measureText(text.c_str(), |
+ GetString16ByteLength(text)); |
+ |
+ switch (pos) { |
+ case LEFT: |
+ return margin_left_in_points*(-1) + header_footer_interstice; |
+ case RIGHT: |
+ return ((content_width_in_points+margin_right_in_points) - |
+ (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( |
+ SkPaint paint, |
+ VerticalHeaderFooterPosition pos, |
+ SkScalar margin_top_in_points, |
+ SkScalar margin_bottom_in_points, |
+ SkScalar content_height_in_points) { |
+ if (pos == TOP) { |
+ return margin_top_in_points*(-0.5); |
+ } else if (pos == BOTTOM) { |
+ return margin_top_in_points+content_height_in_points; |
+ } else { |
+ NOTREACHED(); |
+ return 0; |
+ } |
+} |
+ |
+void PrintWebViewHelper::PrintHeaderFooterText( |
+ string16 text, |
+ SkPaint paint, |
+ const SkRefPtr<skia::VectorCanvas>& canvas, |
+ SkScalar margin_left_in_points, |
+ SkScalar margin_right_in_points, |
+ SkScalar content_width_in_points, |
+ SkScalar margin_top_in_points, |
+ SkScalar margin_bottom_in_points, |
+ SkScalar content_height_in_points, |
+ HorizontalHeaderFooterPosition hor_pos, |
+ VerticalHeaderFooterPosition ver_pos) { |
+ SkScalar x_cord = GetHorizontalCoordinate(text, paint, hor_pos, |
+ margin_left_in_points, |
+ margin_right_in_points, |
+ content_width_in_points); |
+ SkScalar y_cord = GetVerticalCoordinate(paint, ver_pos, margin_top_in_points, |
+ margin_bottom_in_points, |
+ content_height_in_points); |
+ size_t byte_length = GetString16ByteLength(text); |
+ |
+ canvas->drawText(text.c_str(), byte_length, x_cord, y_cord, paint); |
+} |
+ |
void PrintWebViewHelper::PrintPageInternal( |
const PrintMsg_PrintPage_Params& params, |
const gfx::Size& canvas_size, |
WebFrame* frame, |
- printing::Metafile* metafile) { |
+ printing::Metafile* metafile, |
+ bool is_preview, |
+ int total_pages) { |
+ // TODO(aayushkumar): Make these variables private variables of the class. |
double content_width_in_points; |
double content_height_in_points; |
double margin_top_in_points; |
@@ -245,6 +334,70 @@ void PrintWebViewHelper::PrintPageInternal( |
SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
canvas->unref(); // SkRefPtr and new both took a reference. |
printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
+ |
+ // TODO(aayushkumar): Have a seperate function for printing Headers and Footer |
kmadhusu
2011/07/13 18:47:53
Implement this TODO.
Aayush Kumar
2011/07/13 21:52:16
Done.
|
+ // that will hopefully be in common for all platforms. |
+ if (is_preview && params.params.header_footer) { |
+ // Set the drawing area to draw in the margins. |
+ ((skia::VectorPlatformDeviceSkia* )device)-> |
+ setDrawingArea(SkPDFDevice::kMargin_DrawingArea); |
+ |
+ // Setting up styles for the headers and footers text. |
+ SkPaint paint; |
+ paint.setColor(SK_ColorBLACK); |
+ paint.setTextSize(SkIntToScalar(8)); |
+ paint.setTypeface(SkTypeface::CreateFromName(NULL, SkTypeface::kNormal)); |
+ paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
+ |
+ // Printing the Date |
+ Time now = Time::Now(); |
+ string16 date = base::TimeFormatShortDateNumeric(now); |
+ PrintHeaderFooterText(date, paint, canvas, margin_left_in_points, |
+ margin_right_in_points, content_width_in_points, |
+ margin_top_in_points, margin_bottom_in_points, |
+ content_height_in_points, LEFT, TOP); |
+ |
+ // Printing the title |
+ string16 title; |
+ if (!header_footer_info_->GetString("title", &title)) |
+ NOTREACHED(); |
+ |
+ SkScalar maxTitleSize = (margin_left_in_points + |
+ content_width_in_points + |
+ margin_right_in_points)/3; |
+ title = ui::ElideText(title, paint, maxTitleSize, false); |
+ PrintHeaderFooterText(title, paint, canvas, margin_left_in_points, |
+ margin_right_in_points, content_width_in_points, |
+ margin_top_in_points, margin_bottom_in_points, |
+ content_height_in_points, CENTER, TOP); |
+ |
+ // Printing the URL |
+ std::string url; |
+ if (!header_footer_info_->GetString("url", &url)) |
+ NOTREACHED(); |
+ GURL gurl(url); |
+ string16 url_elided = ui::ElideUrl(gurl, paint, maxTitleSize, |
+ std::string()); |
+ PrintHeaderFooterText(url_elided, paint, canvas, margin_left_in_points, |
+ margin_right_in_points, content_width_in_points, |
+ margin_top_in_points, margin_bottom_in_points, |
+ content_height_in_points, LEFT, BOTTOM); |
+ |
+ // Printing the page numbers at the bottom right corner of page. |
+ string16 page_on_page_total = base::IntToString16(params.page_number+1) + |
+ UTF8ToUTF16("/") + |
kmadhusu
2011/07/13 18:47:53
nit: Fix indentation.
Aayush Kumar
2011/07/13 21:52:16
Done.
|
+ base::IntToString16(total_pages); |
+ PrintHeaderFooterText(page_on_page_total, paint, canvas, |
+ margin_left_in_points, margin_right_in_points, |
+ content_width_in_points, margin_top_in_points, |
+ margin_bottom_in_points, content_height_in_points, |
+ RIGHT, BOTTOM); |
+ |
+ // Restore the drawing area to draw in the content area. |
+ ((skia::VectorPlatformDeviceSkia* )device)-> |
+ setDrawingArea(SkPDFDevice::kContent_DrawingArea); |
+ } |
+ |
frame->printPage(params.page_number, canvas.get()); |
// TODO(myhuang): We should render the header and the footer. |