[HN Gopher] Writing a TrueType font renderer
___________________________________________________________________
Writing a TrueType font renderer
Hi HN, happy new year! TrueType is really a neat and fun format,
packed with esoterica. This post gives some background on the
problem of text rendering in general, explains how TrueType works
under the hood, and contains lots of tidbits that I hope will be
interesting. There's also a bunch of screenshots of the in-
progress renderer stumbling over itself. I hope you enjoy the read!
Author : codyd51
Score : 128 points
Date : 2024-01-01 18:01 UTC (4 hours ago)
(HTM) web link (axleos.com)
(TXT) w3m dump (axleos.com)
| signaru wrote:
| Great to see more accessible references on font internals! I have
| dabbled on this a bit last year and managed to have a parser and
| render the points in a glyph's contour (I stopped before Bezier
| and shape filling stuff). I still have not considered hinting, so
| it's nice that it's covered. Besides Apple's and Microsoft's
| extensive documentations, what really helped me is an article
| from the Handmade Network [1] and the source of stb_truetype [2]
| (also used in Dear ImGUI).
|
| [1]
| https://handmade.network/forums/articles/t/7330-implementing....
|
| [2] https://github.com/nothings/stb/blob/master/stb_truetype.h
| 082349872349872 wrote:
| For a problem that is theoretically binary (either in, or out?)
| filling polys can have really fiddly corner (literally!) cases.
|
| Thanks for the WIP screenshots; they made me feel much better
| about my own slicers/rasterisers.
|
| (there's probably a trick I'm missing*, but so far I've had the
| most robust results for 3D booleans by chasing fiddly
| overlaps/intersections on lower facets, all the way down to 0-D
| if necessary)
|
| * like working directly with cubics, a la Jim Blinn?
| kevingadd wrote:
| Filling polys is especially hard if you want to run on the GPU
| where access to denormals is inconsistent/nonexistent. Most of
| the algorithms in CS literature simply won't work. It's a fun
| challenge, at least!
| nsajko wrote:
| > For a problem that is theoretically binary (either in, or
| out?) filling polys can have really fiddly corner (literally!)
| cases.
|
| FTR, in case someone is interested: "predicate" is the name for
| functions that output booleans, so a more general search term
| is "geometric predicate".
| kevingadd wrote:
| I really appreciate that you provided a bunch of screenshots of
| the in-progress development. Graphics programming is very
| challenging, and it can be frustrating to feel like you've spent
| multiple days just rendering glitch art. It's always good to show
| others how much we struggle even if we're experts :)
|
| Having recently implemented a subset of opentype kerning, I can
| only imagine how stressful it was to implement the whole core
| truetype spec...
| vardump wrote:
| Wrote a truetype rasterizer about 20 years ago. I took the easy
| way out and just simply antialised everything, didn't even try to
| support TrueType hinting programs.
|
| Sure, things looked blurry, but it didn't matter much when
| everything would be rotated into weird angles and sizes anyways.
|
| Bezier and implementing correct and FAST filling rules took a lot
| of effort.
| cbrpnk wrote:
| "There's few things in this world I love more than taking an
| opaque binary and gradually uncovering the organization that was
| present all along. Dissecting the underlying and general
| structure of a binary is as gratifying as anything I can
| imagine."
|
| Yes!
| ianlevesque wrote:
| > Inexplicably, though, the TTF stores all fields in big endian
|
| Both the 68k and PPC (as configured for macs) were big endian.
| vitiral wrote:
| I wonder, is there something on the complexity curve between 8x8
| bitmap and fullblown ttf with all the complexity as you've
| outlined?
|
| It would be nice if there were a solution which gets 90% of the
| value in 10% of the code.
| stravant wrote:
| You're looking for SDF fonts.
|
| Very simple at their core but you can incrementally do more
| work to increase the fidelity you get out of them.
| colonwqbang wrote:
| Using a better, higher-resolution bitmap font? They don't have
| to look as awful as that
|
| https://terminus-font.sourceforge.net/img1/12x24b.gif
| userbinator wrote:
| I think TTF isn't that difficult if you just want the basics; I
| wrote a TTF viewer that simply dumped the outlines into GDI and
| let the latter do the rasterisation, without any hinting or other
| fancy stuff. Less than 400 LoC in total including the UI code,
| and the binary was 7KB.
| txdv wrote:
| link please
| hgs3 wrote:
| Correct me if I'm wrong, but I think font hinting isn't needed
| for high DPI displays? I believe macOS Mojave removed subpixel
| anti-aliasing because Apple doesn't ship anything but high DPI
| displays anymore.
| grishka wrote:
| > because Apple doesn't ship anything but high DPI displays
| anymore
|
| People still connect their Apple computers to low-DPI monitors
| all the time. High-DPI monitors are still a rarity. There's the
| LG UltraFine, the Studio Display (too expensive for what it
| is), the Pro Display XDR (the $1000 stand lmao), and that's
| really it.
|
| I'm writing this comment on a 2K monitor connected to a modern
| MacBook.
| jahewson wrote:
| Great article, I've had the fun of implementing TTF rendering and
| this piece hits all the memorable points.
|
| One nit, the comparison to Mach-O is anachronistic as TrueType
| was developed in the late 1980s, while Apple didn't acquire NeXT
| until 1996.
___________________________________________________________________
(page generated 2024-01-01 23:00 UTC)