fontconfig example

This commit is contained in:
Mitchell Hashimoto
2022-04-03 20:08:29 -07:00
parent 529a2c924c
commit 3cb5dae9d8
4 changed files with 46 additions and 0 deletions

View File

@ -21,6 +21,7 @@ pub fn build(b: *std.build.Builder) !void {
ftlib.link(exe); ftlib.link(exe);
exe.linkSystemLibrary("epoxy"); exe.linkSystemLibrary("epoxy");
exe.linkSystemLibrary("fontconfig");
const run_cmd = exe.run(); const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());

View File

@ -8,6 +8,7 @@
, vttest , vttest
, zig , zig
, fontconfig
, libepoxy , libepoxy
, libGL , libGL
, libX11 , libX11
@ -32,6 +33,7 @@
buildInputs = [ buildInputs = [
# TODO: non-linux # TODO: non-linux
] ++ lib.optionals stdenv.isLinux [ ] ++ lib.optionals stdenv.isLinux [
fontconfig
libepoxy libepoxy
libGL libGL

39
src/fonts.zig Normal file
View File

@ -0,0 +1,39 @@
const std = @import("std");
const fc = @cImport({
@cInclude("fontconfig/fontconfig.h");
});
// Set to true when FontConfig is initialized.
var initialized: bool = false;
pub fn list() !void {
if (!initialized) {
if (fc.FcInit() != fc.FcTrue) {
return error.InitializionFailed;
}
}
const pat = fc.FcPatternCreate();
defer fc.FcPatternDestroy(pat);
const key = fc.FC_FULLNAME;
const os = fc.FcObjectSetBuild(
key,
@as([*c]const u8, 0), // @as required Zig #1481
);
defer fc.FcObjectSetDestroy(os);
const fs = fc.FcFontList(null, pat, os);
defer fc.FcFontSetDestroy(fs);
var i: usize = 0;
while (i <= fs.*.nfont) : (i += 1) {
const fpat = fs.*.fonts[i];
var str: [*c]fc.FcChar8 = undefined;
if (fc.FcPatternGetString(fpat, key, 0, &str) == fc.FcResultMatch) {
std.log.info("FONT: {s}", .{
@ptrCast([*:0]u8, str),
});
}
}
}

View File

@ -2,8 +2,12 @@ const std = @import("std");
const glfw = @import("glfw"); const glfw = @import("glfw");
const gl = @import("opengl.zig"); const gl = @import("opengl.zig");
const stb = @import("stb.zig"); const stb = @import("stb.zig");
const fonts = @import("fonts.zig");
pub fn main() !void { pub fn main() !void {
// List our fonts
try fonts.list();
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();