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); | |
36 int page_number = params.page_number; | 35 int page_number = params.page_number; |
37 | 36 gfx::Size page_size_in_dpi; |
38 // Render page for printing. | 37 gfx::Rect content_area_in_dpi; |
39 gfx::Rect content_area(params.params.content_size); | 38 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, |
40 RenderPage(params.params.content_size, content_area, scale_factor, | 39 &page_size_in_dpi, &content_area_in_dpi); |
41 page_number, frame, false, &metafile); | |
42 metafile.FinishDocument(); | 40 metafile.FinishDocument(); |
43 | 41 |
44 PrintHostMsg_DidPrintPage_Params page_params; | 42 PrintHostMsg_DidPrintPage_Params page_params; |
45 page_params.data_size = metafile.GetDataSize(); | 43 page_params.data_size = metafile.GetDataSize(); |
46 page_params.page_number = page_number; | 44 page_params.page_number = page_number; |
47 page_params.document_cookie = params.params.document_cookie; | 45 page_params.document_cookie = params.params.document_cookie; |
48 page_params.actual_shrink = scale_factor; | 46 page_params.actual_shrink = 1.0f; |
vandebo (ex-Chrome)
2012/01/09 19:04:02
nit: I think you said Linux doesn't even set this
kmadhusu
2012/01/09 23:49:27
Done.
| |
49 page_params.page_size = params.params.page_size; | 47 page_params.page_size = page_size_in_dpi; |
50 page_params.content_area = gfx::Rect(params.params.margin_left, | 48 page_params.content_area = content_area_in_dpi; |
51 params.params.margin_top, | |
52 params.params.content_size.width(), | |
53 params.params.content_size.height()); | |
54 | 49 |
55 // Ask the browser to create the shared memory for us. | 50 // Ask the browser to create the shared memory for us. |
56 if (!CopyMetafileDataToSharedMem(&metafile, | 51 if (!CopyMetafileDataToSharedMem(&metafile, |
57 &(page_params.metafile_data_handle))) { | 52 &(page_params.metafile_data_handle))) { |
58 page_params.data_size = 0; | 53 page_params.data_size = 0; |
59 } | 54 } |
60 | 55 |
61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 56 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
62 } | 57 } |
63 | 58 |
64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { | 59 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { |
65 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | |
66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 60 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
67 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, | |
68 printParams.content_size.width(), | |
69 printParams.content_size.height()); | |
70 | |
71 scoped_ptr<printing::Metafile> draft_metafile; | 61 scoped_ptr<printing::Metafile> draft_metafile; |
72 printing::Metafile* initial_render_metafile = | 62 printing::Metafile* initial_render_metafile = |
73 print_preview_context_.metafile(); | 63 print_preview_context_.metafile(); |
74 | 64 |
75 #if defined(USE_SKIA) | 65 #if defined(USE_SKIA) |
76 bool render_to_draft = print_preview_context_.IsModifiable() && | 66 bool render_to_draft = print_preview_context_.IsModifiable() && |
77 is_print_ready_metafile_sent_; | 67 is_print_ready_metafile_sent_; |
78 #else | 68 #else |
79 // If the page needs to be in both draft metafile and print ready metafile, | 69 // If the page needs to be in both draft metafile and print ready metafile, |
80 // we should always render to the draft metafile first and then copy that | 70 // we should always render to the draft metafile first and then copy that |
81 // into the print ready metafile because CG does not allow us to do it in | 71 // into the print ready metafile because CG does not allow us to do it in |
82 // the other order. | 72 // the other order. |
83 bool render_to_draft = print_preview_context_.IsModifiable() && | 73 bool render_to_draft = print_preview_context_.IsModifiable() && |
84 print_preview_context_.generate_draft_pages(); | 74 print_preview_context_.generate_draft_pages(); |
85 #endif | 75 #endif |
86 | 76 |
87 if (render_to_draft) { | 77 if (render_to_draft) { |
88 draft_metafile.reset(new printing::PreviewMetafile()); | 78 draft_metafile.reset(new printing::PreviewMetafile()); |
89 if (!draft_metafile->Init()) { | 79 if (!draft_metafile->Init()) { |
90 print_preview_context_.set_error( | 80 print_preview_context_.set_error( |
91 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); | 81 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); |
92 LOG(ERROR) << "Draft PreviewMetafile Init failed"; | 82 LOG(ERROR) << "Draft PreviewMetafile Init failed"; |
93 return false; | 83 return false; |
94 } | 84 } |
95 initial_render_metafile = draft_metafile.get(); | 85 initial_render_metafile = draft_metafile.get(); |
96 } | 86 } |
97 | 87 |
98 base::TimeTicks begin_time = base::TimeTicks::Now(); | 88 base::TimeTicks begin_time = base::TimeTicks::Now(); |
99 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 89 gfx::Size page_size; |
100 print_preview_context_.frame(), true, initial_render_metafile); | 90 RenderPage(printParams, page_number, print_preview_context_.frame(), true, |
91 initial_render_metafile, &page_size, NULL); | |
101 print_preview_context_.RenderedPreviewPage( | 92 print_preview_context_.RenderedPreviewPage( |
102 base::TimeTicks::Now() - begin_time); | 93 base::TimeTicks::Now() - begin_time); |
103 | 94 |
104 if (draft_metafile.get()) { | 95 if (draft_metafile.get()) { |
105 draft_metafile->FinishDocument(); | 96 draft_metafile->FinishDocument(); |
106 #if !defined(USE_SKIA) | 97 #if !defined(USE_SKIA) |
107 if (!is_print_ready_metafile_sent_) { | 98 if (!is_print_ready_metafile_sent_) { |
108 // With CG, we rendered into a new metafile so we could get it as a draft | 99 // With CG, we rendered into a new metafile so we could get it as a draft |
109 // document. Now we need to add it to print ready document. But the | 100 // document. Now we need to add it to print ready document. But the |
110 // document has already been scaled and adjusted for margins, so do a 1:1 | 101 // document has already been scaled and adjusted for margins, so do a 1:1 |
111 // drawing. | 102 // drawing. |
112 printing::Metafile* print_ready_metafile = | 103 printing::Metafile* print_ready_metafile = |
113 print_preview_context_.metafile(); | 104 print_preview_context_.metafile(); |
114 bool success = print_ready_metafile->StartPage( | 105 bool success = print_ready_metafile->StartPage(page_size, |
115 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); | 106 gfx::Rect(page_size), 1.0); |
116 DCHECK(success); | 107 DCHECK(success); |
117 // StartPage unconditionally flips the content over, flip it back since it | 108 // StartPage unconditionally flips the content over, flip it back since it |
118 // was already flipped in |draft_metafile|. | 109 // was already flipped in |draft_metafile|. |
119 CGContextTranslateCTM(print_ready_metafile->context(), 0, | 110 CGContextTranslateCTM(print_ready_metafile->context(), 0, |
120 printParams.page_size.height()); | 111 page_size.height()); |
121 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); | 112 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); |
122 draft_metafile->RenderPage(1, | 113 draft_metafile->RenderPage(1, |
123 print_ready_metafile->context(), | 114 print_ready_metafile->context(), |
124 draft_metafile->GetPageBounds(1).ToCGRect(), | 115 draft_metafile->GetPageBounds(1).ToCGRect(), |
125 false /* shrink_to_fit */, | 116 false /* shrink_to_fit */, |
126 false /* stretch_to_fit */, | 117 false /* stretch_to_fit */, |
127 true /* center_horizontally */, | 118 true /* center_horizontally */, |
128 true /* center_vertically */); | 119 true /* center_vertically */); |
129 print_ready_metafile->FinishPage(); | 120 print_ready_metafile->FinishPage(); |
130 } | 121 } |
131 #endif | 122 #endif |
132 } else { | 123 } else { |
133 #if defined(USE_SKIA) | 124 #if defined(USE_SKIA) |
134 if (print_preview_context_.IsModifiable() && | 125 if (print_preview_context_.IsModifiable() && |
135 print_preview_context_.generate_draft_pages()) { | 126 print_preview_context_.generate_draft_pages()) { |
136 DCHECK(!draft_metafile.get()); | 127 DCHECK(!draft_metafile.get()); |
137 draft_metafile.reset( | 128 draft_metafile.reset( |
138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 129 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
139 } | 130 } |
140 #endif | 131 #endif |
141 } | 132 } |
142 return PreviewPageRendered(page_number, draft_metafile.get()); | 133 return PreviewPageRendered(page_number, draft_metafile.get()); |
143 } | 134 } |
144 | 135 |
145 void PrintWebViewHelper::RenderPage( | 136 void PrintWebViewHelper::RenderPage( |
146 const gfx::Size& page_size, const gfx::Rect& content_area, | 137 const PrintMsg_Print_Params& params, int page_number, WebFrame* frame, |
147 const float& scale_factor, int page_number, WebFrame* frame, | 138 bool is_preview, printing::Metafile* metafile, gfx::Size* page_size, |
148 bool is_preview, printing::Metafile* metafile) { | 139 gfx::Rect* content_rect) { |
140 double scale_factor = 1.0f; | |
141 double webkit_shrink_factor = frame->getPrintPageShrink(page_number); | |
142 printing::PageSizeMargins page_layout_in_points; | |
143 gfx::Rect content_area; | |
149 | 144 |
145 ComputePageLayoutInPointsForCss(frame, page_number, params, | |
146 ignore_css_margins_, fit_to_page_, | |
147 &scale_factor, &page_layout_in_points); | |
148 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size, | |
149 &content_area); | |
150 if (content_rect) | |
151 *content_rect = content_area; | |
152 | |
153 scale_factor *= webkit_shrink_factor; | |
150 { | 154 { |
151 #if defined(USE_SKIA) | 155 #if defined(USE_SKIA) |
152 SkDevice* device = metafile->StartPageForVectorCanvas( | 156 SkDevice* device = metafile->StartPageForVectorCanvas( |
153 page_size, content_area, scale_factor); | 157 *page_size, content_area, scale_factor); |
154 if (!device) | 158 if (!device) |
155 return; | 159 return; |
156 | 160 |
157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 161 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
158 canvas->unref(); // SkRefPtr and new both took a reference. | 162 canvas->unref(); // SkRefPtr and new both took a reference. |
159 WebKit::WebCanvas* canvas_ptr = canvas.get(); | 163 WebKit::WebCanvas* canvas_ptr = canvas.get(); |
160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 164 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 165 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
162 skia::SetIsPreviewMetafile(*canvas, is_preview); | 166 skia::SetIsPreviewMetafile(*canvas, is_preview); |
163 #else | 167 #else |
164 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 168 bool success = metafile->StartPage(*page_size, content_area, scale_factor); |
165 DCHECK(success); | 169 DCHECK(success); |
166 // printPage can create autoreleased references to |context|. PDF contexts | 170 // 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 | 171 // don't write all their data until they are destroyed, so we need to make |
168 // certain that there are no lingering references. | 172 // certain that there are no lingering references. |
169 base::mac::ScopedNSAutoreleasePool pool; | 173 base::mac::ScopedNSAutoreleasePool pool; |
170 CGContextRef cgContext = metafile->context(); | 174 CGContextRef cgContext = metafile->context(); |
171 CGContextRef canvas_ptr = cgContext; | 175 CGContextRef canvas_ptr = cgContext; |
172 #endif | |
173 | 176 |
174 printing::PageSizeMargins page_layout_in_points; | |
175 GetPageSizeAndMarginsInPoints(frame, page_number, | |
176 print_pages_params_->params, | |
177 &page_layout_in_points); | |
178 | |
179 #if !defined(USE_SKIA) | |
180 // For CoreGraphics, print in the margins before printing in the content | 177 // 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 | 178 // area so that we don't spill over. Webkit draws a white background in the |
182 // content area and this acts as a clip. | 179 // content area and this acts as a clip. |
183 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we | 180 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we |
184 // can draw in the margins safely in Skia in any order. | 181 // can draw in the margins safely in Skia in any order. |
185 if (print_pages_params_->params.display_header_footer) { | 182 if (print_pages_params_->params.display_header_footer) { |
186 PrintHeaderAndFooter(canvas_ptr, page_number + 1, | 183 PrintHeaderAndFooter(canvas_ptr, page_number + 1, |
187 print_preview_context_.total_page_count(), | 184 print_preview_context_.total_page_count(), |
188 scale_factor, page_layout_in_points, | 185 scale_factor, page_layout_in_points, |
189 *header_footer_info_); | 186 *header_footer_info_); |
190 } | 187 } |
191 #endif // !USE_SKIA | 188 #endif // !USE_SKIA |
192 | 189 |
193 frame->printPage(page_number, canvas_ptr); | 190 frame->printPage(page_number, canvas_ptr); |
194 | 191 |
195 #if defined(USE_SKIA) | 192 #if defined(USE_SKIA) |
196 if (print_pages_params_->params.display_header_footer) { | 193 if (print_pages_params_->params.display_header_footer) { |
197 // |page_number| is 0-based, so 1 is added. | 194 // |page_number| is 0-based, so 1 is added. |
198 PrintHeaderAndFooter(canvas_ptr, page_number + 1, | 195 PrintHeaderAndFooter(canvas_ptr, page_number + 1, |
199 print_preview_context_.total_page_count(), | 196 print_preview_context_.total_page_count(), |
200 scale_factor, page_layout_in_points, | 197 scale_factor, page_layout_in_points, |
201 *header_footer_info_); | 198 *header_footer_info_); |
202 } | 199 } |
203 #endif // defined(USE_SKIA) | 200 #endif // defined(USE_SKIA) |
204 } | 201 } |
205 | 202 |
206 // Done printing. Close the device context to retrieve the compiled metafile. | 203 // Done printing. Close the device context to retrieve the compiled metafile. |
207 metafile->FinishPage(); | 204 metafile->FinishPage(); |
208 } | 205 } |
OLD | NEW |