summaryrefslogtreecommitdiff
path: root/src/fonts.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/fonts.zig')
-rw-r--r--src/fonts.zig28
1 files changed, 14 insertions, 14 deletions
diff --git a/src/fonts.zig b/src/fonts.zig
index 505190bd..5f036119 100644
--- a/src/fonts.zig
+++ b/src/fonts.zig
@@ -16,6 +16,7 @@
//! disagree about how to RASTERIZE a file, never about which files there are.
const std = @import("std");
const builtin = @import("builtin");
+/// libc: the tests' scratch fonts, written raw into /tmp.
const libc = std.c;
/// Where each platform keeps fonts. The `home` list is relative to $HOME (a
@@ -152,7 +153,7 @@ fn scan(
// e.path points into the walker's own buffer and dies at the next
// next(), so the path is spelled out here and copied below
const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/{s}", .{ root, e.path }, 0) catch continue;
- if (require_monospaced and !monospaced(path)) continue;
+ if (require_monospaced and !monospaced(io, path)) continue;
found[found_len] = .{
.name = arena.dupe(u8, name) catch break,
.path = arena.dupe(u8, path) catch break,
@@ -237,24 +238,23 @@ test "the picker lists every monospace face rather than the first max_fonts" {
/// latin — the range a terminal is actually worn in — which also (deliberately)
/// keeps the CJK mono faces whose *later* glyphs are double-width, exactly the
/// fonts fontconfig calls "dual-width" and refuses.
-fn monospaced(path_z: [*:0]const u8) bool {
- const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .CLOEXEC = true });
- if (fd < 0) return false;
- defer _ = libc.close(fd);
+fn monospaced(io: std.Io, path: []const u8) bool {
+ const file = std.Io.Dir.cwd().openFile(io, path, .{}) catch return false;
+ defer file.close(io);
// 12-byte header + one 16-byte record per table; 256 records is far more
// than any real font carries
var head: [12 + 16 * 256]u8 = undefined;
- const n = libc.pread(fd, &head, head.len, 0);
+ const n = file.readPositionalAll(io, &head, 0) catch return false;
if (n < 12) return false;
- const hhea = tableOffset(head[0..@intCast(n)], "hhea") orelse return false;
- const hmtx = tableOffset(head[0..@intCast(n)], "hmtx") orelse return false;
+ const hhea = tableOffset(head[0..n], "hhea") orelse return false;
+ const hmtx = tableOffset(head[0..n], "hmtx") orelse return false;
var hh: [36]u8 = undefined;
- if (libc.pread(fd, &hh, hh.len, hhea) != @as(isize, hh.len)) return false;
+ if ((file.readPositionalAll(io, &hh, hhea) catch return false) != hh.len) return false;
const metrics = std.mem.readInt(u16, hh[34..36], .big);
const k: usize = @min(@as(usize, metrics), 64);
if (k == 0) return false;
var mx: [64 * 4]u8 = undefined;
- if (libc.pread(fd, &mx, k * 4, hmtx) != @as(isize, @intCast(k * 4))) return false;
+ if ((file.readPositionalAll(io, mx[0 .. k * 4], hmtx) catch return false) != k * 4) return false;
var ref: u16 = 0;
for (0..k) |i| {
const adv = std.mem.readInt(u16, mx[i * 4 ..][0..2], .big);
@@ -337,12 +337,12 @@ test "monospaced reads the advances out of a real sfnt layout" {
const path = scratchFont(&path_buf, ".ttf");
defer _ = libc.unlink(path);
try writeScratch(path, &f);
- try std.testing.expect(monospaced(path));
+ try std.testing.expect(monospaced(std.testing.io, path));
// ...and one glyph a different width makes it proportional
std.mem.writeInt(u16, f[80 + 4 ..][0..2], 1200, .big);
try writeScratch(path, &f);
- try std.testing.expect(!monospaced(path));
+ try std.testing.expect(!monospaced(std.testing.io, path));
}
test "a ttc collection is read through its first face" {
@@ -371,12 +371,12 @@ test "a ttc collection is read through its first face" {
const path = scratchFont(&path_buf, ".ttc");
defer _ = libc.unlink(path);
try writeScratch(path, &f);
- try std.testing.expect(monospaced(path));
+ try std.testing.expect(monospaced(std.testing.io, path));
// A collection claiming no faces has no first one to read.
std.mem.writeInt(u32, f[8..12], 0, .big);
try writeScratch(path, &f);
- try std.testing.expect(!monospaced(path));
+ try std.testing.expect(!monospaced(std.testing.io, path));
}
test "the walk finds this machine's monospace faces" {