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 = 1.0f; |
| 36 int page_number = params.page_number; | 36 int page_number = params.page_number; |
| 37 | 37 gfx::Size page_size; |
| 38 // Render page for printing. | 38 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, |
| 39 gfx::Rect content_area(params.params.content_size); | 39 &scale_factor, &page_size); |
| 40 RenderPage(params.params.content_size, content_area, scale_factor, | |
| 41 page_number, frame, false, &metafile); | |
| 42 metafile.FinishDocument(); | 40 metafile.FinishDocument(); |
| 43 | 41 |
| 44 PrintHostMsg_DidPrintPage_Params page_params; | 42 PrintHostMsg_DidPrintPage_Params page_params; |
| 45 page_params.data_size = metafile.GetDataSize(); | 43 page_params.data_size = metafile.GetDataSize(); |
| 46 page_params.page_number = page_number; | 44 page_params.page_number = page_number; |
| 47 page_params.document_cookie = params.params.document_cookie; | 45 page_params.document_cookie = params.params.document_cookie; |
| 48 page_params.actual_shrink = scale_factor; | 46 page_params.actual_shrink = scale_factor; |
| 49 page_params.page_size = params.params.page_size; | 47 page_params.page_size = page_size; |
| 50 page_params.content_area = gfx::Rect(params.params.margin_left, | 48 page_params.content_area = gfx::Rect(0, 0, page_size.width(), |
|
vandebo (ex-Chrome)
2012/01/03 21:55:12
Does this still work with the native flow?
kmadhusu
2012/01/04 16:55:35
Yes. Still working on a fix to address this issue.
kmadhusu
2012/01/05 01:15:47
Fixed. Modified pdf_metafile_cg_mac::RenderPage()
| |
| 51 params.params.margin_top, | 49 page_size.height()); |
| 52 params.params.content_size.width(), | |
| 53 params.params.content_size.height()); | |
| 54 | |
| 55 // Ask the browser to create the shared memory for us. | 50 // Ask the browser to create the shared memory for us. |
| 56 if (!CopyMetafileDataToSharedMem(&metafile, | 51 if (!CopyMetafileDataToSharedMem(&metafile, |
| 57 &(page_params.metafile_data_handle))) { | 52 &(page_params.metafile_data_handle))) { |
| 58 page_params.data_size = 0; | 53 page_params.data_size = 0; |
| 59 } | 54 } |
| 60 | 55 |
| 61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 56 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
| 62 } | 57 } |
| 63 | 58 |
| 64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { | 59 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { |
| 65 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | 60 double scale_factor = 1.0f; |
| 66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 61 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
| 67 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, | |
| 68 printParams.content_size.width(), | |
| 69 printParams.content_size.height()); | |
| 70 | |
| 71 scoped_ptr<printing::Metafile> draft_metafile; | 62 scoped_ptr<printing::Metafile> draft_metafile; |
| 72 printing::Metafile* initial_render_metafile = | 63 printing::Metafile* initial_render_metafile = |
| 73 print_preview_context_.metafile(); | 64 print_preview_context_.metafile(); |
| 74 | 65 |
| 75 #if defined(USE_SKIA) | 66 #if defined(USE_SKIA) |
| 76 bool render_to_draft = print_preview_context_.IsModifiable() && | 67 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 77 is_print_ready_metafile_sent_; | 68 is_print_ready_metafile_sent_; |
| 78 #else | 69 #else |
| 79 // If the page needs to be in both draft metafile and print ready metafile, | 70 // 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 | 71 // 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 | 72 // into the print ready metafile because CG does not allow us to do it in |
| 82 // the other order. | 73 // the other order. |
| 83 bool render_to_draft = print_preview_context_.IsModifiable() && | 74 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 84 print_preview_context_.generate_draft_pages(); | 75 print_preview_context_.generate_draft_pages(); |
| 85 #endif | 76 #endif |
| 86 | 77 |
| 87 if (render_to_draft) { | 78 if (render_to_draft) { |
| 88 draft_metafile.reset(new printing::PreviewMetafile()); | 79 draft_metafile.reset(new printing::PreviewMetafile()); |
| 89 if (!draft_metafile->Init()) { | 80 if (!draft_metafile->Init()) { |
| 90 print_preview_context_.set_error( | 81 print_preview_context_.set_error( |
| 91 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); | 82 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); |
| 92 LOG(ERROR) << "Draft PreviewMetafile Init failed"; | 83 LOG(ERROR) << "Draft PreviewMetafile Init failed"; |
| 93 return false; | 84 return false; |
| 94 } | 85 } |
| 95 initial_render_metafile = draft_metafile.get(); | 86 initial_render_metafile = draft_metafile.get(); |
| 96 } | 87 } |
| 97 | 88 |
| 98 base::TimeTicks begin_time = base::TimeTicks::Now(); | 89 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 99 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 90 gfx::Size page_size; |
| 100 print_preview_context_.frame(), true, initial_render_metafile); | 91 RenderPage(printParams, page_number, print_preview_context_.frame(), true, |
| 92 initial_render_metafile, &scale_factor, &page_size); | |
| 101 print_preview_context_.RenderedPreviewPage( | 93 print_preview_context_.RenderedPreviewPage( |
| 102 base::TimeTicks::Now() - begin_time); | 94 base::TimeTicks::Now() - begin_time); |
| 103 | 95 |
| 104 if (draft_metafile.get()) { | 96 if (draft_metafile.get()) { |
| 105 draft_metafile->FinishDocument(); | 97 draft_metafile->FinishDocument(); |
| 106 #if !defined(USE_SKIA) | 98 #if !defined(USE_SKIA) |
| 107 if (!is_print_ready_metafile_sent_) { | 99 if (!is_print_ready_metafile_sent_) { |
| 108 // With CG, we rendered into a new metafile so we could get it as a draft | 100 // 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 | 101 // 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 | 102 // document has already been scaled and adjusted for margins, so do a 1:1 |
| 111 // drawing. | 103 // drawing. |
| 112 printing::Metafile* print_ready_metafile = | 104 printing::Metafile* print_ready_metafile = |
| 113 print_preview_context_.metafile(); | 105 print_preview_context_.metafile(); |
| 114 bool success = print_ready_metafile->StartPage( | 106 bool success = print_ready_metafile->StartPage(page_size, |
| 115 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); | 107 gfx::Rect(page_size), 1.0); |
| 116 DCHECK(success); | 108 DCHECK(success); |
| 117 // StartPage unconditionally flips the content over, flip it back since it | 109 // StartPage unconditionally flips the content over, flip it back since it |
| 118 // was already flipped in |draft_metafile|. | 110 // was already flipped in |draft_metafile|. |
| 119 CGContextTranslateCTM(print_ready_metafile->context(), 0, | 111 CGContextTranslateCTM(print_ready_metafile->context(), 0, |
| 120 printParams.page_size.height()); | 112 page_size.height()); |
| 121 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); | 113 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); |
| 122 draft_metafile->RenderPage(1, | 114 draft_metafile->RenderPage(1, |
| 123 print_ready_metafile->context(), | 115 print_ready_metafile->context(), |
| 124 draft_metafile->GetPageBounds(1).ToCGRect(), | 116 draft_metafile->GetPageBounds(1).ToCGRect(), |
| 125 false /* shrink_to_fit */, | 117 false /* shrink_to_fit */, |
| 126 false /* stretch_to_fit */, | 118 false /* stretch_to_fit */, |
| 127 true /* center_horizontally */, | 119 true /* center_horizontally */, |
| 128 true /* center_vertically */); | 120 true /* center_vertically */); |
| 129 print_ready_metafile->FinishPage(); | 121 print_ready_metafile->FinishPage(); |
| 130 } | 122 } |
| 131 #endif | 123 #endif |
| 132 } else { | 124 } else { |
| 133 #if defined(USE_SKIA) | 125 #if defined(USE_SKIA) |
| 134 if (print_preview_context_.IsModifiable() && | 126 if (print_preview_context_.IsModifiable() && |
| 135 print_preview_context_.generate_draft_pages()) { | 127 print_preview_context_.generate_draft_pages()) { |
| 136 DCHECK(!draft_metafile.get()); | 128 DCHECK(!draft_metafile.get()); |
| 137 draft_metafile.reset( | 129 draft_metafile.reset( |
| 138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 130 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
| 139 } | 131 } |
| 140 #endif | 132 #endif |
| 141 } | 133 } |
| 142 return PreviewPageRendered(page_number, draft_metafile.get()); | 134 return PreviewPageRendered(page_number, draft_metafile.get()); |
| 143 } | 135 } |
| 144 | 136 |
| 145 void PrintWebViewHelper::RenderPage( | 137 void PrintWebViewHelper::RenderPage( |
| 146 const gfx::Size& page_size, const gfx::Rect& content_area, | 138 const PrintMsg_Print_Params& params, int page_number, WebFrame* frame, |
| 147 const float& scale_factor, int page_number, WebFrame* frame, | 139 bool is_preview, printing::Metafile* metafile, double* scale_factor, |
| 148 bool is_preview, printing::Metafile* metafile) { | 140 gfx::Size* page_size) { |
| 141 double webkit_shrink_factor = frame->getPrintPageShrink(page_number); | |
| 142 float webkit_scale_factor = 1.0; | |
| 143 printing::PageSizeMargins page_layout_in_points; | |
| 144 gfx::Rect content_area; | |
| 149 | 145 |
| 146 ComputePageLayout(frame, page_number, params, ignore_css_margins_, | |
| 147 fit_to_page_, scale_factor, &page_layout_in_points); | |
| 148 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size, | |
| 149 &content_area); | |
| 150 *scale_factor *= webkit_shrink_factor; | |
| 150 { | 151 { |
| 151 #if defined(USE_SKIA) | 152 #if defined(USE_SKIA) |
| 152 SkDevice* device = metafile->StartPageForVectorCanvas( | 153 SkDevice* device = metafile->StartPageForVectorCanvas( |
| 153 page_size, content_area, scale_factor); | 154 *page_size, content_area, *scale_factor); |
| 154 if (!device) | 155 if (!device) |
| 155 return; | 156 return; |
| 156 | 157 |
| 157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 158 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 158 canvas->unref(); // SkRefPtr and new both took a reference. | 159 canvas->unref(); // SkRefPtr and new both took a reference. |
| 159 WebKit::WebCanvas* canvas_ptr = canvas.get(); | 160 WebKit::WebCanvas* canvas_ptr = canvas.get(); |
| 160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 161 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
| 161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 162 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
| 162 skia::SetIsPreviewMetafile(*canvas, is_preview); | 163 skia::SetIsPreviewMetafile(*canvas, is_preview); |
| 163 #else | 164 #else |
| 164 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 165 bool success = metafile->StartPage(*page_size, content_area, *scale_factor); |
| 165 DCHECK(success); | 166 DCHECK(success); |
| 166 // printPage can create autoreleased references to |context|. PDF contexts | 167 // 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 | 168 // don't write all their data until they are destroyed, so we need to make |
| 168 // certain that there are no lingering references. | 169 // certain that there are no lingering references. |
| 169 base::mac::ScopedNSAutoreleasePool pool; | 170 base::mac::ScopedNSAutoreleasePool pool; |
| 170 CGContextRef cgContext = metafile->context(); | 171 CGContextRef cgContext = metafile->context(); |
| 171 CGContextRef canvas_ptr = cgContext; | 172 CGContextRef canvas_ptr = cgContext; |
| 172 #endif | 173 #endif |
| 173 | 174 |
| 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) | 175 #if !defined(USE_SKIA) |
|
vandebo (ex-Chrome)
2012/01/03 21:55:12
Combine ifdef block with previous.
kmadhusu
2012/01/04 16:55:35
Done.
| |
| 180 // For CoreGraphics, print in the margins before printing in the content | 176 // 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 | 177 // area so that we don't spill over. Webkit draws a white background in the |
| 182 // content area and this acts as a clip. | 178 // content area and this acts as a clip. |
| 183 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we | 179 // TODO(aayushkumar): Combine the calls to PrintHeaderAndFooter once we |
| 184 // can draw in the margins safely in Skia in any order. | 180 // can draw in the margins safely in Skia in any order. |
| 185 if (print_pages_params_->params.display_header_footer) { | 181 if (print_pages_params_->params.display_header_footer) { |
| 186 PrintHeaderAndFooter(canvas_ptr, page_number + 1, | 182 PrintHeaderAndFooter(canvas_ptr, page_number + 1, |
| 187 print_preview_context_.total_page_count(), | 183 print_preview_context_.total_page_count(), |
| 188 scale_factor, page_layout_in_points, | 184 *scale_factor, page_layout_in_points, |
| 189 *header_footer_info_); | 185 *header_footer_info_); |
| 190 } | 186 } |
| 191 #endif // !USE_SKIA | 187 #endif // !USE_SKIA |
| 192 | 188 |
| 193 frame->printPage(page_number, canvas_ptr); | 189 webkit_scale_factor = frame->printPage(page_number, canvas_ptr); |
| 194 | 190 |
| 195 #if defined(USE_SKIA) | 191 #if defined(USE_SKIA) |
| 196 if (print_pages_params_->params.display_header_footer) { | 192 if (print_pages_params_->params.display_header_footer) { |
| 197 // |page_number| is 0-based, so 1 is added. | 193 // |page_number| is 0-based, so 1 is added. |
| 198 PrintHeaderAndFooter(canvas_ptr, page_number + 1, | 194 PrintHeaderAndFooter(canvas_ptr, page_number + 1, |
| 199 print_preview_context_.total_page_count(), | 195 print_preview_context_.total_page_count(), |
| 200 scale_factor, page_layout_in_points, | 196 *scale_factor, page_layout_in_points, |
| 201 *header_footer_info_); | 197 *header_footer_info_); |
| 202 } | 198 } |
| 203 #endif // defined(USE_SKIA) | 199 #endif // defined(USE_SKIA) |
| 204 } | 200 } |
| 201 *scale_factor /= webkit_scale_factor; | |
| 205 | 202 |
| 206 // Done printing. Close the device context to retrieve the compiled metafile. | 203 // Done printing. Close the device context to retrieve the compiled metafile. |
| 207 metafile->FinishPage(); | 204 metafile->FinishPage(); |
| 208 } | 205 } |
| OLD | NEW |