I have this bitmap font (BDF) (https://github.com/lucy/tewi-font) and I need to parse it into arrays of pixels per character, or at least a way to draw a char into a Bitmap image without antialiasing.
Non platform specific classes if possible.
I have this bitmap font (BDF) (https://github.com/lucy/tewi-font) and I need to parse it into arrays of pixels per character, or at least a way to draw a char into a Bitmap image without antialiasing.
Non platform specific classes if possible.
I had the same problem and wrote a small parser: BdfFontParser (Nuget: BdfFontParser). The library parses the bdf file into a BdfFont object with all the properties.
Usage:
With the helper class GetMapOfString
you will get your needed two-dimensional array for one character or a whole string:
using BdfFontParser;
using System.Text;
var font = new BdfFont("fonts/7x13B.bdf");
var map = bdfFont.GetMapOfString("Hello World");
var width = map.GetLength(0);
var height = map.GetLength(1);
var sb = new StringBuilder();
// draw line by line
for (int line = 0; line < height; line++)
{
// iterate through every bit
for (int bit = 0; bit < width; bit++)
{
sb.Append(map[bit,line] ? '#' : ' ');
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
To visualize the a bdf font character and what the parser does the following hexdraw image from this wiki article is helpful:
00 00000000 --------
00 00000000 --------
00 00000000 --------
00 00000000 --------
18 00011000 ---██---
24 00100100 --█--█--
24 00100100 --█--█--
42 01000010 -█----█-
42 01000010 -█----█-
7E 01111110 -██████-
42 01000010 -█----█-
42 01000010 -█----█-
42 01000010 -█----█-
42 01000010 -█----█-
00 00000000 --------
00 00000000 --------