code wiki / (root) / nx_cdmap_referee.nx

nx_cdmap_referee.nx source

↩ module page · 116 lines · 7209 B

1// nx_cdmap_referee.nx -- the position map's SECOND RULER as a program (datavis DV2a, 2026-09-15). Reads an emitted compare 2// page and its api.json, re-derives the 2D cut's readability grade from the SVG alone (the heuristic critic must REPRODUCE 3// what the page printed), then re-measures every label with real TrueType advances from a real sans face and grades again. 4// usage: nx_cdmap_referee <index.html> <api.json> <regular.ttf> <bold.ttf> 5// exit 0 AGREE the heuristic critic reproduces the published grade AND the metric critic finds the same defect count 6// 1 DISAGREE either the page and its api.json disagree, or real glyph widths change the defect count 7// 2 usage 8// 3 UNPROVEN no cdmap on the page, labels and marks do not pair, the frame is unreadable, api.json lacks the grade, 9// or the font cannot be read -- a verdict this program could not reach is named, never acquitted 10// The verdict line is LAST. Every number it decides on is printed above it so an outside reader can recompute the call. 11// license_tier: ORIGINAL 12import "nx_cdmap_referee_lib.nx" 13import "nx_gate_verdict.nx" 14 15const P_ARGC: i64 = 5 16const P_EXIT_AGREE: i64 = 0 17const P_EXIT_DISAGREE: i64 = 1 18const P_EXIT_USAGE: i64 = 2 19const P_EXIT_UNPROVEN: i64 = 3 20const P_BLOCK: *u8 = "readability_cut" 21 22func p_kv(k: *u8, v: i64) -> i64 { gv_puts(" " as *u8); gv_puts(k); gv_puts("=" as *u8); gv_num(v); return 0 } 23func p_crit(label: *u8, c: *i64) -> i64 { 24 gv_puts(label) 25 p_kv("LL" as *u8, c[CL_C_LL]); p_kv("LM" as *u8, c[CL_C_LM]); p_kv("OFF" as *u8, c[CL_C_OFF]) 26 p_kv("MM" as *u8, c[CL_C_MM]); p_kv("SPREAD" as *u8, c[CL_C_SPREAD]); p_kv("DEFECTS" as *u8, c[CL_C_DEFECTS]) 27 gv_puts("\n" as *u8) 28 return 0 29} 30func p_unproven(why: *u8) -> i64 { gv_puts(" UNPROVEN: " as *u8); gv_puts(why); gv_puts("\nverdict=UNPROVEN\n" as *u8); sys_exit(P_EXIT_UNPROVEN); return P_EXIT_UNPROVEN } 31func p_alloc_i64(n: i64) -> *i64 { return sys_mmap(n * CR_I64_BYTES) as *i64 } 32 33func main(argc: i64, argv: *i64) -> i64 { 34 if argc < P_ARGC { 35 gv_puts("usage: nx_cdmap_referee <index.html> <api.json> <regular.ttf> <bold.ttf>\n" as *u8) 36 sys_exit(P_EXIT_USAGE); return P_EXIT_USAGE 37 } 38 let page: *u8 = argv[1] as *u8 39 let api: *u8 = argv[2] as *u8 40 let freg: *u8 = argv[3] as *u8 41 let fbold: *u8 = argv[4] as *u8 42 let lens: *i64 = p_alloc_i64(2) 43 let h: *u8 = sys_read_file(page, lens) 44 gv_puts("NX-CDMAP-REFEREE page=" as *u8); gv_puts(page); p_kv("page_bytes" as *u8, lens[0]); gv_puts("\n" as *u8) 45 if (h as i64) == 0 { return p_unproven("page unreadable" as *u8) } 46 let mx: *i64 = p_alloc_i64(CR_MAXN); let my: *i64 = p_alloc_i64(CR_MAXN); let mr: *i64 = p_alloc_i64(CR_MAXN) 47 let lx: *i64 = p_alloc_i64(CR_MAXN); let ly: *i64 = p_alloc_i64(CR_MAXN) 48 let lfont: *i64 = p_alloc_i64(CR_MAXN); let lbold: *i64 = p_alloc_i64(CR_MAXN) 49 let lwh: *i64 = p_alloc_i64(CR_MAXN); let lhh: *i64 = p_alloc_i64(CR_MAXN) 50 let lwm: *i64 = p_alloc_i64(CR_MAXN); let lhm: *i64 = p_alloc_i64(CR_MAXN) 51 let names: *u8 = sys_mmap(CR_MAXN * CR_NAME_W) 52 let frame: *i64 = p_alloc_i64(CR_FRAME_W) 53 let out: *i64 = p_alloc_i64(1) 54 let nl: i64 = cr_scan(h, lens[0], mx, my, mr, lx, ly, lfont, lbold, names, frame, out) 55 if nl == CR_ABSENT { return p_unproven("the page carries no cdmap svg" as *u8) } 56 let nm: i64 = out[0] 57 gv_puts(" scan" as *u8); p_kv("labels" as *u8, nl); p_kv("marks" as *u8, nm) 58 p_kv("frame_x0" as *u8, frame[CR_F_X0]); p_kv("frame_y0" as *u8, frame[CR_F_Y0]); p_kv("frame_x1" as *u8, frame[CR_F_X1]); p_kv("frame_y1" as *u8, frame[CR_F_Y1]); gv_puts("\n" as *u8) 59 if nl == 0 { return p_unproven("no player labels on the figure" as *u8) } 60 if nl != nm { return p_unproven("labels and marks do not pair one to one" as *u8) } 61 if frame[CR_F_X0] == CR_ABSENT { return p_unproven("the plot frame rect is unreadable" as *u8) } 62 // the heuristic critic: the emitter's own widths, re-derived from the SVG 63 cr_boxes(nl, names, lfont, lbold, 0 as *i64, 0 as *i64, lwh, lhh) 64 let crith: *i64 = p_alloc_i64(CL_CRIT_W) 65 cr_critic(nl, mx, my, mr, lx, ly, lwh, lhh, frame, crith) 66 // the metric critic: real advances from the named faces 67 let fhr: *i64 = tf_load(freg) 68 var fhb: *i64 = tf_load(fbold) 69 if (fhb as i64) == 0 { fhb = fhr } 70 var metric_ok: i64 = 1 71 if (fhr as i64) == 0 { metric_ok = 0 } 72 let critm: *i64 = p_alloc_i64(CL_CRIT_W) 73 if metric_ok == 1 { 74 gv_puts(" font=" as *u8); gv_puts(freg); p_kv("upem" as *u8, tf_upem(fhr)); p_kv("glyphs" as *u8, tf_nglyphs(fhr)); gv_puts("\n" as *u8) 75 cr_boxes(nl, names, lfont, lbold, fhr, fhb, lwm, lhm) 76 cr_critic(nl, mx, my, mr, lx, ly, lwm, lhm, frame, critm) 77 } 78 var i: i64 = 0 79 while i < nl { 80 gv_puts(" label " as *u8); gv_num(i); gv_puts(" name=" as *u8); gv_puts(cr_name_at(names, i)) 81 p_kv("x" as *u8, lx[i]); p_kv("box_y" as *u8, ly[i]); p_kv("font" as *u8, lfont[i]); p_kv("bold" as *u8, lbold[i]) 82 p_kv("heuristic_w" as *u8, lwh[i]) 83 if metric_ok == 1 { p_kv("metric_w" as *u8, lwm[i]); p_kv("delta_permil" as *u8, cr_delta_permil(lwh[i], lwm[i])) } 84 gv_puts("\n" as *u8) 85 i = i + 1 86 } 87 p_crit(" heuristic:" as *u8, crith) 88 if metric_ok == 1 { p_crit(" metric: " as *u8, critm) } else { gv_puts(" metric: UNMEASURED (font unreadable)\n" as *u8) } 89 // the published claim 90 let jl: *i64 = p_alloc_i64(2) 91 let j: *u8 = sys_read_file(api, jl) 92 if (j as i64) == 0 { return p_unproven("api.json unreadable" as *u8) } 93 let pll: i64 = cr_json_int(j, jl[0], P_BLOCK, "label_overlaps" as *u8) 94 let plm: i64 = cr_json_int(j, jl[0], P_BLOCK, "labels_over_marks" as *u8) 95 let poff: i64 = cr_json_int(j, jl[0], P_BLOCK, "off_canvas" as *u8) 96 let pmm: i64 = cr_json_int(j, jl[0], P_BLOCK, "mark_overlaps" as *u8) 97 let psp: i64 = cr_json_int(j, jl[0], P_BLOCK, "spread_permil" as *u8) 98 let pdef: i64 = cr_json_int(j, jl[0], P_BLOCK, "defects" as *u8) 99 gv_puts(" published:" as *u8); p_kv("LL" as *u8, pll); p_kv("LM" as *u8, plm); p_kv("OFF" as *u8, poff); p_kv("MM" as *u8, pmm); p_kv("SPREAD" as *u8, psp); p_kv("DEFECTS" as *u8, pdef); gv_puts("\n" as *u8) 100 if pll == CR_ABSENT { return p_unproven("api.json carries no readability_cut grade" as *u8) } 101 var reproduced: i64 = 1 102 if crith[CL_C_LL] != pll { reproduced = 0 } 103 if crith[CL_C_LM] != plm { reproduced = 0 } 104 if crith[CL_C_OFF] != poff { reproduced = 0 } 105 if crith[CL_C_MM] != pmm { reproduced = 0 } 106 if crith[CL_C_SPREAD] != psp { reproduced = 0 } 107 p_kv("self_grade_reproduced_from_svg" as *u8, reproduced) 108 if metric_ok == 0 { gv_puts("\n" as *u8); return p_unproven("the metric ruler could not read the font" as *u8) } 109 var agree: i64 = 0 110 if critm[CL_C_DEFECTS] == pdef { agree = 1 } 111 p_kv("metric_defects_equal_published" as *u8, agree); gv_puts("\n" as *u8) 112 if reproduced == 1 { if agree == 1 { gv_puts("verdict=AGREE\n" as *u8); sys_exit(P_EXIT_AGREE); return P_EXIT_AGREE } } 113 gv_puts("verdict=DISAGREE\n" as *u8) 114 sys_exit(P_EXIT_DISAGREE) 115 return P_EXIT_DISAGREE 116}