refactor: handle freetype load flags in face instead of renderer

This commit is contained in:
Nadir Fejzic
2024-11-09 00:41:55 +01:00
parent 74bda5a6eb
commit 945a715b08
7 changed files with 25 additions and 31 deletions

View File

@ -305,7 +305,7 @@ const c = @cImport({
/// * `autohint` - Use the freetype auto-hinter. Enabled by default.
///
/// Example: `hinting`, `no-hinting`, `force-autohint`, `no-force-autohint`
@"freetype-load-flag": FreetypeLoadFlags = .{},
@"freetype-load-flags": FreetypeLoadFlags = .{},
/// A theme to use. If the theme is an absolute pathname, Ghostty will attempt
/// to load that file as a theme. If that file does not exist or is inaccessible,

View File

@ -452,6 +452,8 @@ pub const LoadOptions = struct {
/// for this is owned by the user and is not freed by the collection.
metric_modifiers: Metrics.ModifierSet = .{},
freetype_load_flags: config.Config.FreetypeLoadFlags = .{},
pub fn deinit(self: *LoadOptions, alloc: Allocator) void {
_ = self;
_ = alloc;
@ -462,6 +464,7 @@ pub const LoadOptions = struct {
return .{
.size = self.size,
.metric_modifiers = &self.metric_modifiers,
.freetype_load_flags = self.freetype_load_flags,
};
}
};

View File

@ -168,6 +168,7 @@ fn collection(
.library = self.font_lib,
.size = size,
.metric_modifiers = key.metric_modifiers,
.freetype_load_flags = config.@"freetype-load-flags",
};
var c = Collection.init();
@ -427,6 +428,7 @@ pub const DerivedConfig = struct {
@"adjust-strikethrough-position": ?Metrics.Modifier,
@"adjust-strikethrough-thickness": ?Metrics.Modifier,
@"adjust-cursor-thickness": ?Metrics.Modifier,
@"freetype-load-flags": configpkg.Config.FreetypeLoadFlags,
/// Initialize a DerivedConfig. The config should be either a
/// config.Config or another DerivedConfig to clone from.
@ -461,6 +463,7 @@ pub const DerivedConfig = struct {
.@"adjust-strikethrough-position" = config.@"adjust-strikethrough-position",
.@"adjust-strikethrough-thickness" = config.@"adjust-strikethrough-thickness",
.@"adjust-cursor-thickness" = config.@"adjust-cursor-thickness",
.@"freetype-load-flags" = config.@"freetype-load-flags",
// This must be last so the arena contains all our allocations
// from above since Zig does assignment in order.
@ -500,6 +503,8 @@ pub const Key = struct {
/// font grid.
font_size: DesiredSize = .{ .points = 12 },
load_flags: configpkg.Config.FreetypeLoadFlags = .{},
const style_offsets_len = std.enums.directEnumArrayLen(Style, 0);
const StyleOffsets = [style_offsets_len]usize;
@ -618,6 +623,7 @@ pub const Key = struct {
.codepoint_map = codepoint_map,
.metric_modifiers = metric_modifiers,
.font_size = font_size,
.load_flags = config.@"freetype-load-flags",
};
}
@ -647,6 +653,7 @@ pub const Key = struct {
for (self.descriptors) |d| d.hash(hasher);
self.codepoint_map.hash(hasher);
autoHash(hasher, self.metric_modifiers.count());
autoHash(hasher, self.load_flags);
if (self.metric_modifiers.count() > 0) {
inline for (@typeInfo(Metrics.Key).Enum.fields) |field| {
const key = @field(Metrics.Key, field.name);

View File

@ -31,6 +31,7 @@ pub const default_dpi = if (builtin.os.tag == .macos) 72 else 96;
pub const Options = struct {
size: DesiredSize,
metric_modifiers: ?*const Metrics.ModifierSet = null,
freetype_load_flags: FreetypeLoadFlags,
};
/// The desired size for loading a font.
@ -91,8 +92,6 @@ pub const RenderOptions = struct {
///
/// This only works with CoreText currently.
thicken: bool = false,
load_flags: FreetypeLoadFlags,
};
test {

View File

@ -18,6 +18,7 @@ const Library = font.Library;
const convert = @import("freetype_convert.zig");
const fastmem = @import("../../fastmem.zig");
const quirks = @import("../../quirks.zig");
const FreetypeLoadFlags = @import("../../config/Config.zig").FreetypeLoadFlags;
const log = std.log.scoped(.font_face);
@ -34,6 +35,9 @@ pub const Face = struct {
/// Metrics for this font face. These are useful for renderers.
metrics: font.face.Metrics,
/// Metrics for this font face. These are useful for renderers.
load_flags: FreetypeLoadFlags,
/// Set quirks.disableDefaultFontFeatures
quirks_disable_default_font_features: bool = false,
@ -77,6 +81,7 @@ pub const Face = struct {
.face = face,
.hb_font = hb_font,
.metrics = calcMetrics(face, opts.metric_modifiers),
.load_flags = opts.freetype_load_flags,
};
result.quirks_disable_default_font_features = quirks.disableDefaultFontFeatures(&result);
@ -318,13 +323,13 @@ pub const Face = struct {
//
// This must be enabled for color faces though because those are
// often colored bitmaps, which we support.
.no_bitmap = !self.face.hasColor() or !opts.load_flags.bitmap,
.no_bitmap = !self.face.hasColor() or !self.load_flags.bitmap,
// use options from config
.no_hinting = !opts.load_flags.hinting,
.force_autohint = !opts.load_flags.@"force-autohint",
.monochrome = !opts.load_flags.monochrome,
.no_autohint = !opts.load_flags.autohint,
.no_hinting = !self.load_flags.hinting,
.force_autohint = !self.load_flags.@"force-autohint",
.monochrome = !self.load_flags.monochrome,
.no_autohint = !self.load_flags.autohint,
});
const glyph = self.face.handle.*.glyph;

View File

@ -354,7 +354,6 @@ pub const DerivedConfig = struct {
font_thicken: bool,
font_features: std.ArrayListUnmanaged([:0]const u8),
font_styles: font.CodepointResolver.StyleStatus,
load_flags: configpkg.Config.FreetypeLoadFlags,
cursor_color: ?terminal.color.RGB,
cursor_invert: bool,
cursor_opacity: f64,
@ -400,14 +399,11 @@ pub const DerivedConfig = struct {
const cursor_invert = config.@"cursor-invert-fg-bg";
const load_flags = config.@"freetype-load-flag";
return .{
.background_opacity = @max(0, @min(1, config.@"background-opacity")),
.font_thicken = config.@"font-thicken",
.font_features = font_features,
.font_styles = font_styles,
.load_flags = load_flags,
.cursor_color = if (!cursor_invert and config.@"cursor-color" != null)
config.@"cursor-color".?.toTerminalRGB()
@ -2723,7 +2719,6 @@ fn addUnderline(
.{
.cell_width = 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
);
@ -2756,7 +2751,6 @@ fn addOverline(
.{
.cell_width = 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
);
@ -2789,7 +2783,6 @@ fn addStrikethrough(
.{
.cell_width = 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
);
@ -2829,7 +2822,6 @@ fn addGlyph(
.{
.grid_metrics = self.grid_metrics,
.thicken = self.config.font_thicken,
.load_flags = self.config.load_flags,
},
);
@ -2909,7 +2901,6 @@ fn addCursor(
.{
.cell_width = if (wide) 2 else 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
) catch |err| {
log.warn("error rendering cursor glyph err={}", .{err});
@ -2925,7 +2916,6 @@ fn addCursor(
.{
.cell_width = if (wide) 2 else 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
) catch |err| {
log.warn("error rendering cursor glyph err={}", .{err});
@ -2966,7 +2956,7 @@ fn addPreeditCell(
@intCast(cp.codepoint),
.regular,
.text,
.{ .grid_metrics = self.grid_metrics, .load_flags = self.config.load_flags },
.{ .grid_metrics = self.grid_metrics },
) catch |err| {
log.warn("error rendering preedit glyph err={}", .{err});
return;

View File

@ -289,7 +289,6 @@ pub const DerivedConfig = struct {
font_thicken: bool,
font_features: std.ArrayListUnmanaged([:0]const u8),
font_styles: font.CodepointResolver.StyleStatus,
load_flags: configpkg.Config.FreetypeLoadFlags,
cursor_color: ?terminal.color.RGB,
cursor_invert: bool,
cursor_text: ?terminal.color.RGB,
@ -334,14 +333,11 @@ pub const DerivedConfig = struct {
const cursor_invert = config.@"cursor-invert-fg-bg";
const load_flags = config.@"freetype-load-flag";
return .{
.background_opacity = @max(0, @min(1, config.@"background-opacity")),
.font_thicken = config.@"font-thicken",
.font_features = font_features,
.font_styles = font_styles,
.load_flags = load_flags,
.cursor_color = if (!cursor_invert and config.@"cursor-color" != null)
config.@"cursor-color".?.toTerminalRGB()
@ -1770,7 +1766,7 @@ fn addPreeditCell(
@intCast(cp.codepoint),
.regular,
.text,
.{ .grid_metrics = self.grid_metrics, .load_flags = self.config.load_flags },
.{ .grid_metrics = self.grid_metrics },
) catch |err| {
log.warn("error rendering preedit glyph err={}", .{err});
return;
@ -1871,7 +1867,6 @@ fn addCursor(
.{
.cell_width = if (wide) 2 else 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
) catch |err| {
log.warn("error rendering cursor glyph err={}", .{err});
@ -1887,7 +1882,6 @@ fn addCursor(
.{
.cell_width = if (wide) 2 else 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
) catch |err| {
log.warn("error rendering cursor glyph err={}", .{err});
@ -1950,7 +1944,6 @@ fn addUnderline(
.{
.cell_width = 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
);
@ -1992,7 +1985,6 @@ fn addOverline(
.{
.cell_width = 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
);
@ -2034,7 +2026,6 @@ fn addStrikethrough(
.{
.cell_width = 1,
.grid_metrics = self.grid_metrics,
.load_flags = self.config.load_flags,
},
);
@ -2083,7 +2074,6 @@ fn addGlyph(
.{
.grid_metrics = self.grid_metrics,
.thicken = self.config.font_thicken,
.load_flags = self.config.load_flags,
},
);