Compare commits

..

No commits in common. "65c0b9445feca8c60eaa527dab99d56640c6026c" and "073b4cba418c29018b4ceff7ed8debebdf469d4f" have entirely different histories.

6 changed files with 122 additions and 145 deletions

View file

@ -1,17 +1,9 @@
# Absolutely Worthless Binfon
Takes all your precious fonts and puts them in BINs!
Takes all your precious fonts and puts them in the BIN!
Allows embedding of bitmap fonts into easily embeddable binary blobs.
## Building
`zig build` builds two targets:
- `binfon` is the actual executable that runs the bdf to bin transformation (see Usage below)
- `binfon-viewer` takes a .bin and prints its glyphs to console
## Usage
The inputs must be a .bdf file and a .json configuration file. The JSON configuration file must be formatted as follows:
```json

View file

@ -15,7 +15,6 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
// Main executable
const exe = b.addExecutable(.{
.name = "binfon",
.root_source_file = b.path("src/main.zig"),
@ -32,6 +31,7 @@ pub fn build(b: *std.Build) void {
run_step.dependOn(&run_cmd.step);
// Viewer debug app
const viewer = b.addExecutable(.{
.name = "binfon-viewer",
.root_source_file = b.path("src/view.zig"),
@ -51,65 +51,4 @@ pub fn build(b: *std.Build) void {
run_cmd.addArgs(args);
view_cmd.addArgs(args);
}
// Tests
const bdf_unit_tests = b.addTest(.{
.root_source_file = b.path("src/bdf.zig"),
.target = target,
.optimize = optimize,
});
const run_bdf_unit_tests = b.addRunArtifact(bdf_unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_bdf_unit_tests.step);
// Module
_ = b.addModule("binfon", .{
.root_source_file = .{ .path = "src/lib.zig" },
});
// Library
const libbinfon = buildLibrary(b, .{
.target = target,
.optimize = optimize,
});
const libzimalloc_install = b.addInstallArtifact(libbinfon, .{});
b.getInstallStep().dependOn(&libzimalloc_install.step);
}
const ModuleOptions = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
linkage: std.builtin.LinkMode = .dynamic,
pic: ?bool = null,
};
fn buildLibrary(b: *std.Build, options: ModuleOptions) *std.Build.Step.Compile {
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 };
const library = switch (options.linkage) {
.dynamic => b.addSharedLibrary(.{
.name = "binfon",
.root_source_file = .{ .path = "src/lib.zig" },
.version = version,
.target = options.target,
.optimize = options.optimize,
.pic = options.pic,
}),
.static => b.addStaticLibrary(.{
.name = "binfon",
.root_source_file = .{ .path = "src/lib.zig" },
.version = version,
.target = options.target,
.optimize = options.optimize,
.pic = options.pic,
}),
};
return library;
}

View file

@ -1,12 +1,67 @@
.{
.name = "binfon",
.version = "0.1.0",
.minimum_zig_version = "0.12.0",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
// // `hash`, otherwise you are communicating that you expect to find the old hash at
// // the new URL.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",
// // When this is set to `true`, a package is declared to be lazily
// // fetched. This makes the dependency only get fetched if it is
// // actually used.
// .lazy = false,
//},
},
// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package.
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"LICENSE",
"README.md",
// This makes *all* files, recursively, included in this package. It is generally
// better to explicitly list the files and directories instead, to insure that
// fetching from tarballs, file system paths, and version control all result
// in the same contents hash.
"",
// For example...
//"build.zig",
//"build.zig.zon",
//"src",
//"LICENSE",
//"README.md",
},
}

View file

@ -30,8 +30,27 @@ pub const Font = struct {
allocator.free(self.name);
self.characters.deinit();
}
};
pub fn parse(allocator: std.mem.Allocator, bdf: anytype) !Font {
const Command = enum {
FONT,
FONTBOUNDINGBOX,
ENCODING,
BITMAP,
STARTCHAR,
_IGNORED,
};
const ParsedCommand = union(Command) {
FONT: []const u8,
FONTBOUNDINGBOX: struct { width: u8, height: u8 },
ENCODING: u16,
BITMAP: void,
STARTCHAR: []const u8,
_IGNORED: void,
};
pub fn parse(allocator: std.mem.Allocator, bdf: anytype) !Font {
var font: Font = .{
.name = undefined,
.height = 0,
@ -81,26 +100,7 @@ pub const Font = struct {
}
return font;
}
};
const Command = enum {
FONT,
FONTBOUNDINGBOX,
ENCODING,
BITMAP,
STARTCHAR,
_IGNORED,
};
const ParsedCommand = union(Command) {
FONT: []const u8,
FONTBOUNDINGBOX: struct { width: u8, height: u8 },
ENCODING: u16,
BITMAP: void,
STARTCHAR: []const u8,
_IGNORED: void,
};
}
fn parseLine(allocator: std.mem.Allocator, line: []const u8) !ParsedCommand {
const firstSpace = std.mem.indexOfScalar(u8, line, ' ') orelse line.len;

View file

@ -1,13 +0,0 @@
const std = @import("std");
const bdf = @import("bdf.zig");
const bin = @import("bin.zig");
pub const Font = bdf.Font;
pub const writeGlyphs = bin.writeGlyphs;
pub fn convert(allocator: std.mem.Allocator, input: anytype, output: anytype, glyphs: []const u16) !void {
var font = try bdf.Font.parse(allocator, input);
defer font.deinit(allocator);
try bin.writeGlyphs(output, font, glyphs);
}

View file

@ -1,5 +1,6 @@
const std = @import("std");
const lib = @import("lib.zig");
const bdf = @import("bdf.zig");
const bin = @import("bin.zig");
const FontConfig = struct {
inputFile: []const u8,
@ -35,11 +36,14 @@ pub fn main() !void {
const input = try std.fs.cwd().openFile(fontConfig.value.inputFile, .{});
defer input.close();
var font = try bdf.parse(allocator, input.reader());
defer font.deinit(allocator);
// Write output
const output = try std.fs.cwd().createFile(fontConfig.value.outputFile, .{});
defer output.close();
try lib.convert(allocator, input.reader(), output.writer(), fontConfig.value.glyphs);
try bin.writeGlyphs(output.writer(), font, fontConfig.value.glyphs);
}
fn readConfig(allocator: std.mem.Allocator, input: []u8) !std.json.Parsed(FontConfig) {