font-family settings are repeatable to specify fallback

This commit is contained in:
Mitchell Hashimoto
2024-01-03 09:30:12 -08:00
parent 98237b112f
commit b4f403f152
3 changed files with 25 additions and 14 deletions

View File

@ -342,7 +342,7 @@ pub fn init(
// A buffer we use to store the font names for logging. // A buffer we use to store the font names for logging.
var name_buf: [256]u8 = undefined; var name_buf: [256]u8 = undefined;
if (config.@"font-family") |family| { for (config.@"font-family".list.items) |family| {
var disco_it = try disco.discover(alloc, .{ var disco_it = try disco.discover(alloc, .{
.family = family, .family = family,
.style = config.@"font-style".nameValue(), .style = config.@"font-style".nameValue(),
@ -364,7 +364,7 @@ pub fn init(
// a user says `font-style = italic` for the bold face for example, // a user says `font-style = italic` for the bold face for example,
// no results would be found if we restrict to ALSO searching for // no results would be found if we restrict to ALSO searching for
// italic. // italic.
if (config.@"font-family-bold") |family| { for (config.@"font-family-bold".list.items) |family| {
const style = config.@"font-style-bold".nameValue(); const style = config.@"font-style-bold".nameValue();
var disco_it = try disco.discover(alloc, .{ var disco_it = try disco.discover(alloc, .{
.family = family, .family = family,
@ -379,7 +379,7 @@ pub fn init(
_ = try group.addFace(.bold, .{ .deferred = face }); _ = try group.addFace(.bold, .{ .deferred = face });
} else log.warn("font-family-bold not found: {s}", .{family}); } else log.warn("font-family-bold not found: {s}", .{family});
} }
if (config.@"font-family-italic") |family| { for (config.@"font-family-italic".list.items) |family| {
const style = config.@"font-style-italic".nameValue(); const style = config.@"font-style-italic".nameValue();
var disco_it = try disco.discover(alloc, .{ var disco_it = try disco.discover(alloc, .{
.family = family, .family = family,
@ -394,7 +394,7 @@ pub fn init(
_ = try group.addFace(.italic, .{ .deferred = face }); _ = try group.addFace(.italic, .{ .deferred = face });
} else log.warn("font-family-italic not found: {s}", .{family}); } else log.warn("font-family-italic not found: {s}", .{family});
} }
if (config.@"font-family-bold-italic") |family| { for (config.@"font-family-bold-italic".list.items) |family| {
const style = config.@"font-style-bold-italic".nameValue(); const style = config.@"font-style-bold-italic".nameValue();
var disco_it = try disco.discover(alloc, .{ var disco_it = try disco.discover(alloc, .{
.family = family, .family = family,

View File

@ -29,15 +29,21 @@ const c = @cImport({
}); });
/// The font families to use. /// The font families to use.
///
/// You can generate the list of valid values using the CLI: /// You can generate the list of valid values using the CLI:
/// path/to/ghostty/cli +list-fonts /// path/to/ghostty/cli +list-fonts
/// ///
/// This configuration can be repeated multiple times to specify
/// preferred fallback fonts when the requested codepoint is not
/// available in the primary font. This is particularly useful for
/// multiple languages, symbolic fonts, etc.
///
/// Changing this configuration at runtime will only affect new terminals, /// Changing this configuration at runtime will only affect new terminals,
/// i.e. new windows, tabs, etc. /// i.e. new windows, tabs, etc.
@"font-family": ?[:0]const u8 = null, @"font-family": RepeatableString = .{},
@"font-family-bold": ?[:0]const u8 = null, @"font-family-bold": RepeatableString = .{},
@"font-family-italic": ?[:0]const u8 = null, @"font-family-italic": RepeatableString = .{},
@"font-family-bold-italic": ?[:0]const u8 = null, @"font-family-bold-italic": RepeatableString = .{},
/// The named font style to use for each of the requested terminal font /// The named font style to use for each of the requested terminal font
/// styles. This looks up the style based on the font style string advertised /// styles. This looks up the style based on the font style string advertised
@ -1623,15 +1629,15 @@ pub fn finalize(self: *Config) !void {
// the others to the font family. This way, if someone does // the others to the font family. This way, if someone does
// --font-family=foo, then we try to get the stylized versions of // --font-family=foo, then we try to get the stylized versions of
// "foo" as well. // "foo" as well.
if (self.@"font-family") |family| { if (self.@"font-family".count() > 0) {
const fields = &[_][]const u8{ const fields = &[_][]const u8{
"font-family-bold", "font-family-bold",
"font-family-italic", "font-family-italic",
"font-family-bold-italic", "font-family-bold-italic",
}; };
inline for (fields) |field| { inline for (fields) |field| {
if (@field(self, field) == null) { if (@field(self, field).count() == 0) {
@field(self, field) = family; @field(self, field) = try self.@"font-family".clone(alloc);
} }
} }
} }
@ -1984,9 +1990,9 @@ test "changed" {
defer source.deinit(); defer source.deinit();
var dest = try source.clone(alloc); var dest = try source.clone(alloc);
defer dest.deinit(); defer dest.deinit();
dest.@"font-family" = "something else"; dest.@"font-thicken" = true;
try testing.expect(source.changed(&dest, .@"font-family")); try testing.expect(source.changed(&dest, .@"font-thicken"));
try testing.expect(!source.changed(&dest, .@"font-size")); try testing.expect(!source.changed(&dest, .@"font-size"));
} }
@ -2218,6 +2224,11 @@ pub const RepeatableString = struct {
}; };
} }
/// The number of itemsin the list
pub fn count(self: Self) usize {
return self.list.items.len;
}
/// Compare if two of our value are requal. Required by Config. /// Compare if two of our value are requal. Required by Config.
pub fn equal(self: Self, other: Self) bool { pub fn equal(self: Self, other: Self) bool {
const itemsA = self.list.items; const itemsA = self.list.items;

View File

@ -50,6 +50,6 @@ pub fn Value(comptime key: Key) type {
test "Value" { test "Value" {
const testing = std.testing; const testing = std.testing;
try testing.expectEqual(?[:0]const u8, Value(.@"font-family")); try testing.expectEqual(Config.RepeatableString, Value(.@"font-family"));
try testing.expectEqual(?bool, Value(.@"cursor-style-blink")); try testing.expectEqual(?bool, Value(.@"cursor-style-blink"));
} }