Skip to content

A library for parsing Bits'N'Picas native save format files ('.kbits' and '.kbitx')

License

Notifications You must be signed in to change notification settings

TakWolf/kbitfont-dotnet

Repository files navigation

KbitFont.NET

.NET NuGet

KbitFont is a library for parsing Bits'N'Picas native save format files (.kbits and .kbitx).

Installation

dotnet add package KbitFont

Usage

Create

using KbitSpec;

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

var font = new KbitFont();
font.Props.EmAscent = 14;
font.Props.EmDescent = 2;
font.Props.LineAscent = 14;
font.Props.LineDescent = 2;
font.Props.XHeight = 7;
font.Props.CapHeight = 10;

font.Names.Version = "1.0.0";
font.Names.Family = "My Font";
font.Names.Style = "Regular";
font.Names.Manufacturer = "Pixel Font Studio";
font.Names.Designer = "TakWolf";
font.Names.Description = "A pixel font";
font.Names.Copyright = "Copyright (c) TakWolf";
font.Names.LicenseDescription = "This Font Software is licensed under the SIL Open Font License, Version 1.1";
font.Names.VendorUrl = "https://github.com/TakWolf/kbitfont-dotnet";
font.Names.DesignerUrl = "https://takwolf.com";
font.Names.LicenseUrl = "https://openfontlicense.org";

font.Characters[65] = new KbitGlyph(
    x: 0,
    y: 14,
    advance: 8,
    bitmap: [
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
        [0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00],
        [0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00],
        [0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00],
        [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
        [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
        [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00],
        [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
        [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
        [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
        [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    ]);

font.NamedGlyphs[".notdef"] = new KbitGlyph(
    x: 0,
    y: 14,
    advance: 8,
    bitmap: [
        [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
        [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
    ]);

font.SaveKbits(Path.Combine(outputsDir, "my-font.kbits"));
font.SaveKbitx(Path.Combine(outputsDir, "my-font.kbitx"));

Load Kbits

using KbitSpec;

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

var font = KbitFont.LoadKbits(Path.Combine("assets", "macintosh", "Athens.kbits"));
Console.WriteLine($"name: {font.Names.Family}");
Console.WriteLine($"size: {font.Props.EmHeight}");
Console.WriteLine($"ascent: {font.Props.LineAscent}");
Console.WriteLine($"descent: {font.Props.LineDescent}");
Console.WriteLine();
foreach (var (codePoint, glyph) in font.Characters)
{
    Console.WriteLine($"char: {char.ConvertFromUtf32(codePoint)} ({codePoint:X4})");
    Console.WriteLine($"xy: {(glyph.X, glyph.Y)}");
    Console.WriteLine($"dimensions: {glyph.Dimensions}");
    Console.WriteLine($"advance: {glyph.Advance}");
    foreach (var bitmapRow in glyph.Bitmap)
    {
        var text = string.Join("", bitmapRow.Select(color => color <= 127 ? "  " : "██"));
        Console.WriteLine($"{text}*");
    }
    Console.WriteLine();
}
font.SaveKbits(Path.Combine(outputsDir, "Athens.kbits"));

Load Kbitx

using KbitSpec;

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

var font = KbitFont.LoadKbitx(Path.Combine("assets", "macintosh", "Athens.kbitx"));
Console.WriteLine($"name: {font.Names.Family}");
Console.WriteLine($"size: {font.Props.EmHeight}");
Console.WriteLine($"ascent: {font.Props.LineAscent}");
Console.WriteLine($"descent: {font.Props.LineDescent}");
Console.WriteLine();
foreach (var (codePoint, glyph) in font.Characters)
{
    Console.WriteLine($"char: {char.ConvertFromUtf32(codePoint)} ({codePoint:X4})");
    Console.WriteLine($"xy: {(glyph.X, glyph.Y)}");
    Console.WriteLine($"dimensions: {glyph.Dimensions}");
    Console.WriteLine($"advance: {glyph.Advance}");
    foreach (var bitmapRow in glyph.Bitmap)
    {
        var text = string.Join("", bitmapRow.Select(color => color <= 127 ? "  " : "██"));
        Console.WriteLine($"{text}*");
    }
    Console.WriteLine();
}
font.SaveKbitx(Path.Combine(outputsDir, "Athens.kbitx"));

Specifications

Font Struct

Kbits

Kbitx

License

MIT License

About

A library for parsing Bits'N'Picas native save format files ('.kbits' and '.kbitx')

Topics

Resources

License

Stars

Watchers

Forks

Languages