Commit b1b1492c authored by kmadhusu@chromium.org's avatar kmadhusu@chromium.org

Refactor win printing workflow to support print preview.

Made code changes to create a single metafile for the entire printing document.

Implemented the following function:
1. CreatePreviewDocument(): Create a single metafile and render all the pages(entire printing document) into the same metafile.

Note: Although the structure of PrintPage() function looks similar to CreatePreviewDocument(), merging of these functions is difficult for the following reasons:
  1. As of now, we are using EMF metafile to create a preview document. In future we are going to use Skia->PDF metafile. So construction and destruction of the metafile will be different.
  2. The message for preview workflow(ViewHostMsg_PagesReadyForPreview) is different from normal printing workflow(ViewHostMsg_DidPrintPage).
  3. The param struct which is sent along with the message for preview workflow is different from printing workflow.
  4. Finally, the semantics of PrintPage() function is to print a page per metafile which is completely different from the semantics of CreatePreviewDocument() function.

Once Skia->Pdf work is completed, Modify CreatePreviewDocument() function to use this PDF metafile.

BUG=64121
TEST=printing works after code changes.

Review URL: http://codereview.chromium.org/6261025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72804 0039d316-1c4b-4281-b951-d872f2087c98
parent 83a8d3a4
...@@ -510,12 +510,12 @@ void PrintWebViewHelper::RenderPagesForPreview(WebFrame *frame) { ...@@ -510,12 +510,12 @@ void PrintWebViewHelper::RenderPagesForPreview(WebFrame *frame) {
Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params));
} }
#if !defined(OS_MACOSX) #if defined(OS_LINUX)
void PrintWebViewHelper::CreatePreviewDocument( void PrintWebViewHelper::CreatePreviewDocument(
const ViewMsg_PrintPages_Params& params, const ViewMsg_PrintPages_Params& params,
WebFrame* frame, WebFrame* frame,
ViewHostMsg_DidPreviewDocument_Params* preview_params) { ViewHostMsg_DidPreviewDocument_Params* preview_params) {
// TODO(kmadhusu): Implement this function for windows & linux. // TODO(kmadhusu): Implement this function for linux.
preview_params->document_cookie = params.params.document_cookie; preview_params->document_cookie = params.params.document_cookie;
} }
#endif #endif
......
...@@ -174,6 +174,10 @@ class PrintWebViewHelper : public WebKit::WebViewClient, ...@@ -174,6 +174,10 @@ class PrintWebViewHelper : public WebKit::WebViewClient,
// Render the frame for preview. // Render the frame for preview.
void RenderPagesForPreview(WebKit::WebFrame* frame); void RenderPagesForPreview(WebKit::WebFrame* frame);
// Renders all the pages listed in |params| for preview.
// On Success, |preview_params| will have a valid handle to metafile data,
// data buffer size and document cookie.
void CreatePreviewDocument(const ViewMsg_PrintPages_Params& params, void CreatePreviewDocument(const ViewMsg_PrintPages_Params& params,
WebKit::WebFrame* frame, WebKit::WebFrame* frame,
ViewHostMsg_DidPreviewDocument_Params* preview_params); ViewHostMsg_DidPreviewDocument_Params* preview_params);
...@@ -182,10 +186,9 @@ class PrintWebViewHelper : public WebKit::WebViewClient, ...@@ -182,10 +186,9 @@ class PrintWebViewHelper : public WebKit::WebViewClient,
const float& scale_factor, int page_number, const float& scale_factor, int page_number,
WebKit::WebFrame* frame, printing::NativeMetafile* metafile); WebKit::WebFrame* frame, printing::NativeMetafile* metafile);
#elif defined(OS_WIN) #elif defined(OS_WIN)
void RenderPage(const gfx::Size& page_size, float* scale_factor, void RenderPage(const ViewMsg_Print_Params& params, float* scale_factor,
int page_number, WebKit::WebFrame* frame, int page_number, WebKit::WebFrame* frame,
scoped_ptr<printing::NativeMetafile>* metafile, scoped_ptr<printing::NativeMetafile>* metafile);
bool supports_alpha_blend);
#endif #endif
#if defined(OS_MACOSX) || defined(OS_WIN) #if defined(OS_MACOSX) || defined(OS_WIN)
......
...@@ -75,26 +75,12 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, ...@@ -75,26 +75,12 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
int page_number = params.page_number; int page_number = params.page_number;
double content_width_in_points;
double content_height_in_points;
GetPageSizeAndMarginsInPoints(frame, page_number, params.params,
&content_width_in_points, &content_height_in_points, NULL, NULL, NULL,
NULL);
// Calculate the dpi adjustment. // Calculate the dpi adjustment.
float scale_factor = static_cast<float>(params.params.desired_dpi / float scale_factor = static_cast<float>(params.params.desired_dpi /
params.params.dpi); params.params.dpi);
// 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.
gfx::Size page_size(
static_cast<int>(content_width_in_points * params.params.max_shrink),
static_cast<int>(content_height_in_points * params.params.max_shrink));
// Render page for printing. // Render page for printing.
RenderPage(page_size, &scale_factor, page_number, frame, &metafile, RenderPage(params.params, &scale_factor, page_number, frame, &metafile);
params.params.supports_alpha_blend);
// Close the device context to retrieve the compiled metafile. // Close the device context to retrieve the compiled metafile.
if (!metafile->CloseDc()) if (!metafile->CloseDc())
...@@ -129,15 +115,92 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, ...@@ -129,15 +115,92 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
} }
} }
void PrintWebViewHelper::CreatePreviewDocument(
const ViewMsg_PrintPages_Params& params, WebFrame* frame,
ViewHostMsg_DidPreviewDocument_Params* preview_params) {
int page_count = 0;
ViewMsg_Print_Params print_params = params.params;
UpdatePrintableSizeInPrintParameters(frame, NULL, &print_params);
PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, NULL,
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(new printing::NativeMetafile);
metafile->CreateDc(NULL, NULL);
DCHECK(metafile->hdc());
skia::PlatformDevice::InitializeDC(metafile->hdc());
// 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->CloseDc())
NOTREACHED();
// Get the size of the compiled metafile.
uint32 buf_size = metafile->GetDataSize();
DCHECK_GT(buf_size, 128u);
preview_params->document_cookie = params.params.document_cookie;
preview_params->data_size = 0;
preview_params->metafile_data_handle = NULL;
if (CopyMetafileDataToSharedMem(metafile.get(),
&(preview_params->metafile_data_handle))) {
preview_params->data_size = buf_size;
}
metafile->CloseEmf();
if (!Send(new ViewHostMsg_DuplicateSection(
routing_id(),
preview_params->metafile_data_handle,
&preview_params->metafile_data_handle))) {
NOTREACHED() << "Send message failed.";
}
}
void PrintWebViewHelper::RenderPage( void PrintWebViewHelper::RenderPage(
const gfx::Size& page_size, float* scale_factor, int page_number, const ViewMsg_Print_Params& params, float* scale_factor, int page_number,
WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile, WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) {
bool supports_alpha_blend) {
HDC hdc = (*metafile)->hdc(); HDC hdc = (*metafile)->hdc();
DCHECK(hdc); DCHECK(hdc);
int width = page_size.width(); double content_width_in_points;
int height = page_size.height(); double content_height_in_points;
GetPageSizeAndMarginsInPoints(frame, page_number, params,
&content_width_in_points, &content_height_in_points, NULL, NULL, NULL,
NULL);
// 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);
#if 0 #if 0
// TODO(maruel): This code is kept for testing until the 100% GDI drawing // TODO(maruel): This code is kept for testing until the 100% GDI drawing
// code is stable. maruels use this code's output as a reference when the // code is stable. maruels use this code's output as a reference when the
...@@ -186,7 +249,7 @@ void PrintWebViewHelper::RenderPage( ...@@ -186,7 +249,7 @@ void PrintWebViewHelper::RenderPage(
skia::VectorPlatformDevice* platform_device = skia::VectorPlatformDevice* platform_device =
static_cast<skia::VectorPlatformDevice*>(canvas.getDevice()); static_cast<skia::VectorPlatformDevice*>(canvas.getDevice());
if (platform_device->alpha_blend_used() && !supports_alpha_blend) { if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) {
// Close the device context to retrieve the compiled metafile. // Close the device context to retrieve the compiled metafile.
if (!(*metafile)->CloseDc()) if (!(*metafile)->CloseDc())
NOTREACHED(); NOTREACHED();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment