Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/process_util.h" | 8 #include "base/process_util.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/render_messages_params.h" | 10 #include "chrome/common/render_messages_params.h" |
| 11 #include "chrome/renderer/render_view.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 #include "printing/native_metafile.h" | |
| 14 #include "printing/units.h" | 11 #include "printing/units.h" |
| 15 #include "skia/ext/vector_canvas.h" | 12 #include "skia/ext/vector_canvas.h" |
| 16 #include "skia/ext/vector_platform_device.h" | 13 #include "skia/ext/vector_platform_device.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "ui/gfx/gdi_util.h" | 15 #include "ui/gfx/gdi_util.h" |
| 19 #include "ui/gfx/size.h" | |
| 20 | 16 |
| 21 using printing::ConvertUnitDouble; | 17 using printing::ConvertUnitDouble; |
| 22 using printing::kPointsPerInch; | 18 using printing::kPointsPerInch; |
| 23 using WebKit::WebFrame; | 19 using WebKit::WebFrame; |
| 24 using WebKit::WebString; | 20 |
| 21 namespace { | |
| 25 | 22 |
| 26 int CALLBACK EnhMetaFileProc(HDC dc, | 23 int CALLBACK EnhMetaFileProc(HDC dc, |
| 27 HANDLETABLE* handle_table, | 24 HANDLETABLE* handle_table, |
| 28 const ENHMETARECORD *record, | 25 const ENHMETARECORD *record, |
| 29 int num_objects, | 26 int num_objects, |
| 30 LPARAM data) { | 27 LPARAM data) { |
| 31 HDC* bitmap_dc = reinterpret_cast<HDC*>(data); | 28 HDC* bitmap_dc = reinterpret_cast<HDC*>(data); |
| 32 // Play this command to the bitmap DC. | 29 // Play this command to the bitmap DC. |
| 33 PlayEnhMetaFileRecord(*bitmap_dc, handle_table, record, num_objects); | 30 PlayEnhMetaFileRecord(*bitmap_dc, handle_table, record, num_objects); |
| 34 if (record->iType == EMR_ALPHABLEND) { | 31 if (record->iType == EMR_ALPHABLEND) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 56 // Restore the world transforms of both DC's. | 53 // Restore the world transforms of both DC's. |
| 57 SetWorldTransform(dc, &metafile_dc_transform); | 54 SetWorldTransform(dc, &metafile_dc_transform); |
| 58 SetWorldTransform(*bitmap_dc, &bitmap_dc_transform); | 55 SetWorldTransform(*bitmap_dc, &bitmap_dc_transform); |
| 59 } else { | 56 } else { |
| 60 // Play this command to the metafile DC. | 57 // Play this command to the metafile DC. |
| 61 PlayEnhMetaFileRecord(dc, handle_table, record, num_objects); | 58 PlayEnhMetaFileRecord(dc, handle_table, record, num_objects); |
| 62 } | 59 } |
| 63 return 1; // Continue enumeration | 60 return 1; // Continue enumeration |
| 64 } | 61 } |
| 65 | 62 |
| 63 } // namespace | |
| 64 | |
| 66 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 65 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 67 const gfx::Size& canvas_size, | 66 const gfx::Size& canvas_size, |
| 68 WebFrame* frame) { | 67 WebFrame* frame) { |
| 69 // Generate a memory-based metafile. It will use the current screen's DPI. | 68 // Generate a memory-based metafile. It will use the current screen's DPI. |
| 70 // Each metafile contains a single page. | 69 // Each metafile contains a single page. |
| 71 scoped_ptr<printing::NativeMetafile> metafile(new printing::NativeMetafile); | 70 scoped_ptr<printing::NativeMetafile> metafile(new printing::NativeMetafile); |
| 72 metafile->CreateDc(NULL, NULL); | 71 metafile->CreateDc(NULL, NULL); |
| 73 DCHECK(metafile->hdc()); | 72 DCHECK(metafile->hdc()); |
| 74 skia::PlatformDevice::InitializeDC(metafile->hdc()); | 73 skia::PlatformDevice::InitializeDC(metafile->hdc()); |
| 75 | 74 |
| 76 int page_number = params.page_number; | 75 int page_number = params.page_number; |
| 77 | 76 |
| 78 // Calculate the dpi adjustment. | 77 // Calculate the dpi adjustment. |
| 79 float scale_factor = static_cast<float>(params.params.desired_dpi / | 78 float scale_factor = static_cast<float>(params.params.desired_dpi / |
| 80 params.params.dpi); | 79 params.params.dpi); |
| 81 | 80 |
| 82 // Render page for printing. | 81 // Render page for printing. |
| 83 RenderPage(params.params, &scale_factor, page_number, frame, &metafile); | 82 RenderPage(params.params, &scale_factor, page_number, frame, &metafile); |
| 84 | 83 |
| 85 // Close the device context to retrieve the compiled metafile. | 84 // Close the device context to retrieve the compiled metafile. |
| 86 if (!metafile->CloseDc()) | 85 if (!metafile->CloseDc()) |
| 87 NOTREACHED(); | 86 NOTREACHED(); |
| 88 | 87 |
| 89 // Get the size of the compiled metafile. | 88 // Get the size of the compiled metafile. |
| 90 uint32 buf_size = metafile->GetDataSize(); | 89 uint32 buf_size = metafile->GetDataSize(); |
| 91 DCHECK_GT(buf_size, 128u); | 90 DCHECK_GT(buf_size, 128u); |
| 92 | 91 |
| 93 ViewHostMsg_DidPrintPage_Params page_params; | 92 ViewHostMsg_DidPrintPage_Params page_params; |
| 94 page_params.data_size = buf_size; | 93 page_params.data_size = buf_size; |
| 95 page_params.metafile_data_handle = NULL; | |
|
Lei Zhang
2011/02/18 06:55:34
Already initialized to INVALID_HANDLE_VALUE.
| |
| 96 page_params.page_number = page_number; | 94 page_params.page_number = page_number; |
| 97 page_params.document_cookie = params.params.document_cookie; | 95 page_params.document_cookie = params.params.document_cookie; |
| 98 page_params.actual_shrink = scale_factor; | 96 page_params.actual_shrink = scale_factor; |
| 99 page_params.page_size = params.params.page_size; | 97 page_params.page_size = params.params.page_size; |
| 100 page_params.content_area = gfx::Rect(params.params.margin_left, | 98 page_params.content_area = gfx::Rect(params.params.margin_left, |
| 101 params.params.margin_top, params.params.printable_size.width(), | 99 params.params.margin_top, params.params.printable_size.width(), |
| 102 params.params.printable_size.height()); | 100 params.params.printable_size.height()); |
| 103 page_params.has_visible_overlays = frame->isPageBoxVisible(page_number); | 101 page_params.has_visible_overlays = frame->isPageBoxVisible(page_number); |
| 104 | 102 |
| 105 if (!CopyMetafileDataToSharedMem(metafile.get(), | 103 if (!CopyMetafileDataToSharedMem(metafile.get(), |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 if (!metafile->CloseDc()) | 160 if (!metafile->CloseDc()) |
| 163 NOTREACHED(); | 161 NOTREACHED(); |
| 164 | 162 |
| 165 // Get the size of the compiled metafile. | 163 // Get the size of the compiled metafile. |
| 166 uint32 buf_size = metafile->GetDataSize(); | 164 uint32 buf_size = metafile->GetDataSize(); |
| 167 DCHECK_GT(buf_size, 128u); | 165 DCHECK_GT(buf_size, 128u); |
| 168 | 166 |
| 169 ViewHostMsg_DidPreviewDocument_Params preview_params; | 167 ViewHostMsg_DidPreviewDocument_Params preview_params; |
| 170 preview_params.document_cookie = params.params.document_cookie; | 168 preview_params.document_cookie = params.params.document_cookie; |
| 171 preview_params.data_size = buf_size; | 169 preview_params.data_size = buf_size; |
| 172 preview_params.metafile_data_handle = NULL; | |
| 173 | 170 |
| 174 if (!CopyMetafileDataToSharedMem(metafile.get(), | 171 if (!CopyMetafileDataToSharedMem(metafile.get(), |
| 175 &(preview_params.metafile_data_handle))) { | 172 &(preview_params.metafile_data_handle))) { |
| 176 preview_params.data_size = 0; | 173 preview_params.data_size = 0; |
| 177 } | 174 } |
| 178 metafile->CloseEmf(); | 175 metafile->CloseEmf(); |
| 179 if (!Send(new ViewHostMsg_DuplicateSection( | 176 if (!Send(new ViewHostMsg_DuplicateSection( |
| 180 routing_id(), | 177 routing_id(), |
| 181 preview_params.metafile_data_handle, | 178 preview_params.metafile_data_handle, |
| 182 &preview_params.metafile_data_handle))) { | 179 &preview_params.metafile_data_handle))) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { | 251 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { |
| 255 // Close the device context to retrieve the compiled metafile. | 252 // Close the device context to retrieve the compiled metafile. |
| 256 if (!(*metafile)->CloseDc()) | 253 if (!(*metafile)->CloseDc()) |
| 257 NOTREACHED(); | 254 NOTREACHED(); |
| 258 | 255 |
| 259 scoped_ptr<printing::NativeMetafile> metafile2( | 256 scoped_ptr<printing::NativeMetafile> metafile2( |
| 260 new printing::NativeMetafile); | 257 new printing::NativeMetafile); |
| 261 // Page used alpha blend, but printer doesn't support it. Rewrite the | 258 // Page used alpha blend, but printer doesn't support it. Rewrite the |
| 262 // metafile and flatten out the transparency. | 259 // metafile and flatten out the transparency. |
| 263 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); | 260 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); |
| 264 if (!bitmap_dc) { | 261 if (!bitmap_dc) |
| 265 NOTREACHED() << "Bitmap DC creation failed"; | 262 NOTREACHED() << "Bitmap DC creation failed"; |
| 266 } | |
| 267 SetGraphicsMode(bitmap_dc, GM_ADVANCED); | 263 SetGraphicsMode(bitmap_dc, GM_ADVANCED); |
| 268 void* bits = NULL; | 264 void* bits = NULL; |
| 269 BITMAPINFO hdr; | 265 BITMAPINFO hdr; |
| 270 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); | 266 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); |
| 271 HBITMAP hbitmap = CreateDIBSection( | 267 HBITMAP hbitmap = CreateDIBSection( |
| 272 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); | 268 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); |
| 273 if (!hbitmap) { | 269 if (!hbitmap) |
| 274 NOTREACHED() << "Raster bitmap creation for printing failed"; | 270 NOTREACHED() << "Raster bitmap creation for printing failed"; |
| 275 } | |
| 276 | 271 |
| 277 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); | 272 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); |
| 278 RECT rect = {0, 0, width, height }; | 273 RECT rect = {0, 0, width, height }; |
| 279 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); | 274 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); |
| 280 FillRect(bitmap_dc, &rect, whiteBrush); | 275 FillRect(bitmap_dc, &rect, whiteBrush); |
| 281 | 276 |
| 282 metafile2->CreateDc(NULL, NULL); | 277 metafile2->CreateDc(NULL, NULL); |
| 283 HDC hdc = metafile2->hdc(); | 278 HDC hdc = metafile2->hdc(); |
| 284 DCHECK(hdc); | 279 DCHECK(hdc); |
| 285 skia::PlatformDevice::InitializeDC(hdc); | 280 skia::PlatformDevice::InitializeDC(hdc); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 // Copy the bits into shared memory. | 317 // Copy the bits into shared memory. |
| 323 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | 318 if (!metafile->GetData(shared_buf.memory(), buf_size)) { |
| 324 NOTREACHED() << "GetData() failed"; | 319 NOTREACHED() << "GetData() failed"; |
| 325 shared_buf.Unmap(); | 320 shared_buf.Unmap(); |
| 326 return false; | 321 return false; |
| 327 } | 322 } |
| 328 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 323 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 329 shared_buf.Unmap(); | 324 shared_buf.Unmap(); |
| 330 return true; | 325 return true; |
| 331 } | 326 } |
| OLD | NEW |