Linux: fix printing somewhat.
BUG=29148 TEST=prints documents greater than one page (to real printers) Review URL: http://codereview.chromium.org/1520014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44161 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
@@ -103,38 +103,19 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
|
|||||||
const gfx::Size& canvas_size,
|
const gfx::Size& canvas_size,
|
||||||
WebFrame* frame,
|
WebFrame* frame,
|
||||||
printing::NativeMetafile* metafile) {
|
printing::NativeMetafile* metafile) {
|
||||||
// Since WebKit extends the page width depending on the magical shrink
|
cairo_t* cairo_context =
|
||||||
// factor we make sure the canvas covers the worst case scenario
|
metafile->StartPage(canvas_size.width(), canvas_size.height());
|
||||||
// (x2.0 currently). PrintContext will then set the correct clipping region.
|
if (!cairo_context)
|
||||||
int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink);
|
|
||||||
int size_y = static_cast<int>(canvas_size.height() *
|
|
||||||
params.params.max_shrink);
|
|
||||||
// Calculate the dpi adjustment.
|
|
||||||
float shrink = static_cast<float>(canvas_size.width()) /
|
|
||||||
params.params.printable_size.width();
|
|
||||||
|
|
||||||
cairo_t* cairo_context = metafile->StartPage(size_x, size_y);
|
|
||||||
if (!cairo_context) {
|
|
||||||
// TODO(myhuang): We should handle such kind of error further!
|
|
||||||
// We already have had DLOG(ERROR) in NativeMetafile::StartPage(),
|
|
||||||
// log the error here, too?
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
skia::VectorCanvas canvas(cairo_context, size_x, size_y);
|
skia::VectorCanvas canvas(cairo_context,
|
||||||
float webkit_shrink = frame->printPage(params.page_number, &canvas);
|
canvas_size.width(), canvas_size.height());
|
||||||
if (webkit_shrink <= 0) {
|
frame->printPage(params.page_number, &canvas);
|
||||||
NOTREACHED() << "Printing page " << params.page_number << " failed.";
|
|
||||||
} else {
|
|
||||||
// Update the dpi adjustment with the "page shrink" calculated in webkit.
|
|
||||||
shrink /= webkit_shrink;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(myhuang): We should handle transformation for paper margins.
|
// TODO(myhuang): We should handle transformation for paper margins.
|
||||||
// TODO(myhuang): We should render the header and the footer.
|
// TODO(myhuang): We should render the header and the footer.
|
||||||
|
|
||||||
// Done printing. Close the device context to retrieve the compiled metafile.
|
// Done printing. Close the device context to retrieve the compiled metafile.
|
||||||
if (!metafile->FinishPage(shrink)) {
|
if (!metafile->FinishPage())
|
||||||
NOTREACHED() << "metafile failed";
|
NOTREACHED() << "metafile failed";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
@@ -18,6 +18,13 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
// The hardcoded margins, in points. These values are based on 72 dpi,
|
||||||
|
// with approximately 0.25 margins on top, left, and right, and 0.56 on bottom.
|
||||||
|
const double kTopMargin = 0.25 * 72.0;
|
||||||
|
const double kBottomMargin = 0.56 * 72.0;
|
||||||
|
const double kLeftMargin = 0.25 * 72.0;
|
||||||
|
const double kRightMargin = 0.25 * 72.0;
|
||||||
|
|
||||||
// Tests if |surface| is valid.
|
// Tests if |surface| is valid.
|
||||||
bool IsSurfaceValid(cairo_surface_t* surface) {
|
bool IsSurfaceValid(cairo_surface_t* surface) {
|
||||||
return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS;
|
return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS;
|
||||||
@@ -65,8 +72,7 @@ namespace printing {
|
|||||||
|
|
||||||
PdfPsMetafile::PdfPsMetafile(const FileFormat& format)
|
PdfPsMetafile::PdfPsMetafile(const FileFormat& format)
|
||||||
: format_(format),
|
: format_(format),
|
||||||
surface_(NULL), context_(NULL),
|
surface_(NULL), context_(NULL) {
|
||||||
page_surface_(NULL), page_context_(NULL) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PdfPsMetafile::~PdfPsMetafile() {
|
PdfPsMetafile::~PdfPsMetafile() {
|
||||||
@@ -76,31 +82,33 @@ PdfPsMetafile::~PdfPsMetafile() {
|
|||||||
|
|
||||||
bool PdfPsMetafile::Init() {
|
bool PdfPsMetafile::Init() {
|
||||||
// We need to check at least these two members to ensure Init() has not been
|
// We need to check at least these two members to ensure Init() has not been
|
||||||
// called before. Passing these two checks also implies that surface_,
|
// called before.
|
||||||
// page_surface_, and page_context_ are NULL, and current_page_ is empty.
|
|
||||||
DCHECK(!context_);
|
DCHECK(!context_);
|
||||||
DCHECK(all_pages_.empty());
|
DCHECK(data_.empty());
|
||||||
|
|
||||||
// Creates an 1 by 1 Cairo surface for entire PDF/PS file.
|
// Creates an 1 by 1 Cairo surface for entire PDF/PS file.
|
||||||
// The size for each page will be overwritten later in StartPage().
|
// The size for each page will be overwritten later in StartPage().
|
||||||
switch (format_) {
|
switch (format_) {
|
||||||
case PDF: {
|
case PDF:
|
||||||
surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
|
surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
|
||||||
&all_pages_, 1, 1);
|
&data_, 1, 1);
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case PS: {
|
case PS:
|
||||||
surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
|
surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
|
||||||
&all_pages_, 1, 1);
|
&data_, 1, 1);
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
NOTREACHED();
|
NOTREACHED();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't let WebKit draw over the margins.
|
||||||
|
cairo_surface_set_device_offset(surface_,
|
||||||
|
static_cast<int>(kLeftMargin),
|
||||||
|
static_cast<int>(kTopMargin));
|
||||||
|
|
||||||
// Cairo always returns a valid pointer.
|
// Cairo always returns a valid pointer.
|
||||||
// Hence, we have to check if it points to a "nil" object.
|
// Hence, we have to check if it points to a "nil" object.
|
||||||
if (!IsSurfaceValid(surface_)) {
|
if (!IsSurfaceValid(surface_)) {
|
||||||
@@ -123,16 +131,14 @@ bool PdfPsMetafile::Init() {
|
|||||||
|
|
||||||
bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) {
|
bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) {
|
||||||
// We need to check at least these two members to ensure Init() has not been
|
// We need to check at least these two members to ensure Init() has not been
|
||||||
// called before. Passing these two checks also implies that surface_,
|
// called before
|
||||||
// page_surface_, and page_context_ are NULL, and current_page_ is empty.
|
|
||||||
DCHECK(!context_);
|
DCHECK(!context_);
|
||||||
DCHECK(all_pages_.empty());
|
DCHECK(data_.empty());
|
||||||
|
|
||||||
if (src_buffer == NULL || src_buffer_size == 0) {
|
if (src_buffer == NULL || src_buffer_size == 0)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
all_pages_ = std::string(reinterpret_cast<const char*>(src_buffer),
|
data_ = std::string(reinterpret_cast<const char*>(src_buffer),
|
||||||
src_buffer_size);
|
src_buffer_size);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -144,29 +150,22 @@ cairo_t* PdfPsMetafile::StartPage(double width_in_points,
|
|||||||
DCHECK(IsContextValid(context_));
|
DCHECK(IsContextValid(context_));
|
||||||
// Passing this check implies page_surface_ is NULL, and current_page_ is
|
// Passing this check implies page_surface_ is NULL, and current_page_ is
|
||||||
// empty.
|
// empty.
|
||||||
DCHECK(!page_context_);
|
|
||||||
DCHECK_GT(width_in_points, 0.);
|
DCHECK_GT(width_in_points, 0.);
|
||||||
DCHECK_GT(height_in_points, 0.);
|
DCHECK_GT(height_in_points, 0.);
|
||||||
|
|
||||||
// Creates a target surface for the new page.
|
// We build in extra room for the margins. The Cairo PDF backend will scale
|
||||||
// Cairo 1.6.0 does NOT allow the first argument be NULL,
|
// the output to fit a page.
|
||||||
// but some newer versions do support NULL pointer.
|
double width = width_in_points + kLeftMargin + kRightMargin;
|
||||||
switch (format_) {
|
double height = height_in_points + kTopMargin + kBottomMargin;
|
||||||
case PDF: {
|
|
||||||
page_surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
|
|
||||||
¤t_page_,
|
|
||||||
width_in_points,
|
|
||||||
height_in_points);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PS: {
|
switch (format_) {
|
||||||
page_surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
|
case PDF:
|
||||||
¤t_page_,
|
cairo_pdf_surface_set_size(surface_, width, height);
|
||||||
width_in_points,
|
break;
|
||||||
height_in_points);
|
|
||||||
}
|
case PS:
|
||||||
break;
|
cairo_ps_surface_set_size(surface_, width, height);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
NOTREACHED();
|
NOTREACHED();
|
||||||
@@ -174,134 +173,25 @@ cairo_t* PdfPsMetafile::StartPage(double width_in_points,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cairo always returns a valid pointer.
|
return context_;
|
||||||
// Hence, we have to check if it points to a "nil" object.
|
|
||||||
if (!IsSurfaceValid(page_surface_)) {
|
|
||||||
DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!";
|
|
||||||
CleanUpAll();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a context.
|
|
||||||
page_context_ = cairo_create(page_surface_);
|
|
||||||
if (!IsContextValid(page_context_)) {
|
|
||||||
DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!";
|
|
||||||
CleanUpAll();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return page_context_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PdfPsMetafile::FinishPage(float shrink) {
|
bool PdfPsMetafile::FinishPage() {
|
||||||
DCHECK(IsSurfaceValid(surface_));
|
DCHECK(IsSurfaceValid(surface_));
|
||||||
DCHECK(IsContextValid(context_));
|
DCHECK(IsContextValid(context_));
|
||||||
DCHECK(IsSurfaceValid(page_surface_));
|
|
||||||
DCHECK(IsContextValid(page_context_));
|
|
||||||
DCHECK_GT(shrink, 0);
|
|
||||||
|
|
||||||
// Flushes all rendering for current page.
|
// Flushes all rendering for current page.
|
||||||
cairo_surface_flush(page_surface_);
|
|
||||||
|
|
||||||
// TODO(myhuang): Use real page settings.
|
|
||||||
// We hard-coded page settings here for testing purpose.
|
|
||||||
// The paper size is US Letter (8.5 in. by 11 in.).
|
|
||||||
// The default margins are:
|
|
||||||
// Left = 0.25 in.
|
|
||||||
// Right = 0.25 in.
|
|
||||||
// Top = 0.25 in.
|
|
||||||
// Bottom = 0.56 in.
|
|
||||||
const double kDPI = 72.0; // Dots (points) per inch.
|
|
||||||
const double kWidthInInch = 8.5;
|
|
||||||
const double kHeightInInch = 11.0;
|
|
||||||
const double kWidthInPoint = kWidthInInch * kDPI;
|
|
||||||
const double kHeightInPoint = kHeightInInch * kDPI;
|
|
||||||
switch (format_) {
|
|
||||||
case PDF: {
|
|
||||||
cairo_pdf_surface_set_size(surface_, kWidthInPoint, kHeightInPoint);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PS: {
|
|
||||||
cairo_ps_surface_set_size(surface_, kWidthInPoint, kHeightInPoint);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
NOTREACHED();
|
|
||||||
CleanUpAll();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks if our surface is still valid after resizing.
|
|
||||||
if (!IsSurfaceValid(surface_)) {
|
|
||||||
DLOG(ERROR) << "Cannot resize Cairo surface for PdfPsMetafile!";
|
|
||||||
CleanUpAll();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Saves context's states.
|
|
||||||
cairo_save(context_);
|
|
||||||
// Copies current page onto the surface of final result.
|
|
||||||
// Margins are done by coordinates transformation.
|
|
||||||
// Please NOTE that we have to call cairo_scale() before we call
|
|
||||||
// cairo_set_source_surface().
|
|
||||||
const double scale_factor = 1. / shrink;
|
|
||||||
cairo_scale(context_, scale_factor, scale_factor);
|
|
||||||
const double kLeftMarginInInch = 0.25;
|
|
||||||
const double kTopMarginInInch = 0.25;
|
|
||||||
const double kLeftMarginInPoint = kLeftMarginInInch * kDPI;
|
|
||||||
const double kTopMarginInPoint = kTopMarginInInch * kDPI;
|
|
||||||
const double kScaledLeftMarginInPoint = kLeftMarginInPoint * shrink;
|
|
||||||
const double kScaledTopMarginInPoint = kTopMarginInPoint * shrink;
|
|
||||||
cairo_set_source_surface(context_,
|
|
||||||
page_surface_,
|
|
||||||
kScaledLeftMarginInPoint,
|
|
||||||
kScaledTopMarginInPoint);
|
|
||||||
// In Cairo 1.6.0, if we use the following API, either the renderer will
|
|
||||||
// crash, or we will get an empty page. This might be a bug in Cairo.
|
|
||||||
// cairo_set_operator(context_, CAIRO_OPERATOR_SOURCE);
|
|
||||||
const double kRightMarginInInch = 0.25;
|
|
||||||
const double kBottomMarginInInch = 0.56;
|
|
||||||
const double kPrintableWidthInInch =
|
|
||||||
kWidthInInch - kLeftMarginInInch - kRightMarginInInch;
|
|
||||||
const double kPrintableHeightInInch =
|
|
||||||
kHeightInInch - kTopMarginInInch - kBottomMarginInInch;
|
|
||||||
const double kScaledPrintableWidthInPoint =
|
|
||||||
kPrintableWidthInInch * kDPI * shrink;
|
|
||||||
const double kScaledPrintableHeightInPoint =
|
|
||||||
kPrintableHeightInInch * kDPI * shrink;
|
|
||||||
cairo_rectangle(context_,
|
|
||||||
kScaledLeftMarginInPoint,
|
|
||||||
kScaledTopMarginInPoint,
|
|
||||||
kScaledPrintableWidthInPoint,
|
|
||||||
kScaledPrintableHeightInPoint);
|
|
||||||
cairo_fill(context_);
|
|
||||||
|
|
||||||
// Finishes the duplication of current page.
|
|
||||||
cairo_show_page(context_);
|
|
||||||
cairo_surface_flush(surface_);
|
cairo_surface_flush(surface_);
|
||||||
|
cairo_show_page(context_);
|
||||||
// Destroys resources for current page.
|
|
||||||
CleanUpContext(&page_context_);
|
|
||||||
CleanUpSurface(&page_surface_);
|
|
||||||
current_page_.clear();
|
|
||||||
|
|
||||||
// Restores context's states.
|
|
||||||
cairo_restore(context_);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PdfPsMetafile::Close() {
|
void PdfPsMetafile::Close() {
|
||||||
DCHECK(IsSurfaceValid(surface_));
|
DCHECK(IsSurfaceValid(surface_));
|
||||||
DCHECK(IsContextValid(context_));
|
DCHECK(IsContextValid(context_));
|
||||||
// Passing this check implies page_surface_ is NULL, and current_page_ is
|
|
||||||
// empty.
|
|
||||||
DCHECK(!page_context_);
|
|
||||||
|
|
||||||
cairo_surface_finish(surface_);
|
cairo_surface_finish(surface_);
|
||||||
DCHECK(!all_pages_.empty()); // Make sure we did get something.
|
DCHECK(!data_.empty()); // Make sure we did get something.
|
||||||
|
|
||||||
CleanUpContext(&context_);
|
CleanUpContext(&context_);
|
||||||
CleanUpSurface(&surface_);
|
CleanUpSurface(&surface_);
|
||||||
@@ -309,42 +199,26 @@ void PdfPsMetafile::Close() {
|
|||||||
|
|
||||||
uint32 PdfPsMetafile::GetDataSize() const {
|
uint32 PdfPsMetafile::GetDataSize() const {
|
||||||
// We need to check at least these two members to ensure that either Init()
|
// We need to check at least these two members to ensure that either Init()
|
||||||
// has been called to initialize |all_pages_|, or metafile has been closed.
|
// has been called to initialize |data_|, or metafile has been closed.
|
||||||
// Passing these two checks also implies that surface_, page_surface_, and
|
|
||||||
// page_context_ are NULL, and current_page_ is empty.
|
|
||||||
DCHECK(!context_);
|
DCHECK(!context_);
|
||||||
DCHECK(!all_pages_.empty());
|
DCHECK(!data_.empty());
|
||||||
|
|
||||||
return all_pages_.size();
|
return data_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const {
|
bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const {
|
||||||
DCHECK(dst_buffer);
|
DCHECK(dst_buffer);
|
||||||
DCHECK_GT(dst_buffer_size, 0u);
|
DCHECK_GT(dst_buffer_size, 0u);
|
||||||
// We need to check at least these two members to ensure that either Init()
|
memcpy(dst_buffer, data_.data(), dst_buffer_size);
|
||||||
// has been called to initialize |all_pages_|, or metafile has been closed.
|
|
||||||
// Passing these two checks also implies that surface_, page_surface_, and
|
|
||||||
// page_context_ are NULL, and current_page_ is empty.
|
|
||||||
DCHECK(!context_);
|
|
||||||
DCHECK(!all_pages_.empty());
|
|
||||||
|
|
||||||
uint32 data_size = GetDataSize();
|
|
||||||
if (dst_buffer_size > data_size) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(dst_buffer, all_pages_.data(), dst_buffer_size);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const {
|
bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const {
|
||||||
// We need to check at least these two members to ensure that either Init()
|
// We need to check at least these two members to ensure that either Init()
|
||||||
// has been called to initialize |all_pages_|, or metafile has been closed.
|
// has been called to initialize |data_|, or metafile has been closed.
|
||||||
// Passing these two checks also implies that surface_, page_surface_, and
|
|
||||||
// page_context_ are NULL, and current_page_ is empty.
|
|
||||||
DCHECK(!context_);
|
DCHECK(!context_);
|
||||||
DCHECK(!all_pages_.empty());
|
DCHECK(!data_.empty());
|
||||||
|
|
||||||
if (fd.fd < 0) {
|
if (fd.fd < 0) {
|
||||||
DLOG(ERROR) << "Invalid file descriptor!";
|
DLOG(ERROR) << "Invalid file descriptor!";
|
||||||
@@ -352,7 +226,7 @@ bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool success = true;
|
bool success = true;
|
||||||
if (file_util::WriteFileDescriptor(fd.fd, all_pages_.data(),
|
if (file_util::WriteFileDescriptor(fd.fd, data_.data(),
|
||||||
GetDataSize()) < 0) {
|
GetDataSize()) < 0) {
|
||||||
DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
|
DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
|
||||||
success = false;
|
success = false;
|
||||||
@@ -371,10 +245,7 @@ bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const {
|
|||||||
void PdfPsMetafile::CleanUpAll() {
|
void PdfPsMetafile::CleanUpAll() {
|
||||||
CleanUpContext(&context_);
|
CleanUpContext(&context_);
|
||||||
CleanUpSurface(&surface_);
|
CleanUpSurface(&surface_);
|
||||||
CleanUpContext(&page_context_);
|
data_.clear();
|
||||||
CleanUpSurface(&page_surface_);
|
|
||||||
current_page_.clear();
|
|
||||||
all_pages_.clear();
|
|
||||||
skia::VectorPlatformDevice::ClearFontCache();
|
skia::VectorPlatformDevice::ClearFontCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ class PdfPsMetafile {
|
|||||||
// In the renderer process, callers should also call Init(void) to see if the
|
// In the renderer process, callers should also call Init(void) to see if the
|
||||||
// metafile can obtain all necessary rendering resources.
|
// metafile can obtain all necessary rendering resources.
|
||||||
// In the browser process, callers should also call Init(const void*, uint32)
|
// In the browser process, callers should also call Init(const void*, uint32)
|
||||||
// to initialize the buffer |all_pages_| to use SaveTo().
|
// to initialize the buffer |data_| to use SaveTo().
|
||||||
explicit PdfPsMetafile(const FileFormat& format);
|
explicit PdfPsMetafile(const FileFormat& format);
|
||||||
|
|
||||||
~PdfPsMetafile();
|
~PdfPsMetafile();
|
||||||
@@ -45,7 +45,7 @@ class PdfPsMetafile {
|
|||||||
// Returns true on success.
|
// Returns true on success.
|
||||||
// |src_buffer| should point to the shared memory which stores PDF/PS
|
// |src_buffer| should point to the shared memory which stores PDF/PS
|
||||||
// contents generated in the renderer.
|
// contents generated in the renderer.
|
||||||
// Note: Only call in the browser to initialize |all_pages_|.
|
// Note: Only call in the browser to initialize |data_|.
|
||||||
bool Init(const void* src_buffer, uint32 src_buffer_size);
|
bool Init(const void* src_buffer, uint32 src_buffer_size);
|
||||||
|
|
||||||
FileFormat GetFileFormat() const { return format_; }
|
FileFormat GetFileFormat() const { return format_; }
|
||||||
@@ -56,28 +56,23 @@ class PdfPsMetafile {
|
|||||||
cairo_t* StartPage(double width, double height);
|
cairo_t* StartPage(double width, double height);
|
||||||
|
|
||||||
// Destroys the surface and the context used in rendering current page.
|
// Destroys the surface and the context used in rendering current page.
|
||||||
// The results of current page will be appended into buffer |all_pages_|.
|
// The results of current page will be appended into buffer |data_|.
|
||||||
// Returns true on success
|
// Returns true on success.
|
||||||
// TODO(myhuang): I plan to also do page setup here (margins, the header
|
bool FinishPage();
|
||||||
// and the footer). At this moment, only pre-defined margins for US letter
|
|
||||||
// paper are hard-coded here.
|
|
||||||
// |shrink| decides the scaling factor to fit raw printing results into
|
|
||||||
// printable area.
|
|
||||||
bool FinishPage(float shrink);
|
|
||||||
|
|
||||||
// Closes resulting PDF/PS file. No further rendering is allowed.
|
// Closes resulting PDF/PS file. No further rendering is allowed.
|
||||||
void Close();
|
void Close();
|
||||||
|
|
||||||
// Returns size of PDF/PS contents stored in buffer |all_pages_|.
|
// Returns size of PDF/PS contents stored in buffer |data_|.
|
||||||
// This function should ONLY be called after PDF/PS file is closed.
|
// This function should ONLY be called after PDF/PS file is closed.
|
||||||
uint32 GetDataSize() const;
|
uint32 GetDataSize() const;
|
||||||
|
|
||||||
// Copies PDF/PS contents stored in buffer |all_pages_| into |dst_buffer|.
|
// Copies PDF/PS contents stored in buffer |data_| into |dst_buffer|.
|
||||||
// This function should ONLY be called after PDF/PS file is closed.
|
// This function should ONLY be called after PDF/PS file is closed.
|
||||||
// Returns true only when success.
|
// Returns true only when success.
|
||||||
bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
|
bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
|
||||||
|
|
||||||
// Saves PDF/PS contents stored in buffer |all_pages_| into the file
|
// Saves PDF/PS contents stored in buffer |data_| into the file
|
||||||
// associated with |fd|.
|
// associated with |fd|.
|
||||||
// This function should ONLY be called after PDF/PS file is closed.
|
// This function should ONLY be called after PDF/PS file is closed.
|
||||||
bool SaveTo(const base::FileDescriptor& fd) const;
|
bool SaveTo(const base::FileDescriptor& fd) const;
|
||||||
@@ -92,15 +87,8 @@ class PdfPsMetafile {
|
|||||||
cairo_surface_t* surface_;
|
cairo_surface_t* surface_;
|
||||||
cairo_t* context_;
|
cairo_t* context_;
|
||||||
|
|
||||||
// Cairo surface and context for current page only.
|
|
||||||
cairo_surface_t* page_surface_;
|
|
||||||
cairo_t* page_context_;
|
|
||||||
|
|
||||||
// Buffer stores PDF/PS contents for entire PDF/PS file.
|
// Buffer stores PDF/PS contents for entire PDF/PS file.
|
||||||
std::string all_pages_;
|
std::string data_;
|
||||||
|
|
||||||
// Buffer stores PDF/PS contents for current page only.
|
|
||||||
std::string current_page_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile);
|
DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile);
|
||||||
};
|
};
|
||||||
|
@@ -30,13 +30,13 @@ TEST_F(PdfPsTest, Pdf) {
|
|||||||
cairo_t* context = pdf.StartPage(72, 72);
|
cairo_t* context = pdf.StartPage(72, 72);
|
||||||
EXPECT_TRUE(context != NULL);
|
EXPECT_TRUE(context != NULL);
|
||||||
// In theory, we should use Cairo to draw something on |context|.
|
// In theory, we should use Cairo to draw something on |context|.
|
||||||
EXPECT_TRUE(pdf.FinishPage(1.5));
|
EXPECT_TRUE(pdf.FinishPage());
|
||||||
|
|
||||||
// Renders page 2.
|
// Renders page 2.
|
||||||
context = pdf.StartPage(64, 64);
|
context = pdf.StartPage(64, 64);
|
||||||
EXPECT_TRUE(context != NULL);
|
EXPECT_TRUE(context != NULL);
|
||||||
// In theory, we should use Cairo to draw something on |context|.
|
// In theory, we should use Cairo to draw something on |context|.
|
||||||
EXPECT_TRUE(pdf.FinishPage(0.5));
|
EXPECT_TRUE(pdf.FinishPage());
|
||||||
|
|
||||||
// Closes the file.
|
// Closes the file.
|
||||||
pdf.Close();
|
pdf.Close();
|
||||||
@@ -74,13 +74,13 @@ TEST_F(PdfPsTest, Ps) {
|
|||||||
cairo_t* context = ps.StartPage(72, 72);
|
cairo_t* context = ps.StartPage(72, 72);
|
||||||
EXPECT_TRUE(context != NULL);
|
EXPECT_TRUE(context != NULL);
|
||||||
// In theory, we should use Cairo to draw something on |context|.
|
// In theory, we should use Cairo to draw something on |context|.
|
||||||
EXPECT_TRUE(ps.FinishPage(1.5));
|
EXPECT_TRUE(ps.FinishPage());
|
||||||
|
|
||||||
// Renders page 2.
|
// Renders page 2.
|
||||||
context = ps.StartPage(64, 64);
|
context = ps.StartPage(64, 64);
|
||||||
EXPECT_TRUE(context != NULL);
|
EXPECT_TRUE(context != NULL);
|
||||||
// In theory, we should use Cairo to draw something on |context|.
|
// In theory, we should use Cairo to draw something on |context|.
|
||||||
EXPECT_TRUE(ps.FinishPage(0.5));
|
EXPECT_TRUE(ps.FinishPage());
|
||||||
|
|
||||||
// Closes the file.
|
// Closes the file.
|
||||||
ps.Close();
|
ps.Close();
|
||||||
|
Reference in New Issue
Block a user