Extreme Fonts

    For a very long time, small raster fonts were the scourge of KolibriOS. But relatively recently, another developer joined the project - Pathoswithin . Quickly navigating the project, he set about solving this problem. You can observe the results of his work in the latest nightly builds. Well, this article is a story about working on fonts, written by Pathoswithin himself .


    Each font character is called a "glyph." In fact, this is an ordinary image. Are there many problems with drawing pictures? At least two. First of all, the font must be scaled, and if it is still possible to reduce it, then nothing good will come of increasing.



    Thus, glyphs must initially have high resolution, and to achieve maximum quality, several sets of different sizes are needed. And okay, if the font has 256 characters. What if unicode? Such a font will take up a lot of memory, even by modern standards.

    A bitmap stores the colors of each dot. But why, if you can keep a description of how to draw a glyph. Fortunately, fonts fit perfectly with the principles of vector graphics. In this case, it is enough to indicate the coordinates of several points between which you need to draw lines; maximum - parameters for drawing a curve. Since almost all points are drawn "on the fly", such glyphs are not tied to a specific resolution, and take up much less memory.



    The second problem: reduction, including smoothing, consists in using halftones. This gives an excellent result in relation to photographs, but the glyph is a contrasting image, most often a black symbol on a white background, and when shades of gray appear, it looks blurry. On the other hand, it is impossible to beautifully draw a small symbol using only two colors.

    To solve this problem, the features of LCD monitors are used. Each screen pixel in the horizontal direction consists of three subpixels: red, green and blue. The combination of different brightness of these colors forms all the rest. They are encoded in the same way, which makes it easy to manage subpixels individually. Moreover, if the difference between their brightness is not too high, the colors of neighboring pixels may merge. This principle underlies the ClearType anti-aliasing technology: glyphs are drawn in triple horizontal resolution, pixels are equated with sub-pixels and their brightness is determined taking into account 4 neighbors. In fact, colors are blurred with a diameter of 5 subpixels, the classic ratio is 1/2/3/2/1.



    MenuetOS used a tiny bitmap font with a 6x9 glyph resolution. Each pixel is described by one bit, a line byte, 9 bytes for each of 256 glyphs, total font takes 2 kb. It only looks good with a screen resolution of no more than 800x600. KolibriOS repeatedly tried to use vector fonts, but both problems came up against the edge.

    With the exception of the kernel, writing under KOS is not only possible in assembly language. But in order to remain small, fast and not turn into Linux, the main distribution is the image of a 1.4 MB floppy disk. For some this is a psychological barrier, for others it is a banal fence. Vector fonts save size compared to huge raster fonts, but still take up much more than 2 kb. It will be a shame to use a font larger than the kernel with drivers and graphics.

    Also, in any open OS there are problems with drivers for video cards. Nvidia and ATI are also a mafia, they sell hardware, but they don’t explain how to communicate with them in the absence of Windows. Fortunately, there is VESA BIOS Extensions, which allows you to at least set the screen resolution to normal, but to write translucent pixels to video memory is not. So you need to manually read the pixel from there, mix the colors in the right ratio, and write back. The frequency of communication with video memory is high, but there are large delays from the start of transmission to the receipt of the result. Writing is fast, but when reading, you have to wait a long time until the requested pixel is delivered. For example, taking a screenshot in this mode takes more than a second. Since anti-aliasing is based on mixing the pixels in the background with the color of the glyph, then drawing vector fonts on the screen becomes an extremely slow process. Without anti-aliasing, they either require additional data for hinting, or they look worse than raster ones.



    In the best traditions of low-level programming, I asked myself the question “How to get the maximum at the minimum cost?”, And decided to try to squeeze more out of what is. With a simple increase, the symbols consist of squares and right angles (although some Cubism lovers like this style), but if you draw the dots in the right places, then angles of 45 or even 30 degrees will appear. I was inspired by the image smoothing algorithm, optionally presented in this “pixel” game. However, with existing single-pixel-thick fonts, existing implementations do not do very well.
    For example, the result of hqx:



    As a result, I created my own algorithm designed specifically for small raster fonts, and called it Anti Eye Bleeding, because neither more nor less.



    You can follow the evolution of the algorithm on our forum .

    Using general principles, it produces a multiple increase or smoothing (to choose a regular or subpixel one with a precalculated ratio). In the case of smoothing, only a few individual pixels are subject to mixing, which allows achieving acceptable performance. It is written in assembler and integrated into the KolibriOS core as a basic tool. Also, I added an 8x16 font to the kernel for 1400 Unicode characters, size 22 kb (without compression), which is why it got too hot up to 188 kb (92 kb after compression).

    Immediately after enabling system-wide anti-aliasing, one feature surfaced: when drawing text, you must erase the previous one, otherwise the intensity of mixed pixels increases with each redraw. In some programs, "obesity" text can still be found.

    The main problem remains scaling with a fractional factor. I tried to write a library that uses ClearType to reduce to the right size after a multiple increase, but the result remains controversial. However, you can help evaluate it:


    Also popular now: