summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
Diffstat (limited to 'src/host')
-rw-r--r--src/host/Host.zig12
-rw-r--r--src/host/TimingAtlas.zig3
-rw-r--r--src/host/gfx.zig13
3 files changed, 16 insertions, 12 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig
index ede02df..9a2c0fa 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -1065,7 +1065,8 @@ const api: abi.Host.VTable = .{
.freeBuffer = apiFreeBuffer,
.writeBuffer = apiWriteBuffer,
.imageSize = apiImageSize,
- .textSize = apiTextSize,
+ .textRun = apiTextRun,
+ .font = apiFont,
.loadSound = apiLoadSound,
.decodeSound = apiDecodeSound,
.newSound = apiNewSound,
@@ -1127,9 +1128,12 @@ fn apiSamples(ctx: *anyopaque, audio: abi.Audio) []const f32 {
fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 {
return hostOf(ctx).images.size(image);
}
-fn apiTextSize(ctx: *anyopaque, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 {
- _ = ctx;
- return gfx.textSize(scale, bytes, face);
+fn apiTextRun(_: *anyopaque, face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun {
+ return gfx.textRun(face, scale, bytes, xs);
+}
+fn apiFont(_: *anyopaque, bytes: []const u8) ?abi.Face {
+ const face = c.sk_text_font(bytes.ptr, bytes.len);
+ return if (face < 0) null else @enumFromInt(face);
}
fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio {
return hostOf(ctx).audio.load(path);
diff --git a/src/host/TimingAtlas.zig b/src/host/TimingAtlas.zig
index 4294b6a..a00a87e 100644
--- a/src/host/TimingAtlas.zig
+++ b/src/host/TimingAtlas.zig
@@ -19,7 +19,8 @@ const glyphs = blk: {
/// Called once with Painter on its canvas. Images owns the texture until shutdown.
pub fn init(images: *gfx.Images, painter: *gfx.Painter) !Atlas {
- const size = gfx.textSize(1, "0", .mono);
+ const run = gfx.textRun(.mono, 1, "0", &.{});
+ const size: [2]f32 = .{ run.width, run.height };
var atlas: Atlas = .{
.advance = size[0],
.cell_width = @ceil(size[0]) + padding * 2,
diff --git a/src/host/gfx.zig b/src/host/gfx.zig
index f84568a..d679067 100644
--- a/src/host/gfx.zig
+++ b/src/host/gfx.zig
@@ -613,15 +613,14 @@ fn fcolor(color: abi.Color) c.SDL_FColor {
};
}
-/// What `Command.text` covers at `scale`: the width of `bytes` and the
-/// height of its line, in canvas units (`Host.textSize`, `textRun`).
-pub fn textSize(scale: f32, bytes: []const u8, face: abi.Face) [2]f32 {
- if (!(scale > 0) or !std.math.isFinite(scale)) return .{ 0, 0 };
- var advance: i32 = 0;
+/// What `Command.text` gives `bytes` in `face` at `scale`, in canvas units:
+/// `Host.VTable.textRun`. `xs` is empty, or a byte each.
+pub fn textRun(face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun {
+ if (!(scale > 0) or !std.math.isFinite(scale) or (xs.len != 0 and xs.len != bytes.len)) return .{};
var ascent: i32 = 0;
var height: i32 = 0;
- _ = c.sk_text_shape(@backingInt(face), Glyphs.pixels(scale), bytes.ptr, bytes.len, null, 0, &advance, &ascent, &height);
- return .{ @floatFromInt(advance), @floatFromInt(height) };
+ const advance = c.sk_text_run(@backingInt(face), Glyphs.pixels(scale), bytes.ptr, bytes.len, if (xs.len == 0) null else xs.ptr, &ascent, &height);
+ return .{ .width = @floatFromInt(advance), .ascent = @floatFromInt(ascent), .height = @floatFromInt(height) };
}
fn premultiply(color: abi.Color) abi.Color {