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

Side by Side Diff: components/printing/renderer/print_web_view_helper.cc

Issue 2454293004: Printing: Fix undefined behavior for near 0 scaling (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « no previous file | components/printing/renderer/print_web_view_helper_mac.mm » ('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 "components/printing/renderer/print_web_view_helper.h" 5 #include "components/printing/renderer/print_web_view_helper.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 void CalculatePageLayoutFromPrintParams( 227 void CalculatePageLayoutFromPrintParams(
228 const PrintMsg_Print_Params& params, 228 const PrintMsg_Print_Params& params,
229 double scale_factor, 229 double scale_factor,
230 PageSizeMargins* page_layout_in_points) { 230 PageSizeMargins* page_layout_in_points) {
231 bool fit_to_page = IsWebPrintScalingOptionFitToPage(params); 231 bool fit_to_page = IsWebPrintScalingOptionFitToPage(params);
232 int dpi = GetDPI(&params); 232 int dpi = GetDPI(&params);
233 int content_width = params.content_size.width(); 233 int content_width = params.content_size.width();
234 int content_height = params.content_size.height(); 234 int content_height = params.content_size.height();
235 // Scale the content to its normal size for purpose of computing page layout. 235 // Scale the content to its normal size for purpose of computing page layout.
236 // Otherwise we will get negative margins. 236 // Otherwise we will get negative margins.
237 if (scale_factor > 0 && (fit_to_page || params.print_to_pdf)) { 237 if (scale_factor >= 0.01f && (fit_to_page || params.print_to_pdf)) {
Lei Zhang 2016/10/29 00:26:53 Add a constexpr double kEpsilon constant and use t
rbpotter 2016/10/29 00:41:53 Done.
238 content_width = 238 content_width =
239 static_cast<int>(static_cast<double>(content_width) * scale_factor); 239 static_cast<int>(static_cast<double>(content_width) * scale_factor);
240 content_height = 240 content_height =
241 static_cast<int>(static_cast<double>(content_height) * scale_factor); 241 static_cast<int>(static_cast<double>(content_height) * scale_factor);
242 } 242 }
243 243
244 int margin_bottom = 244 int margin_bottom =
245 params.page_size.height() - content_height - params.margin_top; 245 params.page_size.height() - content_height - params.margin_top;
246 int margin_right = 246 int margin_right =
247 params.page_size.width() - content_width - params.margin_left; 247 params.page_size.width() - content_width - params.margin_left;
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 weak_ptr_factory_(this) { 714 weak_ptr_factory_(this) {
715 PrintMsg_Print_Params print_params = params; 715 PrintMsg_Print_Params print_params = params;
716 if (!should_print_selection_only_ || 716 if (!should_print_selection_only_ ||
717 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { 717 !PrintingNodeOrPdfFrame(frame, node_to_print_)) {
718 bool fit_to_page = 718 bool fit_to_page =
719 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params); 719 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params);
720 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_); 720 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
721 frame->printBegin(web_print_params_, node_to_print_); 721 frame->printBegin(web_print_params_, node_to_print_);
722 double scale_factor = 1.0f; 722 double scale_factor = 1.0f;
723 #if defined(ENABLE_PRINT_PREVIEW) 723 #if defined(ENABLE_PRINT_PREVIEW)
724 if (print_params.scale_factor > 0) 724 if (print_params.scale_factor >= 0.01f)
725 scale_factor = print_params.scale_factor; 725 scale_factor = print_params.scale_factor;
726 #endif 726 #endif
727 print_params = CalculatePrintParamsForCss( 727 print_params = CalculatePrintParamsForCss(
728 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor); 728 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor);
729 frame->printEnd(); 729 frame->printEnd();
730 } 730 }
731 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); 731 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
732 } 732 }
733 733
734 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 734 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 const PrintMsg_Print_Params& print_params = print_pages_params_->params; 1225 const PrintMsg_Print_Params& print_params = print_pages_params_->params;
1226 const std::vector<int>& pages = print_pages_params_->pages; 1226 const std::vector<int>& pages = print_pages_params_->pages;
1227 1227
1228 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(), 1228 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(),
1229 pages)) { 1229 pages)) {
1230 return false; 1230 return false;
1231 } 1231 }
1232 1232
1233 PageSizeMargins default_page_layout; 1233 PageSizeMargins default_page_layout;
1234 double scale_factor = 1234 double scale_factor =
1235 print_params.scale_factor > 0 ? print_params.scale_factor : 1.0f; 1235 print_params.scale_factor >= 0.01f ? print_params.scale_factor : 1.0f;
1236 1236
1237 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0, 1237 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0,
1238 print_params, ignore_css_margins_, 1238 print_params, ignore_css_margins_,
1239 &scale_factor, &default_page_layout); 1239 &scale_factor, &default_page_layout);
1240 bool has_page_size_style = 1240 bool has_page_size_style =
1241 PrintingFrameHasPageSizeStyle(print_preview_context_.prepared_frame(), 1241 PrintingFrameHasPageSizeStyle(print_preview_context_.prepared_frame(),
1242 print_preview_context_.total_page_count()); 1242 print_preview_context_.total_page_count());
1243 int dpi = GetDPI(&print_params); 1243 int dpi = GetDPI(&print_params);
1244 1244
1245 gfx::Rect printable_area_in_points( 1245 gfx::Rect printable_area_in_points(
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
1821 void PrintWebViewHelper::PrintPageInternal( 1821 void PrintWebViewHelper::PrintPageInternal(
1822 const PrintMsg_PrintPage_Params& params, 1822 const PrintMsg_PrintPage_Params& params,
1823 blink::WebLocalFrame* frame, 1823 blink::WebLocalFrame* frame,
1824 PdfMetafileSkia* metafile, 1824 PdfMetafileSkia* metafile,
1825 gfx::Size* page_size_in_dpi, 1825 gfx::Size* page_size_in_dpi,
1826 gfx::Rect* content_area_in_dpi) { 1826 gfx::Rect* content_area_in_dpi) {
1827 PageSizeMargins page_layout_in_points; 1827 PageSizeMargins page_layout_in_points;
1828 1828
1829 double css_scale_factor = 1.0f; 1829 double css_scale_factor = 1.0f;
1830 #if defined(ENABLE_PRINT_PREVIEW) 1830 #if defined(ENABLE_PRINT_PREVIEW)
1831 if (params.params.scale_factor > 0) 1831 if (params.params.scale_factor >= 0.01f)
1832 css_scale_factor = params.params.scale_factor; 1832 css_scale_factor = params.params.scale_factor;
1833 #endif 1833 #endif
1834 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, 1834 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
1835 ignore_css_margins_, &css_scale_factor, 1835 ignore_css_margins_, &css_scale_factor,
1836 &page_layout_in_points); 1836 &page_layout_in_points);
1837 gfx::Size page_size; 1837 gfx::Size page_size;
1838 gfx::Rect content_area; 1838 gfx::Rect content_area;
1839 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, 1839 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
1840 &content_area); 1840 &content_area);
1841 int dpi = static_cast<int>(params.params.dpi); 1841 int dpi = static_cast<int>(params.params.dpi);
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
2333 blink::WebConsoleMessage::LevelWarning, message)); 2333 blink::WebConsoleMessage::LevelWarning, message));
2334 return false; 2334 return false;
2335 } 2335 }
2336 2336
2337 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2337 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2338 // Reset counter on successful print. 2338 // Reset counter on successful print.
2339 count_ = 0; 2339 count_ = 0;
2340 } 2340 }
2341 2341
2342 } // namespace printing 2342 } // namespace printing
OLDNEW
« no previous file with comments | « no previous file | components/printing/renderer/print_web_view_helper_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698