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

Side by Side Diff: chrome/renderer/print_web_view_helper_mac.mm

Issue 8585017: PrintPreview: Honor the print media page size and margin values. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix conflicts Created 9 years 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
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 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/mac/scoped_nsautorelease_pool.h" 10 #include "base/mac/scoped_nsautorelease_pool.h"
(...skipping 14 matching lines...) Expand all
25 using WebKit::WebFrame; 25 using WebKit::WebFrame;
26 26
27 void PrintWebViewHelper::PrintPageInternal( 27 void PrintWebViewHelper::PrintPageInternal(
28 const PrintMsg_PrintPage_Params& params, 28 const PrintMsg_PrintPage_Params& params,
29 const gfx::Size& canvas_size, 29 const gfx::Size& canvas_size,
30 WebFrame* frame) { 30 WebFrame* frame) {
31 printing::NativeMetafile metafile; 31 printing::NativeMetafile metafile;
32 if (!metafile.Init()) 32 if (!metafile.Init())
33 return; 33 return;
34 34
35 float scale_factor = frame->getPrintPageShrink(params.page_number); 35 double scale_factor = frame->getPrintPageShrink(params.page_number);
36 int page_number = params.page_number; 36 int page_number = params.page_number;
37 37
38 // Render page for printing. 38 // Render page for printing.
39 printing::PageSizeMargins page_layout_in_points;
40 gfx::Size page_size(params.params.content_size);
39 gfx::Rect content_area(params.params.content_size); 41 gfx::Rect content_area(params.params.content_size);
40 RenderPage(params.params.content_size, content_area, scale_factor, 42
41 page_number, frame, false, &metafile); 43 #if defined(USE_SKIA)
vandebo (ex-Chrome) 2011/12/14 19:05:38 Probably should note the bug number that will trac
kmadhusu 2011/12/19 09:52:19 I have made code changes to honor the css settings
44 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number,
45 print_pages_params_->params,
46 ignore_css_margins_, fit_to_page_,
47 &scale_factor, &page_layout_in_points);
48 UpdatePageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
49 &content_area);
50 #else
51 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number,
52 print_pages_params_->params, false, false, NULL,
53 &page_layout_in_points);
54 #endif
55
56 RenderPage(page_size, content_area, scale_factor, page_number, frame, false,
57 &metafile, page_layout_in_points);
42 metafile.FinishDocument(); 58 metafile.FinishDocument();
43 59
44 PrintHostMsg_DidPrintPage_Params page_params; 60 PrintHostMsg_DidPrintPage_Params page_params;
45 page_params.data_size = metafile.GetDataSize(); 61 page_params.data_size = metafile.GetDataSize();
46 page_params.page_number = page_number; 62 page_params.page_number = page_number;
47 page_params.document_cookie = params.params.document_cookie; 63 page_params.document_cookie = params.params.document_cookie;
48 page_params.actual_shrink = scale_factor; 64 page_params.actual_shrink = scale_factor;
49 page_params.page_size = params.params.page_size; 65 page_params.page_size = page_size;
50 page_params.content_area = gfx::Rect(params.params.margin_left, 66 page_params.content_area = content_area;
51 params.params.margin_top,
52 params.params.content_size.width(),
53 params.params.content_size.height());
54 67
55 // Ask the browser to create the shared memory for us. 68 // Ask the browser to create the shared memory for us.
56 if (!CopyMetafileDataToSharedMem(&metafile, 69 if (!CopyMetafileDataToSharedMem(&metafile,
57 &(page_params.metafile_data_handle))) { 70 &(page_params.metafile_data_handle))) {
58 page_params.data_size = 0; 71 page_params.data_size = 0;
59 } 72 }
60 73
61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); 74 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
62 } 75 }
63 76
64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { 77 bool PrintWebViewHelper::RenderPreviewPage(int page_number) {
65 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); 78 double scale_factor = print_preview_context_.frame()->getPrintPageShrink(0);
66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); 79 PrintMsg_Print_Params printParams = print_preview_context_.print_params();
80 printing::PageSizeMargins page_layout_in_points;
81 gfx::Size page_size(printParams.page_size);
67 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, 82 gfx::Rect content_area(printParams.margin_left, printParams.margin_top,
68 printParams.content_size.width(), 83 printParams.content_size.width(),
69 printParams.content_size.height()); 84 printParams.content_size.height());
70 85
86 #if defined(USE_SKIA)
87 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number,
88 print_pages_params_->params,
89 ignore_css_margins_, fit_to_page_,
90 &scale_factor, &page_layout_in_points);
91 UpdatePageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
92 &content_area);
93 #else
94 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number,
95 print_pages_params_->params, false, false, NULL,
96 &page_layout_in_points);
97 #endif
98
71 scoped_ptr<printing::Metafile> draft_metafile; 99 scoped_ptr<printing::Metafile> draft_metafile;
72 printing::Metafile* initial_render_metafile = 100 printing::Metafile* initial_render_metafile =
73 print_preview_context_.metafile(); 101 print_preview_context_.metafile();
74 102
75 #if defined(USE_SKIA) 103 #if defined(USE_SKIA)
76 bool render_to_draft = print_preview_context_.IsModifiable() && 104 bool render_to_draft = print_preview_context_.IsModifiable() &&
77 is_print_ready_metafile_sent_; 105 is_print_ready_metafile_sent_;
78 #else 106 #else
79 // If the page needs to be in both draft metafile and print ready metafile, 107 // If the page needs to be in both draft metafile and print ready metafile,
80 // we should always render to the draft metafile first and then copy that 108 // we should always render to the draft metafile first and then copy that
81 // into the print ready metafile because CG does not allow us to do it in 109 // into the print ready metafile because CG does not allow us to do it in
82 // the other order. 110 // the other order.
83 bool render_to_draft = print_preview_context_.IsModifiable() && 111 bool render_to_draft = print_preview_context_.IsModifiable() &&
84 print_preview_context_.generate_draft_pages(); 112 print_preview_context_.generate_draft_pages();
85 #endif 113 #endif
86 114
87 if (render_to_draft) { 115 if (render_to_draft) {
88 draft_metafile.reset(new printing::PreviewMetafile()); 116 draft_metafile.reset(new printing::PreviewMetafile());
89 if (!draft_metafile->Init()) { 117 if (!draft_metafile->Init()) {
90 print_preview_context_.set_error( 118 print_preview_context_.set_error(
91 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); 119 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED);
92 LOG(ERROR) << "Draft PreviewMetafile Init failed"; 120 LOG(ERROR) << "Draft PreviewMetafile Init failed";
93 return false; 121 return false;
94 } 122 }
95 initial_render_metafile = draft_metafile.get(); 123 initial_render_metafile = draft_metafile.get();
96 } 124 }
97 125
98 base::TimeTicks begin_time = base::TimeTicks::Now(); 126 base::TimeTicks begin_time = base::TimeTicks::Now();
99 RenderPage(printParams.page_size, content_area, scale_factor, page_number, 127 RenderPage(page_size, content_area, scale_factor, page_number,
100 print_preview_context_.frame(), true, initial_render_metafile); 128 print_preview_context_.frame(), true, initial_render_metafile,
129 page_layout_in_points);
101 print_preview_context_.RenderedPreviewPage( 130 print_preview_context_.RenderedPreviewPage(
102 base::TimeTicks::Now() - begin_time); 131 base::TimeTicks::Now() - begin_time);
103 132
104 if (draft_metafile.get()) { 133 if (draft_metafile.get()) {
105 draft_metafile->FinishDocument(); 134 draft_metafile->FinishDocument();
106 #if !defined(USE_SKIA) 135 #if !defined(USE_SKIA)
107 if (!is_print_ready_metafile_sent_) { 136 if (!is_print_ready_metafile_sent_) {
108 // With CG, we rendered into a new metafile so we could get it as a draft 137 // With CG, we rendered into a new metafile so we could get it as a draft
109 // document. Now we need to add it to print ready document. But the 138 // document. Now we need to add it to print ready document. But the
110 // document has already been scaled and adjusted for margins, so do a 1:1 139 // document has already been scaled and adjusted for margins, so do a 1:1
111 // drawing. 140 // drawing.
112 printing::Metafile* print_ready_metafile = 141 printing::Metafile* print_ready_metafile =
113 print_preview_context_.metafile(); 142 print_preview_context_.metafile();
114 bool success = print_ready_metafile->StartPage( 143 bool success = print_ready_metafile->StartPage(page_size,
115 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); 144 gfx::Rect(page_size), 1.0);
116 DCHECK(success); 145 DCHECK(success);
117 // StartPage unconditionally flips the content over, flip it back since it 146 // StartPage unconditionally flips the content over, flip it back since it
118 // was already flipped in |draft_metafile|. 147 // was already flipped in |draft_metafile|.
119 CGContextTranslateCTM(print_ready_metafile->context(), 0, 148 CGContextTranslateCTM(print_ready_metafile->context(), 0,
120 printParams.page_size.height()); 149 page_size.height());
121 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); 150 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0);
122 draft_metafile->RenderPage(1, 151 draft_metafile->RenderPage(1,
123 print_ready_metafile->context(), 152 print_ready_metafile->context(),
124 draft_metafile->GetPageBounds(1).ToCGRect(), 153 draft_metafile->GetPageBounds(1).ToCGRect(),
125 false /* shrink_to_fit */, 154 false /* shrink_to_fit */,
126 false /* stretch_to_fit */, 155 false /* stretch_to_fit */,
127 true /* center_horizontally */, 156 true /* center_horizontally */,
128 true /* center_vertically */); 157 true /* center_vertically */);
129 print_ready_metafile->FinishPage(); 158 print_ready_metafile->FinishPage();
130 } 159 }
131 #endif 160 #endif
132 } else { 161 } else {
133 #if defined(USE_SKIA) 162 #if defined(USE_SKIA)
134 if (print_preview_context_.IsModifiable() && 163 if (print_preview_context_.IsModifiable() &&
135 print_preview_context_.generate_draft_pages()) { 164 print_preview_context_.generate_draft_pages()) {
136 DCHECK(!draft_metafile.get()); 165 DCHECK(!draft_metafile.get());
137 draft_metafile.reset( 166 draft_metafile.reset(
138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); 167 print_preview_context_.metafile()->GetMetafileForCurrentPage());
139 } 168 }
140 #endif 169 #endif
141 } 170 }
142 return PreviewPageRendered(page_number, draft_metafile.get()); 171 return PreviewPageRendered(page_number, draft_metafile.get());
143 } 172 }
144 173
145 void PrintWebViewHelper::RenderPage( 174 void PrintWebViewHelper::RenderPage(
146 const gfx::Size& page_size, const gfx::Rect& content_area, 175 const gfx::Size& page_size, const gfx::Rect& content_area,
147 const float& scale_factor, int page_number, WebFrame* frame, 176 const double& scale_factor, int page_number, WebFrame* frame,
148 bool is_preview, printing::Metafile* metafile) { 177 bool is_preview, printing::Metafile* metafile,
178 printing::PageSizeMargins page_layout_in_points) {
149 179
150 { 180 {
151 #if defined(USE_SKIA) 181 #if defined(USE_SKIA)
152 SkDevice* device = metafile->StartPageForVectorCanvas( 182 SkDevice* device = metafile->StartPageForVectorCanvas(
153 page_size, content_area, scale_factor); 183 page_size, content_area, scale_factor);
154 if (!device) 184 if (!device)
155 return; 185 return;
156 186
157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); 187 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device);
158 canvas->unref(); // SkRefPtr and new both took a reference. 188 canvas->unref(); // SkRefPtr and new both took a reference.
159 WebKit::WebCanvas* canvas_ptr = canvas.get(); 189 WebKit::WebCanvas* canvas_ptr = canvas.get();
160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); 190 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); 191 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_);
162 skia::SetIsPreviewMetafile(*canvas, is_preview); 192 skia::SetIsPreviewMetafile(*canvas, is_preview);
163 #else 193 #else
164 bool success = metafile->StartPage(page_size, content_area, scale_factor); 194 bool success = metafile->StartPage(page_size, content_area, scale_factor);
165 DCHECK(success); 195 DCHECK(success);
166 // printPage can create autoreleased references to |context|. PDF contexts 196 // printPage can create autoreleased references to |context|. PDF contexts
167 // don't write all their data until they are destroyed, so we need to make 197 // don't write all their data until they are destroyed, so we need to make
168 // certain that there are no lingering references. 198 // certain that there are no lingering references.
169 base::mac::ScopedNSAutoreleasePool pool; 199 base::mac::ScopedNSAutoreleasePool pool;
170 CGContextRef cgContext = metafile->context(); 200 CGContextRef cgContext = metafile->context();
171 CGContextRef canvas_ptr = cgContext; 201 CGContextRef canvas_ptr = cgContext;
172 #endif 202 #endif
173 203
174 printing::PageSizeMargins page_layout_in_points;
175 GetPageSizeAndMarginsInPoints(frame, page_number,
176 print_pages_params_->params,
177 &page_layout_in_points);
178
179 #if !defined(USE_SKIA) 204 #if !defined(USE_SKIA)
180 // For CoreGraphics, print in the margins before printing in the content 205 // For CoreGraphics, print in the margins before printing in the content
181 // area so that we don't spill over. Webkit draws a white background in the 206 // area so that we don't spill over. Webkit draws a white background in the
182 // content area and this acts as a clip. 207 // content area and this acts as a clip.
183 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we 208 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we
184 // can draw in the margins safely in Skia in any order. 209 // can draw in the margins safely in Skia in any order.
185 if (print_pages_params_->params.display_header_footer) { 210 if (print_pages_params_->params.display_header_footer) {
186 PrintHeaderAndFooter(canvas_ptr, page_number + 1, 211 PrintHeaderAndFooter(canvas_ptr, page_number + 1,
187 print_preview_context_.total_page_count(), 212 print_preview_context_.total_page_count(),
188 scale_factor, page_layout_in_points, 213 scale_factor, page_layout_in_points,
(...skipping 10 matching lines...) Expand all
199 print_preview_context_.total_page_count(), 224 print_preview_context_.total_page_count(),
200 scale_factor, page_layout_in_points, 225 scale_factor, page_layout_in_points,
201 *header_footer_info_); 226 *header_footer_info_);
202 } 227 }
203 #endif // defined(USE_SKIA) 228 #endif // defined(USE_SKIA)
204 } 229 }
205 230
206 // Done printing. Close the device context to retrieve the compiled metafile. 231 // Done printing. Close the device context to retrieve the compiled metafile.
207 metafile->FinishPage(); 232 metafile->FinishPage();
208 } 233 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698