Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font theme rework #128

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Examples.zig
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ pub fn demo() !void {
defer hbox.deinit();

if (try dvui.button(@src(), "Zoom In", .{}, .{})) {
scale_val = @round(dvui.themeGet().font_body.size * scale_val + 1.0) / dvui.themeGet().font_body.size;
scale_val = @round(dvui.themeGet().font_body.getSize() * scale_val + 1.0) / dvui.themeGet().font_body.getSize();
}

if (try dvui.button(@src(), "Zoom Out", .{}, .{})) {
scale_val = @round(dvui.themeGet().font_body.size * scale_val - 1.0) / dvui.themeGet().font_body.size;
scale_val = @round(dvui.themeGet().font_body.getSize() * scale_val - 1.0) / dvui.themeGet().font_body.getSize();
}
}

Expand Down
194 changes: 151 additions & 43 deletions src/Font.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,45 @@ const Size = dvui.Size;

const Font = @This();

size: f32,
//size: f32,
line_height_factor: f32 = 1.0,
name: []const u8,
scale: f32 = 1.0,

pub fn resize(self: *const Font, s: f32) Font {
return Font{ .size = s, .line_height_factor = self.line_height_factor, .name = self.name };
}
//pub fn resize(self: *const Font, s: f32) Font {
//return Font{ .size = s, .line_height_factor = self.line_height_factor, .name = self.name };
//}

pub fn lineHeightFactor(self: *const Font, factor: f32) Font {
return Font{ .size = self.size, .line_height_factor = factor, .name = self.name };
return Font{ .scale = self.scale, .line_height_factor = factor, .name = self.name };
}

pub const Data = struct {
bytes: []const u8,
base_size: f32 = 16,
};

// default bytes if font id is not found in database
pub var default_font_data: Data = TTFBytes.Vera;

/// Gets font ttf bytes and default size for font
pub fn getData(self: Font) *Font.Data {
if (dvui.currentWindow().font_database.getPtr(self.name)) |font_data| {
return font_data;
} else {
return &default_font_data;
}
}

// handles multiple lines
pub fn getBytes(self: Font) []const u8 {
return self.getData().bytes;
}

pub fn getSize(self: Font) f32 {
return @ceil(self.getData().base_size * self.scale);
}

//handles multiple lines
pub fn textSize(self: *const Font, text: []const u8) !Size {
if (text.len == 0) {
// just want the line height
Expand Down Expand Up @@ -46,19 +72,29 @@ pub const EndMetric = enum {
};

/// textSizeEx always stops at a newline, use textSize to get multiline sizes
pub fn textSizeEx(self: *const Font, text: []const u8, max_width: ?f32, end_idx: ?*usize, end_metric: EndMetric) !Size {
pub fn textSizeEx(
self: *const Font,
text: []const u8,
max_width: ?f32,
end_idx: ?*usize,
end_metric: EndMetric,
) !Size {
// ask for a font that matches the natural display pixels so we get a more
// accurate size
const font_data = self.getData();

const ss = dvui.parentGet().screenRectScale(Rect{}).s;
const ask_size = self.size * ss;
const sized_font = self.resize(ask_size);
const screen_scale = dvui.parentGet().screenRectScale(Rect{}).s;
const ask_size = self.getSize() * screen_scale;
//const sized_font = self.resize(ask_size);

// might give us a slightly smaller font
const fce = try dvui.fontCacheGet(sized_font);
const fce = try dvui.fontCacheGet(font_data.bytes, ask_size);

// this must be synced with dvui.renderText()
const target_fraction = if (dvui.currentWindow().snap_to_pixels) 1.0 / ss else self.size / fce.height;
const target_fraction = if (dvui.currentWindow().snap_to_pixels)
1.0 / screen_scale
else
self.getSize() / fce.height;

var max_width_sized: ?f32 = null;
if (max_width) |mwidth| {
Expand All @@ -75,47 +111,119 @@ pub fn textSizeEx(self: *const Font, text: []const u8, max_width: ?f32, end_idx:
// convert size back from font units
return s.scale(target_fraction);
}

//
pub fn lineHeight(self: *const Font) !f32 {
const s = try self.textSizeEx(" ", null, null, .before);
return s.h;
}

// default bytes if font id is not found in database
pub const default_ttf_bytes = TTFBytes.Vera;

// functionality for accessing builtin fonts
pub const TTFBytes = struct {
pub const Aleo = @embedFile("fonts/Aleo/static/Aleo-Regular.ttf");
pub const AleoBd = @embedFile("fonts/Aleo/static/Aleo-Bold.ttf");
pub const Vera = @embedFile("fonts/bitstream-vera/Vera.ttf");
pub const VeraBI = @embedFile("fonts/bitstream-vera/VeraBI.ttf");
pub const VeraBd = @embedFile("fonts/bitstream-vera/VeraBd.ttf");
pub const VeraIt = @embedFile("fonts/bitstream-vera/VeraIt.ttf");
pub const VeraMoBI = @embedFile("fonts/bitstream-vera/VeraMoBI.ttf");
pub const VeraMoBd = @embedFile("fonts/bitstream-vera/VeraMoBd.ttf");
pub const VeraMoIt = @embedFile("fonts/bitstream-vera/VeraMoIt.ttf");
pub const VeraMono = @embedFile("fonts/bitstream-vera/VeraMono.ttf");
pub const VeraSe = @embedFile("fonts/bitstream-vera/VeraSe.ttf");
pub const VeraSeBd = @embedFile("fonts/bitstream-vera/VeraSeBd.ttf");
pub const Pixelify = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Regular.ttf");
pub const PixelifyBd = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Bold.ttf");
pub const PixelifyMe = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Medium.ttf");
pub const PixelifySeBd = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-SemiBold.ttf");
pub const Hack = @embedFile("fonts/hack/Hack-Regular.ttf");
pub const HackBd = @embedFile("fonts/hack/Hack-Bold.ttf");
pub const HackIt = @embedFile("fonts/hack/Hack-Italic.ttf");
pub const HackBdIt = @embedFile("fonts/hack/Hack-BoldItalic.ttf");
pub const OpenDyslexic = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Regular.otf");
pub const OpenDyslexicBd = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold.otf");
pub const OpenDyslexicIt = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Italic.otf");
pub const OpenDyslexicBdIt = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold-Italic.otf");
pub const Aleo = Data{
.bytes = @embedFile("fonts/Aleo/static/Aleo-Regular.ttf"),
.base_size = 16,
};
pub const AleoBd = Data{
.bytes = @embedFile("fonts/Aleo/static/Aleo-Bold.ttf"),
.base_size = 16,
};
pub const Vera = Data{
.bytes = @embedFile("fonts/bitstream-vera/Vera.ttf"),
.base_size = 13,
};
pub const VeraBI = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraBI.ttf"),
.base_size = 13,
};
pub const VeraBd = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraBd.ttf"),
.base_size = 13,
};
pub const VeraIt = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraIt.ttf"),
.base_size = 13,
};
pub const VeraMoBI = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraMoBI.ttf"),
.base_size = 13,
};
pub const VeraMoBd = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraMoBd.ttf"),
.base_size = 13,
};
pub const VeraMoIt = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraMoIt.ttf"),
.base_size = 13,
};
pub const VeraMono = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraMono.ttf"),
.base_size = 13,
};
pub const VeraSe = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraSe.ttf"),
.base_size = 13,
};
pub const VeraSeBd = Data{
.bytes = @embedFile("fonts/bitstream-vera/VeraSeBd.ttf"),
.base_size = 13,
};
pub const Pixelify = Data{
.bytes = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Regular.ttf"),
.base_size = 18,
};
pub const PixelifyBd = Data{
.bytes = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Bold.ttf"),
.base_size = 18,
};
pub const PixelifyMe = Data{
.bytes = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-Medium.ttf"),
.base_size = 18,
};
pub const PixelifySeBd = Data{
.bytes = @embedFile("fonts/Pixelify_Sans/static/PixelifySans-SemiBold.ttf"),
.base_size = 18,
};
pub const Hack = Data{
.bytes = @embedFile("fonts/hack/Hack-Regular.ttf"),
.base_size = 18,
};
pub const HackBd = Data{
.bytes = @embedFile("fonts/hack/Hack-Bold.ttf"),
.base_size = 18,
};
pub const HackIt = Data{
.bytes = @embedFile("fonts/hack/Hack-Italic.ttf"),
.base_size = 18,
};
pub const HackBdIt = Data{
.bytes = @embedFile("fonts/hack/Hack-BoldItalic.ttf"),
.base_size = 18,
};
pub const OpenDyslexic = Data{
.bytes = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Regular.otf"),
.base_size = 18,
};
pub const OpenDyslexicBd = Data{
.bytes = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold.otf"),
.base_size = 18,
};
pub const OpenDyslexicIt = Data{
.bytes = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Italic.otf"),
.base_size = 18,
};
pub const OpenDyslexicBdIt = Data{
.bytes = @embedFile("fonts/OpenDyslexic/compiled/OpenDyslexic-Bold-Italic.otf"),
.base_size = 18,
};
};

pub fn initTTFBytesDatabase(allocator: std.mem.Allocator) !std.StringHashMap([]const u8) {
var result = std.StringHashMap([]const u8).init(allocator);
pub fn initTTFBytesDatabase(allocator: std.mem.Allocator) !std.StringHashMap(Data) {
var result = std.StringHashMap(Data).init(allocator);
inline for (@typeInfo(TTFBytes).Struct.decls) |decl| {
try result.put(decl.name, @field(TTFBytes, decl.name));
try result.put(decl.name, .{
.bytes = @field(TTFBytes, decl.name).bytes,
.base_size = @field(TTFBytes, decl.name).base_size,
});
}
return result;
}
28 changes: 14 additions & 14 deletions src/Theme.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ style_accent: Options,
// used for a button to perform dangerous actions
style_err: Options,

pub fn fontSizeAdd(self: *Theme, delta: f32) Theme {
var ret = self.*;
ret.font_body.size += delta;
ret.font_heading.size += delta;
ret.font_caption.size += delta;
ret.font_caption_heading.size += delta;
ret.font_title.size += delta;
ret.font_title_1.size += delta;
ret.font_title_2.size += delta;
ret.font_title_3.size += delta;
ret.font_title_4.size += delta;

return ret;
}
//pub fn fontSizeAdd(self: *Theme, delta: f32) Theme {
//var ret = self.*;
//ret.font_body.getData().base_size += delta;
//ret.font_heading.getData().base_size += delta;
//ret.font_caption.getData().base_size += delta;
//ret.font_caption_heading.getData().base_size += delta;
//ret.font_title.getData().base_size += delta;
//ret.font_title_1.getData().base_size += delta;
//ret.font_title_2.getData().base_size += delta;
//ret.font_title_3.getData().base_size += delta;
//ret.font_title_4.getData().base_size += delta;
//
//return ret;
//}
Loading