diff options
-rw-r--r-- | chrome/browser/browser_resources.grd | 1 | ||||
-rw-r--r-- | chrome/browser/resources/print_preview/print_preview_page.html | 115 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper.cc | 312 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper.h | 11 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_linux.cc | 17 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_mac.mm | 11 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_win.cc | 19 | ||||
-rw-r--r-- | printing/print_job_constants.cc | 16 | ||||
-rw-r--r-- | printing/print_job_constants.h | 5 | ||||
-rw-r--r-- | printing/print_settings_initializer.cc | 25 | ||||
-rw-r--r-- | printing/units.cc | 9 | ||||
-rw-r--r-- | printing/units.h | 4 |
12 files changed, 244 insertions, 301 deletions
diff --git a/chrome/browser/browser_resources.grd b/chrome/browser/browser_resources.grd index bffa941..f229a02 100644 --- a/chrome/browser/browser_resources.grd +++ b/chrome/browser/browser_resources.grd @@ -159,6 +159,7 @@ <include name="IDR_POLICY_JS" file="resources\policy.js" type="BINDATA"/> <if expr="pp_ifdef('enable_printing')"> <include name="IDR_PRINT_PREVIEW_HTML" file="resources\print_preview\print_preview.html" flattenhtml="true" allowexternalscript="true" type="BINDATA" /> + <include name="IDR_PRINT_PREVIEW_PAGE" file="resources\print_preview\print_preview_page.html" flattenhtml="true" allowexternalscript="false" type="BINDATA" /> <include name="IDR_PRINT_PREVIEW_JS" file="resources\print_preview\print_preview.js" flattenhtml="true" type="BINDATA" /> <include name="IDR_PRINT_PREVIEW_IMAGES_PRINTER" file="resources\print_preview\images\printer.png" type="BINDATA" /> diff --git a/chrome/browser/resources/print_preview/print_preview_page.html b/chrome/browser/resources/print_preview/print_preview_page.html new file mode 100644 index 0000000..415ab37 --- /dev/null +++ b/chrome/browser/resources/print_preview/print_preview_page.html @@ -0,0 +1,115 @@ +<!DOCTYPE html> +<head> +<style> + body { + margin: 0px; + width: 0px; + } + .row { + display: table-row; + vertical-align: inherit; + } + #header, #footer { + display: table; + table-layout:fixed; + width: inherit; + } + #header { + vertical-align: top; + } + #footer { + vertical-align: bottom; + } + .text { + display: table-cell; + font-family: Sans; + font-size: 8px; + vertical-align: inherit; + white-space: nowrap; + } + #page_number { + text-align: right; + } + #title { + text-align: center; + } + #date, #url { + padding-left: 0.7cm; + padding-right: 0.1cm; + } + #title, #page_number { + padding-left: 0.1cm; + padding-right: 0.7cm; + } + #title, #url { + overflow: hidden; + text-overflow: ellipsis; + } + #title, #date { + padding-bottom: 0cm; + padding-top: 0.4cm; + } + #page_number, #url { + padding-bottom: 0.4cm; + padding-top: 0cm; + } +</style> +<script> + +function pixels(value) { + return value + 'px'; +} + +function setup(options) { + var body = document.querySelector('body'); + var header = document.querySelector('#header'); + var content = document.querySelector('#content'); + var footer = document.querySelector('#footer'); + + body.style.width = pixels(options['width']); + body.style.height = pixels(options['height']); + header.style.height = pixels(options['topMargin']); + content.style.height = pixels(options['height'] - options['topMargin'] - + options['bottomMargin']); + footer.style.height = pixels(options['bottomMargin']); + + document.querySelector('#date span').innerText = options['date']; + document.querySelector('#title span').innerText = options['title']; + document.querySelector('#url span').innerText = options['url']; + document.querySelector('#page_number span').innerText = options['pageNumber']; + + // Reduce date and page number space to give more space to title and url. + document.querySelector('#date').style.width = + pixels(document.querySelector('#date span').offsetWidth); + document.querySelector('#page_number').style.width = + pixels(document.querySelector('#page_number span').offsetWidth); + + // Hide text if it doesn't fit into expected margins. + if (header.offsetHeight > options['topMargin'] + 1) { + header.style.display = 'none'; + content.style.height = pixels(options['height'] - options['bottomMargin']); + } + if (footer.offsetHeight > options['bottomMargin'] + 1) { + footer.style.display = 'none'; + } +} + +</script> +</head> +<body> + <div id="header"> + <div class="row"> + <div id="date" class="text"><span/></div> + <div id="title" class="text"><span/></div> + </div> + </div> + <div id="content"> + </div> + <div id="footer"> + <div class="row"> + <div id="url" class="text"><span/></div> + <div id="page_number" class="text"><span/></div> + </div> + </div> +</body> +</html> diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index 3aff753..d5685ba 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -8,67 +8,59 @@ #include "base/auto_reset.h" #include "base/command_line.h" +#include "base/json/json_writer.h" #include "base/logging.h" #include "base/metrics/histogram.h" +#include "base/process_util.h" +#include "base/stringprintf.h" #include "base/string_number_conversions.h" #include "base/utf_string_conversions.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/print_messages.h" #include "chrome/common/render_messages.h" -#include "chrome/common/url_constants.h" #include "chrome/renderer/prerender/prerender_helper.h" #include "content/public/renderer/render_thread.h" #include "content/public/renderer/render_view.h" +#include "grit/browser_resources.h" #include "grit/generated_resources.h" +#include "printing/metafile.h" #include "printing/metafile_impl.h" -#include "printing/page_size_margins.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/SkRect.h" -#include "third_party/skia/include/core/SkTypeface.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRequest.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLResponse.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" #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginDocument.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebPrintParams.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebPrintScalingOption.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebSettings.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/layout.h" -#include "ui/gfx/rect.h" -#include "ui/gfx/skia_util.h" +#include "ui/base/resource/resource_bundle.h" #include "webkit/glue/webpreferences.h" -#if defined(OS_POSIX) -#include "base/process_util.h" -#endif - -#if defined(OS_WIN) || defined(OS_MACOSX) -#define USE_RENDER_TEXT -#endif +namespace { -#if defined(USE_RENDER_TEXT) -#include "ui/gfx/canvas.h" -#include "ui/gfx/render_text.h" -#endif +const double kMinDpi = 1.0; -namespace { +const char kPageLoadScriptFormat[] = + "document.open(); document.write(%s); document.close();"; -#if defined(USE_RENDER_TEXT) -typedef gfx::RenderText* HeaderFooterPaint; -#else -typedef SkPaint HeaderFooterPaint; -#endif +const char kPageSetupScriptFormat[] = "setup(%s);"; -const double kMinDpi = 1.0; +void ExecuteScript(WebKit::WebFrame* frame, + const char* script_format, + const base::Value& parameters) { + std::string json; + base::JSONWriter::Write(¶meters, &json); + std::string script = StringPrintf(script_format, json.c_str()); + frame->executeScript(WebKit::WebString(UTF8ToUTF16(script))); +} int GetDPI(const PrintMsg_Print_Params* print_params) { #if defined(OS_MACOSX) @@ -351,114 +343,6 @@ bool FitToPageEnabled(const DictionaryValue& job_settings) { return fit_to_paper_size; } -// 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). -SkPoint GetHeaderFooterPosition( - float webkit_scale_factor, - const printing::PageSizeMargins& page_layout, - printing::HorizontalHeaderFooterPosition horizontal_position, - printing::VerticalHeaderFooterPosition vertical_position, - double offset_to_baseline, - double text_width_in_points) { - 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: { - SkScalar available_width = printing::GetHeaderFooterSegmentWidth( - 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(); - } - - SkPoint point = SkPoint::Make(x / webkit_scale_factor, - y / webkit_scale_factor); - return point; -} - -// 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( - const string16& text, - WebKit::WebCanvas* canvas, - HeaderFooterPaint paint, - float webkit_scale_factor, - const printing::PageSizeMargins& page_layout, - printing::HorizontalHeaderFooterPosition horizontal_position, - printing::VerticalHeaderFooterPosition vertical_position, - double offset_to_baseline) { -#if defined(USE_RENDER_TEXT) - paint->SetText(text); - paint->SetFontSize(printing::kSettingHeaderFooterFontSize); - double text_width_in_points = paint->GetStringSize().width(); - SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, - horizontal_position, - vertical_position, offset_to_baseline, - text_width_in_points); - // Set the scaled font size before drawing the text. - // This creates a new font instead of calling |paint->SetFontSize()| to work - // around a Windows 8 bug. See: http://crbug.com/139206 - gfx::FontList font_list( - gfx::Font(printing::kSettingHeaderFooterFontFamilyName, - printing::kSettingHeaderFooterFontSize / webkit_scale_factor)); - paint->SetFontList(font_list); - gfx::Size size(paint->GetStringSize()); - gfx::Rect rect(point.x(), point.y() - paint->GetBaseline(), - size.width(), size.height()); - paint->SetDisplayRect(rect); - { - SkMatrix m = canvas->getTotalMatrix(); - ui::ScaleFactor device_scale_factor = ui::GetScaleFactorFromScale( - SkScalarAbs(m.getScaleX())); - scoped_ptr<gfx::Canvas> gfx_canvas(gfx::Canvas::CreateCanvasWithoutScaling( - canvas, device_scale_factor)); - paint->Draw(gfx_canvas.get()); - } -#else - // TODO(arthurhsu): following code has issues with i18n BiDi, see - // crbug.com/108599. - size_t text_byte_length = text.length() * sizeof(char16); - double text_width_in_points = SkScalarToDouble(paint.measureText( - text.c_str(), text_byte_length)); - SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, - horizontal_position, - vertical_position, offset_to_baseline, - text_width_in_points); - paint.setTextSize(SkDoubleToScalar( - paint.getTextSize() / webkit_scale_factor)); - canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), - paint); -#endif -} - PrintMsg_Print_Params CalculatePrintParamsForCss( WebKit::WebFrame* frame, int page_index, @@ -527,102 +411,72 @@ void PrintWebViewHelper::PrintHeaderAndFooter( static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice()); device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); -#if defined(USE_RENDER_TEXT) - scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); - // TODO(asvitkine): The below line is to workaround http://crbug.com/133548. - // Remove it when the underlying Skia bug has been fixed. - render_text->set_clip_to_display_rect(false); - gfx::FontList font_list( - gfx::Font(printing::kSettingHeaderFooterFontFamilyName, - printing::kSettingHeaderFooterFontSize)); - gfx::RenderText* paint = render_text.get(); -#else - SkPaint paint; - paint.setColor(SK_ColorBLACK); - paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); - paint.setTextSize(SkDoubleToScalar(printing::kSettingHeaderFooterFontSize)); - paint.setTypeface(SkTypeface::CreateFromName( - printing::kSettingHeaderFooterFontFamilyName, SkTypeface::kNormal)); -#endif - - // 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; - - // Used for height calculations. Note that the width may be undefined. - SkRect header_vertical_bounds; -#if defined(USE_RENDER_TEXT) - paint->SetFontList(font_list); - paint->SetText(header_text); - { - gfx::Rect rect(gfx::Point(), paint->GetStringSize()); - header_vertical_bounds = gfx::RectToSkRect(rect); - header_vertical_bounds.offset(0, -render_text->GetBaseline()); - } -#else - paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16), - &header_vertical_bounds, 0); -#endif - - double text_height = printing::kSettingHeaderFooterInterstice + - header_vertical_bounds.height(); - if (text_height <= page_layout.margin_top) { - PrintHeaderFooterText(date, canvas, paint, webkit_scale_factor, page_layout, - printing::LEFT, printing::TOP, - header_vertical_bounds.top()); - PrintHeaderFooterText(title, canvas, paint, webkit_scale_factor, - page_layout, printing::CENTER, printing::TOP, - header_vertical_bounds.top()); - } + SkAutoCanvasRestore auto_restore(canvas, true); + canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); - // 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; - - // Used for height calculations. Note that the width may be undefined. - SkRect footer_vertical_bounds; -#if defined(USE_RENDER_TEXT) - paint->SetFontList(font_list); - paint->SetText(footer_text); - { - gfx::Rect rect(gfx::Point(), paint->GetStringSize()); - footer_vertical_bounds = gfx::RectToSkRect(rect); - footer_vertical_bounds.offset(0, -paint->GetBaseline()); - } -#else - paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16), - &footer_vertical_bounds, 0); -#endif - - text_height = printing::kSettingHeaderFooterInterstice + - footer_vertical_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_vertical_bounds.bottom()); - PrintHeaderFooterText(url, canvas, paint, webkit_scale_factor, page_layout, - printing::LEFT, printing::BOTTOM, - footer_vertical_bounds.bottom()); - } + WebKit::WebSize page_size(page_layout.margin_left + page_layout.margin_right + + page_layout.content_width, + page_layout.margin_top + page_layout.margin_bottom + + page_layout.content_height); + + WebKit::WebView* web_view = WebKit::WebView::create(NULL); + web_view->settings()->setJavaScriptEnabled(true); + web_view->initializeMainFrame(NULL); + + WebKit::WebFrame* frame = web_view->mainFrame(); + + base::StringValue html( + ResourceBundle::GetSharedInstance().GetLocalizedString( + IDR_PRINT_PREVIEW_PAGE)); + // Load page with script to avoid async operations. + ExecuteScript(frame, kPageLoadScriptFormat, html); + + scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy()); + options->SetDouble("width", page_size.width); + options->SetDouble("height", page_size.height); + options->SetDouble("topMargin", page_layout.margin_top); + options->SetDouble("bottomMargin", page_layout.margin_bottom); + options->SetString("pageNumber", + StringPrintf("%d/%d", page_number, total_pages)); + + ExecuteScript(frame, kPageSetupScriptFormat, *options); + + WebKit::WebPrintParams webkit_params(page_size); + webkit_params.printerDPI = GetDPI(¶ms); + + frame->printBegin(webkit_params, WebKit::WebNode(), NULL); + frame->printPage(0, canvas); + frame->printEnd(); + + web_view->close(); device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); } +// static - Not anonymous so that platform implementations can use it. +float PrintWebViewHelper::RenderPageContent(WebKit::WebFrame* frame, + int page_number, + const gfx::Rect& canvas_area, + const gfx::Rect& content_area, + double scale_factor, + WebKit::WebCanvas* canvas) { + SkAutoCanvasRestore auto_restore(canvas, true); + if (content_area != canvas_area) { + canvas->translate((content_area.x() - canvas_area.x()) / scale_factor, + (content_area.y() - canvas_area.y()) / scale_factor); + SkRect clip_rect( + SkRect::MakeXYWH(content_area.origin().x() / scale_factor, + content_area.origin().y() / scale_factor, + content_area.size().width() / scale_factor, + content_area.size().height() / scale_factor)); + SkIRect clip_int_rect; + clip_rect.roundOut(&clip_int_rect); + SkRegion clip_region(clip_int_rect); + canvas->setClipRegion(clip_region); + } + return frame->printPage(page_number, canvas); +} + PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( const PrintMsg_Print_Params& print_params, WebKit::WebFrame* frame, diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h index 26678f4..6a0f78d 100644 --- a/chrome/renderer/print_web_view_helper.h +++ b/chrome/renderer/print_web_view_helper.h @@ -292,6 +292,17 @@ class PrintWebViewHelper printing::Metafile* metafile); #endif // defined(OS_WIN) + // Renders page contents from |frame| to |content_area| of |canvas|. + // |page_number| is zero-based. + // When method is called, canvas should be setup to draw to |canvas_area| + // with |scale_factor|. + static float RenderPageContent(WebKit::WebFrame* frame, + int page_number, + const gfx::Rect& canvas_area, + const gfx::Rect& content_area, + double scale_factor, + WebKit::WebCanvas* canvas); + // Helper methods ----------------------------------------------------------- bool CopyAndPrint(WebKit::WebFrame* web_frame); diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc index 4198377..d46c903 100644 --- a/chrome/renderer/print_web_view_helper_linux.cc +++ b/chrome/renderer/print_web_view_helper_linux.cc @@ -192,8 +192,11 @@ void PrintWebViewHelper::PrintPageInternal( gfx::Rect content_area; GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, &content_area); - SkDevice* device = metafile->StartPageForVectorCanvas( - page_size, content_area, scale_factor); + gfx::Rect canvas_area = + params.params.display_header_footer ? gfx::Rect(page_size) : content_area; + + SkDevice* device = metafile->StartPageForVectorCanvas(page_size, canvas_area, + scale_factor); if (!device) return; @@ -203,16 +206,18 @@ void PrintWebViewHelper::PrintPageInternal( canvas->unref(); // SkRefPtr and new both took a reference. printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); - frame->printPage(params.page_number, canvas.get()); if (params.params.display_header_footer) { // |page_number| is 0-based, so 1 is added. - // The scale factor on Linux is 1. + // TODO(vitalybuka) : why does it work only with 1.25? PrintHeaderAndFooter(canvas.get(), params.page_number + 1, print_preview_context_.total_page_count(), - scale_factor, page_layout_in_points, - *header_footer_info_, params.params); + scale_factor / 1.25, + page_layout_in_points, *header_footer_info_, + params.params); } + RenderPageContent(frame, params.page_number, canvas_area, content_area, + scale_factor, canvas.get()); // Done printing. Close the device context to retrieve the compiled metafile. if (!metafile->FinishPage()) diff --git a/chrome/renderer/print_web_view_helper_mac.mm b/chrome/renderer/print_web_view_helper_mac.mm index 7c62839..e74739b 100644 --- a/chrome/renderer/print_web_view_helper_mac.mm +++ b/chrome/renderer/print_web_view_helper_mac.mm @@ -112,9 +112,13 @@ void PrintWebViewHelper::RenderPage( *content_rect = content_area; scale_factor *= webkit_shrink_factor; + + gfx::Rect canvas_area = + params.display_header_footer ? gfx::Rect(*page_size) : content_area; + { SkDevice* device = metafile->StartPageForVectorCanvas( - *page_size, content_area, scale_factor); + *page_size, canvas_area, scale_factor); if (!device) return; @@ -125,15 +129,14 @@ void PrintWebViewHelper::RenderPage( skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); skia::SetIsPreviewMetafile(*canvas, is_preview); - frame->printPage(page_number, canvas_ptr); - if (print_pages_params_->params.display_header_footer) { - // |page_number| is 0-based, so 1 is added. PrintHeaderAndFooter(canvas_ptr, page_number + 1, print_preview_context_.total_page_count(), scale_factor, page_layout_in_points, *header_footer_info_, params); } + RenderPageContent(frame, page_number, canvas_area, content_area, + scale_factor, canvas_ptr); } // Done printing. Close the device context to retrieve the compiled metafile. diff --git a/chrome/renderer/print_web_view_helper_win.cc b/chrome/renderer/print_web_view_helper_win.cc index bdc702b..d5868df 100644 --- a/chrome/renderer/print_web_view_helper_win.cc +++ b/chrome/renderer/print_web_view_helper_win.cc @@ -169,29 +169,36 @@ void PrintWebViewHelper::RenderPage( } float webkit_page_shrink_factor = frame->getPrintPageShrink(page_number); + float scale_factor = css_scale_factor * webkit_page_shrink_factor; + + gfx::Rect canvas_area = + params.display_header_footer ? gfx::Rect(page_size) : content_area; + SkDevice* device = metafile->StartPageForVectorCanvas( - page_size, content_area, css_scale_factor * webkit_page_shrink_factor); + page_size, canvas_area, scale_factor); DCHECK(device); // The printPage method may take a reference to the canvas we pass down, so it // can't be a stack object. SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); canvas->unref(); // SkRefPtr and new both took a reference. + if (is_preview) { printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); skia::SetIsPreviewMetafile(*canvas, is_preview); } - float webkit_scale_factor = frame->printPage(page_number, canvas.get()); - if (params.display_header_footer) { // |page_number| is 0-based, so 1 is added. PrintHeaderAndFooter(canvas.get(), page_number + 1, - print_preview_context_.total_page_count(), - css_scale_factor * webkit_page_shrink_factor, - page_layout_in_points, *header_footer_info_, params); + print_preview_context_.total_page_count(), scale_factor, + page_layout_in_points, *header_footer_info_, params); } + float webkit_scale_factor = RenderPageContent(frame, page_number, canvas_area, + content_area, scale_factor, + canvas.get()); + if (*actual_shrink <= 0 || webkit_scale_factor <= 0) { NOTREACHED() << "Printing page " << page_number << " failed."; } else { diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index a93da56..8cd6208 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -55,22 +55,6 @@ const char kSettingGenerateDraftData[] = "generateDraftData"; // Option to print headers and Footers: true if selected, false if not. const char kSettingHeaderFooterEnabled[] = "headerFooterEnabled"; -// Default character spacing for text while printing headers and footers. -// (For CoreGraphics only). -const int kSettingHeaderFooterCharacterSpacing = 0; - -// Default font family name for printing the headers and footers. -const char kSettingHeaderFooterFontFamilyName[] = "sans"; - -// Default font name for printing the headers and footers. -const char kSettingHeaderFooterFontName[] = "Helvetica"; - -// Default font size for printing the headers and footers. -const int kSettingHeaderFooterFontSize = 8; - -// Number of horizontal regions for headers and footers. -const float kSettingHeaderFooterHorizontalRegions = 3; - // Interstice or gap between different header footer components. Hardcoded to // about 0.5cm, match the value in PrintSettings::SetPrinterPrintableArea. const float kSettingHeaderFooterInterstice = 14.2f; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index d9a6006..88bf125 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -26,11 +26,6 @@ PRINTING_EXPORT extern const char kSettingDuplexMode[]; PRINTING_EXPORT extern const char kSettingFitToPageEnabled[]; PRINTING_EXPORT extern const char kSettingGenerateDraftData[]; PRINTING_EXPORT extern const char kSettingHeaderFooterEnabled[]; -PRINTING_EXPORT extern const int kSettingHeaderFooterCharacterSpacing; -PRINTING_EXPORT extern const char kSettingHeaderFooterFontFamilyName[]; -PRINTING_EXPORT extern const char kSettingHeaderFooterFontName[]; -PRINTING_EXPORT extern const int kSettingHeaderFooterFontSize; -PRINTING_EXPORT extern const float kSettingHeaderFooterHorizontalRegions; PRINTING_EXPORT extern const float kSettingHeaderFooterInterstice; PRINTING_EXPORT extern const char kSettingHeaderFooterDate[]; PRINTING_EXPORT extern const char kSettingHeaderFooterTitle[]; diff --git a/printing/print_settings_initializer.cc b/printing/print_settings_initializer.cc index e82bbfc..c49a556 100644 --- a/printing/print_settings_initializer.cc +++ b/printing/print_settings_initializer.cc @@ -35,34 +35,15 @@ void PrintSettingsInitializer::InitHeaderFooterStrings( string16 date = base::TimeFormatShortDateNumeric(base::Time::Now()); string16 title; - std::string url; + string16 url; if (!job_settings.GetString(kSettingHeaderFooterTitle, &title) || !job_settings.GetString(kSettingHeaderFooterURL, &url)) { NOTREACHED(); } - gfx::Font font( - kSettingHeaderFooterFontName, - ceil(ConvertPointsToPixelDouble(kSettingHeaderFooterFontSize))); - double segment_width = GetHeaderFooterSegmentWidth(ConvertUnitDouble( - print_settings->page_setup_device_units().physical_size().width(), - print_settings->device_units_per_inch(), kPixelsPerInch)); - date = ui::ElideText(date, font, segment_width, ui::ELIDE_AT_END); print_settings->date = date; - - // Calculate the available title width. If the date string is not long - // enough, increase the available space for the title. - // Assumes there is no header text to RIGHT of title. - double date_width = font.GetStringWidth(date); - double max_title_width = std::min(2 * segment_width, - 2 * (segment_width - date_width) + - segment_width); - print_settings->title = - ui::ElideText(title, font, max_title_width, ui::ELIDE_AT_END); - - double max_url_width = 2 * segment_width; - GURL gurl(url); - print_settings->url = ui::ElideUrl(gurl, font, max_url_width, std::string()); + print_settings->title = title; + print_settings->url = ui::ElideUrl(GURL(url), gfx::Font(), 0, std::string()); } } // namespace printing diff --git a/printing/units.cc b/printing/units.cc index f1ec616..dcee9ea 100644 --- a/printing/units.cc +++ b/printing/units.cc @@ -52,13 +52,4 @@ double ConvertPointsToPixelDouble(double points) { return ConvertUnitDouble(points, kPointsPerInch, kPixelsPerInch); } -double GetHeaderFooterSegmentWidth(double page_width) { - // Interstice is left at both ends of the page as well as between - // each region, so 1 is added. - double total_interstice_width = (kSettingHeaderFooterHorizontalRegions + 1) * - kSettingHeaderFooterInterstice; - return (page_width - total_interstice_width) / - kSettingHeaderFooterHorizontalRegions; -} - } // namespace printing diff --git a/printing/units.h b/printing/units.h index 43f68ee..8f02832 100644 --- a/printing/units.h +++ b/printing/units.h @@ -42,10 +42,6 @@ PRINTING_EXPORT double ConvertPixelsToPointDouble(double pixels); // Converts from 1 point to 1 pixel using doubles. double ConvertPointsToPixelDouble(double points); -// Splits the horizontal width equally into segments with an interstice -// between each segment. Returns the width of a segment. -PRINTING_EXPORT double GetHeaderFooterSegmentWidth(double page_width); - } // namespace printing #endif // PRINTING_UNITS_H_ |