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