diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index b26e545764..f471e6796b 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -853,6 +853,13 @@ class TextPainter { /// A given selection might have more than one rect if this text painter /// contains bidirectional text because logically contiguous text might not be /// visually contiguous. + /// + /// Leading or trailing newline characters will be represented by zero-width + /// `Textbox`es. + /// + /// The method only returns `TextBox`es of glyphs that are entirely enclosed by + /// the given `selection`: a multi-code-unit glyph will be excluded if only + /// part of its code units are in `selection`. List getBoxesForSelection( TextSelection selection, { ui.BoxHeightStyle boxHeightStyle = ui.BoxHeightStyle.tight, diff --git a/packages/flutter/lib/src/painting/text_style.dart b/packages/flutter/lib/src/painting/text_style.dart index e1beaba552..ebc33dea1d 100644 --- a/packages/flutter/lib/src/painting/text_style.dart +++ b/packages/flutter/lib/src/painting/text_style.dart @@ -150,6 +150,47 @@ const double _kDefaultFontSize = 14.0; /// /// See [StrutStyle] for further control of line height at the paragraph level. /// +/// ### Leading Distribution and Trimming +/// +/// [Leading](https://en.wikipedia.org/wiki/Leading) is the vertical space +/// between glyphs from adjacent lines. Quantitatively, it is the line height +/// (see the previous section) subtracted by the font's ascent and descent. +/// It's possible to have a negative `Leading` if [height] is sufficiently +/// small. +/// +/// When the [height] multiplier is null, `leading` and how it is distributed +/// is up to the font's +/// [metrics](https://en.wikipedia.org/wiki/Typeface#Font_metrics). +/// When the [height] multiplier is specified, the exact behavior can be +/// configured via [leadingDistribution] and [TextPainter.textHeightBehavior]. +/// +/// ![In configuration 1 the line height is divided by the alphabetic baseline proportionally to the font's ascent and descent, in configuration 3 the glyphs are roughly centered within the line height, configuration 2 is similar to configuration 1 except the Text Top guide on the same line as the font's ascent](https://flutter.github.io/assets-for-api-docs/assets/painting/text_height_breakdown.png) +/// +/// Above is a side-by-side comparison of different [leadingDistribution] and +/// [TextPainter.textHeightBehavior] combinations. +/// +/// * Configuration 1: The default. [leadingDistribution] is set to [TextLeadingDistribution.proportional]. +/// * Configuration 2: same as Configuration 1, except [TextHeightBehavior.applyHeightToFirstAscent] is set to false. +/// * Configuration 3: [leadingDistribution] is set to [TextLeadingDistribution.even]. +/// * Configuration 4: same as Configuration 3, except [TextHeightBehavior.applyHeightToLastDescent] is set to false. +/// +/// The [leadingDistribution] property controls how leading is distributed over +/// and under the text. With [TextLeadingDistribution.proportional] +/// (Configuration 1), `Top Leading : Bottom Leading = Font Ascent : Font +/// Descent`, which also means the alphabetic baseline divides the line height +/// into 2 parts proportional to the font's ascent and descent. With +/// [TextLeadingDistribution.even] (Configuration 3), `Top Leading` equals +/// `Bottom Leading`, and the glyphs are roughly centered within the allotted +/// line height. +/// +/// The [TextPainter.textHeightBehavior] is a property that controls leading at +/// the paragraph level. The `applyHeightToFirstAscent` property is applied +/// **after** [height] and [leadingDistribution]. Setting it to false trims the +/// "Top Leading" of the text box to match the font's ascent if it's on the +/// first line (see Configuration 2). Similarly setting +/// `applyHeightToLastDescent` to false reduces "Bottom Leading" to 0 for the +/// last line of text (Configuration 4). +/// /// ### Wavy red underline with black text /// /// {@tool snippet} @@ -578,7 +619,8 @@ class TextStyle with Diagnosticable { /// When a non-null [height] is specified, after accommodating the glyphs of /// the text, the remaining vertical space from the allotted line height will /// be distributed over and under the text, according to the - /// [leadingDistribution] property. + /// [leadingDistribution] property. See the [TextStyle] class's documentation + /// for an example. /// /// When [height] is null, [leadingDistribution] does not affect the text /// layout.