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 "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
11 #include "chrome/common/render_messages_params.h" | 11 #include "chrome/common/render_messages_params.h" |
12 #include "chrome/renderer/render_view.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "printing/native_metafile.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 | 13 |
21 using WebKit::WebFrame; | 14 using WebKit::WebFrame; |
22 using WebKit::WebCanvas; | |
23 using WebKit::WebRect; | |
24 using WebKit::WebSize; | |
25 | 15 |
26 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 16 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
27 const gfx::Size& canvas_size, | 17 const gfx::Size& canvas_size, |
28 WebFrame* frame) { | 18 WebFrame* frame) { |
29 printing::NativeMetafile metafile; | 19 printing::NativeMetafile metafile; |
30 if (!metafile.Init()) | 20 if (!metafile.Init()) |
31 return; | 21 return; |
32 | 22 |
33 float scale_factor = frame->getPrintPageShrink(params.page_number); | 23 float scale_factor = frame->getPrintPageShrink(params.page_number); |
34 double width = params.params.printable_size.width(); | |
35 double height = params.params.printable_size.height(); | |
36 int page_number = params.page_number; | 24 int page_number = params.page_number; |
37 | 25 |
38 // Render page for printing. | 26 // Render page for printing. |
39 gfx::Point origin(0.0f, 0.0f); | 27 gfx::Point origin(0.0f, 0.0f); |
40 RenderPage(params.params.printable_size, origin, scale_factor, page_number, | 28 RenderPage(params.params.printable_size, origin, scale_factor, page_number, |
41 frame, &metafile); | 29 frame, &metafile); |
42 | |
43 metafile.Close(); | 30 metafile.Close(); |
44 | 31 |
45 double margin_left = params.params.margin_left; | |
46 double margin_top = params.params.margin_top; | |
47 | |
48 // Get the size of the compiled metafile. | |
49 ViewHostMsg_DidPrintPage_Params page_params; | 32 ViewHostMsg_DidPrintPage_Params page_params; |
50 page_params.data_size = metafile.GetDataSize(); | 33 page_params.data_size = metafile.GetDataSize(); |
51 page_params.page_number = page_number; | 34 page_params.page_number = page_number; |
52 page_params.document_cookie = params.params.document_cookie; | 35 page_params.document_cookie = params.params.document_cookie; |
53 page_params.actual_shrink = scale_factor; | 36 page_params.actual_shrink = scale_factor; |
54 | |
55 page_params.page_size = params.params.page_size; | 37 page_params.page_size = params.params.page_size; |
56 page_params.content_area = gfx::Rect(margin_left, margin_top, width, height); | 38 page_params.content_area = gfx::Rect(params.params.margin_left, |
| 39 params.params.margin_top, |
| 40 params.params.printable_size.width(), |
| 41 params.params.printable_size.height()); |
57 | 42 |
58 // Ask the browser to create the shared memory for us. | 43 // Ask the browser to create the shared memory for us. |
59 if (!CopyMetafileDataToSharedMem(&metafile, | 44 if (!CopyMetafileDataToSharedMem(&metafile, |
60 &(page_params.metafile_data_handle))) { | 45 &(page_params.metafile_data_handle))) { |
61 page_params.data_size = 0; | 46 page_params.data_size = 0; |
62 } | 47 } |
63 | 48 |
64 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 49 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
65 } | 50 } |
66 | 51 |
67 void PrintWebViewHelper::CreatePreviewDocument( | 52 void PrintWebViewHelper::CreatePreviewDocument( |
68 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { | 53 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { |
69 ViewMsg_Print_Params printParams = params.params; | 54 ViewMsg_Print_Params printParams = params.params; |
70 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); | 55 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); |
71 | 56 |
72 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 57 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
73 frame, NULL, frame->view()); | 58 frame, NULL, frame->view()); |
74 int page_count = prep_frame_view.GetExpectedPageCount(); | 59 int page_count = prep_frame_view.GetExpectedPageCount(); |
75 | 60 |
76 if (!page_count) | 61 if (!page_count) |
77 return; | 62 return; |
78 | 63 |
79 float scale_factor = frame->getPrintPageShrink(0); | |
80 double originX = printParams.margin_left; | |
81 double originY = printParams.margin_top; | |
82 | |
83 printing::NativeMetafile metafile; | 64 printing::NativeMetafile metafile; |
84 if (!metafile.Init()) | 65 if (!metafile.Init()) |
85 return; | 66 return; |
86 | 67 |
87 gfx::Point origin(originX, originY); | 68 float scale_factor = frame->getPrintPageShrink(0); |
88 | 69 gfx::Point origin(printParams.margin_left, printParams.margin_top); |
89 if (params.pages.empty()) { | 70 if (params.pages.empty()) { |
90 for (int i = 0; i < page_count; ++i) { | 71 for (int i = 0; i < page_count; ++i) { |
91 RenderPage(printParams.page_size, origin, scale_factor, i, frame, | 72 RenderPage(printParams.page_size, origin, scale_factor, i, frame, |
92 &metafile); | 73 &metafile); |
93 } | 74 } |
94 } else { | 75 } else { |
95 for (size_t i = 0; i < params.pages.size(); ++i) { | 76 for (size_t i = 0; i < params.pages.size(); ++i) { |
96 if (params.pages[i] >= page_count) | 77 if (params.pages[i] >= page_count) |
97 break; | 78 break; |
98 RenderPage(printParams.page_size, origin, scale_factor, | 79 RenderPage(printParams.page_size, origin, scale_factor, |
(...skipping 25 matching lines...) Expand all Loading... |
124 // printPage can create autoreleased references to |context|. PDF contexts | 105 // printPage can create autoreleased references to |context|. PDF contexts |
125 // don't write all their data until they are destroyed, so we need to make | 106 // don't write all their data until they are destroyed, so we need to make |
126 // certain that there are no lingering references. | 107 // certain that there are no lingering references. |
127 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | 108 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
128 frame->printPage(page_number, context); | 109 frame->printPage(page_number, context); |
129 [pool release]; | 110 [pool release]; |
130 | 111 |
131 // Done printing. Close the device context to retrieve the compiled metafile. | 112 // Done printing. Close the device context to retrieve the compiled metafile. |
132 metafile->FinishPage(); | 113 metafile->FinishPage(); |
133 } | 114 } |
OLD | NEW |