| 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 "chrome/common/render_messages_params.h" | 10 #include "chrome/common/render_messages_params.h" |
| 11 #include "printing/native_metafile.h" | 11 #include "printing/native_metafile.h" |
| 12 #include "printing/units.h" | |
| 13 #include "skia/ext/vector_canvas.h" | 12 #include "skia/ext/vector_canvas.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" |
| 16 | 15 |
| 17 using printing::NativeMetafile; | 16 using printing::NativeMetafile; |
| 18 using WebKit::WebFrame; | 17 using WebKit::WebFrame; |
| 19 using WebKit::WebSize; | 18 using WebKit::WebSize; |
| 20 | 19 |
| 21 static void FillDefaultPrintParams(ViewMsg_Print_Params* params) { | |
| 22 // TODO(myhuang): Get printing parameters via IPC | |
| 23 // using the print_web_view_helper.cc version of Print. | |
| 24 // For testing purpose, we hard-coded printing parameters here. | |
| 25 | |
| 26 // The paper size is US Letter (8.5 in. by 11 in.). | |
| 27 double page_width_in_pixel = 8.5 * printing::kPixelsPerInch; | |
| 28 double page_height_in_pixel = 11.0 * printing::kPixelsPerInch; | |
| 29 params->page_size = gfx::Size( | |
| 30 static_cast<int>(page_width_in_pixel), | |
| 31 static_cast<int>(page_height_in_pixel)); | |
| 32 params->printable_size = gfx::Size( | |
| 33 static_cast<int>( | |
| 34 page_width_in_pixel - | |
| 35 (NativeMetafile::kLeftMarginInInch + | |
| 36 NativeMetafile::kRightMarginInInch) * printing::kPixelsPerInch), | |
| 37 static_cast<int>( | |
| 38 page_height_in_pixel - | |
| 39 (NativeMetafile::kTopMarginInInch + | |
| 40 NativeMetafile::kBottomMarginInInch) * printing::kPixelsPerInch)); | |
| 41 params->margin_top = static_cast<int>( | |
| 42 NativeMetafile::kTopMarginInInch * printing::kPixelsPerInch); | |
| 43 params->margin_left = static_cast<int>( | |
| 44 NativeMetafile::kLeftMarginInInch * printing::kPixelsPerInch); | |
| 45 params->dpi = printing::kPixelsPerInch; | |
| 46 params->desired_dpi = params->dpi; | |
| 47 } | |
| 48 | |
| 49 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 20 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
| 50 WebFrame* frame) { | 21 WebFrame* frame) { |
| 51 PrepareFrameAndViewForPrint prep_frame_view(params.params, | 22 PrepareFrameAndViewForPrint prep_frame_view(params.params, |
| 52 frame, | 23 frame, |
| 53 frame->view()); | 24 frame->view()); |
| 54 int page_count = prep_frame_view.GetExpectedPageCount(); | 25 int page_count = prep_frame_view.GetExpectedPageCount(); |
| 55 | 26 |
| 56 // TODO(myhuang): Send ViewHostMsg_DidGetPrintedPagesCount. | 27 // TODO(myhuang): Send ViewHostMsg_DidGetPrintedPagesCount. |
| 57 | 28 |
| 58 if (page_count == 0) | 29 if (page_count == 0) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return; | 68 return; |
| 98 | 69 |
| 99 // Tell the browser we've finished writing the file. | 70 // Tell the browser we've finished writing the file. |
| 100 Send(new ViewHostMsg_TempFileForPrintingWritten(fd_in_browser)); | 71 Send(new ViewHostMsg_TempFileForPrintingWritten(fd_in_browser)); |
| 101 } | 72 } |
| 102 | 73 |
| 103 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 74 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 104 const gfx::Size& canvas_size, | 75 const gfx::Size& canvas_size, |
| 105 WebFrame* frame, | 76 WebFrame* frame, |
| 106 printing::NativeMetafile* metafile) { | 77 printing::NativeMetafile* metafile) { |
| 107 ViewMsg_Print_Params default_params; | |
| 108 FillDefaultPrintParams(&default_params); | |
| 109 | |
| 110 double content_width_in_points; | 78 double content_width_in_points; |
| 111 double content_height_in_points; | 79 double content_height_in_points; |
| 112 double margin_top_in_points; | 80 double margin_top_in_points; |
| 113 double margin_right_in_points; | 81 double margin_right_in_points; |
| 114 double margin_bottom_in_points; | 82 double margin_bottom_in_points; |
| 115 double margin_left_in_points; | 83 double margin_left_in_points; |
| 116 GetPageSizeAndMarginsInPoints(frame, | 84 GetPageSizeAndMarginsInPoints(frame, |
| 117 params.page_number, | 85 params.page_number, |
| 118 default_params, | 86 params.params, |
| 119 &content_width_in_points, | 87 &content_width_in_points, |
| 120 &content_height_in_points, | 88 &content_height_in_points, |
| 121 &margin_top_in_points, | 89 &margin_top_in_points, |
| 122 &margin_right_in_points, | 90 &margin_right_in_points, |
| 123 &margin_bottom_in_points, | 91 &margin_bottom_in_points, |
| 124 &margin_left_in_points); | 92 &margin_left_in_points); |
| 125 | 93 |
| 126 cairo_t* cairo_context = | 94 cairo_t* cairo_context = |
| 127 metafile->StartPage(content_width_in_points, | 95 metafile->StartPage(content_width_in_points, |
| 128 content_height_in_points, | 96 content_height_in_points, |
| 129 margin_top_in_points, | 97 margin_top_in_points, |
| 130 margin_right_in_points, | 98 margin_right_in_points, |
| 131 margin_bottom_in_points, | 99 margin_bottom_in_points, |
| 132 margin_left_in_points); | 100 margin_left_in_points); |
| 133 if (!cairo_context) | 101 if (!cairo_context) |
| 134 return; | 102 return; |
| 135 | 103 |
| 136 skia::VectorCanvas canvas(cairo_context, | 104 skia::VectorCanvas canvas(cairo_context, |
| 137 canvas_size.width(), canvas_size.height()); | 105 canvas_size.width(), canvas_size.height()); |
| 138 frame->printPage(params.page_number, &canvas); | 106 frame->printPage(params.page_number, &canvas); |
| 139 | 107 |
| 140 // TODO(myhuang): We should handle transformation for paper margins. | 108 // TODO(myhuang): We should handle transformation for paper margins. |
| 141 // TODO(myhuang): We should render the header and the footer. | 109 // TODO(myhuang): We should render the header and the footer. |
| 142 | 110 |
| 143 // Done printing. Close the device context to retrieve the compiled metafile. | 111 // Done printing. Close the device context to retrieve the compiled metafile. |
| 144 if (!metafile->FinishPage()) | 112 if (!metafile->FinishPage()) |
| 145 NOTREACHED() << "metafile failed"; | 113 NOTREACHED() << "metafile failed"; |
| 146 } | 114 } |
| OLD | NEW |