1
0
Fork 0

feat: use i32 and u31 integer types

closes #2
This commit is contained in:
LordMZTE 2023-12-21 11:57:59 +01:00
parent 4be377ea7b
commit 80209aceac
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
14 changed files with 56 additions and 60 deletions

View file

@ -8,6 +8,6 @@ background: BackgroundStyle,
background_hovered: BackgroundStyle,
/// Spacing between the text and the button's outer bounds.
padding: usize,
padding: u31,
font_style: Style,

View file

@ -7,7 +7,7 @@ const Rectangle = @import("layout/Rectangle.zig");
pub const BackgroundStyle = union(enum) {
none,
fill: Color,
stroked: struct { stroke: Color, fill: ?Color = null, width: usize },
stroked: struct { stroke: Color, fill: ?Color = null, width: u31 },
pub fn drawBackground(self: BackgroundStyle, painter: *Painter, rect: Rectangle) !void {
switch (self) {

View file

@ -1,15 +1,15 @@
//! A 2-Dimensional position, typically used to denote where a widget is relative to the top left.
const Size = @import("Size.zig");
x: isize,
y: isize,
x: i32,
y: i32,
const Position = @This();
pub const zero = two(0);
/// Returns a Position where both components are identical.
pub inline fn two(pos: isize) Position {
pub inline fn two(pos: i32) Position {
return .{ .x = pos, .y = pos };
}

View file

@ -13,13 +13,13 @@ pub const zero = Rectangle{
};
/// Returns the area of this Rectangle.
pub inline fn area(self: Rectangle) usize {
pub inline fn area(self: Rectangle) u31 {
return self.size.area();
}
pub inline fn contains(self: Rectangle, pos: Position) bool {
return pos.x >= self.pos.x and
pos.y >= self.pos.y and
pos.x <= self.pos.x + @as(isize, @intCast(self.size.width)) and
pos.y <= self.pos.y + @as(isize, @intCast(self.size.height));
pos.x <= self.pos.x + self.size.width and
pos.y <= self.pos.y + self.size.height;
}

View file

@ -1,20 +1,20 @@
//! A size, usually used for widget sizes.
const Position = @import("Position.zig");
width: usize,
height: usize,
width: u31,
height: u31,
const Size = @This();
pub const zero = Size{ .width = 0, .height = 0 };
/// Returns a Size where both components are identical.
pub inline fn two(size: usize) Size {
pub inline fn two(size: u31) Size {
return .{ .width = size, .height = size };
}
/// Returns the area of this size.
pub inline fn area(self: Size) usize {
pub inline fn area(self: Size) u31 {
return self.width * self.height;
}

View file

@ -51,7 +51,7 @@ fn Prototype(comptime Self: type) type {
pub fn strokeRect(
self: *Self,
rectangle: Rectangle,
line_width: usize,
line_width: u31,
stroke_color: Color,
fill_color: ?Color,
) !void {
@ -92,7 +92,7 @@ fn Prototype(comptime Self: type) type {
try self.rect(.{
.pos = .{
.x = rectangle.pos.x,
.y = rectangle.pos.y + @as(isize, @intCast(rectangle.size.height - line_width)),
.y = rectangle.pos.y + rectangle.size.height - line_width,
},
.size = ud_size,
}, stroke_color);
@ -100,7 +100,7 @@ fn Prototype(comptime Self: type) type {
// right
try self.rect(.{
.pos = .{
.x = rectangle.pos.x + @as(isize, @intCast(rectangle.size.width - line_width)),
.x = rectangle.pos.x + rectangle.size.width - line_width,
.y = rectangle.pos.y,
},
.size = lr_size,
@ -110,8 +110,8 @@ fn Prototype(comptime Self: type) type {
if (fill_color) |fc| {
if (rectangle.area() > 0) {
try self.rect(.{
.pos = rectangle.pos.add(Position.two(@intCast(line_width))),
.size = rectangle.size.sub(Size.two(@intCast(line_width * 2))),
.pos = rectangle.pos.add(Position.two(line_width)),
.size = rectangle.size.sub(Size.two(line_width * 2)),
}, fc);
}
}

View file

@ -59,11 +59,7 @@ pub const LayoutOptions = struct {
/// Note that this does not guarantee that chunk's width will be within wrap_width,
/// as spans may have their wrap_mode set to .never or not fit within the wrap_width.
/// 0 means no wrapping.
wrap_width: usize = 0,
/// Spacing between lines. The chunk calculates line offset by adding this onto the maximum
/// yOffset of the spans.
line_spacing: usize = 0,
wrap_width: u31 = 0,
/// Also layout spans first.
layout_spans: bool = false,
@ -107,9 +103,9 @@ pub fn layout(self: *Chunk, opts: LayoutOptions) void {
// For spans with wrap_mode != .never, this is the distance to the end of this span,
// otherwise it is either the distance to the next span to satisfy this criterium
// or the length of all remaining spans after this one.
var width: isize = 0;
var width: i32 = 0;
for (self.spans.items[i..]) |lspan| {
width += @intCast(lspan.span.baseline_width);
width += lspan.span.baseline_width;
if (lspan.wrap_mode != .never) break;
}
@ -118,7 +114,7 @@ pub fn layout(self: *Chunk, opts: LayoutOptions) void {
};
if (should_wrap) {
cursor.y += @intCast(self.offsetLineByHeight(line_start_idx, i));
cursor.y += self.offsetLineByHeight(line_start_idx, i);
line_start_idx = i;
if (cursor.x > self.size.width) self.size.width = @intCast(cursor.x);
cursor.x = -span.span.origin_off.x;
@ -126,27 +122,27 @@ pub fn layout(self: *Chunk, opts: LayoutOptions) void {
span.position = .{
.x = cursor.x,
.y = cursor.y - @as(isize, @intCast(span.span.baseline_y)),
.y = cursor.y - span.span.baseline_y,
};
cursor.x += @intCast(span.span.baseline_width);
cursor.x += span.span.baseline_width;
}
cursor.y += @intCast(self.offsetLineByHeight(line_start_idx, self.spans.items.len));
cursor.y += self.offsetLineByHeight(line_start_idx, self.spans.items.len);
self.size.height = @intCast(cursor.y);
if (cursor.x > self.size.width) self.size.width = @intCast(cursor.x);
}
/// Offsets all chunks in the given range downwards by their line height and returns that line height.
fn offsetLineByHeight(self: *const Chunk, start_idx: usize, end_idx: usize) usize {
var max_height: usize = 0;
fn offsetLineByHeight(self: *const Chunk, start_idx: usize, end_idx: usize) u31 {
var max_height: u31 = 0;
for (self.spans.items[start_idx..end_idx]) |span| {
max_height = @max(max_height, span.span.font.yOffset(span.span.style.size));
}
for (self.spans.items[start_idx..end_idx]) |*span| {
span.position.y += @intCast(max_height);
span.position.y += max_height;
}
return max_height;

View file

@ -19,5 +19,5 @@ size: Size,
bearing: Position,
/// How much the cursor should move to the right after this glyph.
advance: usize,
advance: u31,

View file

@ -19,11 +19,11 @@ origin_off: Position = Position.zero,
/// The Y-coordinate of the baseline of this span relative to it's top.
/// The chunker uses this for aligning spans.
baseline_y: usize = 0,
baseline_y: u31 = 0,
/// The width of the baseline. This is calculated as the distance the cursor moved during layout.
/// This does not always correspond to renderSize().width due to padding between glyphs.
baseline_width: usize = 0,
baseline_width: u31 = 0,
const Span = @This();
@ -83,14 +83,14 @@ pub fn updateGlyphs(
/// Positions the glyphs of the span and sets baseline_y.
pub fn layout(self: *Span) void {
var cursor = Position.zero;
var min_y: isize = 0;
var min_y: i32 = 0;
for (self.glyphs.items) |*pglyph| {
pglyph.position = cursor.add(pglyph.glyph.bearing);
if (pglyph.glyph.bearing.y < min_y) min_y = pglyph.glyph.bearing.y;
cursor.x += @intCast(pglyph.glyph.advance);
cursor.x += pglyph.glyph.advance;
}
for (self.glyphs.items) |*pglyph| {
@ -112,11 +112,11 @@ pub fn deinit(self: Span) void {
pub fn renderSize(self: Span) Size {
if (self.glyphs.items.len == 0) return Size.zero;
var max = Position.two(std.math.minInt(isize));
var max = Position.two(std.math.minInt(i32));
for (self.glyphs.items) |glyph| {
max.y = @max(max.y, glyph.position.y + @as(isize, @intCast(glyph.glyph.size.height)));
max.x = @max(max.x, glyph.position.x + @as(isize, @intCast(glyph.glyph.size.width)));
max.y = @max(max.y, glyph.position.y + glyph.glyph.size.height);
max.x = @max(max.x, glyph.position.x + glyph.glyph.size.width);
}
return max.sub(self.origin_off).size();

View file

@ -2,7 +2,7 @@
const Color = @import("../Color.zig");
/// This is the Size of the font in pixels. Some backends (namely, TUI-based ones) may not support this.
size: usize = 24,
size: u31 = 24,
bold: bool = false,
italic: bool = false,

View file

@ -12,8 +12,8 @@ fn FontPrototype(comptime Self: type) type {
return struct {
/// For a given font size in pixels, returns the offset between lines.
pub fn yOffset(self: *Self, size: usize) usize {
return statspatch.implcall(self, .ptr, "yOffset", usize, .{size});
pub fn yOffset(self: *Self, size: u31) u31 {
return statspatch.implcall(self, .ptr, "yOffset", u31, .{size});
}
pub fn getGlyph(self: *Self, codepoint: u21, style: Style) !Glyph {

View file

@ -107,12 +107,12 @@ fn Prototype(comptime Self: type) type {
/// Returns the widget's flex expand factor, or 0 if it should not be expanded.
/// Simple wrappers should delegate this to their child.
/// Used by widgets such as Box to determine the size of their children.
pub fn getFlexExpand(self: Self) usize {
pub fn getFlexExpand(self: Self) u31 {
return statspatch.implcallOptional(
self,
.self,
"getFlexExpand",
usize,
u31,
.{self},
) orelse 0;
}

View file

@ -23,7 +23,7 @@ pub const ChildPositioning = enum {
pub const Child = struct {
widget: *Widget,
/// Offset from the start of the box. Used for positioning.
offset: usize = 0,
offset: u31 = 0,
pos: ChildPositioning,
};
@ -59,11 +59,11 @@ pub fn treevent(self: *Box, selfw: *Widget, tv: anytype) anyerror!void {
const slice = self.children.slice();
// The maximum size of the children in the direction orthogonal to that of the Box.
var max_orth_size: usize = if (self.orth_expand) switch (self.direction) {
var max_orth_size: u31 = if (self.orth_expand) switch (self.direction) {
.vertical => tv.constraints.max.width,
.horizontal => tv.constraints.max.height,
} else 0;
var cur_pos: usize = 0;
var cur_pos: u31 = 0;
// first pass, initial sizes
{
@ -133,7 +133,7 @@ pub fn treevent(self: *Box, selfw: *Widget, tv: anytype) anyerror!void {
if (maybe_fes) |fes| {
const child_cons = switch (self.direction) {
.vertical => v: {
const child_height = @as(usize, @intFromFloat(fes)) +
const child_height = @as(u31, @intFromFloat(fes)) +
child.data.size.height;
break :v layout.Constraints.tight(.{
@ -142,7 +142,7 @@ pub fn treevent(self: *Box, selfw: *Widget, tv: anytype) anyerror!void {
});
},
.horizontal => h: {
const child_width = @as(usize, @intFromFloat(fes)) +
const child_width = @as(u31, @intFromFloat(fes)) +
child.data.size.width;
break :h layout.Constraints.tight(.{
@ -189,27 +189,27 @@ pub fn treevent(self: *Box, selfw: *Widget, tv: anytype) anyerror!void {
) |child, offset, positioning| {
const child_pos = switch (self.direction) {
.vertical => switch (positioning) {
.left => .{ .x = tv.position.x, .y = tv.position.y + @as(isize, @intCast(offset)) },
.left => .{ .x = tv.position.x, .y = tv.position.y + offset },
.center => .{
.x = tv.position.x +
@as(isize, @intCast(@divTrunc(selfw.data.size.width, 2) - @divTrunc(child.data.size.width, 2))),
.y = tv.position.y + @as(isize, @intCast(offset)),
@divTrunc(selfw.data.size.width, 2) - @divTrunc(child.data.size.width, 2),
.y = tv.position.y + offset,
},
.right => .{
.x = tv.position.x + @as(isize, @intCast(selfw.data.size.width - child.data.size.width)),
.y = tv.position.y + @as(isize, @intCast(offset)),
.x = tv.position.x + selfw.data.size.width - child.data.size.width,
.y = tv.position.y + offset,
},
},
.horizontal => switch (positioning) {
.left => .{ .x = tv.position.x + @as(isize, @intCast(offset)), .y = tv.position.y },
.left => .{ .x = tv.position.x + offset, .y = tv.position.y },
.center => .{
.x = tv.position.x + @as(isize, @intCast(offset)),
.x = tv.position.x + offset,
.y = tv.position.y +
@as(isize, @intCast(@divTrunc(selfw.data.size.height, 2) - @divTrunc(child.data.size.height, 2))),
@divTrunc(selfw.data.size.height, 2) - @divTrunc(child.data.size.height, 2),
},
.right => .{
.x = tv.position.x + @as(isize, @intCast(offset)),
.y = tv.position.y + @as(isize, @intCast(selfw.data.size.height - child.data.size.height)),
.x = tv.position.x + offset,
.y = tv.position.y + selfw.data.size.height - child.data.size.height,
},
},
};

View file

@ -69,7 +69,7 @@ pub fn treevent(self: *Button, selfw: *Widget, tv: anytype) !void {
);
try tv.painter.span(
selfw.data.position.add(layout.Position.two(@intCast(style.padding))),
selfw.data.position.add(layout.Position.two(style.padding)),
self.span.?,
);
},