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/file_descriptor_posix.h" | 7 #include "base/file_descriptor_posix.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/utf_string_conversions.h" | |
vandebo (ex-Chrome)
2011/07/19 21:31:20
Looks like you've added a bunch of extra includes
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
11 #include "chrome/common/print_messages.h" | 12 #include "chrome/common/print_messages.h" |
12 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
13 #include "printing/metafile.h" | 14 #include "printing/metafile.h" |
14 #include "printing/metafile_impl.h" | 15 #include "printing/metafile_impl.h" |
15 #include "printing/metafile_skia_wrapper.h" | 16 #include "printing/metafile_skia_wrapper.h" |
16 #include "skia/ext/vector_canvas.h" | 17 #include "skia/ext/vector_canvas.h" |
18 #include "skia/ext/vector_platform_device_skia.h" | |
19 #include "third_party/skia/include/core/SkColor.h" | |
20 #include "third_party/skia/include/core/SkDraw.h" | |
17 #include "third_party/skia/include/core/SkRefCnt.h" | 21 #include "third_party/skia/include/core/SkRefCnt.h" |
22 #include "third_party/skia/include/core/SkTypeface.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
19 #include "ui/gfx/point.h" | 24 #include "ui/gfx/point.h" |
20 | 25 |
21 #if !defined(OS_CHROMEOS) | 26 #if !defined(OS_CHROMEOS) |
22 #include "base/process_util.h" | 27 #include "base/process_util.h" |
23 #endif // !defined(OS_CHROMEOS) | 28 #endif // !defined(OS_CHROMEOS) |
24 | 29 |
25 using WebKit::WebFrame; | 30 using WebKit::WebFrame; |
26 using WebKit::WebNode; | 31 using WebKit::WebNode; |
27 | 32 |
28 void PrintWebViewHelper::RenderPreviewPage(int page_number) { | 33 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
29 PrintMsg_PrintPage_Params page_params; | 34 PrintMsg_PrintPage_Params page_params; |
30 page_params.params = print_preview_context_.print_params(); | 35 page_params.params = print_preview_context_.print_params(); |
31 page_params.page_number = page_number; | 36 page_params.page_number = page_number; |
32 | 37 |
33 base::TimeTicks begin_time = base::TimeTicks::Now(); | 38 base::TimeTicks begin_time = base::TimeTicks::Now(); |
34 PrintPageInternal(page_params, | 39 PrintPageInternal(page_params, |
35 print_preview_context_.GetPrintCanvasSize(), | 40 print_preview_context_.GetPrintCanvasSize(), |
36 print_preview_context_.frame(), | 41 print_preview_context_.frame(), |
37 print_preview_context_.metafile()); | 42 print_preview_context_.metafile(), |
43 true); | |
38 print_preview_context_.RenderedPreviewPage( | 44 print_preview_context_.RenderedPreviewPage( |
39 base::TimeTicks::Now() - begin_time); | 45 base::TimeTicks::Now() - begin_time); |
40 PreviewPageRendered(page_number); | 46 PreviewPageRendered(page_number); |
41 } | 47 } |
42 | 48 |
43 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 49 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
44 WebFrame* frame, | 50 WebFrame* frame, |
45 WebNode* node) { | 51 WebNode* node) { |
46 printing::NativeMetafile metafile; | 52 printing::NativeMetafile metafile; |
47 if (!metafile.Init()) | 53 if (!metafile.Init()) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 147 |
142 base::TimeTicks begin_time = base::TimeTicks::Now(); | 148 base::TimeTicks begin_time = base::TimeTicks::Now(); |
143 base::TimeTicks page_begin_time = begin_time; | 149 base::TimeTicks page_begin_time = begin_time; |
144 | 150 |
145 PrintMsg_PrintPage_Params page_params; | 151 PrintMsg_PrintPage_Params page_params; |
146 page_params.params = printParams; | 152 page_params.params = printParams; |
147 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 153 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
148 if (params.pages.empty()) { | 154 if (params.pages.empty()) { |
149 for (int i = 0; i < *page_count; ++i) { | 155 for (int i = 0; i < *page_count; ++i) { |
150 page_params.page_number = i; | 156 page_params.page_number = i; |
151 PrintPageInternal(page_params, canvas_size, frame, metafile); | 157 PrintPageInternal(page_params, canvas_size, frame, metafile, false); |
152 } | 158 } |
153 } else { | 159 } else { |
154 for (size_t i = 0; i < params.pages.size(); ++i) { | 160 for (size_t i = 0; i < params.pages.size(); ++i) { |
155 page_params.page_number = params.pages[i]; | 161 page_params.page_number = params.pages[i]; |
156 PrintPageInternal(page_params, canvas_size, frame, metafile); | 162 PrintPageInternal(page_params, canvas_size, frame, metafile, false); |
157 } | 163 } |
158 } | 164 } |
159 | 165 |
160 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; | 166 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; |
161 | 167 |
162 prep_frame_view.FinishPrinting(); | 168 prep_frame_view.FinishPrinting(); |
163 metafile->FinishDocument(); | 169 metafile->FinishDocument(); |
164 return true; | 170 return true; |
165 } | 171 } |
166 | 172 |
167 void PrintWebViewHelper::PrintPageInternal( | 173 void PrintWebViewHelper::PrintPageInternal( |
168 const PrintMsg_PrintPage_Params& params, | 174 const PrintMsg_PrintPage_Params& params, |
169 const gfx::Size& canvas_size, | 175 const gfx::Size& canvas_size, |
170 WebFrame* frame, | 176 WebFrame* frame, |
171 printing::Metafile* metafile) { | 177 printing::Metafile* metafile, |
172 double content_width_in_points; | 178 bool is_preview) { |
173 double content_height_in_points; | 179 PageSizeMargins page_size_margins_in_points = |
174 double margin_top_in_points; | 180 GetPageSizeAndMarginsInPoints(frame, params.page_number, params.params); |
175 double margin_right_in_points; | |
176 double margin_bottom_in_points; | |
177 double margin_left_in_points; | |
178 GetPageSizeAndMarginsInPoints(frame, | |
179 params.page_number, | |
180 params.params, | |
181 &content_width_in_points, | |
182 &content_height_in_points, | |
183 &margin_top_in_points, | |
184 &margin_right_in_points, | |
185 &margin_bottom_in_points, | |
186 &margin_left_in_points); | |
187 | 181 |
188 gfx::Size page_size( | 182 gfx::Size page_size( |
189 content_width_in_points + margin_right_in_points + | 183 page_size_margins_in_points.content_width + |
190 margin_left_in_points, | 184 page_size_margins_in_points.margin_right + |
191 content_height_in_points + margin_top_in_points + | 185 page_size_margins_in_points.margin_left, |
192 margin_bottom_in_points); | 186 page_size_margins_in_points.content_height + |
193 gfx::Rect content_area(margin_left_in_points, margin_top_in_points, | 187 page_size_margins_in_points.margin_top + |
194 content_width_in_points, content_height_in_points); | 188 page_size_margins_in_points.margin_bottom); |
189 | |
190 gfx::Rect content_area(page_size_margins_in_points.margin_left, | |
191 page_size_margins_in_points.margin_top, | |
192 page_size_margins_in_points.content_width, | |
193 page_size_margins_in_points.content_height); | |
195 | 194 |
196 SkDevice* device = metafile->StartPageForVectorCanvas( | 195 SkDevice* device = metafile->StartPageForVectorCanvas( |
197 page_size, content_area, 1.0f); | 196 page_size, content_area, 1.0f); |
198 if (!device) | 197 if (!device) |
199 return; | 198 return; |
200 | 199 |
201 // The printPage method take a reference to the canvas we pass down, so it | 200 // The printPage method take a reference to the canvas we pass down, so it |
202 // can't be a stack object. | 201 // can't be a stack object. |
203 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 202 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
204 canvas->unref(); // SkRefPtr and new both took a reference. | 203 canvas->unref(); // SkRefPtr and new both took a reference. |
205 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); | 204 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
205 | |
206 if (is_preview && params.params.display_header_footer) { | |
207 // The page_number count starts from '0', so, we add by '1'. | |
dpapad
2011/07/19 20:34:01
Nit: Replace this line with
"The page_number coun
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
208 // The scale factor on Linux is '1' which is the last parameter. | |
209 PrintHeaderAndFooter(device, canvas, params.page_number + 1, | |
210 print_preview_context_.total_page_count(), 1, | |
211 page_size_margins_in_points, header_footer_info_); | |
212 } | |
213 | |
206 frame->printPage(params.page_number, canvas.get()); | 214 frame->printPage(params.page_number, canvas.get()); |
207 | 215 |
208 // TODO(myhuang): We should render the header and the footer. | 216 // TODO(myhuang): We should render the header and the footer. |
209 | 217 |
210 // Done printing. Close the device context to retrieve the compiled metafile. | 218 // Done printing. Close the device context to retrieve the compiled metafile. |
211 if (!metafile->FinishPage()) | 219 if (!metafile->FinishPage()) |
212 NOTREACHED() << "metafile failed"; | 220 NOTREACHED() << "metafile failed"; |
213 } | 221 } |
OLD | NEW |