From eb5cc495b332be51dcbbf3a95566c2353be11b87 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 22 Sep 2020 17:17:04 -0700 Subject: [PATCH] Provide defaulting for textScaleFactor when passing to dart:ui (#66375) --- packages/flutter/lib/src/painting/text_painter.dart | 9 +++++++++ packages/flutter/lib/src/painting/text_style.dart | 10 ++++++---- .../flutter/test/painting/text_painter_test.dart | 12 ++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 1276d19d8d..51000136b5 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -16,6 +16,11 @@ import 'text_span.dart'; export 'package:flutter/services.dart' show TextRange, TextSelection; +// The default font size if none is specified. This should be kept in +// sync with the default values in text_style.dart, as well as the +// defaults set in the engine (eg, LibTxt's text_style.h, paragraph_style.h). +const double _kDefaultFontSize = 14.0; + /// Holds the [Size] and baseline required to represent the dimensions of /// a placeholder in text. /// @@ -420,6 +425,10 @@ class TextPainter { ) ?? ui.ParagraphStyle( textAlign: textAlign, textDirection: textDirection ?? defaultTextDirection, + // Use the default font size to multiply by as RichText does not + // perform inheriting [TextStyle]s and would otherwise + // fail to apply textScaleFactor. + fontSize: _kDefaultFontSize * textScaleFactor, maxLines: maxLines, textHeightBehavior: _textHeightBehavior, ellipsis: ellipsis, diff --git a/packages/flutter/lib/src/painting/text_style.dart b/packages/flutter/lib/src/painting/text_style.dart index 19f1d5be38..aa213775db 100644 --- a/packages/flutter/lib/src/painting/text_style.dart +++ b/packages/flutter/lib/src/painting/text_style.dart @@ -19,6 +19,11 @@ const String _kColorForegroundWarning = 'Cannot provide both a color and a foreg const String _kColorBackgroundWarning = 'Cannot provide both a backgroundColor and a background\n' 'The backgroundColor argument is just a shorthand for "background: new Paint()..color = color".'; +// The default font size if none is specified. This should be kept in +// sync with the default values in text_painter.dart, as well as the +// defaults set in the engine (eg, LibTxt's text_style.h, paragraph_style.h). +const double _kDefaultFontSize = 14.0; + // Examples can assume: // BuildContext context; @@ -512,9 +517,6 @@ class TextStyle with Diagnosticable { /// isn't specified here. final double? fontSize; - // The default font size if none is specified. - static const double _defaultFontSize = 14.0; - /// The typeface thickness to use when painting the text (e.g., bold). final FontWeight? fontWeight; @@ -1092,7 +1094,7 @@ class TextStyle with Diagnosticable { fontWeight: fontWeight ?? this.fontWeight, fontStyle: fontStyle ?? this.fontStyle, fontFamily: fontFamily ?? this.fontFamily, - fontSize: (fontSize ?? this.fontSize ?? _defaultFontSize) * textScaleFactor, + fontSize: (fontSize ?? this.fontSize ?? _kDefaultFontSize) * textScaleFactor, height: height ?? this.height, textHeightBehavior: textHeightBehavior, strutStyle: strutStyle == null ? null : ui.StrutStyle( diff --git a/packages/flutter/test/painting/text_painter_test.dart b/packages/flutter/test/painting/text_painter_test.dart index 2c1f4076e9..9245a36fb1 100644 --- a/packages/flutter/test/painting/text_painter_test.dart +++ b/packages/flutter/test/painting/text_painter_test.dart @@ -192,6 +192,18 @@ void main() { expect(painter.size, const Size(20.0, 20.0)); }); + test('TextPainter textScaleFactor null style test', () { + final TextPainter painter = TextPainter( + text: const TextSpan( + text: 'X', + ), + textDirection: TextDirection.ltr, + textScaleFactor: 2.0, + ); + painter.layout(); + expect(painter.size, const Size(28.0, 28.0)); + }); + test('TextPainter default text height is 14 pixels', () { final TextPainter painter = TextPainter( text: const TextSpan(text: 'x'),