diff --git a/examples/app.zig b/examples/app.zig index 38815a6d..a841a749 100644 --- a/examples/app.zig +++ b/examples/app.zig @@ -42,7 +42,14 @@ var warn_on_quit_closing: bool = false; // - runs between win.begin()/win.end() pub fn AppInit(win: *dvui.Window) !void { orig_content_scale = win.content_scale; - //try dvui.addFont("NOTO", @embedFile("../src/fonts/NotoSansKR-Regular.ttf"), null); + // Add your own bundled font files...: + // try dvui.addFont("NOTO", @embedFile("../src/fonts/NotoSansKR-Regular.ttf"), null); + + // Or opt-in to adding the fonts for the builin themes... + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); + + // Or add other builtin fonts + try win.fonts.addBuiltinFonts(win.gpa, &.{.Vera}); if (false) { // If you need to set a theme based on the users preferred color scheme, do it here diff --git a/examples/dx11-ontop.zig b/examples/dx11-ontop.zig index 51b9ae42..b402d518 100644 --- a/examples/dx11-ontop.zig +++ b/examples/dx11-ontop.zig @@ -33,6 +33,7 @@ pub fn main() !void { _ = win32.UpdateWindow(wnd); const win: *dvui.Window = backend.getWindow(); + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); log.info("dvui window also init.", .{}); main_loop: while (true) switch (Backend.serviceMessageQueue()) { diff --git a/examples/dx11-standalone.zig b/examples/dx11-standalone.zig index b0edb56c..50713ef3 100644 --- a/examples/dx11-standalone.zig +++ b/examples/dx11-standalone.zig @@ -71,6 +71,7 @@ pub fn main() !void { } const win = first_backend.getWindow(); + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); main_loop: while (true) switch (Backend.serviceMessageQueue()) { .queue_empty => { // beginWait coordinates with waitTime below to run frames only when needed @@ -231,6 +232,8 @@ fn gui_frame() !void { .title = "DVUI DX11 Standalone Example", .icon = window_icon_png, // can also call setIconFromFileContent() }); + errdefer backend.deinit(); + try state.dvui_window.fonts.addBuiltinFontsForTheme(gpa, dvui.Theme.builtin.adwaita_light); extra_windows.appendAssumeCapacity(.{ .state = state, .backend = backend, diff --git a/examples/raylib-ontop.zig b/examples/raylib-ontop.zig index 00c41df8..4e593f13 100644 --- a/examples/raylib-ontop.zig +++ b/examples/raylib-ontop.zig @@ -38,6 +38,7 @@ pub fn main() !void { // OS window is managed by raylib, not dvui var win = try dvui.Window.init(@src(), gpa, backend.backend(), .{}); defer win.deinit(); + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); var selected_color: dvui.Color = dvui.Color.white; diff --git a/examples/raylib-standalone.zig b/examples/raylib-standalone.zig index f661d0f2..170a5231 100644 --- a/examples/raylib-standalone.zig +++ b/examples/raylib-standalone.zig @@ -49,6 +49,7 @@ pub fn main() !void { }, }); defer win.deinit(); + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); main_loop: while (true) { c.BeginDrawing(); diff --git a/examples/sdl-ontop.zig b/examples/sdl-ontop.zig index 3cf111f9..b34bf406 100644 --- a/examples/sdl-ontop.zig +++ b/examples/sdl-ontop.zig @@ -37,6 +37,7 @@ pub fn main() !void { // init dvui Window (maps onto a single OS window) var win = try dvui.Window.init(@src(), gpa, backend.backend(), .{}); defer win.deinit(); + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); main_loop: while (true) { diff --git a/examples/sdl-standalone.zig b/examples/sdl-standalone.zig index 0f44cc47..c5fcf17c 100644 --- a/examples/sdl-standalone.zig +++ b/examples/sdl-standalone.zig @@ -59,6 +59,7 @@ pub fn main() !void { }, }); defer win.deinit(); + try win.fonts.addBuiltinFontsForTheme(win.gpa, dvui.Theme.builtin.adwaita_light); var interrupted = false; diff --git a/examples/web-test.zig b/examples/web-test.zig index d7515a7d..5a23f923 100644 --- a/examples/web-test.zig +++ b/examples/web-test.zig @@ -46,6 +46,8 @@ export fn dvui_init(platform_ptr: [*]const u8, platform_len: usize) i32 { return 2; }; + try WebBackend.win.fonts.addBuiltinFontsForTheme(gpa, dvui.Theme.builtin.adwaita_light); + WebBackend.win_ok = true; orig_content_scale = WebBackend.win.content_scale; diff --git a/src/Examples.zig b/src/Examples.zig index 2424c337..898748bf 100644 --- a/src/Examples.zig +++ b/src/Examples.zig @@ -81,6 +81,9 @@ pub fn demo() void { return; } + // This will include all builtin fonts needed for the builtin themes in the binary + ensureAllRequiredFontsForThemesLoaded(); + const width = 600; var float = dvui.floatingWindow(@src(), .{ .open_flag = &show_demo_window }, .{ .min_size_content = .{ .w = width, .h = 400 }, .max_size_content = .width(width), .tag = demo_window_tag }); @@ -267,6 +270,13 @@ pub fn demo() void { } } +pub fn ensureAllRequiredFontsForThemesLoaded() void { + const cw = dvui.currentWindow(); + inline for (@typeInfo(dvui.Theme.builtin).@"struct".decls) |decl| { + cw.fonts.addBuiltinFontsForTheme(cw.gpa, @field(dvui.Theme.builtin, decl.name)) catch @panic("Could not add builtin fonts"); + } +} + pub fn dialogDirect() void { const data = struct { var extra_stuff: bool = false; diff --git a/src/Font.zig b/src/Font.zig index 30245111..eac5e93d 100644 --- a/src/Font.zig +++ b/src/Font.zig @@ -179,17 +179,16 @@ pub const FontId = enum(u64) { } } - // Ensure that all builtin fonts have a named variant comptime { - const EnumKV = struct { []const u8, FontId }; - const fields = @typeInfo(FontId).@"enum".fields; - var kvs_array: [fields.len]EnumKV = undefined; - for (fields, 0..) |enumField, i| { - kvs_array[i] = .{ enumField.name, @field(FontId, enumField.name) }; - } - const map = std.StaticStringMap(FontId).initComptime(kvs_array); - for (@typeInfo(builtin).@"struct".decls) |decl| { - std.debug.assert(map.get(decl.name) == FontId.fromName(decl.name)); + for (@typeInfo(FontId).@"enum".fields) |field| { + // All named FontIds use the hash of their name as their value + if (field.value != dvui.fnv.hash(field.name)) { + @compileError("The value of '" ++ field.name ++ "' is not the hash of its name"); + } + // All named FontIds reference a builtin font + if (!@hasDecl(builtin, field.name)) { + @compileError("Expected a decl in Font.builtin named '" ++ field.name ++ "'"); + } } } }; @@ -199,27 +198,37 @@ pub const builtin = struct { pub const Aleo = @embedFile("fonts/Aleo/static/Aleo-Regular.ttf"); pub const AleoBd = @embedFile("fonts/Aleo/static/Aleo-Bold.ttf"); pub const Vera = @embedFile("fonts/bitstream-vera/Vera.ttf"); - //pub const VeraBI = @embedFile("fonts/bitstream-vera/VeraBI.ttf"); + pub const VeraBI = @embedFile("fonts/bitstream-vera/VeraBI.ttf"); pub const VeraBd = @embedFile("fonts/bitstream-vera/VeraBd.ttf"); - //pub const VeraIt = @embedFile("fonts/bitstream-vera/VeraIt.ttf"); - //pub const VeraMoBI = @embedFile("fonts/bitstream-vera/VeraMoBI.ttf"); - //pub const VeraMoBd = @embedFile("fonts/bitstream-vera/VeraMoBd.ttf"); - //pub const VeraMoIt = @embedFile("fonts/bitstream-vera/VeraMoIt.ttf"); + pub const VeraIt = @embedFile("fonts/bitstream-vera/VeraIt.ttf"); + pub const VeraMoBI = @embedFile("fonts/bitstream-vera/VeraMoBI.ttf"); + pub const VeraMoBd = @embedFile("fonts/bitstream-vera/VeraMoBd.ttf"); + pub const VeraMoIt = @embedFile("fonts/bitstream-vera/VeraMoIt.ttf"); pub const VeraMono = @embedFile("fonts/bitstream-vera/VeraMono.ttf"); - //pub const VeraSe = @embedFile("fonts/bitstream-vera/VeraSe.ttf"); - //pub const VeraSeBd = @embedFile("fonts/bitstream-vera/VeraSeBd.ttf"); + pub const VeraSe = @embedFile("fonts/bitstream-vera/VeraSe.ttf"); + pub const VeraSeBd = @embedFile("fonts/bitstream-vera/VeraSeBd.ttf"); pub const Pixelify = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Regular.ttf"); - //pub const PixelifyBd = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Bold.ttf"); - //pub const PixelifyMe = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Medium.ttf"); - //pub const PixelifySeBd = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-SemiBold.ttf"); - //pub const Hack = @embedFile("fonts/hack/Hack-Regular.ttf"); - //pub const HackBd = @embedFile("fonts/hack/Hack-Bold.ttf"); - //pub const HackIt = @embedFile("fonts/hack/Hack-Italic.ttf"); - //pub const HackBdIt = @embedFile("fonts/hack/Hack-BoldItalic.ttf"); + pub const PixelifyBd = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Bold.ttf"); + pub const PixelifyMe = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Medium.ttf"); + pub const PixelifySeBd = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-SemiBold.ttf"); + pub const Hack = @embedFile("fonts/hack/Hack-Regular.ttf"); + pub const HackBd = @embedFile("fonts/hack/Hack-Bold.ttf"); + pub const HackIt = @embedFile("fonts/hack/Hack-Italic.ttf"); + pub const HackBdIt = @embedFile("fonts/hack/Hack-BoldItalic.ttf"); pub const OpenDyslexic = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Regular.otf"); pub const OpenDyslexicBd = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold.otf"); - //pub const OpenDyslexicIt = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Italic.otf"); - //pub const OpenDyslexicBdIt = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold-Italic.otf"); + pub const OpenDyslexicIt = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Italic.otf"); + pub const OpenDyslexicBdIt = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold-Italic.otf"); + pub const Noto = @embedFile("fonts/NotoSansKR-Regular.ttf"); + + comptime { + for (@typeInfo(builtin).@"struct".decls) |decl| { + // All builint fonts has a named FontId + if (!@hasField(FontId, decl.name)) { + @compileError("Expected a field in FontId named '" ++ decl.name ++ "'"); + } + } + } }; pub const Cache = struct { @@ -241,23 +250,51 @@ pub const Cache = struct { } }; - pub fn initWithBuiltins(allocator: std.mem.Allocator) std.mem.Allocator.Error!Cache { - var self: Cache = .{}; - inline for (@typeInfo(builtin).@"struct".decls) |decl| { - try self.database.putNoClobber(allocator, .fromName(decl.name), dvui.FontBytesEntry{ - .bytes = @field(builtin, decl.name), - .name = decl.name, + pub fn addBuiltinFonts(self: *Cache, gpa: std.mem.Allocator, comptime named_ids: []const FontId) !void { + inline for (named_ids) |font| { + const name = @tagName(font); // If this line panics, a non-named variant of `FontId` was provided + try self.database.put(gpa, font, .{ + .bytes = @field(builtin, name), + .name = name, .allocator = null, }); } - if (dvui.backend.kind != .web) { - try self.database.putNoClobber(allocator, .Noto, dvui.FontBytesEntry{ - .bytes = @embedFile("fonts/NotoSansKR-Regular.ttf"), - .name = @tagName(FontId.Noto), + } + pub fn addBuiltinFontsForTheme(self: *Cache, gpa: std.mem.Allocator, comptime theme: dvui.Theme) !void { + const named_ids: []const FontId = comptime blk: { + var ids = [_]FontId{ + theme.font_body.id, + theme.font_caption.id, + theme.font_caption_heading.id, + theme.font_heading.id, + theme.font_title.id, + theme.font_title_1.id, + theme.font_title_2.id, + theme.font_title_3.id, + theme.font_title_4.id, + }; + // Filter out duplicates and unnamed `FontId` values + var unique_len: usize = 0; + for (ids) |id| switch (id) { + inline else => |named| { + if (unique_len > 0 and std.mem.lastIndexOfScalar(FontId, ids[0..unique_len], named) != null) continue; + ids[unique_len] = named; + unique_len += 1; + }, + _ => {}, // Unknown/unnamed font + }; + if (unique_len == 0) break :blk &.{}; + break :blk ids[0..unique_len]; + }; + // NOTE: we cannot call `addBuiltinFonts` here because `named_ids` is a slice to a comptime array, so we cannot use it at runtime outside this scope + inline for (named_ids) |font| { + const name = @tagName(font); + try self.database.put(gpa, font, .{ + .bytes = @field(builtin, name), + .name = name, .allocator = null, }); } - return self; } pub fn deinit(self: *Cache, gpa: std.mem.Allocator, backend: Backend) void { diff --git a/src/Window.zig b/src/Window.zig index e769aec9..4a6df213 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -168,8 +168,6 @@ pub fn init( // Set in `begin` .current_parent = undefined, .backend = backend_ctx, - // TODO: Add some way to opt-out of including the builtin fonts in the built binary - .fonts = try .initWithBuiltins(gpa), .theme = if (init_opts.theme) |t| t else switch (init_opts.color_scheme orelse backend_ctx.preferredColorScheme() orelse .light) { .light => Theme.builtin.adwaita_light, .dark => Theme.builtin.adwaita_dark, -- knotmirror 0.0.1 From d181666db99cd77347caf7cebf503c4ee1d3fd8d Mon Sep 17 00:00:00 2001 From: RedPhoenixQ Date: Sun, 16 Nov 2025 20:54:57 +0100 Subject: [PATCH 2/2] web-test: use return code and not "try" Just a small oversight when adding this line to all test --- examples/web-test.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/web-test.zig b/examples/web-test.zig index 5a23f923..eed7faaf 100644 --- a/examples/web-test.zig +++ b/examples/web-test.zig @@ -46,7 +46,9 @@ export fn dvui_init(platform_ptr: [*]const u8, platform_len: usize) i32 { return 2; }; - try WebBackend.win.fonts.addBuiltinFontsForTheme(gpa, dvui.Theme.builtin.adwaita_light); + WebBackend.win.fonts.addBuiltinFontsForTheme(gpa, dvui.Theme.builtin.adwaita_light) catch { + return 3; + }; WebBackend.win_ok = true;