| 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 #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" |
| 15 | 15 |
| 16 using printing::ConvertPixelsToPoint; | |
| 17 using printing::ConvertPixelsToPointDouble; | |
| 18 using printing::NativeMetafile; | 16 using printing::NativeMetafile; |
| 19 using WebKit::WebFrame; | 17 using WebKit::WebFrame; |
| 20 using WebKit::WebSize; | 18 using WebKit::WebSize; |
| 21 | 19 |
| 22 // If frame is NULL, this function returns the default value. | 20 static void FillDefaultPrintParams(ViewMsg_Print_Params* params) { |
| 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 | 21 // TODO(myhuang): Get printing parameters via IPC |
| 32 // using the print_web_view_helper.cc version of Print. | 22 // using the print_web_view_helper.cc version of Print. |
| 33 // For testing purpose, we hard-coded printing parameters here. | 23 // For testing purpose, we hard-coded printing parameters here. |
| 34 | 24 |
| 35 // The paper size is US Letter (8.5 in. by 11 in.). | 25 // The paper size is US Letter (8.5 in. by 11 in.). |
| 36 WebSize page_size_in_pixels(8.5 * printing::kPixelsPerInch, | 26 double page_width_in_pixel = 8.5 * printing::kPixelsPerInch; |
| 37 11.0 * printing::kPixelsPerInch); | 27 double page_height_in_pixel = 11.0 * printing::kPixelsPerInch; |
| 38 | 28 params->page_size = gfx::Size( |
| 39 int margin_top_in_pixels = static_cast<int>( | 29 static_cast<int>(page_width_in_pixel), |
| 30 static_cast<int>(page_height_in_pixel)); |
| 31 params->printable_size = gfx::Size( |
| 32 static_cast<int>( |
| 33 page_width_in_pixel - |
| 34 (NativeMetafile::kLeftMarginInInch + |
| 35 NativeMetafile::kRightMarginInInch) * printing::kPixelsPerInch), |
| 36 static_cast<int>( |
| 37 page_height_in_pixel - |
| 38 (NativeMetafile::kTopMarginInInch + |
| 39 NativeMetafile::kBottomMarginInInch) * printing::kPixelsPerInch)); |
| 40 params->margin_top = static_cast<int>( |
| 40 NativeMetafile::kTopMarginInInch * printing::kPixelsPerInch); | 41 NativeMetafile::kTopMarginInInch * printing::kPixelsPerInch); |
| 41 int margin_right_in_pixels = static_cast<int>( | 42 params->margin_left = 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); | 43 NativeMetafile::kLeftMarginInInch * printing::kPixelsPerInch); |
| 47 | 44 params->dpi = printing::kPixelsPerInch; |
| 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 } | 45 } |
| 90 | 46 |
| 91 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { | 47 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { |
| 92 // If still not finished with earlier print request simply ignore. | 48 // If still not finished with earlier print request simply ignore. |
| 93 if (IsPrinting()) | 49 if (IsPrinting()) |
| 94 return; | 50 return; |
| 95 | 51 |
| 52 ViewMsg_Print_Params default_settings; |
| 53 FillDefaultPrintParams(&default_settings); |
| 54 |
| 96 double content_width, content_height; | 55 double content_width, content_height; |
| 97 GetPageSizeAndMarginsInPoints(frame, 0, &content_width, &content_height, | 56 GetPageSizeAndMarginsInPoints(frame, 0, default_settings, |
| 57 &content_width, &content_height, |
| 98 NULL, NULL, NULL, NULL); | 58 NULL, NULL, NULL, NULL); |
| 99 | 59 |
| 100 ViewMsg_Print_Params default_settings; | |
| 101 default_settings.printable_size = gfx::Size( | |
| 102 static_cast<int>(content_width), static_cast<int>(content_height)); | |
| 103 default_settings.dpi = printing::kPointsPerInch; | 60 default_settings.dpi = printing::kPointsPerInch; |
| 104 default_settings.min_shrink = 1.25; | 61 default_settings.min_shrink = 1.25; |
| 105 default_settings.max_shrink = 2.0; | 62 default_settings.max_shrink = 2.0; |
| 106 default_settings.desired_dpi = printing::kPointsPerInch; | 63 default_settings.desired_dpi = printing::kPointsPerInch; |
| 107 default_settings.document_cookie = 0; | 64 default_settings.document_cookie = 0; |
| 108 default_settings.selection_only = false; | 65 default_settings.selection_only = false; |
| 109 | 66 |
| 67 default_settings.printable_size = gfx::Size( |
| 68 static_cast<int>(content_width), static_cast<int>(content_height)); |
| 69 |
| 110 ViewMsg_PrintPages_Params print_settings; | 70 ViewMsg_PrintPages_Params print_settings; |
| 111 print_settings.params = default_settings; | 71 print_settings.params = default_settings; |
| 112 | 72 |
| 113 PrintPages(print_settings, frame); | 73 PrintPages(print_settings, frame); |
| 114 } | 74 } |
| 115 | 75 |
| 116 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 76 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
| 117 WebFrame* frame) { | 77 WebFrame* frame) { |
| 118 PrepareFrameAndViewForPrint prep_frame_view(params.params, | 78 PrepareFrameAndViewForPrint prep_frame_view(params.params, |
| 119 frame, | 79 frame, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 return; | 124 return; |
| 165 | 125 |
| 166 // Tell the browser we've finished writing the file. | 126 // Tell the browser we've finished writing the file. |
| 167 Send(new ViewHostMsg_TempFileForPrintingWritten(fd_in_browser)); | 127 Send(new ViewHostMsg_TempFileForPrintingWritten(fd_in_browser)); |
| 168 } | 128 } |
| 169 | 129 |
| 170 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 130 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 171 const gfx::Size& canvas_size, | 131 const gfx::Size& canvas_size, |
| 172 WebFrame* frame, | 132 WebFrame* frame, |
| 173 printing::NativeMetafile* metafile) { | 133 printing::NativeMetafile* metafile) { |
| 134 ViewMsg_Print_Params default_params; |
| 135 FillDefaultPrintParams(&default_params); |
| 136 |
| 174 double content_width_in_points; | 137 double content_width_in_points; |
| 175 double content_height_in_points; | 138 double content_height_in_points; |
| 176 double margin_top_in_points; | 139 double margin_top_in_points; |
| 177 double margin_right_in_points; | 140 double margin_right_in_points; |
| 178 double margin_bottom_in_points; | 141 double margin_bottom_in_points; |
| 179 double margin_left_in_points; | 142 double margin_left_in_points; |
| 180 GetPageSizeAndMarginsInPoints(frame, | 143 GetPageSizeAndMarginsInPoints(frame, |
| 181 params.page_number, | 144 params.page_number, |
| 145 default_params, |
| 182 &content_width_in_points, | 146 &content_width_in_points, |
| 183 &content_height_in_points, | 147 &content_height_in_points, |
| 184 &margin_top_in_points, | 148 &margin_top_in_points, |
| 185 &margin_right_in_points, | 149 &margin_right_in_points, |
| 186 &margin_bottom_in_points, | 150 &margin_bottom_in_points, |
| 187 &margin_left_in_points); | 151 &margin_left_in_points); |
| 188 | 152 |
| 189 cairo_t* cairo_context = | 153 cairo_t* cairo_context = |
| 190 metafile->StartPage(content_width_in_points, | 154 metafile->StartPage(content_width_in_points, |
| 191 content_height_in_points, | 155 content_height_in_points, |
| 192 margin_top_in_points, | 156 margin_top_in_points, |
| 193 margin_right_in_points, | 157 margin_right_in_points, |
| 194 margin_bottom_in_points, | 158 margin_bottom_in_points, |
| 195 margin_left_in_points); | 159 margin_left_in_points); |
| 196 if (!cairo_context) | 160 if (!cairo_context) |
| 197 return; | 161 return; |
| 198 | 162 |
| 199 skia::VectorCanvas canvas(cairo_context, | 163 skia::VectorCanvas canvas(cairo_context, |
| 200 canvas_size.width(), canvas_size.height()); | 164 canvas_size.width(), canvas_size.height()); |
| 201 frame->printPage(params.page_number, &canvas); | 165 frame->printPage(params.page_number, &canvas); |
| 202 | 166 |
| 203 // TODO(myhuang): We should handle transformation for paper margins. | 167 // TODO(myhuang): We should handle transformation for paper margins. |
| 204 // TODO(myhuang): We should render the header and the footer. | 168 // TODO(myhuang): We should render the header and the footer. |
| 205 | 169 |
| 206 // Done printing. Close the device context to retrieve the compiled metafile. | 170 // Done printing. Close the device context to retrieve the compiled metafile. |
| 207 if (!metafile->FinishPage()) | 171 if (!metafile->FinishPage()) |
| 208 NOTREACHED() << "metafile failed"; | 172 NOTREACHED() << "metafile failed"; |
| 209 } | 173 } |
| OLD | NEW |