embeddingLevels.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. import {
  2. BN_LIKE_TYPES,
  3. getBidiCharType,
  4. ISOLATE_INIT_TYPES,
  5. NEUTRAL_ISOLATE_TYPES,
  6. STRONG_TYPES,
  7. TRAILING_TYPES,
  8. TYPES
  9. } from './charTypes.js'
  10. import { closingToOpeningBracket, getCanonicalBracket, openingToClosingBracket } from './brackets.js'
  11. // Local type aliases
  12. const {
  13. L: TYPE_L,
  14. R: TYPE_R,
  15. EN: TYPE_EN,
  16. ES: TYPE_ES,
  17. ET: TYPE_ET,
  18. AN: TYPE_AN,
  19. CS: TYPE_CS,
  20. B: TYPE_B,
  21. S: TYPE_S,
  22. ON: TYPE_ON,
  23. BN: TYPE_BN,
  24. NSM: TYPE_NSM,
  25. AL: TYPE_AL,
  26. LRO: TYPE_LRO,
  27. RLO: TYPE_RLO,
  28. LRE: TYPE_LRE,
  29. RLE: TYPE_RLE,
  30. PDF: TYPE_PDF,
  31. LRI: TYPE_LRI,
  32. RLI: TYPE_RLI,
  33. FSI: TYPE_FSI,
  34. PDI: TYPE_PDI
  35. } = TYPES
  36. /**
  37. * @typedef {object} GetEmbeddingLevelsResult
  38. * @property {{start: number, end: number, level: number}[]} paragraphs
  39. * @property {Uint8Array} levels
  40. */
  41. /**
  42. * This function applies the Bidirectional Algorithm to a string, returning the resolved embedding levels
  43. * in a single Uint8Array plus a list of objects holding each paragraph's start and end indices and resolved
  44. * base embedding level.
  45. *
  46. * @param {string} string - The input string
  47. * @param {"ltr"|"rtl"|"auto"} [baseDirection] - Use "ltr" or "rtl" to force a base paragraph direction,
  48. * otherwise a direction will be chosen automatically from each paragraph's contents.
  49. * @return {GetEmbeddingLevelsResult}
  50. */
  51. export function getEmbeddingLevels (string, baseDirection) {
  52. const MAX_DEPTH = 125
  53. // Start by mapping all characters to their unicode type, as a bitmask integer
  54. const charTypes = new Uint32Array(string.length)
  55. for (let i = 0; i < string.length; i++) {
  56. charTypes[i] = getBidiCharType(string[i])
  57. }
  58. const charTypeCounts = new Map() //will be cleared at start of each paragraph
  59. function changeCharType(i, type) {
  60. const oldType = charTypes[i]
  61. charTypes[i] = type
  62. charTypeCounts.set(oldType, charTypeCounts.get(oldType) - 1)
  63. if (oldType & NEUTRAL_ISOLATE_TYPES) {
  64. charTypeCounts.set(NEUTRAL_ISOLATE_TYPES, charTypeCounts.get(NEUTRAL_ISOLATE_TYPES) - 1)
  65. }
  66. charTypeCounts.set(type, (charTypeCounts.get(type) || 0) + 1)
  67. if (type & NEUTRAL_ISOLATE_TYPES) {
  68. charTypeCounts.set(NEUTRAL_ISOLATE_TYPES, (charTypeCounts.get(NEUTRAL_ISOLATE_TYPES) || 0) + 1)
  69. }
  70. }
  71. const embedLevels = new Uint8Array(string.length)
  72. const isolationPairs = new Map() //init->pdi and pdi->init
  73. // === 3.3.1 The Paragraph Level ===
  74. // 3.3.1 P1: Split the text into paragraphs
  75. const paragraphs = [] // [{start, end, level}, ...]
  76. let paragraph = null
  77. for (let i = 0; i < string.length; i++) {
  78. if (!paragraph) {
  79. paragraphs.push(paragraph = {
  80. start: i,
  81. end: string.length - 1,
  82. // 3.3.1 P2-P3: Determine the paragraph level
  83. level: baseDirection === 'rtl' ? 1 : baseDirection === 'ltr' ? 0 : determineAutoEmbedLevel(i, false)
  84. })
  85. }
  86. if (charTypes[i] & TYPE_B) {
  87. paragraph.end = i
  88. paragraph = null
  89. }
  90. }
  91. const FORMATTING_TYPES = TYPE_RLE | TYPE_LRE | TYPE_RLO | TYPE_LRO | ISOLATE_INIT_TYPES | TYPE_PDI | TYPE_PDF | TYPE_B
  92. const nextEven = n => n + ((n & 1) ? 1 : 2)
  93. const nextOdd = n => n + ((n & 1) ? 2 : 1)
  94. // Everything from here on will operate per paragraph.
  95. for (let paraIdx = 0; paraIdx < paragraphs.length; paraIdx++) {
  96. paragraph = paragraphs[paraIdx]
  97. const statusStack = [{
  98. _level: paragraph.level,
  99. _override: 0, //0=neutral, 1=L, 2=R
  100. _isolate: 0 //bool
  101. }]
  102. let stackTop
  103. let overflowIsolateCount = 0
  104. let overflowEmbeddingCount = 0
  105. let validIsolateCount = 0
  106. charTypeCounts.clear()
  107. // === 3.3.2 Explicit Levels and Directions ===
  108. for (let i = paragraph.start; i <= paragraph.end; i++) {
  109. let charType = charTypes[i]
  110. stackTop = statusStack[statusStack.length - 1]
  111. // Set initial counts
  112. charTypeCounts.set(charType, (charTypeCounts.get(charType) || 0) + 1)
  113. if (charType & NEUTRAL_ISOLATE_TYPES) {
  114. charTypeCounts.set(NEUTRAL_ISOLATE_TYPES, (charTypeCounts.get(NEUTRAL_ISOLATE_TYPES) || 0) + 1)
  115. }
  116. // Explicit Embeddings: 3.3.2 X2 - X3
  117. if (charType & FORMATTING_TYPES) { //prefilter all formatters
  118. if (charType & (TYPE_RLE | TYPE_LRE)) {
  119. embedLevels[i] = stackTop._level // 5.2
  120. const level = (charType === TYPE_RLE ? nextOdd : nextEven)(stackTop._level)
  121. if (level <= MAX_DEPTH && !overflowIsolateCount && !overflowEmbeddingCount) {
  122. statusStack.push({
  123. _level: level,
  124. _override: 0,
  125. _isolate: 0
  126. })
  127. } else if (!overflowIsolateCount) {
  128. overflowEmbeddingCount++
  129. }
  130. }
  131. // Explicit Overrides: 3.3.2 X4 - X5
  132. else if (charType & (TYPE_RLO | TYPE_LRO)) {
  133. embedLevels[i] = stackTop._level // 5.2
  134. const level = (charType === TYPE_RLO ? nextOdd : nextEven)(stackTop._level)
  135. if (level <= MAX_DEPTH && !overflowIsolateCount && !overflowEmbeddingCount) {
  136. statusStack.push({
  137. _level: level,
  138. _override: (charType & TYPE_RLO) ? TYPE_R : TYPE_L,
  139. _isolate: 0
  140. })
  141. } else if (!overflowIsolateCount) {
  142. overflowEmbeddingCount++
  143. }
  144. }
  145. // Isolates: 3.3.2 X5a - X5c
  146. else if (charType & ISOLATE_INIT_TYPES) {
  147. // X5c - FSI becomes either RLI or LRI
  148. if (charType & TYPE_FSI) {
  149. charType = determineAutoEmbedLevel(i + 1, true) === 1 ? TYPE_RLI : TYPE_LRI
  150. }
  151. embedLevels[i] = stackTop._level
  152. if (stackTop._override) {
  153. changeCharType(i, stackTop._override)
  154. }
  155. const level = (charType === TYPE_RLI ? nextOdd : nextEven)(stackTop._level)
  156. if (level <= MAX_DEPTH && overflowIsolateCount === 0 && overflowEmbeddingCount === 0) {
  157. validIsolateCount++
  158. statusStack.push({
  159. _level: level,
  160. _override: 0,
  161. _isolate: 1,
  162. _isolInitIndex: i
  163. })
  164. } else {
  165. overflowIsolateCount++
  166. }
  167. }
  168. // Terminating Isolates: 3.3.2 X6a
  169. else if (charType & TYPE_PDI) {
  170. if (overflowIsolateCount > 0) {
  171. overflowIsolateCount--
  172. } else if (validIsolateCount > 0) {
  173. overflowEmbeddingCount = 0
  174. while (!statusStack[statusStack.length - 1]._isolate) {
  175. statusStack.pop()
  176. }
  177. // Add to isolation pairs bidirectional mapping:
  178. const isolInitIndex = statusStack[statusStack.length - 1]._isolInitIndex
  179. if (isolInitIndex != null) {
  180. isolationPairs.set(isolInitIndex, i)
  181. isolationPairs.set(i, isolInitIndex)
  182. }
  183. statusStack.pop()
  184. validIsolateCount--
  185. }
  186. stackTop = statusStack[statusStack.length - 1]
  187. embedLevels[i] = stackTop._level
  188. if (stackTop._override) {
  189. changeCharType(i, stackTop._override)
  190. }
  191. }
  192. // Terminating Embeddings and Overrides: 3.3.2 X7
  193. else if (charType & TYPE_PDF) {
  194. if (overflowIsolateCount === 0) {
  195. if (overflowEmbeddingCount > 0) {
  196. overflowEmbeddingCount--
  197. } else if (!stackTop._isolate && statusStack.length > 1) {
  198. statusStack.pop()
  199. stackTop = statusStack[statusStack.length - 1]
  200. }
  201. }
  202. embedLevels[i] = stackTop._level // 5.2
  203. }
  204. // End of Paragraph: 3.3.2 X8
  205. else if (charType & TYPE_B) {
  206. embedLevels[i] = paragraph.level
  207. }
  208. }
  209. // Non-formatting characters: 3.3.2 X6
  210. else {
  211. embedLevels[i] = stackTop._level
  212. // NOTE: This exclusion of BN seems to go against what section 5.2 says, but is required for test passage
  213. if (stackTop._override && charType !== TYPE_BN) {
  214. changeCharType(i, stackTop._override)
  215. }
  216. }
  217. }
  218. // === 3.3.3 Preparations for Implicit Processing ===
  219. // Remove all RLE, LRE, RLO, LRO, PDF, and BN characters: 3.3.3 X9
  220. // Note: Due to section 5.2, we won't remove them, but we'll use the BN_LIKE_TYPES bitset to
  221. // easily ignore them all from here on out.
  222. // 3.3.3 X10
  223. // Compute the set of isolating run sequences as specified by BD13
  224. const levelRuns = []
  225. let currentRun = null
  226. let isolationLevel = 0
  227. for (let i = paragraph.start; i <= paragraph.end; i++) {
  228. const charType = charTypes[i]
  229. if (!(charType & BN_LIKE_TYPES)) {
  230. const lvl = embedLevels[i]
  231. const isIsolInit = charType & ISOLATE_INIT_TYPES
  232. const isPDI = charType === TYPE_PDI
  233. if (isIsolInit) {
  234. isolationLevel++
  235. }
  236. if (currentRun && lvl === currentRun._level) {
  237. currentRun._end = i
  238. currentRun._endsWithIsolInit = isIsolInit
  239. } else {
  240. levelRuns.push(currentRun = {
  241. _start: i,
  242. _end: i,
  243. _level: lvl,
  244. _startsWithPDI: isPDI,
  245. _endsWithIsolInit: isIsolInit
  246. })
  247. }
  248. if (isPDI) {
  249. isolationLevel--
  250. }
  251. }
  252. }
  253. const isolatingRunSeqs = [] // [{seqIndices: [], sosType: L|R, eosType: L|R}]
  254. for (let runIdx = 0; runIdx < levelRuns.length; runIdx++) {
  255. const run = levelRuns[runIdx]
  256. if (!run._startsWithPDI || (run._startsWithPDI && !isolationPairs.has(run._start))) {
  257. const seqRuns = [currentRun = run]
  258. for (let pdiIndex; currentRun && currentRun._endsWithIsolInit && (pdiIndex = isolationPairs.get(currentRun._end)) != null;) {
  259. for (let i = runIdx + 1; i < levelRuns.length; i++) {
  260. if (levelRuns[i]._start === pdiIndex) {
  261. seqRuns.push(currentRun = levelRuns[i])
  262. break
  263. }
  264. }
  265. }
  266. // build flat list of indices across all runs:
  267. const seqIndices = []
  268. for (let i = 0; i < seqRuns.length; i++) {
  269. const run = seqRuns[i]
  270. for (let j = run._start; j <= run._end; j++) {
  271. seqIndices.push(j)
  272. }
  273. }
  274. // determine the sos/eos types:
  275. let firstLevel = embedLevels[seqIndices[0]]
  276. let prevLevel = paragraph.level
  277. for (let i = seqIndices[0] - 1; i >= 0; i--) {
  278. if (!(charTypes[i] & BN_LIKE_TYPES)) { //5.2
  279. prevLevel = embedLevels[i]
  280. break
  281. }
  282. }
  283. const lastIndex = seqIndices[seqIndices.length - 1]
  284. let lastLevel = embedLevels[lastIndex]
  285. let nextLevel = paragraph.level
  286. if (!(charTypes[lastIndex] & ISOLATE_INIT_TYPES)) {
  287. for (let i = lastIndex + 1; i <= paragraph.end; i++) {
  288. if (!(charTypes[i] & BN_LIKE_TYPES)) { //5.2
  289. nextLevel = embedLevels[i]
  290. break
  291. }
  292. }
  293. }
  294. isolatingRunSeqs.push({
  295. _seqIndices: seqIndices,
  296. _sosType: Math.max(prevLevel, firstLevel) % 2 ? TYPE_R : TYPE_L,
  297. _eosType: Math.max(nextLevel, lastLevel) % 2 ? TYPE_R : TYPE_L
  298. })
  299. }
  300. }
  301. // The next steps are done per isolating run sequence
  302. for (let seqIdx = 0; seqIdx < isolatingRunSeqs.length; seqIdx++) {
  303. const { _seqIndices: seqIndices, _sosType: sosType, _eosType: eosType } = isolatingRunSeqs[seqIdx]
  304. /**
  305. * All the level runs in an isolating run sequence have the same embedding level.
  306. *
  307. * DO NOT change any `embedLevels[i]` within the current scope.
  308. */
  309. const embedDirection = ((embedLevels[seqIndices[0]]) & 1) ? TYPE_R : TYPE_L;
  310. // === 3.3.4 Resolving Weak Types ===
  311. // W1 + 5.2. Search backward from each NSM to the first character in the isolating run sequence whose
  312. // bidirectional type is not BN, and set the NSM to ON if it is an isolate initiator or PDI, and to its
  313. // type otherwise. If the NSM is the first non-BN character, change the NSM to the type of sos.
  314. if (charTypeCounts.get(TYPE_NSM)) {
  315. for (let si = 0; si < seqIndices.length; si++) {
  316. const i = seqIndices[si]
  317. if (charTypes[i] & TYPE_NSM) {
  318. let prevType = sosType
  319. for (let sj = si - 1; sj >= 0; sj--) {
  320. if (!(charTypes[seqIndices[sj]] & BN_LIKE_TYPES)) { //5.2 scan back to first non-BN
  321. prevType = charTypes[seqIndices[sj]]
  322. break
  323. }
  324. }
  325. changeCharType(i, (prevType & (ISOLATE_INIT_TYPES | TYPE_PDI)) ? TYPE_ON : prevType)
  326. }
  327. }
  328. }
  329. // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sos)
  330. // is found. If an AL is found, change the type of the European number to Arabic number.
  331. if (charTypeCounts.get(TYPE_EN)) {
  332. for (let si = 0; si < seqIndices.length; si++) {
  333. const i = seqIndices[si]
  334. if (charTypes[i] & TYPE_EN) {
  335. for (let sj = si - 1; sj >= -1; sj--) {
  336. const prevCharType = sj === -1 ? sosType : charTypes[seqIndices[sj]]
  337. if (prevCharType & STRONG_TYPES) {
  338. if (prevCharType === TYPE_AL) {
  339. changeCharType(i, TYPE_AN)
  340. }
  341. break
  342. }
  343. }
  344. }
  345. }
  346. }
  347. // W3. Change all ALs to R
  348. if (charTypeCounts.get(TYPE_AL)) {
  349. for (let si = 0; si < seqIndices.length; si++) {
  350. const i = seqIndices[si]
  351. if (charTypes[i] & TYPE_AL) {
  352. changeCharType(i, TYPE_R)
  353. }
  354. }
  355. }
  356. // W4. A single European separator between two European numbers changes to a European number. A single common
  357. // separator between two numbers of the same type changes to that type.
  358. if (charTypeCounts.get(TYPE_ES) || charTypeCounts.get(TYPE_CS)) {
  359. for (let si = 1; si < seqIndices.length - 1; si++) {
  360. const i = seqIndices[si]
  361. if (charTypes[i] & (TYPE_ES | TYPE_CS)) {
  362. let prevType = 0, nextType = 0
  363. for (let sj = si - 1; sj >= 0; sj--) {
  364. prevType = charTypes[seqIndices[sj]]
  365. if (!(prevType & BN_LIKE_TYPES)) { //5.2
  366. break
  367. }
  368. }
  369. for (let sj = si + 1; sj < seqIndices.length; sj++) {
  370. nextType = charTypes[seqIndices[sj]]
  371. if (!(nextType & BN_LIKE_TYPES)) { //5.2
  372. break
  373. }
  374. }
  375. if (prevType === nextType && (charTypes[i] === TYPE_ES ? prevType === TYPE_EN : (prevType & (TYPE_EN | TYPE_AN)))) {
  376. changeCharType(i, prevType)
  377. }
  378. }
  379. }
  380. }
  381. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  382. if (charTypeCounts.get(TYPE_EN)) {
  383. for (let si = 0; si < seqIndices.length; si++) {
  384. const i = seqIndices[si]
  385. if (charTypes[i] & TYPE_EN) {
  386. for (let sj = si - 1; sj >= 0 && (charTypes[seqIndices[sj]] & (TYPE_ET | BN_LIKE_TYPES)); sj--) {
  387. changeCharType(seqIndices[sj], TYPE_EN)
  388. }
  389. for (si++; si < seqIndices.length && (charTypes[seqIndices[si]] & (TYPE_ET | BN_LIKE_TYPES | TYPE_EN)); si++) {
  390. if (charTypes[seqIndices[si]] !== TYPE_EN) {
  391. changeCharType(seqIndices[si], TYPE_EN)
  392. }
  393. }
  394. }
  395. }
  396. }
  397. // W6. Otherwise, separators and terminators change to Other Neutral.
  398. if (charTypeCounts.get(TYPE_ET) || charTypeCounts.get(TYPE_ES) || charTypeCounts.get(TYPE_CS)) {
  399. for (let si = 0; si < seqIndices.length; si++) {
  400. const i = seqIndices[si]
  401. if (charTypes[i] & (TYPE_ET | TYPE_ES | TYPE_CS)) {
  402. changeCharType(i, TYPE_ON)
  403. // 5.2 transform adjacent BNs too:
  404. for (let sj = si - 1; sj >= 0 && (charTypes[seqIndices[sj]] & BN_LIKE_TYPES); sj--) {
  405. changeCharType(seqIndices[sj], TYPE_ON)
  406. }
  407. for (let sj = si + 1; sj < seqIndices.length && (charTypes[seqIndices[sj]] & BN_LIKE_TYPES); sj++) {
  408. changeCharType(seqIndices[sj], TYPE_ON)
  409. }
  410. }
  411. }
  412. }
  413. // W7. Search backward from each instance of a European number until the first strong type (R, L, or sos)
  414. // is found. If an L is found, then change the type of the European number to L.
  415. // NOTE: implemented in single forward pass for efficiency
  416. if (charTypeCounts.get(TYPE_EN)) {
  417. for (let si = 0, prevStrongType = sosType; si < seqIndices.length; si++) {
  418. const i = seqIndices[si]
  419. const type = charTypes[i]
  420. if (type & TYPE_EN) {
  421. if (prevStrongType === TYPE_L) {
  422. changeCharType(i, TYPE_L)
  423. }
  424. } else if (type & STRONG_TYPES) {
  425. prevStrongType = type
  426. }
  427. }
  428. }
  429. // === 3.3.5 Resolving Neutral and Isolate Formatting Types ===
  430. if (charTypeCounts.get(NEUTRAL_ISOLATE_TYPES)) {
  431. // N0. Process bracket pairs in an isolating run sequence sequentially in the logical order of the text
  432. // positions of the opening paired brackets using the logic given below. Within this scope, bidirectional
  433. // types EN and AN are treated as R.
  434. const R_TYPES_FOR_N_STEPS = (TYPE_R | TYPE_EN | TYPE_AN)
  435. const STRONG_TYPES_FOR_N_STEPS = R_TYPES_FOR_N_STEPS | TYPE_L
  436. // * Identify the bracket pairs in the current isolating run sequence according to BD16.
  437. const bracketPairs = []
  438. {
  439. const openerStack = []
  440. for (let si = 0; si < seqIndices.length; si++) {
  441. // NOTE: for any potential bracket character we also test that it still carries a NI
  442. // type, as that may have been changed earlier. This doesn't seem to be explicitly
  443. // called out in the spec, but is required for passage of certain tests.
  444. if (charTypes[seqIndices[si]] & NEUTRAL_ISOLATE_TYPES) {
  445. const char = string[seqIndices[si]]
  446. let oppositeBracket
  447. // Opening bracket
  448. if (openingToClosingBracket(char) !== null) {
  449. if (openerStack.length < 63) {
  450. openerStack.push({ char, seqIndex: si })
  451. } else {
  452. break
  453. }
  454. }
  455. // Closing bracket
  456. else if ((oppositeBracket = closingToOpeningBracket(char)) !== null) {
  457. for (let stackIdx = openerStack.length - 1; stackIdx >= 0; stackIdx--) {
  458. const stackChar = openerStack[stackIdx].char
  459. if (stackChar === oppositeBracket ||
  460. stackChar === closingToOpeningBracket(getCanonicalBracket(char)) ||
  461. openingToClosingBracket(getCanonicalBracket(stackChar)) === char
  462. ) {
  463. bracketPairs.push([openerStack[stackIdx].seqIndex, si])
  464. openerStack.length = stackIdx //pop the matching bracket and all following
  465. break
  466. }
  467. }
  468. }
  469. }
  470. }
  471. bracketPairs.sort((a, b) => a[0] - b[0])
  472. }
  473. // * For each bracket-pair element in the list of pairs of text positions
  474. for (let pairIdx = 0; pairIdx < bracketPairs.length; pairIdx++) {
  475. const [openSeqIdx, closeSeqIdx] = bracketPairs[pairIdx]
  476. // a. Inspect the bidirectional types of the characters enclosed within the bracket pair.
  477. // b. If any strong type (either L or R) matching the embedding direction is found, set the type for both
  478. // brackets in the pair to match the embedding direction.
  479. let foundStrongType = false
  480. let useStrongType = 0
  481. for (let si = openSeqIdx + 1; si < closeSeqIdx; si++) {
  482. const i = seqIndices[si]
  483. if (charTypes[i] & STRONG_TYPES_FOR_N_STEPS) {
  484. foundStrongType = true
  485. const lr = (charTypes[i] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L
  486. if (lr === embedDirection) {
  487. useStrongType = lr
  488. break
  489. }
  490. }
  491. }
  492. // c. Otherwise, if there is a strong type it must be opposite the embedding direction. Therefore, test
  493. // for an established context with a preceding strong type by checking backwards before the opening paired
  494. // bracket until the first strong type (L, R, or sos) is found.
  495. // 1. If the preceding strong type is also opposite the embedding direction, context is established, so
  496. // set the type for both brackets in the pair to that direction.
  497. // 2. Otherwise set the type for both brackets in the pair to the embedding direction.
  498. if (foundStrongType && !useStrongType) {
  499. useStrongType = sosType
  500. for (let si = openSeqIdx - 1; si >= 0; si--) {
  501. const i = seqIndices[si]
  502. if (charTypes[i] & STRONG_TYPES_FOR_N_STEPS) {
  503. const lr = (charTypes[i] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L
  504. if (lr !== embedDirection) {
  505. useStrongType = lr
  506. } else {
  507. useStrongType = embedDirection
  508. }
  509. break
  510. }
  511. }
  512. }
  513. if (useStrongType) {
  514. charTypes[seqIndices[openSeqIdx]] = charTypes[seqIndices[closeSeqIdx]] = useStrongType
  515. // * Any number of characters that had original bidirectional character type NSM prior to the application
  516. // of W1 that immediately follow a paired bracket which changed to L or R under N0 should change to match
  517. // the type of their preceding bracket.
  518. if (useStrongType !== embedDirection) {
  519. for (let si = openSeqIdx + 1; si < seqIndices.length; si++) {
  520. if (!(charTypes[seqIndices[si]] & BN_LIKE_TYPES)) {
  521. if (getBidiCharType(string[seqIndices[si]]) & TYPE_NSM) {
  522. charTypes[seqIndices[si]] = useStrongType
  523. }
  524. break
  525. }
  526. }
  527. }
  528. if (useStrongType !== embedDirection) {
  529. for (let si = closeSeqIdx + 1; si < seqIndices.length; si++) {
  530. if (!(charTypes[seqIndices[si]] & BN_LIKE_TYPES)) {
  531. if (getBidiCharType(string[seqIndices[si]]) & TYPE_NSM) {
  532. charTypes[seqIndices[si]] = useStrongType
  533. }
  534. break
  535. }
  536. }
  537. }
  538. }
  539. }
  540. // N1. A sequence of NIs takes the direction of the surrounding strong text if the text on both sides has the
  541. // same direction.
  542. // N2. Any remaining NIs take the embedding direction.
  543. for (let si = 0; si < seqIndices.length; si++) {
  544. if (charTypes[seqIndices[si]] & NEUTRAL_ISOLATE_TYPES) {
  545. let niRunStart = si, niRunEnd = si
  546. let prevType = sosType //si === 0 ? sosType : (charTypes[seqIndices[si - 1]] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L
  547. for (let si2 = si - 1; si2 >= 0; si2--) {
  548. if (charTypes[seqIndices[si2]] & BN_LIKE_TYPES) {
  549. niRunStart = si2 //5.2 treat BNs adjacent to NIs as NIs
  550. } else {
  551. prevType = (charTypes[seqIndices[si2]] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L
  552. break
  553. }
  554. }
  555. let nextType = eosType
  556. for (let si2 = si + 1; si2 < seqIndices.length; si2++) {
  557. if (charTypes[seqIndices[si2]] & (NEUTRAL_ISOLATE_TYPES | BN_LIKE_TYPES)) {
  558. niRunEnd = si2
  559. } else {
  560. nextType = (charTypes[seqIndices[si2]] & R_TYPES_FOR_N_STEPS) ? TYPE_R : TYPE_L
  561. break
  562. }
  563. }
  564. for (let sj = niRunStart; sj <= niRunEnd; sj++) {
  565. charTypes[seqIndices[sj]] = prevType === nextType ? prevType : embedDirection
  566. }
  567. si = niRunEnd
  568. }
  569. }
  570. }
  571. }
  572. // === 3.3.6 Resolving Implicit Levels ===
  573. for (let i = paragraph.start; i <= paragraph.end; i++) {
  574. const level = embedLevels[i]
  575. const type = charTypes[i]
  576. // I2. For all characters with an odd (right-to-left) embedding level, those of type L, EN or AN go up one level.
  577. if (level & 1) {
  578. if (type & (TYPE_L | TYPE_EN | TYPE_AN)) {
  579. embedLevels[i]++
  580. }
  581. }
  582. // I1. For all characters with an even (left-to-right) embedding level, those of type R go up one level
  583. // and those of type AN or EN go up two levels.
  584. else {
  585. if (type & TYPE_R) {
  586. embedLevels[i]++
  587. } else if (type & (TYPE_AN | TYPE_EN)) {
  588. embedLevels[i] += 2
  589. }
  590. }
  591. // 5.2: Resolve any LRE, RLE, LRO, RLO, PDF, or BN to the level of the preceding character if there is one,
  592. // and otherwise to the base level.
  593. if (type & BN_LIKE_TYPES) {
  594. embedLevels[i] = i === 0 ? paragraph.level : embedLevels[i - 1]
  595. }
  596. // 3.4 L1.1-4: Reset the embedding level of segment/paragraph separators, and any sequence of whitespace or
  597. // isolate formatting characters preceding them or the end of the paragraph, to the paragraph level.
  598. // NOTE: this will also need to be applied to each individual line ending after line wrapping occurs.
  599. if (i === paragraph.end || getBidiCharType(string[i]) & (TYPE_S | TYPE_B)) {
  600. for (let j = i; j >= 0 && (getBidiCharType(string[j]) & TRAILING_TYPES); j--) {
  601. embedLevels[j] = paragraph.level
  602. }
  603. }
  604. }
  605. }
  606. // DONE! The resolved levels can then be used, after line wrapping, to flip runs of characters
  607. // according to section 3.4 Reordering Resolved Levels
  608. return {
  609. levels: embedLevels,
  610. paragraphs
  611. }
  612. function determineAutoEmbedLevel (start, isFSI) {
  613. // 3.3.1 P2 - P3
  614. for (let i = start; i < string.length; i++) {
  615. const charType = charTypes[i]
  616. if (charType & (TYPE_R | TYPE_AL)) {
  617. return 1
  618. }
  619. if ((charType & (TYPE_B | TYPE_L)) || (isFSI && charType === TYPE_PDI)) {
  620. return 0
  621. }
  622. if (charType & ISOLATE_INIT_TYPES) {
  623. const pdi = indexOfMatchingPDI(i)
  624. i = pdi === -1 ? string.length : pdi
  625. }
  626. }
  627. return 0
  628. }
  629. function indexOfMatchingPDI (isolateStart) {
  630. // 3.1.2 BD9
  631. let isolationLevel = 1
  632. for (let i = isolateStart + 1; i < string.length; i++) {
  633. const charType = charTypes[i]
  634. if (charType & TYPE_B) {
  635. break
  636. }
  637. if (charType & TYPE_PDI) {
  638. if (--isolationLevel === 0) {
  639. return i
  640. }
  641. } else if (charType & ISOLATE_INIT_TYPES) {
  642. isolationLevel++
  643. }
  644. }
  645. return -1
  646. }
  647. }