code wiki / (root) / nx_font_web_embed.nx

nx_font_web_embed.nx source

↩ module page · 76 lines · 4999 B

1// nx_font_web_embed.nx -- R3 of the sovereign-font ladder: read web_assets/nishi.ttf (from R2), base64-encode it, 2// and emit web_assets/nishi_font_test.html with an @font-face data-URI (NO external load, fully sovereign) that 3// renders text in OUR font -> the browser loads a typeface we built from scratch. Closes the distinctive-typeface 4// axis end-to-end (R0 census -> R1 outline -> R2 .ttf -> R3 embed). Seed glyphs = H I T + space, so the demo word 5// uses them. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls_x86_64.nx" 7const K_MAGIC_65536: i64 = 65536 8const K_MAGIC_131072: i64 = 131072 9 10func he_puts(buf: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=0 { buf[o]=s[i]; o=o+1; i=i+1 } return o } 11 12func b64c(v: i64) -> i64 { 13 if v<26 { return 65+v } 14 if v<52 { return 97+(v-26) } 15 if v<62 { return 48+(v-52) } 16 if v==62 { return 43 } 17 return 47 18} 19// base64-encode inb[0..n) into out (NUL-terminated). return length. 20func b64_encode(inb: *u8, n: i64, out: *u8) -> i64 { 21 var o: i64=0; var i: i64=0 22 while i+3<=n { 23 let b0: i64=inb[i] as i64; let b1: i64=inb[i+1] as i64; let b2: i64=inb[i+2] as i64 24 out[o]=b64c(b0/4) as u8; o=o+1 25 out[o]=b64c((b0%4)*16 + b1/16) as u8; o=o+1 26 out[o]=b64c((b1%16)*4 + b2/64) as u8; o=o+1 27 out[o]=b64c(b2%64) as u8; o=o+1 28 i=i+3 29 } 30 let rem: i64=n-i 31 if rem==1 { 32 let b0: i64=inb[i] as i64 33 out[o]=b64c(b0/4) as u8; o=o+1 34 out[o]=b64c((b0%4)*16) as u8; o=o+1 35 out[o]=61 as u8; o=o+1; out[o]=61 as u8; o=o+1 36 } 37 if rem==2 { 38 let b0: i64=inb[i] as i64; let b1: i64=inb[i+1] as i64 39 out[o]=b64c(b0/4) as u8; o=o+1 40 out[o]=b64c((b0%4)*16 + b1/16) as u8; o=o+1 41 out[o]=b64c((b1%16)*4) as u8; o=o+1 42 out[o]=61 as u8; o=o+1 43 } 44 out[o]=0 as u8 45 return o 46} 47 48func main() -> i64 { 49 let len_p: *i64 = sys_mmap(8) as *i64 50 let ttf: *u8 = sys_read_file_x86_64("web_assets/nishi.ttf" as *u8, len_p) 51 if ttf == (0 as *u8) { sys_write(1, "cannot read nishi.ttf\n" as *u8, 22); return 5 } 52 let tn: i64 = len_p[0] 53 54 let b64: *u8 = sys_mmap(K_MAGIC_65536) 55 let bl: i64 = b64_encode(ttf, tn, b64) 56 57 let out: *u8 = sys_mmap(K_MAGIC_131072) 58 var o: i64 = 0 59 o = he_puts(out, o, "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta name=\"color-scheme\" content=\"dark\"><title>Nishi Sans &mdash; a sovereign font, built from scratch</title><style>" as *u8) 60 o = he_puts(out, o, "@font-face{font-family:'Nishi Sans';font-style:normal;font-weight:400;src:url(data:font/ttf;base64," as *u8) 61 o = he_puts(out, o, b64) 62 o = he_puts(out, o, ") format('truetype')}" as *u8) 63 o = he_puts(out, o, "*{box-sizing:border-box}body{margin:0;min-height:100vh;display:grid;place-items:center;background:radial-gradient(1000px 500px at 50% -10%,#1a1633,#0A0A0F);color:#F5F5FA;font-family:ui-sans-serif,system-ui,-apple-system,\"Segoe UI\",Roboto,sans-serif}.wrap{text-align:center;padding:32px}.big{font-family:'Nishi Sans';font-size:clamp(4rem,24vw,16rem);line-height:1;letter-spacing:.06em;background:linear-gradient(100deg,#8A7BFF 20%,#FF7A59 90%);-webkit-background-clip:text;background-clip:text;color:transparent}.tag{display:inline-block;font-size:.8rem;letter-spacing:.2em;text-transform:uppercase;color:#9EA0B8;margin-bottom:22px}.note{color:#9EA0B8;max-width:46ch;margin:34px auto 0;font-size:1.05rem;line-height:1.65}.note b{color:#F5F5FA}.mono{font-family:ui-monospace,Consolas,monospace;color:#8A7BFF}</style></head><body><div class=\"wrap\">" as *u8) 64 o = he_puts(out, o, "<span class=\"tag\">&#9733; rendered in a font we built ourselves</span>" as *u8) 65 o = he_puts(out, o, "<div class=\"big\">HI IT</div>" as *u8) 66 o = he_puts(out, o, "<p class=\"note\"><b>Nishi Sans</b> is sovereign to the last byte: in-house glyph designs &rarr; our own TrueType emitter (<span class=\"mono\">nx_font_ttf_emit</span>, self-validated) &rarr; embedded here as a data-URI &mdash; <b>zero external load</b>. This seed carries H, I, T and space (hence &ldquo;HI IT&rdquo;); the full alphabet is the next rung. If these letters render blocky and bold, your browser just loaded a font Nishi made from scratch, silicon-up.</p>" as *u8) 67 o = he_puts(out, o, "</div></body></html>" as *u8) 68 69 let fd: i64 = sys_openat_wr("web_assets/nishi_font_test.html" as *u8, 420) 70 if fd < 0 { sys_write(1, "SAVE-FAIL\n" as *u8, 9); return 6 } 71 sys_write(fd, out, o); sys_close(fd) 72 sys_write(1, "nx_font_web_embed: wrote web_assets/nishi_font_test.html (ttf=" as *u8, 61) 73 let nb: *u8 = sys_mmap(28); var m: i64=tn; var k: i64=0; if m==0{nb[0]=48;k=1} let tt: *u8=sys_mmap(28); while m>0{tt[k]=(48+(m-(m/10)*10)) as u8;m=m/10;k=k+1} var j: i64=0; while j<k{nb[j]=tt[k-1-j];j=j+1} sys_write(1,nb,k) 74 sys_write(1, " bytes -> base64 embedded)\n" as *u8, 27) 75 return 0 76}