Cosmic Text: Pure Rust multi-line text handling
github.com
github.com
Now that is testing.
I recently got a tip from someone who recently worked on a project I was at that he had a suspicion some validation code didn't cover all all cases.
There were maybe tre tests which where all green.
So I generated 50 random, but known valid cases and, voila, a few of them failed.
From there it was easy to fix the issue.
The biggest difficulty is trying to create good test cases. Unlike normal tests where you're asserting that some specific case is true, with quickcheck-style tests, you're instead trying to find an invariant in your code that will always be true. So it's quite easy to do "this algorithm never throws an error" but harder to do "this algorithm returns true in these cases and false in these cases". That said, a lot of tools in this space give you a lot of flexibility for generating cases, like saying "generate strings matching this regex" or "generate these arbitrary primitives and map then into the correct structure with this function".
The randomness involved isn't ideal, because you can't necessarily guarantee that the same errors will always show up every time, but it's a good habit to copy the failing generated case into its own test as a kind of regression test. And obviously still write a lot of the standard test cases for more obvious problem points.
I'd definitely love to use it more though, it's one of those things I come back to every so often, unfortunately thus far without much success.
One problem though is that being better at finding bugs isn’t always great: if you find bugs you might feel the desire to fix them but for many software teams, having rare bugs is acceptable, even if they aren’t rare in absolute terms (ie a 1-in-10,000,000 when you have a billion chances for it to happen a day)
I've been thinking of how browsers are eventually proving to be bad at complex text rendering, like for a word processor. You either have to make do with mimicking DOM like a text layouting engine or use browser canvas APIs - both of them locks your codebase into a single target (the browser).
The right way feels like using a cross platform text rendering engine like Skia (or this one, if it could be compiled to WASM and somehow made to write to a browser canvas)
These projects are important for that future. The one where word-processor like apps can be written cross platform by using libraries like these.
While browsing this, I found the Rustybuzz project which implements the Harfbuzz shaping algorithm in pure Rust. Harfbuzz is yet another example of a mission critical piece of infrastructure that is maintained with meager resources. It is excellent to have an alternative.
Text rendering is truly a daunting engineering problem.
The safety aspects of Rust are well suited to this problem space, as the fonts and the text we render are often coming from untrusted sources. TrueType font parser bugs have been exploited in the past.
Thanks to everyone who has put their time and effort into this. Truly important work.
- https://github.com/RazrFalcon/rustybuzz
I am guessing that most normal people desktop distros like popos just have something like fcitx set up. In which case you'd just have to install the plugin, or, I wouldn't be surprised if it comes preinstalled on some of them.
It’s just very different from MacOS and Windows where you just add a keyboard layout and you’re done, that’s why I was curious. Admittedly I love the amount of customisation options on fcitx.