Skip to content

Expose preferredLineHeigh, getFullHeightForCaret and getLineBoundary on RenderParagraph #65069

@pulyaevskiy

Description

@pulyaevskiy

Use case

I've created a rich text editor for Flutter and currently work on cleaning up and refactoring its rendering layer.

In the editor I'm taking advantage of the built-in RenderParagraph to represent a single paragraph in a document.
Since I'm using it in an editable context I need it to provide a few more metrics related to handling of editing cursor:

  • preferredLineHeight
  • getFullHeightForCaret
  • getLineBoundary

These methods would be proxying to the underlying TextPainter instance. Right now I have implemented a workaround for preferredLineHeight but it doesn't seem like I can create one for getFullHeightForCaret or getLineBoundary.

I noticed that RenderParagraph already exposes some editing-specific methods (getOffsetForCaret and getBoxesForSelection) so it doesn't seem like it'd go against certain design goals for this class.

Proposal

I'd like to extend RenderParagraph public interface with following three methods:

  /// An estimate of the height of a line in the text. See [TextPainter.preferredLineHeight].
  /// This does not required the layout to be updated.
  double get preferredLineHeight => _textPainter.preferredLineHeight;

  /// Returns the tight bounded height of the glyph at the given [position].
  ///
  /// Valid only after [layout].
  double? getFullHeightForCaret(TextPosition position, Rect caretPrototype) {
    assert(!debugNeedsLayout);
    _layoutTextWithConstraints(constraints);
    return _textPainter.getFullHeightForCaret(position, caretPrototype);
  }

  /// Returns the text range of the line at the given offset.
  ///
  /// The newline, if any, is included in the range.
  TextRange getLineBoundary(TextPosition position) {
    assert(!debugNeedsLayout);
    _layoutTextWithConstraints(constraints);
    return _textPainter.getLineBoundary(position);
  }

I will be submitting a PR with above changes shortly, just need to go over the contribution checklist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: typographyText rendering, possibly libtxtc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions