Chromium Code Reviews| 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 "base/mac/scoped_nsautorelease_pool.h" | 10 #include "base/mac/scoped_nsautorelease_pool.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 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.printable_size); | |
| 39 gfx::Rect content_area(params.params.printable_size); | 41 gfx::Rect content_area(params.params.printable_size); |
| 40 RenderPage(params.params.printable_size, content_area, scale_factor, | 42 |
| 41 page_number, frame, false, &metafile); | 43 #if defined(USE_SKIA) |
|
vandebo (ex-Chrome)
2011/12/04 22:20:34
I thought that the mac-skia flow was already diffe
kmadhusu
2011/12/05 09:06:54
In the mac-cg workflow, we are doing some scaling
| |
| 44 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number, | |
| 45 print_pages_params_->params, | |
| 46 ignore_frame_margins_css_, fit_to_page_, | |
| 47 &scale_factor, &page_layout_in_points); | |
| 48 page_size = gfx::Size( | |
|
vandebo (ex-Chrome)
2011/12/04 22:20:34
Will this calculation of page_size come out to the
kmadhusu
2011/12/05 09:06:54
When source is html (with a special margin & size
| |
| 49 page_layout_in_points.content_width + | |
| 50 page_layout_in_points.margin_right + | |
| 51 page_layout_in_points.margin_left, | |
| 52 page_layout_in_points.content_height + | |
| 53 page_layout_in_points.margin_top + | |
| 54 page_layout_in_points.margin_bottom); | |
| 55 content_area = gfx::Rect(page_layout_in_points.margin_left, | |
| 56 page_layout_in_points.margin_top, | |
| 57 page_layout_in_points.content_width, | |
| 58 page_layout_in_points.content_height); | |
| 59 #else | |
| 60 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number, | |
| 61 print_pages_params_->params, false, false, NULL, | |
| 62 &page_layout_in_points); | |
| 63 #endif | |
| 64 | |
| 65 RenderPage(page_size, content_area, scale_factor, page_number, frame, false, | |
| 66 &metafile, page_layout_in_points); | |
| 42 metafile.FinishDocument(); | 67 metafile.FinishDocument(); |
| 43 | 68 |
| 44 PrintHostMsg_DidPrintPage_Params page_params; | 69 PrintHostMsg_DidPrintPage_Params page_params; |
| 45 page_params.data_size = metafile.GetDataSize(); | 70 page_params.data_size = metafile.GetDataSize(); |
| 46 page_params.page_number = page_number; | 71 page_params.page_number = page_number; |
| 47 page_params.document_cookie = params.params.document_cookie; | 72 page_params.document_cookie = params.params.document_cookie; |
| 48 page_params.actual_shrink = scale_factor; | 73 page_params.actual_shrink = scale_factor; |
| 49 page_params.page_size = params.params.page_size; | 74 page_params.page_size = page_size; |
| 50 page_params.content_area = gfx::Rect(params.params.margin_left, | 75 page_params.content_area = content_area; |
| 51 params.params.margin_top, | |
| 52 params.params.printable_size.width(), | |
| 53 params.params.printable_size.height()); | |
| 54 | 76 |
| 55 // Ask the browser to create the shared memory for us. | 77 // Ask the browser to create the shared memory for us. |
| 56 if (!CopyMetafileDataToSharedMem(&metafile, | 78 if (!CopyMetafileDataToSharedMem(&metafile, |
| 57 &(page_params.metafile_data_handle))) { | 79 &(page_params.metafile_data_handle))) { |
| 58 page_params.data_size = 0; | 80 page_params.data_size = 0; |
| 59 } | 81 } |
| 60 | 82 |
| 61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 83 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
| 62 } | 84 } |
| 63 | 85 |
| 64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { | 86 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { |
| 65 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | 87 double scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); |
| 66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 88 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
| 89 printing::PageSizeMargins page_layout_in_points; | |
| 90 gfx::Size page_size(printParams.page_size); | |
| 67 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, | 91 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, |
| 68 printParams.printable_size.width(), | 92 printParams.printable_size.width(), |
| 69 printParams.printable_size.height()); | 93 printParams.printable_size.height()); |
| 70 | 94 |
| 95 #if defined(USE_SKIA) | |
|
vandebo (ex-Chrome)
2011/12/04 22:20:34
Should this get pulled out into a common function?
kmadhusu
2011/12/05 09:06:54
I created a helper function to update the page siz
| |
| 96 // scale_factor = 1.0f; | |
|
vandebo (ex-Chrome)
2011/12/04 22:20:34
extra
kmadhusu
2011/12/05 09:06:54
Removed.
| |
| 97 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number, | |
| 98 print_pages_params_->params, | |
| 99 ignore_frame_margins_css_, fit_to_page_, | |
| 100 &scale_factor, &page_layout_in_points); | |
| 101 page_size = gfx::Size( | |
| 102 page_layout_in_points.content_width + | |
| 103 page_layout_in_points.margin_right + | |
| 104 page_layout_in_points.margin_left, | |
| 105 page_layout_in_points.content_height + | |
| 106 page_layout_in_points.margin_top + | |
| 107 page_layout_in_points.margin_bottom); | |
| 108 content_area = gfx::Rect(page_layout_in_points.margin_left, | |
| 109 page_layout_in_points.margin_top, | |
| 110 page_layout_in_points.content_width, | |
| 111 page_layout_in_points.content_height); | |
| 112 #else | |
| 113 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), page_number, | |
| 114 print_pages_params_->params, false, false, NULL, | |
| 115 &page_layout_in_points); | |
| 116 #endif | |
| 117 | |
| 71 scoped_ptr<printing::Metafile> draft_metafile; | 118 scoped_ptr<printing::Metafile> draft_metafile; |
| 72 printing::Metafile* initial_render_metafile = | 119 printing::Metafile* initial_render_metafile = |
| 73 print_preview_context_.metafile(); | 120 print_preview_context_.metafile(); |
| 74 | 121 |
| 75 #if defined(USE_SKIA) | 122 #if defined(USE_SKIA) |
| 76 bool render_to_draft = print_preview_context_.IsModifiable() && | 123 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 77 is_print_ready_metafile_sent_; | 124 is_print_ready_metafile_sent_; |
| 78 #else | 125 #else |
| 79 // If the page needs to be in both draft metafile and print ready metafile, | 126 // 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 | 127 // 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 | 128 // into the print ready metafile because CG does not allow us to do it in |
| 82 // the other order. | 129 // the other order. |
| 83 bool render_to_draft = print_preview_context_.IsModifiable() && | 130 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 84 print_preview_context_.generate_draft_pages(); | 131 print_preview_context_.generate_draft_pages(); |
| 85 #endif | 132 #endif |
| 86 | 133 |
| 87 if (render_to_draft) { | 134 if (render_to_draft) { |
| 88 draft_metafile.reset(new printing::PreviewMetafile()); | 135 draft_metafile.reset(new printing::PreviewMetafile()); |
| 89 if (!draft_metafile->Init()) { | 136 if (!draft_metafile->Init()) { |
| 90 print_preview_context_.set_error( | 137 print_preview_context_.set_error( |
| 91 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); | 138 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); |
| 92 LOG(ERROR) << "Draft PreviewMetafile Init failed"; | 139 LOG(ERROR) << "Draft PreviewMetafile Init failed"; |
| 93 return false; | 140 return false; |
| 94 } | 141 } |
| 95 initial_render_metafile = draft_metafile.get(); | 142 initial_render_metafile = draft_metafile.get(); |
| 96 } | 143 } |
| 97 | 144 |
| 98 base::TimeTicks begin_time = base::TimeTicks::Now(); | 145 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 99 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 146 RenderPage(page_size, content_area, scale_factor, page_number, |
| 100 print_preview_context_.frame(), true, initial_render_metafile); | 147 print_preview_context_.frame(), true, initial_render_metafile, |
| 148 page_layout_in_points); | |
| 101 print_preview_context_.RenderedPreviewPage( | 149 print_preview_context_.RenderedPreviewPage( |
| 102 base::TimeTicks::Now() - begin_time); | 150 base::TimeTicks::Now() - begin_time); |
| 103 | 151 |
| 104 if (draft_metafile.get()) { | 152 if (draft_metafile.get()) { |
| 105 draft_metafile->FinishDocument(); | 153 draft_metafile->FinishDocument(); |
| 106 #if !defined(USE_SKIA) | 154 #if !defined(USE_SKIA) |
| 107 if (!is_print_ready_metafile_sent_) { | 155 if (!is_print_ready_metafile_sent_) { |
| 108 // With CG, we rendered into a new metafile so we could get it as a draft | 156 // 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 | 157 // 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 | 158 // document has already been scaled and adjusted for margins, so do a 1:1 |
| 111 // drawing. | 159 // drawing. |
| 112 printing::Metafile* print_ready_metafile = | 160 printing::Metafile* print_ready_metafile = |
| 113 print_preview_context_.metafile(); | 161 print_preview_context_.metafile(); |
| 114 bool success = print_ready_metafile->StartPage( | 162 bool success = print_ready_metafile->StartPage(page_size, |
| 115 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); | 163 gfx::Rect(page_size), 1.0); |
| 116 DCHECK(success); | 164 DCHECK(success); |
| 117 // StartPage unconditionally flips the content over, flip it back since it | 165 // StartPage unconditionally flips the content over, flip it back since it |
| 118 // was already flipped in |draft_metafile|. | 166 // was already flipped in |draft_metafile|. |
| 119 CGContextTranslateCTM(print_ready_metafile->context(), 0, | 167 CGContextTranslateCTM(print_ready_metafile->context(), 0, |
| 120 printParams.page_size.height()); | 168 page_size.height()); |
| 121 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); | 169 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); |
| 122 draft_metafile->RenderPage(1, | 170 draft_metafile->RenderPage(1, |
| 123 print_ready_metafile->context(), | 171 print_ready_metafile->context(), |
| 124 draft_metafile->GetPageBounds(1).ToCGRect(), | 172 draft_metafile->GetPageBounds(1).ToCGRect(), |
| 125 false /* shrink_to_fit */, | 173 false /* shrink_to_fit */, |
| 126 false /* stretch_to_fit */, | 174 false /* stretch_to_fit */, |
| 127 true /* center_horizontally */, | 175 true /* center_horizontally */, |
| 128 true /* center_vertically */); | 176 true /* center_vertically */); |
| 129 print_ready_metafile->FinishPage(); | 177 print_ready_metafile->FinishPage(); |
| 130 } | 178 } |
| 131 #endif | 179 #endif |
| 132 } else { | 180 } else { |
| 133 #if defined(USE_SKIA) | 181 #if defined(USE_SKIA) |
| 134 if (print_preview_context_.IsModifiable() && | 182 if (print_preview_context_.IsModifiable() && |
| 135 print_preview_context_.generate_draft_pages()) { | 183 print_preview_context_.generate_draft_pages()) { |
| 136 DCHECK(!draft_metafile.get()); | 184 DCHECK(!draft_metafile.get()); |
| 137 draft_metafile.reset( | 185 draft_metafile.reset( |
| 138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 186 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
| 139 } | 187 } |
| 140 #endif | 188 #endif |
| 141 } | 189 } |
| 142 return PreviewPageRendered(page_number, draft_metafile.get()); | 190 return PreviewPageRendered(page_number, draft_metafile.get()); |
| 143 } | 191 } |
| 144 | 192 |
| 145 void PrintWebViewHelper::RenderPage( | 193 void PrintWebViewHelper::RenderPage( |
| 146 const gfx::Size& page_size, const gfx::Rect& content_area, | 194 const gfx::Size& page_size, const gfx::Rect& content_area, |
| 147 const float& scale_factor, int page_number, WebFrame* frame, | 195 const double& scale_factor, int page_number, WebFrame* frame, |
| 148 bool is_preview, printing::Metafile* metafile) { | 196 bool is_preview, printing::Metafile* metafile, |
| 197 printing::PageSizeMargins page_layout_in_points) { | |
| 149 | 198 |
| 150 { | 199 { |
| 151 #if defined(USE_SKIA) | 200 #if defined(USE_SKIA) |
| 152 SkDevice* device = metafile->StartPageForVectorCanvas( | 201 SkDevice* device = metafile->StartPageForVectorCanvas( |
| 153 page_size, content_area, scale_factor); | 202 page_size, content_area, scale_factor); |
| 154 if (!device) | 203 if (!device) |
| 155 return; | 204 return; |
| 156 | 205 |
| 157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 206 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 158 canvas->unref(); // SkRefPtr and new both took a reference. | 207 canvas->unref(); // SkRefPtr and new both took a reference. |
| 159 WebKit::WebCanvas* canvas_ptr = canvas.get(); | 208 WebKit::WebCanvas* canvas_ptr = canvas.get(); |
| 160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 209 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
| 161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 210 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
| 162 skia::SetIsPreviewMetafile(*canvas, is_preview); | 211 skia::SetIsPreviewMetafile(*canvas, is_preview); |
| 163 #else | 212 #else |
| 164 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 213 bool success = metafile->StartPage(page_size, content_area, scale_factor); |
| 165 DCHECK(success); | 214 DCHECK(success); |
| 166 // printPage can create autoreleased references to |context|. PDF contexts | 215 // 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 | 216 // don't write all their data until they are destroyed, so we need to make |
| 168 // certain that there are no lingering references. | 217 // certain that there are no lingering references. |
| 169 base::mac::ScopedNSAutoreleasePool pool; | 218 base::mac::ScopedNSAutoreleasePool pool; |
| 170 CGContextRef cgContext = metafile->context(); | 219 CGContextRef cgContext = metafile->context(); |
| 171 CGContextRef canvas_ptr = cgContext; | 220 CGContextRef canvas_ptr = cgContext; |
| 172 #endif | 221 #endif |
| 173 | 222 |
| 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) | 223 #if !defined(USE_SKIA) |
| 180 // For CoreGraphics, print in the margins before printing in the content | 224 // 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 | 225 // area so that we don't spill over. Webkit draws a white background in the |
| 182 // content area and this acts as a clip. | 226 // content area and this acts as a clip. |
| 183 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we | 227 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we |
| 184 // can draw in the margins safely in Skia in any order. | 228 // can draw in the margins safely in Skia in any order. |
| 185 if (print_pages_params_->params.display_header_footer) { | 229 if (print_pages_params_->params.display_header_footer) { |
| 186 PrintHeaderAndFooter(canvas_ptr, page_number + 1, | 230 PrintHeaderAndFooter(canvas_ptr, page_number + 1, |
| 187 print_preview_context_.total_page_count(), | 231 print_preview_context_.total_page_count(), |
| 188 scale_factor, page_layout_in_points, | 232 scale_factor, page_layout_in_points, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 199 print_preview_context_.total_page_count(), | 243 print_preview_context_.total_page_count(), |
| 200 scale_factor, page_layout_in_points, | 244 scale_factor, page_layout_in_points, |
| 201 *header_footer_info_); | 245 *header_footer_info_); |
| 202 } | 246 } |
| 203 #endif // defined(USE_SKIA) | 247 #endif // defined(USE_SKIA) |
| 204 } | 248 } |
| 205 | 249 |
| 206 // Done printing. Close the device context to retrieve the compiled metafile. | 250 // Done printing. Close the device context to retrieve the compiled metafile. |
| 207 metafile->FinishPage(); | 251 metafile->FinishPage(); |
| 208 } | 252 } |
| OLD | NEW |