P
P
Petr Vasiliev2019-01-28 20:27:38
Dart
Petr Vasiliev, 2019-01-28 20:27:38

How to count the number of characters to display in an image?

There is a big line. A very, very large line. Approximately 100k characters. It is necessary to organize page-by-page and line-by-line output of this line. The question arises: how to calculate the maximum number of characters in a line, if we do not know for sure either the width of one character (each character has a different width) or the width of the screen for output (all devices are different)? Are there any ready-made algorithms?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KnightForce, 2020-02-25
@KnightForce

You can pre-render this text.
But first break it into small pieces, otherwise there may be jambs.
Therefore, look how much you fit, select the sizes.

final TextPainter textPainter = TextPainter(
        text: TextSpan(text: text, style: style), maxLines: 1, textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.size;

1) Take the first 100 characters.
2) Compare the size of a piece of text with the size of the screen. If it still fits, take it.
3) Subtract from the rest of the text as many characters as there were in the end in that part of the text
6) Render.
7) Repeat this until you get all the text.
8) Either do everything through Fluture. Either create an isolate, feed him the text, and let him give you pieces of the text on demand or periodically. When initializing it, pass it the size of the screen or the widget in which you will display text. If the sizes are hardcore - everything is simple, otherwise xthtp double width = (context.findRenderObject() as RenderBox).size.width

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question