diff --git a/src/renderer.zig b/src/renderer.zig index d60649a..92f6a99 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -252,6 +252,59 @@ pub const Renderer = struct { try self.renderNode(alloc, nodes, child, writer, null, parent_content); } }, + .widthratio => { + var divisor: Value = Value{ .float = 1.0 }; + var float_divisor: f64 = 1.0; + + var value: Value = Value{ .float = 1.0 }; + var float_value: f64 = 1.0; + + var max_value: Value = Value{ .float = 1.0 }; + var float_max_value: f64 = 1.0; + + if (!std.mem.eql(u8, node.widthratio.?.value, "")) { + value = Value{ .string = node.widthratio.?.value }; + if (self.context.get(node.widthratio.?.value)) |v| { + value = v; + } + float_value = switch (value) { + .int => @as(f64, @floatFromInt(value.int)), + .float => value.float, + .string => std.fmt.parseFloat(f64, value.string) catch 1.0, + else => 1.0, + }; + } + + if (!std.mem.eql(u8, node.widthratio.?.max_value, "")) { + max_value = Value{ .string = node.widthratio.?.max_value }; + if (self.context.get(node.widthratio.?.max_value)) |v| { + max_value = v; + } + float_max_value = switch (max_value) { + .int => @as(f64, @floatFromInt(max_value.int)), + .float => max_value.float, + .string => std.fmt.parseFloat(f64, max_value.string) catch 1.0, + else => 1.0, + }; + } + + if (node.widthratio.?.divisor) |div| { + divisor = Value{ .string = div }; + if (self.context.get(div)) |d| { + divisor = d; + } + float_divisor = switch (divisor) { + .int => @as(f64, @floatFromInt(divisor.int)), + .float => divisor.float, + .string => std.fmt.parseFloat(f64, divisor.string) catch 0.0, + else => 1.0, + }; + } + + const ratio = (float_value / float_max_value) * float_divisor; + + try writer.writeAll(std.fmt.allocPrint(alloc, "{d}", .{ratio}) catch "0"); + }, else => {}, } }