| 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 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { | 253 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { |
| 255 // Close the device context to retrieve the compiled metafile. | 254 // Close the device context to retrieve the compiled metafile. |
| 256 if (!(*metafile)->CloseDc()) | 255 if (!(*metafile)->CloseDc()) |
| 257 NOTREACHED(); | 256 NOTREACHED(); |
| 258 | 257 |
| 259 scoped_ptr<printing::NativeMetafile> metafile2( | 258 scoped_ptr<printing::NativeMetafile> metafile2( |
| 260 new printing::NativeMetafile); | 259 new printing::NativeMetafile); |
| 261 // Page used alpha blend, but printer doesn't support it. Rewrite the | 260 // Page used alpha blend, but printer doesn't support it. Rewrite the |
| 262 // metafile and flatten out the transparency. | 261 // metafile and flatten out the transparency. |
| 263 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); | 262 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); |
| 264 if (!bitmap_dc) { | 263 if (!bitmap_dc) |
| 265 NOTREACHED() << "Bitmap DC creation failed"; | 264 NOTREACHED() << "Bitmap DC creation failed"; |
| 266 } | |
| 267 SetGraphicsMode(bitmap_dc, GM_ADVANCED); | 265 SetGraphicsMode(bitmap_dc, GM_ADVANCED); |
| 268 void* bits = NULL; | 266 void* bits = NULL; |
| 269 BITMAPINFO hdr; | 267 BITMAPINFO hdr; |
| 270 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); | 268 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); |
| 271 HBITMAP hbitmap = CreateDIBSection( | 269 HBITMAP hbitmap = CreateDIBSection( |
| 272 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); | 270 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); |
| 273 if (!hbitmap) { | 271 if (!hbitmap) |
| 274 NOTREACHED() << "Raster bitmap creation for printing failed"; | 272 NOTREACHED() << "Raster bitmap creation for printing failed"; |
| 275 } | |
| 276 | 273 |
| 277 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); | 274 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); |
| 278 RECT rect = {0, 0, width, height }; | 275 RECT rect = {0, 0, width, height }; |
| 279 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); | 276 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); |
| 280 FillRect(bitmap_dc, &rect, whiteBrush); | 277 FillRect(bitmap_dc, &rect, whiteBrush); |
| 281 | 278 |
| 282 metafile2->CreateDc(NULL, NULL); | 279 metafile2->CreateDc(NULL, NULL); |
| 283 HDC hdc = metafile2->hdc(); | 280 HDC hdc = metafile2->hdc(); |
| 284 DCHECK(hdc); | 281 DCHECK(hdc); |
| 285 skia::PlatformDevice::InitializeDC(hdc); | 282 skia::PlatformDevice::InitializeDC(hdc); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 // Copy the bits into shared memory. | 319 // Copy the bits into shared memory. |
| 323 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | 320 if (!metafile->GetData(shared_buf.memory(), buf_size)) { |
| 324 NOTREACHED() << "GetData() failed"; | 321 NOTREACHED() << "GetData() failed"; |
| 325 shared_buf.Unmap(); | 322 shared_buf.Unmap(); |
| 326 return false; | 323 return false; |
| 327 } | 324 } |
| 328 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 325 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 329 shared_buf.Unmap(); | 326 shared_buf.Unmap(); |
| 330 return true; | 327 return true; |
| 331 } | 328 } |
| OLD | NEW |