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 18 matching lines...) Expand all Loading... |
29 printing::NativeMetafile metafile; | 29 printing::NativeMetafile metafile; |
30 if (!metafile.Init()) | 30 if (!metafile.Init()) |
31 return; | 31 return; |
32 | 32 |
33 float scale_factor = frame->getPrintPageShrink(params.page_number); | 33 float scale_factor = frame->getPrintPageShrink(params.page_number); |
34 int page_number = params.page_number; | 34 int page_number = params.page_number; |
35 | 35 |
36 // Render page for printing. | 36 // Render page for printing. |
37 gfx::Rect content_area(params.params.printable_size); | 37 gfx::Rect content_area(params.params.printable_size); |
38 RenderPage(params.params.printable_size, content_area, scale_factor, | 38 RenderPage(params.params.printable_size, content_area, scale_factor, |
39 page_number, frame, &metafile); | 39 page_number, frame, false, &metafile); |
40 metafile.FinishDocument(); | 40 metafile.FinishDocument(); |
41 | 41 |
42 PrintHostMsg_DidPrintPage_Params page_params; | 42 PrintHostMsg_DidPrintPage_Params page_params; |
43 page_params.data_size = metafile.GetDataSize(); | 43 page_params.data_size = metafile.GetDataSize(); |
44 page_params.page_number = page_number; | 44 page_params.page_number = page_number; |
45 page_params.document_cookie = params.params.document_cookie; | 45 page_params.document_cookie = params.params.document_cookie; |
46 page_params.actual_shrink = scale_factor; | 46 page_params.actual_shrink = scale_factor; |
47 page_params.page_size = params.params.page_size; | 47 page_params.page_size = params.params.page_size; |
48 page_params.content_area = gfx::Rect(params.params.margin_left, | 48 page_params.content_area = gfx::Rect(params.params.margin_left, |
49 params.params.margin_top, | 49 params.params.margin_top, |
(...skipping 11 matching lines...) Expand all Loading... |
61 | 61 |
62 void PrintWebViewHelper::RenderPreviewPage(int page_number) { | 62 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
63 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | 63 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); |
64 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 64 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
65 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, | 65 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, |
66 printParams.printable_size.width(), | 66 printParams.printable_size.width(), |
67 printParams.printable_size.height()); | 67 printParams.printable_size.height()); |
68 | 68 |
69 base::TimeTicks begin_time = base::TimeTicks::Now(); | 69 base::TimeTicks begin_time = base::TimeTicks::Now(); |
70 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 70 RenderPage(printParams.page_size, content_area, scale_factor, page_number, |
71 print_preview_context_.frame(), print_preview_context_.metafile()); | 71 print_preview_context_.frame(), true, |
| 72 print_preview_context_.metafile()); |
72 print_preview_context_.RenderedPreviewPage( | 73 print_preview_context_.RenderedPreviewPage( |
73 base::TimeTicks::Now() - begin_time); | 74 base::TimeTicks::Now() - begin_time); |
74 PreviewPageRendered(page_number); | 75 PreviewPageRendered(page_number); |
75 } | 76 } |
76 | 77 |
77 void PrintWebViewHelper::RenderPage( | 78 void PrintWebViewHelper::RenderPage( |
78 const gfx::Size& page_size, const gfx::Rect& content_area, | 79 const gfx::Size& page_size, const gfx::Rect& content_area, |
79 const float& scale_factor, int page_number, WebFrame* frame, | 80 const float& scale_factor, int page_number, WebFrame* frame, |
80 printing::Metafile* metafile) { | 81 bool is_preview, printing::Metafile* metafile) { |
81 | 82 |
82 { | 83 { |
83 #if defined(USE_SKIA) | 84 #if defined(USE_SKIA) |
84 SkDevice* device = metafile->StartPageForVectorCanvas( | 85 SkDevice* device = metafile->StartPageForVectorCanvas( |
85 page_number, page_size, content_area, scale_factor); | 86 page_number, page_size, content_area, scale_factor); |
86 if (!device) | 87 if (!device) |
87 return; | 88 return; |
88 | 89 |
89 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 90 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
90 canvas->unref(); // SkRefPtr and new both took a reference. | 91 canvas->unref(); // SkRefPtr and new both took a reference. |
91 WebKit::WebCanvas* canvasPtr = canvas.get(); | 92 WebKit::WebCanvas* canvasPtr = canvas.get(); |
92 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvasPtr, metafile); | 93 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvasPtr, metafile); |
93 #else | 94 #else |
94 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 95 bool success = metafile->StartPage(page_size, content_area, scale_factor); |
95 DCHECK(success); | 96 DCHECK(success); |
96 // printPage can create autoreleased references to |context|. PDF contexts | 97 // printPage can create autoreleased references to |context|. PDF contexts |
97 // don't write all their data until they are destroyed, so we need to make | 98 // don't write all their data until they are destroyed, so we need to make |
98 // certain that there are no lingering references. | 99 // certain that there are no lingering references. |
99 base::mac::ScopedNSAutoreleasePool pool; | 100 base::mac::ScopedNSAutoreleasePool pool; |
100 CGContextRef cgContext = metafile->context(); | 101 CGContextRef cgContext = metafile->context(); |
101 CGContextRef canvasPtr = cgContext; | 102 CGContextRef canvasPtr = cgContext; |
102 #endif | 103 #endif |
103 frame->printPage(page_number, canvasPtr); | 104 float webkit_scale_factor = frame->printPage(page_number, canvasPtr); |
| 105 #if defined(USE_SKIA) |
| 106 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
| 107 if (is_preview && printParams.display_header_footer) { |
| 108 PageSizeMargins page_layout_in_points; |
| 109 GetPageSizeAndMarginsInPoints(frame, page_number, printParams, |
| 110 &page_layout_in_points); |
| 111 // |page_number| is 0-based, so 1 is added. |
| 112 PrintHeaderAndFooter(device, *canvas, page_number + 1, |
| 113 print_preview_context_.total_page_count(), |
| 114 webkit_scale_factor, page_layout_in_points, |
| 115 header_footer_info_); |
| 116 } |
| 117 #endif |
104 } | 118 } |
105 | 119 |
106 // Done printing. Close the device context to retrieve the compiled metafile. | 120 // Done printing. Close the device context to retrieve the compiled metafile. |
107 metafile->FinishPage(); | 121 metafile->FinishPage(); |
108 } | 122 } |
OLD | NEW |