summaryrefslogtreecommitdiff
path: root/src/font/Collection.zig
diff options
context:
space:
mode:
authorMitchell Hashimoto <m@mitchellh.com>2024-08-23 20:52:13 -0700
committerMitchell Hashimoto <m@mitchellh.com>2024-08-23 20:53:22 -0700
commitd22551cd31b76a4d25d5f9cdbc08451d5d35d3b1 (patch)
treecba63445e524028561a5713b7e6759b2981a4e39 /src/font/Collection.zig
parent74291793db0437d2c5d201f7f38d15dfcaf0ba83 (diff)
font/coretext: support synthetic bold
Diffstat (limited to 'src/font/Collection.zig')
-rw-r--r--src/font/Collection.zig32
1 files changed, 29 insertions, 3 deletions
diff --git a/src/font/Collection.zig b/src/font/Collection.zig
index 256c6b73e..01237a107 100644
--- a/src/font/Collection.zig
+++ b/src/font/Collection.zig
@@ -273,9 +273,15 @@ pub fn completeStyles(self: *Collection, alloc: Allocator) CompleteError!void {
// If we don't have bold, use the regular font.
const bold_list = self.faces.getPtr(.bold);
- if (bold_list.count() == 0) {
- log.warn("bold style not available, using regular font", .{});
- try bold_list.append(alloc, .{ .alias = regular_entry });
+ if (bold_list.count() == 0) bold: {
+ const synthetic = self.syntheticBold(regular_entry) catch |err| {
+ log.warn("failed to create synthetic bold, bold style will not be available err={}", .{err});
+ try bold_list.append(alloc, .{ .alias = regular_entry });
+ break :bold;
+ };
+
+ log.info("synthetic bold face created", .{});
+ try bold_list.append(alloc, .{ .loaded = synthetic });
}
// If we don't have bold italic, use the regular italic font.
@@ -305,6 +311,26 @@ pub fn completeStyles(self: *Collection, alloc: Allocator) CompleteError!void {
}
// Create an synthetic italic font face from the given entry and return it.
+fn syntheticBold(self: *Collection, entry: *Entry) !Face {
+ // Not all font backends support synthetic bold.
+ if (comptime !@hasDecl(Face, "syntheticBold")) return error.SyntheticBoldUnavailable;
+
+ // We require loading options to create a synthetic bold face.
+ const opts = self.load_options orelse return error.DeferredLoadingUnavailable;
+
+ // Try to bold it.
+ const regular = try self.getFaceFromEntry(entry);
+ const face = try regular.syntheticBold(opts.faceOptions());
+
+ var buf: [256]u8 = undefined;
+ if (face.name(&buf)) |name| {
+ log.info("font synthetic bold created family={s}", .{name});
+ } else |_| {}
+
+ return face;
+}
+
+// Create an synthetic italic font face from the given entry and return it.
fn syntheticItalic(self: *Collection, entry: *Entry) !Face {
// Not all font backends support synthetic italicization.
if (comptime !@hasDecl(Face, "syntheticItalic")) return error.SyntheticItalicUnavailable;