summaryrefslogtreecommitdiff
path: root/src/abi.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/abi.zig')
-rw-r--r--src/abi.zig40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/abi.zig b/src/abi.zig
index 6411b0d..e5fe372 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -62,9 +62,15 @@ pub const Color = extern struct {
/// The face a text is set in: the Go family, and JetBrains Mono NL (`code`,
/// without ligatures; its medium weight for small sizes on a dark ground)
/// for programs, all bundled with the native host. Mono is for data, sans
-/// for what is said about it. The web host has one face, the page's
-/// `monospace`.
-pub const Face = enum(u8) { mono, mono_bold, mono_italic, sans, sans_medium, sans_bold, code, code_bold, code_italic, code_medium };
+/// for what is said about it. Past these ten, a sketch's own fonts
+/// (`Host.VTable.font`). The web host draws them in the page's
+/// `monospace` and `sans-serif`.
+pub const Face = enum(u8) { mono, mono_bold, mono_italic, sans, sans_medium, sans_bold, code, code_bold, code_italic, code_medium, _ };
+
+/// A run of text in one face at one scale, measured (`Host.VTable.textRun`):
+/// its advance, and its line box (the ascent, the baseline from the top, and
+/// the height), in canvas units.
+pub const TextRun = struct { width: f32 = 0, ascent: f32 = 0, height: f32 = 0 };
/// How 2D drawing combines with what is already there (`Command.blend`).
/// Both are fixed-function on both hosts and match: `alpha` is source
@@ -646,9 +652,15 @@ pub const Host = struct {
writeBuffer: *const fn (*anyopaque, Buffer, u32, []const u8) anyerror!void,
/// Zero while unknown.
imageSize: *const fn (*anyopaque, Image) [2]u32,
- /// What `Command.text` gives the bytes at a scale: the width they
- /// advance and the height of their line, in canvas units.
- textSize: *const fn (*anyopaque, f32, []const u8, Face) [2]f32,
+ /// What `Command.text` gives the bytes in a face at a scale, as data:
+ /// a pure function of the font, the scale and the bytes, never of the
+ /// display. With the last slice (empty, or one a byte), each byte's x
+ /// from the run's start; a byte inside a cluster has its cluster's.
+ /// `sketch.text` lays paragraphs out over it.
+ textRun: *const fn (*anyopaque, Face, f32, []const u8, []f32) TextRun,
+ /// A face for TTF or OTF bytes, copied: the same face for the same
+ /// bytes. Null when they are not a font.
+ font: *const fn (*anyopaque, []const u8) ?Face,
/// `.none` if the file does not load.
loadSound: *const fn (*anyopaque, []const u8) Audio,
/// WAV, FLAC or Ogg Vorbis bytes in memory. Not cached. `.none` if
@@ -705,16 +717,18 @@ pub const Host = struct {
return h.vtable.imageSize(h.ctx, image);
}
/// The size `f.text(x, y, scale, bytes, ...)` covers from `x, y`: width
- /// and line height in canvas units, for laying text out (in `draw`:
- /// `update` gets no host). Native text is shaped and hinted at the size
- /// it is drawn, so a width is not a multiple of the scale: measure at
- /// the scale drawn.
+ /// and line height in canvas units, of one line (`sketch.text.size`
+ /// measures lines, anywhere, `update` too). Advances are whole pixels
+ /// at the size drawn, so a width is not a multiple of the scale: measure
+ /// at the scale drawn.
pub fn textSize(h: Host, scale: f32, bytes: []const u8) [2]f32 {
- return h.vtable.textSize(h.ctx, scale, bytes, .mono);
+ const run = h.vtable.textRun(h.ctx, .mono, scale, bytes, &.{});
+ return .{ run.width, run.height };
}
/// `textSize` for `f.textIn(face, ...)`.
pub fn textSizeIn(h: Host, face: Face, scale: f32, bytes: []const u8) [2]f32 {
- return h.vtable.textSize(h.ctx, scale, bytes, face);
+ const run = h.vtable.textRun(h.ctx, face, scale, bytes, &.{});
+ return .{ run.width, run.height };
}
pub fn loadSound(h: Host, path: []const u8) Audio {
return h.vtable.loadSound(h.ctx, path);
@@ -886,7 +900,7 @@ pub const hash: u64 = std.hash.Wyhash.hash(@import("abi_hash").layout, build);
pub const layout_hash: u64 = blk: {
@setEvalBranchQuota(1_000_000);
var h: u64 = 0;
- for (.{ Rect, Color, BlendMode, Corners, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, gpu.PostProcess, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| {
+ for (.{ Rect, Color, Face, TextRun, BlendMode, Corners, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, gpu.PostProcess, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| {
h = std.hash.Wyhash.hash(h, @typeName(T) ++ layout.signature(T));
}
break :blk h;