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

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

Issue 10083060: [Print Preview]: Added code to support pdf fit to page functionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changed UpdateFitToPageInfo function signature and updated comments Created 8 years, 7 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 | « chrome/renderer/print_web_view_helper.h ('k') | chrome/test/data/webui/print_preview.js » ('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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 return frame_has_custom_page_size_style; 334 return frame_has_custom_page_size_style;
335 } 335 }
336 336
337 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { 337 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) {
338 if (frame->isPrintScalingDisabledForPlugin(node)) 338 if (frame->isPrintScalingDisabledForPlugin(node))
339 return printing::NO_MARGINS; 339 return printing::NO_MARGINS;
340 else 340 else
341 return printing::PRINTABLE_AREA_MARGINS; 341 return printing::PRINTABLE_AREA_MARGINS;
342 } 342 }
343 343
344 bool FitToPageEnabled(const DictionaryValue& job_settings) {
345 bool fit_to_paper_size = false;
346 if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled,
347 &fit_to_paper_size)) {
348 NOTREACHED();
349 }
350 return fit_to_paper_size;
351 }
352
344 // Get the (x, y) coordinate from where printing of the current text should 353 // Get the (x, y) coordinate from where printing of the current text should
345 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and 354 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
346 // vertical alignment (TOP, BOTTOM). 355 // vertical alignment (TOP, BOTTOM).
347 SkPoint GetHeaderFooterPosition( 356 SkPoint GetHeaderFooterPosition(
348 float webkit_scale_factor, 357 float webkit_scale_factor,
349 const PageSizeMargins& page_layout, 358 const PageSizeMargins& page_layout,
350 printing::HorizontalHeaderFooterPosition horizontal_position, 359 printing::HorizontalHeaderFooterPosition horizontal_position,
351 printing::VerticalHeaderFooterPosition vertical_position, 360 printing::VerticalHeaderFooterPosition vertical_position,
352 double offset_to_baseline, 361 double offset_to_baseline,
353 double text_width_in_points) { 362 double text_width_in_points) {
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 } 835 }
827 836
828 bool PrintWebViewHelper::IsPrintToPdfRequested( 837 bool PrintWebViewHelper::IsPrintToPdfRequested(
829 const DictionaryValue& job_settings) { 838 const DictionaryValue& job_settings) {
830 bool print_to_pdf = false; 839 bool print_to_pdf = false;
831 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) 840 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf))
832 NOTREACHED(); 841 NOTREACHED();
833 return print_to_pdf; 842 return print_to_pdf;
834 } 843 }
835 844
845 bool PrintWebViewHelper::IsFitToPaperSizeRequested(
846 const DictionaryValue& job_settings, const PrintMsg_Print_Params& params) {
847 if (print_for_preview_ || params.print_to_pdf) {
Lei Zhang 2012/04/27 04:32:47 Do you need to check |print_for_preview_|? Right n
kmadhusu 2012/05/01 16:43:15 Changed the default value of |fit_to_paper_size| t
848 // Do not fit to paper size when the user is printing the preview data or
849 // saving the print contents as pdf.
850 return false;
851 }
852
853 bool source_is_html = IsSourceHTML(print_preview_context_.frame(),
854 print_preview_context_.node(),
855 job_settings);
856
857 if (!source_is_html) {
858 // Get the print scaling option for the initiator renderer pdf.
859 bool print_scaling_disabled_for_plugin =
860 print_preview_context_.frame()->isPrintScalingDisabledForPlugin(
861 print_preview_context_.node());
862
863 // If this is the first preview request, UI doesn't know about the print
864 // scaling option of the plugin. Therefore, check the print scaling option
865 // and update the print params accordingly.
866 //
867 // If this is not the first preview request, update print params based on
868 // preview job settings.
869 if ((params.is_first_request && print_scaling_disabled_for_plugin) ||
870 !FitToPageEnabled(job_settings)) {
Lei Zhang 2012/04/27 04:32:47 You can check the result of FitToPageEnabled() fir
kmadhusu 2012/05/01 16:43:15 Done.
871 return false;
872 }
873 }
874 return true;
875 }
876
877 bool PrintWebViewHelper::IsSourceHTML(const WebFrame* frame,
878 const WebNode& node,
879 const DictionaryValue& job_settings) {
880 bool source_is_html = true;
881 if (print_for_preview_) {
882 if (!job_settings.GetBoolean(printing::kSettingPreviewModifiable,
883 &source_is_html)) {
884 NOTREACHED();
885 }
886 } else {
887 source_is_html = !PrintingNodeOrPdfFrame(frame, node);
888 }
889 return source_is_html;
890 }
891
836 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 892 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
837 DCHECK(is_preview_enabled_); 893 DCHECK(is_preview_enabled_);
838 print_preview_context_.OnPrintPreview(); 894 print_preview_context_.OnPrintPreview();
839 895
840 if (!UpdatePrintSettings(print_preview_context_.frame(), 896 if (!UpdatePrintSettings(print_preview_context_.frame(),
841 print_preview_context_.node(), settings)) { 897 print_preview_context_.node(), settings)) {
842 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 898 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
843 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 899 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
844 routing_id(), print_pages_params_->params.document_cookie)); 900 routing_id(), print_pages_params_->params.document_cookie));
845 notify_browser_of_print_failure_ = false; // Already sent. 901 notify_browser_of_print_failure_ = false; // Already sent.
(...skipping 14 matching lines...) Expand all
860 preview_params.expected_pages_count = 916 preview_params.expected_pages_count =
861 print_preview_context_.total_page_count(); 917 print_preview_context_.total_page_count();
862 preview_params.modifiable = print_preview_context_.IsModifiable(); 918 preview_params.modifiable = print_preview_context_.IsModifiable();
863 preview_params.preview_request_id = 919 preview_params.preview_request_id =
864 print_pages_params_->params.preview_request_id; 920 print_pages_params_->params.preview_request_id;
865 921
866 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), 922 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(),
867 preview_params)); 923 preview_params));
868 return; 924 return;
869 } 925 }
926
927 // If we are previewing a pdf and the print scaling is disabled, send a
928 // message to browser.
929 if (!print_preview_context_.IsModifiable() &&
930 print_pages_params_->params.is_first_request &&
931 print_preview_context_.frame()->isPrintScalingDisabledForPlugin(
932 print_preview_context_.node())) {
933 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id()));
934 }
935
870 // Always clear |old_print_pages_params_| before rendering the pages. 936 // Always clear |old_print_pages_params_| before rendering the pages.
871 old_print_pages_params_.reset(); 937 old_print_pages_params_.reset();
872 is_print_ready_metafile_sent_ = false; 938 is_print_ready_metafile_sent_ = false;
873 939
874 // PDF printer device supports alpha blending. 940 // PDF printer device supports alpha blending.
875 print_pages_params_->params.supports_alpha_blend = true; 941 print_pages_params_->params.supports_alpha_blend = true;
876 942
877 bool generate_draft_pages = false; 943 bool generate_draft_pages = false;
878 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, 944 if (!settings.GetBoolean(printing::kSettingGenerateDraftData,
879 &generate_draft_pages)) { 945 &generate_draft_pages)) {
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
1288 const DictionaryValue& passed_job_settings) { 1354 const DictionaryValue& passed_job_settings) {
1289 DCHECK(is_preview_enabled_); 1355 DCHECK(is_preview_enabled_);
1290 const DictionaryValue* job_settings = &passed_job_settings; 1356 const DictionaryValue* job_settings = &passed_job_settings;
1291 DictionaryValue modified_job_settings; 1357 DictionaryValue modified_job_settings;
1292 if (job_settings->empty()) { 1358 if (job_settings->empty()) {
1293 if (!print_for_preview_) 1359 if (!print_for_preview_)
1294 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1360 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1295 return false; 1361 return false;
1296 } 1362 }
1297 1363
1298 bool source_is_html = true; 1364 bool source_is_html = IsSourceHTML(frame, node, *job_settings);
Lei Zhang 2012/04/27 04:32:47 It occurred to me that you can pass this to IsFitT
kmadhusu 2012/05/01 16:43:15 Done.
1299 if (print_for_preview_) {
1300 if (!job_settings->GetBoolean(printing::kSettingPreviewModifiable,
1301 &source_is_html)) {
1302 NOTREACHED();
1303 }
1304 } else {
1305 source_is_html = !PrintingNodeOrPdfFrame(frame, node);
1306 }
1307
1308 if (print_for_preview_ || !source_is_html) { 1365 if (print_for_preview_ || !source_is_html) {
1309 modified_job_settings.MergeDictionary(job_settings); 1366 modified_job_settings.MergeDictionary(job_settings);
1310 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, 1367 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled,
1311 false); 1368 false);
1312 1369
1313 // - On Windows, we don't add a margin until we turn it into an EMF when
1314 // printing for print preview (We could add it in the plugin).
1315 // - On Mac with Skia, we don't add a margin until we send it to the printer
1316 // using the CG PDF class (We could add it in the plugin).
1317 // - On Mac with CG, we can add a margin when generating the preview.
1318 // - On Linux, we never add a margin (We Could add it in the plugin).
1319 #if defined(OS_MACOSX) && !defined(USE_SKIA)
1320 bool get_margins_from_pdf = !source_is_html && !print_for_preview_;
1321 #elif defined(OS_WIN) || defined(OS_MACOSX)
1322 bool get_margins_from_pdf = !source_is_html && print_for_preview_;
1323 #else
1324 bool get_margins_from_pdf = false;
1325 #endif
1326
1327 printing::MarginType margin_type = printing::NO_MARGINS; 1370 printing::MarginType margin_type = printing::NO_MARGINS;
1328 if (get_margins_from_pdf)
1329 margin_type = GetMarginsForPdf(frame, node);
1330 modified_job_settings.SetInteger(printing::kSettingMarginsType, 1371 modified_job_settings.SetInteger(printing::kSettingMarginsType,
1331 margin_type); 1372 margin_type);
1332 job_settings = &modified_job_settings; 1373 job_settings = &modified_job_settings;
1333 } 1374 }
1334 1375
1335 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when 1376 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
1336 // possible. 1377 // possible.
1337 int cookie = print_pages_params_.get() ? 1378 int cookie = print_pages_params_.get() ?
1338 print_pages_params_->params.document_cookie : 0; 1379 print_pages_params_->params.document_cookie : 0;
1339 PrintMsg_PrintPages_Params settings; 1380 PrintMsg_PrintPages_Params settings;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1373 &(settings.params.preview_request_id)) || 1414 &(settings.params.preview_request_id)) ||
1374 !job_settings->GetBoolean(printing::kIsFirstRequest, 1415 !job_settings->GetBoolean(printing::kIsFirstRequest,
1375 &(settings.params.is_first_request))) { 1416 &(settings.params.is_first_request))) {
1376 NOTREACHED(); 1417 NOTREACHED();
1377 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1418 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1378 return false; 1419 return false;
1379 } 1420 }
1380 1421
1381 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); 1422 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1382 UpdateFrameMarginsCssInfo(*job_settings); 1423 UpdateFrameMarginsCssInfo(*job_settings);
1383 1424 settings.params.fit_to_paper_size =
1384 // Fit to paper size. 1425 IsFitToPaperSizeRequested(*job_settings, settings.params);
1385 settings.params.fit_to_paper_size = source_is_html &&
1386 !IsPrintToPdfRequested(*job_settings);
1387 1426
1388 // Header/Footer: Set |header_footer_info_|. 1427 // Header/Footer: Set |header_footer_info_|.
1389 if (settings.params.display_header_footer) { 1428 if (settings.params.display_header_footer) {
1390 header_footer_info_.reset(new DictionaryValue()); 1429 header_footer_info_.reset(new DictionaryValue());
1391 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1430 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1392 settings.params.date); 1431 settings.params.date);
1393 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1432 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1394 settings.params.url); 1433 settings.params.url);
1395 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1434 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1396 settings.params.title); 1435 settings.params.title);
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
1820 DCHECK(IsRendering()); 1859 DCHECK(IsRendering());
1821 return prep_frame_view_->GetPrintCanvasSize(); 1860 return prep_frame_view_->GetPrintCanvasSize();
1822 } 1861 }
1823 1862
1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1863 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1825 prep_frame_view_.reset(); 1864 prep_frame_view_.reset();
1826 metafile_.reset(); 1865 metafile_.reset();
1827 pages_to_render_.clear(); 1866 pages_to_render_.clear();
1828 error_ = PREVIEW_ERROR_NONE; 1867 error_ = PREVIEW_ERROR_NONE;
1829 } 1868 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | chrome/test/data/webui/print_preview.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698