Replace Rust host-native with 100% pure-MoonBit Wayland compositor
CI / test (push) Successful in 36s
CI / test (push) Successful in 36s
This commit is contained in:
1 parent
17a8a46cfd
commit
ae4f3fa93c
31 files changed
+3647
-4962
No files matched your search
@@ -0,0 +1,668 @@
|
||||
///| Pure MoonBit ARGB8888/RGBA8888 software rasterizer, wl_shm blitter, and PNG/PPM encoder.
|
||||
|
||||
///|
|
||||
pub(all) struct Pixmap {
|
||||
width : Int
|
||||
height : Int
|
||||
/// RGBA8888 byte buffer (4 bytes per pixel: R, G, B, A)
|
||||
data : FixedArray[Byte]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Pixmap::new(width : Int, height : Int) -> Pixmap {
|
||||
let w = if width < 1 { 1 } else { width }
|
||||
let h = if height < 1 { 1 } else { height }
|
||||
{ width: w, height: h, data: FixedArray::make(w * h * 4, b'\x00') }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Pixmap::pixel(self : Pixmap, x : Int, y : Int) -> @scene.Rgba? {
|
||||
if x < 0 || y < 0 || x >= self.width || y >= self.height {
|
||||
return None
|
||||
}
|
||||
let off = (y * self.width + x) * 4
|
||||
Some({
|
||||
r: self.data[off],
|
||||
g: self.data[off + 1],
|
||||
b: self.data[off + 2],
|
||||
a: self.data[off + 3],
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
fn blend_pixel(
|
||||
dst : Pixmap,
|
||||
x : Int,
|
||||
y : Int,
|
||||
r : Int,
|
||||
g : Int,
|
||||
b : Int,
|
||||
a : Int,
|
||||
clip : @scene.Rect?,
|
||||
) -> Unit {
|
||||
if x < 0 || y < 0 || x >= dst.width || y >= dst.height || a <= 0 {
|
||||
return
|
||||
}
|
||||
match clip {
|
||||
Some(c) =>
|
||||
if x < c.x || y < c.y || x >= c.x + c.w || y >= c.y + c.h {
|
||||
return
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
let off = (y * dst.width + x) * 4
|
||||
if a >= 255 {
|
||||
dst.data[off] = r.to_byte()
|
||||
dst.data[off + 1] = g.to_byte()
|
||||
dst.data[off + 2] = b.to_byte()
|
||||
dst.data[off + 3] = b'\xff'
|
||||
return
|
||||
}
|
||||
let inv = 255 - a
|
||||
let dr = dst.data[off].to_int()
|
||||
let dg = dst.data[off + 1].to_int()
|
||||
let db = dst.data[off + 2].to_int()
|
||||
let da = dst.data[off + 3].to_int()
|
||||
dst.data[off] = ((r * a + dr * inv + 127) / 255).to_byte()
|
||||
dst.data[off + 1] = ((g * a + dg * inv + 127) / 255).to_byte()
|
||||
dst.data[off + 2] = ((b * a + db * inv + 127) / 255).to_byte()
|
||||
dst.data[off + 3] = (a + (da * inv + 127) / 255).clamp(min=0, max=255).to_byte()
|
||||
}
|
||||
|
||||
///|
|
||||
fn in_rounded_rect(
|
||||
px : Int,
|
||||
py : Int,
|
||||
rx : Int,
|
||||
ry : Int,
|
||||
rw : Int,
|
||||
rh : Int,
|
||||
rad : Int,
|
||||
) -> Bool {
|
||||
if px < rx || py < ry || px >= rx + rw || py >= ry + rh {
|
||||
return false
|
||||
}
|
||||
if rad <= 0 {
|
||||
return true
|
||||
}
|
||||
let max_r = (if rw < rh { rw } else { rh }) / 2
|
||||
let r = if rad > max_r { max_r } else { rad }
|
||||
if r <= 0 {
|
||||
return true
|
||||
}
|
||||
let x_left = rx + r
|
||||
let x_right = rx + rw - r - 1
|
||||
let y_top = ry + r
|
||||
let y_bot = ry + rh - r - 1
|
||||
if px < x_left && py < y_top {
|
||||
let dx = x_left - px
|
||||
let dy = y_top - py
|
||||
dx * dx + dy * dy <= r * r
|
||||
} else if px > x_right && py < y_top {
|
||||
let dx = px - x_right
|
||||
let dy = y_top - py
|
||||
dx * dx + dy * dy <= r * r
|
||||
} else if px < x_left && py > y_bot {
|
||||
let dx = x_left - px
|
||||
let dy = py - y_bot
|
||||
dx * dx + dy * dy <= r * r
|
||||
} else if px > x_right && py > y_bot {
|
||||
let dx = px - x_right
|
||||
let dy = py - y_bot
|
||||
dx * dx + dy * dy <= r * r
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn fill_rounded_rect(
|
||||
dst : Pixmap,
|
||||
rect : @scene.Rect,
|
||||
color : @scene.Rgba,
|
||||
radius : Int,
|
||||
clip : @scene.Rect?,
|
||||
) -> Unit {
|
||||
if rect.w <= 0 || rect.h <= 0 || color.a.to_int() == 0 {
|
||||
return
|
||||
}
|
||||
let x0 = if rect.x < 0 { 0 } else { rect.x }
|
||||
let y0 = if rect.y < 0 { 0 } else { rect.y }
|
||||
let x1 = if rect.x + rect.w > dst.width { dst.width } else { rect.x + rect.w }
|
||||
let y1 = if rect.y + rect.h > dst.height {
|
||||
dst.height
|
||||
} else {
|
||||
rect.y + rect.h
|
||||
}
|
||||
let cr = color.r.to_int()
|
||||
let cg = color.g.to_int()
|
||||
let cb = color.b.to_int()
|
||||
let ca = color.a.to_int()
|
||||
for y = y0; y < y1; y = y + 1 {
|
||||
for x = x0; x < x1; x = x + 1 {
|
||||
if radius == 0 ||
|
||||
in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius) {
|
||||
blend_pixel(dst, x, y, cr, cg, cb, ca, clip)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn stroke_rounded_rect(
|
||||
dst : Pixmap,
|
||||
rect : @scene.Rect,
|
||||
color : @scene.Rgba,
|
||||
width : Int,
|
||||
radius : Int,
|
||||
clip : @scene.Rect?,
|
||||
) -> Unit {
|
||||
if rect.w <= 0 || rect.h <= 0 || width <= 0 || color.a.to_int() == 0 {
|
||||
return
|
||||
}
|
||||
let x0 = if rect.x < 0 { 0 } else { rect.x }
|
||||
let y0 = if rect.y < 0 { 0 } else { rect.y }
|
||||
let x1 = if rect.x + rect.w > dst.width { dst.width } else { rect.x + rect.w }
|
||||
let y1 = if rect.y + rect.h > dst.height {
|
||||
dst.height
|
||||
} else {
|
||||
rect.y + rect.h
|
||||
}
|
||||
let cr = color.r.to_int()
|
||||
let cg = color.g.to_int()
|
||||
let cb = color.b.to_int()
|
||||
let ca = color.a.to_int()
|
||||
let inner_x = rect.x + width
|
||||
let inner_y = rect.y + width
|
||||
let inner_w = rect.w - width * 2
|
||||
let inner_h = rect.h - width * 2
|
||||
let inner_r = if radius > width { radius - width } else { 0 }
|
||||
for y = y0; y < y1; y = y + 1 {
|
||||
for x = x0; x < x1; x = x + 1 {
|
||||
let in_outer = in_rounded_rect(
|
||||
x,
|
||||
y,
|
||||
rect.x,
|
||||
rect.y,
|
||||
rect.w,
|
||||
rect.h,
|
||||
radius,
|
||||
)
|
||||
let in_inner = inner_w > 0 &&
|
||||
inner_h > 0 &&
|
||||
in_rounded_rect(x, y, inner_x, inner_y, inner_w, inner_h, inner_r)
|
||||
if in_outer && !in_inner {
|
||||
blend_pixel(dst, x, y, cr, cg, cb, ca, clip)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn draw_line(
|
||||
dst : Pixmap,
|
||||
x0_in : Int,
|
||||
y0_in : Int,
|
||||
x1_in : Int,
|
||||
y1_in : Int,
|
||||
color : @scene.Rgba,
|
||||
width : Int,
|
||||
clip : @scene.Rect?,
|
||||
) -> Unit {
|
||||
let cr = color.r.to_int()
|
||||
let cg = color.g.to_int()
|
||||
let cb = color.b.to_int()
|
||||
let ca = color.a.to_int()
|
||||
let half = if width <= 1 { 0 } else { width / 2 }
|
||||
let mut x0 = x0_in
|
||||
let mut y0 = y0_in
|
||||
let dx = (x1_in - x0_in).abs()
|
||||
let sx = if x0_in < x1_in { 1 } else { -1 }
|
||||
let dy = -(y1_in - y0_in).abs()
|
||||
let sy = if y0_in < y1_in { 1 } else { -1 }
|
||||
let mut err = dx + dy
|
||||
while true {
|
||||
for oy = -half; oy <= half; oy = oy + 1 {
|
||||
for ox = -half; ox <= half; ox = ox + 1 {
|
||||
blend_pixel(dst, x0 + ox, y0 + oy, cr, cg, cb, ca, clip)
|
||||
}
|
||||
}
|
||||
if x0 == x1_in && y0 == y1_in {
|
||||
break
|
||||
}
|
||||
let e2 = 2 * err
|
||||
if e2 >= dy {
|
||||
err = err + dy
|
||||
x0 = x0 + sx
|
||||
}
|
||||
if e2 <= dx {
|
||||
err = err + dx
|
||||
y0 = y0 + sy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn draw_circle(
|
||||
dst : Pixmap,
|
||||
cx : Int,
|
||||
cy : Int,
|
||||
radius : Int,
|
||||
color : @scene.Rgba,
|
||||
filled : Bool,
|
||||
clip : @scene.Rect?,
|
||||
) -> Unit {
|
||||
if radius <= 0 {
|
||||
return
|
||||
}
|
||||
let cr = color.r.to_int()
|
||||
let cg = color.g.to_int()
|
||||
let cb = color.b.to_int()
|
||||
let ca = color.a.to_int()
|
||||
let r2 = radius * radius
|
||||
let inner_r2 = if radius > 1 { (radius - 1) * (radius - 1) } else { 0 }
|
||||
for dy = -radius; dy <= radius; dy = dy + 1 {
|
||||
for dx = -radius; dx <= radius; dx = dx + 1 {
|
||||
let d2 = dx * dx + dy * dy
|
||||
if d2 <= r2 && (filled || d2 >= inner_r2) {
|
||||
blend_pixel(dst, cx + dx, cy + dy, cr, cg, cb, ca, clip)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Minimal 5x7 ASCII bitmap glyph rows (5 bits wide, 7 rows).
|
||||
fn glyph_rows(ch : Int) -> Array[Int] {
|
||||
match ch {
|
||||
// Space
|
||||
32 => [0, 0, 0, 0, 0, 0, 0]
|
||||
// Digits 0-9
|
||||
48 => [14, 17, 19, 21, 25, 17, 14]
|
||||
49 => [4, 12, 4, 4, 4, 4, 14]
|
||||
50 => [14, 17, 1, 2, 4, 8, 31]
|
||||
51 => [31, 2, 4, 2, 1, 17, 14]
|
||||
52 => [2, 6, 10, 18, 31, 2, 2]
|
||||
53 => [31, 16, 30, 1, 1, 17, 14]
|
||||
54 => [6, 8, 16, 30, 17, 17, 14]
|
||||
55 => [31, 1, 2, 4, 8, 8, 8]
|
||||
56 => [14, 17, 17, 14, 17, 17, 14]
|
||||
57 => [14, 17, 17, 15, 1, 2, 12]
|
||||
// Uppercase A-Z (mapped for both A-Z and a-z fallback if needed, plus lowercase)
|
||||
65 => [14, 17, 17, 31, 17, 17, 17] // A
|
||||
66 => [30, 17, 17, 30, 17, 17, 30] // B
|
||||
67 => [14, 17, 16, 16, 16, 17, 14] // C
|
||||
68 => [28, 18, 17, 17, 17, 18, 28] // D
|
||||
69 => [31, 16, 16, 30, 16, 16, 31] // E
|
||||
70 => [31, 16, 16, 30, 16, 16, 16] // F
|
||||
71 => [14, 17, 16, 23, 17, 17, 15] // G
|
||||
72 => [17, 17, 17, 31, 17, 17, 17] // H
|
||||
73 => [14, 4, 4, 4, 4, 4, 14] // I
|
||||
74 => [7, 2, 2, 2, 2, 18, 12] // J
|
||||
75 => [17, 18, 20, 24, 20, 18, 17] // K
|
||||
76 => [16, 16, 16, 16, 16, 16, 31] // L
|
||||
77 => [17, 27, 21, 21, 17, 17, 17] // M
|
||||
78 => [17, 17, 25, 21, 19, 17, 17] // N
|
||||
79 => [14, 17, 17, 17, 17, 17, 14] // O
|
||||
80 => [30, 17, 17, 30, 16, 16, 16] // P
|
||||
81 => [14, 17, 17, 17, 21, 18, 13] // Q
|
||||
82 => [30, 17, 17, 30, 20, 18, 17] // R
|
||||
83 => [15, 16, 16, 14, 1, 1, 30] // S
|
||||
84 => [31, 4, 4, 4, 4, 4, 4] // T
|
||||
85 => [17, 17, 17, 17, 17, 17, 14] // U
|
||||
86 => [17, 17, 17, 17, 17, 10, 4] // V
|
||||
87 => [17, 17, 17, 21, 21, 27, 17] // W
|
||||
88 => [17, 17, 10, 4, 10, 17, 17] // X
|
||||
89 => [17, 17, 10, 4, 4, 4, 4] // Y
|
||||
90 => [31, 1, 2, 4, 8, 16, 31] // Z
|
||||
// Lowercase a-z
|
||||
97 => [0, 0, 14, 1, 15, 17, 15] // a
|
||||
98 => [16, 16, 22, 25, 17, 17, 30] // b
|
||||
99 => [0, 0, 14, 16, 16, 17, 14] // c
|
||||
100 => [1, 1, 13, 19, 17, 17, 15] // d
|
||||
101 => [0, 0, 14, 17, 31, 16, 14] // e
|
||||
102 => [6, 9, 8, 28, 8, 8, 8] // f
|
||||
103 => [0, 0, 15, 17, 15, 1, 14] // g
|
||||
104 => [16, 16, 22, 25, 17, 17, 17] // h
|
||||
105 => [4, 0, 12, 4, 4, 4, 14] // i
|
||||
106 => [2, 0, 6, 2, 2, 18, 12] // j
|
||||
107 => [16, 16, 18, 20, 24, 20, 18] // k
|
||||
108 => [12, 4, 4, 4, 4, 4, 14] // l
|
||||
109 => [0, 0, 26, 21, 21, 17, 17] // m
|
||||
110 => [0, 0, 22, 25, 17, 17, 17] // n
|
||||
111 => [0, 0, 14, 17, 17, 17, 14] // o
|
||||
112 => [0, 0, 30, 17, 30, 16, 16] // p
|
||||
113 => [0, 0, 15, 17, 15, 1, 1] // q
|
||||
114 => [0, 0, 22, 25, 16, 16, 16] // r
|
||||
115 => [0, 0, 14, 16, 14, 1, 30] // s
|
||||
116 => [8, 8, 28, 8, 8, 9, 6] // t
|
||||
117 => [0, 0, 17, 17, 17, 19, 13] // u
|
||||
118 => [0, 0, 17, 17, 17, 10, 4] // v
|
||||
119 => [0, 0, 17, 17, 21, 21, 10] // w
|
||||
120 => [0, 0, 17, 10, 4, 10, 17] // x
|
||||
121 => [0, 0, 17, 17, 15, 1, 14] // y
|
||||
122 => [0, 0, 31, 2, 4, 8, 31] // z
|
||||
// Punctuation
|
||||
45 => [0, 0, 0, 14, 0, 0, 0] // -
|
||||
46 => [0, 0, 0, 0, 0, 12, 12] // .
|
||||
58 => [0, 12, 12, 0, 12, 12, 0] // :
|
||||
47 => [1, 2, 2, 4, 8, 8, 16] // /
|
||||
95 => [0, 0, 0, 0, 0, 0, 31] // _
|
||||
_ => [31, 17, 21, 21, 21, 17, 31]
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn draw_text(
|
||||
dst : Pixmap,
|
||||
x : Int,
|
||||
y : Int,
|
||||
text : String,
|
||||
size : Int,
|
||||
color : @scene.Rgba,
|
||||
align : @scene.TextAlign,
|
||||
bold : Bool,
|
||||
clip : @scene.Rect?,
|
||||
) -> Unit {
|
||||
if text.length() == 0 || color.a.to_int() == 0 {
|
||||
return
|
||||
}
|
||||
let scale = if size >= 14 { 2 } else { 1 }
|
||||
let char_w = 6 * scale
|
||||
let total_w = text.length() * char_w
|
||||
let start_x = match align {
|
||||
Left => x
|
||||
Center => x - total_w / 2
|
||||
Right => x - total_w
|
||||
}
|
||||
let start_y = y - 7 * scale / 2
|
||||
let cr = color.r.to_int()
|
||||
let cg = color.g.to_int()
|
||||
let cb = color.b.to_int()
|
||||
let ca = color.a.to_int()
|
||||
|
||||
let mut idx = 0
|
||||
for ch in text {
|
||||
let rows = glyph_rows(ch.to_int())
|
||||
let gx = start_x + idx * char_w
|
||||
for row = 0; row < 7; row = row + 1 {
|
||||
let bits = rows[row]
|
||||
for col = 0; col < 5; col = col + 1 {
|
||||
if ((bits >> (4 - col)) & 1) != 0 {
|
||||
for sy = 0; sy < scale; sy = sy + 1 {
|
||||
for sx = 0; sx < scale; sx = sx + 1 {
|
||||
let px = gx + col * scale + sx
|
||||
let py = start_y + row * scale + sy
|
||||
blend_pixel(dst, px, py, cr, cg, cb, ca, clip)
|
||||
if bold {
|
||||
blend_pixel(dst, px + 1, py, cr, cg, cb, ca, clip)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
idx = idx + 1
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn draw_ops(
|
||||
dst : Pixmap,
|
||||
ops : Array[@scene.DrawOp],
|
||||
ox : Int,
|
||||
oy : Int,
|
||||
) -> Unit {
|
||||
let clip_stack : Array[@scene.Rect] = []
|
||||
for op in ops {
|
||||
let cur_clip : @scene.Rect? = if clip_stack.length() > 0 {
|
||||
Some(clip_stack[clip_stack.length() - 1])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
match op {
|
||||
FillRect(r, col, rad) =>
|
||||
fill_rounded_rect(
|
||||
dst,
|
||||
@scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h),
|
||||
col,
|
||||
rad,
|
||||
cur_clip,
|
||||
)
|
||||
StrokeRect(r, col, w, rad) =>
|
||||
stroke_rounded_rect(
|
||||
dst,
|
||||
@scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h),
|
||||
col,
|
||||
w,
|
||||
rad,
|
||||
cur_clip,
|
||||
)
|
||||
Line(x0, y0, x1, y1, col, w) =>
|
||||
draw_line(dst, x0 + ox, y0 + oy, x1 + ox, y1 + oy, col, w, cur_clip)
|
||||
Circle(cx, cy, rad, col, filled) =>
|
||||
draw_circle(dst, cx + ox, cy + oy, rad, col, filled, cur_clip)
|
||||
Text(x, y, txt, sz, col, align, bold) =>
|
||||
draw_text(dst, x + ox, y + oy, txt, sz, col, align, bold, cur_clip)
|
||||
PushClip(r) =>
|
||||
clip_stack.push(@scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h))
|
||||
PopClip => if clip_stack.length() > 0 { ignore(clip_stack.pop()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Blit a Wayland wl_shm client buffer (ARGB8888=0, XRGB8888=1, little-endian [B,G,R,A]).
|
||||
pub fn blit_shm_surface(
|
||||
dst : Pixmap,
|
||||
buf : @scene.ShmBufferSnapshot,
|
||||
dst_rect : @scene.Rect,
|
||||
) -> Unit {
|
||||
if buf.width <= 0 || buf.height <= 0 || dst_rect.w <= 0 || dst_rect.h <= 0 {
|
||||
return
|
||||
}
|
||||
let copy_w = if buf.width < dst_rect.w { buf.width } else { dst_rect.w }
|
||||
let copy_h = if buf.height < dst_rect.h { buf.height } else { dst_rect.h }
|
||||
let is_xrgb = buf.format == 1
|
||||
for row = 0; row < copy_h; row = row + 1 {
|
||||
let dy = dst_rect.y + row
|
||||
if dy < 0 || dy >= dst.height {
|
||||
continue
|
||||
}
|
||||
for col = 0; col < copy_w; col = col + 1 {
|
||||
let dx = dst_rect.x + col
|
||||
if dx < 0 || dx >= dst.width {
|
||||
continue
|
||||
}
|
||||
let src_off = row * buf.stride + col * 4
|
||||
if src_off + 3 >= buf.bytes.length() {
|
||||
continue
|
||||
}
|
||||
let b = buf.bytes[src_off].to_int()
|
||||
let g = buf.bytes[src_off + 1].to_int()
|
||||
let r = buf.bytes[src_off + 2].to_int()
|
||||
let a = if is_xrgb { 255 } else { buf.bytes[src_off + 3].to_int() }
|
||||
blend_pixel(dst, dx, dy, r, g, b, a, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn render_scene(scene : @scene.SceneGraph) -> Pixmap {
|
||||
let pixmap = Pixmap::new(scene.output_w, scene.output_h)
|
||||
|
||||
// 1. Background layer
|
||||
draw_ops(pixmap, scene.background_ops, 0, 0)
|
||||
|
||||
// 2. Windows layer (bottom-to-top)
|
||||
for win in scene.windows {
|
||||
if !win.fullscreen {
|
||||
let (top, right, bottom, left) = win.deco_insets
|
||||
let outer_x = win.geometry.x - left
|
||||
let outer_y = win.geometry.y - top
|
||||
let outer_w = win.geometry.w + left + right
|
||||
let outer_h = win.geometry.h + top + bottom
|
||||
|
||||
// Subtle drop shadow
|
||||
fill_rounded_rect(
|
||||
pixmap,
|
||||
@scene.Rect::new(outer_x + 4, outer_y + 6, outer_w, outer_h),
|
||||
@scene.Rgba::new(0, 0, 0, 80),
|
||||
8,
|
||||
None,
|
||||
)
|
||||
|
||||
// Breeze SSD decoration draw-ops
|
||||
draw_ops(pixmap, win.deco_ops, outer_x, outer_y)
|
||||
}
|
||||
|
||||
// Client wl_shm surface
|
||||
match win.surface {
|
||||
Some(buf) => blit_shm_surface(pixmap, buf, win.geometry)
|
||||
None =>
|
||||
fill_rounded_rect(
|
||||
pixmap,
|
||||
win.geometry,
|
||||
@scene.Rgba::new(0x1e, 0x29, 0x3b, 0xff),
|
||||
0,
|
||||
None,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Panel layer
|
||||
draw_ops(pixmap, scene.panel_ops, 0, 0)
|
||||
for widget in scene.widgets {
|
||||
draw_ops(pixmap, widget.ops, widget.rect.x, widget.rect.y)
|
||||
}
|
||||
|
||||
// 4. Overlay layer
|
||||
for widget in scene.widgets {
|
||||
match widget.popup {
|
||||
Some((pr, pops)) => draw_ops(pixmap, pops, pr.x, pr.y)
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
pixmap
|
||||
}
|
||||
|
||||
///| Encode Pixmap as binary PPM (P6).
|
||||
pub fn encode_ppm(p : Pixmap) -> Bytes {
|
||||
let header = "P6\n" +
|
||||
p.width.to_string() +
|
||||
" " +
|
||||
p.height.to_string() +
|
||||
"\n255\n"
|
||||
let out : Array[Byte] = []
|
||||
for ch in header {
|
||||
out.push(ch.to_int().to_byte())
|
||||
}
|
||||
let count = p.width * p.height
|
||||
for i = 0; i < count; i = i + 1 {
|
||||
let off = i * 4
|
||||
out.push(p.data[off])
|
||||
out.push(p.data[off + 1])
|
||||
out.push(p.data[off + 2])
|
||||
}
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
|
||||
///|
|
||||
fn push_u32_be(out : Array[Byte], v : UInt) -> Unit {
|
||||
out.push(((v >> 24) & 0xFFU).to_byte())
|
||||
out.push(((v >> 16) & 0xFFU).to_byte())
|
||||
out.push(((v >> 8) & 0xFFU).to_byte())
|
||||
out.push((v & 0xFFU).to_byte())
|
||||
}
|
||||
|
||||
///|
|
||||
fn crc32(bytes : Array[Byte], start : Int, len : Int) -> UInt {
|
||||
let mut c = 0xFFFF_FFFFU
|
||||
for i = 0; i < len; i = i + 1 {
|
||||
c = c ^ bytes[start + i].to_uint()
|
||||
for _b = 0; _b < 8; _b = _b + 1 {
|
||||
if (c & 1U) != 0U {
|
||||
c = (c >> 1) ^ 0xEDB8_8320U
|
||||
} else {
|
||||
c = c >> 1
|
||||
}
|
||||
}
|
||||
}
|
||||
c ^ 0xFFFF_FFFFU
|
||||
}
|
||||
|
||||
///|
|
||||
fn push_png_chunk(out : Array[Byte], tag : String, payload : Array[Byte]) -> Unit {
|
||||
push_u32_be(out, payload.length().reinterpret_as_uint())
|
||||
let crc_start = out.length()
|
||||
for ch in tag {
|
||||
out.push(ch.to_int().to_byte())
|
||||
}
|
||||
for b in payload {
|
||||
out.push(b)
|
||||
}
|
||||
let crc = crc32(out, crc_start, 4 + payload.length())
|
||||
push_u32_be(out, crc)
|
||||
}
|
||||
|
||||
///| Encode Pixmap as a valid uncompressed RGBA PNG in pure MoonBit.
|
||||
pub fn encode_png(p : Pixmap) -> Bytes {
|
||||
let out : Array[Byte] = [
|
||||
b'\x89', b'\x50', b'\x4e', b'\x47', b'\x0d', b'\x0a', b'\x1a', b'\x0a',
|
||||
]
|
||||
|
||||
// IHDR
|
||||
let ihdr : Array[Byte] = []
|
||||
push_u32_be(ihdr, p.width.reinterpret_as_uint())
|
||||
push_u32_be(ihdr, p.height.reinterpret_as_uint())
|
||||
ihdr.push(b'\x08') // bit depth = 8
|
||||
ihdr.push(b'\x06') // color type = RGBA
|
||||
ihdr.push(b'\x00') // compression = 0
|
||||
ihdr.push(b'\x00') // filter = 0
|
||||
ihdr.push(b'\x00') // interlace = 0
|
||||
push_png_chunk(out, "IHDR", ihdr)
|
||||
|
||||
// Raw scanlines: 1 filter byte (0) + width * 4 bytes per row
|
||||
let row_bytes = 1 + p.width * 4
|
||||
let raw : Array[Byte] = Array(capacity=row_bytes * p.height)
|
||||
let mut s1 = 1U
|
||||
let mut s2 = 0U
|
||||
for y = 0; y < p.height; y = y + 1 {
|
||||
raw.push(b'\x00')
|
||||
s1 = (s1 + 0U) % 65521U
|
||||
s2 = (s2 + s1) % 65521U
|
||||
let row_off = y * p.width * 4
|
||||
for i = 0; i < p.width * 4; i = i + 1 {
|
||||
let b = p.data[row_off + i]
|
||||
raw.push(b)
|
||||
s1 = (s1 + b.to_uint()) % 65521U
|
||||
s2 = (s2 + s1) % 65521U
|
||||
}
|
||||
}
|
||||
|
||||
// Zlib stream with stored (uncompressed) deflate blocks (max 65535 bytes each)
|
||||
let idat : Array[Byte] = [b'\x78', b'\x01']
|
||||
let mut pos = 0
|
||||
let total = raw.length()
|
||||
while pos < total {
|
||||
let rem = total - pos
|
||||
let blk = if rem > 65535 { 65535 } else { rem }
|
||||
let is_last = pos + blk >= total
|
||||
idat.push(if is_last { b'\x01' } else { b'\x00' })
|
||||
idat.push((blk & 0xFF).to_byte())
|
||||
idat.push(((blk >> 8) & 0xFF).to_byte())
|
||||
let nlen = blk ^ 0xFFFF
|
||||
idat.push((nlen & 0xFF).to_byte())
|
||||
idat.push(((nlen >> 8) & 0xFF).to_byte())
|
||||
for i = 0; i < blk; i = i + 1 {
|
||||
idat.push(raw[pos + i])
|
||||
}
|
||||
pos = pos + blk
|
||||
}
|
||||
let adler = (s2 << 16) | s1
|
||||
push_u32_be(idat, adler)
|
||||
push_png_chunk(out, "IDAT", idat)
|
||||
|
||||
// IEND
|
||||
push_png_chunk(out, "IEND", [])
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
Reference in new issue
Block a user