Handmade Hero»Forums»Code
Ameen Sayegh
51 posts
Math requirement for TrueType format
Edited by Ameen Sayegh on
I love looking and parsing file formats.

When I saw that on handmade hero Casey used STB library I thought why he didn't parse TryeType format manually just like he did with BMP and WAV files.
I thought I should give it a shot.

I looked at the old documentation at Microsoft and Apple.
Also I looked at the STB source code and some other libraries until I got to the point where I can get a list of vertices for a given codepoint.

So far it wasn't that difficult.
These vertices forms a spline curve which I never worked with before.
The spline need to be divided into multiple bezier curves and then they need to be rasterized to a bitmap.

I don't know anything about splines or rasterization, I didn't work with any of that before .
I know a little about bezier curves I watched the interpolation video done by Casey, and I know a little about parametric equations.

Basically I don't know where to start and I really want learn about these things.
I already looked Wikipedia page on splines but the math was really really crazy, it was never good for learning it is more of a reference.
So any topics I should start with, any materials, any related explanation, on any of the mentioned topics, is helpful and appreciated.
Mārtiņš Možeiko
2559 posts / 2 projects
Math requirement for TrueType format
I'm not very familiar with ttf format, but maybe this will help you - explanation how rasterization works in stb_truetype: http://nothings.org/gamedev/rasterize/
Bryan Taylor
55 posts
Math requirement for TrueType format
Edited by Bryan Taylor on
aameen951
I don't know anything about splines or rasterization, I didn't work with any of that before .
I know a little about bezier curves I watched the interpolation video done by Casey, and I know a little about parametric equations.

The particular type of spline used in TrueType is a composite Bezier curve (wiki). This is just a series of quadratic Bezier curves joined end-to-end (sharing the end points). (A quadratic curve has two endpoints and one control point, versus the two control points of a cubic curve, which is what Casey demonstrated in his video.) So, first curve is vertices 0, 1, 2, second is 2, 3, 4, etc.

Rasterization is a lot simpler than it seems. All you need is a way to answer "am I inside or outside of the shape?" Any pixel inside the shape is filled, anything outside is not. (Anti-aliasing makes this more complicated -- I'd suggest ignoring AA and figure out the rough rasterization first.) Sean's library uses a scanline approach: for each line of pixels, solve for the intersections. At any given pixel, if you have to cross an odd number of intersections, counting from the left, it's inside. (Like most geometry, it sounds more complicated than it actually is.)

Finally, as a general note: don't let Wikipedia scare you off of something. The math (and CS) articles are generally written by people who are terrible at explanation. High-school algebra will take you a lot further than you might think.
Ameen Sayegh
51 posts
Math requirement for TrueType format
Edited by Ameen Sayegh on
This is exactly why I'm asking.
DLLs, hash tables, file formats, assembly, reverse-engineering, parsing, interpreters, compilers and all programming actually will seem complicated and mysterious at first and then once you start tackling them they will become easier and more obvious.

Actually I figured out how to convert quadratic spline to a series of quadratic curves. All you need is if you have two off-curve points there is an implicit on-curve point which is the midpoint between the two.

Thank you for the information about rasterization. I'll definitely look at that.
but I would like to know more about drawing lines and curves anti-aliased, if you can point out something that I can look at.