Compare commits

...

2 commits

Author SHA1 Message Date
07d34128dd
use config file as sole param 2024-05-07 23:58:26 +02:00
543610c6d8
add tests 2024-05-07 23:58:04 +02:00
3 changed files with 115 additions and 10 deletions

2
.gitignore vendored
View file

@ -2,4 +2,4 @@ zig-cache
zig-out
*.bdf
out
*.font.zon
*.font.json

View file

@ -134,6 +134,58 @@ fn parseLine(allocator: std.mem.Allocator, line: []const u8) !ParsedCommand {
}
}
test "parseLine: Parses FONTBOUNDINGBOX correctly" {
const allocator = std.testing.allocator;
const line = "FONTBOUNDINGBOX 10 20";
const parsed = try parseLine(allocator, line);
try std.testing.expectEqual(ParsedCommand{ .FONTBOUNDINGBOX = .{ .width = 10, .height = 20 } }, parsed);
}
test "parseLine: Parses ENCODING correctly" {
const allocator = std.testing.allocator;
const line = "ENCODING 123";
const parsed = try parseLine(allocator, line);
try std.testing.expectEqual(ParsedCommand{ .ENCODING = 123 }, parsed);
}
test "parseLine: Parses STARTCHAR correctly" {
const allocator = std.testing.allocator;
const line = "STARTCHAR ABC";
const parsed = try parseLine(allocator, line);
switch (parsed) {
.STARTCHAR => |name| {
try std.testing.expectEqualStrings("ABC", name);
allocator.free(name);
},
else => unreachable,
}
}
test "parseLine: Parses FONT correctly" {
const allocator = std.testing.allocator;
const line = "FONT ABC";
const parsed = try parseLine(allocator, line);
switch (parsed) {
.FONT => |name| {
try std.testing.expectEqualStrings("ABC", name);
allocator.free(name);
},
else => unreachable,
}
}
test "parseLine: Parses BITMAP correctly" {
const allocator = std.testing.allocator;
const line = "BITMAP";
const parsed = try parseLine(allocator, line);
try std.testing.expectEqual(ParsedCommand{ .BITMAP = {} }, parsed);
}
fn readBitmap(
allocator: std.mem.Allocator,
width: u8,
@ -181,3 +233,36 @@ fn readBitmap(
return char;
}
test "readBitmap: Reads bitmap correctly for < 8px wide glyphs" {
const allocator = std.testing.allocator;
var reader = std.io.fixedBufferStream("10\n20\n30\nENDCHAR");
const bitmap = try readBitmap(allocator, 8, 3, reader.reader());
try std.testing.expectEqual(@as(usize, 3), bitmap.len);
try std.testing.expectEqualSlices(u8, &[_]u8{0x10}, bitmap[0]);
try std.testing.expectEqualSlices(u8, &[_]u8{0x20}, bitmap[1]);
try std.testing.expectEqualSlices(u8, &[_]u8{0x30}, bitmap[2]);
for (bitmap) |line| {
allocator.free(line);
}
allocator.free(bitmap);
}
test "readBitmap: Reads bitmap correctly for > 8px wide glyphs" {
const allocator = std.testing.allocator;
var reader = std.io.fixedBufferStream("1234\nABDC\nENDCHAR");
const bitmap = try readBitmap(allocator, 16, 2, reader.reader());
try std.testing.expectEqual(@as(usize, 2), bitmap.len);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0x12, 0x34 }, bitmap[0]);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xAB, 0xDC }, bitmap[1]);
for (bitmap) |line| {
allocator.free(line);
}
allocator.free(bitmap);
}

View file

@ -1,6 +1,11 @@
const std = @import("std");
const bdf = @import("bdf.zig");
const FontConfig = struct {
inputFile: []const u8,
outputFile: []const u8,
};
pub fn main() !void {
// Get allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -11,21 +16,36 @@ pub fn main() !void {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 4) {
std.log.err("Usage: {s} <input.bdf> <input.zon> <output-dir>", .{args[0]});
if (args.len < 2) {
std.log.err("Usage: {s} <config.json>", .{args[0]});
std.process.exit(1);
}
const inputFile = args[1];
const inputConfig = args[2];
_ = inputConfig; // autofix
const outputPath = args[3];
_ = outputPath; // autofix
const inputConfig = args[1];
// Read font
const input = try std.fs.cwd().openFile(inputFile, .{});
// Read config
const configFile = try std.fs.cwd().readFileAlloc(allocator, inputConfig, std.math.maxInt(usize));
defer allocator.free(configFile);
const fontConfig = try readConfig(allocator, configFile);
defer fontConfig.deinit();
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);
}
fn readConfig(allocator: std.mem.Allocator, input: []u8) !std.json.Parsed(FontConfig) {
const fontConfig = try std.json.parseFromSlice(
FontConfig,
allocator,
input,
.{
.ignore_unknown_fields = true,
},
);
return fontConfig;
}