code wiki / _hdl_build / nx_glyph_compose.nx
nx_glyph_compose.nx source
↩ module page · 79 lines · 3784 B
1// nx_glyph_compose.nx -- RUNG 2+3 of the sovereign font BUILDER: COMPONENTS + COMPOSITION (the kanji-scaling
2// heart). A COMPONENT (radical) is a stroke group defined ONCE in a 0..1000 em box; a glyph is a COMPOSITION
3// of that component placed by a transform (offset+scale). Proof: define 木 (tree) once, render it standalone
4// AND compose 林 (forest) = 木 placed LEFT + 木 placed RIGHT -- the SAME radical reused -> adding a script is
5// DATA (more components + compositions), not a new engine. Renders AA -> _offc/nx_aa_kanji.png. ORIGINAL.
6import "nx_glyph_stroke.nx"
7import "nx_aa_raster.nx"
8import "nx_png_write.nx"
9const K_MAGIC_1080: i64 = 1080
10const K_MAGIC_8192: i64 = 8192
11const K_MAGIC_1024: i64 = 1024
12
13// stroke a component-local polyline (lx/ly in 0..1000 em) through a transform (tx,ty + scale sx,sy /1000),
14// width lw scaled by sx. The one bridge from component-space to glyph/grid-space.
15func put_tstroke(xs: *i64, ys: *i64, cstart: *i64, clen: *i64, np: *i64, nc: *i64, cs: *i64, sn: *i64,
16 lx: *i64, ly: *i64, n: i64, lw: i64, tx: i64, ty: i64, sx: i64, sy: i64) -> i64 {
17 let gx: *i64 = sys_mmap(8*32) as *i64
18 let gy: *i64 = sys_mmap(8*32) as *i64
19 var i: i64 = 0
20 while i < n {
21 gx[i] = tx + (lx[i] * sx) / 1000
22 gy[i] = ty + (ly[i] * sy) / 1000
23 i = i + 1
24 }
25 gs_stroke(xs, ys, cstart, clen, np, nc, cs, sn, gx, gy, n, (lw * sx) / 1000)
26 return 0
27}
28
29// COMPONENT 木 (tree): vertical + horizontal + two sweeps, defined ONCE in 0..1000 em.
30func comp_tree(xs: *i64, ys: *i64, cstart: *i64, clen: *i64, np: *i64, nc: *i64, cs: *i64, sn: *i64,
31 tx: i64, ty: i64, sx: i64, sy: i64) -> i64 {
32 let lx: *i64 = sys_mmap(8*8) as *i64
33 let ly: *i64 = sys_mmap(8*8) as *i64
34 let LW: i64 = 85
35 lx[0]=500;ly[0]=70; lx[1]=500;ly[1]=930 // vertical stem
36 put_tstroke(xs,ys,cstart,clen,np,nc,cs,sn, lx,ly,2, LW, tx,ty,sx,sy)
37 lx[0]=130;ly[0]=360; lx[1]=870;ly[1]=360 // horizontal bar
38 put_tstroke(xs,ys,cstart,clen,np,nc,cs,sn, lx,ly,2, LW, tx,ty,sx,sy)
39 lx[0]=520;ly[0]=380; lx[1]=150;ly[1]=880 // left sweep
40 put_tstroke(xs,ys,cstart,clen,np,nc,cs,sn, lx,ly,2, LW, tx,ty,sx,sy)
41 lx[0]=490;ly[0]=440; lx[1]=850;ly[1]=880 // right sweep
42 put_tstroke(xs,ys,cstart,clen,np,nc,cs,sn, lx,ly,2, LW, tx,ty,sx,sy)
43 return 0
44}
45
46func main() -> i64 {
47 let W: i64 = 270
48 let H: i64 = 130
49 let ss: i64 = 4 // grid K_MAGIC_1080 x 520
50
51 let cs: *i64 = sys_mmap(8*16) as *i64
52 let sn: *i64 = sys_mmap(8*16) as *i64
53 gs_init_trig(cs, sn)
54
55 let xs: *i64 = sys_mmap(8*K_MAGIC_8192) as *i64
56 let ys: *i64 = sys_mmap(8*K_MAGIC_8192) as *i64
57 let cstart: *i64 = sys_mmap(8*K_MAGIC_1024) as *i64
58 let clen: *i64 = sys_mmap(8*K_MAGIC_1024) as *i64
59 let np: *i64 = sys_mmap(16) as *i64; np[0]=0
60 let nc: *i64 = sys_mmap(16) as *i64; nc[0]=0
61
62 // 木 standalone (full box) at x 60..420
63 comp_tree(xs,ys,cstart,clen,np,nc, cs,sn, 60, 60, 360, 380)
64 // 林 (forest) = 木 + 木, each compressed to ~half width, placed side by side
65 comp_tree(xs,ys,cstart,clen,np,nc, cs,sn, 480, 60, 250, 380)
66 comp_tree(xs,ys,cstart,clen,np,nc, cs,sn, 740, 60, 250, 380)
67
68 let cov: *u8 = sys_mmap(W*H)
69 aa_render(xs, ys, cstart, clen, nc[0], cov, W, H, ss)
70
71 let rgb: *u8 = sys_mmap(W*H*3)
72 var i: i64 = 0
73 while i < W*H*3 { rgb[i]=255 as u8; i=i+1 }
74 aa_blit(rgb, W, H, 0, 0, cov, W, H, 20, 30, 50)
75
76 nx_png_write_rgb("_offc/nx_aa_kanji.png\x00" as *u8, rgb, W, H)
77 sys_write(1, "nx_glyph_compose: wrote _offc/nx_aa_kanji.png (mu + hayashi)\n\x00" as *u8, 60)
78 return 0
79}