Files
sx/examples/1626-vendor-stb-truetype-metrics.sx
agra 58af806b7a library: vendors/stb_image + vendors/stb_truetype — stb ships with sx
The stb headers move from the repo-root vendors/ (resolvable only
with CWD = sx repo) into library/vendors/ following the sqlite
convention — bindings module + c/ sources + provenance README — so
'#import "vendors/stb_image/stb_image.sx"' (image v2.30 + image_write
v1.16) and '#import "vendors/stb_truetype/stb_truetype.sx"' (v1.26)
work from any consumer via the stdlib search paths. modules/ffi/stb.sx
dissolves into the stb_image vendor; modules/ffi/stb_truetype.sx keeps
its non-stb text-shaping companions and re-imports the vendored unit.
examples/1625 pins a deterministic in-memory BMP decode; examples/1626
pins font init + metric invariants against the system Helvetica.
2026-06-12 17:50:21 +03:00

37 lines
1.3 KiB
Plaintext

// The sx library ships stb_truetype: `#import "vendors/stb_truetype/
// stb_truetype.sx"` resolves through the stdlib search paths and the
// implementation compiles through the object cache. Loads the system
// Helvetica collection and pins INVARIANTS only (font versions vary
// across macOS releases): init succeeds, the pixel-height scale is
// positive, the ascender is positive and the descender negative.
#import "modules/std.sx";
fs :: #import "modules/std/fs.sx";
tt :: #import "vendors/stb_truetype/stb_truetype.sx";
main :: () -> i32 {
data := fs.read_file("/System/Library/Fonts/Helvetica.ttc");
if data == null {
print("font missing\n");
return 1;
}
bytes := data!;
off := tt.stbtt_GetFontOffsetForIndex(bytes.ptr, 0);
print("font offset >= 0: {}\n", off >= 0);
info : *void = xx context.allocator.alloc_bytes(256);
ok := tt.stbtt_InitFont(info, bytes.ptr, off);
print("init ok: {}\n", ok != 0);
px : f32 = 32.0;
scale := tt.stbtt_ScaleForPixelHeight(info, px);
print("scale > 0: {}\n", scale > 0.0);
ascent : i32 = 0;
descent : i32 = 0;
linegap : i32 = 0;
tt.stbtt_GetFontVMetrics(info, @ascent, @descent, @linegap);
print("ascent > 0, descent < 0: {} {}\n", ascent > 0, descent < 0);
0
}