| 1 | (* Widths: makes the tables of lib/Width.raz from Unicode's data. |
| 2 | |
| 3 | Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt |
| 4 | |
| 5 | reads three files of Unicode's character database, which |
| 6 | https://www.unicode.org/Public/UCD/latest/ucd/ has, and writes the |
| 7 | tables of lib/Width.raz again, the lines between (* tables *) and |
| 8 | (* end of the tables *), with the version of Unicode they come from. |
| 9 | The rest of the file stays as it is. Width's opening comment says |
| 10 | what each width is; this program is the same rules, in code. *) |
| 11 | module Widths; |
| 12 | import Out, Rt, Str, Buf, Heap, Files; |
| 13 | |
| 14 | const |
| 15 | Last = 10FFFFH; (* the last code point *) |
| 16 | Start = "(* tables *)\n"; |
| 17 | Stop = "(* end of the tables *)"; |
| 18 | |
| 19 | var |
| 20 | w: [Last + 1]U8; (* each character's width, 1 unless a file says *) |
| 21 | |
| 22 | (* The whole file at path. *) |
| 23 | proc load(path: []const U8; var b: Buf.Buf): []const U8; |
| 24 | begin |
| 25 | Buf.init(b, Heap.heap); |
| 26 | if not Files.read(path, b) then |
| 27 | Out.str(path); Out.str(": can't be read\n"); |
| 28 | Rt.exit(1); |
| 29 | end; |
| 30 | return Buf.bytes(b); |
| 31 | end; |
| 32 | |
| 33 | (* s in hexadecimal, a code point; stops the program if it isn't one. *) |
| 34 | proc point(s: []const U8): INT; |
| 35 | var v: U64; ok: BOOL; |
| 36 | begin |
| 37 | v, ok := Str.toHex(Str.trim(s)); |
| 38 | if not ok or (v > Last) then |
| 39 | Out.str("not a code point: "); Out.str(s); Out.ln; |
| 40 | Rt.exit(1); |
| 41 | end; |
| 42 | return INT(v); |
| 43 | end; |
| 44 | |
| 45 | (* Sets the width of the characters from lo to hi. *) |
| 46 | proc mark(lo, hi: INT; width: U8); |
| 47 | var c: INT; |
| 48 | begin |
| 49 | for c := lo to hi do w[c] := width; end; |
| 50 | end; |
| 51 | |
| 52 | (* EastAsianWidth.txt: lines of a code point, or a range lo..hi, ";" |
| 53 | and the property; W and F are 2 columns. Its first line names the |
| 54 | file, and so the version. *) |
| 55 | proc eastAsian(text: []const U8): []const U8; |
| 56 | var line, rest, r, p, lo, hi, version: []const U8; ok: BOOL; |
| 57 | begin |
| 58 | line, rest, ok := Str.cut(text, "\n"); |
| 59 | _, version, ok := Str.cut(line, "-"); |
| 60 | version, _, ok := Str.cut(version, ".txt"); |
| 61 | rest := text; |
| 62 | while LEN(rest) > 0 do |
| 63 | line, rest, ok := Str.cut(rest, "\n"); |
| 64 | line, _, ok := Str.cut(line, "#"); |
| 65 | r, p, ok := Str.cut(line, ";"); |
| 66 | if ok then |
| 67 | p := Str.trim(p); |
| 68 | if (p = "W") or (p = "F") then |
| 69 | lo, hi, ok := Str.cut(r, ".."); |
| 70 | if ok then mark(point(lo), point(hi), 2); else mark(point(lo), point(lo), 2); end; |
| 71 | end; |
| 72 | end; |
| 73 | end; |
| 74 | return version; |
| 75 | end; |
| 76 | |
| 77 | (* UnicodeData.txt: lines of fields between ";", the code point, the |
| 78 | name and the general category first. A range is two lines, its |
| 79 | first and last, named "<..., First>" and "<..., Last>". *) |
| 80 | proc categories(text: []const U8); |
| 81 | var line, rest, c, name, gc: []const U8; ok: BOOL; first, n: INT; |
| 82 | begin |
| 83 | rest := text; |
| 84 | first := -1; |
| 85 | while LEN(rest) > 0 do |
| 86 | line, rest, ok := Str.cut(rest, "\n"); |
| 87 | c, line, ok := Str.cut(line, ";"); |
| 88 | name, line, ok := Str.cut(line, ";"); |
| 89 | gc, line, ok := Str.cut(line, ";"); |
| 90 | if not ok then continue; end; |
| 91 | n := point(c); |
| 92 | if Str.hasSuffix(name, ", First>") then first := n; continue; end; |
| 93 | if not Str.hasSuffix(name, ", Last>") then first := n; end; |
| 94 | if ((gc = "Mn") or (gc = "Me") or (gc = "Cf") or (gc = "Cc")) and (n # 0ADH) then |
| 95 | mark(first, n, 0); (* the soft hyphen is seen: 1 *) |
| 96 | end; |
| 97 | end; |
| 98 | end; |
| 99 | |
| 100 | (* PropList.txt: lines of a code point, or a range lo..hi, ";" and a |
| 101 | property; a Prepended_Concatenation_Mark, a sign set over the digits |
| 102 | after it, is seen, so 1 column, though a format character. *) |
| 103 | proc concatenation(text: []const U8); |
| 104 | var line, rest, r, p, lo, hi: []const U8; ok: BOOL; |
| 105 | begin |
| 106 | rest := text; |
| 107 | while LEN(rest) > 0 do |
| 108 | line, rest, ok := Str.cut(rest, "\n"); |
| 109 | line, _, ok := Str.cut(line, "#"); |
| 110 | r, p, ok := Str.cut(line, ";"); |
| 111 | if ok and (Str.trim(p) = "Prepended_Concatenation_Mark") then |
| 112 | lo, hi, ok := Str.cut(r, ".."); |
| 113 | if ok then mark(point(lo), point(hi), 1); else mark(point(lo), point(lo), 1); end; |
| 114 | end; |
| 115 | end; |
| 116 | end; |
| 117 | |
| 118 | (* c as a constant of raz: 0, six hexadecimal digits and H. *) |
| 119 | proc hex(var b: Buf.Buf; c: INT); |
| 120 | const Digits = "0123456789ABCDEF"; |
| 121 | var i: INT; |
| 122 | begin |
| 123 | Buf.add(b, '0'); |
| 124 | for i := 5 to 0 by -1 do Buf.add(b, Digits[(c shr (4 * i)) & 15]); end; |
| 125 | Buf.add(b, 'H'); |
| 126 | end; |
| 127 | |
| 128 | (* The constant name, the ranges of characters whose width is width, |
| 129 | each its first and last, four ranges to a line. *) |
| 130 | proc table(var b: Buf.Buf; name: []const U8; width: U8); |
| 131 | var c, lo, n: INT; |
| 132 | begin |
| 133 | Buf.str(b, " "); Buf.str(b, name); Buf.str(b, ": []I32 = ["); |
| 134 | n := 0; |
| 135 | c := 0; |
| 136 | while c <= Last do |
| 137 | if w[c] # width then INC(c); continue; end; |
| 138 | lo := c; |
| 139 | while (c < Last) and (w[c + 1] = width) do INC(c); end; |
| 140 | if n > 0 then Buf.str(b, ","); end; |
| 141 | if n mod 4 = 0 then Buf.str(b, "\n "); else Buf.str(b, " "); end; |
| 142 | hex(b, lo); Buf.str(b, ", "); hex(b, c); |
| 143 | INC(n); |
| 144 | INC(c); |
| 145 | end; |
| 146 | Buf.str(b, "];\n"); |
| 147 | end; |
| 148 | |
| 149 | proc main*; |
| 150 | var src, eaw, ucd, props, out: Buf.Buf; s, before, after, version: []const U8; ok: BOOL; |
| 151 | begin |
| 152 | if LEN(Files.arg(4)) = 0 then |
| 153 | Out.str("usage: Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt\n"); |
| 154 | Rt.exit(2); |
| 155 | end; |
| 156 | s := load(Files.arg(1), src); |
| 157 | before, after, ok := Str.cut(s, Start); |
| 158 | if ok then _, after, ok := Str.cut(after, Stop); end; |
| 159 | if not ok then |
| 160 | Out.str(Files.arg(1)); Out.str(": no (* tables *) and (* end of the tables *)\n"); |
| 161 | Rt.exit(1); |
| 162 | end; |
| 163 | mark(0, Last, 1); |
| 164 | version := eastAsian(load(Files.arg(2), eaw)); |
| 165 | categories(load(Files.arg(3), ucd)); |
| 166 | concatenation(load(Files.arg(4), props)); |
| 167 | mark(1160H, 11FFH, 0); (* Hangul's medial and final jamo *) |
| 168 | mark(0D7B0H, 0D7FFH, 0); |
| 169 | mark(3164H, 3164H, 0); (* the Hangul fillers, which draw nothing *) |
| 170 | mark(0FFA0H, 0FFA0H, 0); |
| 171 | Buf.init(out, Heap.heap); |
| 172 | Buf.str(out, before); |
| 173 | Buf.str(out, Start); |
| 174 | Buf.str(out, "(* Unicode "); Buf.str(out, version); Buf.str(out, " *)\nconst\n"); |
| 175 | table(out, "Zero", 0); |
| 176 | table(out, "Wide", 2); |
| 177 | Buf.str(out, Stop); |
| 178 | Buf.str(out, after); |
| 179 | if not Files.write(Files.arg(1), Buf.bytes(out)) then |
| 180 | Out.str(Files.arg(1)); Out.str(": can't be written\n"); |
| 181 | Rt.exit(1); |
| 182 | end; |
| 183 | end; |
| 184 | |
| 185 | end Widths. |