| 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); | |
| 36 int page_number = params.page_number; | 35 int page_number = params.page_number; |
| 37 | 36 gfx::Size page_size_in_dpi; |
| 38 // Render page for printing. | 37 gfx::Rect content_area_in_dpi; |
| 39 gfx::Rect content_area(params.params.content_size); | 38 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, |
| 40 RenderPage(params.params.content_size, content_area, scale_factor, | 39 &page_size_in_dpi, &content_area_in_dpi); |
| 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.page_size = page_size_in_dpi; |
| 49 page_params.page_size = params.params.page_size; | 47 page_params.content_area = content_area_in_dpi; |
| 50 page_params.content_area = gfx::Rect(params.params.margin_left, | |
| 51 params.params.margin_top, | |
| 52 params.params.content_size.width(), | |
| 53 params.params.content_size.height()); | |
| 54 | 48 |
| 55 // Ask the browser to create the shared memory for us. | 49 // Ask the browser to create the shared memory for us. |
| 56 if (!CopyMetafileDataToSharedMem(&metafile, | 50 if (!CopyMetafileDataToSharedMem(&metafile, |
| 57 &(page_params.metafile_data_handle))) { | 51 &(page_params.metafile_data_handle))) { |
| 58 page_params.data_size = 0; | 52 page_params.data_size = 0; |
| 59 } | 53 } |
| 60 | 54 |
| 61 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 55 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
| 62 } | 56 } |
| 63 | 57 |
| 64 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { | 58 bool PrintWebViewHelper::RenderPreviewPage(int page_number) { |
| 65 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | |
| 66 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 59 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; | 60 scoped_ptr<printing::Metafile> draft_metafile; |
| 72 printing::Metafile* initial_render_metafile = | 61 printing::Metafile* initial_render_metafile = |
| 73 print_preview_context_.metafile(); | 62 print_preview_context_.metafile(); |
| 74 | 63 |
| 75 #if defined(USE_SKIA) | 64 #if defined(USE_SKIA) |
| 76 bool render_to_draft = print_preview_context_.IsModifiable() && | 65 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 77 is_print_ready_metafile_sent_; | 66 is_print_ready_metafile_sent_; |
| 78 #else | 67 #else |
| 79 // If the page needs to be in both draft metafile and print ready metafile, | 68 // 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 | 69 // 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 | 70 // into the print ready metafile because CG does not allow us to do it in |
| 82 // the other order. | 71 // the other order. |
| 83 bool render_to_draft = print_preview_context_.IsModifiable() && | 72 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 84 print_preview_context_.generate_draft_pages(); | 73 print_preview_context_.generate_draft_pages(); |
| 85 #endif | 74 #endif |
| 86 | 75 |
| 87 if (render_to_draft) { | 76 if (render_to_draft) { |
| 88 draft_metafile.reset(new printing::PreviewMetafile()); | 77 draft_metafile.reset(new printing::PreviewMetafile()); |
| 89 if (!draft_metafile->Init()) { | 78 if (!draft_metafile->Init()) { |
| 90 print_preview_context_.set_error( | 79 print_preview_context_.set_error( |
| 91 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); | 80 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); |
| 92 LOG(ERROR) << "Draft PreviewMetafile Init failed"; | 81 LOG(ERROR) << "Draft PreviewMetafile Init failed"; |
| 93 return false; | 82 return false; |
| 94 } | 83 } |
| 95 initial_render_metafile = draft_metafile.get(); | 84 initial_render_metafile = draft_metafile.get(); |
| 96 } | 85 } |
| 97 | 86 |
| 98 base::TimeTicks begin_time = base::TimeTicks::Now(); | 87 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 99 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 88 gfx::Size page_size; |
| 100 print_preview_context_.frame(), true, initial_render_metafile); | 89 RenderPage(printParams, page_number, print_preview_context_.frame(), true, |
| 90 initial_render_metafile, &page_size, NULL); |
| 101 print_preview_context_.RenderedPreviewPage( | 91 print_preview_context_.RenderedPreviewPage( |
| 102 base::TimeTicks::Now() - begin_time); | 92 base::TimeTicks::Now() - begin_time); |
| 103 | 93 |
| 104 if (draft_metafile.get()) { | 94 if (draft_metafile.get()) { |
| 105 draft_metafile->FinishDocument(); | 95 draft_metafile->FinishDocument(); |
| 106 #if !defined(USE_SKIA) | 96 #if !defined(USE_SKIA) |
| 107 if (!is_print_ready_metafile_sent_) { | 97 if (!is_print_ready_metafile_sent_) { |
| 108 // With CG, we rendered into a new metafile so we could get it as a draft | 98 // 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 | 99 // 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 | 100 // document has already been scaled and adjusted for margins, so do a 1:1 |
| 111 // drawing. | 101 // drawing. |
| 112 printing::Metafile* print_ready_metafile = | 102 printing::Metafile* print_ready_metafile = |
| 113 print_preview_context_.metafile(); | 103 print_preview_context_.metafile(); |
| 114 bool success = print_ready_metafile->StartPage( | 104 bool success = print_ready_metafile->StartPage(page_size, |
| 115 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); | 105 gfx::Rect(page_size), 1.0); |
| 116 DCHECK(success); | 106 DCHECK(success); |
| 117 // StartPage unconditionally flips the content over, flip it back since it | 107 // StartPage unconditionally flips the content over, flip it back since it |
| 118 // was already flipped in |draft_metafile|. | 108 // was already flipped in |draft_metafile|. |
| 119 CGContextTranslateCTM(print_ready_metafile->context(), 0, | 109 CGContextTranslateCTM(print_ready_metafile->context(), 0, |
| 120 printParams.page_size.height()); | 110 page_size.height()); |
| 121 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); | 111 CGContextScaleCTM(print_ready_metafile->context(), 1.0, -1.0); |
| 122 draft_metafile->RenderPage(1, | 112 draft_metafile->RenderPage(1, |
| 123 print_ready_metafile->context(), | 113 print_ready_metafile->context(), |
| 124 draft_metafile->GetPageBounds(1).ToCGRect(), | 114 draft_metafile->GetPageBounds(1).ToCGRect(), |
| 125 false /* shrink_to_fit */, | 115 false /* shrink_to_fit */, |
| 126 false /* stretch_to_fit */, | 116 false /* stretch_to_fit */, |
| 127 true /* center_horizontally */, | 117 true /* center_horizontally */, |
| 128 true /* center_vertically */); | 118 true /* center_vertically */); |
| 129 print_ready_metafile->FinishPage(); | 119 print_ready_metafile->FinishPage(); |
| 130 } | 120 } |
| 131 #endif | 121 #endif |
| 132 } else { | 122 } else { |
| 133 #if defined(USE_SKIA) | 123 #if defined(USE_SKIA) |
| 134 if (print_preview_context_.IsModifiable() && | 124 if (print_preview_context_.IsModifiable() && |
| 135 print_preview_context_.generate_draft_pages()) { | 125 print_preview_context_.generate_draft_pages()) { |
| 136 DCHECK(!draft_metafile.get()); | 126 DCHECK(!draft_metafile.get()); |
| 137 draft_metafile.reset( | 127 draft_metafile.reset( |
| 138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 128 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
| 139 } | 129 } |
| 140 #endif | 130 #endif |
| 141 } | 131 } |
| 142 return PreviewPageRendered(page_number, draft_metafile.get()); | 132 return PreviewPageRendered(page_number, draft_metafile.get()); |
| 143 } | 133 } |
| 144 | 134 |
| 145 void PrintWebViewHelper::RenderPage( | 135 void PrintWebViewHelper::RenderPage( |
| 146 const gfx::Size& page_size, const gfx::Rect& content_area, | 136 const PrintMsg_Print_Params& params, int page_number, WebFrame* frame, |
| 147 const float& scale_factor, int page_number, WebFrame* frame, | 137 bool is_preview, printing::Metafile* metafile, gfx::Size* page_size, |
| 148 bool is_preview, printing::Metafile* metafile) { | 138 gfx::Rect* content_rect) { |
| 139 double scale_factor = 1.0f; |
| 140 double webkit_shrink_factor = frame->getPrintPageShrink(page_number); |
| 141 printing::PageSizeMargins page_layout_in_points; |
| 142 gfx::Rect content_area; |
| 149 | 143 |
| 144 ComputePageLayoutInPointsForCss(frame, page_number, params, |
| 145 ignore_css_margins_, fit_to_page_, |
| 146 &scale_factor, &page_layout_in_points); |
| 147 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size, |
| 148 &content_area); |
| 149 if (content_rect) |
| 150 *content_rect = content_area; |
| 151 |
| 152 scale_factor *= webkit_shrink_factor; |
| 150 { | 153 { |
| 151 #if defined(USE_SKIA) | 154 #if defined(USE_SKIA) |
| 152 SkDevice* device = metafile->StartPageForVectorCanvas( | 155 SkDevice* device = metafile->StartPageForVectorCanvas( |
| 153 page_size, content_area, scale_factor); | 156 *page_size, content_area, scale_factor); |
| 154 if (!device) | 157 if (!device) |
| 155 return; | 158 return; |
| 156 | 159 |
| 157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 160 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 158 canvas->unref(); // SkRefPtr and new both took a reference. | 161 canvas->unref(); // SkRefPtr and new both took a reference. |
| 159 WebKit::WebCanvas* canvas_ptr = canvas.get(); | 162 WebKit::WebCanvas* canvas_ptr = canvas.get(); |
| 160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 163 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
| 161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 164 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
| 162 skia::SetIsPreviewMetafile(*canvas, is_preview); | 165 skia::SetIsPreviewMetafile(*canvas, is_preview); |
| 163 #else | 166 #else |
| 164 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 167 bool success = metafile->StartPage(*page_size, content_area, scale_factor); |
| 165 DCHECK(success); | 168 DCHECK(success); |
| 166 // printPage can create autoreleased references to |context|. PDF contexts | 169 // 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 | 170 // don't write all their data until they are destroyed, so we need to make |
| 168 // certain that there are no lingering references. | 171 // certain that there are no lingering references. |
| 169 base::mac::ScopedNSAutoreleasePool pool; | 172 base::mac::ScopedNSAutoreleasePool pool; |
| 170 CGContextRef cgContext = metafile->context(); | 173 CGContextRef cgContext = metafile->context(); |
| 171 CGContextRef canvas_ptr = cgContext; | 174 CGContextRef canvas_ptr = cgContext; |
| 172 #endif | |
| 173 | 175 |
| 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) | |
| 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 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 } |
| 205 | 201 |
| 206 // Done printing. Close the device context to retrieve the compiled metafile. | 202 // Done printing. Close the device context to retrieve the compiled metafile. |
| 207 metafile->FinishPage(); | 203 metafile->FinishPage(); |
| 208 } | 204 } |
| OLD | NEW |