diff --git a/sdl-test.zig b/sdl-test.zig index aa8dbd43..02a778b0 100644 --- a/sdl-test.zig +++ b/sdl-test.zig @@ -497,7 +497,7 @@ pub fn main() !void { defer fwin.deinit(); try dvui.labelNoFmt(@src(), "Floating Window", .{ .gravity_x = 0.5, .gravity_y = 0.5 }); - try dvui.label(@src(), "Pretty Cool", .{}, .{ .font = .{ .name = "VeraMono", .ttf_bytes_id = dvui.Font.TTFBytesId.VeraMono, .size = 20 } }); + try dvui.label(@src(), "Pretty Cool", .{}, .{ .font = .{ .name = "VeraMono", .ttf_bytes_id = "VeraMono", .size = 20 } }); if (try dvui.button(@src(), "button", .{}, .{})) { floats[0] = true; diff --git a/src/Font.zig b/src/Font.zig index a3d8d362..62d6ef8e 100644 --- a/src/Font.zig +++ b/src/Font.zig @@ -9,8 +9,7 @@ const Font = @This(); size: f32, line_height_factor: f32 = 1.0, name: []const u8, -ttf_bytes_id: TTFBytesId, -//ttf_bytes: []const u8, +ttf_bytes_id: []const u8, pub fn resize(self: *const Font, s: f32) Font { return Font{ .size = s, .line_height_factor = self.line_height_factor, .name = self.name, .ttf_bytes_id = self.ttf_bytes_id }; @@ -85,7 +84,9 @@ pub const bitstream_vera = @import("fonts/bitstream_vera.zig"); pub const pixelify_sans = @import("fonts/pixelify-sans.zig"); pub const hack = @import("fonts/hack.zig"); -pub const FontBytes = struct { +pub const default_ttf_bytes = bitstream_vera.Vera; + +pub const TTFBytes = struct { pub const Vera = bitstream_vera.Vera; pub const VeraBd = bitstream_vera.VeraBd; pub const VeraBI = bitstream_vera.VeraBI; @@ -101,13 +102,10 @@ pub const FontBytes = struct { pub const HackBdIt = hack.HackBdIt; }; -pub const TTFBytesId = std.meta.DeclEnum(FontBytes); - -pub fn getFontBytes(ttf_bytes_id: TTFBytesId) []const u8 { - inline for (@typeInfo(FontBytes).Struct.decls, 0..) |decl, i| { - if (@intFromEnum(ttf_bytes_id) == i) { - return @field(FontBytes, decl.name); - } +pub fn initTTFBytesDatabase(allocator: std.mem.Allocator) !std.StringHashMap([]const u8) { + var result = std.StringHashMap([]const u8).init(allocator); + inline for (@typeInfo(TTFBytes).Struct.decls) |decl| { + try result.put(decl.name, @field(TTFBytes, decl.name)); } - unreachable; + return result; } diff --git a/src/dvui.zig b/src/dvui.zig index 88a9fc51..321b11b6 100644 --- a/src/dvui.zig +++ b/src/dvui.zig @@ -343,10 +343,14 @@ const FontCacheEntry = struct { pub fn hash(font: Font) u32 { var h = fnv.init(); - const bytes = Font.getFontBytes(font.ttf_bytes_id); - h.update(std.mem.asBytes(&bytes.ptr)); - h.update(std.mem.asBytes(&font.size)); - return h.final(); + if (current_window) |win| { + const bytes = win.ttf_bytes_database.get(font.ttf_bytes_id) orelse Font.default_ttf_bytes; + h.update(std.mem.asBytes(&bytes.ptr)); + h.update(std.mem.asBytes(&font.size)); + return h.final(); + } else { + @panic("Current window not initialized"); + } } pub fn glyphInfoGet(self: *FontCacheEntry, codepoint: u32, font_name: []const u8) !GlyphInfo { @@ -490,7 +494,8 @@ pub fn fontCacheGet(font: Font) !*FontCacheEntry { } //ttf bytes - const bytes = Font.getFontBytes(font.ttf_bytes_id); + if (current_window == null) @panic("Window is null"); + const bytes = current_window.?.ttf_bytes_database.get(font.ttf_bytes_id) orelse Font.default_ttf_bytes; log.debug("FontCacheGet creating font hash {x} ptr {*} size {d} name \"{s}\"", .{ fontHash, bytes.ptr, font.size, font.name }); var entry: FontCacheEntry = undefined; @@ -2081,6 +2086,7 @@ pub const Window = struct { tab_index_prev: std.ArrayList(TabIndex), tab_index: std.ArrayList(TabIndex), font_cache: std.AutoHashMap(u32, FontCacheEntry), + ttf_bytes_database: std.StringHashMap([]const u8), texture_cache: std.AutoHashMap(u32, TextureCacheEntry), dialog_mutex: std.Thread.Mutex, dialogs: std.ArrayList(Dialog), @@ -2146,6 +2152,7 @@ pub const Window = struct { .debug_refresh_mutex = std.Thread.Mutex{}, .wd = WidgetData{ .src = src, .id = hashval, .init_options = .{ .subwindow = true }, .options = .{ .name = "Window" } }, .backend = backend, + .ttf_bytes_database = try Font.initTTFBytesDatabase(gpa), }; const winSize = self.backend.windowSize(); @@ -2204,6 +2211,7 @@ pub const Window = struct { self.dialogs.deinit(); self.toasts.deinit(); self._arena.deinit(); + self.ttf_bytes_database.deinit(); } // called from any thread diff --git a/src/themes/Adwaita.zig b/src/themes/Adwaita.zig index 569fbe7e..fd3d3559 100644 --- a/src/themes/Adwaita.zig +++ b/src/themes/Adwaita.zig @@ -4,8 +4,8 @@ const Color = dvui.Color; const Font = dvui.Font; const Theme = dvui.Theme; const Options = dvui.Options; -const Vera = Font.TTFBytesId.Vera; -const VeraBd = Font.TTFBytesId.VeraBd; +const Vera = "Vera"; +const VeraBd = "VeraBd"; const accent = Color{ .r = 0x35, .g = 0x84, .b = 0xe4 }; const accent_hsl = Color.HSLuv.fromColor(accent); diff --git a/src/themes/Dracula.zig b/src/themes/Dracula.zig index 8b9de4c3..d0fecdaf 100644 --- a/src/themes/Dracula.zig +++ b/src/themes/Dracula.zig @@ -4,8 +4,8 @@ const Color = dvui.Color; const Font = dvui.Font; const Theme = dvui.Theme; const Options = dvui.Options; -const Hack = Font.TTFBytesId.Hack; -const HackBd = Font.TTFBytesId.HackBd; +const Hack = "Hack"; +const HackBd = "HackBd"; const accent = Color{ .r = 0xff, .g = 0x79, .b = 0xc6, .a = 0xff }; const err = Color{ .r = 0xf8, .g = 0xf8, .b = 0xf2, .a = 0xff }; // color7 diff --git a/src/themes/Flow.zig b/src/themes/Flow.zig index d356f155..f273edbf 100644 --- a/src/themes/Flow.zig +++ b/src/themes/Flow.zig @@ -4,8 +4,8 @@ const Color = dvui.Color; const Font = dvui.Font; const Theme = dvui.Theme; const Options = dvui.Options; -const Vera = Font.TTFBytesId.Vera; -const VeraBd = Font.TTFBytesId.VeraBd; +const Vera = "Vera"; +const VeraBd = "VeraBd"; //Colors inspired by the flow neovim theme const accent = Color{ .r = 0xff, .g = 0x33, .b = 0x99, .a = 0xff }; // colors.fluo.pink diff --git a/src/themes/Gruvbox.zig b/src/themes/Gruvbox.zig index 8f7cfb6b..a533e517 100644 --- a/src/themes/Gruvbox.zig +++ b/src/themes/Gruvbox.zig @@ -4,8 +4,8 @@ const Color = dvui.Color; const Font = dvui.Font; const Theme = dvui.Theme; const Options = dvui.Options; -const Vera = Font.TTFBytesId.Vera; -const VeraBd = Font.TTFBytesId.VeraBd; +const Vera = "Vera"; +const VeraBd = "VeraBd"; const accent = Color{ .r = 0xfe, .g = 0x80, .b = 0x19, .a = 0xff }; // s:gb.bright_orange const err = Color{ .r = 0xcc, .g = 0x24, .b = 0x1d, .a = 0xff }; // s:gb.neutral_red diff --git a/src/themes/Jungle.zig b/src/themes/Jungle.zig index 7ca7d986..f20b7826 100644 --- a/src/themes/Jungle.zig +++ b/src/themes/Jungle.zig @@ -4,7 +4,7 @@ const Color = dvui.Color; const Font = dvui.Font; const Theme = dvui.Theme; const Options = dvui.Options; -const pixelify = Font.TTFBytesId.Pixelify; +const pixelify = "Pixelify"; const accent = Color{ .r = 0x63, .g = 0x84, .b = 0x65, .a = 0xff }; const err = Color.white; diff --git a/web-test.zig b/web-test.zig index a67a2216..95b90407 100644 --- a/web-test.zig +++ b/web-test.zig @@ -46,8 +46,8 @@ var orig_content_scale: f32 = 1.0; const zig_favicon = @embedFile("src/zig-favicon.png"); export fn app_init() i32 { - dvui.Adwaita.light = dvui.Adwaita.light.fontSizeAdd(2); - dvui.Adwaita.dark = dvui.Adwaita.dark.fontSizeAdd(2); + dvui.Theme.AdwaitaLight = dvui.Theme.AdwaitaLight.fontSizeAdd(2); + dvui.Theme.AdwaitaDark = dvui.Theme.AdwaitaDark.fontSizeAdd(2); backend = WebBackend.init() catch { return 1; @@ -219,22 +219,26 @@ fn dvui_frame() !void { { const Data = struct { - var theme_choice: usize = 1; + var theme_choice: usize = 0; }; + const entries = [_][]const u8{ "Adwaita Light", "Adwaita Dark", "Jungle", "Dracula", + "Flow", + "Gruvbox", }; const themes = [_]*dvui.Theme{ - &dvui.Adwaita.light, - &dvui.Adwaita.dark, - &dvui.Jungle.jungle, - &dvui.Dracula.dracula, + &dvui.Theme.AdwaitaLight, + &dvui.Theme.AdwaitaDark, + &dvui.Theme.Jungle, + &dvui.Theme.Dracula, + &dvui.Theme.Flow, + &dvui.Theme.Gruvbox, }; - _ = try dvui.dropdown(@src(), &entries, &Data.theme_choice, .{ .min_size_content = .{ .w = 120 }, .id_extra = 1 }); if (dvui.themeGet() != themes[Data.theme_choice]) {