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/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 DCHECK(metafile->context()); | 83 DCHECK(metafile->context()); |
84 skia::InitializeDC(metafile->context()); | 84 skia::InitializeDC(metafile->context()); |
85 | 85 |
86 int page_number = params.page_number; | 86 int page_number = params.page_number; |
87 | 87 |
88 // Calculate the dpi adjustment. | 88 // Calculate the dpi adjustment. |
89 float scale_factor = static_cast<float>(params.params.desired_dpi / | 89 float scale_factor = static_cast<float>(params.params.desired_dpi / |
90 params.params.dpi); | 90 params.params.dpi); |
91 | 91 |
92 // Render page for printing. | 92 // Render page for printing. |
93 RenderPage(params.params, &scale_factor, page_number, false, frame, | 93 metafile.reset(RenderPage(params.params, &scale_factor, page_number, false, |
94 &metafile); | 94 frame, metafile.get())); |
95 | 95 |
96 // Close the device context to retrieve the compiled metafile. | 96 // Close the device context to retrieve the compiled metafile. |
97 if (!metafile->FinishDocument()) | 97 if (!metafile->FinishDocument()) |
98 NOTREACHED(); | 98 NOTREACHED(); |
99 | 99 |
100 // Get the size of the compiled metafile. | 100 // Get the size of the compiled metafile. |
101 uint32 buf_size = metafile->GetDataSize(); | 101 uint32 buf_size = metafile->GetDataSize(); |
102 DCHECK_GT(buf_size, 128u); | 102 DCHECK_GT(buf_size, 128u); |
103 | 103 |
104 PrintHostMsg_DidPrintPage_Params page_params; | 104 PrintHostMsg_DidPrintPage_Params page_params; |
(...skipping 15 matching lines...) Expand all Loading... |
120 | 120 |
121 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 121 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
122 } | 122 } |
123 | 123 |
124 void PrintWebViewHelper::RenderPreviewPage(int page_number) { | 124 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
125 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); | 125 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); |
126 // Calculate the dpi adjustment. | 126 // Calculate the dpi adjustment. |
127 float scale_factor = static_cast<float>(print_params.desired_dpi / | 127 float scale_factor = static_cast<float>(print_params.desired_dpi / |
128 print_params.dpi); | 128 print_params.dpi); |
129 | 129 |
130 // |metafile| is needed for RenderPage() below. |metafile| will not take the | |
131 // ownership of |print_preview_context_| metafile. | |
132 scoped_ptr<Metafile> metafile(print_preview_context_.metafile()); | |
133 | |
134 base::TimeTicks begin_time = base::TimeTicks::Now(); | 130 base::TimeTicks begin_time = base::TimeTicks::Now(); |
135 RenderPage(print_params, &scale_factor, page_number, true, | 131 printing::Metafile* render_page_result = |
136 print_preview_context_.frame(), &metafile); | 132 RenderPage(print_params, &scale_factor, page_number, true, |
| 133 print_preview_context_.frame(), |
| 134 print_preview_context_.metafile()); |
| 135 // In the preview flow, RenderPage will never return a new metafile. |
| 136 DCHECK_EQ(render_page_result, print_preview_context_.metafile()); |
137 | 137 |
138 print_preview_context_.RenderedPreviewPage( | 138 print_preview_context_.RenderedPreviewPage( |
139 base::TimeTicks::Now() - begin_time); | 139 base::TimeTicks::Now() - begin_time); |
140 | 140 |
141 // Release since |print_preview_context_| is the real owner. | |
142 metafile.release(); | |
143 scoped_ptr<printing::Metafile> page_metafile; | 141 scoped_ptr<printing::Metafile> page_metafile; |
144 if (print_preview_context_.IsModifiable()) { | 142 if (print_preview_context_.IsModifiable()) { |
145 page_metafile.reset( | 143 page_metafile.reset( |
146 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 144 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
147 } | 145 } |
148 PreviewPageRendered(page_number, page_metafile.get()); | 146 PreviewPageRendered(page_number, page_metafile.get()); |
149 } | 147 } |
150 | 148 |
151 void PrintWebViewHelper::RenderPage( | 149 Metafile* PrintWebViewHelper::RenderPage( |
152 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, | 150 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, |
153 bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) { | 151 bool is_preview, WebFrame* frame, Metafile* metafile) { |
154 PageSizeMargins page_layout_in_points; | 152 PageSizeMargins page_layout_in_points; |
155 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 153 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
156 &page_layout_in_points); | 154 &page_layout_in_points); |
157 | 155 |
158 int width; | 156 int width; |
159 int height; | 157 int height; |
160 if (is_preview) { | 158 if (is_preview) { |
161 int dpi = static_cast<int>(params.dpi); | 159 int dpi = static_cast<int>(params.dpi); |
162 int desired_dpi = printing::kPointsPerInch; | 160 int desired_dpi = printing::kPointsPerInch; |
163 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); | 161 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); |
164 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); | 162 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); |
165 } else { | 163 } else { |
166 // Since WebKit extends the page width depending on the magical scale factor | 164 // Since WebKit extends the page width depending on the magical scale factor |
167 // we make sure the canvas covers the worst case scenario (x2.0 currently). | 165 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
168 // PrintContext will then set the correct clipping region. | 166 // PrintContext will then set the correct clipping region. |
169 width = static_cast<int>(page_layout_in_points.content_width * | 167 width = static_cast<int>(page_layout_in_points.content_width * |
170 params.max_shrink); | 168 params.max_shrink); |
171 height = static_cast<int>(page_layout_in_points.content_height * | 169 height = static_cast<int>(page_layout_in_points.content_height * |
172 params.max_shrink); | 170 params.max_shrink); |
173 } | 171 } |
174 | 172 |
175 gfx::Size page_size(width, height); | 173 gfx::Size page_size(width, height); |
176 gfx::Rect content_area( | 174 gfx::Rect content_area( |
177 static_cast<int>(page_layout_in_points.margin_left), | 175 static_cast<int>(page_layout_in_points.margin_left), |
178 static_cast<int>(page_layout_in_points.margin_top), | 176 static_cast<int>(page_layout_in_points.margin_top), |
179 static_cast<int>(page_layout_in_points.content_width), | 177 static_cast<int>(page_layout_in_points.content_width), |
180 static_cast<int>(page_layout_in_points.content_height)); | 178 static_cast<int>(page_layout_in_points.content_height)); |
181 SkDevice* device = (*metafile)->StartPageForVectorCanvas( | 179 SkDevice* device = metafile->StartPageForVectorCanvas( |
182 page_size, content_area, frame->getPrintPageShrink(page_number)); | 180 page_size, content_area, frame->getPrintPageShrink(page_number)); |
183 DCHECK(device); | 181 DCHECK(device); |
184 // The printPage method may take a reference to the canvas we pass down, so it | 182 // The printPage method may take a reference to the canvas we pass down, so it |
185 // can't be a stack object. | 183 // can't be a stack object. |
186 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 184 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
187 canvas->unref(); // SkRefPtr and new both took a reference. | 185 canvas->unref(); // SkRefPtr and new both took a reference. |
188 if (is_preview) { | 186 if (is_preview) |
189 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), | 187 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
190 metafile->get()); | |
191 } | |
192 | 188 |
193 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); | 189 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); |
194 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { | 190 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { |
195 NOTREACHED() << "Printing page " << page_number << " failed."; | 191 NOTREACHED() << "Printing page " << page_number << " failed."; |
196 } else { | 192 } else { |
197 // Update the dpi adjustment with the "page |scale_factor|" calculated in | 193 // Update the dpi adjustment with the "page |scale_factor|" calculated in |
198 // webkit. | 194 // webkit. |
199 *scale_factor /= webkit_scale_factor; | 195 *scale_factor /= webkit_scale_factor; |
200 } | 196 } |
201 | 197 |
202 bool result = (*metafile)->FinishPage(); | 198 bool result = metafile->FinishPage(); |
203 DCHECK(result); | 199 DCHECK(result); |
204 | 200 |
205 if (!params.supports_alpha_blend) { | 201 if (!params.supports_alpha_blend) { |
206 // PreviewMetafile (PDF) supports alpha blend, so we only hit this case | 202 // PreviewMetafile (PDF) supports alpha blend, so we only hit this case |
207 // for NativeMetafile. | 203 // for NativeMetafile. |
208 DCHECK(!is_preview); | 204 DCHECK(!is_preview); |
209 skia::PlatformDevice* platform_device = skia::GetPlatformDevice(device); | 205 skia::PlatformDevice* platform_device = skia::GetPlatformDevice(device); |
210 if (platform_device && platform_device->AlphaBlendUsed()) { | 206 if (platform_device && platform_device->AlphaBlendUsed()) { |
211 // Currently, we handle alpha blend transparency for a single page. | 207 // Currently, we handle alpha blend transparency for a single page. |
212 // Therefore, expecting a metafile with page count 1. | 208 // Therefore, expecting a metafile with page count 1. |
213 DCHECK_EQ(1U, (*metafile)->GetPageCount()); | 209 DCHECK_EQ(1U, metafile->GetPageCount()); |
214 | 210 |
215 // Close the device context to retrieve the compiled metafile. | 211 // Close the device context to retrieve the compiled metafile. |
216 if (!(*metafile)->FinishDocument()) | 212 if (!metafile->FinishDocument()) |
217 NOTREACHED(); | 213 NOTREACHED(); |
218 | 214 |
219 // Page used alpha blend, but printer doesn't support it. Rewrite the | 215 // Page used alpha blend, but printer doesn't support it. Rewrite the |
220 // metafile and flatten out the transparency. | 216 // metafile and flatten out the transparency. |
221 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); | 217 HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL)); |
222 if (!bitmap_dc) | 218 if (!bitmap_dc) |
223 NOTREACHED() << "Bitmap DC creation failed"; | 219 NOTREACHED() << "Bitmap DC creation failed"; |
224 SetGraphicsMode(bitmap_dc, GM_ADVANCED); | 220 SetGraphicsMode(bitmap_dc, GM_ADVANCED); |
225 void* bits = NULL; | 221 void* bits = NULL; |
226 BITMAPINFO hdr; | 222 BITMAPINFO hdr; |
227 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); | 223 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); |
228 HBITMAP hbitmap = CreateDIBSection( | 224 HBITMAP hbitmap = CreateDIBSection( |
229 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); | 225 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0); |
230 if (!hbitmap) | 226 if (!hbitmap) |
231 NOTREACHED() << "Raster bitmap creation for printing failed"; | 227 NOTREACHED() << "Raster bitmap creation for printing failed"; |
232 | 228 |
233 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); | 229 HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap); |
234 RECT rect = {0, 0, width, height }; | 230 RECT rect = {0, 0, width, height }; |
235 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); | 231 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); |
236 FillRect(bitmap_dc, &rect, whiteBrush); | 232 FillRect(bitmap_dc, &rect, whiteBrush); |
237 | 233 |
238 scoped_ptr<Metafile> metafile2(new printing::NativeMetafile); | 234 Metafile* metafile2(new printing::NativeMetafile); |
239 metafile2->Init(); | 235 metafile2->Init(); |
240 HDC hdc = metafile2->context(); | 236 HDC hdc = metafile2->context(); |
241 DCHECK(hdc); | 237 DCHECK(hdc); |
242 skia::InitializeDC(hdc); | 238 skia::InitializeDC(hdc); |
243 | 239 |
244 RECT metafile_bounds = (*metafile)->GetPageBounds(1).ToRECT(); | 240 RECT metafile_bounds = metafile->GetPageBounds(1).ToRECT(); |
245 // Process the old metafile, placing all non-AlphaBlend calls into the | 241 // Process the old metafile, placing all non-AlphaBlend calls into the |
246 // new metafile, and copying the results of all the AlphaBlend calls | 242 // new metafile, and copying the results of all the AlphaBlend calls |
247 // from the bitmap DC. | 243 // from the bitmap DC. |
248 EnumEnhMetaFile(hdc, | 244 EnumEnhMetaFile(hdc, |
249 (*metafile)->emf(), | 245 metafile->emf(), |
250 EnhMetaFileProc, | 246 EnhMetaFileProc, |
251 &bitmap_dc, | 247 &bitmap_dc, |
252 &metafile_bounds); | 248 &metafile_bounds); |
253 | 249 |
254 SelectObject(bitmap_dc, old_bitmap); | 250 SelectObject(bitmap_dc, old_bitmap); |
255 metafile->reset(metafile2.release()); | 251 return metafile2; |
256 } | 252 } |
257 } | 253 } |
| 254 return metafile; |
258 } | 255 } |
259 | 256 |
260 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 257 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
261 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { | 258 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { |
262 uint32 buf_size = metafile->GetDataSize(); | 259 uint32 buf_size = metafile->GetDataSize(); |
263 base::SharedMemory shared_buf; | 260 base::SharedMemory shared_buf; |
264 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx | 261 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx |
265 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 | 262 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 |
266 // MB, it can fail to print and not send an error message. | 263 // MB, it can fail to print and not send an error message. |
267 if (buf_size >= 350*1024*1024) { | 264 if (buf_size >= 350*1024*1024) { |
(...skipping 13 matching lines...) Expand all Loading... |
281 shared_buf.Unmap(); | 278 shared_buf.Unmap(); |
282 return false; | 279 return false; |
283 } | 280 } |
284 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 281 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
285 shared_buf.Unmap(); | 282 shared_buf.Unmap(); |
286 | 283 |
287 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 284 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
288 shared_mem_handle)); | 285 shared_mem_handle)); |
289 return true; | 286 return true; |
290 } | 287 } |
OLD | NEW |