code wiki / (root) / nx_font_1bpp_test.nx

nx_font_1bpp_test.nx source

↩ module page · 60 lines · 2249 B

1// nx_font_1bpp_test.nx -- smoke for nx_font_1bpp. 2 3import "nx_syscalls.nx" 4import "nx_font_1bpp.nx" 5 6func main() -> i64 { 7 // Build a minimal 3-char font: 'A', 'B', 'C' as 3x5 glyphs. 8 // Layout: each column is 1 byte (height 5 fits in 8-bit page). 9 let data: *u8 = (sys_mmap(64)) as *u8 10 // 'A': /\| (3 cols, 5 rows) 11 // X X 12 // X . X 13 // X X X 14 // X . X 15 // X . X 16 // column 0: rows 0=0,1=1,2=1,3=1,4=1 -> 0b11110 = 0x1E 17 // column 1: rows 0=1,1=0,2=1,3=0,4=0 -> 0b00101 = 0x05 18 // column 2: same as col 0 19 data[0] = 0x1E as u8; data[1] = 0x05 as u8; data[2] = 0x1E as u8 20 // 'B' (3 cols) 21 data[3] = 0x1F as u8; data[4] = 0x15 as u8; data[5] = 0x0A as u8 22 // 'C' (3 cols) 23 data[6] = 0x0E as u8; data[7] = 0x11 as u8; data[8] = 0x11 as u8 24 25 let f: *NxFont = nx_font_new(data, 3, 5, 3, 65) // base_char='A' 26 if (f as i64) == 0 { return 1 } 27 if f.glyph_width != 3 { return 2 } 28 if f.glyph_height != 5 { return 3 } 29 if f.bytes_per_glyph != 3 { return 4 } // 3 columns, height 5 fits in 1 byte per col 30 31 // Has chars 32 if nx_font_has_char(f, 65) != 1 { return 5 } // 'A' 33 if nx_font_has_char(f, 67) != 1 { return 6 } // 'C' 34 if nx_font_has_char(f, 68) != 0 { return 7 } // 'D' out of range 35 if nx_font_has_char(f, 64) != 0 { return 8 } // '@' below range 36 37 // Glyph_at pointer arithmetic 38 let g_a: *u8 = nx_font_glyph_at(f, 65) 39 let g_b: *u8 = nx_font_glyph_at(f, 66) 40 if (g_b as i64) - (g_a as i64) != 3 { return 9 } 41 // Bad char -> NULL 42 let null_g: *u8 = nx_font_glyph_at(f, 200) 43 if (null_g as i64) != 0 { return 10 } 44 45 // Glyph pixel queries -- 'A' col 0 row 1 should be ON 46 if nx_font_glyph_pixel(f, 65, 0, 1) != 1 { return 11 } 47 // 'A' col 1 row 1 should be OFF (the gap in the A) 48 if nx_font_glyph_pixel(f, 65, 1, 1) != 0 { return 12 } 49 // Out of glyph -> 0 50 if nx_font_glyph_pixel(f, 65, 99, 0) != 0 { return 13 } 51 if nx_font_glyph_pixel(f, 65, 0, 99) != 0 { return 14 } 52 53 // Bad construction 54 let bad1: *NxFont = nx_font_new(data, 0, 5, 3, 65) 55 if (bad1 as i64) != 0 { return 15 } 56 let bad2: *NxFont = nx_font_new(data, 3, 5, 0, 65) 57 if (bad2 as i64) != 0 { return 16 } 58 59 return 0 60}