Skip to content

TakWolf/bdffont-dotnet

Repository files navigation

BdfFont.NET

.NET NuGet

BdfFont is a library for manipulating Glyph Bitmap Distribution Format (BDF) Fonts.

Installation

dotnet add package BdfFont

Usage

Create

using BdfSpec;

var outputsDir = Path.Combine("build");
if (Directory.Exists(outputsDir))
{
    Directory.Delete(outputsDir, true);
}
Directory.CreateDirectory(outputsDir);

var font = new BdfFont(
    pointSize: 16,
    resolution: (75, 75),
    boundingBox: (16, 16, 0, -2));

font.Glyphs.Add(new BdfGlyph(
    name: ".notdef",
    encoding: -1,
    scalableWidth: (500, 0),
    deviceWidth: (8, 0),
    boundingBox: (8, 16, 0, -2),
    bitmap: [
        [1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1]
    ]));

font.Glyphs.Add(new BdfGlyph(
    name: "CAP_LETTER_A",
    encoding: 65,
    scalableWidth: (500, 0),
    deviceWidth: (8, 0),
    boundingBox: (8, 16, 0, -2),
    bitmap: [
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 1, 0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0, 1, 0, 0],
        [0, 1, 0, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0, 0, 1, 0],
        [0, 1, 1, 1, 1, 1, 1, 0],
        [0, 1, 0, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0]
    ]));

font.Properties.Foundry = "Pixel Font Studio";
font.Properties.FamilyName = "My Font";
font.Properties.WeightName = "Medium";
font.Properties.Slant = "R";
font.Properties.SetWidthName = "Normal";
font.Properties.AddStyleName = "Sans Serif";
font.Properties.PixelSize = font.PointSize;
font.Properties.PointSize = font.PointSize * 10;
font.Properties.ResolutionX = font.ResolutionX;
font.Properties.ResolutionY = font.ResolutionY;
font.Properties.Spacing = "P";
font.Properties.AverageWidth = Convert.ToInt32(font.Glyphs.Average(glyph => glyph.DeviceWidthX * 10));
font.Properties.CharsetRegistry = "ISO10646";
font.Properties.CharsetEncoding = "1";
font.GenerateNameAsXlfd();

font.Properties.DefaultChar = -1;
font.Properties.FontAscent = 14;
font.Properties.FontDescent = 2;
font.Properties.XHeight = 7;
font.Properties.CapHeight = 10;
font.Properties.UnderlinePosition = -2;
font.Properties.UnderlineThickness = 1;

font.Properties.FontVersion = "1.0.0";
font.Properties.Copyright = "Copyright (c) TakWolf";

font.Save(Path.Combine(outputsDir, "my-font.bdf"));

Load

using BdfSpec;

var outputsDir = Path.Combine("build");
if (Directory.Exists(outputsDir))
{
    Directory.Delete(outputsDir, true);
}
Directory.CreateDirectory(outputsDir);

var font = BdfFont.Load(Path.Combine("assets", "unifont", "unifont-17.0.04.bdf"));
Console.WriteLine($"name: {font.Name}");
Console.WriteLine($"size: {font.PointSize}");
Console.WriteLine($"ascent: {font.Properties.FontAscent}");
Console.WriteLine($"descent: {font.Properties.FontDescent}");
Console.WriteLine();
foreach (var glyph in font.Glyphs)
{
    Console.WriteLine($"char: {char.ConvertFromUtf32(glyph.Encoding)} ({glyph.Encoding:X4})");
    Console.WriteLine($"glyphName: {glyph.Name}");
    Console.WriteLine($"advanceWidth: {glyph.DeviceWidthX}");
    Console.WriteLine($"dimensions: {glyph.Dimensions}");
    Console.WriteLine($"offset: {glyph.Offset}");
    foreach (var bitmapRow in glyph.Bitmap)
    {
        var text = string.Join("", bitmapRow).Replace("0", "  ").Replace("1", "██");
        Console.WriteLine($"{text}*");
    }
    Console.WriteLine();
}
font.Save(Path.Combine(outputsDir, "unifont-17.0.04.bdf"));

References

License

MIT License

About

A library for manipulating Glyph Bitmap Distribution Format (BDF) Fonts

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages