code wiki / _hdl_build / nx_caption_burn.nx
nx_caption_burn.nx source
↩ module page · 42 lines · 1963 B
1// nx_caption_burn.nx -- burn a caption (e.g. a translated subtitle) onto a video FRAME (Framebuffer): an
2// opaque caption BAR across the bottom + the caption text centered, integer scale, NO float. Composes the
3// proven bits-up paint substrate (nx_paint_solid_rect + nx_paint_text_scaled over the 5x7 bitmap font). A
4// caption TRACK = timestamped [t0,t1) segments; cb_active_at(t) selects the on-screen line (or -1). This is
5// the live-translation RENDER core -- ASR (speech->text) + open-domain MT are RESEARCHER long-poles; the
6// phrase-table MT (nx_mt_phrasetable) feeds it translated bytes for the gated R1 vertical. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_css_color_decode.nx"
9import "nx_paint_solid_rect.nx"
10import "nx_font_bitmap_5x7.nx"
11import "nx_paint_text.nx"
12
13// index of the caption segment active at time t (ms), or -1. (linear scan; tracks are short)
14func cb_active_at(t0s: *i64, t1s: *i64, nseg: i64, t: i64) -> i64 {
15 var i: i64 = 0
16 while i < nseg {
17 if t >= t0s[i] { if t < t1s[i] { return i } }
18 i = i + 1
19 }
20 return 0 - 1
21}
22
23// burn `text` (len bytes) as a caption onto fb at integer `scale`: a bottom bar (bar color) + centered text
24// (fg color). returns text pixels actually set (0 if len<=0). Bar height tracks the scaled glyph cell.
25func nx_caption_burn(fb: *Framebuffer, text: *u8, len: i64, scale: i64,
26 bar: *CssColor, fg: *CssColor) -> i64 {
27 if len <= 0 { return 0 }
28 var sc: i64 = scale
29 if sc < 1 { sc = 1 }
30 let cell_w: i64 = 6 * sc
31 let cell_h: i64 = 7 * sc
32 let pad: i64 = 2 * sc
33 let bar_h: i64 = cell_h + pad + pad
34 var bar_y: i64 = fb.height - bar_h
35 if bar_y < 0 { bar_y = 0 }
36 nx_paint_solid_rect(fb, 0, bar_y, fb.width, bar_h, bar)
37 let tw: i64 = len * cell_w
38 var tx: i64 = (fb.width - tw) / 2
39 if tx < 0 { tx = 0 }
40 let ty: i64 = bar_y + pad
41 return nx_paint_text_scaled(fb, tx, ty, text, len, fg, sc)
42}