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 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_nsautorelease_pool.h" | 10 #include "base/mac/scoped_nsautorelease_pool.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 using WebKit::WebFrame; | 25 using WebKit::WebFrame; |
26 | 26 |
27 void PrintWebViewHelper::PrintPageInternal( | 27 void PrintWebViewHelper::PrintPageInternal( |
28 const PrintMsg_PrintPage_Params& params, | 28 const PrintMsg_PrintPage_Params& params, |
29 const gfx::Size& canvas_size, | 29 const gfx::Size& canvas_size, |
30 WebFrame* frame) { | 30 WebFrame* frame) { |
31 printing::NativeMetafile metafile; | 31 printing::NativeMetafile metafile; |
32 if (!metafile.Init()) | 32 if (!metafile.Init()) |
33 return; | 33 return; |
34 | 34 |
35 float scale_factor = frame->getPrintPageShrink(params.page_number); | 35 double scale_factor = frame->getPrintPageShrink(params.page_number); |
36 int page_number = params.page_number; | 36 int page_number = params.page_number; |
37 | 37 |
38 // Render page for printing. | 38 // Render page for printing. |
39 gfx::Rect content_area(params.params.printable_size); | 39 gfx::Rect content_area(params.params.printable_size); |
40 RenderPage(params.params.printable_size, content_area, scale_factor, | 40 RenderPage(params.params.printable_size, content_area, scale_factor, |
41 page_number, frame, false, &metafile); | 41 page_number, frame, false, &metafile); |
42 metafile.FinishDocument(); | 42 metafile.FinishDocument(); |
43 | 43 |
44 PrintHostMsg_DidPrintPage_Params page_params; | 44 PrintHostMsg_DidPrintPage_Params page_params; |
45 page_params.data_size = metafile.GetDataSize(); | 45 page_params.data_size = metafile.GetDataSize(); |
46 page_params.page_number = page_number; | 46 page_params.page_number = page_number; |
47 page_params.document_cookie = params.params.document_cookie; | 47 page_params.document_cookie = params.params.document_cookie; |
48 page_params.actual_shrink = scale_factor; | 48 page_params.actual_shrink = scale_factor; |
49 page_params.page_size = params.params.page_size; | 49 page_params.page_size = params.params.page_size; |
50 page_params.content_area = gfx::Rect(params.params.margin_left, | 50 page_params.content_area = gfx::Rect(params.params.margin_left, |
51 params.params.margin_top, | 51 params.params.margin_top, |
52 params.params.printable_size.width(), | 52 params.params.printable_size.width(), |
53 params.params.printable_size.height()); | 53 params.params.printable_size.height()); |
54 | 54 |
55 // Ask the browser to create the shared memory for us. | 55 // Ask the browser to create the shared memory for us. |
56 if (!CopyMetafileDataToSharedMem(&metafile, | 56 if (!CopyMetafileDataToSharedMem(&metafile, |
57 &(page_params.metafile_data_handle))) { | 57 &(page_params.metafile_data_handle))) { |
58 page_params.data_size = 0; | 58 page_params.data_size = 0; |
59 } | 59 } |
60 | 60 |
61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
62 } | 62 } |
63 | 63 |
64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { | 64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { |
65 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | 65 double scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); |
66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
67 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, | 67 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, |
68 printParams.printable_size.width(), | 68 printParams.printable_size.width(), |
69 printParams.printable_size.height()); | 69 printParams.printable_size.height()); |
70 | 70 |
71 scoped_ptr<printing::Metafile> draft_metafile; | 71 scoped_ptr<printing::Metafile> draft_metafile; |
72 printing::Metafile* initial_render_metafile = | 72 printing::Metafile* initial_render_metafile = |
73 print_preview_context_.metafile(); | 73 print_preview_context_.metafile(); |
74 | 74 |
75 #if defined(USE_SKIA) | 75 #if defined(USE_SKIA) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 draft_metafile.reset( | 137 draft_metafile.reset( |
138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
139 } | 139 } |
140 #endif | 140 #endif |
141 } | 141 } |
142 return PreviewPageRendered(page_number, draft_metafile.get()); | 142 return PreviewPageRendered(page_number, draft_metafile.get()); |
143 } | 143 } |
144 | 144 |
145 void PrintWebViewHelper::RenderPage( | 145 void PrintWebViewHelper::RenderPage( |
146 const gfx::Size& page_size, const gfx::Rect& content_area, | 146 const gfx::Size& page_size, const gfx::Rect& content_area, |
147 const float& scale_factor, int page_number, WebFrame* frame, | 147 const double& scale_factor, int page_number, WebFrame* frame, |
148 bool is_preview, printing::Metafile* metafile) { | 148 bool is_preview, printing::Metafile* metafile) { |
149 | 149 |
150 { | 150 { |
151 #if defined(USE_SKIA) | 151 #if defined(USE_SKIA) |
152 SkDevice* device = metafile->StartPageForVectorCanvas( | 152 SkDevice* device = metafile->StartPageForVectorCanvas( |
153 page_size, content_area, scale_factor); | 153 page_size, content_area, scale_factor); |
154 if (!device) | 154 if (!device) |
155 return; | 155 return; |
156 | 156 |
157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
158 canvas->unref(); // SkRefPtr and new both took a reference. | 158 canvas->unref(); // SkRefPtr and new both took a reference. |
159 WebKit::WebCanvas* canvas_ptr = canvas.get(); | 159 WebKit::WebCanvas* canvas_ptr = canvas.get(); |
160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
162 skia::SetIsPreviewMetafile(*canvas, is_preview); | 162 skia::SetIsPreviewMetafile(*canvas, is_preview); |
163 #else | 163 #else |
164 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 164 bool success = metafile->StartPage(page_size, content_area, scale_factor); |
165 DCHECK(success); | 165 DCHECK(success); |
166 // printPage can create autoreleased references to |context|. PDF contexts | 166 // printPage can create autoreleased references to |context|. PDF contexts |
167 // don't write all their data until they are destroyed, so we need to make | 167 // don't write all their data until they are destroyed, so we need to make |
168 // certain that there are no lingering references. | 168 // certain that there are no lingering references. |
169 base::mac::ScopedNSAutoreleasePool pool; | 169 base::mac::ScopedNSAutoreleasePool pool; |
170 CGContextRef cgContext = metafile->context(); | 170 CGContextRef cgContext = metafile->context(); |
171 CGContextRef canvas_ptr = cgContext; | 171 CGContextRef canvas_ptr = cgContext; |
172 #endif | 172 #endif |
173 | 173 |
174 printing::PageSizeMargins page_layout_in_points; | 174 printing::PageSizeMargins page_layout_in_points; |
175 GetPageSizeAndMarginsInPoints(frame, page_number, | 175 GetPageSizeAndMarginsInPoints( |
176 print_pages_params_->params, | 176 frame, page_number, print_pages_params_->params, |
177 &page_layout_in_points); | 177 ignore_frame_margins_css_, fit_to_page_, NULL, |
| 178 &page_layout_in_points); |
178 | 179 |
179 #if !defined(USE_SKIA) | 180 #if !defined(USE_SKIA) |
180 // For CoreGraphics, print in the margins before printing in the content | 181 // For CoreGraphics, print in the margins before printing in the content |
181 // area so that we don't spill over. Webkit draws a white background in the | 182 // area so that we don't spill over. Webkit draws a white background in the |
182 // content area and this acts as a clip. | 183 // content area and this acts as a clip. |
183 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we | 184 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we |
184 // can draw in the margins safely in Skia in any order. | 185 // can draw in the margins safely in Skia in any order. |
185 if (print_pages_params_->params.display_header_footer) { | 186 if (print_pages_params_->params.display_header_footer) { |
186 PrintHeaderAndFooter(canvas_ptr, page_number + 1, | 187 PrintHeaderAndFooter(canvas_ptr, page_number + 1, |
187 print_preview_context_.total_page_count(), | 188 print_preview_context_.total_page_count(), |
(...skipping 11 matching lines...) Expand all Loading... |
199 print_preview_context_.total_page_count(), | 200 print_preview_context_.total_page_count(), |
200 scale_factor, page_layout_in_points, | 201 scale_factor, page_layout_in_points, |
201 *header_footer_info_); | 202 *header_footer_info_); |
202 } | 203 } |
203 #endif // defined(USE_SKIA) | 204 #endif // defined(USE_SKIA) |
204 } | 205 } |
205 | 206 |
206 // Done printing. Close the device context to retrieve the compiled metafile. | 207 // Done printing. Close the device context to retrieve the compiled metafile. |
207 metafile->FinishPage(); | 208 metafile->FinishPage(); |
208 } | 209 } |
OLD | NEW |