bold font rendering

This commit is contained in:
Mitchell Hashimoto
2022-06-28 13:44:20 -07:00
parent d5b018349c
commit 75b9d1cb84
4 changed files with 19 additions and 5 deletions

View File

@ -106,6 +106,7 @@ pub fn init(alloc: Allocator) !Grid {
var fam = try font.Family.init(atlas);
errdefer fam.deinit(alloc);
try fam.loadFaceFromMemory(.regular, face_ttf, 32);
try fam.loadFaceFromMemory(.bold, face_bold_ttf, 32);
// Load all visible ASCII characters and build our cell width based on
// the widest character that we see.
@ -310,13 +311,19 @@ pub fn updateCells(self: *Grid, term: Terminal) !void {
// If the cell is empty then we draw nothing in the box.
if (cell.empty()) continue;
// Determine our glyph styling
const style: font.Style = if (cell.attrs.bold == 1)
.bold
else
.regular;
// Get our glyph
// TODO: if we add a glyph, I think we need to rerender the texture.
const glyph = if (self.font_atlas.getGlyph(cell.char, .regular)) |glyph|
const glyph = if (self.font_atlas.getGlyph(cell.char, style)) |glyph|
glyph
else glyph: {
self.atlas_dirty = true;
break :glyph try self.font_atlas.addGlyph(self.alloc, cell.char, .regular);
break :glyph try self.font_atlas.addGlyph(self.alloc, cell.char, style);
};
const fg = cell.fg orelse self.foreground;
@ -509,4 +516,4 @@ test "GridSize update rounding" {
}
const face_ttf = @embedFile("../fonts/FiraCode-Regular.ttf");
//const face_ttf = @embedFile("../fonts/Inconsolata-Regular.ttf");
const face_bold_ttf = @embedFile("../fonts/FiraCode-Bold.ttf");

View File

@ -32,6 +32,7 @@ glyphs: std.AutoHashMapUnmanaged(GlyphKey, Glyph) = .{},
/// The font faces representing all the styles in this family.
/// These should be set directly or via various loader functions.
regular: ?Face = null,
bold: ?Face = null,
/// This struct is used for the hash key for glyphs.
const GlyphKey = struct {
@ -55,6 +56,7 @@ pub fn deinit(self: *Family, alloc: Allocator) void {
self.glyphs.deinit(alloc);
if (self.regular) |*face| face.deinit();
if (self.bold) |*face| face.deinit();
if (ftc.FT_Done_FreeType(self.ft_library) != ftok)
log.err("failed to clean up FreeType", .{});
@ -77,7 +79,7 @@ pub fn loadFaceFromMemory(
@field(self, switch (style) {
.regular => "regular",
.bold => unreachable,
.bold => "bold",
.italic => unreachable,
.bold_italic => unreachable,
}) = face;
@ -106,7 +108,7 @@ pub fn addGlyph(self: *Family, alloc: Allocator, v: anytype, style: Style) !*Gly
// Real is the face we SHOULD use for this style.
var real = switch (style) {
.regular => self.regular,
.bold => unreachable,
.bold => self.bold,
.italic => unreachable,
.bold_italic => unreachable,
};

View File

@ -23,6 +23,7 @@ pub const Cell = struct {
/// On/off attributes that can be set
/// TODO: pack it
attrs: struct {
bold: u1 = 0,
inverse: u1 = 0,
} = .{},

View File

@ -150,6 +150,10 @@ pub fn setAttribute(self: *Terminal, attr: sgr.Attribute) !void {
self.cursor.pen.attrs = .{};
},
.bold => {
self.cursor.pen.attrs.bold = 1;
},
.inverse => {
self.cursor.pen.attrs.inverse = 1;
},