Debian Codegraph @

pua2ot/src/mklist.js

raw
1 /*
2 PUA2OT: A utility for converting MUFI PUA characters to standard
3 Unicode with Junicode's OpenType features.
4
5 Code for charlist.html, a test and reference page for pua2ot.
6
7 Copyright © 2025-26 by Peter S. Baker
8 Licensed under the Apache License, version 2.0:
9 https://www.apache.org/licenses/LICENSE-2.0
10 */
11
12
13 const otagpref = ['otag', 'utag', 'zwj', 'entity', 'enla'];
14 const utagpref = ['utag', 'otag', 'zwj', 'entity', 'enla'];
15 const zwjpref = ['zwj', 'utag', 'otag', 'entity', 'enla'];
16 const entitypref = ['entity', 'zwj', 'utag', 'otag', 'enla'];
17 const enlapref = ['enla', 'entity', 'zwj', 'utag', 'otag'];
18
19 //const table = document.getElementById('tbl');
20 const dialog = document.getElementById('method_info');
21 const info_para = document.getElementById("dialogtext");
22 const dlg_header = document.getElementById("dialogheadertext");
23 const tbl_head = document.getElementById("table_head");
24 const tbl_body = document.getElementById("table_body");
25 const copy_text = document.getElementById("copyText");
26
27 let isMark = false;
28
29 let lastSelection = null;
30
31 /**
32 * Converts a string hex code to a character.
33 *
34 * @param {string} hexString - The hex code of the desired character.
35 * @returns {string} A one-character string.
36 */
37 function hexToChar(hexString) {
38 let i = parseInt(hexString, 16);
39 return String.fromCodePoint(i);
40 }
41
42 /**
43 * For each character in a string, get the hex code and save it
44 * in a space-separated list.
45 *
46 * @param {string} s - A string to get the hex codes for.
47 * @returns {string} string containing the hex codes for each character in a string.
48 */
49 function makeHex(s) {
50 let result = "";
51 for (const c of s) {
52 const i = c.codePointAt(0);
53 let hexstr = i.toString(16).toUpperCase();
54 while (hexstr.length < 4) {
55 hexstr = '0' + hexstr;
56 }
57 if (result.length > 0) {
58 result = result + ' ';
59 }
60 result = result + hexstr
61 }
62 return result;
63 }
64
65 /**
66 * Make one row of the table generated by this script.
67 *
68 * @param {string} hex - The hex code for this row.
69 * @param {string} vartext - Text for the "var" column of the table.
70 * @param {Object} block - The block recording the methods for this row.
71 * @param {string} desc - The MUFI description for this glyph.
72 * @returns {object} The row generated by this function.
73 */
74 function makeRow(hex, vartext, block, desc) {
75 let ch = hexToChar(hex);
76 //
77 // HEX CODE
78 //
79 const row = document.createElement("tr");
80 const hexcell = document.createElement("td");
81 cellContent = hex;
82 hexcell.innerHTML = cellContent;
83 hexcell.classList.add("hex");
84 row.appendChild(hexcell);
85 //
86 // VAR LABEL
87 //
88 const varcell = document.createElement("td");
89 varcell.classList.add("varc");
90 if (vartext != null) {
91 varcell.textContent = vartext;
92 }
93 row.appendChild(varcell);
94 //
95 // DESCRIPTION
96 //
97 const desccell = document.createElement("td");
98 desccell.classList.add("desc");
99 desccell.innerHTML = desc.toLowerCase();
100 row.appendChild(desccell);
101 //
102 // PUA CHARACTER
103 //
104 const puacell = document.createElement("td");
105 puacell.classList.add("char");
106 let b = ch;
107 if (isMark) {
108 b = "\u25CC" + b;
109 }
110 puacell.textContent = b;
111 row.appendChild(puacell);
112 //
113 // BASE
114 //
115 const basecell = document.createElement("td");
116 basecell.classList.add("basec");
117 if ("base" in block) {
118 b = block["base"];
119 if (isMark) {
120 b = "\u25CC" + b;
121 }
122 basecell.textContent = b;
123 }
124 row.appendChild(basecell);
125 //
126 // OTAG
127 //
128 const otagcell = document.createElement("td");
129 otagcell.classList.add("otag");
130 if ("otag" in block) {
131 options.prefList = otagpref;
132 b = ch;
133 if (isMark) {
134 b = "\u25CC" + b;
135 }
136 otagcell.innerHTML = convert(b);
137 }
138 row.appendChild(otagcell);
139 //
140 // UTAG
141 //
142 const utagcell = document.createElement("td");
143 utagcell.classList.add("utag");
144 if ("utag" in block) {
145 options.prefList = utagpref;
146 b = convert(ch);
147 if (isMark) {
148 b = "\u25CC" + b;
149 }
150 utagcell.innerHTML = b;
151 }
152 row.appendChild(utagcell);
153 //
154 // ZWJ
155 //
156 const zwjcell = document.createElement("td");
157 zwjcell.classList.add("zwj");
158 if ("zwj" in block) {
159 options.prefList = zwjpref;
160 b = convert(ch);
161 if (isMark) {
162 b = "\u25CC" + b;
163 }
164 zwjcell.innerHTML = b;
165 }
166 row.appendChild(zwjcell);
167 //
168 // ENTITY
169 //
170 const entitycell = document.createElement("td");
171 entitycell.classList.add("entity");
172 if ("entity" in block) {
173 options.prefList = entitypref;
174 b = convert(ch);
175 if (isMark) {
176 b = "\u25CC" + b;
177 }
178 entitycell.textContent = b;
179 }
180 row.appendChild(entitycell);
181 //
182 // ENLA
183 //
184 const enlacell = document.createElement("td");
185 enlacell.classList.add("enla");
186 if ("enla" in block) {
187 options.prefList = enlapref;
188 b = convert(ch);
189 enlacell.innerHTML = b;
190 }
191 row.appendChild(enlacell);
192
193 return row;
194 }
195
196 /**
197 * Make a cell for the table header.
198 *
199 * @param {string} name - A name for this cell.
200 * @param {object} parent - the parent element to which we will add this cell.
201 */
202 function tableHeaderCell(name, parent) {
203 const cell = document.createElement("th");
204 cell.textContent = name;
205 parent.appendChild(cell);
206 }
207
208 /**
209 * Figures out which members two arrays have in common and returns
210 * an array with just those members.
211 *
212 * @param {array} a - one of the arrays to compare.
213 * @param {array} b - the other array to comnpare.
214 * @returns {array} an array containing the common elements.
215 */
216 function intersect(a, b) {
217 var t;
218 if (b.length > a.length) t = b, b = a, a = t;
219 return a.filter(function (e) {
220 return b.indexOf(e) > -1;
221 });
222 }
223
224 /**
225 * Copies text to the clipboard.
226 *
227 * @param {string} t - The text to copy.
228 */
229 async function toClipboard(t) {
230 try {
231 await navigator.clipboard.writeText(t);
232 } catch (err) {
233 console.error('Failed to copy text: ', err);
234 }
235 }
236
237 /*
238 Helper for "Filter" input event.
239 */
240 function filterTable () {
241 let input, filter, table, tr, td, i, txtValue;
242 input = document.getElementById("tblfilter");
243 filter = input.value.toUpperCase();
244 isFilterHex = isHex(filter);
245 table = document.getElementById("tbl");
246 tr = table.getElementsByTagName("tr");
247 for (i = 0; i < tr.length; i++) {
248 td = tr[i].getElementsByTagName("td")[isFilterHex ? 0 : 2];
249 if (td) {
250 txtValue = td.textContent || td.innerText;
251 idx = txtValue.toUpperCase().indexOf(filter);
252 tr[i].style.display = (isFilterHex ? idx == 0 : idx > -1) ? "" : "none";
253 }
254 }
255 }
256
257 document.getElementById("tblfilter").addEventListener("input", function(e) {
258 filterTable();
259 });
260
261 /**
262 * Sets up listeners that make many cells in the table
263 * clickable.
264 */
265 function setupListeners() {
266 tbl_body.addEventListener('click', function(e) {
267 tgt = e.target.closest('td');
268 if (lastSelection != null) {
269 lastSelection.style = '';
270 }
271 lastSelection = tgt;
272 const cl = Array.from(tgt.classList);
273 if (intersect(cl, otagpref).length > 0) {
274 let tc = tgt.innerHTML;
275 tc = tc.replaceAll("&amp;", "&");
276 tc = tc.replaceAll("\u200D", "&zwj;");
277 if (tc.length > 0) {
278 tgt.style.backgroundColor = "DarkSeaGreen";
279 dlg_header.innerHTML = "Character code:";
280 info_para.innerText = tc;
281 dialog.showModal();
282 }
283 } else if (cl.includes("basec")) {
284 tgt.style.backgroundColor = "DarkSeaGreen";
285 let hs = tgt.innerText;
286 dlg_header.innerHTML = "Base code points:";
287 info_para.innerText = makeHex(hs);
288 dialog.showModal();
289 }
290 })
291 copy_text.addEventListener('click', function(e) {
292 toClipboard(info_para.innerText);
293 });
294 }
295
296 // We need to be able to see these Unicode tags.
297 options.utagEntities = true;
298
299 let toprow = document.createElement("tr");
300 tableHeaderCell("hex", toprow);
301 tableHeaderCell("var", toprow);
302 tableHeaderCell("description", toprow);
303 tableHeaderCell("pua", toprow);
304 tableHeaderCell("base", toprow);
305 tableHeaderCell("otag", toprow);
306 tableHeaderCell("utag", toprow);
307 tableHeaderCell("zwj", toprow);
308 tableHeaderCell("entity", toprow);
309 tableHeaderCell("enla", toprow);
310 tbl_head.appendChild(toprow);
311
312 let PUA_LIST = [];
313 for (h in PUA_DATA) {
314 PUA_LIST.push(hex2int(h));
315 }
316 let puaCounter = 0
317 PUA_LIST.sort((a, b) => a - b);
318 for (const hi of PUA_LIST) {
319 const h = int2hex(hi);
320 let description = "";
321 let block = PUA_DATA[h];
322 if ("desc" in block) {
323 description = block.desc;
324 isMark = block.desc.includes("COMBINING");
325 } else {
326 isMark = false;
327 }
328 let topbase = "";
329 if ("base" in block) {
330 topbase = block["base"];
331 }
332 if ("var" in block) {
333 dbl = block["var"];
334 for (d in dbl) {
335 const varbackup = options.basePreferences;
336 options.basePreferences = [d];
337 let r = makeRow(h, d, dbl[d], description);
338 tbl_body.appendChild(r);
339 options.basePreferences = varbackup;
340 puaCounter += 1;
341 }
342 } else {
343 r = makeRow(h, null, block, description);
344 tbl_body.appendChild(r);
345 puaCounter += 1;
346 }
347 }
348 console.log("Rows:", puaCounter);
349
350 setupListeners();