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