Unverified Commit 9c1783e9 authored by Gary Qian's avatar Gary Qian Committed by GitHub

Update TextStyle and StrutStyle height docs (#33281)

parent aae50f54
...@@ -8,19 +8,21 @@ import 'basic_types.dart'; ...@@ -8,19 +8,21 @@ import 'basic_types.dart';
import 'text_style.dart'; import 'text_style.dart';
/// Defines the strut, which sets the minimum height a line can be /// Defines the strut, which sets the minimum height a line can be
/// relative to the baseline. Strut applies to all lines in the paragraph. /// relative to the baseline.
/// ///
/// Strut is a feature that allows minimum line heights to be set. The effect is as /// Strut applies to all lines in the paragraph. Strut is a feature that
/// if a zero width space was included at the beginning of each line in the /// allows minimum line heights to be set. The effect is as if a zero
/// paragraph. This imaginary space is 'shaped' according the properties defined /// width space was included at the beginning of each line in the
/// in this class. Flutter's strut is based on [typesetting strut](https://en.wikipedia.org/wiki/Strut_(typesetting)) /// paragraph. This imaginary space is 'shaped' according the properties
/// defined in this class. Flutter's strut is based on
/// [typesetting strut](https://en.wikipedia.org/wiki/Strut_(typesetting))
/// and CSS's [line-height](https://www.w3.org/TR/CSS2/visudet.html#line-height). /// and CSS's [line-height](https://www.w3.org/TR/CSS2/visudet.html#line-height).
/// ///
/// No lines may be shorter than the strut. The ascent and descent of the strut /// No lines may be shorter than the strut. The ascent and descent of the
/// are calculated, and any laid out text that has a shorter ascent or descent than /// strut are calculated, and any laid out text that has a shorter ascent or
/// the strut's ascent or descent will take the ascent and descent of the strut. /// descent than the strut's ascent or descent will take the ascent and
/// Text with ascents or descents larger than the strut's ascent or descent will lay /// descent of the strut. Text with ascents or descents larger than the
/// out as normal and extend past the strut. /// strut's ascent or descent will layout as normal and extend past the strut.
/// ///
/// Strut is defined independently from any text content or [TextStyle]s. /// Strut is defined independently from any text content or [TextStyle]s.
/// ///
...@@ -33,16 +35,26 @@ import 'text_style.dart'; ...@@ -33,16 +35,26 @@ import 'text_style.dart';
/// ///
/// The sum of these four values is the total height of the line. /// The sum of these four values is the total height of the line.
/// ///
/// The `ascent + descent` is equivalent to the [fontSize]. Ascent is the font's /// Ascent is the font's spacing above the baseline without leading and
/// spacing above the baseline without leading and descent is the spacing below the /// descent is the spacing below the baseline without leading. Leading is
/// baseline without leading. Leading is split evenly between the top and bottom. /// split evenly betweenthe top and bottom. The values for `ascent` and
/// The values for `ascent` and `descent` are provided by the font named by /// `descent` are provided by the font named by [fontFamily]. If no
/// [fontFamily]. If no [fontFamily] or [fontFamilyFallback] is provided, then the /// [fontFamily] or [fontFamilyFallback] is provided, then the platform's
/// platform's default family will be used. /// default family will be used.
///
/// When [height] is omitted or null, then the font defined ascent and descent
/// will be used. The font's combined ascent and descent may be taller or
/// shorter than the [fontSize]. When [height] is provided, the line's EM-square
/// ascent and descent (which sums to [fontSize]) will be scaled by [height] to
/// achieve a final line height of `fontSize * height + fontSize * leading`
/// logical pixels. The proportion of ascent:descent with [height] specified
/// is the same as the font metrics defined ascent:descent ratio.
///
/// ![Text height diagram](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_diagram.png)
/// ///
/// Each line's spacing above the baseline will be at least as tall as the half /// Each line's spacing above the baseline will be at least as tall as the
/// leading plus ascent. Each line's spacing below the baseline will be at least as /// half leading plus ascent. Each line's spacing below the baseline will
/// tall as the half leading plus descent. /// be at least as tall as the half leading plus descent.
/// ///
/// See also: /// See also:
/// ///
...@@ -65,47 +77,55 @@ import 'text_style.dart'; ...@@ -65,47 +77,55 @@ import 'text_style.dart';
/// (e.g., Roboto). No glyphs from the font will be drawn and the font will /// (e.g., Roboto). No glyphs from the font will be drawn and the font will
/// be used purely for metrics. /// be used purely for metrics.
/// ///
/// * [fontFamilyFallback]: an ordered list of font family names that will be searched for when /// * [fontFamilyFallback]: an ordered list of font family names that will
/// the font in [fontFamily] cannot be found. When all specified font families have been /// be searched for when the font in [fontFamily] cannot be found. When
/// exhausted an no match was found, the default platform font will be used. /// all specified font families have been exhausted an no match was found,
/// the default platform font will be used.
/// ///
/// * [fontSize]: the size of the ascent plus descent in logical pixels. This /// * [fontSize]: the size of the ascent plus descent in logical pixels. This
/// is also used as the basis of the custom leading calculation. This value /// is also used as the basis of the custom leading calculation. This value
/// cannot be negative. /// cannot be negative.
/// Default is 14 logical pixels. /// Default is 14 logical pixels.
/// ///
/// * [height]: the multiple of [fontSize] to multiply the ascent and descent by. /// * [height]: the multiple of [fontSize] the line's height should be.
/// The [height] will impact the spacing above and below the baseline differently /// The line's height will take the font's ascent and descent values if
/// depending on the ratios between the font's ascent and descent. This property is /// [height] is omitted or null. If provided, the EM-square ascent and
/// separate from the leading multiplier, which is controlled through [leading]. /// descent (which sum to [fontSize]) is scaled by [height].
/// Default is 1.0. /// The [height] will impact the spacing above and below the baseline
/// /// differently depending on the ratios between the font's ascent and
/// * [leading]: the custom leading to apply to the strut as a multiple of [fontSize]. /// descent. This property is separate from the leading multiplier, which
/// Leading is additional spacing between lines. Half of the leading is added /// is controlled through [leading].
/// to the top and the other half to the bottom of the line height. This differs /// Default is null.
/// from [height] since the spacing is equally distributed above and below the ///
/// baseline. /// * [leading]: the custom leading to apply to the strut as a multiple of
/// Default is `null`, which will use the font-specified leading. /// [fontSize]. Leading is additional spacing between lines. Half of the
/// /// leading is added to the top and the other half to the bottom of the
/// * [fontWeight]: the typeface thickness to use when calculating the strut (e.g., bold). /// line height. This differs from [height] since the spacing is equally
/// distributed above and below the baseline.
/// Default is null, which will use the font-specified leading.
///
/// * [fontWeight]: the typeface thickness to use when calculating the strut
/// (e.g., bold).
/// Default is [FontWeight.w400]. /// Default is [FontWeight.w400].
/// ///
/// * [fontStyle]: the typeface variant to use when calculating the strut (e.g., italic). /// * [fontStyle]: the typeface variant to use when calculating the strut
/// (e.g., italic).
/// Default is [FontStyle.normal]. /// Default is [FontStyle.normal].
/// ///
/// * [forceStrutHeight]: when true, all lines will be laid out with the height of the /// * [forceStrutHeight]: when true, all lines will be laid out with the
/// strut. All line and run-specific metrics will be ignored/overridden and only strut /// height of the strut. All line and run-specific metrics will be
/// metrics will be used instead. This property guarantees uniform line spacing, however /// ignored/overridden and only strut metrics will be used instead.
/// text in adjacent lines may overlap. This property should be enabled with caution as /// This property guarantees uniform line spacing, however text in
/// it bypasses a large portion of the vertical layout system. /// adjacent lines may overlap. This property should be enabled with
/// caution as it bypasses a large portion of the vertical layout system.
/// The default value is false. /// The default value is false.
/// ///
/// ### Examples /// ### Examples
/// ///
/// {@tool sample} /// {@tool sample}
/// In this simple case, the text will be rendered at font size 10, however, the vertical /// In this simple case, the text will be rendered at font size 10, however,
/// height of each line will be the strut height (Roboto in font size 30 * 1.5) as the text /// the vertical height of each line will be the strut height (Roboto in
/// itself is shorter than the strut. /// font size 30 * 1.5) as the text itself is shorter than the strut.
/// ///
/// ```dart /// ```dart
/// const Text( /// const Text(
...@@ -271,7 +291,8 @@ class StrutStyle extends Diagnosticable { ...@@ -271,7 +291,8 @@ class StrutStyle extends Diagnosticable {
/// package. It is combined with the `fontFamily` argument to set the /// package. It is combined with the `fontFamily` argument to set the
/// [fontFamily] property. /// [fontFamily] property.
/// ///
/// If provided, fontSize must be positive and non-zero, leading must be zero or positive. /// If provided, fontSize must be positive and non-zero, leading must be
/// zero or positive.
const StrutStyle({ const StrutStyle({
String fontFamily, String fontFamily,
List<String> fontFamilyFallback, List<String> fontFamilyFallback,
...@@ -296,16 +317,19 @@ class StrutStyle extends Diagnosticable { ...@@ -296,16 +317,19 @@ class StrutStyle extends Diagnosticable {
/// The [textStyle] parameter must not be null. /// The [textStyle] parameter must not be null.
/// ///
/// The named parameters override the [textStyle]'s argument's properties. /// The named parameters override the [textStyle]'s argument's properties.
/// Since TextStyle does not contain [leading] or [forceStrutHeight], these values /// Since TextStyle does not contain [leading] or [forceStrutHeight], these
/// will take on default values (null and false) unless otherwise specified. /// values will take on default values (null and false) unless otherwise
/// specified.
/// ///
/// If provided, fontSize must be positive and non-zero, leading must be zero or positive. /// If provided, fontSize must be positive and non-zero, leading must be
/// zero or positive.
/// ///
/// When [textStyle] has a package and a new [package] is also specified, the entire /// When [textStyle] has a package and a new [package] is also specified,
/// font family fallback list should be redefined since the [textStyle]'s package data /// the entire font family fallback list should be redefined since the
/// is inherited by being prepended onto the font family names. If /// [textStyle]'s package data is inherited by being prepended onto the
/// [fontFamilyFallback] is meant to be empty, pass an empty list instead of null. /// font family names. If [fontFamilyFallback] is meant to be empty, pass
/// This prevents the previous package name from being prepended twice. /// an empty list instead of null. This prevents the previous package name
/// from being prepended twice.
StrutStyle.fromTextStyle( StrutStyle.fromTextStyle(
TextStyle textStyle, { TextStyle textStyle, {
String fontFamily, String fontFamily,
...@@ -329,8 +353,8 @@ class StrutStyle extends Diagnosticable { ...@@ -329,8 +353,8 @@ class StrutStyle extends Diagnosticable {
fontWeight = fontWeight ?? textStyle.fontWeight, fontWeight = fontWeight ?? textStyle.fontWeight,
fontStyle = fontStyle ?? textStyle.fontStyle, fontStyle = fontStyle ?? textStyle.fontStyle,
debugLabel = debugLabel ?? textStyle.debugLabel, debugLabel = debugLabel ?? textStyle.debugLabel,
_package = package; // the textStyle._package data is embedded in the fontFamily names, _package = package; // the textStyle._package data is embedded in the
// so we no longer need it. // fontFamily names, so we no longer need it.
/// A [StrutStyle] that will have no impact on the text layout. /// A [StrutStyle] that will have no impact on the text layout.
/// ///
...@@ -394,15 +418,32 @@ class StrutStyle extends Diagnosticable { ...@@ -394,15 +418,32 @@ class StrutStyle extends Diagnosticable {
/// The default fontSize is 14 logical pixels. /// The default fontSize is 14 logical pixels.
final double fontSize; final double fontSize;
/// The multiple of [fontSize] to multiply the ascent and descent by where `ascent + descent = fontSize`. /// The multiple of [fontSize] to multiply the ascent and descent by where
/// `ascent + descent = fontSize`.
/// ///
/// Ascent is the spacing above the baseline and descent is the spacing below the baseline. /// Ascent is the spacing above the baseline and descent is the spacing below
/// the baseline.
///
/// When [height] is omitted or null, then the font defined ascent and descent
/// will be used. The font's combined ascent and descent may be taller or
/// shorter than the [fontSize]. When [height] is provided, the line's EM-square
/// ascent and descent (which sums to [fontSize]) will be scaled by [height] to
/// achieve a final line height of `fontSize * height + fontSize * leading`
/// logical pixels. The following diagram illustrates the differences between
/// the font metrics defined height and the EM-square height:
///
/// ![Text height diagram](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_diagram.png)
/// ///
/// The [height] will impact the spacing above and below the baseline differently /// The [height] will impact the spacing above and below the baseline differently
/// depending on the ratios between the font's ascent and descent. This property is /// depending on the ratios between the font's ascent and descent. This property is
/// separate from the leading multiplier, which is controlled through [leading]. /// separate from the leading multiplier, which is controlled through [leading].
/// ///
/// The default height is 1.0. /// The ratio of ascent:descent with [height] specified is the same as the
/// font metrics defined ascent:descent ratio when [height] is null or omitted.
///
/// See [TextStyle.height], which works in a similar manner.
///
/// The default height is null.
final double height; final double height;
/// The typeface thickness to use when calculating the strut (e.g., bold). /// The typeface thickness to use when calculating the strut (e.g., bold).
...@@ -428,9 +469,9 @@ class StrutStyle extends Diagnosticable { ...@@ -428,9 +469,9 @@ class StrutStyle extends Diagnosticable {
/// Whether the strut height should be forced. /// Whether the strut height should be forced.
/// ///
/// When true, all lines will be laid out with the height of the /// When true, all lines will be laid out with the height of the
/// strut. All line and run-specific metrics will be ignored/overridden and only strut /// strut. All line and run-specific metrics will be ignored/overridden
/// metrics will be used instead. This property guarantees uniform line spacing, however /// and only strut metrics will be used instead. This property guarantees
/// text in adjacent lines may overlap. /// uniform line spacing, however text in adjacent lines may overlap.
/// ///
/// This property should be enabled with caution as /// This property should be enabled with caution as
/// it bypasses a large portion of the vertical layout system. /// it bypasses a large portion of the vertical layout system.
...@@ -473,12 +514,12 @@ class StrutStyle extends Diagnosticable { ...@@ -473,12 +514,12 @@ class StrutStyle extends Diagnosticable {
return RenderComparison.identical; return RenderComparison.identical;
} }
/// Returns a new strut style that inherits its null values from corresponding /// Returns a new strut style that inherits its null values from
/// properties in the [other] [TextStyle]. /// corresponding properties in the [other] [TextStyle].
/// ///
/// The "missing" properties of the this strut style are _filled_ by the properties /// The "missing" properties of the this strut style are _filled_ by
/// of the provided [TextStyle]. This is possible because [StrutStyle] shares many of /// the properties of the provided [TextStyle]. This is possible because
/// the same basic properties as [TextStyle]. /// [StrutStyle] shares many of the same basic properties as [TextStyle].
/// ///
/// If the given text style is null, returns this strut style. /// If the given text style is null, returns this strut style.
StrutStyle inheritFromTextStyle(TextStyle other) { StrutStyle inheritFromTextStyle(TextStyle other) {
......
...@@ -106,18 +106,38 @@ const String _kColorBackgroundWarning = 'Cannot provide both a backgroundColor a ...@@ -106,18 +106,38 @@ const String _kColorBackgroundWarning = 'Cannot provide both a backgroundColor a
/// ///
/// ### Line height /// ### Line height
/// ///
/// By default, text will layout with line height as defined by the font.
/// Font-metrics defined line height may be taller or shorter than the font size.
/// The [height] property allows manual adjustment of the height of the line as
/// a multiple of [fontSize]. For most fonts, setting [height] to 1.0 is not
/// the same as omitting or setting height to null. The following diagram
/// illustrates the difference between the font-metrics defined line height and
/// the line height produced with `height: 1.0` (also known as the EM-square):
///
/// ![Text height diagram](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_diagram.png)
///
/// {@tool sample} /// {@tool sample}
/// The [height] property can be used to change the line height. Here, the line /// The [height] property can be used to change the line height. Here, the line
/// height is set to 5 times the font size, so that the text is very spaced out. /// height is set to 5 times the font size, so that the text is very spaced out.
/// Since the `fontSize` is set to 10, the final height of the line is
/// 50 pixels.
/// ///
/// ```dart /// ```dart
/// Text( /// Text(
/// 'Don\'t act surprised, you guys, cuz I wrote \'em!', /// 'Don\'t act surprised, you guys, cuz I wrote \'em!',
/// style: TextStyle(height: 5.0), /// style: TextStyle(fontSize: 10, height: 5.0),
/// ) /// )
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
/// ///
/// {@tool sample}
///
/// Examples of the resulting heights from different values of `TextStyle.height`:
///
/// ![Text height comparison diagram](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_comparison_diagram.png)
///
/// {@end-tool}
///
/// ### Wavy red underline with black text /// ### Wavy red underline with black text
/// ///
/// {@tool sample} /// {@tool sample}
...@@ -439,9 +459,28 @@ class TextStyle extends Diagnosticable { ...@@ -439,9 +459,28 @@ class TextStyle extends Diagnosticable {
/// The height of this text span, as a multiple of the font size. /// The height of this text span, as a multiple of the font size.
/// ///
/// If applied to the root [TextSpan], this value sets the line height, which /// When [height] is null or omitted, the line height will be determined
/// is the minimum distance between subsequent text baselines, as multiple of /// by the font's metrics directly, which may differ from the fontSize.
/// the font size. /// When [height] is non-null, the line height of the span of text will be a
/// multiple of [fontSize] and be exactly `fontSize * height` logical pixels
/// tall.
///
/// For most fonts, setting [height] to 1.0 is not the same as omitting or
/// setting height to null because the [fontSize] sets the height of the EM-square,
/// which is different than the font provided metrics for line height. The
/// following diagram illustrates the difference between the font-metrics
/// defined line height and the line height produced with `height: 1.0`
/// (which forms the upper and lower edges of the EM-square):
///
/// ![Text height diagram](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_diagram.png)
///
/// {@tool sample}
///
/// Examples of the resulting line heights from different values of `TextStyle.height`:
///
/// ![Text height comparison diagram](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_comparison_diagram.png)
///
/// {@end-tool}
final double height; final double height;
/// The locale used to select region-specific glyphs. /// The locale used to select region-specific glyphs.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment