TextPainter correctly deals with punctuation after words, but punctuation after inline maths gets separated from the maths.
E.g. "... $x$."

In this example the "." should not separate from the "$x$" and they should be grouped together for line-breaking purposes.