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