The author has implemented a hyphenation solution for text layout in their Flutter app, addressing issues with long words not fitting on the screen.
Abstract
The author of the article has tackled the challenge of text hyphenation in their Flutter application, which became more apparent after using an iPhone 13 Mini. Recognizing two types of text display in the app—simple Text() for English and complex RichText() for interactive features—the author focused on improving the former by implementing an algorithm that breaks long words into syllables to fit on the page. This was achieved by utilizing a Dart package called hyphenator, which the author updated to support null safety. The implementation is available on GitHub and is intended to be a drop-in replacement for the default Text widget, offering automatic hyphenation. The author notes that while Flutter does not currently support hyphenation natively, there is an open issue requesting this feature.
Opinions
The author acknowledges the rarity of hyphenation on websites, with many opting to add a scroll bar or avoid hyphenation altogether.
There is an appreciation for the importance of good typography, as seen in the care taken to implement hyphenation and the mention of its common use in LaTeX documents.
The author found the LaTeX hyphenation algorithm too complex to implement directly and was not satisfied with ChatGPT's explanation of it.
The author is pleased with the responsiveness of the hyphenator package maintainer, who quickly addressed the null safety update despite the package's two-year period of inactivity.
The author encourages readers to support their work by clapping for the article, highlighting the significance of community support in the development process.
And when I wrote it I knew I’d probably have to improve it later. Because it didn’t work well for words that were too long to fit on the page. I needed hyphenation.
Now, this wasn’t that much of a problem at the time. I mean it still popped up but it was rare enough that I didn’t care. But then I got an iPhone 13 Mini and it made the problem more noticeable.
Not a lot. I’m always surprised by how the iPhone 13 Mini feels almost the same as my iPhone 12. But I was just finishing up another update at the time and I realized it was finally time to tackle this hyphenation problem.
Hyphenation Part 1
So, first of all I have two hyphenation problems in my app. Because text is displayed in two ways. The first is the dumb way where I just do Text(). This is used for the English text. Then we have the more complex way which uses a RichText().
The reason I use a RichText() is because of this thing I wanted to do. When you tap on a word the word is spoken to you and the translation is highlighted. So the way it does this is it actually passes a bunch of WidgetSpans.
So splitting these is going to be significantly more complicated because I’m going to have to unpack the WidgetSpans and repack them plus set up the state management so it still works with the glow effect. Maybe I might not even do it in this update.
The State Of Hyphenation
But the hyphenation problem I solved was for normal Text(). So you’ll notice that… actually I’m checking some websites and they don’t even bother with text hyphenation. A lot of websites just add a scroll bar or refuse to hyphenate the text at all.
But anyways, on websites that care about typography they add hyphenation which means they break words that are too long into two and add a hyphen. This is commonly seen in latex documents.
I actually wanted to copy latex’s algorithm but I just couldn’t read the whole thing. It was just too dry. I actually tried to get ChatGPT to explain the algorithm to me but it didn’t do a very good job.
Or maybe I’m just not good at wording my question. It’s a little disappointing as I saw people get simple sorting algorithms explained to them incredibly easily. Guess it doesn’t work well with complex algorithms.
But anyways, Flutter does not currently support hyphenation. But there is an issue for it.
So the last time I tried to fix hyphenation in my app I found out you could find the size of text before actually drawing it. And this worked surprisingly well as in it’s perfect. So this forms the basic idea behind my algorithm.
First we split our text into tokens by simply splitting at spaces. Then we keep on inserting tokens into the output until we run out of room on the line. Once we run out of room we want to try to break up the current word into syllables and insert as many syllables as possible. We use syllables, because, well, to ke-
ep this fro-
m happening.
Then we just repeat for all the other lines. It’s pretty simple. The only thing I need is a way to break a word into syllables. I was thinking about using an implementation from Javascript or something. But thankfully someone has already done this.
The only problem is it hasn’t been updated in 2 years so it doesn’t support null safety. Well, I just forked it and added that stuff. Surprisingly the maintainer responded to my pull request in like a day or two. It usually takes a week. And given this repo wasn’t updated in 2 years I expected it to take a month to get a response.
Anyways, with all the pieces in place I can present my implementation.