summaryrefslogtreecommitdiff
path: root/src/font/Collection.zig
diff options
context:
space:
mode:
authorQwerasd <qwerasd205@users.noreply.github.com>2025-09-03 20:04:29 -0600
committerQwerasd <qwerasd205@users.noreply.github.com>2025-09-03 21:13:39 -0600
commit5c1d87fda6b4a7304248fb8bdd00a27a2d0cfbfd (patch)
tree6ec2001dfd909ded6b2623814ea9bf429d340329 /src/font/Collection.zig
parentc3e7857a2c5031e8274a411c84695351e8e3fffe (diff)
fix(font): make `face.getMetrics()` infallible
Before we had a bad day if we tried to get the metrics of a bitmap font, which would happen if we ever used one as fallback because we started doing it for all fonts when we added fallback font scaling. This is a pretty easy fix and finally allows users to configure true bitmap fonts as their primary font as long as FreeType/CoreText can handle it.
Diffstat (limited to 'src/font/Collection.zig')
-rw-r--r--src/font/Collection.zig23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/font/Collection.zig b/src/font/Collection.zig
index 2b5f591a5..ad9590d70 100644
--- a/src/font/Collection.zig
+++ b/src/font/Collection.zig
@@ -92,7 +92,6 @@ pub const AddOptions = struct {
pub const AddError =
Allocator.Error ||
- Face.GetMetricsError ||
error{
/// There's no more room in the collection.
CollectionFull,
@@ -127,7 +126,7 @@ pub fn add(
// Scale factor to adjust the size of the added face.
const scale_factor = self.scaleFactor(
- try owned_face.getMetrics(),
+ owned_face.getMetrics(),
opts.size_adjustment,
);
@@ -225,7 +224,7 @@ fn getFaceFromEntry(
// entry now that we have a loaded face.
entry.scale_factor = .{
.scale = self.scaleFactor(
- try face.getMetrics(),
+ face.getMetrics(),
entry.scale_factor.adjustment,
),
};
@@ -592,7 +591,7 @@ fn scaleFactor(
@branchHint(.unlikely);
// If we can't load the primary face, just use 1.0 as the scale factor.
const primary_face = self.getFace(.{ .idx = 0 }) catch return 1.0;
- self.primary_face_metrics = primary_face.getMetrics() catch return 1.0;
+ self.primary_face_metrics = primary_face.getMetrics();
}
const primary_metrics = self.primary_face_metrics.?;
@@ -652,7 +651,7 @@ fn scaleFactor(
return primary_metric / face_metric;
}
-const UpdateMetricsError = font.Face.GetMetricsError || error{
+const UpdateMetricsError = error{
CannotLoadPrimaryFont,
};
@@ -663,7 +662,7 @@ const UpdateMetricsError = font.Face.GetMetricsError || error{
pub fn updateMetrics(self: *Collection) UpdateMetricsError!void {
const primary_face = self.getFace(.{ .idx = 0 }) catch return error.CannotLoadPrimaryFont;
- self.primary_face_metrics = try primary_face.getMetrics();
+ self.primary_face_metrics = primary_face.getMetrics();
var metrics = Metrics.calc(self.primary_face_metrics.?);
@@ -1288,8 +1287,8 @@ test "adjusted sizes" {
// The chosen metric should match.
{
- const primary_metrics = try (try c.getFace(.{ .idx = 0 })).getMetrics();
- const fallback_metrics = try (try c.getFace(fallback_idx)).getMetrics();
+ const primary_metrics = (try c.getFace(.{ .idx = 0 })).getMetrics();
+ const fallback_metrics = (try c.getFace(fallback_idx)).getMetrics();
try std.testing.expectApproxEqAbs(
@field(primary_metrics, metric).?,
@@ -1302,8 +1301,8 @@ test "adjusted sizes" {
// Resize should keep that relationship.
try c.setSize(.{ .points = 37, .xdpi = 96, .ydpi = 96 });
{
- const primary_metrics = try (try c.getFace(.{ .idx = 0 })).getMetrics();
- const fallback_metrics = try (try c.getFace(fallback_idx)).getMetrics();
+ const primary_metrics = (try c.getFace(.{ .idx = 0 })).getMetrics();
+ const fallback_metrics = (try c.getFace(fallback_idx)).getMetrics();
try std.testing.expectApproxEqAbs(
@field(primary_metrics, metric).?,
@@ -1359,8 +1358,8 @@ test "adjusted sizes" {
// Test fallback to lineHeight() (ex_height and cap_height not defined in symbols font).
{
- const primary_metrics = try (try c.getFace(.{ .idx = 0 })).getMetrics();
- const symbol_metrics = try (try c.getFace(symbol_idx)).getMetrics();
+ const primary_metrics = (try c.getFace(.{ .idx = 0 })).getMetrics();
+ const symbol_metrics = (try c.getFace(symbol_idx)).getMetrics();
try std.testing.expectApproxEqAbs(
primary_metrics.lineHeight(),