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 |
(...skipping 11 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); | |
184 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 185 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
185 &page_layout_in_points); | 186 ignore_frame_margins_css_, fit_to_page_, |
186 | 187 scale_factor, &page_layout_in_points); |
187 int width; | 188 int width; |
188 int height; | 189 int height; |
189 if (is_preview) { | 190 if (is_preview) { |
190 int dpi = static_cast<int>(params.dpi); | 191 int dpi = static_cast<int>(params.dpi); |
191 int desired_dpi = printing::kPointsPerInch; | 192 int desired_dpi = printing::kPointsPerInch; |
192 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); | 193 width = page_layout_in_points.content_width + |
vandebo (ex-Chrome)
2011/12/04 22:20:34
Why is the width no longer just params.page_size_w
kmadhusu
2011/12/05 09:06:54
(same here)
When source is html (with a special m
| |
193 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); | 194 page_layout_in_points.margin_right + |
195 page_layout_in_points.margin_left; | |
196 height = page_layout_in_points.content_height + | |
197 page_layout_in_points.margin_top + | |
198 page_layout_in_points.margin_bottom; | |
194 } else { | 199 } else { |
195 // Since WebKit extends the page width depending on the magical scale factor | 200 // 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). | 201 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
197 // PrintContext will then set the correct clipping region. | 202 // PrintContext will then set the correct clipping region. |
198 width = static_cast<int>(page_layout_in_points.content_width * | 203 width = static_cast<int>(page_layout_in_points.content_width * |
199 params.max_shrink); | 204 params.max_shrink); |
200 height = static_cast<int>(page_layout_in_points.content_height * | 205 height = static_cast<int>(page_layout_in_points.content_height * |
201 params.max_shrink); | 206 params.max_shrink); |
202 } | 207 } |
203 | 208 |
204 gfx::Size page_size(width, height); | 209 gfx::Size page_size(width, height); |
205 gfx::Rect content_area( | 210 gfx::Rect content_area( |
206 static_cast<int>(page_layout_in_points.margin_left), | 211 static_cast<int>(page_layout_in_points.margin_left), |
207 static_cast<int>(page_layout_in_points.margin_top), | 212 static_cast<int>(page_layout_in_points.margin_top), |
208 static_cast<int>(page_layout_in_points.content_width), | 213 static_cast<int>(page_layout_in_points.content_width), |
209 static_cast<int>(page_layout_in_points.content_height)); | 214 static_cast<int>(page_layout_in_points.content_height)); |
210 SkDevice* device = metafile->StartPageForVectorCanvas( | 215 SkDevice* device = metafile->StartPageForVectorCanvas( |
211 page_size, content_area, frame->getPrintPageShrink(page_number)); | 216 page_size, content_area, *scale_factor); |
212 DCHECK(device); | 217 DCHECK(device); |
213 // The printPage method may take a reference to the canvas we pass down, so it | 218 // The printPage method may take a reference to the canvas we pass down, so it |
214 // can't be a stack object. | 219 // can't be a stack object. |
215 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 220 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
216 canvas->unref(); // SkRefPtr and new both took a reference. | 221 canvas->unref(); // SkRefPtr and new both took a reference. |
217 if (is_preview) { | 222 if (is_preview) { |
218 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 223 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
219 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 224 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
220 skia::SetIsPreviewMetafile(*canvas, is_preview); | 225 skia::SetIsPreviewMetafile(*canvas, is_preview); |
221 } | 226 } |
222 | 227 |
223 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); | 228 float webkit_scale_factor = frame->printPage(page_number, canvas.get()); |
224 | 229 |
225 if (params.display_header_footer) { | 230 if (params.display_header_footer) { |
226 // |page_number| is 0-based, so 1 is added. | 231 // |page_number| is 0-based, so 1 is added. |
227 PrintHeaderAndFooter(canvas.get(), page_number + 1, | 232 PrintHeaderAndFooter(canvas.get(), page_number + 1, |
228 print_preview_context_.total_page_count(), | 233 print_preview_context_.total_page_count(), |
229 webkit_scale_factor, page_layout_in_points, | 234 *scale_factor, page_layout_in_points, |
230 *header_footer_info_); | 235 *header_footer_info_); |
231 } | 236 } |
232 | 237 |
233 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { | 238 if (*scale_factor <= 0 || webkit_scale_factor <= 0) { |
234 NOTREACHED() << "Printing page " << page_number << " failed."; | 239 NOTREACHED() << "Printing page " << page_number << " failed."; |
235 } else { | 240 } else { |
236 // Update the dpi adjustment with the "page |scale_factor|" calculated in | 241 // Update the dpi adjustment with the "page |scale_factor|" calculated in |
237 // webkit. | 242 // webkit. |
238 *scale_factor /= webkit_scale_factor; | 243 *scale_factor /= webkit_scale_factor; |
239 } | 244 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 shared_buf.Unmap(); | 325 shared_buf.Unmap(); |
321 return false; | 326 return false; |
322 } | 327 } |
323 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 328 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
324 shared_buf.Unmap(); | 329 shared_buf.Unmap(); |
325 | 330 |
326 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 331 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
327 shared_mem_handle)); | 332 shared_mem_handle)); |
328 return true; | 333 return true; |
329 } | 334 } |
OLD | NEW |