blob: 840e5fbce9a6bdfa39826962326a52f75fd3b0f0 [file] [log] [blame]
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/renderer/print_web_view_helper.h"
#include "base/logging.h"
#include "base/process_util.h"
#include "chrome/common/print_messages.h"
#include "printing/native_metafile_factory.h"
#include "printing/native_metafile.h"
#include "printing/units.h"
#include "skia/ext/vector_canvas.h"
#include "skia/ext/vector_platform_device.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
using printing::ConvertUnitDouble;
using printing::kPointsPerInch;
using WebKit::WebFrame;
namespace {
int CALLBACK EnhMetaFileProc(HDC dc,
HANDLETABLE* handle_table,
const ENHMETARECORD *record,
int num_objects,
LPARAM data) {
HDC* bitmap_dc = reinterpret_cast<HDC*>(data);
// Play this command to the bitmap DC.
PlayEnhMetaFileRecord(*bitmap_dc, handle_table, record, num_objects);
if (record->iType == EMR_ALPHABLEND) {
const EMRALPHABLEND* emr_alpha_blend =
reinterpret_cast<const EMRALPHABLEND*>(record);
XFORM bitmap_dc_transform, metafile_dc_transform;
XFORM identity = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
// Temporarily set the world transforms of both DC's to identity.
GetWorldTransform(dc, &metafile_dc_transform);
SetWorldTransform(dc, &identity);
GetWorldTransform(*bitmap_dc, &bitmap_dc_transform);
SetWorldTransform(*bitmap_dc, &identity);
const RECTL& rect = emr_alpha_blend->rclBounds;
// Since the printer does not support alpha blend, copy the alpha blended
// region from our (software-rendered) bitmap DC to the metafile DC.
BitBlt(dc,
rect.left,
rect.top,
rect.right - rect.left + 1,
rect.bottom - rect.top + 1,
*bitmap_dc,
rect.left,
rect.top,
SRCCOPY);
// Restore the world transforms of both DC's.
SetWorldTransform(dc, &metafile_dc_transform);
SetWorldTransform(*bitmap_dc, &bitmap_dc_transform);
} else {
// Play this command to the metafile DC.
PlayEnhMetaFileRecord(dc, handle_table, record, num_objects);
}
return 1; // Continue enumeration
}
} // namespace
void PrintWebViewHelper::PrintPage(const PrintMsg_PrintPage_Params& params,
const gfx::Size& canvas_size,
WebFrame* frame) {
// Generate a memory-based metafile. It will use the current screen's DPI.
// Each metafile contains a single page.
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::Create());
DCHECK(metafile->context());
skia::PlatformDevice::InitializeDC(metafile->context());
int page_number = params.page_number;
// Calculate the dpi adjustment.
float scale_factor = static_cast<float>(params.params.desired_dpi /
params.params.dpi);
// Render page for printing.
RenderPage(params.params, &scale_factor, page_number, frame, &metafile);
// Close the device context to retrieve the compiled metafile.
if (!metafile->FinishDocument())
NOTREACHED();
// Get the size of the compiled metafile.
uint32 buf_size = metafile->GetDataSize();
DCHECK_GT(buf_size, 128u);
PrintHostMsg_DidPrintPage_Params page_params;
page_params.data_size = buf_size;
page_params.metafile_data_handle = NULL;
page_params.page_number = page_number;
page_params.document_cookie = params.params.document_cookie;
page_params.actual_shrink = scale_factor;
page_params.page_size = params.params.page_size;
page_params.content_area = gfx::Rect(params.params.margin_left,
params.params.margin_top, params.params.printable_size.width(),
params.params.printable_size.height());
page_params.has_visible_overlays = frame->isPageBoxVisible(page_number);
if (!CopyMetafileDataToSharedMem(metafile.get(),
&(page_params.metafile_data_handle))) {
page_params.data_size = 0;
}
if (!Send(new PrintHostMsg_DuplicateSection(
routing_id(),
page_params.metafile_data_handle,
&page_params.metafile_data_handle))) {
NOTREACHED() << "Send message failed.";
}
Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
}
void PrintWebViewHelper::CreatePreviewDocument(
const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame,
WebKit::WebNode* node) {
int page_count = 0;
PrintMsg_Print_Params print_params = params.params;
UpdatePrintableSizeInPrintParameters(frame, node, &print_params);
PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node,
frame->view());
page_count = prep_frame_view.GetExpectedPageCount();
if (!page_count)
return;
// NOTE: This is an enhanced-format metafile(EMF) which has an appearance of
// single page metafile. For print preview, we need a metafile with multiple
// pages.
// TODO(kmadhusu): Use a PDF metafile to support multiple pages. After "Skia
// PDF backend" work is completed for windows, make changes to replace this
// EMF with PDF metafile.
// http://code.google.com/p/chromium/issues/detail?id=62889
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::Create());
DCHECK(metafile->context());
skia::PlatformDevice::InitializeDC(metafile->context());
// Calculate the dpi adjustment.
float shrink = static_cast<float>(params.params.desired_dpi /
params.params.dpi);
if (params.pages.empty()) {
for (int i = 0; i < page_count; ++i) {
float scale_factor = shrink;
RenderPage(params.params, &scale_factor, i, frame, &metafile);
}
} else {
for (size_t i = 0; i < params.pages.size(); ++i) {
if (params.pages[i] >= page_count)
break;
float scale_factor = shrink;
RenderPage(params.params, &scale_factor,
static_cast<int>(params.pages[i]), frame, &metafile);
}
}
// Close the device context to retrieve the compiled metafile.
if (!metafile->FinishDocument())
NOTREACHED();
// Get the size of the compiled metafile.
uint32 buf_size = metafile->GetDataSize();
DCHECK_GT(buf_size, 128u);
PrintHostMsg_DidPreviewDocument_Params preview_params;
preview_params.document_cookie = params.params.document_cookie;
preview_params.data_size = buf_size;
preview_params.metafile_data_handle = NULL;
preview_params.expected_pages_count = page_count;
if (!CopyMetafileDataToSharedMem(metafile.get(),
&(preview_params.metafile_data_handle))) {
preview_params.data_size = 0;
preview_params.expected_pages_count = 0;
}
if (!Send(new PrintHostMsg_DuplicateSection(
routing_id(),
preview_params.metafile_data_handle,
&preview_params.metafile_data_handle))) {
NOTREACHED() << "Send message failed.";
}
Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params));
}
void PrintWebViewHelper::RenderPage(
const PrintMsg_Print_Params& params, float* scale_factor, int page_number,
WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) {
DCHECK(metafile->get()->context());
double content_width_in_points;
double content_height_in_points;
double margin_top_in_points;
double margin_left_in_points;
GetPageSizeAndMarginsInPoints(frame, page_number, params,
&content_width_in_points,
&content_height_in_points,
&margin_top_in_points, NULL, NULL,
&margin_left_in_points);
// Since WebKit extends the page width depending on the magical scale factor
// we make sure the canvas covers the worst case scenario (x2.0 currently).
// PrintContext will then set the correct clipping region.
int width = static_cast<int>(content_width_in_points * params.max_shrink);
int height = static_cast<int>(content_height_in_points * params.max_shrink);
gfx::Size page_size(width, height);
gfx::Point content_origin(static_cast<int>(margin_left_in_points),
static_cast<int>(margin_top_in_points));
skia::PlatformDevice* device = (*metafile)->StartPageForVectorCanvas(
page_size, content_origin, 1.0f);
DCHECK(device);
skia::VectorCanvas canvas(device);
float webkit_scale_factor = frame->printPage(page_number, &canvas);
if (*scale_factor <= 0 || webkit_scale_factor <= 0) {
NOTREACHED() << "Printing page " << page_number << " failed.";
} else {
// Update the dpi adjustment with the "page |scale_factor|" calculated in
// webkit.
*scale_factor /= webkit_scale_factor;
}
bool result = (*metafile)->FinishPage();
DCHECK(result);
skia::VectorPlatformDevice* platform_device =
static_cast<skia::VectorPlatformDevice*>(device);
if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) {
// Close the device context to retrieve the compiled metafile.
if (!(*metafile)->FinishDocument())
NOTREACHED();
scoped_ptr<printing::NativeMetafile> metafile2(
printing::NativeMetafileFactory::Create());
// Page used alpha blend, but printer doesn't support it. Rewrite the
// metafile and flatten out the transparency.
HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL));
if (!bitmap_dc)
NOTREACHED() << "Bitmap DC creation failed";
SetGraphicsMode(bitmap_dc, GM_ADVANCED);
void* bits = NULL;
BITMAPINFO hdr;
gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader);
HBITMAP hbitmap = CreateDIBSection(
bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0);
if (!hbitmap)
NOTREACHED() << "Raster bitmap creation for printing failed";
HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap);
RECT rect = {0, 0, width, height };
HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
FillRect(bitmap_dc, &rect, whiteBrush);
HDC hdc = metafile2->context();
DCHECK(hdc);
skia::PlatformDevice::InitializeDC(hdc);
RECT metafile_bounds = (*metafile)->GetPageBounds(1).ToRECT();
// Process the old metafile, placing all non-AlphaBlend calls into the
// new metafile, and copying the results of all the AlphaBlend calls
// from the bitmap DC.
EnumEnhMetaFile(hdc,
(*metafile)->emf(),
EnhMetaFileProc,
&bitmap_dc,
&metafile_bounds);
SelectObject(bitmap_dc, old_bitmap);
metafile->reset(metafile2.release());
}
}
bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
printing::NativeMetafile* metafile,
base::SharedMemoryHandle* shared_mem_handle) {
uint32 buf_size = metafile->GetDataSize();
base::SharedMemory shared_buf;
// http://msdn2.microsoft.com/en-us/library/ms535522.aspx
// Windows 2000/XP: When a page in a spooled file exceeds approximately 350
// MB, it can fail to print and not send an error message.
if (buf_size >= 350*1024*1024) {
NOTREACHED() << "Buffer too large: " << buf_size;
return false;
}
// Allocate a shared memory buffer to hold the generated metafile data.
if (!shared_buf.CreateAndMapAnonymous(buf_size)) {
NOTREACHED() << "Buffer allocation failed";
return false;
}
// Copy the bits into shared memory.
if (!metafile->GetData(shared_buf.memory(), buf_size)) {
NOTREACHED() << "GetData() failed";
shared_buf.Unmap();
return false;
}
shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle);
shared_buf.Unmap();
return true;
}