bidi.mjs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. function bidiFactory() {
  2. var bidi = (function (exports) {
  3. // Bidi character types data, auto generated
  4. var DATA = {
  5. "R": "13k,1a,2,3,3,2+1j,ch+16,a+1,5+2,2+n,5,a,4,6+16,4+3,h+1b,4mo,179q,2+9,2+11,2i9+7y,2+68,4,3+4,5+13,4+3,2+4k,3+29,8+cf,1t+7z,w+17,3+3m,1t+3z,16o1+5r,8+30,8+mc,29+1r,29+4v,75+73",
  6. "EN": "1c+9,3d+1,6,187+9,513,4+5,7+9,sf+j,175h+9,qw+q,161f+1d,4xt+a,25i+9",
  7. "ES": "17,2,6dp+1,f+1,av,16vr,mx+1,4o,2",
  8. "ET": "z+2,3h+3,b+1,ym,3e+1,2o,p4+1,8,6u,7c,g6,1wc,1n9+4,30+1b,2n,6d,qhx+1,h0m,a+1,49+2,63+1,4+1,6bb+3,12jj",
  9. "AN": "16o+5,2j+9,2+1,35,ed,1ff2+9,87+u",
  10. "CS": "18,2+1,b,2u,12k,55v,l,17v0,2,3,53,2+1,b",
  11. "B": "a,3,f+2,2v,690",
  12. "S": "9,2,k",
  13. "WS": "c,k,4f4,1vk+a,u,1j,335",
  14. "ON": "x+1,4+4,h+5,r+5,r+3,z,5+3,2+1,2+1,5,2+2,3+4,o,w,ci+1,8+d,3+d,6+8,2+g,39+1,9,6+1,2,33,b8,3+1,3c+1,7+1,5r,b,7h+3,sa+5,2,3i+6,jg+3,ur+9,2v,ij+1,9g+9,7+a,8m,4+1,49+x,14u,2+2,c+2,e+2,e+2,e+1,i+n,e+e,2+p,u+2,e+2,36+1,2+3,2+1,b,2+2,6+5,2,2,2,h+1,5+4,6+3,3+f,16+2,5+3l,3+81,1y+p,2+40,q+a,m+13,2r+ch,2+9e,75+hf,3+v,2+2w,6e+5,f+6,75+2a,1a+p,2+2g,d+5x,r+b,6+3,4+o,g,6+1,6+2,2k+1,4,2j,5h+z,1m+1,1e+f,t+2,1f+e,d+3,4o+3,2s+1,w,535+1r,h3l+1i,93+2,2s,b+1,3l+x,2v,4g+3,21+3,kz+1,g5v+1,5a,j+9,n+v,2,3,2+8,2+1,3+2,2,3,46+1,4+4,h+5,r+5,r+a,3h+2,4+6,b+4,78,1r+24,4+c,4,1hb,ey+6,103+j,16j+c,1ux+7,5+g,fsh,jdq+1t,4,57+2e,p1,1m,1m,1m,1m,4kt+1,7j+17,5+2r,d+e,3+e,2+e,2+10,m+4,w,1n+5,1q,4z+5,4b+rb,9+c,4+c,4+37,d+2g,8+b,l+b,5+1j,9+9,7+13,9+t,3+1,27+3c,2+29,2+3q,d+d,3+4,4+2,6+6,a+o,8+6,a+2,e+6,16+42,2+1i",
  15. "BN": "0+8,6+d,2s+5,2+p,e,4m9,1kt+2,2b+5,5+5,17q9+v,7k,6p+8,6+1,119d+3,440+7,96s+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+1,1ekf+75,6p+2rz,1ben+1,1ekf+1,1ekf+1",
  16. "NSM": "lc+33,7o+6,7c+18,2,2+1,2+1,2,21+a,1d+k,h,2u+6,3+5,3+1,2+3,10,v+q,2k+a,1n+8,a,p+3,2+8,2+2,2+4,18+2,3c+e,2+v,1k,2,5+7,5,4+6,b+1,u,1n,5+3,9,l+1,r,3+1,1m,5+1,5+1,3+2,4,v+1,4,c+1,1m,5+4,2+1,5,l+1,n+5,2,1n,3,2+3,9,8+1,c+1,v,1q,d,1f,4,1m+2,6+2,2+3,8+1,c+1,u,1n,g+1,l+1,t+1,1m+1,5+3,9,l+1,u,21,8+2,2,2j,3+6,d+7,2r,3+8,c+5,23+1,s,2,2,1k+d,2+4,2+1,6+a,2+z,a,2v+3,2+5,2+1,3+1,q+1,5+2,h+3,e,3+1,7,g,jk+2,qb+2,u+2,u+1,v+1,1t+1,2+6,9,3+a,a,1a+2,3c+1,z,3b+2,5+1,a,7+2,64+1,3,1n,2+6,2,2,3+7,7+9,3,1d+g,1s+3,1d,2+4,2,6,15+8,d+1,x+3,3+1,2+2,1l,2+1,4,2+2,1n+7,3+1,49+2,2+c,2+6,5,7,4+1,5j+1l,2+4,k1+w,2db+2,3y,2p+v,ff+3,30+1,n9x+3,2+9,x+1,29+1,7l,4,5,q+1,6,48+1,r+h,e,13+7,q+a,1b+2,1d,3+3,3+1,14,1w+5,3+1,3+1,d,9,1c,1g,2+2,3+1,6+1,2,17+1,9,6n,3,5,fn5,ki+f,h+f,r2,6b,46+4,1af+2,2+1,6+3,15+2,5,4m+1,fy+3,as+1,4a+a,4x,1j+e,1l+2,1e+3,3+1,1y+2,11+4,2+7,1r,d+1,1h+8,b+3,3,2o+2,3,2+1,7,4h,4+7,m+1,1m+1,4,12+6,4+4,5g+7,3+2,2,o,2d+5,2,5+1,2+1,6n+3,7+1,2+1,s+1,2e+7,3,2+1,2z,2,3+5,2,2u+2,3+3,2+4,78+8,2+1,75+1,2,5,41+3,3+1,5,x+5,3+1,15+5,3+3,9,a+5,3+2,1b+c,2+1,bb+6,2+5,2d+l,3+6,2+1,2+1,3f+5,4,2+1,2+6,2,21+1,4,2,9o+1,f0c+4,1o+6,t5,1s+3,2a,f5l+1,43t+2,i+7,3+6,v+3,45+2,1j0+1i,5+1d,9,f,n+4,2+e,11t+6,2+g,3+6,2+1,2+4,7a+6,c6+3,15t+6,32+6,gzhy+6n",
  17. "AL": "16w,3,2,e+1b,z+2,2+2s,g+1,8+1,b+m,2+t,s+2i,c+e,4h+f,1d+1e,1bwe+dp,3+3z,x+c,2+1,35+3y,2rm+z,5+7,b+5,dt+l,c+u,17nl+27,1t+27,4x+6n,3+d",
  18. "LRO": "6ct",
  19. "RLO": "6cu",
  20. "LRE": "6cq",
  21. "RLE": "6cr",
  22. "PDF": "6cs",
  23. "LRI": "6ee",
  24. "RLI": "6ef",
  25. "FSI": "6eg",
  26. "PDI": "6eh"
  27. };
  28. var TYPES = {};
  29. var TYPES_TO_NAMES = {};
  30. TYPES.L = 1; //L is the default
  31. TYPES_TO_NAMES[1] = 'L';
  32. Object.keys(DATA).forEach(function (type, i) {
  33. TYPES[type] = 1 << (i + 1);
  34. TYPES_TO_NAMES[TYPES[type]] = type;
  35. });
  36. Object.freeze(TYPES);
  37. var ISOLATE_INIT_TYPES = TYPES.LRI | TYPES.RLI | TYPES.FSI;
  38. var STRONG_TYPES = TYPES.L | TYPES.R | TYPES.AL;
  39. var NEUTRAL_ISOLATE_TYPES = TYPES.B | TYPES.S | TYPES.WS | TYPES.ON | TYPES.FSI | TYPES.LRI | TYPES.RLI | TYPES.PDI;
  40. var BN_LIKE_TYPES = TYPES.BN | TYPES.RLE | TYPES.LRE | TYPES.RLO | TYPES.LRO | TYPES.PDF;
  41. var TRAILING_TYPES = TYPES.S | TYPES.WS | TYPES.B | ISOLATE_INIT_TYPES | TYPES.PDI | BN_LIKE_TYPES;
  42. var map = null;
  43. function parseData () {
  44. if (!map) {
  45. //const start = performance.now()
  46. map = new Map();
  47. var loop = function ( type ) {
  48. if (DATA.hasOwnProperty(type)) {
  49. var lastCode = 0;
  50. DATA[type].split(',').forEach(function (range) {
  51. var ref = range.split('+');
  52. var skip = ref[0];
  53. var step = ref[1];
  54. skip = parseInt(skip, 36);
  55. step = step ? parseInt(step, 36) : 0;
  56. map.set(lastCode += skip, TYPES[type]);
  57. for (var i = 0; i < step; i++) {
  58. map.set(++lastCode, TYPES[type]);
  59. }
  60. });
  61. }
  62. };
  63. for (var type in DATA) loop( type );
  64. //console.log(`char types parsed in ${performance.now() - start}ms`)
  65. }
  66. }
  67. /**
  68. * @param {string} char
  69. * @return {number}
  70. */
  71. function getBidiCharType (char) {
  72. parseData();
  73. return map.get(char.codePointAt(0)) || TYPES.L
  74. }
  75. function getBidiCharTypeName(char) {
  76. return TYPES_TO_NAMES[getBidiCharType(char)]
  77. }
  78. // Bidi bracket pairs data, auto generated
  79. var data$1 = {
  80. "pairs": "14>1,1e>2,u>2,2wt>1,1>1,1ge>1,1wp>1,1j>1,f>1,hm>1,1>1,u>1,u6>1,1>1,+5,28>1,w>1,1>1,+3,b8>1,1>1,+3,1>3,-1>-1,3>1,1>1,+2,1s>1,1>1,x>1,th>1,1>1,+2,db>1,1>1,+3,3>1,1>1,+2,14qm>1,1>1,+1,4q>1,1e>2,u>2,2>1,+1",
  81. "canonical": "6f1>-6dx,6dy>-6dx,6ec>-6ed,6ee>-6ed,6ww>2jj,-2ji>2jj,14r4>-1e7l,1e7m>-1e7l,1e7m>-1e5c,1e5d>-1e5b,1e5c>-14qx,14qy>-14qx,14vn>-1ecg,1ech>-1ecg,1edu>-1ecg,1eci>-1ecg,1eda>-1ecg,1eci>-1ecg,1eci>-168q,168r>-168q,168s>-14ye,14yf>-14ye"
  82. };
  83. /**
  84. * Parses an string that holds encoded codepoint mappings, e.g. for bracket pairs or
  85. * mirroring characters, as encoded by scripts/generateBidiData.js. Returns an object
  86. * holding the `map`, and optionally a `reverseMap` if `includeReverse:true`.
  87. * @param {string} encodedString
  88. * @param {boolean} includeReverse - true if you want reverseMap in the output
  89. * @return {{map: Map<number, number>, reverseMap?: Map<number, number>}}
  90. */
  91. function parseCharacterMap (encodedString, includeReverse) {
  92. var radix = 36;
  93. var lastCode = 0;
  94. var map = new Map();
  95. var reverseMap = includeReverse && new Map();
  96. var prevPair;
  97. encodedString.split(',').forEach(function visit(entry) {
  98. if (entry.indexOf('+') !== -1) {
  99. for (var i = +entry; i--;) {
  100. visit(prevPair);
  101. }
  102. } else {
  103. prevPair = entry;
  104. var ref = entry.split('>');
  105. var a = ref[0];
  106. var b = ref[1];
  107. a = String.fromCodePoint(lastCode += parseInt(a, radix));
  108. b = String.fromCodePoint(lastCode += parseInt(b, radix));
  109. map.set(a, b);
  110. includeReverse && reverseMap.set(b, a);
  111. }
  112. });
  113. return { map: map, reverseMap: reverseMap }
  114. }
  115. var openToClose, closeToOpen, canonical;
  116. function parse$1 () {
  117. if (!openToClose) {
  118. //const start = performance.now()
  119. var ref = parseCharacterMap(data$1.pairs, true);
  120. var map = ref.map;
  121. var reverseMap = ref.reverseMap;
  122. openToClose = map;
  123. closeToOpen = reverseMap;
  124. canonical = parseCharacterMap(data$1.canonical, false).map;
  125. //console.log(`brackets parsed in ${performance.now() - start}ms`)
  126. }
  127. }
  128. function openingToClosingBracket (char) {
  129. parse$1();
  130. return openToClose.get(char) || null
  131. }
  132. function closingToOpeningBracket (char) {
  133. parse$1();
  134. return closeToOpen.get(char) || null
  135. }
  136. function getCanonicalBracket (char) {
  137. parse$1();
  138. return canonical.get(char) || null
  139. }
  140. // Local type aliases
  141. var TYPE_L = TYPES.L;
  142. var TYPE_R = TYPES.R;
  143. var TYPE_EN = TYPES.EN;
  144. var TYPE_ES = TYPES.ES;
  145. var TYPE_ET = TYPES.ET;
  146. var TYPE_AN = TYPES.AN;
  147. var TYPE_CS = TYPES.CS;
  148. var TYPE_B = TYPES.B;
  149. var TYPE_S = TYPES.S;
  150. var TYPE_ON = TYPES.ON;
  151. var TYPE_BN = TYPES.BN;
  152. var TYPE_NSM = TYPES.NSM;
  153. var TYPE_AL = TYPES.AL;
  154. var TYPE_LRO = TYPES.LRO;
  155. var TYPE_RLO = TYPES.RLO;
  156. var TYPE_LRE = TYPES.LRE;
  157. var TYPE_RLE = TYPES.RLE;
  158. var TYPE_PDF = TYPES.PDF;
  159. var TYPE_LRI = TYPES.LRI;
  160. var TYPE_RLI = TYPES.RLI;
  161. var TYPE_FSI = TYPES.FSI;
  162. var TYPE_PDI = TYPES.PDI;
  163. /**
  164. * @typedef {object} GetEmbeddingLevelsResult
  165. * @property {{start, end, level}[]} paragraphs
  166. * @property {Uint8Array} levels
  167. */
  168. /**
  169. * This function applies the Bidirectional Algorithm to a string, returning the resolved embedding levels
  170. * in a single Uint8Array plus a list of objects holding each paragraph's start and end indices and resolved
  171. * base embedding level.
  172. *
  173. * @param {string} string - The input string
  174. * @param {"ltr"|"rtl"|"auto"} [baseDirection] - Use "ltr" or "rtl" to force a base paragraph direction,
  175. * otherwise a direction will be chosen automatically from each paragraph's contents.
  176. * @return {GetEmbeddingLevelsResult}
  177. */
  178. function getEmbeddingLevels (string, baseDirection) {
  179. var MAX_DEPTH = 125;
  180. // Start by mapping all characters to their unicode type, as a bitmask integer
  181. var charTypes = new Uint32Array(string.length);
  182. for (var i = 0; i < string.length; i++) {
  183. charTypes[i] = getBidiCharType(string[i]);
  184. }
  185. var charTypeCounts = new Map(); //will be cleared at start of each paragraph
  186. function changeCharType(i, type) {
  187. var oldType = charTypes[i];
  188. charTypes[i] = type;
  189. charTypeCounts.set(oldType, charTypeCounts.get(oldType) - 1);
  190. if (oldType & NEUTRAL_ISOLATE_TYPES) {
  191. charTypeCounts.set(NEUTRAL_ISOLATE_TYPES, charTypeCounts.get(NEUTRAL_ISOLATE_TYPES) - 1);
  192. }
  193. charTypeCounts.set(type, (charTypeCounts.get(type) || 0) + 1);
  194. if (type & NEUTRAL_ISOLATE_TYPES) {
  195. charTypeCounts.set(NEUTRAL_ISOLATE_TYPES, (charTypeCounts.get(NEUTRAL_ISOLATE_TYPES) || 0) + 1);
  196. }
  197. }
  198. var embedLevels = new Uint8Array(string.length);
  199. var isolationPairs = new Map(); //init->pdi and pdi->init
  200. // === 3.3.1 The Paragraph Level ===
  201. // 3.3.1 P1: Split the text into paragraphs
  202. var paragraphs = []; // [{start, end, level}, ...]
  203. var paragraph = null;
  204. for (var i$1 = 0; i$1 < string.length; i$1++) {
  205. if (!paragraph) {
  206. paragraphs.push(paragraph = {
  207. start: i$1,
  208. end: string.length - 1,
  209. // 3.3.1 P2-P3: Determine the paragraph level
  210. level: baseDirection === 'rtl' ? 1 : baseDirection === 'ltr' ? 0 : determineAutoEmbedLevel(i$1, false)
  211. });
  212. }
  213. if (charTypes[i$1] & TYPE_B) {
  214. paragraph.end = i$1;
  215. paragraph = null;
  216. }
  217. }
  218. var FORMATTING_TYPES = TYPE_RLE | TYPE_LRE | TYPE_RLO | TYPE_LRO | ISOLATE_INIT_TYPES | TYPE_PDI | TYPE_PDF | TYPE_B;
  219. var nextEven = function (n) { return n + ((n & 1) ? 1 : 2); };
  220. var nextOdd = function (n) { return n + ((n & 1) ? 2 : 1); };
  221. // Everything from here on will operate per paragraph.
  222. for (var paraIdx = 0; paraIdx < paragraphs.length; paraIdx++) {
  223. paragraph = paragraphs[paraIdx];
  224. var statusStack = [{
  225. _level: paragraph.level,
  226. _override: 0, //0=neutral, 1=L, 2=R
  227. _isolate: 0 //bool
  228. }];
  229. var stackTop = (void 0);
  230. var overflowIsolateCount = 0;
  231. var overflowEmbeddingCount = 0;
  232. var validIsolateCount = 0;
  233. charTypeCounts.clear();
  234. // === 3.3.2 Explicit Levels and Directions ===
  235. for (var i$2 = paragraph.start; i$2 <= paragraph.end; i$2++) {
  236. var charType = charTypes[i$2];
  237. stackTop = statusStack[statusStack.length - 1];
  238. // Set initial counts
  239. charTypeCounts.set(charType, (charTypeCounts.get(charType) || 0) + 1);
  240. if (charType & NEUTRAL_ISOLATE_TYPES) {
  241. charTypeCounts.set(NEUTRAL_ISOLATE_TYPES, (charTypeCounts.get(NEUTRAL_ISOLATE_TYPES) || 0) + 1);
  242. }
  243. // Explicit Embeddings: 3.3.2 X2 - X3
  244. if (charType & FORMATTING_TYPES) { //prefilter all formatters
  245. if (charType & (TYPE_RLE | TYPE_LRE)) {
  246. embedLevels[i$2] = stackTop._level; // 5.2
  247. var level = (charType === TYPE_RLE ? nextOdd : nextEven)(stackTop._level);
  248. if (level <= MAX_DEPTH && !overflowIsolateCount && !overflowEmbeddingCount) {
  249. statusStack.push({
  250. _level: level,
  251. _override: 0,
  252. _isolate: 0
  253. });
  254. } else if (!overflowIsolateCount) {
  255. overflowEmbeddingCount++;
  256. }
  257. }
  258. // Explicit Overrides: 3.3.2 X4 - X5
  259. else if (charType & (TYPE_RLO | TYPE_LRO)) {
  260. embedLevels[i$2] = stackTop._level; // 5.2
  261. var level$1 = (charType === TYPE_RLO ? nextOdd : nextEven)(stackTop._level);
  262. if (level$1 <= MAX_DEPTH && !overflowIsolateCount && !overflowEmbeddingCount) {
  263. statusStack.push({
  264. _level: level$1,
  265. _override: (charType & TYPE_RLO) ? TYPE_R : TYPE_L,
  266. _isolate: 0
  267. });
  268. } else if (!overflowIsolateCount) {
  269. overflowEmbeddingCount++;
  270. }
  271. }
  272. // Isolates: 3.3.2 X5a - X5c
  273. else if (charType & ISOLATE_INIT_TYPES) {
  274. // X5c - FSI becomes either RLI or LRI
  275. if (charType & TYPE_FSI) {
  276. charType = determineAutoEmbedLevel(i$2 + 1, true) === 1 ? TYPE_RLI : TYPE_LRI;
  277. }
  278. embedLevels[i$2] = stackTop._level;
  279. if (stackTop._override) {
  280. changeCharType(i$2, stackTop._override);
  281. }
  282. var level$2 = (charType === TYPE_RLI ? nextOdd : nextEven)(stackTop._level);
  283. if (level$2 <= MAX_DEPTH && overflowIsolateCount === 0 && overflowEmbeddingCount === 0) {
  284. validIsolateCount++;
  285. statusStack.push({
  286. _level: level$2,
  287. _override: 0,
  288. _isolate: 1,
  289. _isolInitIndex: i$2
  290. });
  291. } else {
  292. overflowIsolateCount++;
  293. }
  294. }
  295. // Terminating Isolates: 3.3.2 X6a
  296. else if (charType & TYPE_PDI) {
  297. if (overflowIsolateCount > 0) {
  298. overflowIsolateCount--;
  299. } else if (validIsolateCount > 0) {
  300. overflowEmbeddingCount = 0;
  301. while (!statusStack[statusStack.length - 1]._isolate) {
  302. statusStack.pop();
  303. }
  304. // Add to isolation pairs bidirectional mapping:
  305. var isolInitIndex = statusStack[statusStack.length - 1]._isolInitIndex;
  306. if (isolInitIndex != null) {
  307. isolationPairs.set(isolInitIndex, i$2);
  308. isolationPairs.set(i$2, isolInitIndex);
  309. }
  310. statusStack.pop();
  311. validIsolateCount--;
  312. }
  313. stackTop = statusStack[statusStack.length - 1];
  314. embedLevels[i$2] = stackTop._level;
  315. if (stackTop._override) {
  316. changeCharType(i$2, stackTop._override);
  317. }
  318. }
  319. // Terminating Embeddings and Overrides: 3.3.2 X7
  320. else if (charType & TYPE_PDF) {
  321. if (overflowIsolateCount === 0) {
  322. if (overflowEmbeddingCount > 0) {
  323. overflowEmbeddingCount--;
  324. } else if (!stackTop._isolate && statusStack.length > 1) {
  325. statusStack.pop();
  326. stackTop = statusStack[statusStack.length - 1];
  327. }
  328. }
  329. embedLevels[i$2] = stackTop._level; // 5.2
  330. }
  331. // End of Paragraph: 3.3.2 X8
  332. else if (charType & TYPE_B) {
  333. embedLevels[i$2] = paragraph.level;
  334. }
  335. }
  336. // Non-formatting characters: 3.3.2 X6
  337. else {
  338. embedLevels[i$2] = stackTop._level;
  339. // NOTE: This exclusion of BN seems to go against what section 5.2 says, but is required for test passage
  340. if (stackTop._override && charType !== TYPE_BN) {
  341. changeCharType(i$2, stackTop._override);
  342. }
  343. }
  344. }
  345. // === 3.3.3 Preparations for Implicit Processing ===
  346. // Remove all RLE, LRE, RLO, LRO, PDF, and BN characters: 3.3.3 X9
  347. // Note: Due to section 5.2, we won't remove them, but we'll use the BN_LIKE_TYPES bitset to
  348. // easily ignore them all from here on out.
  349. // 3.3.3 X10
  350. // Compute the set of isolating run sequences as specified by BD13
  351. var levelRuns = [];
  352. var currentRun = null;
  353. for (var i$3 = paragraph.start; i$3 <= paragraph.end; i$3++) {
  354. var charType$1 = charTypes[i$3];
  355. if (!(charType$1 & BN_LIKE_TYPES)) {
  356. var lvl = embedLevels[i$3];
  357. var isIsolInit = charType$1 & ISOLATE_INIT_TYPES;
  358. var isPDI = charType$1 === TYPE_PDI;
  359. if (currentRun && lvl === currentRun._level) {
  360. currentRun._end = i$3;
  361. currentRun._endsWithIsolInit = isIsolInit;
  362. } else {
  363. levelRuns.push(currentRun = {
  364. _start: i$3,
  365. _end: i$3,
  366. _level: lvl,
  367. _startsWithPDI: isPDI,
  368. _endsWithIsolInit: isIsolInit
  369. });
  370. }
  371. }
  372. }
  373. var isolatingRunSeqs = []; // [{seqIndices: [], sosType: L|R, eosType: L|R}]
  374. for (var runIdx = 0; runIdx < levelRuns.length; runIdx++) {
  375. var run = levelRuns[runIdx];
  376. if (!run._startsWithPDI || (run._startsWithPDI && !isolationPairs.has(run._start))) {
  377. var seqRuns = [currentRun = run];
  378. for (var pdiIndex = (void 0); currentRun && currentRun._endsWithIsolInit && (pdiIndex = isolationPairs.get(currentRun._end)) != null;) {
  379. for (var i$4 = runIdx + 1; i$4 < levelRuns.length; i$4++) {
  380. if (levelRuns[i$4]._start === pdiIndex) {
  381. seqRuns.push(currentRun = levelRuns[i$4]);
  382. break
  383. }
  384. }
  385. }
  386. // build flat list of indices across all runs:
  387. var seqIndices = [];
  388. for (var i$5 = 0; i$5 < seqRuns.length; i$5++) {
  389. var run$1 = seqRuns[i$5];
  390. for (var j = run$1._start; j <= run$1._end; j++) {
  391. seqIndices.push(j);
  392. }
  393. }
  394. // determine the sos/eos types:
  395. var firstLevel = embedLevels[seqIndices[0]];
  396. var prevLevel = paragraph.level;
  397. for (var i$6 = seqIndices[0] - 1; i$6 >= 0; i$6--) {
  398. if (!(charTypes[i$6] & BN_LIKE_TYPES)) { //5.2
  399. prevLevel = embedLevels[i$6];
  400. break
  401. }
  402. }
  403. var lastIndex = seqIndices[seqIndices.length - 1];
  404. var lastLevel = embedLevels[lastIndex];
  405. var nextLevel = paragraph.level;
  406. if (!(charTypes[lastIndex] & ISOLATE_INIT_TYPES)) {
  407. for (var i$7 = lastIndex + 1; i$7 <= paragraph.end; i$7++) {
  408. if (!(charTypes[i$7] & BN_LIKE_TYPES)) { //5.2
  409. nextLevel = embedLevels[i$7];
  410. break
  411. }
  412. }
  413. }
  414. isolatingRunSeqs.push({
  415. _seqIndices: seqIndices,
  416. _sosType: Math.max(prevLevel, firstLevel) % 2 ? TYPE_R : TYPE_L,
  417. _eosType: Math.max(nextLevel, lastLevel) % 2 ? TYPE_R : TYPE_L
  418. });
  419. }
  420. }
  421. // The next steps are done per isolating run sequence
  422. for (var seqIdx = 0; seqIdx < isolatingRunSeqs.length; seqIdx++) {
  423. var ref = isolatingRunSeqs[seqIdx];
  424. var seqIndices$1 = ref._seqIndices;
  425. var sosType = ref._sosType;
  426. var eosType = ref._eosType;
  427. /**
  428. * All the level runs in an isolating run sequence have the same embedding level.
  429. *
  430. * DO NOT change any `embedLevels[i]` within the current scope.
  431. */
  432. var embedDirection = ((embedLevels[seqIndices$1[0]]) & 1) ? TYPE_R : TYPE_L;
  433. // === 3.3.4 Resolving Weak Types ===
  434. // W1 + 5.2. Search backward from each NSM to the first character in the isolating run sequence whose
  435. // bidirectional type is not BN, and set the NSM to ON if it is an isolate initiator or PDI, and to its
  436. // type otherwise. If the NSM is the first non-BN character, change the NSM to the type of sos.
  437. if (charTypeCounts.get(TYPE_NSM)) {
  438. for (var si = 0; si < seqIndices$1.length; si++) {
  439. var i$8 = seqIndices$1[si];
  440. if (charTypes[i$8] & TYPE_NSM) {
  441. var prevType = sosType;
  442. for (var sj = si - 1; sj >= 0; sj--) {
  443. if (!(charTypes[seqIndices$1[sj]] & BN_LIKE_TYPES)) { //5.2 scan back to first non-BN
  444. prevType = charTypes[seqIndices$1[sj]];
  445. break
  446. }
  447. }
  448. changeCharType(i$8, (prevType & (ISOLATE_INIT_TYPES | TYPE_PDI)) ? TYPE_ON : prevType);
  449. }
  450. }
  451. }
  452. // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sos)
  453. // is found. If an AL is found, change the type of the European number to Arabic number.
  454. if (charTypeCounts.get(TYPE_EN)) {
  455. for (var si$1 = 0; si$1 < seqIndices$1.length; si$1++) {
  456. var i$9 = seqIndices$1[si$1];
  457. if (charTypes[i$9] & TYPE_EN) {
  458. for (var sj$1 = si$1 - 1; sj$1 >= -1; sj$1--) {
  459. var prevCharType = sj$1 === -1 ? sosType : charTypes[seqIndices$1[sj$1]];
  460. if (prevCharType & STRONG_TYPES) {
  461. if (prevCharType === TYPE_AL) {
  462. changeCharType(i$9, TYPE_AN);
  463. }
  464. break
  465. }
  466. }
  467. }
  468. }
  469. }
  470. // W3. Change all ALs to R
  471. if (charTypeCounts.get(TYPE_AL)) {
  472. for (var si$2 = 0; si$2 < seqIndices$1.length; si$2++) {
  473. var i$10 = seqIndices$1[si$2];
  474. if (charTypes[i$10] & TYPE_AL) {
  475. changeCharType(i$10, TYPE_R);
  476. }
  477. }
  478. }
  479. // W4. A single European separator between two European numbers changes to a European number. A single common
  480. // separator between two numbers of the same type changes to that type.
  481. if (charTypeCounts.get(TYPE_ES) || charTypeCounts.get(TYPE_CS)) {
  482. for (var si$3 = 1; si$3 < seqIndices$1.length - 1; si$3++) {
  483. var i$11 = seqIndices$1[si$3];
  484. if (charTypes[i$11] & (TYPE_ES | TYPE_CS)) {
  485. var prevType$1 = 0, nextType = 0;
  486. for (var sj$2 = si$3 - 1; sj$2 >= 0; sj$2--) {
  487. prevType$1 = charTypes[seqIndices$1[sj$2]];
  488. if (!(prevType$1 & BN_LIKE_TYPES)) { //5.2
  489. break
  490. }
  491. }
  492. for (var sj$3 = si$3 + 1; sj$3 < seqIndices$1.length; sj$3++) {
  493. nextType = charTypes[seqIndices$1[sj$3]];
  494. if (!(nextType & BN_LIKE_TYPES)) { //5.2
  495. break
  496. }
  497. }
  498. if (prevType$1 === nextType && (charTypes[i$11] === TYPE_ES ? prevType$1 === TYPE_EN : (prevType$1 & (TYPE_EN | TYPE_AN)))) {
  499. changeCharType(i$11, prevType$1);
  500. }
  501. }
  502. }
  503. }
  504. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  505. if (charTypeCounts.get(TYPE_EN)) {
  506. for (var si$4 = 0; si$4 < seqIndices$1.length; si$4++) {
  507. var i$12 = seqIndices$1[si$4];
  508. if (charTypes[i$12] & TYPE_EN) {
  509. for (var sj$4 = si$4 - 1; sj$4 >= 0 && (charTypes[seqIndices$1[sj$4]] & (TYPE_ET | BN_LIKE_TYPES)); sj$4--) {
  510. changeCharType(seqIndices$1[sj$4], TYPE_EN);
  511. }
  512. for (si$4++; si$4 < seqIndices$1.length && (charTypes[seqIndices$1[si$4]] & (TYPE_ET | BN_LIKE_TYPES | TYPE_EN)); si$4++) {
  513. if (charTypes[seqIndices$1[si$4]] !== TYPE_EN) {
  514. changeCharType(seqIndices$1[si$4], TYPE_EN);
  515. }
  516. }
  517. }
  518. }
  519. }
  520. // W6. Otherwise, separators and terminators change to Other Neutral.
  521. if (charTypeCounts.get(TYPE_ET) || charTypeCounts.get(TYPE_ES) || charTypeCounts.get(TYPE_CS)) {
  522. for (var si$5 = 0; si$5 < seqIndices$1.length; si$5++) {
  523. var i$13 = seqIndices$1[si$5];
  524. if (charTypes[i$13] & (TYPE_ET | TYPE_ES | TYPE_CS)) {
  525. changeCharType(i$13, TYPE_ON);
  526. // 5.2 transform adjacent BNs too:
  527. for (var sj$5 = si$5 - 1; sj$5 >= 0 && (charTypes[seqIndices$1[sj$5]] & BN_LIKE_TYPES); sj$5--) {
  528. changeCharType(seqIndices$1[sj$5], TYPE_ON);
  529. }
  530. for (var sj$6 = si$5 + 1; sj$6 < seqIndices$1.length && (charTypes[seqIndices$1[sj$6]] & BN_LIKE_TYPES); sj$6++) {
  531. changeCharType(seqIndices$1[sj$6], TYPE_ON);
  532. }
  533. }
  534. }
  535. }
  536. // W7. Search backward from each instance of a European number until the first strong type (R, L, or sos)
  537. // is found. If an L is found, then change the type of the European number to L.
  538. // NOTE: implemented in single forward pass for efficiency
  539. if (charTypeCounts.get(TYPE_EN)) {
  540. for (var si$6 = 0, prevStrongType = sosType; si$6 < seqIndices$1.length; si$6++) {
  541. var i$14 = seqIndices$1[si$6];
  542. var type = charTypes[i$14];
  543. if (type & TYPE_EN) {
  544. if (prevStrongType === TYPE_L) {
  545. changeCharType(i$14, TYPE_L);
  546. }
  547. } else if (type & STRONG_TYPES) {
  548. prevStrongType = type;
  549. }
  550. }
  551. }
  552. // === 3.3.5 Resolving Neutral and Isolate Formatting Types ===
  553. if (charTypeCounts.get(NEUTRAL_ISOLATE_TYPES)) {
  554. // N0. Process bracket pairs in an isolating run sequence sequentially in the logical order of the text
  555. // positions of the opening paired brackets using the logic given below. Within this scope, bidirectional
  556. // types EN and AN are treated as R.
  557. var R_TYPES_FOR_N_STEPS = (TYPE_R | TYPE_EN | TYPE_AN);
  558. var STRONG_TYPES_FOR_N_STEPS = R_TYPES_FOR_N_STEPS | TYPE_L;
  559. // * Identify the bracket pairs in the current isolating run sequence according to BD16.
  560. var bracketPairs = [];
  561. {
  562. var openerStack = [];
  563. for (var si$7 = 0; si$7 < seqIndices$1.length; si$7++) {
  564. // NOTE: for any potential bracket character we also test that it still carries a NI
  565. // type, as that may have been changed earlier. This doesn't seem to be explicitly
  566. // called out in the spec, but is required for passage of certain tests.
  567. if (charTypes[seqIndices$1[si$7]] & NEUTRAL_ISOLATE_TYPES) {
  568. var char = string[seqIndices$1[si$7]];
  569. var oppositeBracket = (void 0);
  570. // Opening bracket
  571. if (openingToClosingBracket(char) !== null) {
  572. if (openerStack.length < 63) {
  573. openerStack.push({ char: char, seqIndex: si$7 });
  574. } else {
  575. break
  576. }
  577. }
  578. // Closing bracket
  579. else if ((oppositeBracket = closingToOpeningBracket(char)) !== null) {
  580. for (var stackIdx = openerStack.length - 1; stackIdx >= 0; stackIdx--) {
  581. var stackChar = openerStack[stackIdx].char;
  582. if (stackChar === oppositeBracket ||
  583. stackChar === closingToOpeningBracket(getCanonicalBracket(char)) ||
  584. openingToClosingBracket(getCanonicalBracket(stackChar)) === char
  585. ) {
  586. bracketPairs.push([openerStack[stackIdx].seqIndex, si$7]);
  587. openerStack.length = stackIdx; //pop the matching bracket and all following
  588. break
  589. }
  590. }
  591. }
  592. }
  593. }
  594. bracketPairs.sort(function (a, b) { return a[0] - b[0]; });
  595. }
  596. // * For each bracket-pair element in the list of pairs of text positions
  597. for (var pairIdx = 0; pairIdx < bracketPairs.length; pairIdx++) {
  598. var ref$1 = bracketPairs[pairIdx];
  599. var openSeqIdx = ref$1[0];
  600. var closeSeqIdx = ref$1[1];
  601. // a. Inspect the bidirectional types of the characters enclosed within the bracket pair.
  602. // b. If any strong type (either L or R) matching the embedding direction is found, set the type for both
  603. // brackets in the pair to match the embedding direction.
  604. var foundStrongType = false;
  605. var useStrongType = 0;
  606. for (var si$8 = openSeqIdx + 1; si$8 < closeSeqIdx; si$8++) {
  607. var i$15 = seqIndices$1[si$8];
  608. if (charTypes[i$15] & STRONG_TYPES_FOR_N_STEPS) {
  609. foundStrongType = true;
  610. var lr = (charTypes[i$15] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L;
  611. if (lr === embedDirection) {
  612. useStrongType = lr;
  613. break
  614. }
  615. }
  616. }
  617. // c. Otherwise, if there is a strong type it must be opposite the embedding direction. Therefore, test
  618. // for an established context with a preceding strong type by checking backwards before the opening paired
  619. // bracket until the first strong type (L, R, or sos) is found.
  620. // 1. If the preceding strong type is also opposite the embedding direction, context is established, so
  621. // set the type for both brackets in the pair to that direction.
  622. // 2. Otherwise set the type for both brackets in the pair to the embedding direction.
  623. if (foundStrongType && !useStrongType) {
  624. useStrongType = sosType;
  625. for (var si$9 = openSeqIdx - 1; si$9 >= 0; si$9--) {
  626. var i$16 = seqIndices$1[si$9];
  627. if (charTypes[i$16] & STRONG_TYPES_FOR_N_STEPS) {
  628. var lr$1 = (charTypes[i$16] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L;
  629. if (lr$1 !== embedDirection) {
  630. useStrongType = lr$1;
  631. } else {
  632. useStrongType = embedDirection;
  633. }
  634. break
  635. }
  636. }
  637. }
  638. if (useStrongType) {
  639. charTypes[seqIndices$1[openSeqIdx]] = charTypes[seqIndices$1[closeSeqIdx]] = useStrongType;
  640. // * Any number of characters that had original bidirectional character type NSM prior to the application
  641. // of W1 that immediately follow a paired bracket which changed to L or R under N0 should change to match
  642. // the type of their preceding bracket.
  643. if (useStrongType !== embedDirection) {
  644. for (var si$10 = openSeqIdx + 1; si$10 < seqIndices$1.length; si$10++) {
  645. if (!(charTypes[seqIndices$1[si$10]] & BN_LIKE_TYPES)) {
  646. if (getBidiCharType(string[seqIndices$1[si$10]]) & TYPE_NSM) {
  647. charTypes[seqIndices$1[si$10]] = useStrongType;
  648. }
  649. break
  650. }
  651. }
  652. }
  653. if (useStrongType !== embedDirection) {
  654. for (var si$11 = closeSeqIdx + 1; si$11 < seqIndices$1.length; si$11++) {
  655. if (!(charTypes[seqIndices$1[si$11]] & BN_LIKE_TYPES)) {
  656. if (getBidiCharType(string[seqIndices$1[si$11]]) & TYPE_NSM) {
  657. charTypes[seqIndices$1[si$11]] = useStrongType;
  658. }
  659. break
  660. }
  661. }
  662. }
  663. }
  664. }
  665. // N1. A sequence of NIs takes the direction of the surrounding strong text if the text on both sides has the
  666. // same direction.
  667. // N2. Any remaining NIs take the embedding direction.
  668. for (var si$12 = 0; si$12 < seqIndices$1.length; si$12++) {
  669. if (charTypes[seqIndices$1[si$12]] & NEUTRAL_ISOLATE_TYPES) {
  670. var niRunStart = si$12, niRunEnd = si$12;
  671. var prevType$2 = sosType; //si === 0 ? sosType : (charTypes[seqIndices[si - 1]] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L
  672. for (var si2 = si$12 - 1; si2 >= 0; si2--) {
  673. if (charTypes[seqIndices$1[si2]] & BN_LIKE_TYPES) {
  674. niRunStart = si2; //5.2 treat BNs adjacent to NIs as NIs
  675. } else {
  676. prevType$2 = (charTypes[seqIndices$1[si2]] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L;
  677. break
  678. }
  679. }
  680. var nextType$1 = eosType;
  681. for (var si2$1 = si$12 + 1; si2$1 < seqIndices$1.length; si2$1++) {
  682. if (charTypes[seqIndices$1[si2$1]] & (NEUTRAL_ISOLATE_TYPES | BN_LIKE_TYPES)) {
  683. niRunEnd = si2$1;
  684. } else {
  685. nextType$1 = (charTypes[seqIndices$1[si2$1]] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L;
  686. break
  687. }
  688. }
  689. for (var sj$7 = niRunStart; sj$7 <= niRunEnd; sj$7++) {
  690. charTypes[seqIndices$1[sj$7]] = prevType$2 === nextType$1 ? prevType$2 : embedDirection;
  691. }
  692. si$12 = niRunEnd;
  693. }
  694. }
  695. }
  696. }
  697. // === 3.3.6 Resolving Implicit Levels ===
  698. for (var i$17 = paragraph.start; i$17 <= paragraph.end; i$17++) {
  699. var level$3 = embedLevels[i$17];
  700. var type$1 = charTypes[i$17];
  701. // I2. For all characters with an odd (right-to-left) embedding level, those of type L, EN or AN go up one level.
  702. if (level$3 & 1) {
  703. if (type$1 & (TYPE_L | TYPE_EN | TYPE_AN)) {
  704. embedLevels[i$17]++;
  705. }
  706. }
  707. // I1. For all characters with an even (left-to-right) embedding level, those of type R go up one level
  708. // and those of type AN or EN go up two levels.
  709. else {
  710. if (type$1 & TYPE_R) {
  711. embedLevels[i$17]++;
  712. } else if (type$1 & (TYPE_AN | TYPE_EN)) {
  713. embedLevels[i$17] += 2;
  714. }
  715. }
  716. // 5.2: Resolve any LRE, RLE, LRO, RLO, PDF, or BN to the level of the preceding character if there is one,
  717. // and otherwise to the base level.
  718. if (type$1 & BN_LIKE_TYPES) {
  719. embedLevels[i$17] = i$17 === 0 ? paragraph.level : embedLevels[i$17 - 1];
  720. }
  721. // 3.4 L1.1-4: Reset the embedding level of segment/paragraph separators, and any sequence of whitespace or
  722. // isolate formatting characters preceding them or the end of the paragraph, to the paragraph level.
  723. // NOTE: this will also need to be applied to each individual line ending after line wrapping occurs.
  724. if (i$17 === paragraph.end || getBidiCharType(string[i$17]) & (TYPE_S | TYPE_B)) {
  725. for (var j$1 = i$17; j$1 >= 0 && (getBidiCharType(string[j$1]) & TRAILING_TYPES); j$1--) {
  726. embedLevels[j$1] = paragraph.level;
  727. }
  728. }
  729. }
  730. }
  731. // DONE! The resolved levels can then be used, after line wrapping, to flip runs of characters
  732. // according to section 3.4 Reordering Resolved Levels
  733. return {
  734. levels: embedLevels,
  735. paragraphs: paragraphs
  736. }
  737. function determineAutoEmbedLevel (start, isFSI) {
  738. // 3.3.1 P2 - P3
  739. for (var i = start; i < string.length; i++) {
  740. var charType = charTypes[i];
  741. if (charType & (TYPE_R | TYPE_AL)) {
  742. return 1
  743. }
  744. if ((charType & (TYPE_B | TYPE_L)) || (isFSI && charType === TYPE_PDI)) {
  745. return 0
  746. }
  747. if (charType & ISOLATE_INIT_TYPES) {
  748. var pdi = indexOfMatchingPDI(i);
  749. i = pdi === -1 ? string.length : pdi;
  750. }
  751. }
  752. return 0
  753. }
  754. function indexOfMatchingPDI (isolateStart) {
  755. // 3.1.2 BD9
  756. var isolationLevel = 1;
  757. for (var i = isolateStart + 1; i < string.length; i++) {
  758. var charType = charTypes[i];
  759. if (charType & TYPE_B) {
  760. break
  761. }
  762. if (charType & TYPE_PDI) {
  763. if (--isolationLevel === 0) {
  764. return i
  765. }
  766. } else if (charType & ISOLATE_INIT_TYPES) {
  767. isolationLevel++;
  768. }
  769. }
  770. return -1
  771. }
  772. }
  773. // Bidi mirrored chars data, auto generated
  774. var data = "14>1,j>2,t>2,u>2,1a>g,2v3>1,1>1,1ge>1,1wd>1,b>1,1j>1,f>1,ai>3,-2>3,+1,8>1k0,-1jq>1y7,-1y6>1hf,-1he>1h6,-1h5>1ha,-1h8>1qi,-1pu>1,6>3u,-3s>7,6>1,1>1,f>1,1>1,+2,3>1,1>1,+13,4>1,1>1,6>1eo,-1ee>1,3>1mg,-1me>1mk,-1mj>1mi,-1mg>1mi,-1md>1,1>1,+2,1>10k,-103>1,1>1,4>1,5>1,1>1,+10,3>1,1>8,-7>8,+1,-6>7,+1,a>1,1>1,u>1,u6>1,1>1,+5,26>1,1>1,2>1,2>2,8>1,7>1,4>1,1>1,+5,b8>1,1>1,+3,1>3,-2>1,2>1,1>1,+2,c>1,3>1,1>1,+2,h>1,3>1,a>1,1>1,2>1,3>1,1>1,d>1,f>1,3>1,1a>1,1>1,6>1,7>1,13>1,k>1,1>1,+19,4>1,1>1,+2,2>1,1>1,+18,m>1,a>1,1>1,lk>1,1>1,4>1,2>1,f>1,3>1,1>1,+3,db>1,1>1,+3,3>1,1>1,+2,14qm>1,1>1,+1,6>1,4j>1,j>2,t>2,u>2,2>1,+1";
  775. var mirrorMap;
  776. function parse () {
  777. if (!mirrorMap) {
  778. //const start = performance.now()
  779. var ref = parseCharacterMap(data, true);
  780. var map = ref.map;
  781. var reverseMap = ref.reverseMap;
  782. // Combine both maps into one
  783. reverseMap.forEach(function (value, key) {
  784. map.set(key, value);
  785. });
  786. mirrorMap = map;
  787. //console.log(`mirrored chars parsed in ${performance.now() - start}ms`)
  788. }
  789. }
  790. function getMirroredCharacter (char) {
  791. parse();
  792. return mirrorMap.get(char) || null
  793. }
  794. /**
  795. * Given a string and its resolved embedding levels, build a map of indices to replacement chars
  796. * for any characters in right-to-left segments that have defined mirrored characters.
  797. * @param string
  798. * @param embeddingLevels
  799. * @param [start]
  800. * @param [end]
  801. * @return {Map<number, string>}
  802. */
  803. function getMirroredCharactersMap(string, embeddingLevels, start, end) {
  804. var strLen = string.length;
  805. start = Math.max(0, start == null ? 0 : +start);
  806. end = Math.min(strLen - 1, end == null ? strLen - 1 : +end);
  807. var map = new Map();
  808. for (var i = start; i <= end; i++) {
  809. if (embeddingLevels[i] & 1) { //only odd (rtl) levels
  810. var mirror = getMirroredCharacter(string[i]);
  811. if (mirror !== null) {
  812. map.set(i, mirror);
  813. }
  814. }
  815. }
  816. return map
  817. }
  818. /**
  819. * Given a start and end denoting a single line within a string, and a set of precalculated
  820. * bidi embedding levels, produce a list of segments whose ordering should be flipped, in sequence.
  821. * @param {string} string - the full input string
  822. * @param {GetEmbeddingLevelsResult} embeddingLevelsResult - the result object from getEmbeddingLevels
  823. * @param {number} [start] - first character in a subset of the full string
  824. * @param {number} [end] - last character in a subset of the full string
  825. * @return {number[][]} - the list of start/end segments that should be flipped, in order.
  826. */
  827. function getReorderSegments(string, embeddingLevelsResult, start, end) {
  828. var strLen = string.length;
  829. start = Math.max(0, start == null ? 0 : +start);
  830. end = Math.min(strLen - 1, end == null ? strLen - 1 : +end);
  831. var segments = [];
  832. embeddingLevelsResult.paragraphs.forEach(function (paragraph) {
  833. var lineStart = Math.max(start, paragraph.start);
  834. var lineEnd = Math.min(end, paragraph.end);
  835. if (lineStart < lineEnd) {
  836. // Local slice for mutation
  837. var lineLevels = embeddingLevelsResult.levels.slice(lineStart, lineEnd + 1);
  838. // 3.4 L1.4: Reset any sequence of whitespace characters and/or isolate formatting characters at the
  839. // end of the line to the paragraph level.
  840. for (var i = lineEnd; i >= lineStart && (getBidiCharType(string[i]) & TRAILING_TYPES); i--) {
  841. lineLevels[i] = paragraph.level;
  842. }
  843. // L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels
  844. // not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
  845. var maxLevel = paragraph.level;
  846. var minOddLevel = Infinity;
  847. for (var i$1 = 0; i$1 < lineLevels.length; i$1++) {
  848. var level = lineLevels[i$1];
  849. if (level > maxLevel) { maxLevel = level; }
  850. if (level < minOddLevel) { minOddLevel = level | 1; }
  851. }
  852. for (var lvl = maxLevel; lvl >= minOddLevel; lvl--) {
  853. for (var i$2 = 0; i$2 < lineLevels.length; i$2++) {
  854. if (lineLevels[i$2] >= lvl) {
  855. var segStart = i$2;
  856. while (i$2 + 1 < lineLevels.length && lineLevels[i$2 + 1] >= lvl) {
  857. i$2++;
  858. }
  859. if (i$2 > segStart) {
  860. segments.push([segStart + lineStart, i$2 + lineStart]);
  861. }
  862. }
  863. }
  864. }
  865. }
  866. });
  867. return segments
  868. }
  869. /**
  870. * @param {string} string
  871. * @param {GetEmbeddingLevelsResult} embedLevelsResult
  872. * @param {number} [start]
  873. * @param {number} [end]
  874. * @return {string} the new string with bidi segments reordered
  875. */
  876. function getReorderedString(string, embedLevelsResult, start, end) {
  877. var indices = getReorderedIndices(string, embedLevelsResult, start, end);
  878. var chars = [].concat( string );
  879. indices.forEach(function (charIndex, i) {
  880. chars[i] = (
  881. (embedLevelsResult.levels[charIndex] & 1) ? getMirroredCharacter(string[charIndex]) : null
  882. ) || string[charIndex];
  883. });
  884. return chars.join('')
  885. }
  886. /**
  887. * @param {string} string
  888. * @param {GetEmbeddingLevelsResult} embedLevelsResult
  889. * @param {number} [start]
  890. * @param {number} [end]
  891. * @return {number[]} an array with character indices in their new bidi order
  892. */
  893. function getReorderedIndices(string, embedLevelsResult, start, end) {
  894. var segments = getReorderSegments(string, embedLevelsResult, start, end);
  895. // Fill an array with indices
  896. var indices = [];
  897. for (var i = 0; i < string.length; i++) {
  898. indices[i] = i;
  899. }
  900. // Reverse each segment in order
  901. segments.forEach(function (ref) {
  902. var start = ref[0];
  903. var end = ref[1];
  904. var slice = indices.slice(start, end + 1);
  905. for (var i = slice.length; i--;) {
  906. indices[end - i] = slice[i];
  907. }
  908. });
  909. return indices
  910. }
  911. exports.closingToOpeningBracket = closingToOpeningBracket;
  912. exports.getBidiCharType = getBidiCharType;
  913. exports.getBidiCharTypeName = getBidiCharTypeName;
  914. exports.getCanonicalBracket = getCanonicalBracket;
  915. exports.getEmbeddingLevels = getEmbeddingLevels;
  916. exports.getMirroredCharacter = getMirroredCharacter;
  917. exports.getMirroredCharactersMap = getMirroredCharactersMap;
  918. exports.getReorderSegments = getReorderSegments;
  919. exports.getReorderedIndices = getReorderedIndices;
  920. exports.getReorderedString = getReorderedString;
  921. exports.openingToClosingBracket = openingToClosingBracket;
  922. Object.defineProperty(exports, '__esModule', { value: true });
  923. return exports;
  924. }({}));
  925. return bidi}
  926. export default bidiFactory;