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

Unified Diff: chrome/renderer/print_web_view_helper.cc

Issue 7348010: Added Header and Footer support using Skia (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Refactored JS code 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 side-by-side diff with in-line comments
Download patch
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 b065f31b4416deae0f9ecc570ab673dde50736e9..38981506f7ac47c6e78cb6428f25d0d6f1e8e90f 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -36,10 +36,19 @@
#include "content/common/view_messages.h"
#endif
+#if defined(USE_SKIA)
+#include "base/string_number_conversions.h"
+#include "skia/ext/vector_canvas.h"
+#include "skia/ext/vector_platform_device_skia.h"
+#include "third_party/skia/include/core/SkTypeface.h"
+#endif // USE_SKIA
James Hawkins 2011/08/15 19:39:35 #endif // defined(USE_SKIA) Here and elsewhere.
Aayush Kumar 2011/08/15 21:37:37 Done.
+
+using base::Time;
using printing::ConvertPixelsToPoint;
using printing::ConvertPixelsToPointDouble;
using printing::ConvertUnit;
using printing::ConvertUnitDouble;
+using printing::GetSegmentWidth;
using WebKit::WebConsoleMessage;
using WebKit::WebDocument;
using WebKit::WebElement;
@@ -87,6 +96,8 @@ 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 &&
kmadhusu 2011/08/15 16:46:54 As I said earlier, you need to compare the individ
Aayush Kumar 2011/08/15 21:37:37 Done.
std::equal(oldParams.pages.begin(), oldParams.pages.end(),
newParams.pages.begin());
}
@@ -101,8 +112,144 @@ void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
print_params.desired_dpi));
}
+#if defined(USE_SKIA)
+// Given a text, the positions, and the paint object, this method gets the
+// coordinates and prints the text at those coordinates on the canvas.
+void PrintHeaderFooterText(
+ string16 text,
+ skia::VectorCanvas* canvas,
+ SkPaint paint,
+ float webkit_scale_factor,
+ const PageSizeMargins& page_layout,
+ printing::HorizontalHeaderFooterPosition horizontal_position,
+ printing::VerticalHeaderFooterPosition vertical_position,
+ SkScalar offset_to_baseline) {
+ size_t text_byte_length = text.length() * sizeof(char16);
+ // Get the (x, y) coordinate from where printing of the current text should
+ // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
+ // vertical alignment (TOP, BOTTOM).
+ SkScalar text_width_in_points = paint.measureText(text.c_str(),
+ text_byte_length);
+ SkScalar x = 0;
+ switch (horizontal_position) {
+ case printing::LEFT:
+ x = printing::kSettingHeaderFooterInterstice - page_layout.margin_left;
+ break;
+ case printing::RIGHT:
+ x = page_layout.content_width + page_layout.margin_right -
+ printing::kSettingHeaderFooterInterstice - text_width_in_points;
+ break;
+ case printing::CENTER: {
James Hawkins 2011/08/15 19:39:35 Either use braces for all cases or none.
Aayush Kumar 2011/08/15 21:37:37 Done.
+ SkScalar available_width = GetSegmentWidth(
+ page_layout.margin_left + page_layout.margin_right +
+ page_layout.content_width);
+ x = available_width - page_layout.margin_left +
+ (available_width - text_width_in_points) / 2;
+ break;
+ }
+ default:
+ NOTREACHED();
+ }
+
+ SkScalar y = 0;
+ switch (vertical_position) {
+ case printing::TOP:
+ y = printing::kSettingHeaderFooterInterstice -
+ page_layout.margin_top - offset_to_baseline;
+ break;
+ case printing::BOTTOM:
+ y = page_layout.margin_bottom + page_layout.content_height -
+ printing::kSettingHeaderFooterInterstice - offset_to_baseline;
+ break;
+ default:
+ NOTREACHED();
+ }
+
+ x = x / webkit_scale_factor;
+ y = y / webkit_scale_factor;
+ paint.setTextSize(paint.getTextSize() / webkit_scale_factor);
+ canvas->drawText(text.c_str(), text_byte_length, x, y, paint);
+}
+#endif // USE_SKIA
+
} // namespace
+#if defined(USE_SKIA)
+// static - Not anonymous so that platform implementations can use it.
+void PrintWebViewHelper::PrintHeaderAndFooter(
+ SkDevice* device,
+ skia::VectorCanvas* canvas,
+ int page_number,
+ int total_pages,
+ float webkit_scale_factor,
+ const PageSizeMargins& page_layout,
+ const DictionaryValue& header_footer_info) {
+ static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea(
+ SkPDFDevice::kMargin_DrawingArea);
+
+ SkPaint paint;
+ paint.setColor(SK_ColorBLACK);
+ paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
+ paint.setTextSize(printing::kSettingHeaderFooterFontSize);
+ paint.setTypeface(SkTypeface::CreateFromName(
+ printing::kSettingHeaderFooterFontName, SkTypeface::kNormal));
+
+ // Print the headers onto the |canvas| if there is enough space to print
+ // them.
+ string16 date;
+ string16 title;
+ if (!header_footer_info.GetString(printing::kSettingHeaderFooterTitle,
+ &title) ||
+ !header_footer_info.GetString(printing::kSettingHeaderFooterDate,
+ &date)) {
+ NOTREACHED();
+ }
+ string16 header_text = date + title;
+
+ SkRect header_bounds;
+ 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) {
+ PrintHeaderFooterText(date, canvas, paint, webkit_scale_factor, page_layout,
+ printing::LEFT, printing::TOP, header_bounds.top());
+ PrintHeaderFooterText(title, canvas, paint, webkit_scale_factor,
+ page_layout, printing::CENTER, printing::TOP,
+ header_bounds.top());
+ }
+
+ // Prints the footers onto the |canvas| if there is enough space to print
+ // them.
+ string16 page_of_total_pages = base::IntToString16(page_number) +
+ UTF8ToUTF16("/") +
+ base::IntToString16(total_pages);
+ string16 url;
+ if (!header_footer_info.GetString(printing::kSettingHeaderFooterURL,
+ &url)) {
+ NOTREACHED();
+ }
+ string16 footer_text = page_of_total_pages + url;
+
+ SkRect footer_bounds;
+ 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) {
+ PrintHeaderFooterText(page_of_total_pages, canvas, paint,
+ webkit_scale_factor, page_layout, printing::RIGHT,
+ printing::BOTTOM, footer_bounds.bottom());
+ PrintHeaderFooterText(url, canvas, paint, webkit_scale_factor, page_layout,
+ printing::LEFT, printing::BOTTOM,
+ footer_bounds.bottom());
+ }
+
+ static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea(
+ SkPDFDevice::kContent_DrawingArea);
+}
+#endif // USE_SKIA
+
PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
const PrintMsg_Print_Params& print_params,
WebFrame* frame,
@@ -765,7 +912,7 @@ bool PrintWebViewHelper::UpdatePrintSettings(
PrintMsg_PrintPages_Params settings;
Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
- print_pages_params_->params.document_cookie, job_settings, &settings));
+ print_pages_params_->params.document_cookie, job_settings, &settings));
if (settings.params.dpi < kMinDpi || !settings.params.document_cookie)
return false;
@@ -774,6 +921,17 @@ bool PrintWebViewHelper::UpdatePrintSettings(
return false;
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
+
+ if (print_pages_params_->params.display_header_footer) {
+ header_footer_info_.reset(new DictionaryValue());
vandebo (ex-Chrome) 2011/08/15 19:35:49 Why do we need header_footer_info_ ? Can't we just
+ header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
+ print_pages_params_->params.date);
+ header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
+ print_pages_params_->params.url);
+ header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
+ print_pages_params_->params.title);
+ }
+
Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
settings.params.document_cookie));
return true;

Powered by Google App Engine
This is Rietveld 408576698