tiny.gui.paint.regions
Defined in paint.
API (11)
Actions
Public operations.
Types and contracts
Public types and contracts.
Values and defaults
Public values and defaults.
Source
Source: lib/gui/src/paint/regions.zig
zig
const paint = @import("root.zig");const Bounds = paint.cpu.Bounds;const Rect = @import("../root.zig").layout.Rect;const Region = paint.cpu.Region;pub const capacity: usize = 8;pub const Set = struct { items: [capacity]Region = undefined, len: usize = 0, pub fn clear(self: *Set) void { self.len = 0; } pub fn append(self: *Set, region: Region) void { if (self.len < self.items.len) { self.items[self.len] = region; self.len += 1; return; } const collapsed = unite(self.bounding().?, region); self.items[0] = collapsed; self.len = 1; } pub fn slice(self: *const Set) []const Region { return self.items[0..self.len]; } pub fn pixelCount(self: Set) usize { var count: usize = 0; for (self.items[0..self.len]) |region| { count +|= region.pixelCount(); } return count; } pub fn bounding(self: Set) ?Region { if (self.len == 0) return null; var result = self.items[0]; for (self.items[1..self.len]) |region| { result = unite(result, region); } return result; }};pub fn fromBounds( bounds: Bounds, margin: u32, width: u32, height: u32,) Region { const x0 = @min(bounds.x0 -| margin, width); const y0 = @min(bounds.y0 -| margin, height); const x1 = @min(bounds.x1 +| margin, width); const y1 = @min(bounds.y1 +| margin, height); if (x0 >= x1 or y0 >= y1) return .{ .x = x0, .y = y0 }; return .{ .x = x0, .y = y0, .width = x1 - x0, .height = y1 - y0, };}pub fn fromRect( rect: Rect, margin: u32, width: u32, height: u32,) ?Region { if (rect.width <= 0 or rect.height <= 0) return null; const geometry = paint.cpu.geometry; const x0 = geometry.floorClamp(rect.x, width); const y0 = geometry.floorClamp(rect.y, height); const x1 = geometry.ceilClamp(rect.x + rect.width, width); const y1 = geometry.ceilClamp(rect.y + rect.height, height); if (x0 >= x1 or y0 >= y1) return null; return fromBounds(.{ .x0 = x0, .y0 = y0, .x1 = x1, .y1 = y1 }, margin, width, height);}pub fn unite(left: Region, right: Region) Region { const x0 = @min(left.x, right.x); const y0 = @min(left.y, right.y); const x1 = @max(left.x +| left.width, right.x +| right.width); const y1 = @max(left.y +| left.height, right.y +| right.height); return .{ .x = x0, .y = y0, .width = x1 - x0, .height = y1 - y0, };}pub fn intersect(left: Region, right: Region) Region { const x0 = @max(left.x, right.x); const y0 = @max(left.y, right.y); const x1 = @min(left.x +| left.width, right.x +| right.width); const y1 = @min(left.y +| left.height, right.y +| right.height); if (x0 >= x1 or y0 >= y1) return .{ .x = x0, .y = y0 }; return .{ .x = x0, .y = y0, .width = x1 - x0, .height = y1 - y0, };}test "region set collapses overflow without losing damage" { var regions = Set{}; for (0..capacity + 1) |index| { regions.append(.{ .x = @intCast(index * 2), .width = 1, .height = 1, }); } try @import("std").testing.expectEqual(@as(usize, 1), regions.len); try @import("std").testing.expectEqual( Region{ .x = 0, .width = capacity * 2 + 1, .height = 1 }, regions.bounding().?, );}test "bounds outside the target collapse to an empty edge region" { const std = @import("std"); try std.testing.expectEqual( Region{ .x = 16, .y = 4 }, fromBounds( .{ .x0 = 20, .y0 = 4, .x1 = 30, .y1 = 8 }, 0, 16, 16, ), ); try std.testing.expectEqual( Region{ .x = 12, .y = 4 }, fromBounds( .{ .x0 = 12, .y0 = 4, .x1 = 8, .y1 = 8 }, 0, 16, 16, ), );}Source: lib/gui/src/paint/root.zig:9
zig
pub const regions = @import("regions.zig");Audit
| Definitions | 12 |
|---|---|
| Public names | 18 |
| Members | 2 |
| Version | 26.7.0 |
| Revision | daab053ee433 |