Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: chrome/renderer/print_web_view_helper_win.cc

Issue 6261025: Refactor win printing workflow to support print preview. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/print_web_view_helper.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/logging.h" 7 #include "base/logging.h"
8 #include "base/process_util.h" 8 #include "base/process_util.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"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 // Restore the world transforms of both DC's. 56 // Restore the world transforms of both DC's.
57 SetWorldTransform(dc, &metafile_dc_transform); 57 SetWorldTransform(dc, &metafile_dc_transform);
58 SetWorldTransform(*bitmap_dc, &bitmap_dc_transform); 58 SetWorldTransform(*bitmap_dc, &bitmap_dc_transform);
59 } else { 59 } else {
60 // Play this command to the metafile DC. 60 // Play this command to the metafile DC.
61 PlayEnhMetaFileRecord(dc, handle_table, record, num_objects); 61 PlayEnhMetaFileRecord(dc, handle_table, record, num_objects);
62 } 62 }
63 return 1; // Continue enumeration 63 return 1; // Continue enumeration
64 } 64 }
65 65
66 gfx::Size PrintWebViewHelper::GetUpdatedPageSizeForRendering(
67 const ViewMsg_Print_Params& params, WebFrame* frame, int page_number) {
Lei Zhang 2011/01/26 08:06:52 Have you considered putting this code in RenderPag
kmadhusu 2011/01/26 18:08:29 Moved this code to RenderPage().
68 double content_width_in_points;
69 double content_height_in_points;
70 GetPageSizeAndMarginsInPoints(frame, page_number, params,
71 &content_width_in_points, &content_height_in_points, NULL, NULL, NULL,
72 NULL);
73
74 // Since WebKit extends the page width depending on the magical scale factor
75 // we make sure the canvas covers the worst case scenario (x2.0 currently).
76 // PrintContext will then set the correct clipping region.
77 return gfx::Size(
78 static_cast<int>(content_width_in_points * params.max_shrink),
79 static_cast<int>(content_height_in_points * params.max_shrink));
80 }
81
66 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, 82 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
67 const gfx::Size& canvas_size, 83 const gfx::Size& canvas_size,
68 WebFrame* frame) { 84 WebFrame* frame) {
69 // Generate a memory-based metafile. It will use the current screen's DPI. 85 // Generate a memory-based metafile. It will use the current screen's DPI.
70 // Each metafile contains a single page. 86 // Each metafile contains a single page.
71 scoped_ptr<printing::NativeMetafile> metafile(new printing::NativeMetafile); 87 scoped_ptr<printing::NativeMetafile> metafile(new printing::NativeMetafile);
72 metafile->CreateDc(NULL, NULL); 88 metafile->CreateDc(NULL, NULL);
73 DCHECK(metafile->hdc()); 89 DCHECK(metafile->hdc());
74 skia::PlatformDevice::InitializeDC(metafile->hdc()); 90 skia::PlatformDevice::InitializeDC(metafile->hdc());
75 91
76 int page_number = params.page_number; 92 int page_number = params.page_number;
77 93
78 double content_width_in_points;
79 double content_height_in_points;
80 GetPageSizeAndMarginsInPoints(frame, page_number, params.params,
81 &content_width_in_points, &content_height_in_points, NULL, NULL, NULL,
82 NULL);
83
84 // Calculate the dpi adjustment. 94 // Calculate the dpi adjustment.
85 float scale_factor = static_cast<float>(params.params.desired_dpi / 95 float scale_factor = static_cast<float>(params.params.desired_dpi /
86 params.params.dpi); 96 params.params.dpi);
87 97
88 // Since WebKit extends the page width depending on the magical |scale_factor| 98 gfx::Size page_size = GetUpdatedPageSizeForRendering(params.params, frame,
89 // we make sure the canvas covers the worst case scenario (x2.0 currently). 99 page_number);
90 // PrintContext will then set the correct clipping region.
91 gfx::Size page_size(
92 static_cast<int>(content_width_in_points * params.params.max_shrink),
93 static_cast<int>(content_height_in_points * params.params.max_shrink));
94 100
95 // Render page for printing. 101 // Render page for printing.
96 RenderPage(page_size, &scale_factor, page_number, frame, &metafile, 102 RenderPage(page_size, &scale_factor, page_number, frame, &metafile,
97 params.params.supports_alpha_blend); 103 params.params.supports_alpha_blend);
98 104
99 // Close the device context to retrieve the compiled metafile. 105 // Close the device context to retrieve the compiled metafile.
100 if (!metafile->CloseDc()) 106 if (!metafile->CloseDc())
101 NOTREACHED(); 107 NOTREACHED();
102 108
103 // Get the size of the compiled metafile. 109 // Get the size of the compiled metafile.
(...skipping 18 matching lines...) Expand all
122 } 128 }
123 metafile->CloseEmf(); 129 metafile->CloseEmf();
124 if (Send(new ViewHostMsg_DuplicateSection( 130 if (Send(new ViewHostMsg_DuplicateSection(
125 routing_id(), 131 routing_id(),
126 page_params.metafile_data_handle, 132 page_params.metafile_data_handle,
127 &page_params.metafile_data_handle))) { 133 &page_params.metafile_data_handle))) {
128 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); 134 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params));
129 } 135 }
130 } 136 }
131 137
138 void PrintWebViewHelper::CreatePreviewDocument(
139 const ViewMsg_PrintPages_Params& params, WebFrame* frame,
140 ViewHostMsg_DidPreviewDocument_Params* print_params) {
141 int page_count = 0;
142 ViewMsg_Print_Params printParams = params.params;
Lei Zhang 2011/01/26 08:06:52 You still use params.params in several places belo
Lei Zhang 2011/01/26 08:06:52 Having both |print_params| and |printParams| is co
kmadhusu 2011/01/26 18:08:29 Renamed |print_params| to |preview_params|.
kmadhusu 2011/01/26 18:08:29 To get the expected page count, |printParams| val
143 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams);
144 PrepareFrameAndViewForPrint prep_frame_view(printParams, frame, NULL,
145 frame->view());
146 page_count = prep_frame_view.GetExpectedPageCount();
147 if (!page_count)
148 return;
149
150 // NOTE: This is an enhanced-format metafile(EMF) which has an appearance of
151 // single page metafile. For print preview, we need a metafile with multiple
152 // pages.
153 // TODO(kmadhusu): Use a PDF metafile to support multiple pages. After "Skia
154 // PDF backend" work is completed for windows, make changes to replace this
155 // EMF with PDF metafile.
156 // http://code.google.com/p/chromium/issues/detail?id=62889
157 scoped_ptr<printing::NativeMetafile> metafile(new printing::NativeMetafile);
158 metafile->CreateDc(NULL, NULL);
159 DCHECK(metafile->hdc());
160 skia::PlatformDevice::InitializeDC(metafile->hdc());
161
162 gfx::Size page_size = GetUpdatedPageSizeForRendering(printParams, frame,
Lei Zhang 2011/01/26 08:06:52 The third argument to GetUpdatedPageSizeForRenderi
kmadhusu 2011/01/26 18:08:29 Fixed.
163 page_count);
164 float scale_factor;
Lei Zhang 2011/01/26 08:06:52 declare this in the scope in which it is used.
kmadhusu 2011/01/26 18:08:29 Done.
165
166 // Calculate the dpi adjustment.
167 float shrink = static_cast<float>(params.params.desired_dpi /
Lei Zhang 2011/01/26 08:06:52 but I thought you wanted to get rid of variables n
kmadhusu 2011/01/26 18:08:29 Yeah. Since I am using |scale_factor| variable wit
168 params.params.dpi);
169
170 if (params.pages.empty()) {
171 for (int i = 0; i < page_count; ++i) {
172 scale_factor = shrink;
173 RenderPage(page_size, &scale_factor, i, frame, &metafile,
174 params.params.supports_alpha_blend);
175 }
176 } else {
177 for (size_t i = 0; i < params.pages.size(); ++i) {
178 if (params.pages[i] >= page_count)
179 break;
180 scale_factor = shrink;
181 RenderPage(page_size, &scale_factor, static_cast<int>(params.pages[i]),
182 frame, &metafile, params.params.supports_alpha_blend);
183 }
184 }
185
186 // Close the device context to retrieve the compiled metafile.
187 if (!metafile->CloseDc())
188 NOTREACHED();
189
190 // Get the size of the compiled metafile.
191 uint32 buf_size = metafile->GetDataSize();
192 DCHECK_GT(buf_size, 128u);
193
194 print_params->document_cookie = params.params.document_cookie;
195 print_params->data_size = 0;
196 print_params->metafile_data_handle = NULL;
197
198 if (CopyMetafileDataToSharedMem(metafile.get(),
199 &(print_params->metafile_data_handle))) {
200 print_params->data_size = buf_size;
201 }
202 metafile->CloseEmf();
203 if (!Send(new ViewHostMsg_DuplicateSection(
204 routing_id(),
205 print_params->metafile_data_handle,
206 &print_params->metafile_data_handle))) {
207 NOTREACHED() << "Send message failed.";
208 }
209 }
210
132 void PrintWebViewHelper::RenderPage( 211 void PrintWebViewHelper::RenderPage(
133 const gfx::Size& page_size, float* scale_factor, int page_number, 212 const gfx::Size& page_size, float* scale_factor, int page_number,
134 WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile, 213 WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile,
135 bool supports_alpha_blend) { 214 bool supports_alpha_blend) {
136 HDC hdc = (*metafile)->hdc(); 215 HDC hdc = (*metafile)->hdc();
137 DCHECK(hdc); 216 DCHECK(hdc);
138 217
139 int width = page_size.width(); 218 int width = page_size.width();
140 int height = page_size.height(); 219 int height = page_size.height();
141 #if 0 220 #if 0
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // Copy the bits into shared memory. 336 // Copy the bits into shared memory.
258 if (!metafile->GetData(shared_buf.memory(), buf_size)) { 337 if (!metafile->GetData(shared_buf.memory(), buf_size)) {
259 NOTREACHED() << "GetData() failed"; 338 NOTREACHED() << "GetData() failed";
260 shared_buf.Unmap(); 339 shared_buf.Unmap();
261 return false; 340 return false;
262 } 341 }
263 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); 342 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle);
264 shared_buf.Unmap(); 343 shared_buf.Unmap();
265 return true; 344 return true;
266 } 345 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698