| 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" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } // namespace | 65 } // namespace |
| 66 | 66 |
| 67 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 67 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 68 const gfx::Size& canvas_size, | 68 const gfx::Size& canvas_size, |
| 69 WebFrame* frame) { | 69 WebFrame* frame) { |
| 70 // Generate a memory-based metafile. It will use the current screen's DPI. | 70 // Generate a memory-based metafile. It will use the current screen's DPI. |
| 71 // Each metafile contains a single page. | 71 // Each metafile contains a single page. |
| 72 scoped_ptr<printing::NativeMetafile> metafile( | 72 scoped_ptr<printing::NativeMetafile> metafile( |
| 73 printing::NativeMetafileFactory::CreateMetafile()); | 73 printing::NativeMetafileFactory::CreateMetafile()); |
| 74 metafile->CreateDc(NULL, NULL); | 74 metafile->CreateDc(NULL, NULL); |
| 75 DCHECK(metafile->hdc()); | 75 DCHECK(metafile->context()); |
| 76 skia::PlatformDevice::InitializeDC(metafile->hdc()); | 76 skia::PlatformDevice::InitializeDC(metafile->context()); |
| 77 | 77 |
| 78 int page_number = params.page_number; | 78 int page_number = params.page_number; |
| 79 | 79 |
| 80 // Calculate the dpi adjustment. | 80 // Calculate the dpi adjustment. |
| 81 float scale_factor = static_cast<float>(params.params.desired_dpi / | 81 float scale_factor = static_cast<float>(params.params.desired_dpi / |
| 82 params.params.dpi); | 82 params.params.dpi); |
| 83 | 83 |
| 84 // Render page for printing. | 84 // Render page for printing. |
| 85 RenderPage(params.params, &scale_factor, page_number, frame, &metafile); | 85 RenderPage(params.params, &scale_factor, page_number, frame, &metafile); |
| 86 | 86 |
| 87 // Close the device context to retrieve the compiled metafile. | 87 // Close the device context to retrieve the compiled metafile. |
| 88 if (!metafile->CloseDc()) | 88 if (!metafile->Close()) |
| 89 NOTREACHED(); | 89 NOTREACHED(); |
| 90 | 90 |
| 91 // Get the size of the compiled metafile. | 91 // Get the size of the compiled metafile. |
| 92 uint32 buf_size = metafile->GetDataSize(); | 92 uint32 buf_size = metafile->GetDataSize(); |
| 93 DCHECK_GT(buf_size, 128u); | 93 DCHECK_GT(buf_size, 128u); |
| 94 | 94 |
| 95 ViewHostMsg_DidPrintPage_Params page_params; | 95 ViewHostMsg_DidPrintPage_Params page_params; |
| 96 page_params.data_size = buf_size; | 96 page_params.data_size = buf_size; |
| 97 page_params.metafile_data_handle = NULL; | 97 page_params.metafile_data_handle = NULL; |
| 98 page_params.page_number = page_number; | 98 page_params.page_number = page_number; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // NOTE: This is an enhanced-format metafile(EMF) which has an appearance of | 132 // NOTE: This is an enhanced-format metafile(EMF) which has an appearance of |
| 133 // single page metafile. For print preview, we need a metafile with multiple | 133 // single page metafile. For print preview, we need a metafile with multiple |
| 134 // pages. | 134 // pages. |
| 135 // TODO(kmadhusu): Use a PDF metafile to support multiple pages. After "Skia | 135 // TODO(kmadhusu): Use a PDF metafile to support multiple pages. After "Skia |
| 136 // PDF backend" work is completed for windows, make changes to replace this | 136 // PDF backend" work is completed for windows, make changes to replace this |
| 137 // EMF with PDF metafile. | 137 // EMF with PDF metafile. |
| 138 // http://code.google.com/p/chromium/issues/detail?id=62889 | 138 // http://code.google.com/p/chromium/issues/detail?id=62889 |
| 139 scoped_ptr<printing::NativeMetafile> metafile( | 139 scoped_ptr<printing::NativeMetafile> metafile( |
| 140 printing::NativeMetafileFactory::CreateMetafile()); | 140 printing::NativeMetafileFactory::CreateMetafile()); |
| 141 metafile->CreateDc(NULL, NULL); | 141 metafile->CreateDc(NULL, NULL); |
| 142 DCHECK(metafile->hdc()); | 142 DCHECK(metafile->context()); |
| 143 skia::PlatformDevice::InitializeDC(metafile->hdc()); | 143 skia::PlatformDevice::InitializeDC(metafile->context()); |
| 144 | 144 |
| 145 // Calculate the dpi adjustment. | 145 // Calculate the dpi adjustment. |
| 146 float shrink = static_cast<float>(params.params.desired_dpi / | 146 float shrink = static_cast<float>(params.params.desired_dpi / |
| 147 params.params.dpi); | 147 params.params.dpi); |
| 148 | 148 |
| 149 if (params.pages.empty()) { | 149 if (params.pages.empty()) { |
| 150 for (int i = 0; i < page_count; ++i) { | 150 for (int i = 0; i < page_count; ++i) { |
| 151 float scale_factor = shrink; | 151 float scale_factor = shrink; |
| 152 RenderPage(params.params, &scale_factor, i, frame, &metafile); | 152 RenderPage(params.params, &scale_factor, i, frame, &metafile); |
| 153 } | 153 } |
| 154 } else { | 154 } else { |
| 155 for (size_t i = 0; i < params.pages.size(); ++i) { | 155 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 156 if (params.pages[i] >= page_count) | 156 if (params.pages[i] >= page_count) |
| 157 break; | 157 break; |
| 158 float scale_factor = shrink; | 158 float scale_factor = shrink; |
| 159 RenderPage(params.params, &scale_factor, | 159 RenderPage(params.params, &scale_factor, |
| 160 static_cast<int>(params.pages[i]), frame, &metafile); | 160 static_cast<int>(params.pages[i]), frame, &metafile); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Close the device context to retrieve the compiled metafile. | 164 // Close the device context to retrieve the compiled metafile. |
| 165 if (!metafile->CloseDc()) | 165 if (!metafile->Close()) |
| 166 NOTREACHED(); | 166 NOTREACHED(); |
| 167 | 167 |
| 168 // Get the size of the compiled metafile. | 168 // Get the size of the compiled metafile. |
| 169 uint32 buf_size = metafile->GetDataSize(); | 169 uint32 buf_size = metafile->GetDataSize(); |
| 170 DCHECK_GT(buf_size, 128u); | 170 DCHECK_GT(buf_size, 128u); |
| 171 | 171 |
| 172 ViewHostMsg_DidPreviewDocument_Params preview_params; | 172 ViewHostMsg_DidPreviewDocument_Params preview_params; |
| 173 preview_params.document_cookie = params.params.document_cookie; | 173 preview_params.document_cookie = params.params.document_cookie; |
| 174 preview_params.data_size = buf_size; | 174 preview_params.data_size = buf_size; |
| 175 preview_params.metafile_data_handle = NULL; | 175 preview_params.metafile_data_handle = NULL; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 186 preview_params.metafile_data_handle, | 186 preview_params.metafile_data_handle, |
| 187 &preview_params.metafile_data_handle))) { | 187 &preview_params.metafile_data_handle))) { |
| 188 NOTREACHED() << "Send message failed."; | 188 NOTREACHED() << "Send message failed."; |
| 189 } | 189 } |
| 190 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 190 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void PrintWebViewHelper::RenderPage( | 193 void PrintWebViewHelper::RenderPage( |
| 194 const ViewMsg_Print_Params& params, float* scale_factor, int page_number, | 194 const ViewMsg_Print_Params& params, float* scale_factor, int page_number, |
| 195 WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) { | 195 WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile) { |
| 196 HDC hdc = (*metafile)->hdc(); | 196 HDC hdc = (*metafile)->context(); |
| 197 DCHECK(hdc); | 197 DCHECK(hdc); |
| 198 | 198 |
| 199 double content_width_in_points; | 199 double content_width_in_points; |
| 200 double content_height_in_points; | 200 double content_height_in_points; |
| 201 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 201 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
| 202 &content_width_in_points, &content_height_in_points, NULL, NULL, NULL, | 202 &content_width_in_points, &content_height_in_points, NULL, NULL, NULL, |
| 203 NULL); | 203 NULL); |
| 204 | 204 |
| 205 // Since WebKit extends the page width depending on the magical scale factor | 205 // Since WebKit extends the page width depending on the magical scale factor |
| 206 // we make sure the canvas covers the worst case scenario (x2.0 currently). | 206 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // Update the dpi adjustment with the "page |scale_factor|" calculated in | 251 // Update the dpi adjustment with the "page |scale_factor|" calculated in |
| 252 // webkit. | 252 // webkit. |
| 253 *scale_factor /= webkit_scale_factor; | 253 *scale_factor /= webkit_scale_factor; |
| 254 } | 254 } |
| 255 #endif | 255 #endif |
| 256 | 256 |
| 257 skia::VectorPlatformDevice* platform_device = | 257 skia::VectorPlatformDevice* platform_device = |
| 258 static_cast<skia::VectorPlatformDevice*>(canvas.getDevice()); | 258 static_cast<skia::VectorPlatformDevice*>(canvas.getDevice()); |
| 259 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { | 259 if (platform_device->alpha_blend_used() && !params.supports_alpha_blend) { |
| 260 // Close the device context to retrieve the compiled metafile. | 260 // Close the device context to retrieve the compiled metafile. |
| 261 if (!(*metafile)->CloseDc()) | 261 if (!(*metafile)->Close()) |
| 262 NOTREACHED(); | 262 NOTREACHED(); |
| 263 | 263 |
| 264 scoped_ptr<printing::NativeMetafile> metafile2( | 264 scoped_ptr<printing::NativeMetafile> metafile2( |
| 265 printing::NativeMetafileFactory::CreateMetafile()); | 265 printing::NativeMetafileFactory::CreateMetafile()); |
| 266 // Page used alpha blend, but printer doesn't support it. Rewrite the | 266 // Page used alpha blend, but printer doesn't support it. Rewrite the |
| 267 // metafile and flatten out the transparency. | 267 // metafile and flatten out the transparency. |
| 268 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); | 268 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); |
| 269 if (!bitmap_dc) | 269 if (!bitmap_dc) |
| 270 NOTREACHED() << "Bitmap DC creation failed"; | 270 NOTREACHED() << "Bitmap DC creation failed"; |
| 271 SetGraphicsMode(bitmap_dc, GM_ADVANCED); | 271 SetGraphicsMode(bitmap_dc, GM_ADVANCED); |
| 272 void* bits = NULL; | 272 void* bits = NULL; |
| 273 BITMAPINFO hdr; | 273 BITMAPINFO hdr; |
| 274 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); | 274 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); |
| 275 HBITMAP hbitmap = CreateDIBSection( | 275 HBITMAP hbitmap = CreateDIBSection( |
| 276 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); | 276 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); |
| 277 if (!hbitmap) | 277 if (!hbitmap) |
| 278 NOTREACHED() << "Raster bitmap creation for printing failed"; | 278 NOTREACHED() << "Raster bitmap creation for printing failed"; |
| 279 | 279 |
| 280 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); | 280 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); |
| 281 RECT rect = {0, 0, width, height }; | 281 RECT rect = {0, 0, width, height }; |
| 282 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); | 282 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); |
| 283 FillRect(bitmap_dc, &rect, whiteBrush); | 283 FillRect(bitmap_dc, &rect, whiteBrush); |
| 284 | 284 |
| 285 metafile2->CreateDc(NULL, NULL); | 285 metafile2->CreateDc(NULL, NULL); |
| 286 HDC hdc = metafile2->hdc(); | 286 HDC hdc = metafile2->context(); |
| 287 DCHECK(hdc); | 287 DCHECK(hdc); |
| 288 skia::PlatformDevice::InitializeDC(hdc); | 288 skia::PlatformDevice::InitializeDC(hdc); |
| 289 | 289 |
| 290 RECT metafile_bounds = (*metafile)->GetBounds().ToRECT(); | 290 RECT metafile_bounds = (*metafile)->GetPageBounds(1).ToRECT(); |
| 291 // Process the old metafile, placing all non-AlphaBlend calls into the | 291 // Process the old metafile, placing all non-AlphaBlend calls into the |
| 292 // new metafile, and copying the results of all the AlphaBlend calls | 292 // new metafile, and copying the results of all the AlphaBlend calls |
| 293 // from the bitmap DC. | 293 // from the bitmap DC. |
| 294 EnumEnhMetaFile(hdc, | 294 EnumEnhMetaFile(hdc, |
| 295 (*metafile)->emf(), | 295 (*metafile)->emf(), |
| 296 EnhMetaFileProc, | 296 EnhMetaFileProc, |
| 297 &bitmap_dc, | 297 &bitmap_dc, |
| 298 &metafile_bounds); | 298 &metafile_bounds); |
| 299 | 299 |
| 300 SelectObject(bitmap_dc, old_bitmap); | 300 SelectObject(bitmap_dc, old_bitmap); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 325 // Copy the bits into shared memory. | 325 // Copy the bits into shared memory. |
| 326 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | 326 if (!metafile->GetData(shared_buf.memory(), buf_size)) { |
| 327 NOTREACHED() << "GetData() failed"; | 327 NOTREACHED() << "GetData() failed"; |
| 328 shared_buf.Unmap(); | 328 shared_buf.Unmap(); |
| 329 return false; | 329 return false; |
| 330 } | 330 } |
| 331 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 331 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 332 shared_buf.Unmap(); | 332 shared_buf.Unmap(); |
| 333 return true; | 333 return true; |
| 334 } | 334 } |
| OLD | NEW |