OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
10 #include "printing/native_metafile.h" | 10 #include "printing/native_metafile.h" |
11 #include "printing/units.h" | 11 #include "printing/units.h" |
12 #include "skia/ext/vector_canvas.h" | 12 #include "skia/ext/vector_canvas.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" |
14 | 15 |
| 16 using printing::ConvertPixelsToPoint; |
| 17 using printing::ConvertPixelsToPointDouble; |
15 using printing::NativeMetafile; | 18 using printing::NativeMetafile; |
16 using WebKit::WebFrame; | 19 using WebKit::WebFrame; |
| 20 using WebKit::WebSize; |
| 21 |
| 22 // If frame is NULL, this function returns the default value. |
| 23 static void GetPageSizeAndMarginsInPoints(WebFrame* frame, |
| 24 int page_index, |
| 25 double* content_width_in_points, |
| 26 double* content_height_in_points, |
| 27 double* margin_top_in_points, |
| 28 double* margin_right_in_points, |
| 29 double* margin_bottom_in_points, |
| 30 double* margin_left_in_points) { |
| 31 // TODO(myhuang): Get printing parameters via IPC |
| 32 // using the print_web_view_helper.cc version of Print. |
| 33 // For testing purpose, we hard-coded printing parameters here. |
| 34 |
| 35 // The paper size is US Letter (8.5 in. by 11 in.). |
| 36 WebSize page_size_in_pixels(8.5 * printing::kPixelsPerInch, |
| 37 11.0 * printing::kPixelsPerInch); |
| 38 |
| 39 int margin_top_in_pixels = static_cast<int>( |
| 40 NativeMetafile::kTopMarginInInch * printing::kPixelsPerInch); |
| 41 int margin_right_in_pixels = static_cast<int>( |
| 42 NativeMetafile::kRightMarginInInch * printing::kPixelsPerInch); |
| 43 int margin_bottom_in_pixels = static_cast<int>( |
| 44 NativeMetafile::kBottomMarginInInch * printing::kPixelsPerInch); |
| 45 int margin_left_in_pixels = static_cast<int>( |
| 46 NativeMetafile::kLeftMarginInInch * printing::kPixelsPerInch); |
| 47 |
| 48 if (frame) { |
| 49 frame->pageSizeAndMarginsInPixels(page_index, |
| 50 page_size_in_pixels, |
| 51 margin_top_in_pixels, |
| 52 margin_right_in_pixels, |
| 53 margin_bottom_in_pixels, |
| 54 margin_left_in_pixels); |
| 55 } |
| 56 |
| 57 *content_width_in_points = ConvertPixelsToPoint(page_size_in_pixels.width |
| 58 - margin_left_in_pixels |
| 59 - margin_right_in_pixels); |
| 60 *content_height_in_points = ConvertPixelsToPoint(page_size_in_pixels.height |
| 61 - margin_top_in_pixels |
| 62 - margin_bottom_in_pixels); |
| 63 |
| 64 // Invalid page size and/or margins. We just use the default setting. |
| 65 if (*content_width_in_points < 1.0 || *content_height_in_points < 1.0) { |
| 66 GetPageSizeAndMarginsInPoints(NULL, |
| 67 page_index, |
| 68 content_width_in_points, |
| 69 content_height_in_points, |
| 70 margin_top_in_points, |
| 71 margin_right_in_points, |
| 72 margin_bottom_in_points, |
| 73 margin_left_in_points); |
| 74 return; |
| 75 } |
| 76 |
| 77 if (margin_top_in_points) |
| 78 *margin_top_in_points = |
| 79 ConvertPixelsToPointDouble(margin_top_in_pixels); |
| 80 if (margin_right_in_points) |
| 81 *margin_right_in_points = |
| 82 ConvertPixelsToPointDouble(margin_right_in_pixels); |
| 83 if (margin_bottom_in_points) |
| 84 *margin_bottom_in_points = |
| 85 ConvertPixelsToPointDouble(margin_bottom_in_pixels); |
| 86 if (margin_left_in_points) |
| 87 *margin_left_in_points = |
| 88 ConvertPixelsToPointDouble(margin_left_in_pixels); |
| 89 } |
17 | 90 |
18 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { | 91 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { |
19 // If still not finished with earlier print request simply ignore. | 92 // If still not finished with earlier print request simply ignore. |
20 if (IsPrinting()) | 93 if (IsPrinting()) |
21 return; | 94 return; |
22 | 95 |
23 // TODO(myhuang): Get printing parameters via IPC | 96 double content_width, content_height; |
24 // using the print_web_view_helper.cc version of Print. | 97 GetPageSizeAndMarginsInPoints(frame, 0, &content_width, &content_height, |
25 // For testing purpose, we hard-coded printing parameters here. | 98 NULL, NULL, NULL, NULL); |
26 | 99 |
27 // The paper size is US Letter (8.5 in. by 11 in.). | |
28 const int kDPI = printing::kPointsPerInch; | |
29 const int kWidth = static_cast<int>( | |
30 8.5 * kDPI - NativeMetafile::kLeftMargin - NativeMetafile::kRightMargin); | |
31 const int kHeight = static_cast<int>( | |
32 11 * kDPI - NativeMetafile::kTopMargin - NativeMetafile::kBottomMargin); | |
33 ViewMsg_Print_Params default_settings; | 100 ViewMsg_Print_Params default_settings; |
34 default_settings.printable_size = gfx::Size(kWidth, kHeight); | 101 default_settings.printable_size = gfx::Size( |
35 default_settings.dpi = kDPI; | 102 static_cast<int>(content_width), static_cast<int>(content_height)); |
| 103 default_settings.dpi = printing::kPointsPerInch; |
36 default_settings.min_shrink = 1.25; | 104 default_settings.min_shrink = 1.25; |
37 default_settings.max_shrink = 2.0; | 105 default_settings.max_shrink = 2.0; |
38 default_settings.desired_dpi = kDPI; | 106 default_settings.desired_dpi = printing::kPointsPerInch; |
39 default_settings.document_cookie = 0; | 107 default_settings.document_cookie = 0; |
40 default_settings.selection_only = false; | 108 default_settings.selection_only = false; |
41 | 109 |
42 ViewMsg_PrintPages_Params print_settings; | 110 ViewMsg_PrintPages_Params print_settings; |
43 print_settings.params = default_settings; | 111 print_settings.params = default_settings; |
44 | 112 |
45 PrintPages(print_settings, frame); | 113 PrintPages(print_settings, frame); |
46 } | 114 } |
47 | 115 |
48 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 116 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 return; | 164 return; |
97 | 165 |
98 // Tell the browser we've finished writing the file. | 166 // Tell the browser we've finished writing the file. |
99 Send(new ViewHostMsg_TempFileForPrintingWritten(fd_in_browser)); | 167 Send(new ViewHostMsg_TempFileForPrintingWritten(fd_in_browser)); |
100 } | 168 } |
101 | 169 |
102 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 170 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
103 const gfx::Size& canvas_size, | 171 const gfx::Size& canvas_size, |
104 WebFrame* frame, | 172 WebFrame* frame, |
105 printing::NativeMetafile* metafile) { | 173 printing::NativeMetafile* metafile) { |
| 174 double content_width_in_points; |
| 175 double content_height_in_points; |
| 176 double margin_top_in_points; |
| 177 double margin_right_in_points; |
| 178 double margin_bottom_in_points; |
| 179 double margin_left_in_points; |
| 180 GetPageSizeAndMarginsInPoints(frame, |
| 181 params.page_number, |
| 182 &content_width_in_points, |
| 183 &content_height_in_points, |
| 184 &margin_top_in_points, |
| 185 &margin_right_in_points, |
| 186 &margin_bottom_in_points, |
| 187 &margin_left_in_points); |
| 188 |
106 cairo_t* cairo_context = | 189 cairo_t* cairo_context = |
107 metafile->StartPage(canvas_size.width(), canvas_size.height()); | 190 metafile->StartPage(content_width_in_points, |
| 191 content_height_in_points, |
| 192 margin_top_in_points, |
| 193 margin_right_in_points, |
| 194 margin_bottom_in_points, |
| 195 margin_left_in_points); |
108 if (!cairo_context) | 196 if (!cairo_context) |
109 return; | 197 return; |
110 | 198 |
111 skia::VectorCanvas canvas(cairo_context, | 199 skia::VectorCanvas canvas(cairo_context, |
112 canvas_size.width(), canvas_size.height()); | 200 canvas_size.width(), canvas_size.height()); |
113 frame->printPage(params.page_number, &canvas); | 201 frame->printPage(params.page_number, &canvas); |
114 | 202 |
115 // TODO(myhuang): We should handle transformation for paper margins. | 203 // TODO(myhuang): We should handle transformation for paper margins. |
116 // TODO(myhuang): We should render the header and the footer. | 204 // TODO(myhuang): We should render the header and the footer. |
117 | 205 |
118 // Done printing. Close the device context to retrieve the compiled metafile. | 206 // Done printing. Close the device context to retrieve the compiled metafile. |
119 if (!metafile->FinishPage()) | 207 if (!metafile->FinishPage()) |
120 NOTREACHED() << "metafile failed"; | 208 NOTREACHED() << "metafile failed"; |
121 } | 209 } |
OLD | NEW |