Skip to content

Commit

Permalink
graphics: Add fillTextExt.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Jun 18, 2022
1 parent eee0f1b commit a58d278
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
18 changes: 13 additions & 5 deletions graphics/src/backend/gpu/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const cgltf = @import("cgltf");
const stbi = @import("stbi");
const math = stdx.math;
const Vec2 = math.Vec2;
const Vec3 = math.Vec3;
const vec2 = math.Vec2.init;
const Mat4 = math.Mat4;
const geom = math.geom;
Expand Down Expand Up @@ -513,7 +514,14 @@ pub const Graphics = struct {
return text_renderer.textGlyphIter(self, font_gid, size, self.cur_dpr_ceil, str);
}

pub fn fillText(self: *Self, x: f32, y: f32, str: []const u8) void {
pub inline fn fillText(self: *Self, x: f32, y: f32, str: []const u8) void {
self.fillTextExt(x, y, str, .{
.@"align" = self.cur_text_align,
.baseline = self.cur_text_baseline,
});
}

pub fn fillTextExt(self: *Self, x: f32, y: f32, str: []const u8, opts: graphics.TextOptions) void {
// log.info("draw text '{s}'", .{str});
var vert: TexShaderVertex = undefined;

Expand All @@ -522,18 +530,18 @@ pub const Graphics = struct {
var start_x = x;
var start_y = y;

if (self.cur_text_align != .Left) {
if (opts.@"align" != .Left) {
var metrics: TextMetrics = undefined;
self.measureText(str, &metrics);
switch (self.cur_text_align) {
switch (opts.@"align") {
.Left => {},
.Right => start_x = x-metrics.width,
.Center => start_x = x-metrics.width/2,
}
}
if (self.cur_text_baseline != .Top) {
if (opts.baseline != .Top) {
const vmetrics = self.font_cache.getPrimaryFontVMetrics(self.cur_font_gid, self.cur_font_size);
switch (self.cur_text_baseline) {
switch (opts.baseline) {
.Top => {},
.Middle => start_y = y - vmetrics.height / 2,
.Alphabetic => start_y = y - vmetrics.ascender,
Expand Down
14 changes: 14 additions & 0 deletions graphics/src/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,15 @@ pub const Graphics = struct {
self.fillText(x, y, self.text_buf.items);
}

pub fn fillTextExt(self: *Self, x: f32, y: f32, comptime format: []const u8, args: anytype, opts: TextOptions) void {
self.text_buf.clearRetainingCapacity();
std.fmt.format(self.text_buf.writer(), format, args) catch unreachable;
switch (Backend) {
.OpenGL, .Vulkan => gpu.Graphics.fillTextExt(&self.impl, x, y, self.text_buf.items, opts),
else => stdx.unsupported(),
}
}

/// Measure many text at once.
pub fn measureTextBatch(self: *Self, arr: []*TextMeasure) void {
switch (Backend) {
Expand Down Expand Up @@ -1932,6 +1941,11 @@ pub const TextBaseline = enum {
Bottom,
};

pub const TextOptions = struct {
@"align": TextAlign = .Left,
baseline: TextBaseline = .Top,
};

pub const BitmapFontData = struct {
data: []const u8,
size: u8,
Expand Down

0 comments on commit a58d278

Please sign in to comment.