{"id":3145,"date":"2014-03-14T19:28:08","date_gmt":"2014-03-14T09:28:08","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=3145"},"modified":"2014-03-15T18:08:42","modified_gmt":"2014-03-15T08:08:42","slug":"font-rendering","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2014\/03\/14\/font-rendering\/","title":{"rendered":"Font rendering"},"content":{"rendered":"

\"BeforeAfter\"<\/a>I’ve been spending a surprising amount of time working on font rendering.\u00a0 Particularly considering that VectorStorm has supported font rendering for rather a long time now;\u00a0 it’s only been recently that I’ve really spent a lot of time making it exactly right.<\/p>\n

Drawing text is tricky for a number of reasons.\u00a0 For one thing, font glyphs are usually complicated, with smooth curves and other shapes which are difficult to represent nicely as triangles, particularly at small sizes.\u00a0 In games, we usually want to draw text as images.\u00a0 That way, each letter becomes a single rectangle with an image on it.\u00a0 It sounds like that’s all that should be required, but there are further problems.<\/p>\n

<\/p>\n

First, it’s worth pointing out that Valve Software have done some awesome work on improving drawing high-quality big text<\/a>.\u00a0 For text inside a 3D scene which players might approach, that’s exactly what you want.\u00a0 But it doesn’t work at all for drawing small, crisply antialiased text, and that’s what I’m going to talk about in this post.<\/p>\n

In practice, using textures is the only practical way to handle small text glyphs.\u00a0 The chief problem with this approach is that you have all the problems of textures — if you draw your texture too large on the screen, it starts to look blocky.\u00a0 Similarly, if you draw it too small, you start to lose detail.\u00a0 In a regular texture, that’s not a problem — nobody minds if a particular rock in a pile of rubble starts to go indistinct as it becomes smaller on screen.\u00a0 But when we’re drawing text, losing information can make the text blurry or otherwise unreadable.\u00a0 In the image above, there are only two mistakes in the top copy of the text, but it makes the text look very clunky and blurry compared against the correct rendering, beneath it.<\/p>\n

Drawing text in the wrong size<\/h2>\n

The first common mistake is of drawing text in the wrong size.\u00a0 If the image of your glyph is 20 pixels tall, you need to draw it 20 pixels tall on-screen, or else it will become either blurry or blocky (depending on your texture sampling mode).\u00a0 The bigger the difference in size, the worse the problem.\u00a0 In the image above, the top text is being drawn about two pixels larger than the images it’s drawing from, and that’s turning the text very slightly blocky.<\/p>\n

In a real game, you’ll probably want several different font sizes in your game.\u00a0 You may also want to support different screen resolutions.\u00a0 Each combination of these will require separate fonts, so you can draw text at the correct size in each situation.<\/p>\n

Right now, MMORPG Tycoon 2 has seven different versions of the same font, ranging from 12- to 48-point, and I still need to generate a few larger ones for use in places like the title screen.\u00a0 I’ve been careful in my choices of font size to try to reuse fonts for example, if you run on a high DPI screen, the game will use the 24-point sprite font where the game wants a 12-point sprite font.\u00a0 But that same 24-point sprite font is used on standard DPI for the spots where I do want a 24-point sprite font.\u00a0 (and the high DPI version will use the 48-point font).\u00a0 This gives you a good variety of available print sizes, while still maintaining far better text quality than if you tried to draw a large sprite font into a small space on the screen.<\/p>\n

Drawing text misaligned from the screen<\/h2>\n

Remember that we have our text as an image.\u00a0 As discussed above, we’ve ensured that we draw our text at exactly the same size in our game as the font images exist in our data, to avoid scaling those images on the screen.\u00a0 So we should be good, right?<\/p>\n

There’s another problem, though.\u00a0 A computer screen is made up of a fixed grid of pixels, each pixel a fixed-size box which can only show one color at a time.\u00a0 The same is true of our texture — a fixed grid of pixel colors.\u00a0 What happens if when we tell the computer to draw our texture to the screen, we don’t precisely line up the texture’s grid of pixels with the screen’s grid of pixels, so each screen pixel overlaps several of the pixels in the texture?<\/p>\n

When this happens with normal rendering (which it does almost all the time<\/em>), we want the computer to blend between the colors of the texture pixels which intersect each screen pixel.\u00a0 This works great for most textures, but it’s catastrophic for text — we’ve carefully built anti-aliasing into our text to make it appear as sharp as possible at exactly its current size;\u00a0 adding any blur at all will likely make the text quite blurry.\u00a0 (In the top image above, the text is being drawn half a pixel lower than it should be, accidentally introducing extra vertical blurring as each screen pixel is displaying the average of two glyph texture pixels).\u00a0 Alternately, we can tell the computer not to blur between pixels, but to instead just use the texture pixel which is closest to the middle of the screen pixel, which at least stops the blur, but can result in different glyphs of our text moving jumping up, down, left, or right by a pixel based upon which direction each pixel thinks is closest — this can make the text appear very uneven, and is possibly even worse than being blurry.<\/p>\n

So not only do we need to be sure that we draw our text at exactly the right size so that it takes up the same number of pixels on the screen as it does in the texture, but we also need to make sure that those pixels are exactly aligned between the texture and the screen.<\/p>\n

How do we do this?<\/h2>\n

Well, there are a lot of approaches.\u00a0 But here’s the simple approach that I’m using in VectorStorm for the information-dense displays in MMORPG Tycoon 2:<\/p>\n

    \n
  1. For critical text that needs to be drawn small and crisply, render them in an orthographic projection which has the vertical and horizontal clipping planes set at 0 and at the width\/height-in-pixels of the rendering context.\u00a0 This way, a movement of 1 unit in that space will exactly correspond to a movement of 1 pixel on the screen.<\/li>\n
  2. When drawing text, you draw it at a scale which matches its original pixel size — drawing a 24-point sprite font at a size of 24 units in this orthographic projection ensures that your text will come out at exactly 24 pixels tall, without needing to do any special math.<\/li>\n
  3. Immediately before drawing text, inspect the local to world matrix, and round off any accumulated translations to the nearest integer.\u00a0 This effectively snaps fonts to the nearest pixel, to ensure that they don’t get smeared between multiple pixels.\u00a0 (You probably want to restore the old matrix once you’re done)<\/li>\n<\/ol>\n

    Where sprite fonts come from<\/h2>\n

    “Sprite fonts” have been around for ages.\u00a0 I’ve used them in every project I’ve ever worked on, dating all the way back to the original PlayStation, and there exist several tools which will convert normal vector fonts into sprite fonts.\u00a0 In VectorStorm, I used my own tool, “FontMaker” to do the job, but it was a bit of a hack and never worked 100% correctly, so I’ve now added support for the ubiquitous BMFont<\/a> sprite font format to VectorStorm v2 (which is still in development on Github), and that’s what I recommend.\u00a0 (There are lots of other tools which spit out the same sprite format, too, so look around to see what works for you)<\/p>\n

    Another approach used to draw text in games is to use a library like Freetype or SDL_ttf to render full strings into textures, and then draw those full-string textures to screen.\u00a0 That’s a great option if your text doesn’t change often, but it doesn’t solve these issues;\u00a0 that full-string texture suffers from both these same issues, if you’re not drawing it at exactly the correct size or position (but at least you don’t have the problem of individual letters wiggling up and down independently of the rest of the text).<\/p>\n","protected":false},"excerpt":{"rendered":"

    I’ve been spending a surprising amount of time working on font rendering.\u00a0 Particularly considering that VectorStorm has supported font rendering for rather a long time now;\u00a0 it’s only been recently that I’ve really spent a lot of time making it exactly right. Drawing text is tricky for a number of reasons.\u00a0 For one thing, font…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[33],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/po9WK-OJ","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3145"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=3145"}],"version-history":[{"count":1,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3145\/revisions"}],"predecessor-version":[{"id":3147,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3145\/revisions\/3147"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=3145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=3145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=3145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}