I noticed that the algorithm is not quite like what you described, here are two examples,
https://jsfiddle.skia.org/canvaskit/c5e71ac56041f759246edf7609a60df2daeb08044ce2c15cffa9faa1a9bf9cbbIn the first example paragraph is broken as I would like, despite that the second word is definitely longer than line.
The second example shows that if I add third word, it will, for some reason, break the second word in the middle.
And the third example shows that if I add one more letter to the second word from the first example, it will break second word in the middle.
I am not sure that it is right behaviour, looks like some sort of a bug.
My problem is basically the following, I have a rich text editor written in html, but it is not very performant, since I want to be able to scale this editor quite freequently. So I decided to live the editing part using html, since it is already written, but display the editor using canvas-kit, so that it is able to scale fast. Since editing is done via html, I should stick to their way of wrapping and centering text, I sent some html code in one of previous posts.
Your solution seems fine, but I am concerned about performance, I am not sure how expensive the rebuild of paragraph is, since I have to do that several times just for single change, however I can imagine some ways to optimize that.
About another approach. Is it possible to change the skia's algorithm itself? This difference between wrapping seems to be minor, maybe there is an exact place in the code which handles that. This solution is quite good, because I will be able to use paragraph in the same way I do now, without tweaking (except that magic with different kind of whitespaces)