Skip to content

SkParagraph doesn't account for applyHeightToFirstAscent applyHeightToLastDescent on empty paragraphs with strut enabled #112123

Description

@LongCatIsLooong

Steps to Reproduce

const style = TextStyle(height: 11, fontSize: 7);
const simple = TextSpan(text: 'x', style: style);
const emptyString = TextSpan(text: '', style: style);
const emptyParagraph = TextSpan(style: style);

final painter = TextPainter(
  textDirection: TextDirection.ltr,
  strutStyle: StrutStyle.fromTextStyle(style),
  textHeightBehavior: const TextHeightBehavior(applyHeightToFirstAscent: false, applyHeightToLastDescent: false),
);

for (final span in [simple, emptyString, emptyParagraph]) {
  painter.text = span;
  painter.layout();
  final realHeight = painter.height;
  final preferredHeight = painter.preferredLineHeight;
  print('${span.text} > real height: $realHeight, preferred height: $preferredHeight');
}

"real height" should always be the same as "preferred height" (8 px) in all 3 cases. However right now for both "emptyString" and "emptyParagraph" the reported "real height" is 77.0.

Activity

  1. jason-simmons commented on Sep 23, 2022

    @jason-simmons
    Member

    I tried the example in SkParagraph and the old Libtxt text layout library:

    Libtxt

    Text painter.height painter. preferredLineHeight painter.size
    simple 77.0 77.0 (4.0, 77.0)
    emptyString 77.0 77.0 (0.0, 77.0)
    emptyParagraph 77.0 77.0 (0.0, 77.0)

    SkParagraph

    Text painter.height painter. preferredLineHeight painter.size
    simple 8.0 8.0 (4.0, 8.0)
    emptyString 77.0 8.0 (0.0, 77.0)
    emptyParagraph 77.0 8.0 (0.0, 77.0)

    I think that Libtxt's results are correct in this case. SkParagraph does not appear to be applying the strut as intended.

    The strut defines a minimum line height that applies to all lines in the paragraph (see https://api.flutter.dev/flutter/painting/StrutStyle-class.html). But for some of these paragraphs SkParagraph is calculating height and preferredLineHeight that is less than the strut height.

    SkParagraph does produce results similar to Libtxt if the app does not set the TextHeightBehavior(applyHeightToFirstAscent: false, applyHeightToLastDescent: false) parameter. So it looks like SkParagraph is improperly allowing the TextHeightBehavior to override the strut height.

    @Rusino

  2. jason-simmons commented on Sep 24, 2022

    @jason-simmons
    Member
  3. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    p: material_uimaterial_ui package in flutter/packages
    on Sep 28, 2022
  4. jason-simmons commented on Oct 11, 2022

    @jason-simmons
    Member

    The fix has landed on the Flutter master branch
    (see https://skia.googlesource.com/skia/+/d0ee294bc90fe676458b32417737ecae409a4c21)

  5. github-actions commented on Oct 25, 2022

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  6. locked as resolved and limited conversation to collaborators on Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsa: typographyText rendering, possibly libtxtdependency: skiaSkia team may need to help usframeworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions