Use em size as nerd font reference metric

This commit is contained in:
Daniel Wennberg
2025-07-15 13:04:51 -07:00
parent 80e52aa6f0
commit 69b83e7de4
3 changed files with 10 additions and 24 deletions

View File

@ -533,7 +533,7 @@ pub fn add(
const nf_symbols = b.dependency("nerd_fonts_symbols_only", .{});
step.root_module.addAnonymousImport(
"nerd_fonts_symbols_only",
.{ .root_source_file = nf_symbols.path("SymbolsNerdFontMono-Regular.ttf") },
.{ .root_source_file = nf_symbols.path("SymbolsNerdFont-Regular.ttf") },
);
}

View File

@ -305,7 +305,7 @@ fn collection(
.{ .fallback_loaded = try Face.init(
self.font_lib,
font.embedded.symbols_nerd_font,
load_options.faceOptions(),
load_options.faceOptions().setReferenceMetric(.em_size),
) },
);

View File

@ -45,6 +45,13 @@ pub const Options = struct {
// the implementation of Collections.adjustedSize() for fallback
// rules when the font does not define the specified metric.
reference_metric: ReferenceMetric = .ic_width,
// Convenience function to create a copy with a different reference metric.
pub fn setReferenceMetric(self: Options, reference_metric: ReferenceMetric) Options {
var opts = self;
opts.reference_metric = reference_metric;
return opts;
}
};
/// The desired size for loading a font.
@ -242,7 +249,7 @@ pub const RenderOptions = struct {
) GlyphSize {
var g = glyph;
var available_width: f64 = @floatFromInt(
const available_width: f64 = @floatFromInt(
metrics.cell_width * @min(
self.max_constraint_width,
constraint_width,
@ -253,22 +260,6 @@ pub const RenderOptions = struct {
.icon => metrics.icon_height,
});
// We make the opinionated choice here to reduce the width
// of icon-height symbols by the same amount horizontally,
// since otherwise wide aspect ratio icons like folders end
// up far too wide.
//
// But we *only* do this if the constraint width is 2, since
// otherwise it would make them way too small when sized for
// a single cell.
const is_icon_width = self.height == .icon and @min(self.max_constraint_width, constraint_width) > 1;
const orig_avail_width = available_width;
if (is_icon_width) {
const cell_height: f64 = @floatFromInt(metrics.cell_height);
const ratio = available_height / cell_height;
available_width *= ratio;
}
const w = available_width -
self.pad_left * available_width -
self.pad_right * available_width;
@ -392,11 +383,6 @@ pub const RenderOptions = struct {
.center => g.y = (h - g.height) / 2,
}
// Add offset for icon width restriction, to keep it centered.
if (is_icon_width) {
g.x += (orig_avail_width - available_width) / 2;
}
// Re-add our padding before returning.
g.x += self.pad_left * available_width;
g.y += self.pad_bottom * available_height;