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

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: '' 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 bool source_is_html, const DictionaryValue& job_settings,
847 const PrintMsg_Print_Params& params) {
848 DCHECK(!print_for_preview_);
849
850 // Do not fit to paper size when the user is saving the print contents as pdf.
851 if (params.print_to_pdf)
852 return false;
853
854 if (!source_is_html) {
855 if (!FitToPageEnabled(job_settings))
856 return false;
857
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 return false;
871 }
872 return true;
873 }
874
836 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 875 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
837 DCHECK(is_preview_enabled_); 876 DCHECK(is_preview_enabled_);
838 print_preview_context_.OnPrintPreview(); 877 print_preview_context_.OnPrintPreview();
839 878
840 if (!UpdatePrintSettings(print_preview_context_.frame(), 879 if (!UpdatePrintSettings(print_preview_context_.frame(),
841 print_preview_context_.node(), settings)) { 880 print_preview_context_.node(), settings)) {
842 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 881 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
843 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 882 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
844 routing_id(), print_pages_params_->params.document_cookie)); 883 routing_id(), print_pages_params_->params.document_cookie));
845 notify_browser_of_print_failure_ = false; // Already sent. 884 notify_browser_of_print_failure_ = false; // Already sent.
(...skipping 14 matching lines...) Expand all
860 preview_params.expected_pages_count = 899 preview_params.expected_pages_count =
861 print_preview_context_.total_page_count(); 900 print_preview_context_.total_page_count();
862 preview_params.modifiable = print_preview_context_.IsModifiable(); 901 preview_params.modifiable = print_preview_context_.IsModifiable();
863 preview_params.preview_request_id = 902 preview_params.preview_request_id =
864 print_pages_params_->params.preview_request_id; 903 print_pages_params_->params.preview_request_id;
865 904
866 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), 905 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(),
867 preview_params)); 906 preview_params));
868 return; 907 return;
869 } 908 }
909
910 // If we are previewing a pdf and the print scaling is disabled, send a
911 // message to browser.
912 if (print_pages_params_->params.is_first_request &&
913 !print_preview_context_.IsModifiable() &&
914 print_preview_context_.frame()->isPrintScalingDisabledForPlugin(
915 print_preview_context_.node())) {
916 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id()));
917 }
918
870 // Always clear |old_print_pages_params_| before rendering the pages. 919 // Always clear |old_print_pages_params_| before rendering the pages.
871 old_print_pages_params_.reset(); 920 old_print_pages_params_.reset();
872 is_print_ready_metafile_sent_ = false; 921 is_print_ready_metafile_sent_ = false;
873 922
874 // PDF printer device supports alpha blending. 923 // PDF printer device supports alpha blending.
875 print_pages_params_->params.supports_alpha_blend = true; 924 print_pages_params_->params.supports_alpha_blend = true;
876 925
877 bool generate_draft_pages = false; 926 bool generate_draft_pages = false;
878 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, 927 if (!settings.GetBoolean(printing::kSettingGenerateDraftData,
879 &generate_draft_pages)) { 928 &generate_draft_pages)) {
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 NOTREACHED(); 1351 NOTREACHED();
1303 } 1352 }
1304 } else { 1353 } else {
1305 source_is_html = !PrintingNodeOrPdfFrame(frame, node); 1354 source_is_html = !PrintingNodeOrPdfFrame(frame, node);
1306 } 1355 }
1307 1356
1308 if (print_for_preview_ || !source_is_html) { 1357 if (print_for_preview_ || !source_is_html) {
1309 modified_job_settings.MergeDictionary(job_settings); 1358 modified_job_settings.MergeDictionary(job_settings);
1310 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, 1359 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled,
1311 false); 1360 false);
1312
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;
1328 if (get_margins_from_pdf)
1329 margin_type = GetMarginsForPdf(frame, node);
1330 modified_job_settings.SetInteger(printing::kSettingMarginsType, 1361 modified_job_settings.SetInteger(printing::kSettingMarginsType,
1331 margin_type); 1362 printing::NO_MARGINS);
1332 job_settings = &modified_job_settings; 1363 job_settings = &modified_job_settings;
1333 } 1364 }
1334 1365
1335 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when 1366 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
1336 // possible. 1367 // possible.
1337 int cookie = print_pages_params_.get() ? 1368 int cookie = print_pages_params_.get() ?
1338 print_pages_params_->params.document_cookie : 0; 1369 print_pages_params_->params.document_cookie : 0;
1339 PrintMsg_PrintPages_Params settings; 1370 PrintMsg_PrintPages_Params settings;
1340 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 1371 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
1341 cookie, *job_settings, &settings)); 1372 cookie, *job_settings, &settings));
(...skipping 30 matching lines...) Expand all
1372 !job_settings->GetInteger(printing::kPreviewRequestID, 1403 !job_settings->GetInteger(printing::kPreviewRequestID,
1373 &(settings.params.preview_request_id)) || 1404 &(settings.params.preview_request_id)) ||
1374 !job_settings->GetBoolean(printing::kIsFirstRequest, 1405 !job_settings->GetBoolean(printing::kIsFirstRequest,
1375 &(settings.params.is_first_request))) { 1406 &(settings.params.is_first_request))) {
1376 NOTREACHED(); 1407 NOTREACHED();
1377 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1408 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1378 return false; 1409 return false;
1379 } 1410 }
1380 1411
1381 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); 1412 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1413 settings.params.fit_to_paper_size = IsFitToPaperSizeRequested(
1414 source_is_html, *job_settings, settings.params);
1382 UpdateFrameMarginsCssInfo(*job_settings); 1415 UpdateFrameMarginsCssInfo(*job_settings);
1383 1416
1384 // Fit to paper size.
1385 settings.params.fit_to_paper_size = source_is_html &&
1386 !IsPrintToPdfRequested(*job_settings);
1387
1388 // Header/Footer: Set |header_footer_info_|. 1417 // Header/Footer: Set |header_footer_info_|.
1389 if (settings.params.display_header_footer) { 1418 if (settings.params.display_header_footer) {
1390 header_footer_info_.reset(new DictionaryValue()); 1419 header_footer_info_.reset(new DictionaryValue());
1391 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1420 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1392 settings.params.date); 1421 settings.params.date);
1393 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1422 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1394 settings.params.url); 1423 settings.params.url);
1395 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1424 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1396 settings.params.title); 1425 settings.params.title);
1397 } 1426 }
(...skipping 19 matching lines...) Expand all
1417 params.cookie = print_pages_params_->params.document_cookie; 1446 params.cookie = print_pages_params_->params.document_cookie;
1418 params.has_selection = frame->hasSelection(); 1447 params.has_selection = frame->hasSelection();
1419 params.expected_pages_count = expected_pages_count; 1448 params.expected_pages_count = expected_pages_count;
1420 printing::MarginType margin_type = printing::DEFAULT_MARGINS; 1449 printing::MarginType margin_type = printing::DEFAULT_MARGINS;
1421 if (PrintingNodeOrPdfFrame(frame, node)) 1450 if (PrintingNodeOrPdfFrame(frame, node))
1422 margin_type = GetMarginsForPdf(frame, node); 1451 margin_type = GetMarginsForPdf(frame, node);
1423 params.margin_type = margin_type; 1452 params.margin_type = margin_type;
1424 1453
1425 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); 1454 Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
1426 1455
1456 // Store the calculated fit_to_paper_size value.
1457 bool fit_to_paper_size = print_pages_params_->params.fit_to_paper_size;
1458
1427 print_pages_params_.reset(); 1459 print_pages_params_.reset();
1428 IPC::SyncMessage* msg = 1460 IPC::SyncMessage* msg =
1429 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); 1461 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
1430 msg->EnableMessagePumping(); 1462 msg->EnableMessagePumping();
1431 Send(msg); 1463 Send(msg);
1432 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); 1464 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings));
1465 print_pages_params_->params.fit_to_paper_size = fit_to_paper_size;
kmadhusu 2012/05/06 20:10:15 While testing the native printing workflow, I foun
Lei Zhang 2012/05/07 18:54:05 You may want to add a comment here to explain that
kmadhusu 2012/05/16 18:45:11 Done.
1433 return (print_settings.params.dpi && print_settings.params.document_cookie); 1466 return (print_settings.params.dpi && print_settings.params.document_cookie);
1434 } 1467 }
1435 1468
1436 bool PrintWebViewHelper::RenderPagesForPrint( 1469 bool PrintWebViewHelper::RenderPagesForPrint(
1437 WebKit::WebFrame* frame, 1470 WebKit::WebFrame* frame,
1438 const WebKit::WebNode& node) { 1471 const WebKit::WebNode& node) {
1439 if (print_pages_params_->params.selection_only) 1472 if (print_pages_params_->params.selection_only)
1440 return CopyAndPrint(frame); 1473 return CopyAndPrint(frame);
1441 return PrintPages(frame, node); 1474 return PrintPages(frame, node);
1442 } 1475 }
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
1820 DCHECK(IsRendering()); 1853 DCHECK(IsRendering());
1821 return prep_frame_view_->GetPrintCanvasSize(); 1854 return prep_frame_view_->GetPrintCanvasSize();
1822 } 1855 }
1823 1856
1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1857 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1825 prep_frame_view_.reset(); 1858 prep_frame_view_.reset();
1826 metafile_.reset(); 1859 metafile_.reset();
1827 pages_to_render_.clear(); 1860 pages_to_render_.clear();
1828 error_ = PREVIEW_ERROR_NONE; 1861 error_ = PREVIEW_ERROR_NONE;
1829 } 1862 }
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