编写SDF字体指南
Writing a Guide to SDF Fonts

原始链接: https://www.redblobgames.com/blog/2026-02-26-writing-a-guide-to-sdf-fonts/

2024年,作者在个人项目中尝试了Signed Distance Field (SDF)字体渲染,并将过程记录在网上。令人惊讶的是,这些不完整的笔记在2025年末开始在“SDF字体”的搜索结果中排名很高。意识到需要一个更好的资源,作者开始了一年的旅程来创建一个全面的指南。 最初的尝试过于雄心勃勃,涵盖了多个SDF库。项目被反复缩减,最终只专注于`msdfgen`及其各种参数。在意识到无休止的测试没有价值后,作者从一个参数导向的指南转变为“如何操作”的方法,然后又转回核心概念。 最终,作者确定了一个“概念”页面,解释了SDF *是什么* 以及 *如何* 实现所需的效果,并将实现细节移至单独的资源。虽然没有创建可下载的项目,但作者现在对结果感到满意,并希望它能成为学习SDF字体的权威在线资源。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 编写 SDF 字体指南 (redblobgames.com) 11 分,由 chunkles 1 小时前发布 | 隐藏 | 过去 | 收藏 | 讨论 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Back in 2024 I learned about SDF (signed distance field) rendering of fonts. I was trying to implement outlines and shadows in a single pass instead of drawing over the text multiple times in different styles. I intended to use these fonts for two different projects, a game and a map generator. I got things working but didn’t fully understand why certain things worked or didn’t work. I wrote some notes on my site about what I tried. In the end, I stopped working on both the game’s fonts and the map generator, so I put all of this on hold.

Contour line visualization of distance fields

Fast forward to late 2025, and my incomplete notes sometimes show up on the first page of search results for “sdf fonts”[1]! Surely that isn’t the best page on the topic. It would be better to point to library documentation or maybe one of the research papers about the topic. My page isn’t that good.

Initially my thought was “search engines are in their decline” but then I decided “this is an opportunity”. I decided to make a page worthy of being the top search result.

I first looked through everything I had written. I already had started an “overview” page but hadn’t gotten very far on it. I also have 22 separate pages that were “diary style”, about what I did rather than what you should know.

The overview page covered how to use various SDF font libraries (msdfgen, stb_truetype, tiny-sdf, etc.). I wrote code for multiple libraries, had sketched out diagrams for various concepts, and had screenshots of outputs from each of those libraries.

Sketch of distance fields
Sketched out concepts, using Excalidraw[2]

At some point I realized the scope was too large. I had spent the most time with msdfgen and hadn’t yet learned enough about the other libraries to write a proper guide. They all worked differently. I kept getting stuck. So I reduced the scope. In redesign 2 I decided to only use msdfgen, but show the various tradeoffs involved (atlas size, antialias width, shader derivatives, smoothing function).

I made several diagrams for concepts, such as:

Diagram of glyph bounds Diagram of layout algorithm Diagram of reading from font atlas
Diagrams from redesign 2, using Excalidraw[3]

And I started running tests. I wanted to compare the effect of atlas size, so I made lots of screenshots and started looking closely. I wanted to come up with a way to recommend a specific size. I wanted to make recommendations for all the other parameters. I showed all the commands I ran.

At some point I realized I could run tests forever. And I had already done that last year, and wrote it up in blog posts (one and two). Doing it again here didn’t seem especially valuable. So I pivoted to a “how to” page. In redesign 3 I decided to show the concepts, then a JavaScript implementation using CPU rendering, and then another implementation using GPU rendering. I made new versions of the diagrams:

Cleaned up diagram of glyph bounds Cleaned up diagram of layout algorithm Cleaned up diagram of reading from font atlas
Diagrams from redesign 3, hand-written SVG

I was making progress on that page but it didn’t feel like a Red Blob Games page. The page started out with tons of shell commands, and then showed lots of code. It felt like a page that only I would find useful. So I started over and designed a “concepts” page. In redesign 4 I focused on what effects I wanted, how SDF works, and how to use it to create those effects. I again reduced the scope by removing the implementation details. What I had already written, I moved to a separate (unpolished) page. And I never wrote a standalone downloadable project like I originally wanted.

I had tried several ideas over the past year but didn’t make a lot of progress until I figured out what I actually wanted to put on the page:

Chart of how active I was each day
Work over the past year, using Cal-heatmap[4]

I’m finally happy with the page.  Take a look! I hope search engines point to it eventually.

联系我们 contact @ memedata.com