summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-01 07:04:05 +0000
committerhamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-01 07:04:05 +0000
commit40c7cfe39957248d38d8c376fbfc7d332c796741 (patch)
tree1e9cbc2bda99a4b4bc3286504dbca59a7542fb70
parent829c9bbc87035a0e6a5606ff7654b761a2d9d0bf (diff)
downloadchromium_src-40c7cfe39957248d38d8c376fbfc7d332c796741.zip
chromium_src-40c7cfe39957248d38d8c376fbfc7d332c796741.tar.gz
chromium_src-40c7cfe39957248d38d8c376fbfc7d332c796741.tar.bz2
Implement limited paged media support for mac.
BUG=47277 TEST=none Review URL: http://codereview.chromium.org/2876020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51347 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/printing/print_view_manager.cc4
-rw-r--r--chrome/renderer/print_web_view_helper.cc27
-rw-r--r--chrome/renderer/print_web_view_helper.h3
-rw-r--r--chrome/renderer/print_web_view_helper_mac.mm34
-rw-r--r--printing/page_overlays_unittest.cc3
-rw-r--r--printing/printed_document.cc9
-rw-r--r--printing/printed_document.h3
-rw-r--r--printing/printed_document_mac.cc16
-rw-r--r--printing/printed_page.cc6
-rw-r--r--printing/printed_page.h7
10 files changed, 95 insertions, 17 deletions
diff --git a/chrome/browser/printing/print_view_manager.cc b/chrome/browser/printing/print_view_manager.cc
index bb6bc35..2795a3d 100644
--- a/chrome/browser/printing/print_view_manager.cc
+++ b/chrome/browser/printing/print_view_manager.cc
@@ -133,7 +133,9 @@ void PrintViewManager::DidPrintPage(
// Update the rendered document. It will send notifications to the listener.
document->SetPage(params.page_number,
metafile.release(),
- params.actual_shrink);
+ params.actual_shrink,
+ params.page_size,
+ params.content_area);
#endif
ShouldQuitFromInnerMessageLoop();
}
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc
index d229df1..fb0ece7 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -44,12 +44,12 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
dpi = printing::kPointsPerInch;
#endif // defined(OS_MACOSX)
print_canvas_size_.set_width(
- printing::ConvertUnit(print_params.printable_size.width(), dpi,
- print_params.desired_dpi));
+ ConvertUnit(print_params.printable_size.width(), dpi,
+ print_params.desired_dpi));
print_canvas_size_.set_height(
- printing::ConvertUnit(print_params.printable_size.height(), dpi,
- print_params.desired_dpi));
+ ConvertUnit(print_params.printable_size.height(), dpi,
+ print_params.desired_dpi));
// Layout page according to printer page size. Since WebKit shrinks the
// size of the page automatically (from 125% to 200%) we trick it to
@@ -131,6 +131,8 @@ void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) {
return;
}
+ UpdatePrintableSizeInPrintParameters(frame, &default_settings);
+
// Continue only if the settings are valid.
if (default_settings.dpi && default_settings.document_cookie) {
int expected_pages_count = 0;
@@ -173,6 +175,8 @@ void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) {
if (Send(msg)) {
msg = NULL;
+ UpdatePrintableSizeInPrintParameters(frame, &print_settings.params);
+
// If the settings are invalid, early quit.
if (print_settings.params.dpi &&
print_settings.params.document_cookie) {
@@ -430,3 +434,18 @@ void PrintWebViewHelper::GetPageSizeAndMarginsInPoints(
*margin_left_in_points =
ConvertPixelsToPointDouble(margin_left_in_pixels);
}
+
+void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters(
+ WebFrame* frame, ViewMsg_Print_Params* params) {
+#if defined(OS_MACOSX)
+ double content_width_in_points;
+ double content_height_in_points;
+ PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params,
+ &content_width_in_points,
+ &content_height_in_points,
+ NULL, NULL, NULL, NULL);
+ params->printable_size = gfx::Size(
+ static_cast<int>(content_width_in_points),
+ static_cast<int>(content_height_in_points));
+#endif
+}
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index 4805de0..273299f 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -136,6 +136,9 @@ class PrintWebViewHelper : public WebKit::WebViewClient,
double* margin_bottom_in_points,
double* margin_left_in_points);
+ void UpdatePrintableSizeInPrintParameters(WebKit::WebFrame* frame,
+ ViewMsg_Print_Params* params);
+
RenderView* render_view_;
WebKit::WebView* print_web_view_;
scoped_ptr<ViewMsg_PrintPages_Params> print_pages_params_;
diff --git a/chrome/renderer/print_web_view_helper_mac.mm b/chrome/renderer/print_web_view_helper_mac.mm
index d6d91b4..b94221e 100644
--- a/chrome/renderer/print_web_view_helper_mac.mm
+++ b/chrome/renderer/print_web_view_helper_mac.mm
@@ -16,9 +16,13 @@
#include "printing/native_metafile.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCanvas.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebRect.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebSize.h"
using WebKit::WebFrame;
using WebKit::WebCanvas;
+using WebKit::WebRect;
+using WebKit::WebSize;
void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
const gfx::Size& canvas_size,
@@ -27,7 +31,24 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
CGContextRef context = metafile.Init();
float scale_factor = frame->getPrintPageShrink(params.page_number);
- metafile.StartPage(canvas_size.width(), canvas_size.height(), scale_factor);
+ 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;
+ GetPageSizeAndMarginsInPoints(frame,
+ params.page_number,
+ params.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);
+ metafile.StartPage(content_width_in_points,
+ content_height_in_points,
+ scale_factor);
// printPage can create autoreleased references to |canvas|. PDF contexts
// don't write all their data until they are destroyed, so we need to make
@@ -47,6 +68,17 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
page_params.actual_shrink = scale_factor;
base::SharedMemory shared_buf;
+ page_params.page_size = gfx::Size(
+ static_cast<int>(content_width_in_points
+ + margin_left_in_points + margin_right_in_points),
+ static_cast<int>(content_height_in_points
+ + margin_top_in_points + margin_bottom_in_points));
+ page_params.content_area = gfx::Rect(
+ static_cast<int>(margin_left_in_points),
+ static_cast<int>(margin_top_in_points),
+ static_cast<int>(content_width_in_points),
+ static_cast<int>(content_height_in_points));
+
// Ask the browser to create the shared memory for us.
uint32 buf_size = metafile.GetDataSize();
base::SharedMemoryHandle shared_mem_handle;
diff --git a/printing/page_overlays_unittest.cc b/printing/page_overlays_unittest.cc
index bad78de..8a19dcd 100644
--- a/printing/page_overlays_unittest.cc
+++ b/printing/page_overlays_unittest.cc
@@ -57,8 +57,9 @@ TEST_F(PageOverlaysTest, StringConversion) {
new printing::PrintedDocument(settings, &source, cookie));
doc->set_page_count(2);
gfx::Size page_size(100, 100);
+ gfx::Rect page_content_area(5, 5, 90, 90);
scoped_refptr<printing::PrintedPage> page(
- new printing::PrintedPage(1, NULL, page_size));
+ new printing::PrintedPage(1, NULL, page_size, page_content_area));
std::wstring input;
std::wstring out;
diff --git a/printing/printed_document.cc b/printing/printed_document.cc
index f49e1e6..333f1d87 100644
--- a/printing/printed_document.cc
+++ b/printing/printed_document.cc
@@ -70,13 +70,16 @@ PrintedDocument::~PrintedDocument() {
void PrintedDocument::SetPage(int page_number,
NativeMetafile* metafile,
- double shrink) {
+ double shrink,
+ const gfx::Size& paper_size,
+ const gfx::Rect& page_rect) {
// Notice the page_number + 1, the reason is that this is the value that will
// be shown. Users dislike 0-based counting.
scoped_refptr<PrintedPage> page(
new PrintedPage(page_number + 1,
- metafile,
- immutable_.settings_.page_setup_device_units().physical_size()));
+ metafile,
+ paper_size,
+ page_rect));
{
AutoLock lock(lock_);
mutable_.pages_[page_number] = page;
diff --git a/printing/printed_document.h b/printing/printed_document.h
index dbf4c3d..049add0 100644
--- a/printing/printed_document.h
+++ b/printing/printed_document.h
@@ -42,7 +42,8 @@ class PrintedDocument : public base::RefCountedThreadSafe<PrintedDocument> {
// Sets a page's data. 0-based. Takes metafile ownership.
// Note: locks for a short amount of time.
- void SetPage(int page_number, NativeMetafile* metafile, double shrink);
+ void SetPage(int page_number, NativeMetafile* metafile, double shrink,
+ const gfx::Size& paper_size, const gfx::Rect& page_rect);
// Retrieves a page. If the page is not available right now, it
// requests to have this page be rendered and returns false.
diff --git a/printing/printed_document_mac.cc b/printing/printed_document_mac.cc
index a3ea8c4..3f5d75d 100644
--- a/printing/printed_document_mac.cc
+++ b/printing/printed_document_mac.cc
@@ -26,13 +26,23 @@ void PrintedDocument::RenderPrintedPage(
const printing::PageSetup& page_setup(
immutable_.settings_.page_setup_device_units());
- CGRect target_rect = page_setup.content_area().ToCGRect();
+ gfx::Rect target_rect = page.page_content_rect();
+ const gfx::Rect& physical_size = page_setup.physical_size();
+ // http://dev.w3.org/csswg/css3-page/#positioning-page-box
+ if (physical_size.width() > page.page_size().width()) {
+ int diff = physical_size.width() - page.page_size().width();
+ target_rect.set_x(target_rect.x() + diff / 2);
+ }
+ if (physical_size.height() > page.page_size().height()) {
+ int diff = physical_size.height() - page.page_size().height();
+ target_rect.set_y(target_rect.y() + diff / 2);
+ }
const printing::NativeMetafile* metafile = page.native_metafile();
// Each NativeMetafile is a one-page PDF, and pages use 1-based indexing.
const int page_number = 1;
- metafile->RenderPage(page_number, context, target_rect, true, false, false,
- false);
+ metafile->RenderPage(page_number, context, target_rect.ToCGRect(),
+ true, false, false, false);
// TODO(stuartmorgan): Print the header and footer.
}
diff --git a/printing/printed_page.cc b/printing/printed_page.cc
index 441690a..242adb2 100644
--- a/printing/printed_page.cc
+++ b/printing/printed_page.cc
@@ -8,10 +8,12 @@ namespace printing {
PrintedPage::PrintedPage(int page_number,
NativeMetafile* native_metafile,
- const gfx::Size& page_size)
+ const gfx::Size& page_size,
+ const gfx::Rect& page_content_rect)
: page_number_(page_number),
native_metafile_(native_metafile),
- page_size_(page_size) {
+ page_size_(page_size),
+ page_content_rect_(page_content_rect) {
}
PrintedPage::~PrintedPage() {
diff --git a/printing/printed_page.h b/printing/printed_page.h
index f7d2f7d..83425ba 100644
--- a/printing/printed_page.h
+++ b/printing/printed_page.h
@@ -23,12 +23,14 @@ class PrintedPage : public base::RefCountedThreadSafe<PrintedPage> {
public:
PrintedPage(int page_number,
NativeMetafile* native_metafile,
- const gfx::Size& page_size);
+ const gfx::Size& page_size,
+ const gfx::Rect& page_content_rect);
// Getters
int page_number() const { return page_number_; }
const NativeMetafile* native_metafile() const;
const gfx::Size& page_size() const { return page_size_; }
+ const gfx::Rect& page_content_rect() const { return page_content_rect_; }
private:
friend class base::RefCountedThreadSafe<PrintedPage>;
@@ -45,6 +47,9 @@ class PrintedPage : public base::RefCountedThreadSafe<PrintedPage> {
// job.
const gfx::Size page_size_;
+ // The printable area of the page.
+ const gfx::Rect page_content_rect_;
+
DISALLOW_COPY_AND_ASSIGN(PrintedPage);
};