| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 printing::Metafile* initial_render_metafile = | 69 printing::Metafile* initial_render_metafile = |
| 70 print_preview_context_.metafile(); | 70 print_preview_context_.metafile(); |
| 71 scoped_ptr<printing::Metafile> draft_metafile; | 71 scoped_ptr<printing::PreviewMetafile> draft_metafile; |
| 72 #if !defined(USE_SKIA) | 72 #if !defined(USE_SKIA) |
| 73 if (print_preview_context_.IsModifiable()) { | 73 if (print_preview_context_.IsModifiable()) { |
| 74 draft_metafile.reset(new printing::PreviewMetafile); | 74 draft_metafile.reset(new printing::PreviewMetafile); |
| 75 if (!draft_metafile->Init()) { | 75 if (!draft_metafile->Init()) { |
| 76 DidFinishPrinting(FAIL_PREVIEW); | 76 DidFinishPrinting(FAIL_PREVIEW); |
| 77 return; | 77 return; |
| 78 } | 78 } |
| 79 initial_render_metafile = draft_metafile.get(); | 79 initial_render_metafile = draft_metafile.get(); |
| 80 } | 80 } |
| 81 #endif | 81 #endif |
| 82 | 82 |
| 83 base::TimeTicks begin_time = base::TimeTicks::Now(); | 83 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 84 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 84 RenderPage(printParams.page_size, content_area, scale_factor, page_number, |
| 85 print_preview_context_.frame(), initial_render_metafile); | 85 print_preview_context_.frame(), initial_render_metafile); |
| 86 print_preview_context_.RenderedPreviewPage( | 86 print_preview_context_.RenderedPreviewPage( |
| 87 base::TimeTicks::Now() - begin_time); | 87 base::TimeTicks::Now() - begin_time); |
| 88 | 88 |
| 89 if (print_preview_context_.IsModifiable()) { | 89 if (print_preview_context_.IsModifiable()) { |
| 90 #if defined(USE_SKIA) | 90 #if defined(USE_SKIA) |
| 91 DCHECK(!draft_metafile.get()); | 91 DCHECK(!draft_metafile.get()); |
| 92 draft_metafile.reset(reinterpret_cast<printing::PreviewMetafile*>( | 92 draft_metafile.reset(reinterpret_cast<printing::PreviewMetafile*>( |
| 93 print_preview_context_.metafile())->GetMetafileForCurrentPage()); | 93 print_preview_context_.metafile())->GetMetafileForCurrentPage()); |
| 94 #else | 94 #else |
| 95 draft_metafile->FinishDocument(); | 95 draft_metafile->FinishDocument(); |
| 96 | 96 |
| 97 // With CG, we rendered into a new metafile so we could get it as a draft | 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. | 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 |
| 99 draft_metafile->RenderPage(1, | 110 draft_metafile->RenderPage(1, |
| 100 print_preview_context_.metafile()->context(), | 111 complete_metafile->context(), |
| 101 CGRectMake(content_area.x(), content_area.y(), | 112 draft_metafile->GetPageBounds(1).ToCGRect(), |
| 102 content_area.width(), | 113 false /* shrink_to_fit */, |
| 103 content_area.height()), | 114 false /* stretch_to_fit */, |
| 104 false /*shrunk_to_fit*/, | 115 true /* center_horizontally */, |
| 105 false /*stretch_to_fit*/, | 116 true /* center_vertically */); |
| 106 true /*center_horizontally*/, | 117 complete_metafile->FinishPage(); |
| 107 true /*center_vertically*/); | |
| 108 #endif | 118 #endif |
| 109 } | 119 } |
| 110 | 120 |
| 111 PreviewPageRendered(page_number, draft_metafile.get()); | 121 PreviewPageRendered(page_number, draft_metafile.get()); |
| 112 } | 122 } |
| 113 | 123 |
| 114 void PrintWebViewHelper::RenderPage( | 124 void PrintWebViewHelper::RenderPage( |
| 115 const gfx::Size& page_size, const gfx::Rect& content_area, | 125 const gfx::Size& page_size, const gfx::Rect& content_area, |
| 116 const float& scale_factor, int page_number, WebFrame* frame, | 126 const float& scale_factor, int page_number, WebFrame* frame, |
| 117 printing::Metafile* metafile) { | 127 printing::Metafile* metafile) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 base::mac::ScopedNSAutoreleasePool pool; | 146 base::mac::ScopedNSAutoreleasePool pool; |
| 137 CGContextRef cgContext = metafile->context(); | 147 CGContextRef cgContext = metafile->context(); |
| 138 CGContextRef canvasPtr = cgContext; | 148 CGContextRef canvasPtr = cgContext; |
| 139 #endif | 149 #endif |
| 140 frame->printPage(page_number, canvasPtr); | 150 frame->printPage(page_number, canvasPtr); |
| 141 } | 151 } |
| 142 | 152 |
| 143 // Done printing. Close the device context to retrieve the compiled metafile. | 153 // Done printing. Close the device context to retrieve the compiled metafile. |
| 144 metafile->FinishPage(); | 154 metafile->FinishPage(); |
| 145 } | 155 } |
| OLD | NEW |