Categories
Coding Experience Fun

The Art of Scaling Fonts

Hey! Welcome to yet another blog article showing my progress this week, before starting let me remind that for this long week I was on vacation and therefore the progress is very slow, but regardless here’s what’s been done since last blog!

The Art of Scaling Fonts!

If you remember, my last blog where Fonts text height were fixed and text was aligned properly, this week I tried implementing scaling of Fonts (ie character scale) for windows font and fixing bugs in already scaling code for BDFonts!

Let’s start with terminologies and keep it aside!

What is BDF?

Wikipedia says:

The Glyph Bitmap Distribution Format (BDF) by Adobe isĀ a file format for storing bitmap fonts. The content takes the form of a text file intended to be human- and computer-readable. BDF is typically used in Unix X Window environments.

https://en.m.wikipedia.org/wiki/Glyph_Bitmap_Distribution_Format

What is Glyph?

A glyph is a single representation of a character.

In these bitmap fonts, what happens is that a single character (or glyph) is represented by sequence of bytes, now let’s take an example of character A

It is already understood that an image is combination of dots (ie black and whites), what we see as a 2D figure is actually a matrix with filled 0s and 1s, similarly the pictorial version of these characters (ie how A looks on scteen) is given by a matrix that will have 1 in places where a black spot appears.. (when seen as image this matrix will be seen as how we understand character A should look like :)!

How this matrix information is packed into text?

Let’s assume we have this character A which is 12 bits in width and 16 bits in height,

Now if you understand basic data types or logic gates, you can already deduce that it takes 1 byte to pack 8 bits, or here in this case 2 bytes to pack 16 bits

Also since height of this glyph is 16 bits, therefore it takes 16 x 2 or 32 bytes to represent full character matrix!

 

That is all in the basics of Fonts, if you understood it all, unfortunately I still cannot award you the prestigious certificate of completion in basic font terminologies as the real deal has lot more technical jargon like offsets, ascent, descent etc etc, but for our case only understand these bits will do!

So what is font scaling?

It is, as name sounds scaling each character to some size and thus using it, a good scaling is one in which the texts using font is still readable (and crisp), however in lot of cases it is difficult to moderate how good we scale.. Here at scummvm currently we implement a primitive scaler that works like:

1. Take each of the size parameters ie width, height etc and multiply it with scale (ie newsize/oldsize) round it off and that will be our new size!

2. For glphy scaling (ie the actual image part), what we do is to draw the character on empty surface and scale the surface upwards, finally we trace the scaled screen and convert it into bytes form!

3. This new object (ie scaled font) is returned to be used as per demand!

Fixing bugs in BDF Font Scaling!

Now bdf font scaler was implemented by a student themselves, and it was kind of not finished or let’s say not furnished and was giving crashes and overflows! So as usual I started looking into it and after some time found the source of error! Finally fixing it I was able to run the program with scaledFont..

But it doesn’t end here:

While debugging the already written code, I found lot of issues with gui, like different text preview with different size, artifacts in certain glphys and many other..

After spending some time finding and fixing, bdf scaler was fixed upto certain extent (however would like to mention that not still some minor issues persisted, which are still being looked through)

Link to accepted PR: #5125

Implementing Windows Font Scaling

Related to scaling of Fonts, I was asked to implement windows font scaler, these are in formats of FOND and FNT, etc with their own specifications etc, as usual I read up some old documentations and archives information about fonts and went to implement it just like how BDF Scaler was implemented! A day later it was merged and yay, there we have our windows font scaling implemented, now windows games using fonts of different size will be rendered!

Link to merged PR: #5126

The End

Thank you for reading so far, this week blog, I apologize for the delay for this weeks progress and as mentioned I will be fully active and working from next week so look forward for the next release!

Leave a Reply

Your email address will not be published. Required fields are marked *