Skip to content
Myr

README.md

This file

Path

tests/oversample/README.md

94 lines · 4.5 KiB · Markdown

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than aeec66c point at it.

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1–94why, one step back
1# Font character oversampling for rendering from atlas textures
23TL,DR: Run oversample.exe on a windows machine to see the4benefits of oversampling. It will try to use arial.ttf from the5Windows font directory unless you type the name of a .ttf file as6a command-line argument.78## Benefits of oversampling910Oversampling is a mechanism for improving subpixel rendering of characters.1112Improving subpixel has a few benefits:1314* With horizontal-oversampling, text can remain sharper while still being sub-pixel positioned for better kerning15* Horizontally-oversampled text significantly reduces aliasing when text animates horizontally16* Vertically-oversampled text significantly reduces aliasing when text animates vertically17* Text oversampled in both directions significantly reduces aliasing when text rotates1819## What text oversampling is2021A common strategy for rendering text is to cache character bitmaps22and reuse them. For hinted characters, every instance of a given23character is always identical, so this works fine. However, stb_truetype24doesn't do hinting.2526For anti-aliased characters, you can actually position the characters27with subpixel precision, and get different bitmaps based on that positioning28if you re-render the vector data.2930However, if you simply cache a single version of the bitmap and31draw it at different subpixel positions with a GPU, you will get32either the exact same result (if you use point-sampling on the33texture) or linear filtering. Linear filtering will cause a sub-pixel34positioned bitmap to blur further, causing a visible de-sharpening35of the character. (And, since the character wasn't hinted, it was36already blurrier than a hinted one would be, and now it gets even37more blurry.)3839You can avoid this by caching multiple variants of a character which40were rendered independently from the vector data. For example, you41might cache 3 versions of a char, at 0, 1/3, and 2/3rds of a pixel42horizontal offset, and always require characters to fall on integer43positions vertically.4445When creating a texture atlas for use on GPUs, which support bilinear46filtering, there is a better approach than caching several independent47positions, which is to allow lerping between the versions to allow48finer subpixel positioning. You can achieve these by interleaving49each of the cached bitmaps, but this turns out to be mathematically50equivalent to a simpler operation: oversampling and prefiltering the51characters.5253So, setting oversampling of 2x2 in stb_truetype is equivalent to caching54each character in 4 different variations, 1 for each subpixel position55in a 2x2 set.5657An advantage of this formulation is that no changes are required to58the rendering code; the exact same quad-rendering code works, it just59uses different texture coordinates. (Note this does potentially increase60texture bandwidth for text rendering since we end up minifying the texture61without using mipmapping, but you probably are not going to be fill-bound62by your text rendering.)6364## What about gamma?6566Gamma-correction for fonts just doesn't work. This doesn't seem to make67much sense -- it's physically correct, it simulates what we'd see if you68shrunk a font down really far, right?6970But you can play with it in the oversample.exe app. If you turn it on,71white-on-black fonts become too thick (i.e. they become too bright), and72black-on-white fonts become too thin (i.e. they are insufficiently dark). There is73no way to adjust the font's inherent thickness (i.e. by switching to74bold) to fix this for both; making the font thicker will make white75text worse, and making the font thinner will make black text worse.76Obviously you could use different fonts for light and dark cases, but77this doesn't seem like a very good way for fonts to work.7879Multiple people who have experimented with this independently (me,80Fabian Giesen,and Maxim Shemanarev of Anti-Grain Geometry) have all81concluded that correct gamma-correction does not produce the best82results for fonts. Font rendering just generally looks better without83gamma correction (or possibly with some arbitrary power stuck in84there, but it's not really correcting for gamma at that point). Maybe85this is in part a product of how we're used to fonts being on screens86which has changed how we expect them to look (e.g. perhaps hinting87oversharpens them and prevents the real-world thinning you'd see in88a black-on-white text).8990(AGG link on text rendering, including mention of gamma:91 http://www.antigrain.com/research/font_rasterization/ )9293Nevertheless, even if you turn on gamma-correction, you will find that94oversampling still helps in many cases for small fonts.