Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: src/chrome/renderer/print_web_view_helper.cc

Issue 196021: Implement PrintPageAsJPEG feature for Print Preview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/
Patch Set: '' Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/chrome/renderer/print_web_view_helper.h ('k') | src/chrome/renderer/render_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #include "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "base/gfx/jpeg_codec.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "chrome/common/render_messages.h" 10 #include "chrome/common/render_messages.h"
10 #include "chrome/renderer/render_view.h" 11 #include "chrome/renderer/render_view.h"
11 #include "grit/generated_resources.h" 12 #include "grit/generated_resources.h"
12 #include "printing/units.h" 13 #include "printing/units.h"
13 #include "webkit/api/public/WebFrame.h" 14 #include "webkit/api/public/WebFrame.h"
14 #include "webkit/api/public/WebRect.h" 15 #include "webkit/api/public/WebRect.h"
15 #include "webkit/api/public/WebScreenInfo.h" 16 #include "webkit/api/public/WebScreenInfo.h"
16 #include "webkit/api/public/WebSize.h" 17 #include "webkit/api/public/WebSize.h"
17 #include "webkit/api/public/WebURLRequest.h" 18 #include "webkit/api/public/WebURLRequest.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 } 133 }
133 } else { 134 } else {
134 for (size_t i = 0; i < params.pages.size(); ++i) { 135 for (size_t i = 0; i < params.pages.size(); ++i) {
135 page_params.page_number = params.pages[i]; 136 page_params.page_number = params.pages[i];
136 PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); 137 PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame);
137 } 138 }
138 } 139 }
139 } 140 }
140 } 141 }
141 142
143 void PrintWebViewHelper::PrintPageAsJPEG(
144 const ViewMsg_PrintPage_Params& params,
145 WebFrame* frame,
146 float zoom_factor,
147 std::vector<unsigned char>* image_data) {
148 PrepareFrameAndViewForPrint prep_frame_view(params.params,
149 frame,
150 frame->view());
151 const gfx::Size& canvas_size(prep_frame_view.GetPrintCanvasSize());
152
153 // Since WebKit extends the page width depending on the magical shrink
154 // factor we make sure the canvas covers the worst case scenario
155 // (x2.0 currently). PrintContext will then set the correct clipping region.
156 int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink);
157 int size_y = static_cast<int>(canvas_size.height() *
158 params.params.max_shrink);
159
160 // Access the bitmap from the canvas device.
161 skia::PlatformCanvas canvas(size_x, size_y, true);
162 frame->printPage(params.page_number, &canvas);
163 const SkBitmap& bitmap = canvas.getDevice()->accessBitmap(false);
164
165 // Encode the SkBitmap to jpeg.
166 SkAutoLockPixels image_lock(bitmap);
167 bool encoded = JPEGCodec::Encode(
168 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
169 JPEGCodec::FORMAT_BGRA,
170 static_cast<int>(bitmap.width() * zoom_factor),
171 static_cast<int>(bitmap.height() * zoom_factor),
172 static_cast<int>(bitmap.rowBytes()),
173 90,
174 image_data);
175 DCHECK(encoded);
176 }
177
142 bool PrintWebViewHelper::Send(IPC::Message* msg) { 178 bool PrintWebViewHelper::Send(IPC::Message* msg) {
143 return render_view_->Send(msg); 179 return render_view_->Send(msg);
144 } 180 }
145 181
146 int32 PrintWebViewHelper::routing_id() { 182 int32 PrintWebViewHelper::routing_id() {
147 return render_view_->routing_id(); 183 return render_view_->routing_id();
148 } 184 }
149 185
150 WebRect PrintWebViewHelper::windowRect() { 186 WebRect PrintWebViewHelper::windowRect() {
151 NOTREACHED(); 187 NOTREACHED();
(...skipping 13 matching lines...) Expand all
165 WebScreenInfo PrintWebViewHelper::screenInfo() { 201 WebScreenInfo PrintWebViewHelper::screenInfo() {
166 NOTREACHED(); 202 NOTREACHED();
167 return WebScreenInfo(); 203 return WebScreenInfo();
168 } 204 }
169 205
170 void PrintWebViewHelper::DidStopLoading(WebView* webview) { 206 void PrintWebViewHelper::DidStopLoading(WebView* webview) {
171 DCHECK(print_pages_params_.get() != NULL); 207 DCHECK(print_pages_params_.get() != NULL);
172 DCHECK_EQ(webview, print_web_view_.get()); 208 DCHECK_EQ(webview, print_web_view_.get());
173 PrintPages(*print_pages_params_.get(), print_web_view_->GetMainFrame()); 209 PrintPages(*print_pages_params_.get(), print_web_view_->GetMainFrame());
174 } 210 }
OLDNEW
« no previous file with comments | « src/chrome/renderer/print_web_view_helper.h ('k') | src/chrome/renderer/render_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698