///| `chandra:host/raster` and `chandra:host/font`: the compositor sends draw operations and window /// surface ids; the pixels live here (raster_stub.c) and never cross the component boundary. /// What to draw where (layers, the wallpaper cache, damage) is decided here. ///| extern "C" fn rs_resize(w : Int, h : Int) -> Int = "rs_resize" ///| extern "C" fn rs_clear() = "rs_clear" ///| extern "C" fn rs_set_clip(on : Int, x : Int, y : Int, w : Int, h : Int) = "rs_set_clip" ///| extern "C" fn rs_fill_rect( x : Int, y : Int, w : Int, h : Int, r : Int, g : Int, b : Int, a : Int, radius : Int, ) = "rs_fill_rect" ///| extern "C" fn rs_stroke_rect( x : Int, y : Int, w : Int, h : Int, r : Int, g : Int, b : Int, a : Int, width : Int, radius : Int, ) = "rs_stroke_rect" ///| extern "C" fn rs_line( x0 : Int, y0 : Int, x1 : Int, y1 : Int, r : Int, g : Int, b : Int, a : Int, width : Int, ) = "rs_line" ///| extern "C" fn rs_circle( cx : Int, cy : Int, radius : Int, r : Int, g : Int, b : Int, a : Int, filled : Int, ) = "rs_circle" ///| compositor/font's shim (stb_truetype); 0 on success. #borrow(path) extern "C" fn mp_font_load(slot : Int, path : Bytes) -> Int = "mp_font_load" ///| extern "C" fn rs_font_loaded(slot : Int) = "rs_font_loaded" ///| Load the TTF file at `path` into `slot` (0..3). fn font_load(slot : Int, path : String) -> Bool { if slot < 0 || slot >= 4 { return false } if mp_font_load(slot, @utf8.encode(path)) != 0 { return false } rs_font_loaded(slot) true } ///| extern "C" fn rs_font_has(slot : Int) -> Int = "rs_font_has" ///| #borrow(info) extern "C" fn rs_glyph_info( slot : Int, cp : Int, size : Int, info : FixedArray[Int], ) = "rs_glyph_info" ///| #borrow(out) extern "C" fn rs_glyph_coverage( slot : Int, cp : Int, size : Int, out : FixedArray[Byte], ) = "rs_glyph_coverage" ///| extern "C" fn rs_draw_glyph( slot : Int, cp : Int, size : Int, pen : Int, baseline : Int, r : Int, g : Int, b : Int, a : Int, embolden : Int, ) = "rs_draw_glyph" ///| #borrow(rgba) extern "C" fn rs_image_put( reuse : Int, w : Int, h : Int, rgba : FixedArray[Byte], ) -> Int = "rs_image_put" ///| #borrow(data) extern "C" fn rs_image_svg(data : Bytes) -> Int = "rs_image_svg" ///| extern "C" fn rs_draw_image(handle : Int, x : Int, y : Int, w : Int, h : Int) = "rs_draw_image" ///| extern "C" fn rs_capture( fd : Int, offset : Int, stride : Int, w : Int, h : Int, format : Int, ) -> UInt = "rs_capture" ///| extern "C" fn rs_release(id : UInt) = "rs_release" ///| extern "C" fn rs_thumbnail(id : UInt, w : Int, h : Int, reuse : Int) -> Int = "rs_thumbnail" ///| extern "C" fn rs_blit_surface(id : UInt, x : Int, y : Int, w : Int, h : Int) -> Int = "rs_blit_surface" ///| extern "C" fn rs_surface_covers(id : UInt, w : Int, h : Int) -> Int = "rs_surface_covers" ///| extern "C" fn rs_maps_gc() = "rs_maps_gc" ///| extern "C" fn rs_image_take_changed(handle : Int) -> Int = "rs_image_take_changed" ///| Where the panel layer draws an image whose pixels changed under the same handle (the /// Alt+Tab switcher's live previews): the ops alone compare equal, so this is damage too. fn changed_images(panel : @wt.Val) -> R { let mut d : R = { x: 0, y: 0, w: 0, h: 0 } for op in panel.to_list() { if op is Variant("draw-image", Some(p)) && rs_image_take_changed(p.field("image").to_int()) != 0 { d = union(d, rect_of(p.field("area"))) } } d } ///| extern "C" fn rs_frame_begin() = "rs_frame_begin" ///| extern "C" fn rs_publish_frame() = "rs_publish_frame" ///| extern "C" fn rs_damage(x : Int, y : Int, w : Int, h : Int) = "rs_damage" ///| extern "C" fn rs_save_backdrop() = "rs_save_backdrop" ///| extern "C" fn rs_has_backdrop() -> Int = "rs_has_backdrop" ///| extern "C" fn rs_copy_backdrop() = "rs_copy_backdrop" ///| extern "C" fn rs_width() -> Int = "rs_width" ///| extern "C" fn rs_height() -> Int = "rs_height" ///| #borrow(out) extern "C" fn rs_read_frame(out : FixedArray[Byte]) = "rs_read_frame" ///| extern "C" fn rs_export_memfd() -> Int = "rs_export_memfd" ///| extern "C" fn rs_grab_recording( x : Int, y : Int, w : Int, h : Int, pointer : Int, px : Int, py : Int, ) -> Int = "rs_grab_recording" ///| extern "C" fn rs_send_recording(fd : Int) -> Int = "rs_send_recording" ///| extern "C" fn rs_snapshot() -> Int = "rs_snapshot" ///| extern "C" fn rs_snapshot_w() -> Int = "rs_snapshot_w" ///| extern "C" fn rs_snapshot_h() -> Int = "rs_snapshot_h" ///| #borrow(out) extern "C" fn rs_snapshot_pixels( x : Int, y : Int, w : Int, h : Int, out_w : Int, out_h : Int, out : FixedArray[Byte], ) -> Unit = "rs_snapshot_pixels" ///| `x`, `y`, `w` x `h` of the screenshot snapshot scaled to `out_w` x `out_h`, opaque. fn snapshot_pixels(x : Int, y : Int, w : Int, h : Int, out_w : Int, out_h : Int) -> FixedArray[Byte] { if out_w <= 0 || out_h <= 0 || out_w > 16384 || out_h > 16384 { return FixedArray::make(0, b'\x00') } let out = FixedArray::make(out_w * out_h * 4, b'\x00') rs_snapshot_pixels(x, y, w, h, out_w, out_h, out) out } ///| The framebuffer as a pixmap. fn frame_pixmap() -> @renderer.Pixmap { let (width, height) = (rs_width(), rs_height()) let data = FixedArray::make(width * height * 4, b'\x00') rs_read_frame(data) { width, height, data } } // ---------------------------------------------------------------- geometry ///| struct R { x : Int y : Int w : Int h : Int } derive(Eq, Show) ///| fn R::empty(self : R) -> Bool { self.w <= 0 || self.h <= 0 } ///| fn rect_of(v : @wt.Val) -> R { { x: v.field("x").to_int(), y: v.field("y").to_int(), w: v.field("w").to_int(), h: v.field("h").to_int(), } } ///| The overlap of two clips (None: no clip). fn intersect(a : R?, b : R?) -> R? { match (a, b) { (None, k) | (k, None) => k (Some(a), Some(b)) => { let x0 = @cmp.maximum(a.x, b.x) let y0 = @cmp.maximum(a.y, b.y) let x1 = @cmp.minimum(a.x + a.w, b.x + b.w) let y1 = @cmp.minimum(a.y + a.h, b.y + b.h) Some({ x: x0, y: y0, w: @cmp.maximum(x1 - x0, 0), h: @cmp.maximum(y1 - y0, 0), }) } } } ///| The bounding box of two rectangles (an empty one counts for nothing). fn union(a : R, b : R) -> R { if a.empty() { return b } if b.empty() { return a } let x0 = @cmp.minimum(a.x, b.x) let y0 = @cmp.minimum(a.y, b.y) let x1 = @cmp.maximum(a.x + a.w, b.x + b.w) let y1 = @cmp.maximum(a.y + a.h, b.y + b.h) { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } } ///| fn set_clip(k : R?) -> Unit { match k { Some(r) => rs_set_clip(1, r.x, r.y, r.w, r.h) None => rs_set_clip(0, 0, 0, 0, 0) } } // ---------------------------------------------------------------- text ///| The font slot for (bold, mono), and whether its glyphs must be emboldened (a face with no /// bold instance gets drawn a pixel wider). Slots: 0 mono, 1 mono bold, 2 sans. None when no /// face is loaded at all. fn pick_face(bold : Bool, mono : Bool) -> (Int, Bool)? { let has_mono = rs_font_has(0) != 0 let has_sans = rs_font_has(2) != 0 if !has_mono && !has_sans { return None } let has_bold = has_mono && rs_font_has(1) != 0 Some( if mono && has_mono { if bold && has_bold { (1, false) } else { (0, bold) } } else if has_sans { (2, bold) } else { (0, bold) }, ) } ///| let glyph_info : FixedArray[Int] = FixedArray::make(5, 0) ///| Advance of `cp` in `slot`. fn advance(slot : Int, cp : Int, size : Int) -> Int { rs_glyph_info(slot, cp, size, glyph_info) glyph_info[4] } ///| fn draw_text(l : @wt.Val, ox : Int, oy : Int) -> Unit { let text = l.field("text").to_string() let col = l.field("color") let ca = col.field("a").to_int() if text == "" || ca == 0 { return } let size = @cmp.maximum(l.field("size").to_int(), 0) let bold = l.field("bold").to_bool() let mono = l.field("mono").to_bool() let (slot, embolden) = match pick_face(bold, mono) { Some(f) => f None => return // no face loaded: nothing to draw with } let extra = if embolden { 1 } else { 0 } let cps : Array[Int] = [] let mut total_w = 0 for ch in text { let cp = ch.to_int() cps.push(cp) total_w += advance(slot, cp, size) + extra } let x = l.field("x").to_int() + ox let start_x = match l.field("align").to_int() { 1 => x - total_w / 2 2 => x - total_w _ => x } // `y` is the vertical centre of the text; centre the capital height ('H') on it. let (cap_slot, _) = match pick_face(false, mono) { Some(f) => f None => return } rs_glyph_info(cap_slot, 72, size, glyph_info) let baseline = l.field("y").to_int() + oy + -glyph_info[3] / 2 let r = col.field("r").to_int() let g = col.field("g").to_int() let b = col.field("b").to_int() let mut pen = start_x for cp in cps { rs_draw_glyph(slot, cp, size, pen, baseline, r, g, b, ca, extra) pen += advance(slot, cp, size) + extra } } // ---------------------------------------------------------------- ops ///| fn rgba(c : @wt.Val) -> (Int, Int, Int, Int) { ( c.field("r").to_int(), c.field("g").to_int(), c.field("b").to_int(), c.field("a").to_int(), ) } ///| Draw `ops` offset by (ox, oy). `base` limits everything (the frame's damaged area); a /// push-clip op replaces the clip within it. fn draw_ops(ops : Array[@wt.Val], ox : Int, oy : Int, base : R?) -> Unit { let stack : Array[R] = [] set_clip(base) for op in ops { guard op is Variant(name, payload) match (name, payload) { ("fill-rect", Some(f)) => { let a = rect_of(f.field("area")) let (r, g, b, al) = rgba(f.field("color")) rs_fill_rect( a.x + ox, a.y + oy, a.w, a.h, r, g, b, al, f.field("radius").to_int(), ) } ("stroke-rect", Some(s)) => { let a = rect_of(s.field("area")) let (r, g, b, al) = rgba(s.field("color")) rs_stroke_rect( a.x + ox, a.y + oy, a.w, a.h, r, g, b, al, s.field("width").to_int(), s.field("radius").to_int(), ) } ("draw-line", Some(s)) => { let (r, g, b, al) = rgba(s.field("color")) rs_line( s.field("x0").to_int() + ox, s.field("y0").to_int() + oy, s.field("x1").to_int() + ox, s.field("y1").to_int() + oy, r, g, b, al, s.field("width").to_int(), ) } ("draw-circle", Some(d)) => { let (r, g, b, al) = rgba(d.field("color")) rs_circle( d.field("cx").to_int() + ox, d.field("cy").to_int() + oy, d.field("radius").to_int(), r, g, b, al, if d.field("filled").to_bool() { 1 } else { 0 }, ) } ("draw-text", Some(l)) => draw_text(l, ox, oy) ("push-clip", Some(k)) => { let k = rect_of(k) stack.push({ x: k.x + ox, y: k.y + oy, w: k.w, h: k.h }) set_clip(intersect(Some(stack[stack.length() - 1]), base)) } ("pop-clip", _) => { stack.pop() |> ignore set_clip( if stack.length() > 0 { intersect(Some(stack[stack.length() - 1]), base) } else { base }, ) } ("draw-image", Some(p)) => { let a = rect_of(p.field("area")) rs_draw_image(p.field("image").to_int(), a.x + ox, a.y + oy, a.w, a.h) } _ => () } } } // ---------------------------------------------------------------- frames ///| A frame as the compositor sent it (the parts compared to find what changed). struct Frame { width : Int height : Int wallpaper : @wt.Val? // Some(ops) when sent with this frame background : @wt.Val windows : Array[@wt.Val] panel : @wt.Val popups : @wt.Val } ///| The last frame drawn, and the wallpaper ops the cached backdrop was drawn from. let prev_frame : Ref[Frame?] = Ref(None) ///| The backdrop ops last sent (`wallpaper`, None before any), and how many of them, from the /// start, the cached backdrop pixels hold. Usually all of them; when the ops change every frame /// but begin the same (a desktop module such as Live Starfield: the sky and nebulae stay, the stars /// move), only that common start is kept, and the rest is drawn over a copy of it each frame. let backdrop_ops : Ref[Array[@wt.Val]?] = Ref(None) ///| let backdrop_kept : Ref[Int] = Ref(0) ///| How many ops `a` and `b` share from the start. fn common_prefix(a : Array[@wt.Val], b : Array[@wt.Val]) -> Int { let n = @cmp.minimum(a.length(), b.length()) let mut i = 0 while i < n && a[i] == b[i] { i = i + 1 } i } ///| The backdrop ops past what the cached pixels hold, drawn over them (within `clip`). fn draw_backdrop_rest(clip : R?) -> Unit { match backdrop_ops.val { Some(ops) if backdrop_kept.val < ops.length() => draw_ops(ops[backdrop_kept.val:].to_array(), 0, 0, clip) _ => () } } ///| Room around a window's frame for its shadow, which the decoration ops draw (up to 16 px all /// round for the flat frame, `@scene.decoration_shadow_reach`). const SHADOW_PAD : Int = 18 ///| What a window covers on screen: its decoration and drop shadow, or just itself when fullscreen. fn window_footprint(win : @wt.Val) -> R { let g = rect_of(win.field("geometry")) if win.field("fullscreen").to_bool() { return g } let it = win.field("inset-top").to_int() let ir = win.field("inset-right").to_int() let ib = win.field("inset-bottom").to_int() let il = win.field("inset-left").to_int() { x: g.x - il - SHADOW_PAD, y: g.y - it - SHADOW_PAD, w: g.w + il + ir + 2 * SHADOW_PAD, h: g.h + it + ib + 2 * SHADOW_PAD, } } ///| True when window `b` is window `a` with only a new client buffer (a client redrawing): same /// place, size and decoration. fn only_contents_changed(a : @wt.Val, b : @wt.Val) -> Bool { a.field("geometry") == b.field("geometry") && a.field("fullscreen") == b.field("fullscreen") && a.field("inset-top") == b.field("inset-top") && a.field("inset-right") == b.field("inset-right") && a.field("inset-bottom") == b.field("inset-bottom") && a.field("inset-left") == b.field("inset-left") && a.field("deco") == b.field("deco") } ///| True when `inner` lies inside `outer`. fn contains(outer : R, inner : R) -> Bool { inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.w <= outer.x + outer.w && inner.y + inner.h <= outer.y + outer.h } ///| The index of the topmost window whose opaque client buffer covers all of `area`: nothing /// under it (backdrop, desktop card, lower windows, its own decoration) shows there, so none of /// that is drawn. -1 when no window covers it. fn occluder(windows : Array[@wt.Val], area : R) -> Int { let mut i = windows.length() - 1 while i >= 0 { let win = windows[i] let g = rect_of(win.field("geometry")) if contains(g, area) && rs_surface_covers(win.field("surface").to_uint(), g.w, g.h) != 0 { return i } i = i - 1 } -1 } ///| The part of the screen `f` changes over `prev`, as one rectangle; None when all of it must /// be redrawn. Only window changes are narrowed down: any change to the wallpaper, desktop card, /// panel or the set of windows redraws everything. fn damage(prev : Frame, f : Frame) -> R? { if prev.width != f.width || prev.height != f.height || prev.windows.length() != f.windows.length() { return None } if f.background != prev.background || f.panel != prev.panel { return None } if f.wallpaper is Some(ops) && prev.wallpaper != Some(ops) { return None } let mut d : R = { x: 0, y: 0, w: 0, h: 0 } for i, a in prev.windows { let b = f.windows[i] if a == b { continue } if only_contents_changed(a, b) { // A new buffer in the same place: only the client area is drawn again. d = union(d, rect_of(b.field("geometry"))) continue } d = union(d, union(window_footprint(a), window_footprint(b))) } d = union(d, changed_images(f.panel)) if prev.popups != f.popups { for p in prev.popups.to_list() { d = union(d, rect_of(p.field("area"))) } for p in f.popups.to_list() { d = union(d, rect_of(p.field("area"))) } } Some(d) } ///| Remember `f` as the last frame. Its wallpaper ops are kept from the frame that sent them, so /// a later frame that resends the same wallpaper still compares equal. fn keep_prev(f : Frame) -> Unit { let wallpaper = match f.wallpaper { Some(_) => f.wallpaper None => match prev_frame.val { Some(p) => p.wallpaper None => None } } prev_frame.val = Some({ ..f, wallpaper, }) } ///| Pixels redrawn by the last submit (for --profile). let last_drawn : Ref[Int] = Ref(0) ///| Draw a frame into the framebuffer: wallpaper (cached), background, windows bottom to top, /// panel, then client popups. The same layering as the software renderer's `render_scene`. Only /// the part that differs from the last frame is redrawn. fn submit(v : @wt.Val) -> Unit { let f : Frame = { width: @cmp.maximum(v.field("width").to_int(), 1), height: @cmp.maximum(v.field("height").to_int(), 1), wallpaper: v.field("wallpaper").to_option(), background: v.field("background"), windows: v.field("windows").to_list(), panel: v.field("panel"), popups: v.field("popups"), } let w = f.width let h = f.height rs_frame_begin() let resized = rs_resize(w, h) != 0 // The frame starts from the cached backdrop (which covers every pixel), so the buffer is only // cleared when there is none. let backdrop_ready = rs_has_backdrop() != 0 && (match (f.wallpaper, backdrop_ops.val) { (None, Some(_)) => true (Some(ops), Some(o)) => o == ops.to_list() _ => false }) let clip : R? = match prev_frame.val { Some(prev) if !resized && backdrop_ready => match damage(prev, f) { Some(d) => match intersect(Some(d), Some({ x: 0, y: 0, w, h })) { Some(k) if !k.empty() => Some(k) _ => { // Nothing on screen changed (a window off screen, say). last_drawn.val = 0 keep_prev(f) rs_publish_frame() return } } None => None } _ => None } // (A new backdrop clears the buffer itself, below, unless its kept start can be copied in.) if !resized && !backdrop_ready && f.wallpaper is None { rs_clear() } last_drawn.val = match clip { Some(k) => k.w * k.h None => w * h } rs_maps_gc() match clip { Some(k) => rs_damage(k.x, k.y, k.w, k.h) None => rs_damage(0, 0, w, h) } // A full-screen (or tiled) client redrawing covers the whole damaged area with its own opaque // pixels: start from that window instead of from the backdrop. // (Only with the backdrop cached: a new wallpaper must still be drawn once to be kept.) let top = if backdrop_ready { occluder(f.windows, clip.unwrap_or({ x: 0, y: 0, w, h })) } else { -1 } let occluded = top >= 0 let first = if occluded { top } else { 0 } // 1. Wallpaper: drawn once, copied back while unchanged (only the damaged part). match f.wallpaper { Some(_) if occluded => () Some(ops) => if backdrop_ready { set_clip(clip) rs_copy_backdrop() draw_backdrop_rest(clip) } else { let ops = ops.to_list() let kept = backdrop_kept.val match backdrop_ops.val { Some(old) if rs_has_backdrop() != 0 && kept > 0 && common_prefix(old, ops) >= kept => { // The cached start still holds: copy it, draw the rest. set_clip(None) rs_copy_backdrop() backdrop_ops.val = Some(ops) draw_backdrop_rest(None) } old => { // Keep only the start these ops share with the last ones when they share one (they // will likely change again next frame), else all of them. let shared = match old { Some(o) => common_prefix(o, ops) None => 0 } let keep = if shared > 0 && shared < ops.length() { shared } else { ops.length() } if !resized { rs_clear() } draw_ops(ops[:keep].to_array(), 0, 0, None) rs_save_backdrop() backdrop_ops.val = Some(ops) backdrop_kept.val = keep draw_backdrop_rest(None) } } } None => if rs_has_backdrop() != 0 && !occluded { set_clip(clip) rs_copy_backdrop() draw_backdrop_rest(clip) } } if !occluded { draw_ops(f.background.to_list(), 0, 0, clip) } // 2. Windows, bottom to top (from the one covering the damage, when one does). for wi = first; wi < f.windows.length(); wi = wi + 1 { let win = f.windows[wi] let g = rect_of(win.field("geometry")) if clip is Some(_) { match intersect(Some(window_footprint(win)), clip) { Some(r) if !r.empty() => () _ => continue } } if !win.field("fullscreen").to_bool() && !(occluded && wi == first) { let it = win.field("inset-top").to_int() let il = win.field("inset-left").to_int() // The frame, shadow included, is all in the decoration ops. draw_ops(win.field("deco").to_list(), g.x - il, g.y - it, clip) } set_clip(clip) if rs_blit_surface(win.field("surface").to_uint(), g.x, g.y, g.w, g.h) == 0 { rs_fill_rect(g.x, g.y, g.w, g.h, 0x1e, 0x29, 0x3b, 0xff, 0) } } // 3. Panel (and its popups, menus and notices), then 4. client popups above everything. draw_ops(f.panel.to_list(), 0, 0, clip) set_clip(clip) for p in f.popups.to_list() { let a = rect_of(p.field("area")) rs_blit_surface(p.field("surface").to_uint(), a.x, a.y, a.w, a.h) |> ignore } keep_prev(f) rs_publish_frame() } // ---------------------------------------------------------------- images ///| Image handles by path (0: it failed, don't try again). let image_paths : Map[String, Int] = {} ///| Biggest image side accepted from a file. const MAX_IMAGE : Int = 1024 ///| Whether a file without the `.svg` extension is an SVG: it starts like XML and has an ` Bool { let head = b[:@cmp.minimum(b.length(), 4096)] let text = @utf8.decode_lossy(head).trim(chars="\u{FEFF} \t\r\n") text.has_prefix("<") && text.contains(" Int { match image_paths.get(path) { Some(h) => return h None => () } let b = @sys.read_file_bytes(path) let h = if b.length() > 8 && b[0] == b'\x89' && b[1] == b'P' && b[2] == b'N' { match @renderer.decode_png(b) { Some(p) if p.width <= MAX_IMAGE && p.height <= MAX_IMAGE => rs_image_put(0, p.width, p.height, p.data) _ => 0 } } else if b.length() > 0 && (path.has_suffix(".svg") || looks_like_svg(b)) { rs_image_svg(b) } else { 0 } image_paths.set(path, h) h } ///| The colour emoji font, for the emoji picker (`emoji-*`). let emoji_source : @emoji.EmojiSource = @emoji.EmojiSource::new() ///| Handles of the emoji pictures made so far (shared, like path images: never overwritten). let emoji_handles : Array[Int] = [] ///| fn emoji_image(text : String, px : Int) -> UInt { emoji_source.image(text, px, p => { let h = rs_image_put(0, p.width, p.height, p.data) emoji_handles.push(h) h.reinterpret_as_uint() }) } ///| fn is_path_image(h : Int) -> Bool { for _, v in image_paths { if v == h { return true } } emoji_handles.contains(h) } // ---------------------------------------------------------------- the interfaces ///| fn font_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] { [ ( "load", a => boolean(font_load(a[0].to_int(), a[1].to_string())), ), ( "rasterise", a => { let (slot, cp, size) = (a[0].to_int(), a[1].to_int(), a[2].to_int()) let info = FixedArray::make(5, 0) rs_glyph_info(slot, cp, size, info) let cov = FixedArray::make(info[0] * info[1], b'\x00') if cov.length() > 0 { rs_glyph_coverage(slot, cp, size, cov) } [ Record([ ("width", S32(info[0])), ("height", S32(info[1])), ("xoff", S32(info[2])), ("yoff", S32(info[3])), ("advance", S32(info[4])), ("coverage", @wt.Val::bytes(Bytes::from_fixedarray(cov))), ]), ] }, ), ] } ///| fn raster_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] { [ ( "capture", a => [ U32( rs_capture( a[0].to_int(), a[1].to_int(), a[2].to_int(), a[3].to_int(), a[4].to_int(), a[5].to_int(), ), ), ], ), ( "release", a => { rs_release(a[0].to_uint()) none }, ), ("load-image", a => [U32(load_image(a[0].to_string()).reinterpret_as_uint())]), ( "emoji-font", _ => match emoji_source.font() { Ok(path) => [Result(true, Some(Str(path)))] Err(why) => [Result(false, Some(Str(why)))] }, ), ( "emoji-has", a => [List(emoji_source.has(a[0].to_list().map(v => v.to_string())).map(b => Bool(b)))], ), ("emoji-image", a => [U32(emoji_image(a[0].to_string(), a[1].to_int()))]), ( "image-pixels", a => { let reuse = a[3].to_int() let reuse = if is_path_image(reuse) { 0 } else { reuse } let px = a[2].to_bytes().to_fixedarray() [U32(rs_image_put(reuse, a[0].to_int(), a[1].to_int(), px).reinterpret_as_uint())] }, ), ( "thumbnail", a => { let reuse = a[3].to_int() let reuse = if is_path_image(reuse) { 0 } else { reuse } [ U32( rs_thumbnail(a[0].to_uint(), a[1].to_int(), a[2].to_int(), reuse).reinterpret_as_uint(), ), ] }, ), ( "submit", a => { submit(a[0]) none }, ), ( "read-frame", _ => { [@wt.Val::bytes(Bytes::from_fixedarray(frame_pixmap().data))] }, ), ("export-memfd", _ => s32(rs_export_memfd())), ( "write-frame-png", a => { let p = frame_pixmap() let ok = p.width > 0 && @sys.write_file_bytes(a[0].to_string(), @renderer.encode_png(p)) == 0 [Bool(ok)] }, ), ( "grab-recording-frame", a => { let n = rs_grab_recording( a[0].to_int(), a[1].to_int(), a[2].to_int(), a[3].to_int(), if a[4].to_bool() { 1 } else { 0 }, a[5].to_int(), a[6].to_int(), ) [Bool(n != 0)] }, ), ("send-recording-frame", a => s32(rs_send_recording(a[0].to_int()))), ("snapshot-frame", _ => [Bool(rs_snapshot() != 0)]), ( "snapshot-png", a => { // Clipped to the snapshot; a 1:1 copy is a scale by one. let (sw, sh) = (rs_snapshot_w(), rs_snapshot_h()) let x0 = a[0].to_int().max(0) let y0 = a[1].to_int().max(0) let x1 = (a[0].to_int() + a[2].to_int()).min(sw) let y1 = (a[1].to_int() + a[3].to_int()).min(sh) if x1 <= x0 || y1 <= y0 { return [@wt.Val::bytes(b"")] } let (w, h) = (x1 - x0, y1 - y0) let p : @renderer.Pixmap = { width: w, height: h, data: snapshot_pixels(x0, y0, w, h, w, h), } [@wt.Val::bytes(@renderer.encode_png_compressed(p))] }, ), ( "snapshot-pixels", a => { let px = snapshot_pixels( a[0].to_int(), a[1].to_int(), a[2].to_int(), a[3].to_int(), a[4].to_int(), a[5].to_int(), ) [@wt.Val::bytes(Bytes::from_fixedarray(px))] }, ), ] }