summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/abi.zig17
-rw-r--r--src/anim/root.zig12
-rw-r--r--src/host/Host.zig4
-rw-r--r--src/host/gfx.zig12
-rw-r--r--src/sketch/root.zig6
-rw-r--r--src/sketch/web.zig2
6 files changed, 40 insertions, 13 deletions
diff --git a/src/abi.zig b/src/abi.zig
index b30b7ae..327110d 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -57,6 +57,11 @@ pub const Color = extern struct {
}
};
+/// The face a text is set in: the Go family, bundled with the native host.
+/// Mono is for data and code, 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 };
+
/// How 2D drawing combines with what is already there (`Command.blend`).
/// Both are fixed-function on both hosts and match: `alpha` is source
/// over (canvas `source-over`, SDL `BLEND`), `add` adds the colour times
@@ -341,8 +346,8 @@ pub const Command = union(enum) {
/// The blend for the 2D commands after it (`rect`, `shape`, `image`;
/// `text` and `clear` ignore it). Each frame starts with `alpha`.
blend: BlendMode,
- /// Fixed 8px cells, `scale` times larger.
- text: struct { x: f32, y: f32, scale: f32, color: Color, bytes: []const u8 },
+ /// One line from its top left corner; scale 1 is a 13 px font.
+ text: struct { x: f32, y: f32, scale: f32, color: Color, bytes: []const u8, face: Face = .mono },
clip: ?Rect,
/// Logical canvas size; the host scales the window to fit.
canvas: struct { w: u32, h: u32 },
@@ -583,7 +588,7 @@ pub const Host = struct {
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) [2]f32,
+ textSize: *const fn (*anyopaque, f32, []const u8, Face) [2]f32,
/// `.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
@@ -637,7 +642,11 @@ pub const Host = struct {
/// it is 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);
+ return h.vtable.textSize(h.ctx, scale, bytes, .mono);
+ }
+ /// `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);
}
pub fn loadSound(h: Host, path: []const u8) Audio {
return h.vtable.loadSound(h.ctx, path);
diff --git a/src/anim/root.zig b/src/anim/root.zig
index 00e9362..a676f38 100644
--- a/src/anim/root.zig
+++ b/src/anim/root.zig
@@ -186,6 +186,18 @@ pub const Placed = struct {
f.text(at[0], at[1], scale * p.scale, bytes, p.faded(ink));
}
+ /// `text` in another face (`sk.Face`: bold or italic mono, the sans).
+ pub fn textIn(p: Placed, f: *sk.Frame, face: sk.Face, x: f32, y: f32, scale: f32, bytes: []const u8, ink: sk.Color) void {
+ const at = p.xf.apply(.{ x, y });
+ f.textIn(face, at[0], at[1], scale * p.scale, bytes, p.faded(ink));
+ }
+
+ /// The width and line height `textIn` covers, in the object's units.
+ pub fn textSizeIn(p: Placed, f: *sk.Frame, face: sk.Face, scale: f32, bytes: []const u8) [2]f32 {
+ const size = f.host.textSizeIn(face, scale * p.scale, bytes);
+ return .{ size[0] / p.scale, size[1] / p.scale };
+ }
+
/// The width and line height `text` covers, in the object's units.
pub fn textSize(p: Placed, f: *sk.Frame, scale: f32, bytes: []const u8) [2]f32 {
const size = f.host.textSize(scale * p.scale, bytes);
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 4b5f2a4..86e1873 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -834,8 +834,8 @@ 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) [2]f32 {
- return gfx.textSize(hostOf(ctx).renderer, scale, bytes);
+fn apiTextSize(ctx: *anyopaque, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 {
+ return gfx.textSize(hostOf(ctx).renderer, scale, bytes, face);
}
fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio {
return hostOf(ctx).audio.load(path);
diff --git a/src/host/gfx.zig b/src/host/gfx.zig
index 0466f27..2aac380 100644
--- a/src/host/gfx.zig
+++ b/src/host/gfx.zig
@@ -239,7 +239,7 @@ pub const Painter = struct {
.image => |img| p.drawImage(img.image, img.dst, img.src, img.tint, img.corners),
.text => |text| {
_ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND);
- drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color);
+ drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face);
},
.clip => |clip| {
if (clip) |r| {
@@ -527,8 +527,8 @@ fn fcolor(color: abi.Color) c.SDL_FColor {
/// Native shaped text on Linux, using a 13px monospace face at scale 1.
/// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops.
-pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color) void {
- if (@import("builtin").os.tag == .linux and c.sk_text_draw(renderer, x, y, scale, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return;
+pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color, face: abi.Face) void {
+ if (@import("builtin").os.tag == .linux and c.sk_text_draw_face(renderer, @intFromEnum(face), x, y, scale, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return;
var buf: [1024]u8 = undefined;
const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1);
@memcpy(buf[0..len], bytes[0..len]);
@@ -558,9 +558,9 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []
/// What `drawText` gives `bytes` at `scale`: its width and the height of
/// its line, in canvas units (`Host.textSize`).
-pub fn textSize(renderer: *c.SDL_Renderer, scale: f32, bytes: []const u8) [2]f32 {
+pub fn textSize(renderer: *c.SDL_Renderer, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 {
var size: [2]f32 = .{ 0, 0 };
- if (@import("builtin").os.tag == .linux and c.sk_text_measure(renderer, scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size;
+ if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @intFromEnum(face), scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size;
// The debug font's 8 px cells.
const s = @max(scale, 1);
return .{ @as(f32, @floatFromInt(bytes.len)) * 8 * s, 8 * s };
@@ -575,7 +575,7 @@ fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void {
_ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND);
setColor(renderer, .{ .r = 110, .g = 30, .b = 70 });
_ = c.SDL_RenderFillRect(renderer, &frect(dst));
- drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 });
+ drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 }, .mono);
}
fn setColor(renderer: *c.SDL_Renderer, color: abi.Color) void {
diff --git a/src/sketch/root.zig b/src/sketch/root.zig
index 33c9ba7..01aa977 100644
--- a/src/sketch/root.zig
+++ b/src/sketch/root.zig
@@ -57,6 +57,7 @@ pub const Buffer = abi.Buffer;
pub const Cursor = abi.Cursor;
pub const Voice = abi.Voice;
pub const BlendMode = abi.BlendMode;
+pub const Face = abi.Face;
/// What `draw` receives: input, host services, memory by lifetime and the
/// command list. Commands may be built from anything: the methods copy what
@@ -146,6 +147,11 @@ pub const Frame = struct {
pub fn text(f: *Frame, x: f32, y: f32, scale: f32, bytes: []const u8, color: anytype) void {
f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes) } });
}
+ /// `text` in another face of the built-in family: bold or italic mono,
+ /// or the sans (`host.textSizeIn` measures it).
+ pub fn textIn(f: *Frame, face: Face, x: f32, y: f32, scale: f32, bytes: []const u8, color: anytype) void {
+ f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes), .face = face } });
+ }
pub fn print(f: *Frame, x: f32, y: f32, scale: f32, color: anytype, comptime fmt: []const u8, args: anytype) void {
f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.format(fmt, args) } });
}
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 4445e8d..db9d9a8 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -431,7 +431,7 @@ fn Driver(comptime App: type) type {
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) [2]f32 {
+ 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 };
}