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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 // Generate a memory-based metafile. It will use the current screen's DPI. | 102 // Generate a memory-based metafile. It will use the current screen's DPI. |
103 // Each metafile contains a single page. | 103 // Each metafile contains a single page. |
104 scoped_ptr<Metafile> metafile(new printing::NativeMetafile); | 104 scoped_ptr<Metafile> metafile(new printing::NativeMetafile); |
105 metafile->Init(); | 105 metafile->Init(); |
106 DCHECK(metafile->context()); | 106 DCHECK(metafile->context()); |
107 skia::InitializeDC(metafile->context()); | 107 skia::InitializeDC(metafile->context()); |
108 | 108 |
109 int page_number = params.page_number; | 109 int page_number = params.page_number; |
110 | 110 |
111 // Calculate the dpi adjustment. | 111 // Calculate the dpi adjustment. |
112 float scale_factor = static_cast<float>(params.params.desired_dpi / | 112 double scale_factor = static_cast<float>(params.params.desired_dpi / |
113 params.params.dpi); | 113 params.params.dpi); |
114 | 114 |
115 // Render page for printing. | 115 // Render page for printing. |
116 metafile.reset(RenderPage(params.params, &scale_factor, page_number, false, | 116 metafile.reset(RenderPage(params.params, &scale_factor, page_number, false, |
117 frame, metafile.get())); | 117 frame, metafile.get())); |
118 | 118 |
119 // Close the device context to retrieve the compiled metafile. | 119 // Close the device context to retrieve the compiled metafile. |
120 if (!metafile->FinishDocument()) | 120 if (!metafile->FinishDocument()) |
121 NOTREACHED(); | 121 NOTREACHED(); |
122 | 122 |
123 // Get the size of the compiled metafile. | 123 // Get the size of the compiled metafile. |
(...skipping 15 matching lines...) Expand all Loading... | |
139 &(page_params.metafile_data_handle))) { | 139 &(page_params.metafile_data_handle))) { |
140 page_params.data_size = 0; | 140 page_params.data_size = 0; |
141 } | 141 } |
142 | 142 |
143 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 143 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
144 } | 144 } |
145 | 145 |
146 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { | 146 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { |
147 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); | 147 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); |
148 // Calculate the dpi adjustment. | 148 // Calculate the dpi adjustment. |
149 float scale_factor = static_cast<float>(print_params.desired_dpi / | 149 double scale_factor = static_cast<float>(print_params.desired_dpi / |
150 print_params.dpi); | 150 print_params.dpi); |
151 scoped_ptr<Metafile> draft_metafile; | 151 scoped_ptr<Metafile> draft_metafile; |
152 printing::Metafile* initial_render_metafile = | 152 printing::Metafile* initial_render_metafile = |
153 print_preview_context_.metafile(); | 153 print_preview_context_.metafile(); |
154 | 154 |
155 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | 155 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { |
156 draft_metafile.reset(new printing::PreviewMetafile); | 156 draft_metafile.reset(new printing::PreviewMetafile); |
157 initial_render_metafile = draft_metafile.get(); | 157 initial_render_metafile = draft_metafile.get(); |
158 } | 158 } |
159 | 159 |
160 base::TimeTicks begin_time = base::TimeTicks::Now(); | 160 base::TimeTicks begin_time = base::TimeTicks::Now(); |
(...skipping 10 matching lines...) Expand all Loading... | |
171 } else if (print_preview_context_.IsModifiable() && | 171 } else if (print_preview_context_.IsModifiable() && |
172 print_preview_context_.generate_draft_pages()) { | 172 print_preview_context_.generate_draft_pages()) { |
173 DCHECK(!draft_metafile.get()); | 173 DCHECK(!draft_metafile.get()); |
174 draft_metafile.reset( | 174 draft_metafile.reset( |
175 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 175 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
176 } | 176 } |
177 return PreviewPageRendered(page_number, draft_metafile.get()); | 177 return PreviewPageRendered(page_number, draft_metafile.get()); |
178 } | 178 } |
179 | 179 |
180 Metafile* PrintWebViewHelper::RenderPage( | 180 Metafile* PrintWebViewHelper::RenderPage( |
181 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, | 181 const PrintMsg_Print_Params& params, double* scale_factor, int page_number, |
182 bool is_preview, WebFrame* frame, Metafile* metafile) { | 182 bool is_preview, WebFrame* frame, Metafile* metafile) { |
183 printing::PageSizeMargins page_layout_in_points; | 183 printing::PageSizeMargins page_layout_in_points; |
184 *scale_factor = frame->getPrintPageShrink(page_number); | |
vandebo (ex-Chrome)
2011/12/14 19:05:38
Don't we need both scale factors? GetPageSizeANdM
kmadhusu
2011/12/19 09:52:19
Fixed.
| |
184 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 185 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
185 &page_layout_in_points); | 186 ignore_css_margins_, fit_to_page_, |
186 | 187 scale_factor, &page_layout_in_points); |
187 int width; | 188 gfx::Size page_size; |
188 int height; | 189 gfx::Rect content_area; |
189 if (is_preview) { | 190 UpdatePageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, |
190 int dpi = static_cast<int>(params.dpi); | 191 &content_area); |
191 int desired_dpi = printing::kPointsPerInch; | 192 if (!is_preview) { |
192 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); | |
193 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); | |
194 } else { | |
195 // Since WebKit extends the page width depending on the magical scale factor | 193 // Since WebKit extends the page width depending on the magical scale factor |
196 // we make sure the canvas covers the worst case scenario (x2.0 currently). | 194 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
197 // PrintContext will then set the correct clipping region. | 195 // PrintContext will then set the correct clipping region. |
198 width = static_cast<int>(page_layout_in_points.content_width * | 196 page_size = gfx::Size( |
199 params.max_shrink); | 197 static_cast<int>(page_layout_in_points.content_width * |
200 height = static_cast<int>(page_layout_in_points.content_height * | 198 params.max_shrink), |
201 params.max_shrink); | 199 static_cast<int>(page_layout_in_points.content_height * |
200 params.max_shrink)); | |
202 } | 201 } |
203 | 202 |
204 gfx::Size page_size(width, height); | |
205 gfx::Rect content_area( | |
206 static_cast<int>(page_layout_in_points.margin_left), | |
207 static_cast<int>(page_layout_in_points.margin_top), | |
208 static_cast<int>(page_layout_in_points.content_width), | |
209 static_cast<int>(page_layout_in_points.content_height)); | |
210 SkDevice* device = metafile->StartPageForVectorCanvas( | 203 SkDevice* device = metafile->StartPageForVectorCanvas( |
211 page_size, content_area, frame->getPrintPageShrink(page_number)); | 204 page_size, content_area, *scale_factor); |
212 DCHECK(device); | 205 DCHECK(device); |
213 // The printPage method may take a reference to the canvas we pass down, so it | 206 // The printPage method may take a reference to the canvas we pass down, so it |
214 // can't be a stack object. | 207 // can't be a stack object. |
215 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 208 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
216 canvas->unref(); // SkRefPtr and new both took a reference. | 209 canvas->unref(); // SkRefPtr and new both took a reference. |
217 if (is_preview) { | 210 if (is_preview) { |
218 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 211 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
219 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 212 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
220 skia::SetIsPreviewMetafile(*canvas, is_preview); | 213 skia::SetIsPreviewMetafile(*canvas, is_preview); |
221 } | 214 } |
222 | 215 |
223 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); | 216 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); |
224 | 217 |
225 if (params.display_header_footer) { | 218 if (params.display_header_footer) { |
226 // |page_number| is 0-based, so 1 is added. | 219 // |page_number| is 0-based, so 1 is added. |
227 PrintHeaderAndFooter(canvas.get(), page_number + 1, | 220 PrintHeaderAndFooter(canvas.get(), page_number + 1, |
228 print_preview_context_.total_page_count(), | 221 print_preview_context_.total_page_count(), |
229 webkit_scale_factor, page_layout_in_points, | 222 *scale_factor, page_layout_in_points, |
230 *header_footer_info_); | 223 *header_footer_info_); |
231 } | 224 } |
232 | 225 |
233 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { | 226 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { |
234 NOTREACHED() << "Printing page " << page_number << " failed."; | 227 NOTREACHED() << "Printing page " << page_number << " failed."; |
235 } else { | 228 } else { |
236 // Update the dpi adjustment with the "page |scale_factor|" calculated in | 229 // Update the dpi adjustment with the "page |scale_factor|" calculated in |
237 // webkit. | 230 // webkit. |
238 *scale_factor /= webkit_scale_factor; | 231 *scale_factor /= webkit_scale_factor; |
239 } | 232 } |
(...skipping 17 matching lines...) Expand all Loading... | |
257 | 250 |
258 // Page used alpha blend, but printer doesn't support it. Rewrite the | 251 // Page used alpha blend, but printer doesn't support it. Rewrite the |
259 // metafile and flatten out the transparency. | 252 // metafile and flatten out the transparency. |
260 base::win::ScopedGetDC screen_dc(NULL); | 253 base::win::ScopedGetDC screen_dc(NULL); |
261 base::win::ScopedCreateDC bitmap_dc(CreateCompatibleDC(screen_dc)); | 254 base::win::ScopedCreateDC bitmap_dc(CreateCompatibleDC(screen_dc)); |
262 if (!bitmap_dc) | 255 if (!bitmap_dc) |
263 NOTREACHED() << "Bitmap DC creation failed"; | 256 NOTREACHED() << "Bitmap DC creation failed"; |
264 SetGraphicsMode(bitmap_dc, GM_ADVANCED); | 257 SetGraphicsMode(bitmap_dc, GM_ADVANCED); |
265 void* bits = NULL; | 258 void* bits = NULL; |
266 BITMAPINFO hdr; | 259 BITMAPINFO hdr; |
267 gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader); | 260 gfx::CreateBitmapHeader(page_size.width(), page_size.height(), |
261 &hdr.bmiHeader); | |
268 base::win::ScopedBitmap hbitmap(CreateDIBSection( | 262 base::win::ScopedBitmap hbitmap(CreateDIBSection( |
269 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0)); | 263 bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0)); |
270 if (!hbitmap) | 264 if (!hbitmap) |
271 NOTREACHED() << "Raster bitmap creation for printing failed"; | 265 NOTREACHED() << "Raster bitmap creation for printing failed"; |
272 | 266 |
273 base::win::ScopedSelectObject selectBitmap(bitmap_dc, hbitmap); | 267 base::win::ScopedSelectObject selectBitmap(bitmap_dc, hbitmap); |
274 RECT rect = {0, 0, width, height }; | 268 RECT rect = { 0, 0, page_size.width(), page_size.height() }; |
275 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); | 269 HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); |
276 FillRect(bitmap_dc, &rect, whiteBrush); | 270 FillRect(bitmap_dc, &rect, whiteBrush); |
277 | 271 |
278 Metafile* metafile2(new printing::NativeMetafile); | 272 Metafile* metafile2(new printing::NativeMetafile); |
279 metafile2->Init(); | 273 metafile2->Init(); |
280 HDC hdc = metafile2->context(); | 274 HDC hdc = metafile2->context(); |
281 DCHECK(hdc); | 275 DCHECK(hdc); |
282 skia::InitializeDC(hdc); | 276 skia::InitializeDC(hdc); |
283 | 277 |
284 RECT metafile_bounds = metafile->GetPageBounds(1).ToRECT(); | 278 RECT metafile_bounds = metafile->GetPageBounds(1).ToRECT(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 shared_buf.Unmap(); | 314 shared_buf.Unmap(); |
321 return false; | 315 return false; |
322 } | 316 } |
323 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 317 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
324 shared_buf.Unmap(); | 318 shared_buf.Unmap(); |
325 | 319 |
326 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 320 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
327 shared_mem_handle)); | 321 shared_mem_handle)); |
328 return true; | 322 return true; |
329 } | 323 } |
OLD | NEW |