| 1 |
/* |
| 2 |
PUA2OT: A utility for converting MUFI PUA characters to standard |
| 3 |
Unicode with Junicode's OpenType features. |
| 4 |
|
| 5 |
Common variables and functions |
| 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 |
/* To use this file as a Node module, uncomment the following two lines */ |
| 13 |
|
| 14 |
// puaData = require("./PUA2OT_DATA-node.js") |
| 15 |
// const PUA_DATA = puaData.data; |
| 16 |
|
| 17 |
/* |
| 18 |
Also, you must add the following to the end of PUA2OT_DATA.js. Keep it |
| 19 |
in the same directory as this file. |
| 20 |
*/ |
| 21 |
|
| 22 |
// module.exports = { |
| 23 |
// data: PUA_DATA |
| 24 |
// }; |
| 25 |
|
| 26 |
/* |
| 27 |
Finally, follow the instructions at the bottom of this file. |
| 28 |
*/ |
| 29 |
|
| 30 |
// --------------------------------------------- |
| 31 |
// CONSTANT AND VARIABLE SETUP |
| 32 |
// --------------------------------------------- |
| 33 |
|
| 34 |
/* |
| 35 |
Chars with OT feature 'case' (utags c a) applied |
| 36 |
(*some are lc): |
| 37 |
|
| 38 |
E004 E00A E0EA E099 E03A E135 E22D E309 E317 E373 |
| 39 |
*E4F0 *E516 *E737 EBEC F0C5 *F0BA |
| 40 |
*/ |
| 41 |
|
| 42 |
/** |
| 43 |
* Preference list for the "compact" style of markup. |
| 44 |
*/ |
| 45 |
const COMPACT_PREF_LIST = [ "utag", "zwj", "enla", "otag", "entity" ]; |
| 46 |
|
| 47 |
/** |
| 48 |
* Preference list for the "legible" style of markkup. |
| 49 |
*/ |
| 50 |
const LEGIBLE_PREF_LIST = [ "enla", "otag", "utag", "zwj", "entity" ]; |
| 51 |
|
| 52 |
/** |
| 53 |
* Defines Unicodes for problematic Unicodes (those that look like variants |
| 54 |
* on letters of the alphabet but are not). |
| 55 |
*/ |
| 56 |
const UNICODE_HEXES = [ '0131', '0262', '026A', '0274', '0276', '0280', '028F', |
| 57 |
'029C', '0299', '029F', '1D00', '1D01', '1D04', '1D05', |
| 58 |
'1D06', '1D07', '1D0A', '1D0B', '1D0D', '1D0F', '1D18', |
| 59 |
'1D1B', '1D1C', '1D20', '1D21', '1D22', '1D79', '2E4A', |
| 60 |
'2E53', '2E54', 'A730', 'A731', 'A75A', 'A75B', 'A779', |
| 61 |
'A77A', 'A77C', 'A77D', 'A782', 'A783', 'A784', 'A785', |
| 62 |
'A786', 'A787', 'A7AF' ]; |
| 63 |
|
| 64 |
/** |
| 65 |
* The default lookup for Unicode tags. This replaces directly with tag characters. |
| 66 |
*/ |
| 67 |
const UTAG_DICT = { "a": "\u{0E0061}", |
| 68 |
"b": "\u{0E0062}", |
| 69 |
"c": "\u{0E0063}", |
| 70 |
"d": "\u{0E0064}", |
| 71 |
"e": "\u{0E0065}", |
| 72 |
"f": "\u{0E0066}", |
| 73 |
"g": "\u{0E0067}", |
| 74 |
"h": "\u{0E0068}", |
| 75 |
"i": "\u{0E0069}", |
| 76 |
"j": "\u{0E006A}", |
| 77 |
"k": "\u{0E006B}", |
| 78 |
"l": "\u{0E006C}", |
| 79 |
"m": "\u{0E006D}", |
| 80 |
"n": "\u{0E006E}", |
| 81 |
"o": "\u{0E006F}", |
| 82 |
"p": "\u{0E0070}", |
| 83 |
"q": "\u{0E0071}", |
| 84 |
"r": "\u{0E0072}", |
| 85 |
"s": "\u{0E0073}", |
| 86 |
"t": "\u{0E0074}", |
| 87 |
"u": "\u{0E0075}", |
| 88 |
"v": "\u{0E0076}", |
| 89 |
"w": "\u{0E0077}", |
| 90 |
"x": "\u{0E0078}", |
| 91 |
"y": "\u{0E0079}", |
| 92 |
"z": "\u{0E007A}", |
| 93 |
"0": "\u{0E0030}", |
| 94 |
"1": "\u{0E0031}", |
| 95 |
"2": "\u{0E0032}", |
| 96 |
"3": "\u{0E0033}", |
| 97 |
"4": "\u{0E0034}", |
| 98 |
"5": "\u{0E0035}", |
| 99 |
"6": "\u{0E0036}", |
| 100 |
"7": "\u{0E0037}", |
| 101 |
"8": "\u{0E0038}", |
| 102 |
"9": "\u{0E0039}" }; |
| 103 |
|
| 104 |
/** |
| 105 |
* Lookup for replacing Unicode tags with entities. This makes them |
| 106 |
* visible and easy to debug. |
| 107 |
*/ |
| 108 |
const UTAG_ENTITY_DICT = { "a": "&__a;", |
| 109 |
"b": "&__b;", |
| 110 |
"c": "&__c;", |
| 111 |
"d": "&__d;", |
| 112 |
"e": "&__e;", |
| 113 |
"f": "&__f;", |
| 114 |
"g": "&__g;", |
| 115 |
"h": "&__h;", |
| 116 |
"i": "&__i;", |
| 117 |
"j": "&__j;", |
| 118 |
"k": "&__k;", |
| 119 |
"l": "&__l;", |
| 120 |
"m": "&__m;", |
| 121 |
"n": "&__n;", |
| 122 |
"o": "&__o;", |
| 123 |
"p": "&__p;", |
| 124 |
"q": "&__q;", |
| 125 |
"r": "&__r;", |
| 126 |
"s": "&__s;", |
| 127 |
"t": "&__t;", |
| 128 |
"u": "&__u;", |
| 129 |
"v": "&__v;", |
| 130 |
"w": "&__w;", |
| 131 |
"x": "&__x;", |
| 132 |
"y": "&__y;", |
| 133 |
"z": "&__z;", |
| 134 |
"0": "&__0;", |
| 135 |
"1": "&__1;", |
| 136 |
"2": "&__2;", |
| 137 |
"3": "&__3;", |
| 138 |
"4": "&__4;", |
| 139 |
"5": "&__5;", |
| 140 |
"6": "&__6;", |
| 141 |
"7": "&__7;", |
| 142 |
"8": "&__8;", |
| 143 |
"9": "&__9;" }; |
| 144 |
|
| 145 |
/** |
| 146 |
* Entities for combining marks. |
| 147 |
*/ |
| 148 |
const MARK_TAG_ENTITIES = { '\uf03a': '&_ansc;', |
| 149 |
'\uf036': '&_an;', |
| 150 |
'\uf038': '&_ar;', |
| 151 |
'\uf130': '&_arsc;', |
| 152 |
'\u{10fffc}': '&_as;', |
| 153 |
'\uf013': '&_bsc;', |
| 154 |
'\uf016': '&_dsc;', |
| 155 |
'\uf135': '&_eogo;', |
| 156 |
'\uf02f': '&_idotl;', |
| 157 |
'\uf030': '&_j;', |
| 158 |
'\uf031': '&_jdotl;', |
| 159 |
'\uf01c': '&_ksc;', |
| 160 |
'\uf136': '&_emac;', |
| 161 |
'\uf01f': '&_munc;', |
| 162 |
'\uf13e': '&_oogo;', |
| 163 |
'\uf032': '&_oslash;', |
| 164 |
'\uf13f': '&_omac;', |
| 165 |
'\uf03e': '&_orr;', |
| 166 |
'\uf03f': '&_oru;', |
| 167 |
'\uf033': '&_q;', |
| 168 |
'\uf040': '&_ru;', |
| 169 |
'\u{10fffb}': '&_sa;', |
| 170 |
'\uf02a': '&_tsc;', |
| 171 |
'\uf02b': '&_y;', |
| 172 |
'\uf03d': '&_thorn;' }; |
| 173 |
|
| 174 |
// Regular expression for testing for a cvNN tag. Used by is_cv_tag(). |
| 175 |
const cvRegEx = /^cv\d\d$/; |
| 176 |
|
| 177 |
// Regular expression for testing for a ssNN tag. Used by is_ss_tag(). |
| 178 |
const ssRegEx = /^ss\d\d$/; |
| 179 |
|
| 180 |
// Tags (aside from cvNN and ssNN) used by this program. |
| 181 |
const OTTags = ["case", "hlig", "dlig", "liga", "pcap", "smcp", "sups"]; |
| 182 |
|
| 183 |
/* |
| 184 |
For internal use. No need to document. |
| 185 |
*/ |
| 186 |
let _prefList = COMPACT_PREF_LIST; |
| 187 |
|
| 188 |
let _defaultTags = { "ss10": 1, "cv69": 7 }; |
| 189 |
|
| 190 |
let _keepUnicode = false |
| 191 |
|
| 192 |
let _nonWordTags = [ "ss04", "ss05", "ss06", "pcap", "smcp", "hlig", "case", "ENLA" ]; |
| 193 |
|
| 194 |
let _language = "en"; |
| 195 |
|
| 196 |
let _replaceMUFIEntities = true; |
| 197 |
|
| 198 |
let _utagLookup = UTAG_DICT; |
| 199 |
|
| 200 |
let _charSkip = []; |
| 201 |
|
| 202 |
//const variantLabels = [ "insular", "smallcap", "punctuation", "alpha", "mark", "currency" ]; |
| 203 |
|
| 204 |
let _basePreferences = []; |
| 205 |
|
| 206 |
let _enlargedScale = 32; |
| 207 |
|
| 208 |
const phRegExp = new RegExp("%\\^%", "g"); |
| 209 |
|
| 210 |
/* |
| 211 |
* Object mapping MUFI PUA hexes to entries in the database. We build this from the |
| 212 |
* database rather than coding it into this file. |
| 213 |
*/ |
| 214 |
let entityDict = {}; |
| 215 |
for (const pua_code in PUA_DATA) { |
| 216 |
const mufi_entity = PUA_DATA[pua_code].mntity; |
| 217 |
try { |
| 218 |
entityDict["\&" + mufi_entity + ";"] = String.fromCodePoint(parseInt(pua_code, 16)); |
| 219 |
} catch (error) { |
| 220 |
; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* RegExp for doing rapid searches for entities. |
| 226 |
*/ |
| 227 |
const ENTITY_MAP = new RegExp(Object.keys(entityDict).join("|"), "g"); |
| 228 |
|
| 229 |
// ---------------------- |
| 230 |
// OPTIONS |
| 231 |
// ---------------------- |
| 232 |
|
| 233 |
/** |
| 234 |
* Stores options for controlling the behavior of this script. |
| 235 |
* |
| 236 |
* @property {string} language - The language of the text being converted. |
| 237 |
* @property {boolean} utagEntities - Whether to show entities for Unicode tags. |
| 238 |
* @property {{string: string}} utagLookup - Get or set lookups for the utag method. |
| 239 |
* @property {string[]} nonWordTags - List of OpenType features that can be applied only to letters. |
| 240 |
* @property {boolean} keepUnicode - Whether to keep problematic Unicodes. |
| 241 |
* @property {string} methodPriority - Selects a predefined priority list: "compact" or "legible". |
| 242 |
* @property {string[]} prefList - Supplies a custom priority list. |
| 243 |
* @property {{string: number}} defaultTags - OpenType features that are always on. |
| 244 |
* @property {string[]} basePreferences - Different bases for certain categories of character. |
| 245 |
* @property {boolean} replaceMUFIEntities - Whether to search and replace MUFI entities. |
| 246 |
* @property {number} enlargedScale - Scale (0-100) for Enlarge axis. |
| 247 |
* @property {string[]} charSkip - List of characters to skip (not convert). |
| 248 |
* @type {{string: string | boolean | string[] | object}} |
| 249 |
*/ |
| 250 |
const options = {} |
| 251 |
|
| 252 |
Object.defineProperty(options, 'charSkip', { |
| 253 |
get: () => _charSkip, |
| 254 |
set: a => _charSkip = a |
| 255 |
} ); |
| 256 |
|
| 257 |
Object.defineProperty(options, 'language', { |
| 258 |
get: () => {return _language}, |
| 259 |
set: l => _language = l |
| 260 |
} ); |
| 261 |
|
| 262 |
Object.defineProperty(options, 'utagEntities', { |
| 263 |
// true if entity dictionary is being used instead of the regular one. |
| 264 |
get: () => _utagLookup === UTAG_ENTITY_DICT, |
| 265 |
set: useEntities => _utagLookup = useEntities ? UTAG_ENTITY_DICT : UTAG_DICT |
| 266 |
} ); |
| 267 |
|
| 268 |
Object.defineProperty(options, 'utagLookup', { |
| 269 |
// Get or set the dictionary used for Unicode tags. Could be used for a |
| 270 |
// custom one. |
| 271 |
get: () => _utagLookup, |
| 272 |
set: ute => {if (ute in [UTAG_ENTITY_DICT, UTAG_DICT]) {_utagLookup = ute}} |
| 273 |
} ); |
| 274 |
|
| 275 |
Object.defineProperty(options, 'nonWordTags', { |
| 276 |
// Get or set the list of non-word tags. Just an array of OpenType |
| 277 |
// feature tags. |
| 278 |
get: () => _nonWordTags, |
| 279 |
set: nwt => _nonWordTags = nwt |
| 280 |
} ); |
| 281 |
|
| 282 |
Object.defineProperty(options, 'keepUnicode', { |
| 283 |
// A boolean property. Either we replace problem Unicodes or we don't. |
| 284 |
get: () => _keepUnicode, |
| 285 |
set: ku => _keepUnicode = ku |
| 286 |
} ); |
| 287 |
|
| 288 |
Object.defineProperty(options, 'methodPriority', { |
| 289 |
// Choose between two predefined priority lists. |
| 290 |
get: () => options.prefList === COMPACT_PREF_LIST ? "compact" : "legible", |
| 291 |
set: function(p) { |
| 292 |
if (p == "compact") { |
| 293 |
options.prefList = COMPACT_PREF_LIST; |
| 294 |
} else if (p == "legible") { |
| 295 |
options.prefList = LEGIBLE_PREF_LIST; |
| 296 |
} |
| 297 |
} |
| 298 |
} ); |
| 299 |
|
| 300 |
Object.defineProperty(options, 'prefList', { |
| 301 |
// get or install a custom list of options for char replacement. |
| 302 |
get: () => _prefList, |
| 303 |
set: pfl => {if (pfl.length == 5) {_prefList = pfl}} |
| 304 |
} ); |
| 305 |
|
| 306 |
Object.defineProperty(options, 'defaultTags', { |
| 307 |
// Get or set a list of tags that should always be on (aside from |
| 308 |
// the ones that OpenType recommends to be on by default, e.g. calt, liga). |
| 309 |
get: () => _defaultTags, |
| 310 |
set: dft => _defaultTags = dft |
| 311 |
} ); |
| 312 |
|
| 313 |
Object.defineProperty(options, 'replaceMUFIEntities', { |
| 314 |
// Boolean. Whether to replace MUFI entities. |
| 315 |
get: () => _replaceMUFIEntities, |
| 316 |
set: r => _replaceMUFIEntities = r |
| 317 |
} ); |
| 318 |
|
| 319 |
Object.defineProperty(options, 'basePreferences', { |
| 320 |
// For dealing with classes of character that have more than |
| 321 |
// one possible base. The default choices maximize accessibility, |
| 322 |
// but the alternates may be good for particular purposes. |
| 323 |
// _basePreferences is an array that has any of these values |
| 324 |
// in any order: insular, smallcap, punctuation, mark, |
| 325 |
// currency, alpha. |
| 326 |
get: () => _basePreferences, |
| 327 |
set: v => _basePreferences = v |
| 328 |
} ); |
| 329 |
|
| 330 |
Object.defineProperty(options, 'enlargedScale', { |
| 331 |
get: () => _enlargedScale, |
| 332 |
set: s => _enlargedScale = s |
| 333 |
} ); |
| 334 |
|
| 335 |
// --------------------- |
| 336 |
// FUNCTIONS |
| 337 |
// --------------------- |
| 338 |
|
| 339 |
// Tests if a string is a Character Variant (cvNN) tag. Helper for is_OT_tag. |
| 340 |
const is_cv_tag = s => s.match(cvRegEx) != null; |
| 341 |
|
| 342 |
// Tests if a string is a Stylistic Set (ssNN) tag. Helper for is_OT_tag. |
| 343 |
const is_ss_tag = s => s.match(ssRegEx) != null; |
| 344 |
|
| 345 |
// Tests if a string is one of the tags (other than |
| 346 |
// ssNN or cvNN) used by this program. Helper for is_OT_tag. |
| 347 |
const is_other_tag = s => OTTags.includes(s); |
| 348 |
|
| 349 |
/** |
| 350 |
* Tests whether a string is one of the OpenType tags used by this |
| 351 |
* program. |
| 352 |
* |
| 353 |
* @param {string} - The string to test. |
| 354 |
* @return {boolean} True if this is a valid tag. |
| 355 |
*/ |
| 356 |
const is_OT_tag = (s) => is_cv_tag(s) || is_ss_tag(s) || is_other_tag(s); |
| 357 |
|
| 358 |
/** |
| 359 |
* Tests if the argument is a valid hexadecimal number. |
| 360 |
* |
| 361 |
* @param {string} s - String to test. |
| 362 |
* @returns {boolean} Result of the test. |
| 363 |
*/ |
| 364 |
function isHex(s) { |
| 365 |
return /^[0-9A-F]+$/.test(s.toUpperCase()); |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Gets the length of a string, accounting correctly for characters |
| 370 |
* that consist of more than one UTF-16 code unit. |
| 371 |
* |
| 372 |
* @param {string} s - The string whose length we want. |
| 373 |
* @return {number} Length of the string s. |
| 374 |
*/ |
| 375 |
const realLength = s => [...s].length; |
| 376 |
|
| 377 |
/** |
| 378 |
* Converts all whitespace characters to spaces and changes ampersand |
| 379 |
* entity to a plain ampersand. |
| 380 |
* |
| 381 |
* @param {string} s - The string to clean up. |
| 382 |
* @return {string} The cleaned up string. |
| 383 |
*/ |
| 384 |
const cleanup_string = s => s.replace(/\s+/g, " ").trim().replace(/&/g, '&'); |
| 385 |
|
| 386 |
/** |
| 387 |
* Gets a count of the placeholder sequences ("%^%") in a string. |
| 388 |
* |
| 389 |
* @param {string} s - The string to survey |
| 390 |
* @return {number} The number of placeholders in the string. |
| 391 |
*/ |
| 392 |
const placeholderCount = s => (s.match(phRegExp) || []).length; |
| 393 |
|
| 394 |
/** |
| 395 |
* Converts a hexadecimal string to an integer. |
| 396 |
* |
| 397 |
* @param {string} h - Hexadecimal number (as string). |
| 398 |
* @returns {number} Integer derived from the hex sring. |
| 399 |
*/ |
| 400 |
const hex2int = h => parseInt(h, 16); |
| 401 |
|
| 402 |
/** |
| 403 |
* Converts an integer to a hexadecimal string. |
| 404 |
* |
| 405 |
* @param {number} i - The number to be converted. |
| 406 |
* @returns {string} Hexadecimal representation of the number. |
| 407 |
*/ |
| 408 |
function int2hex(i) { |
| 409 |
let h = i.toString(16).toUpperCase(); |
| 410 |
while (h.length < 4) { |
| 411 |
h = '0' + h; |
| 412 |
} |
| 413 |
return h; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Converts an object containing OpenType tags with indices into a |
| 418 |
* string that can be used with CSS font-feature-settings. |
| 419 |
* |
| 420 |
* @param {{string: number}} Object containing tags and indices. |
| 421 |
* @return {string} A string that can be used with CSS font-feature-settings. |
| 422 |
*/ |
| 423 |
function featureString(tag_dict) { |
| 424 |
let result_string = ""; |
| 425 |
for (const key in tag_dict) { |
| 426 |
let idx = tag_dict[key]; |
| 427 |
if (result_string.length > 0) { |
| 428 |
result_string += ", "; |
| 429 |
} |
| 430 |
result_string = result_string + "'" + key + "'"; |
| 431 |
if (idx > 1) { |
| 432 |
result_string = result_string + " " + idx.toString(); |
| 433 |
} |
| 434 |
} |
| 435 |
return result_string; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Tests whether a character is MUFI PUA or problematic Unicode |
| 440 |
* (note that the return value may depend on the value of option.keepUnicodes). |
| 441 |
* |
| 442 |
* @param {string} ch - the character to test. |
| 443 |
* @return {boolean} True if character is MUFI PUA or problematic Unicode. |
| 444 |
*/ |
| 445 |
function isReplaceable(ch) { |
| 446 |
const hexstr = int2hex(ch.codePointAt(0)); |
| 447 |
if (_charSkip.includes(hexstr)) { |
| 448 |
return false; |
| 449 |
} |
| 450 |
if (UNICODE_HEXES.includes(hexstr)) { |
| 451 |
return !_keepUnicode; |
| 452 |
} |
| 453 |
return (hexstr in PUA_DATA); |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* Tests whether a feature is on. This is a helper for resolveOtag(). |
| 458 |
* |
| 459 |
* @param {{string: number}} feature_dict - one-entry Object with the feature we're testing for. |
| 460 |
* @param {{string: number}} current_features - features currently on. |
| 461 |
* @return {boolean} True if the feature is currently on. |
| 462 |
*/ |
| 463 |
function isFeatureOn(feature_dict, current_features) { |
| 464 |
let tagname = feature_dict.name; |
| 465 |
let tagindex = 1; |
| 466 |
if ("index" in feature_dict) { |
| 467 |
tagindex = feature_dict.index; |
| 468 |
} |
| 469 |
try { |
| 470 |
return (current_features[tagname] == tagindex); |
| 471 |
} catch (error) { |
| 472 |
; |
| 473 |
} |
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
/* |
| 478 |
* Helper for resolveUtag(), applyEnlargeAxis(), and resolveZwj(). |
| 479 |
* Given a list of |
| 480 |
* Unicode tags and a string, this function substitutes pairs of |
| 481 |
* tags for placeholders (%^%) or, if there are no placeholders, |
| 482 |
* appends them to the end of the string. |
| 483 |
* The number of tags in taglist should be double the number of |
| 484 |
* placeholders in the string. If it is shorter, the function |
| 485 |
* will repeat the last pair of tags until the placeholders are |
| 486 |
* all replaced. |
| 487 |
* |
| 488 |
* @param {string} base - The string that forms the basis of the |
| 489 |
* output of resolveUtag() or resolveZwj(). |
| 490 |
* @param {array} taglist - Array of one-character strings representing utags. |
| 491 |
* @return {string} The base with tags merged in. |
| 492 |
*/ |
| 493 |
function mergeTags(base, taglist) { |
| 494 |
if (taglist.length >= 2) { |
| 495 |
let utagList = []; |
| 496 |
for (const idx in taglist) { |
| 497 |
utagList.push(_utagLookup[taglist[idx].name]); |
| 498 |
} |
| 499 |
let phCount = placeholderCount(base) |
| 500 |
if (phCount == 0) { |
| 501 |
let utags = ""; |
| 502 |
if (utagList.length >= 2) { |
| 503 |
for (idx in utagList) { |
| 504 |
utags = utags + utagList[idx]; |
| 505 |
} |
| 506 |
} |
| 507 |
base = base + utags; |
| 508 |
} else { |
| 509 |
while (phCount > 0) { |
| 510 |
let utags = utagList[0] + utagList[1]; |
| 511 |
base = base.replace(/%\^%/, utags); |
| 512 |
phCount = placeholderCount(base); |
| 513 |
if (utagList.length >= 4) { |
| 514 |
utagList.shift(); |
| 515 |
utagList.shift(); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
return base; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Applies the ENLA axis to a base and returns the necessary |
| 525 |
* markup. |
| 526 |
* |
| 527 |
* @param {object} enla - Information for applying the ENLA axis. |
| 528 |
* @param {string} base - The base for this operation. |
| 529 |
* @param {array} current_tags - current active OT tags (not used). |
| 530 |
* @returns {string} string with base and markup. |
| 531 |
*/ |
| 532 |
function applyEnlargeAxis(enla, base, current_tags) { |
| 533 |
let loclbase = ("base" in enla) ? enla.base : base; |
| 534 |
if ("utags" in enla) { |
| 535 |
taglist = enla.utags; |
| 536 |
if (taglist.length >= 2) { |
| 537 |
loclbase = mergeTags(loclbase, taglist); |
| 538 |
} |
| 539 |
} |
| 540 |
t = `<span style="font-variation-settings: 'ENLA' ${_enlargedScale}">`; |
| 541 |
return t + loclbase + "</span>"; |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* An otag block specifies the OpenType features we need to activate |
| 546 |
* to represent this "base". These are added to the features |
| 547 |
* that are already on, and then we call featureString() to |
| 548 |
* generate the correctly formatted arguments for |
| 549 |
* font_feature_settings, which we plug into a "style" |
| 550 |
* attribute of a <span>. We wrap the "base" into the <span>. |
| 551 |
* |
| 552 |
* @param {object} otag - The block of json data we are using for the transformation. |
| 553 |
* @param {string} base - The letter(s) we are transforming. |
| 554 |
* @param {array} current_tags - array of OpenType features currently on. |
| 555 |
* @param {number} current_pass - 0 for first pass, 1 for second. |
| 556 |
* @param {string} tags_name - name of the tags key to look for |
| 557 |
* @return {array} - Array with converted base and number of replacements made. |
| 558 |
*/ |
| 559 |
function resolveOtag(otag, base, current_tags, current_pass=0, tags_name="tags") { |
| 560 |
let word_tag_count = 0; |
| 561 |
let loclbase = base; |
| 562 |
if ("base" in otag) { |
| 563 |
loclbase = otag.base; |
| 564 |
} |
| 565 |
loclbase = loclbase.replaceAll("%\^%", ""); |
| 566 |
let taglist = otag[tags_name]; |
| 567 |
// Pass 0 collects tags that can safely be applied to the whole word. |
| 568 |
if (current_pass == 0) { |
| 569 |
let target_is_word = true; |
| 570 |
for (const idx in taglist) { |
| 571 |
let tagname = taglist[idx].name; |
| 572 |
if (_nonWordTags.includes(tagname)) { |
| 573 |
target_is_word = false; |
| 574 |
} |
| 575 |
} |
| 576 |
if (target_is_word) { |
| 577 |
for (const idx in taglist) { |
| 578 |
let tag_item = taglist[idx]; |
| 579 |
if (!isFeatureOn(tag_item, current_tags)) { |
| 580 |
let tagindex = 1; |
| 581 |
if ("index" in tag_item) { |
| 582 |
tagindex = tag_item.index; |
| 583 |
} |
| 584 |
current_tags[tag_item.name] = tagindex; |
| 585 |
word_tag_count += 1; |
| 586 |
} |
| 587 |
} |
| 588 |
return [loclbase, word_tag_count]; |
| 589 |
} |
| 590 |
return ["", word_tag_count]; |
| 591 |
} |
| 592 |
// Will only execute the following if current_pass == 1. Pass 1 looks |
| 593 |
// for characters that should be enclosed in their own <span> |
| 594 |
// elements. |
| 595 |
let local_tag_count = 0; |
| 596 |
let target_is_char = false; |
| 597 |
let local_tags = structuredClone(current_tags); |
| 598 |
for (const idx in taglist) { |
| 599 |
if (_nonWordTags.includes(taglist[idx].name)) { |
| 600 |
target_is_char = true; |
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
if (target_is_char) { |
| 605 |
for (const idx in taglist) { |
| 606 |
let tag_dict = taglist[idx]; |
| 607 |
if (!isFeatureOn(tag_dict, current_tags)) { |
| 608 |
let tagindex = ("index" in tag_dict) ? tag_dict.index : 1; |
| 609 |
local_tags[tag_dict.name] = tagindex; |
| 610 |
local_tag_count += 1; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
if (local_tag_count > 0) { |
| 615 |
let tag_string = featureString(local_tags) + ";"; |
| 616 |
let open_tag = `<span style="font-feature-settings: ${tag_string}">`; |
| 617 |
let close_tag = "</span>"; |
| 618 |
return [open_tag + loclbase + close_tag, local_tag_count]; |
| 619 |
} |
| 620 |
return [loclbase, local_tag_count]; |
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* Forms a ligature by inserting a zero-width joiner (zwj) before each |
| 625 |
* alphabetic character (Unicode categories Ll or Lu). If the zwj block |
| 626 |
* has a "utags" key, merge the utags into the base. A zwj block can |
| 627 |
* also have a "tags" section: if so, run resolveOtag after the other |
| 628 |
* processing. |
| 629 |
* |
| 630 |
* @param {object} zwj - The block of json data we are using for the transformation. |
| 631 |
* @param {string} base - The letter(s) we are transforming. |
| 632 |
* @param {array} current_tags - array of OpenType features currently on. |
| 633 |
* @param {number} current_pass - 0 for first pass, 1 for second. |
| 634 |
* @return {string} - the converted base. |
| 635 |
*/ |
| 636 |
function resolveZwj(zwj, base, current_tags, current_pass=1) { |
| 637 |
const loclbase = ("base" in zwj) ? zwj.base : base; |
| 638 |
if (loclbase.length < 2) { |
| 639 |
return loclbase; |
| 640 |
} |
| 641 |
let result_string = ""; |
| 642 |
let otag_result = []; |
| 643 |
for (const character of loclbase) { |
| 644 |
if (result_string.length > 0) { |
| 645 |
let isLetter = /\p{Letter}/u.test(character); |
| 646 |
if (isLetter) { |
| 647 |
result_string = result_string + "‍"; |
| 648 |
} |
| 649 |
} |
| 650 |
result_string = result_string + character; |
| 651 |
} |
| 652 |
if ("utags" in zwj) { |
| 653 |
taglist = zwj.utags; |
| 654 |
if (taglist.length >= 2) { |
| 655 |
result_string = mergeTags(result_string, taglist); |
| 656 |
} |
| 657 |
} |
| 658 |
if ("otags" in zwj) { |
| 659 |
otag_result = resolveOtag(zwj, result_string, current_tags, |
| 660 |
current_pass); |
| 661 |
} |
| 662 |
if (otag_result > 0) { |
| 663 |
return otag_result[0]; |
| 664 |
} |
| 665 |
return result_string; |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Utags are Junicode's alternative to cvNN for producing |
| 670 |
* characters. It can greatly reduce the amount of html required |
| 671 |
* in a file. If an OpenType tag is needed, just call |
| 672 |
* resolveOtag. If there's an error we just return the base. |
| 673 |
* |
| 674 |
* @param {object} utag - The block of json data we are using for the transformation. |
| 675 |
* @param {string} base - The letter(s) we are transforming. |
| 676 |
* @param {array} current_tags - array of OpenType features currently active. |
| 677 |
* @param {number} current_pass - 0 for first pass, 1 for second. |
| 678 |
* @return {string} - the converted base. |
| 679 |
*/ |
| 680 |
function resolveUtag(utag, base, current_tags, current_pass=1) { |
| 681 |
let loclbase = "base" in utag ? utag.base : base; |
| 682 |
taglist = utag.tags; |
| 683 |
if (taglist === undefined) { |
| 684 |
if (!("otags" in utag)) { |
| 685 |
return loclbase; |
| 686 |
} |
| 687 |
taglist = []; |
| 688 |
} |
| 689 |
loclbase = mergeTags(loclbase, taglist); |
| 690 |
if ("otags" in utag) { |
| 691 |
let otag_result = resolveOtag(utag, loclbase, current_tags, |
| 692 |
current_pass, "otags"); |
| 693 |
return otag_result[0]; |
| 694 |
} |
| 695 |
return loclbase; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Place a Junicode (not MUFI) entity in the text in the place of the PUA |
| 700 |
* character. Junicode uses this for combining marks that |
| 701 |
* have only PUA code points. |
| 702 |
* |
| 703 |
* @param {object} entity - The block of json data we are using for the transformation. |
| 704 |
* @param {string} base - The letter(s) we are transforming. |
| 705 |
* @param {array} current_tags - array of OpenType features currently active. |
| 706 |
* @param {number} current_pass - 0 for first pass, 1 for second. |
| 707 |
* @return {string} - the converted base. |
| 708 |
*/ |
| 709 |
function resolveEntity(entity, base, current_tags, current_pass=1) { |
| 710 |
let loclbase = ("base" in entity) ? entity.base : base; |
| 711 |
if ("tags" in entity) { |
| 712 |
let otag_result = resolveOtag(entity, loclbase, current_tags, |
| 713 |
current_pass); |
| 714 |
loclbase = otag_result[0]; |
| 715 |
} |
| 716 |
return loclbase; |
| 717 |
} |
| 718 |
|
| 719 |
/* |
| 720 |
* A helper for convert(). This function locates and returns the |
| 721 |
* json block needed for the next method. |
| 722 |
* |
| 723 |
* @param {*} char_block {object} - The character block to search for |
| 724 |
* a suitable conversion method for the current character. |
| 725 |
* @return {array} - 0: a string with the selected type; 1: section |
| 726 |
* of the character block needed for pending operation. |
| 727 |
*/ |
| 728 |
function getMethodBlock(char_block, h) { |
| 729 |
let preferred_block = null; |
| 730 |
let selected_type = null; |
| 731 |
for (const idx in _prefList) { |
| 732 |
let tag = _prefList[idx]; |
| 733 |
if (tag in char_block) { |
| 734 |
preferred_block = char_block[tag]; |
| 735 |
selected_type = tag; |
| 736 |
break; |
| 737 |
} |
| 738 |
} |
| 739 |
return [selected_type, preferred_block]; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Run to replace MUFI entities with PUA code points. Uses |
| 744 |
* a regex method that's said to be efficient (important, |
| 745 |
* since we're doing 850+ searches). |
| 746 |
* |
| 747 |
* @param {string} text_buffer - The text in which to perform a |
| 748 |
* search-and-replace operation substituting characters for MUFI |
| 749 |
* entitys. |
| 750 |
* @return {string} the text_buffer after the search-and-replace |
| 751 |
*/ |
| 752 |
function replaceEntities(text_buffer) { |
| 753 |
return text_buffer.replace(ENTITY_MAP, matched => entityDict[matched]); |
| 754 |
} |
| 755 |
|
| 756 |
/* |
| 757 |
* Lets us define different method blocks for different languages. |
| 758 |
* Not currently used, but ready! |
| 759 |
* |
| 760 |
* @param {object} codepoint_entry - block in which to search for the |
| 761 |
* currently active language. |
| 762 |
* @return {object} The block for the selected language, or null if the function has failed. |
| 763 |
*/ |
| 764 |
function getLangBlock(codepoint_entry) { |
| 765 |
lang = _language; |
| 766 |
return lang in codepoint_entry.lang ? codepoint_entry.lang[lang] : codepoint_entry.lang["other"]; |
| 767 |
} |
| 768 |
|
| 769 |
/* |
| 770 |
* Allows you to select alternate bases for certain categories of character: number, |
| 771 |
* insular, alpha, currency, punctuation, mark, smallcap. |
| 772 |
* |
| 773 |
* @param {object} codepoint_entry - The json block for the current character |
| 774 |
* @return {object} - The block for the selected base, or null if the function has failed. |
| 775 |
*/ |
| 776 |
function getVarBlock(codepoint_entry) { |
| 777 |
for (const v in _basePreferences) { |
| 778 |
const vv = _basePreferences[v]; |
| 779 |
if (vv in codepoint_entry.var) { |
| 780 |
return codepoint_entry.var[vv]; |
| 781 |
} |
| 782 |
} |
| 783 |
if ("other" in codepoint_entry.var) { |
| 784 |
return codepoint_entry.var["other"]; |
| 785 |
} |
| 786 |
return null; |
| 787 |
} |
| 788 |
|
| 789 |
/** |
| 790 |
* Runs the program. Splits the text_buffer into an array of |
| 791 |
* words and processes each word in two passes. Then |
| 792 |
* reassembles text from array (space-separated). |
| 793 |
* |
| 794 |
* @param {string} text_buffer - The text to be converted. This can contain multiple |
| 795 |
* and nested HTML elements. |
| 796 |
* @param {boolean} repl_ent - Whether to replace MUFI entities before running the rest of the function. |
| 797 |
* @param {boolean} code_on - In the GUI flavor, whether the "Show code" box is checked, and the |
| 798 |
* underlying code is showing in the "Destination" box. In embedded and Node flavors you must |
| 799 |
* also set options.replaceMUFIEntities. |
| 800 |
* @return {string} The processed text. |
| 801 |
*/ |
| 802 |
function convert(text_buffer, repl_ent = true, code_on = false) { |
| 803 |
let processed_word_list = []; |
| 804 |
|
| 805 |
if (options.replaceMUFIEntities && repl_ent) { |
| 806 |
text_buffer = replaceEntities(text_buffer); |
| 807 |
} |
| 808 |
let word_list = text_buffer.split(" "); |
| 809 |
for (const idx in word_list) { |
| 810 |
let _word = word_list[idx]; |
| 811 |
if (_word.length == 0) { |
| 812 |
continue; |
| 813 |
} |
| 814 |
let current_tags = structuredClone(options.defaultTags); |
| 815 |
let tag_stack = [structuredClone(current_tags)]; |
| 816 |
let result_string = ""; |
| 817 |
let word_tag_count = 0; |
| 818 |
let word_string = ""; |
| 819 |
for (const this_pass in [0, 1]) { |
| 820 |
if (this_pass == 0) { |
| 821 |
word_string = _word; |
| 822 |
} else { |
| 823 |
word_string = result_string; |
| 824 |
result_string = ""; |
| 825 |
} |
| 826 |
// work through the word character by character. |
| 827 |
for (const _char of word_string) { |
| 828 |
const hexstr = int2hex(_char.codePointAt(0)); |
| 829 |
if (_charSkip.includes(hexstr)) { |
| 830 |
result_string = result_string + _char; |
| 831 |
continue; |
| 832 |
} |
| 833 |
if (options.keepUnicode && UNICODE_HEXES.includes(hexstr)) { |
| 834 |
result_string = result_string + _char; |
| 835 |
continue; |
| 836 |
} |
| 837 |
if (hexstr in PUA_DATA) { |
| 838 |
let codepoint_entry = PUA_DATA[hexstr]; |
| 839 |
if ("lang" in codepoint_entry) { |
| 840 |
codepoint_entry = getLangBlock(codepoint_entry); |
| 841 |
} |
| 842 |
if ("var" in codepoint_entry) { |
| 843 |
codepoint_entry = getVarBlock(codepoint_entry); |
| 844 |
} |
| 845 |
if (codepoint_entry == null) { |
| 846 |
continue; |
| 847 |
} |
| 848 |
let current_base = codepoint_entry.base; |
| 849 |
let method_info = getMethodBlock(codepoint_entry, hexstr); |
| 850 |
let method_type = method_info[0]; |
| 851 |
let method_block = method_info[1]; |
| 852 |
let tmpstr = current_base; |
| 853 |
if (method_type == "otag") { |
| 854 |
let tag_info = resolveOtag(method_block, current_base, current_tags, |
| 855 |
this_pass); |
| 856 |
tmpstr = tag_info[0]; |
| 857 |
let tag_count = tag_info[1]; |
| 858 |
if (this_pass == 0) { |
| 859 |
word_tag_count += tag_count; |
| 860 |
} |
| 861 |
if (tmpstr.length == 0) { |
| 862 |
tmpstr = _char; |
| 863 |
} |
| 864 |
} |
| 865 |
else if (method_type == "enla") { |
| 866 |
tmpstr = applyEnlargeAxis(method_block, current_base, current_tags); |
| 867 |
} |
| 868 |
else if (method_type == "zwj") { |
| 869 |
tmpstr = resolveZwj(method_block, current_base, current_tags, |
| 870 |
this_pass); |
| 871 |
} |
| 872 |
else if (method_type == "utag") { |
| 873 |
tmpstr = resolveUtag(method_block, current_base, current_tags, |
| 874 |
this_pass); |
| 875 |
} |
| 876 |
else if (method_type == "entity") { |
| 877 |
let entity_base = current_base; |
| 878 |
if (hexstr in MARK_TAG_ENTITIES) { |
| 879 |
entity_base = MARK_TAG_ENTITIES[hexstr]; |
| 880 |
} |
| 881 |
tmpstr = resolveEntity(method_block, entity_base, current_tags, |
| 882 |
this_pass); |
| 883 |
} |
| 884 |
try { |
| 885 |
result_string = result_string + tmpstr |
| 886 |
} catch (error) { |
| 887 |
console.error("passing after TypeError with " + hexstr); |
| 888 |
} |
| 889 |
} else { |
| 890 |
result_string = result_string + _char; |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
let whole_word_str = result_string; |
| 896 |
if (word_tag_count > 0) { |
| 897 |
let word_tag_content = featureString(current_tags) + ";"; |
| 898 |
let tag_open = "<span style=\"font-feature-settings: "; |
| 899 |
let tag_close = "\">"; |
| 900 |
whole_word_str = tag_open + word_tag_content + tag_close + result_string + "</span>"; |
| 901 |
} |
| 902 |
processed_word_list.push(whole_word_str); |
| 903 |
current_tags = tag_stack.pop(); |
| 904 |
} |
| 905 |
|
| 906 |
let processed_text = processed_word_list.join(" "); |
| 907 |
if (code_on) { |
| 908 |
processed_text = processed_text.replaceAll("<", "<"); |
| 909 |
processed_text = processed_text.replaceAll(">", ">"); |
| 910 |
} |
| 911 |
|
| 912 |
return processed_text; |
| 913 |
} |
| 914 |
|
| 915 |
/* To use this file as a Node module, uncomment the following lines. */ |
| 916 |
|
| 917 |
// module.exports = { |
| 918 |
// options: options, |
| 919 |
// cleanup_string: cleanup_string, |
| 920 |
// convert: convert |
| 921 |
// }; |