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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 59 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
| 60 } | 60 } |
| 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 scoped_ptr<printing::Metafile> draft_metafile; | |
| 69 printing::Metafile* initial_render_metafile = | 70 printing::Metafile* initial_render_metafile = |
| 70 print_preview_context_.metafile(); | 71 print_preview_context_.metafile(); |
| 71 scoped_ptr<printing::PreviewMetafile> draft_metafile; | 72 |
| 72 #if !defined(USE_SKIA) | 73 if (print_preview_context_.IsModifiable() && |
| 73 if (print_preview_context_.IsModifiable()) { | 74 #if defined(USE_SKIA) |
|
vandebo (ex-Chrome)
2011/08/10 17:21:40
The if inside an if is kind of hard to follow. Ho
kmadhusu
2011/08/10 19:01:13
Done.
| |
| 74 draft_metafile.reset(new printing::PreviewMetafile); | 75 is_complete_metafile_sent_) { |
| 76 #else | |
| 77 print_preview_context_.generate_draft_pages()) { | |
|
vandebo (ex-Chrome)
2011/08/10 17:21:40
Maybe add a comment here says that for CG, if we w
kmadhusu
2011/08/10 19:01:13
Done.
| |
| 78 #endif | |
| 79 draft_metafile.reset(new printing::PreviewMetafile()); | |
| 75 if (!draft_metafile->Init()) { | 80 if (!draft_metafile->Init()) { |
| 76 DidFinishPrinting(FAIL_PREVIEW); | 81 DidFinishPrinting(FAIL_PREVIEW); |
| 77 return; | 82 return; |
| 78 } | 83 } |
| 79 initial_render_metafile = draft_metafile.get(); | 84 initial_render_metafile = draft_metafile.get(); |
| 80 } | 85 } |
| 81 #endif | |
| 82 | 86 |
| 83 base::TimeTicks begin_time = base::TimeTicks::Now(); | 87 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 84 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 88 RenderPage(printParams.page_size, content_area, scale_factor, page_number, |
| 85 print_preview_context_.frame(), initial_render_metafile); | 89 print_preview_context_.frame(), initial_render_metafile); |
| 86 print_preview_context_.RenderedPreviewPage( | 90 print_preview_context_.RenderedPreviewPage( |
| 87 base::TimeTicks::Now() - begin_time); | 91 base::TimeTicks::Now() - begin_time); |
| 88 | 92 |
| 89 if (print_preview_context_.IsModifiable()) { | 93 if (draft_metafile.get()) { |
| 94 draft_metafile->FinishDocument(); | |
| 95 #if !defined(USE_SKIA) | |
| 96 if (!is_complete_metafile_sent_) { | |
| 97 // With CG, we rendered into a new metafile so we could get it as a draft | |
| 98 // document. Now we need to add it to complete document. But the | |
| 99 // document has already been scaled and adjusted for margins, so do a 1:1 | |
| 100 // drawing. | |
| 101 printing::Metafile* complete_metafile = print_preview_context_.metafile(); | |
| 102 bool success = complete_metafile->StartPage( | |
| 103 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); | |
| 104 DCHECK(success); | |
| 105 // StartPage unconditionally flips the content over, flip it back since it | |
| 106 // was already flipped in |draft_metafile|. | |
| 107 CGContextTranslateCTM(complete_metafile->context(), 0, | |
| 108 printParams.page_size.height()); | |
| 109 CGContextScaleCTM(complete_metafile->context(), 1.0, -1.0); | |
| 110 draft_metafile->RenderPage(1, | |
| 111 complete_metafile->context(), | |
| 112 draft_metafile->GetPageBounds(1).ToCGRect(), | |
| 113 false /* shrink_to_fit */, | |
| 114 false /* stretch_to_fit */, | |
| 115 true /* center_horizontally */, | |
| 116 true /* center_vertically */); | |
| 117 complete_metafile->FinishPage(); | |
| 118 } | |
| 119 #endif | |
| 120 } else if (print_preview_context_.IsModifiable() && | |
|
vandebo (ex-Chrome)
2011/08/10 17:21:40
This if only has a body for the use skia case. Ca
kmadhusu
2011/08/10 19:01:13
Changed to:
if (draft_metafile.get()) {
..
} else
| |
| 121 print_preview_context_.generate_draft_pages()) { | |
| 90 #if defined(USE_SKIA) | 122 #if defined(USE_SKIA) |
| 91 DCHECK(!draft_metafile.get()); | 123 DCHECK(!draft_metafile.get()); |
| 92 draft_metafile.reset( | 124 draft_metafile.reset( |
| 93 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 125 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
| 94 #else | |
| 95 draft_metafile->FinishDocument(); | |
| 96 | |
| 97 // With CG, we rendered into a new metafile so we could get it as a draft | |
| 98 // document. Now we need to add it to complete document. But the document | |
| 99 // has already been scaled and adjusted for margins, so do a 1:1 drawing. | |
| 100 printing::Metafile* complete_metafile = print_preview_context_.metafile(); | |
| 101 bool success = complete_metafile->StartPage( | |
| 102 printParams.page_size, gfx::Rect(printParams.page_size), 1.0); | |
| 103 DCHECK(success); | |
| 104 // StartPage unconditionally flips the content over, flip it back since it | |
| 105 // was already flipped in |draft_metafile|. | |
| 106 CGContextTranslateCTM(complete_metafile->context(), 0, | |
| 107 printParams.page_size.height()); | |
| 108 CGContextScaleCTM(complete_metafile->context(), 1.0, -1.0); | |
| 109 | |
| 110 draft_metafile->RenderPage(1, | |
| 111 complete_metafile->context(), | |
| 112 draft_metafile->GetPageBounds(1).ToCGRect(), | |
| 113 false /* shrink_to_fit */, | |
| 114 false /* stretch_to_fit */, | |
| 115 true /* center_horizontally */, | |
| 116 true /* center_vertically */); | |
| 117 complete_metafile->FinishPage(); | |
| 118 #endif | 126 #endif |
| 119 } | 127 } |
| 120 | |
| 121 PreviewPageRendered(page_number, draft_metafile.get()); | 128 PreviewPageRendered(page_number, draft_metafile.get()); |
| 122 } | 129 } |
| 123 | 130 |
| 124 void PrintWebViewHelper::RenderPage( | 131 void PrintWebViewHelper::RenderPage( |
| 125 const gfx::Size& page_size, const gfx::Rect& content_area, | 132 const gfx::Size& page_size, const gfx::Rect& content_area, |
| 126 const float& scale_factor, int page_number, WebFrame* frame, | 133 const float& scale_factor, int page_number, WebFrame* frame, |
| 127 printing::Metafile* metafile) { | 134 printing::Metafile* metafile) { |
| 128 | 135 |
| 129 { | 136 { |
| 130 #if defined(USE_SKIA) | 137 #if defined(USE_SKIA) |
| 131 SkDevice* device = metafile->StartPageForVectorCanvas( | 138 SkDevice* device = metafile->StartPageForVectorCanvas( |
| 132 page_size, content_area, scale_factor); | 139 page_size, content_area, scale_factor); |
| 133 if (!device) | 140 if (!device) |
| 134 return; | 141 return; |
| 135 | 142 |
| 136 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 143 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 137 canvas->unref(); // SkRefPtr and new both took a reference. | 144 canvas->unref(); // SkRefPtr and new both took a reference. |
| 138 WebKit::WebCanvas* canvasPtr = canvas.get(); | 145 WebKit::WebCanvas* canvasPtr = canvas.get(); |
| 139 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvasPtr, metafile); | 146 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvasPtr, metafile); |
| 147 printing::MetafileSkiaWrapper::SetDraftMode(canvasPtr, | |
| 148 is_complete_metafile_sent_); | |
| 140 #else | 149 #else |
| 141 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 150 bool success = metafile->StartPage(page_size, content_area, scale_factor); |
| 142 DCHECK(success); | 151 DCHECK(success); |
| 143 // printPage can create autoreleased references to |context|. PDF contexts | 152 // printPage can create autoreleased references to |context|. PDF contexts |
| 144 // don't write all their data until they are destroyed, so we need to make | 153 // don't write all their data until they are destroyed, so we need to make |
| 145 // certain that there are no lingering references. | 154 // certain that there are no lingering references. |
| 146 base::mac::ScopedNSAutoreleasePool pool; | 155 base::mac::ScopedNSAutoreleasePool pool; |
| 147 CGContextRef cgContext = metafile->context(); | 156 CGContextRef cgContext = metafile->context(); |
| 148 CGContextRef canvasPtr = cgContext; | 157 CGContextRef canvasPtr = cgContext; |
| 149 #endif | 158 #endif |
| 150 frame->printPage(page_number, canvasPtr); | 159 frame->printPage(page_number, canvasPtr); |
| 151 } | 160 } |
| 152 | 161 |
| 153 // Done printing. Close the device context to retrieve the compiled metafile. | 162 // Done printing. Close the device context to retrieve the compiled metafile. |
| 154 metafile->FinishPage(); | 163 metafile->FinishPage(); |
| 155 } | 164 } |
| OLD | NEW |