summaryrefslogtreecommitdiff
path: root/src/sketch/web.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/web.zig')
-rw-r--r--src/sketch/web.zig25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 804a4df..e12acb3 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -15,6 +15,11 @@ const js = struct {
extern "host" fn newImage(w: u32, h: u32) u32;
extern "host" fn freeImage(image: u32) void;
extern "host" fn clock() f64;
+ /// canvas measureText: the run's width, ascent and height into `out`,
+ /// and with `xs_len` (the run's length) each byte's x.
+ extern "host" fn textRun(face: u32, scale: f32, ptr: [*]const u8, len: usize, xs: [*]f32, xs_len: usize, out: *[3]f32) void;
+ /// A FontFace from the bytes: its face, or -1.
+ extern "host" fn font(ptr: [*]const u8, len: usize) i32;
extern "host" fn openFile(command: [*]const u8, command_len: usize, pattern: [*]const u8, pattern_len: usize) void;
extern "canvas" fn clear(r: u32, g: u32, b: u32, a: u32) void;
extern "canvas" fn fill(x: f32, y: f32, w: f32, h: f32, r: u32, g: u32, b: u32, a: u32) void;
@@ -30,7 +35,7 @@ const js = struct {
extern "canvas" fn target(id: u32) void;
/// `abi.BlendMode` by value.
extern "canvas" fn blend(mode: u32) void;
- extern "canvas" fn text(x: f32, y: f32, scale: f32, ptr: [*]const u8, len: usize, r: u32, g: u32, b: u32, a: u32) void;
+ extern "canvas" fn text(x: f32, y: f32, scale: f32, face: u32, ptr: [*]const u8, len: usize, r: u32, g: u32, b: u32, a: u32) void;
extern "canvas" fn clip(enabled: u32, x: f32, y: f32, w: f32, h: f32) void;
extern "canvas" fn canvas(w: u32, h: u32) void;
extern "canvas" fn cursor(kind: u32) void;
@@ -207,7 +212,7 @@ fn Driver(comptime App: type) type {
.shape => |v| js.shape(v.points.ptr, v.points.len, v.ends.ptr, v.ends.len, v.colors.ptr, v.colors.len, v.color.r, v.color.g, v.color.b, v.color.a),
.target => |v| js.target(@backingInt(v)),
.blend => |v| js.blend(@backingInt(v)),
- .text => |v| js.text(v.x, v.y, v.scale, v.bytes.ptr, v.bytes.len, v.color.r, v.color.g, v.color.b, v.color.a),
+ .text => |v| js.text(v.x, v.y, v.scale, @backingInt(v.face), v.bytes.ptr, v.bytes.len, v.color.r, v.color.g, v.color.b, v.color.a),
.clip => |v| if (v) |r| js.clip(1, r.x, r.y, r.w, r.h) else js.clip(0, 0, 0, 0, 0),
.canvas => |v| {
input.width = @floatFromInt(v.w);
@@ -409,7 +414,8 @@ fn Driver(comptime App: type) type {
.freeBuffer = freeBuffer,
.writeBuffer = writeBuffer,
.imageSize = imageSize,
- .textSize = textSize,
+ .textRun = textRun,
+ .font = font,
.loadSound = loadSound,
.decodeSound = unsupportedSound,
.newSound = newSound,
@@ -449,10 +455,15 @@ fn Driver(comptime App: type) type {
fn imageSize(_: *anyopaque, img: abi.Image) [2]u32 {
return .{ js.imageWidth(@backingInt(img)), js.imageHeight(@backingInt(img)) };
}
- /// As host.js draws text: 8 px a character and a 16 px line at scale 1.
- fn textSize(_: *anyopaque, scale: f32, bytes: []const u8, _: abi.Face) [2]f32 {
- const count = std.unicode.utf8CountCodepoints(bytes) catch bytes.len;
- return .{ @as(f32, @floatFromInt(count)) * 8 * scale, 16 * scale };
+ /// As host.js draws text: canvas measureText, in the page's fonts.
+ fn textRun(_: *anyopaque, face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun {
+ var out: [3]f32 = .{ 0, 0, 0 };
+ js.textRun(@backingInt(face), scale, bytes.ptr, bytes.len, xs.ptr, xs.len, &out);
+ return .{ .width = out[0], .ascent = out[1], .height = out[2] };
+ }
+ fn font(_: *anyopaque, bytes: []const u8) ?abi.Face {
+ const face = js.font(bytes.ptr, bytes.len);
+ return if (face < 0) null else @enumFromInt(face);
}
/// Only sounds the page has decoded (opened or dropped files): the
/// browser decodes asynchronously, so there is no reading on demand.