diff options
Diffstat (limited to 'src/sketch/root.zig')
| -rw-r--r-- | src/sketch/root.zig | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 755e446..8a24417 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -40,9 +40,13 @@ pub const List = @import("list.zig").List; /// Data computed in draw from a few State fields, kept in Session and rebuilt when they change (or after a reload). pub const Derived = @import("derived.zig").Derived; pub const web = @import("web.zig"); +/// Text measured and laid out anywhere (`update` too): `size`, `layout`, +/// `font`, and the `Paragraph` `Frame.paragraph` draws. +pub const text = @import("text.zig"); /// sfxr's sound effects, rendered to samples for `host.newSound`. pub const sfxr = @import("sfxr.zig"); const request = @import("request.zig"); +const text_module = text; pub const Request = request.Request; pub const Handled = request.Handled; @@ -157,13 +161,23 @@ pub const Frame = struct { pub fn blend(f: *Frame, mode: BlendMode) void { f.emit(.{ .blend = mode }); } + /// Text from its top left corner, scale 1 a 13 px face; a `\n` starts + /// a line. `sk.text.size(.mono, scale, bytes)` is what it covers. 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) } }); + f.textIn(.mono, x, y, scale, bytes, color); } - /// `text` in another face of the built-in family: bold or italic mono, - /// or the sans (`host.textSizeIn` measures it). + /// `text` in another face: bold or italic mono, the sans, JetBrains + /// Mono NL, or a sketch's own (`sk.text.font`). 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 } }); + if (std.mem.indexOfScalar(u8, bytes, '\n') != null) { + _ = f.paragraph(x, y, bytes, .{ .face = face, .scale = scale, .color = paint(color) }); + } else f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes), .face = face } }); + } + /// A paragraph from its top left corner (`sk.text.Paragraph`: a width + /// to wrap at, alignment, spans, the glyphs revealed, a caret), drawn; + /// its `Layout` returned, as `sk.text.layout` gives it, from `x`, `y`. + pub fn paragraph(f: *Frame, x: f32, y: f32, bytes: []const u8, p: text_module.Paragraph) text_module.Layout { + return text_module.lay(f.keep(u8, bytes), p, x, y, f); } /// `textIn` laid out and rasterized at `raster` and drawn at `scale`, /// its position not snapped to a pixel: for text under a moving @@ -174,7 +188,7 @@ pub const Frame = struct { f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes), .face = face, .raster = raster } }); } 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) } }); + f.textIn(.mono, x, y, scale, f.format(fmt, args), color); } /// Formats into scratch memory. pub fn format(f: *Frame, comptime fmt: []const u8, args: anytype) []const u8 { @@ -369,6 +383,7 @@ pub fn Glue(comptime App: type, comptime made: Build) type { /// Calls `func` with the arguments its parameter types ask for. fn call(comptime func: anytype, comptime name: []const u8, offer: Offer) @typeInfo(@TypeOf(func)).@"fn".return_type.? { + text_module.host = offer.ctx.host; const F = @TypeOf(func); var args: std.meta.ArgsTuple(F) = undefined; inline for (@typeInfo(F).@"fn".param_types, 0..) |P, i| { @@ -461,6 +476,7 @@ test { _ = @import("list.zig"); _ = @import("derived.zig"); _ = sfxr; + _ = text; } test "setup receives session resources without a Frame and reports errors" { |
