BiDiAlgorithm.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. // Copyright (c) Six Labors.
  2. // Licensed under the Apache License, Version 2.0.
  3. // Ported from: https://github.com/SixLabors/Fonts/
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading;
  8. using Avalonia.Utilities;
  9. namespace Avalonia.Media.TextFormatting.Unicode
  10. {
  11. /// <summary>
  12. /// Implementation of Unicode bidirectional algorithm (UAX #9)
  13. /// https://unicode.org/reports/tr9/
  14. /// </summary>
  15. /// <remarks>
  16. /// <para>
  17. /// The Bidi algorithm uses a number of memory arrays for resolved
  18. /// types, level information, bracket types, x9 removal maps and
  19. /// more...
  20. /// </para>
  21. /// <para>
  22. /// This implementation of the BiDi algorithm has been designed
  23. /// to reduce memory pressure on the GC by re-using the same
  24. /// work buffers, so instances of this class should be re-used
  25. /// as much as possible.
  26. /// </para>
  27. /// </remarks>
  28. internal sealed class BidiAlgorithm
  29. {
  30. /// <summary>
  31. /// The original BiDiClass classes as provided by the caller
  32. /// </summary>
  33. private ArraySlice<BidiClass> _originalClasses;
  34. /// <summary>
  35. /// Paired bracket types as provided by caller
  36. /// </summary>
  37. private ArraySlice<BidiPairedBracketType> _pairedBracketTypes;
  38. /// <summary>
  39. /// Paired bracket values as provided by caller
  40. /// </summary>
  41. private ArraySlice<int> _pairedBracketValues;
  42. /// <summary>
  43. /// Try if the incoming data is known to contain brackets
  44. /// </summary>
  45. private bool _hasBrackets;
  46. /// <summary>
  47. /// True if the incoming data is known to contain embedding runs
  48. /// </summary>
  49. private bool _hasEmbeddings;
  50. /// <summary>
  51. /// True if the incoming data is known to contain isolating runs
  52. /// </summary>
  53. private bool _hasIsolates;
  54. /// <summary>
  55. /// Two directional mapping of isolate start/end pairs
  56. /// </summary>
  57. /// <remarks>
  58. /// The forward mapping maps the start index to the end index.
  59. /// The reverse mapping maps the end index to the start index.
  60. /// </remarks>
  61. private readonly BidiDictionary<int, int> _isolatePairs = new BidiDictionary<int, int>();
  62. /// <summary>
  63. /// The working BiDi classes
  64. /// </summary>
  65. private ArraySlice<BidiClass> _workingClasses;
  66. /// <summary>
  67. /// The working classes buffer
  68. /// </summary>
  69. private ArrayBuilder<BidiClass> _workingClassesBuffer;
  70. /// <summary>
  71. /// A slice of the resolved levels
  72. /// </summary>
  73. private ArraySlice<sbyte> _resolvedLevels;
  74. /// <summary>
  75. /// The buffer underlying resolvedLevels
  76. /// </summary>
  77. private ArrayBuilder<sbyte> _resolvedLevelsBuffer;
  78. /// <summary>
  79. /// The resolve paragraph embedding level
  80. /// </summary>
  81. private sbyte _paragraphEmbeddingLevel;
  82. /// <summary>
  83. /// The status stack used during resolution of explicit
  84. /// embedding and isolating runs
  85. /// </summary>
  86. private readonly Stack<Status> _statusStack = new Stack<Status>();
  87. /// <summary>
  88. /// Mapping used to virtually remove characters for rule X9
  89. /// </summary>
  90. private ArrayBuilder<int> _x9Map;
  91. /// <summary>
  92. /// Re-usable list of level runs
  93. /// </summary>
  94. private readonly List<LevelRun> _levelRuns = new List<LevelRun>();
  95. /// <summary>
  96. /// Mapping for the current isolating sequence, built
  97. /// by joining level runs from the x9 map.
  98. /// </summary>
  99. private ArrayBuilder<int> _isolatedRunMapping;
  100. /// <summary>
  101. /// A stack of pending isolate openings used by FindIsolatePairs()
  102. /// </summary>
  103. private readonly Stack<int> _pendingIsolateOpenings = new Stack<int>();
  104. /// <summary>
  105. /// The level of the isolating run currently being processed
  106. /// </summary>
  107. private int _runLevel;
  108. /// <summary>
  109. /// The direction of the isolating run currently being processed
  110. /// </summary>
  111. private BidiClass _runDirection;
  112. /// <summary>
  113. /// The length of the isolating run currently being processed
  114. /// </summary>
  115. private int _runLength;
  116. /// <summary>
  117. /// A mapped slice of the resolved types for the isolating run currently
  118. /// being processed
  119. /// </summary>
  120. private MappedArraySlice<BidiClass> _runResolvedClasses;
  121. /// <summary>
  122. /// A mapped slice of the original types for the isolating run currently
  123. /// being processed
  124. /// </summary>
  125. private MappedArraySlice<BidiClass> _runOriginalClasses;
  126. /// <summary>
  127. /// A mapped slice of the run levels for the isolating run currently
  128. /// being processed
  129. /// </summary>
  130. private MappedArraySlice<sbyte> _runLevels;
  131. /// <summary>
  132. /// A mapped slice of the paired bracket types of the isolating
  133. /// run currently being processed
  134. /// </summary>
  135. private MappedArraySlice<BidiPairedBracketType> _runBiDiPairedBracketTypes;
  136. /// <summary>
  137. /// A mapped slice of the paired bracket values of the isolating
  138. /// run currently being processed
  139. /// </summary>
  140. private MappedArraySlice<int> _runPairedBracketValues;
  141. /// <summary>
  142. /// Maximum pairing depth for paired brackets
  143. /// </summary>
  144. private const int MaxPairedBracketDepth = 63;
  145. /// <summary>
  146. /// Reusable list of pending opening brackets used by the
  147. /// LocatePairedBrackets method
  148. /// </summary>
  149. private readonly List<int> _pendingOpeningBrackets = new List<int>();
  150. /// <summary>
  151. /// Resolved list of paired brackets
  152. /// </summary>
  153. private readonly List<BracketPair> _pairedBrackets = new List<BracketPair>();
  154. /// <summary>
  155. /// Initializes a new instance of the <see cref="BidiAlgorithm"/> class.
  156. /// </summary>
  157. internal BidiAlgorithm()
  158. {
  159. }
  160. /// <summary>
  161. /// Gets the resolved levels.
  162. /// </summary>
  163. public ArraySlice<sbyte> ResolvedLevels => _resolvedLevels;
  164. /// <summary>
  165. /// Gets the resolved paragraph embedding level
  166. /// </summary>
  167. public int ResolvedParagraphEmbeddingLevel => _paragraphEmbeddingLevel;
  168. /// <summary>
  169. /// Process data from a BiDiData instance
  170. /// </summary>
  171. /// <param name="data">The BiDi Unicode data.</param>
  172. public void Process(BidiData data)
  173. => Process(
  174. data.Classes,
  175. data.PairedBracketTypes,
  176. data.PairedBracketValues,
  177. data.ParagraphEmbeddingLevel,
  178. data.HasBrackets,
  179. data.HasEmbeddings,
  180. data.HasIsolates,
  181. null);
  182. /// <summary>
  183. /// Processes Bidi Data
  184. /// </summary>
  185. public void Process(
  186. ArraySlice<BidiClass> types,
  187. ArraySlice<BidiPairedBracketType> pairedBracketTypes,
  188. ArraySlice<int> pairedBracketValues,
  189. sbyte paragraphEmbeddingLevel,
  190. bool? hasBrackets,
  191. bool? hasEmbeddings,
  192. bool? hasIsolates,
  193. ArraySlice<sbyte>? outLevels)
  194. {
  195. // Reset state
  196. _isolatePairs.Clear();
  197. _workingClassesBuffer.Clear();
  198. _levelRuns.Clear();
  199. _resolvedLevelsBuffer.Clear();
  200. if (types.IsEmpty)
  201. {
  202. return;
  203. }
  204. // Setup original types and working types
  205. _originalClasses = types;
  206. _workingClasses = _workingClassesBuffer.Add(types);
  207. // Capture paired bracket values and types
  208. _pairedBracketTypes = pairedBracketTypes;
  209. _pairedBracketValues = pairedBracketValues;
  210. // Store things we know
  211. _hasBrackets = hasBrackets ?? _pairedBracketTypes.Length == _originalClasses.Length;
  212. _hasEmbeddings = hasEmbeddings ?? true;
  213. _hasIsolates = hasIsolates ?? true;
  214. // Find all isolate pairs
  215. FindIsolatePairs();
  216. // Resolve the paragraph embedding level
  217. if (paragraphEmbeddingLevel == 2)
  218. {
  219. _paragraphEmbeddingLevel = ResolveEmbeddingLevel(_originalClasses);
  220. }
  221. else
  222. {
  223. _paragraphEmbeddingLevel = paragraphEmbeddingLevel;
  224. }
  225. // Create resolved levels buffer
  226. if (outLevels.HasValue)
  227. {
  228. if (outLevels.Value.Length != _originalClasses.Length)
  229. {
  230. throw new ArgumentException("Out levels must be the same length as the input data");
  231. }
  232. _resolvedLevels = outLevels.Value;
  233. }
  234. else
  235. {
  236. _resolvedLevels = _resolvedLevelsBuffer.Add(_originalClasses.Length);
  237. _resolvedLevels.Fill(_paragraphEmbeddingLevel);
  238. }
  239. // Resolve explicit embedding levels (Rules X1-X8)
  240. ResolveExplicitEmbeddingLevels();
  241. // Build the rule X9 map
  242. BuildX9RemovalMap();
  243. // Process all isolated run sequences
  244. ProcessIsolatedRunSequences();
  245. // Reset whitespace levels
  246. ResetWhitespaceLevels();
  247. // Clean up
  248. AssignLevelsToCodePointsRemovedByX9();
  249. }
  250. /// <summary>
  251. /// Resolve the paragraph embedding level if not explicitly passed
  252. /// by the caller. Also used by rule X5c for FSI isolating sequences.
  253. /// </summary>
  254. /// <param name="data">The data to be evaluated</param>
  255. /// <returns>The resolved embedding level</returns>
  256. public sbyte ResolveEmbeddingLevel(ArraySlice<BidiClass> data)
  257. {
  258. // P2
  259. for (var i = 0; i < data.Length; ++i)
  260. {
  261. switch (data[i])
  262. {
  263. case BidiClass.LeftToRight:
  264. // P3
  265. return 0;
  266. case BidiClass.ArabicLetter:
  267. case BidiClass.RightToLeft:
  268. // P3
  269. return 1;
  270. case BidiClass.FirstStrongIsolate:
  271. case BidiClass.LeftToRightIsolate:
  272. case BidiClass.RightToLeftIsolate:
  273. // Skip isolate pairs
  274. // (Because we're working with a slice, we need to adjust the indices
  275. // we're using for the isolatePairs map)
  276. if (_isolatePairs.TryGetValue(data.Start + i, out i))
  277. {
  278. i -= data.Start;
  279. }
  280. else
  281. {
  282. i = data.Length;
  283. }
  284. break;
  285. }
  286. }
  287. // P3
  288. return 0;
  289. }
  290. /// <summary>
  291. /// Build a list of matching isolates for a directionality slice
  292. /// Implements BD9
  293. /// </summary>
  294. private void FindIsolatePairs()
  295. {
  296. // Redundant?
  297. if (!_hasIsolates)
  298. {
  299. return;
  300. }
  301. // Lets double check this as we go and clear the flag
  302. // if there actually aren't any isolate pairs as this might
  303. // mean we can skip some later steps
  304. _hasIsolates = false;
  305. // BD9...
  306. _pendingIsolateOpenings.Clear();
  307. for (var i = 0; i < _originalClasses.Length; i++)
  308. {
  309. var t = _originalClasses[i];
  310. switch (t)
  311. {
  312. case BidiClass.LeftToRightIsolate:
  313. case BidiClass.RightToLeftIsolate:
  314. case BidiClass.FirstStrongIsolate:
  315. {
  316. _pendingIsolateOpenings.Push(i);
  317. _hasIsolates = true;
  318. break;
  319. }
  320. case BidiClass.PopDirectionalIsolate:
  321. {
  322. if (_pendingIsolateOpenings.Count > 0)
  323. {
  324. _isolatePairs.Add(_pendingIsolateOpenings.Pop(), i);
  325. }
  326. _hasIsolates = true;
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. /// <summary>
  333. /// Resolve the explicit embedding levels from the original
  334. /// data. Implements rules X1 to X8.
  335. /// </summary>
  336. private void ResolveExplicitEmbeddingLevels()
  337. {
  338. // Redundant?
  339. if (!_hasIsolates && !_hasEmbeddings)
  340. {
  341. return;
  342. }
  343. // Work variables
  344. _statusStack.Clear();
  345. var overflowIsolateCount = 0;
  346. var overflowEmbeddingCount = 0;
  347. var validIsolateCount = 0;
  348. // Constants
  349. const int maxStackDepth = 125;
  350. // Rule X1 - setup initial state
  351. _statusStack.Clear();
  352. // Neutral
  353. _statusStack.Push(new Status(_paragraphEmbeddingLevel, BidiClass.OtherNeutral, false));
  354. // Process all characters
  355. for (var i = 0; i < _originalClasses.Length; i++)
  356. {
  357. switch (_originalClasses[i])
  358. {
  359. case BidiClass.RightToLeftEmbedding:
  360. {
  361. // Rule X2
  362. var newLevel = (sbyte)((_statusStack.Peek().EmbeddingLevel + 1) | 1);
  363. if (newLevel <= maxStackDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0)
  364. {
  365. _statusStack.Push(new Status(newLevel, BidiClass.OtherNeutral, false));
  366. _resolvedLevels[i] = newLevel;
  367. }
  368. else if (overflowIsolateCount == 0)
  369. {
  370. overflowEmbeddingCount++;
  371. }
  372. break;
  373. }
  374. case BidiClass.LeftToRightEmbedding:
  375. {
  376. // Rule X3
  377. var newLevel = (sbyte)((_statusStack.Peek().EmbeddingLevel + 2) & ~1);
  378. if (newLevel < maxStackDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0)
  379. {
  380. _statusStack.Push(new Status(newLevel, BidiClass.OtherNeutral, false));
  381. _resolvedLevels[i] = newLevel;
  382. }
  383. else if (overflowIsolateCount == 0)
  384. {
  385. overflowEmbeddingCount++;
  386. }
  387. break;
  388. }
  389. case BidiClass.RightToLeftOverride:
  390. {
  391. // Rule X4
  392. var newLevel = (sbyte)((_statusStack.Peek().EmbeddingLevel + 1) | 1);
  393. if (newLevel <= maxStackDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0)
  394. {
  395. _statusStack.Push(new Status(newLevel, BidiClass.RightToLeft, false));
  396. _resolvedLevels[i] = newLevel;
  397. }
  398. else if (overflowIsolateCount == 0)
  399. {
  400. overflowEmbeddingCount++;
  401. }
  402. break;
  403. }
  404. case BidiClass.LeftToRightOverride:
  405. {
  406. // Rule X5
  407. var newLevel = (sbyte)((_statusStack.Peek().EmbeddingLevel + 2) & ~1);
  408. if (newLevel <= maxStackDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0)
  409. {
  410. _statusStack.Push(new Status(newLevel, BidiClass.LeftToRight, false));
  411. _resolvedLevels[i] = newLevel;
  412. }
  413. else if (overflowIsolateCount == 0)
  414. {
  415. overflowEmbeddingCount++;
  416. }
  417. break;
  418. }
  419. case BidiClass.RightToLeftIsolate:
  420. case BidiClass.LeftToRightIsolate:
  421. case BidiClass.FirstStrongIsolate:
  422. {
  423. // Rule X5a, X5b and X5c
  424. var resolvedIsolate = _originalClasses[i];
  425. if (resolvedIsolate == BidiClass.FirstStrongIsolate)
  426. {
  427. if (!_isolatePairs.TryGetValue(i, out var endOfIsolate))
  428. {
  429. endOfIsolate = _originalClasses.Length;
  430. }
  431. // Rule X5c
  432. if (ResolveEmbeddingLevel(_originalClasses.Slice(i + 1,endOfIsolate - (i + 1))) == 1)
  433. {
  434. resolvedIsolate = BidiClass.RightToLeftIsolate;
  435. }
  436. else
  437. {
  438. resolvedIsolate = BidiClass.LeftToRightIsolate;
  439. }
  440. }
  441. // Replace RLI's level with current embedding level
  442. var tos = _statusStack.Peek();
  443. _resolvedLevels[i] = tos.EmbeddingLevel;
  444. // Apply override
  445. if (tos.OverrideStatus != BidiClass.OtherNeutral)
  446. {
  447. _workingClasses[i] = tos.OverrideStatus;
  448. }
  449. // Work out new level
  450. sbyte newLevel;
  451. if (resolvedIsolate == BidiClass.RightToLeftIsolate)
  452. {
  453. newLevel = (sbyte)((tos.EmbeddingLevel + 1) | 1);
  454. }
  455. else
  456. {
  457. newLevel = (sbyte)((tos.EmbeddingLevel + 2) & ~1);
  458. }
  459. // Valid?
  460. if (newLevel <= maxStackDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0)
  461. {
  462. validIsolateCount++;
  463. _statusStack.Push(new Status(newLevel, BidiClass.OtherNeutral, true));
  464. }
  465. else
  466. {
  467. overflowIsolateCount++;
  468. }
  469. break;
  470. }
  471. case BidiClass.BoundaryNeutral:
  472. {
  473. // Mentioned in rule X6 - "for all types besides ..., BN, ..."
  474. // no-op
  475. break;
  476. }
  477. default:
  478. {
  479. // Rule X6
  480. var tos = _statusStack.Peek();
  481. _resolvedLevels[i] = tos.EmbeddingLevel;
  482. if (tos.OverrideStatus != BidiClass.OtherNeutral)
  483. {
  484. _workingClasses[i] = tos.OverrideStatus;
  485. }
  486. break;
  487. }
  488. case BidiClass.PopDirectionalIsolate:
  489. {
  490. // Rule X6a
  491. if (overflowIsolateCount > 0)
  492. {
  493. overflowIsolateCount--;
  494. }
  495. else if (validIsolateCount != 0)
  496. {
  497. overflowEmbeddingCount = 0;
  498. while (!_statusStack.Peek().IsolateStatus)
  499. {
  500. _statusStack.Pop();
  501. }
  502. _statusStack.Pop();
  503. validIsolateCount--;
  504. }
  505. var tos = _statusStack.Peek();
  506. _resolvedLevels[i] = tos.EmbeddingLevel;
  507. if (tos.OverrideStatus != BidiClass.OtherNeutral)
  508. {
  509. _workingClasses[i] = tos.OverrideStatus;
  510. }
  511. break;
  512. }
  513. case BidiClass.PopDirectionalFormat:
  514. {
  515. // Rule X7
  516. if (overflowIsolateCount == 0)
  517. {
  518. if (overflowEmbeddingCount > 0)
  519. {
  520. overflowEmbeddingCount--;
  521. }
  522. else if (!_statusStack.Peek().IsolateStatus && _statusStack.Count >= 2)
  523. {
  524. _statusStack.Pop();
  525. }
  526. }
  527. break;
  528. }
  529. case BidiClass.ParagraphSeparator:
  530. {
  531. // Rule X8
  532. _resolvedLevels[i] = _paragraphEmbeddingLevel;
  533. break;
  534. }
  535. }
  536. }
  537. }
  538. /// <summary>
  539. /// Build a map to the original data positions that excludes all
  540. /// the types defined by rule X9
  541. /// </summary>
  542. private void BuildX9RemovalMap()
  543. {
  544. // Reserve room for the x9 map
  545. _x9Map.Length = _originalClasses.Length;
  546. if (_hasEmbeddings || _hasIsolates)
  547. {
  548. // Build a map the removes all x9 characters
  549. var j = 0;
  550. for (var i = 0; i < _originalClasses.Length; i++)
  551. {
  552. if (!IsRemovedByX9(_originalClasses[i]))
  553. {
  554. _x9Map[j++] = i;
  555. }
  556. }
  557. // Set the final length
  558. _x9Map.Length = j;
  559. }
  560. else
  561. {
  562. for (int i = 0, count = _originalClasses.Length; i < count; i++)
  563. {
  564. _x9Map[i] = i;
  565. }
  566. }
  567. }
  568. /// <summary>
  569. /// Find the original character index for an entry in the X9 map
  570. /// </summary>
  571. /// <param name="index">Index in the x9 removal map</param>
  572. /// <returns>Index to the original data</returns>
  573. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  574. private int MapX9(int index) => _x9Map[index];
  575. /// <summary>
  576. /// Add a new level run
  577. /// </summary>
  578. /// <remarks>
  579. /// This method resolves the sos and eos values for the run
  580. /// and adds the run to the list
  581. /// /// </remarks>
  582. /// <param name="start">The index of the start of the run (in x9 removed units)</param>
  583. /// <param name="length">The length of the run (in x9 removed units)</param>
  584. /// <param name="level">The level of the run</param>
  585. private void AddLevelRun(int start, int length, int level)
  586. {
  587. // Get original indices to first and last character in this run
  588. var firstCharIndex = MapX9(start);
  589. var lastCharIndex = MapX9(start + length - 1);
  590. // Work out sos
  591. var i = firstCharIndex - 1;
  592. while (i >= 0 && IsRemovedByX9(_originalClasses[i]))
  593. {
  594. i--;
  595. }
  596. var prevLevel = i < 0 ? _paragraphEmbeddingLevel : _resolvedLevels[i];
  597. var sos = DirectionFromLevel(Math.Max(prevLevel, level));
  598. // Work out eos
  599. var lastType = _workingClasses[lastCharIndex];
  600. int nextLevel;
  601. switch (lastType)
  602. {
  603. case BidiClass.LeftToRightIsolate:
  604. case BidiClass.RightToLeftIsolate:
  605. case BidiClass.FirstStrongIsolate:
  606. {
  607. nextLevel = _paragraphEmbeddingLevel;
  608. break;
  609. }
  610. default:
  611. {
  612. i = lastCharIndex + 1;
  613. while (i < _originalClasses.Length && IsRemovedByX9(_originalClasses[i]))
  614. {
  615. i++;
  616. }
  617. nextLevel = i >= _originalClasses.Length ? _paragraphEmbeddingLevel : _resolvedLevels[i];
  618. break;
  619. }
  620. }
  621. var eos = DirectionFromLevel(Math.Max(nextLevel, level));
  622. // Add the run
  623. _levelRuns.Add(new LevelRun(start, length, level, sos, eos));
  624. }
  625. /// <summary>
  626. /// Find all runs of the same level, populating the _levelRuns
  627. /// collection
  628. /// </summary>
  629. private void FindLevelRuns()
  630. {
  631. var currentLevel = -1;
  632. var runStart = 0;
  633. for (var i = 0; i < _x9Map.Length; ++i)
  634. {
  635. int level = _resolvedLevels[MapX9(i)];
  636. if (level == currentLevel)
  637. {
  638. continue;
  639. }
  640. if (currentLevel != -1)
  641. {
  642. AddLevelRun(runStart, i - runStart, currentLevel);
  643. }
  644. currentLevel = level;
  645. runStart = i;
  646. }
  647. // Don't forget the final level run
  648. if (currentLevel != -1)
  649. {
  650. AddLevelRun(runStart, _x9Map.Length - runStart, currentLevel);
  651. }
  652. }
  653. /// <summary>
  654. /// Given a character index, find the level run that starts at that position
  655. /// </summary>
  656. /// <param name="index">The index into the original (unmapped) data</param>
  657. /// <returns>The index of the run that starts at that index</returns>
  658. private int FindRunForIndex(int index)
  659. {
  660. for (var i = 0; i < _levelRuns.Count; i++)
  661. {
  662. // Passed index is for the original non-x9 filtered data, however
  663. // the level run ranges are for the x9 filtered data. Convert before
  664. // comparing
  665. if (MapX9(_levelRuns[i].Start) == index)
  666. {
  667. return i;
  668. }
  669. }
  670. throw new InvalidOperationException("Internal error");
  671. }
  672. /// <summary>
  673. /// Determine and the process all isolated run sequences
  674. /// </summary>
  675. private void ProcessIsolatedRunSequences()
  676. {
  677. // Find all runs with the same level
  678. FindLevelRuns();
  679. // Process them one at a time by first building
  680. // a mapping using slices from the x9 map for each
  681. // run section that needs to be joined together to
  682. // form an complete run. That full run mapping
  683. // will be placed in _isolatedRunMapping and then
  684. // processed by ProcessIsolatedRunSequence().
  685. while (_levelRuns.Count > 0)
  686. {
  687. // Clear the mapping
  688. _isolatedRunMapping.Clear();
  689. // Combine mappings from this run and all runs that continue on from it
  690. var runIndex = 0;
  691. BidiClass eos;
  692. var sos = _levelRuns[0].Sos;
  693. var level = _levelRuns[0].Level;
  694. while (true)
  695. {
  696. // Get the run
  697. var r = _levelRuns[runIndex];
  698. // The eos of the isolating run is the eos of the
  699. // last level run that comprises it.
  700. eos = r.Eos;
  701. // Remove this run as we've now processed it
  702. _levelRuns.RemoveAt(runIndex);
  703. // Add the x9 map indices for the run range to the mapping
  704. // for this isolated run
  705. _isolatedRunMapping.Add(_x9Map.AsSlice(r.Start, r.Length));
  706. // Get the last character and see if it's an isolating run with a matching
  707. // PDI and concatenate that run to this one
  708. var lastCharacterIndex = _isolatedRunMapping[_isolatedRunMapping.Length - 1];
  709. var lastType = _originalClasses[lastCharacterIndex];
  710. if ((lastType == BidiClass.LeftToRightIsolate || lastType == BidiClass.RightToLeftIsolate || lastType == BidiClass.FirstStrongIsolate) &&
  711. _isolatePairs.TryGetValue(lastCharacterIndex, out var nextRunIndex))
  712. {
  713. // Find the continuing run index
  714. runIndex = FindRunForIndex(nextRunIndex);
  715. }
  716. else
  717. {
  718. break;
  719. }
  720. }
  721. // Process this isolated run
  722. ProcessIsolatedRunSequence(sos, eos, level);
  723. }
  724. }
  725. /// <summary>
  726. /// Process a single isolated run sequence, where the character sequence
  727. /// mapping is currently held in _isolatedRunMapping.
  728. /// </summary>
  729. private void ProcessIsolatedRunSequence(BidiClass sos, BidiClass eos, int runLevel)
  730. {
  731. // Create mappings onto the underlying data
  732. _runResolvedClasses = new MappedArraySlice<BidiClass>(_workingClasses, _isolatedRunMapping.AsSlice());
  733. _runOriginalClasses = new MappedArraySlice<BidiClass>(_originalClasses, _isolatedRunMapping.AsSlice());
  734. _runLevels = new MappedArraySlice<sbyte>(_resolvedLevels, _isolatedRunMapping.AsSlice());
  735. if (_hasBrackets)
  736. {
  737. _runBiDiPairedBracketTypes = new MappedArraySlice<BidiPairedBracketType>(_pairedBracketTypes, _isolatedRunMapping.AsSlice());
  738. _runPairedBracketValues = new MappedArraySlice<int>(_pairedBracketValues, _isolatedRunMapping.AsSlice());
  739. }
  740. _runLevel = runLevel;
  741. _runDirection = DirectionFromLevel(runLevel);
  742. _runLength = _runResolvedClasses.Length;
  743. // By tracking the types of characters known to be in the current run, we can
  744. // skip some of the rules that we know won't apply. The flags will be
  745. // initialized while we're processing rule W1 below.
  746. var hasEN = false;
  747. var hasAL = false;
  748. var hasES = false;
  749. var hasCS = false;
  750. var hasAN = false;
  751. var hasET = false;
  752. // Rule W1
  753. // Also, set hasXX flags
  754. int i;
  755. var previousClass = sos;
  756. for (i = 0; i < _runLength; i++)
  757. {
  758. var resolvedClass = _runResolvedClasses[i];
  759. switch (resolvedClass)
  760. {
  761. case BidiClass.NonspacingMark:
  762. _runResolvedClasses[i] = previousClass;
  763. break;
  764. case BidiClass.LeftToRightIsolate:
  765. case BidiClass.RightToLeftIsolate:
  766. case BidiClass.FirstStrongIsolate:
  767. case BidiClass.PopDirectionalIsolate:
  768. previousClass = BidiClass.OtherNeutral;
  769. break;
  770. case BidiClass.EuropeanNumber:
  771. hasEN = true;
  772. previousClass = resolvedClass;
  773. break;
  774. case BidiClass.ArabicLetter:
  775. hasAL = true;
  776. previousClass = resolvedClass;
  777. break;
  778. case BidiClass.EuropeanSeparator:
  779. hasES = true;
  780. previousClass = resolvedClass;
  781. break;
  782. case BidiClass.CommonSeparator:
  783. hasCS = true;
  784. previousClass = resolvedClass;
  785. break;
  786. case BidiClass.ArabicNumber:
  787. hasAN = true;
  788. previousClass = resolvedClass;
  789. break;
  790. case BidiClass.EuropeanTerminator:
  791. hasET = true;
  792. previousClass = resolvedClass;
  793. break;
  794. default:
  795. previousClass = resolvedClass;
  796. break;
  797. }
  798. }
  799. // Rule W2
  800. if (hasEN)
  801. {
  802. for (i = 0; i < _runLength; i++)
  803. {
  804. if (_runResolvedClasses[i] != BidiClass.EuropeanNumber)
  805. {
  806. continue;
  807. }
  808. for (var j = i - 1; j >= 0; j--)
  809. {
  810. var resolvedClass = _runResolvedClasses[j];
  811. switch (resolvedClass)
  812. {
  813. case BidiClass.LeftToRight:
  814. case BidiClass.RightToLeft:
  815. case BidiClass.ArabicLetter:
  816. {
  817. if (resolvedClass == BidiClass.ArabicLetter)
  818. {
  819. _runResolvedClasses[i] = BidiClass.ArabicNumber;
  820. hasAN = true;
  821. }
  822. j = -1;
  823. break;
  824. }
  825. }
  826. }
  827. }
  828. }
  829. // Rule W3
  830. if (hasAL)
  831. {
  832. for (i = 0; i < _runLength; i++)
  833. {
  834. if (_runResolvedClasses[i] == BidiClass.ArabicLetter)
  835. {
  836. _runResolvedClasses[i] = BidiClass.RightToLeft;
  837. }
  838. }
  839. }
  840. // Rule W4
  841. if ((hasES || hasCS) && (hasEN || hasAN))
  842. {
  843. for (i = 1; i < _runLength - 1; ++i)
  844. {
  845. ref var resolvedClass = ref _runResolvedClasses[i];
  846. if (resolvedClass == BidiClass.EuropeanSeparator)
  847. {
  848. var previousSeparatorClass = _runResolvedClasses[i - 1];
  849. var nextSeparatorClass = _runResolvedClasses[i + 1];
  850. if (previousSeparatorClass == BidiClass.EuropeanNumber && nextSeparatorClass == BidiClass.EuropeanNumber)
  851. {
  852. // ES between EN and EN
  853. resolvedClass = BidiClass.EuropeanNumber;
  854. }
  855. }
  856. else if (resolvedClass == BidiClass.CommonSeparator)
  857. {
  858. var previousSeparatorClass = _runResolvedClasses[i - 1];
  859. var nextSeparatorClass = _runResolvedClasses[i + 1];
  860. if ((previousSeparatorClass == BidiClass.ArabicNumber && nextSeparatorClass == BidiClass.ArabicNumber) ||
  861. (previousSeparatorClass == BidiClass.EuropeanNumber && nextSeparatorClass == BidiClass.EuropeanNumber))
  862. {
  863. // CS between (AN and AN) or (EN and EN)
  864. resolvedClass = previousSeparatorClass;
  865. }
  866. }
  867. }
  868. }
  869. // Rule W5
  870. if (hasET && hasEN)
  871. {
  872. for (i = 0; i < _runLength; ++i)
  873. {
  874. if (_runResolvedClasses[i] != BidiClass.EuropeanTerminator)
  875. {
  876. continue;
  877. }
  878. // Locate end of sequence
  879. var sequenceStart = i;
  880. var sequenceEnd = i;
  881. while (sequenceEnd < _runLength && _runResolvedClasses[sequenceEnd] == BidiClass.EuropeanTerminator)
  882. {
  883. sequenceEnd++;
  884. }
  885. // Preceded by, or followed by EN?
  886. if ((sequenceStart == 0 ? sos : _runResolvedClasses[sequenceStart - 1]) == BidiClass.EuropeanNumber
  887. || (sequenceEnd == _runLength ? eos : _runResolvedClasses[sequenceEnd]) == BidiClass.EuropeanNumber)
  888. {
  889. // Change the entire range
  890. for (var j = sequenceStart; i < sequenceEnd; ++i)
  891. {
  892. _runResolvedClasses[i] = BidiClass.EuropeanNumber;
  893. }
  894. }
  895. // continue at end of sequence
  896. i = sequenceEnd;
  897. }
  898. }
  899. // Rule W6
  900. if (hasES || hasET || hasCS)
  901. {
  902. for (i = 0; i < _runLength; ++i)
  903. {
  904. ref var resolvedClass = ref _runResolvedClasses[i];
  905. switch (resolvedClass)
  906. {
  907. case BidiClass.EuropeanSeparator:
  908. case BidiClass.EuropeanTerminator:
  909. case BidiClass.CommonSeparator:
  910. {
  911. resolvedClass = BidiClass.OtherNeutral;
  912. break;
  913. }
  914. }
  915. }
  916. }
  917. // Rule W7.
  918. if (hasEN)
  919. {
  920. var previousStrongClass = sos;
  921. for (i = 0; i < _runLength; ++i)
  922. {
  923. ref var resolvedClass = ref _runResolvedClasses[i];
  924. switch (resolvedClass)
  925. {
  926. case BidiClass.EuropeanNumber:
  927. {
  928. // If prev strong type was an L change this to L too
  929. if (previousStrongClass == BidiClass.LeftToRight)
  930. {
  931. _runResolvedClasses[i] = BidiClass.LeftToRight;
  932. }
  933. break;
  934. }
  935. case BidiClass.LeftToRight:
  936. case BidiClass.RightToLeft:
  937. {
  938. // Remember previous strong type (NB: AL should already be changed to R)
  939. previousStrongClass = resolvedClass;
  940. break;
  941. }
  942. }
  943. }
  944. }
  945. // Rule N0 - process bracket pairs
  946. if (_hasBrackets)
  947. {
  948. int count;
  949. var pairedBrackets = LocatePairedBrackets();
  950. for (i = 0, count = pairedBrackets.Count; i < count; i++)
  951. {
  952. var pairedBracket = pairedBrackets[i];
  953. var strongDirection = InspectPairedBracket(pairedBracket);
  954. // Case "d" - no strong types in the brackets, ignore
  955. if (strongDirection == BidiClass.OtherNeutral)
  956. {
  957. continue;
  958. }
  959. // Case "b" - strong type found that matches the embedding direction
  960. if ((strongDirection == BidiClass.LeftToRight || strongDirection == BidiClass.RightToLeft) && strongDirection == _runDirection)
  961. {
  962. SetPairedBracketDirection(pairedBracket, strongDirection);
  963. continue;
  964. }
  965. // Case "c" - found opposite strong type found, look before to establish context
  966. strongDirection = InspectBeforePairedBracket(pairedBracket, sos);
  967. if (strongDirection == _runDirection || strongDirection == BidiClass.OtherNeutral)
  968. {
  969. strongDirection = _runDirection;
  970. }
  971. SetPairedBracketDirection(pairedBracket, strongDirection);
  972. }
  973. }
  974. // Rules N1 and N2 - resolve neutral types
  975. for (i = 0; i < _runLength; ++i)
  976. {
  977. var resolvedClass = _runResolvedClasses[i];
  978. if (IsNeutralClass(resolvedClass))
  979. {
  980. // Locate end of sequence
  981. var seqStart = i;
  982. var seqEnd = i;
  983. while (seqEnd < _runLength && IsNeutralClass(_runResolvedClasses[seqEnd]))
  984. {
  985. seqEnd++;
  986. }
  987. // Work out the preceding class
  988. BidiClass classBefore;
  989. if (seqStart == 0)
  990. {
  991. classBefore = sos;
  992. }
  993. else
  994. {
  995. classBefore = _runResolvedClasses[seqStart - 1];
  996. switch (classBefore)
  997. {
  998. case BidiClass.ArabicNumber:
  999. case BidiClass.EuropeanNumber:
  1000. {
  1001. classBefore = BidiClass.RightToLeft;
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. // Work out the following class
  1007. BidiClass classAfter;
  1008. if (seqEnd == _runLength)
  1009. {
  1010. classAfter = eos;
  1011. }
  1012. else
  1013. {
  1014. classAfter = _runResolvedClasses[seqEnd];
  1015. switch (classAfter)
  1016. {
  1017. case BidiClass.ArabicNumber:
  1018. case BidiClass.EuropeanNumber:
  1019. {
  1020. classAfter = BidiClass.RightToLeft;
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. // Work out the final resolved type
  1026. BidiClass finalResolveClass;
  1027. if (classBefore == classAfter)
  1028. {
  1029. // Rule N1
  1030. finalResolveClass = classBefore;
  1031. }
  1032. else
  1033. {
  1034. // Rule N2
  1035. finalResolveClass = _runDirection;
  1036. }
  1037. // Apply changes
  1038. for (var j = seqStart; j < seqEnd; j++)
  1039. {
  1040. _runResolvedClasses[j] = finalResolveClass;
  1041. }
  1042. // continue after this run
  1043. i = seqEnd;
  1044. }
  1045. }
  1046. // Rules I1 and I2 - resolve implicit types
  1047. if ((_runLevel & 0x01) == 0)
  1048. {
  1049. // Rule I1 - even
  1050. for (i = 0; i < _runLength; i++)
  1051. {
  1052. var resolvedClass = _runResolvedClasses[i];
  1053. ref var currentRunLevel = ref _runLevels[i];
  1054. switch (resolvedClass)
  1055. {
  1056. case BidiClass.RightToLeft:
  1057. {
  1058. currentRunLevel++;
  1059. break;
  1060. }
  1061. case BidiClass.ArabicNumber:
  1062. case BidiClass.EuropeanNumber:
  1063. {
  1064. currentRunLevel += 2;
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. else
  1071. {
  1072. // Rule I2 - odd
  1073. for (i = 0; i < _runLength; i++)
  1074. {
  1075. var resolvedClass = _runResolvedClasses[i];
  1076. ref var currentRunLevel = ref _runLevels[i];
  1077. if (resolvedClass != BidiClass.RightToLeft)
  1078. {
  1079. currentRunLevel++;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. /// <summary>
  1085. /// Locate all pair brackets in the current isolating run
  1086. /// </summary>
  1087. /// <returns>A sorted list of BracketPairs</returns>
  1088. private List<BracketPair> LocatePairedBrackets()
  1089. {
  1090. // Clear work collections
  1091. _pendingOpeningBrackets.Clear();
  1092. _pairedBrackets.Clear();
  1093. // Since List.Sort is expensive on memory if called often (it internally
  1094. // allocates an ArraySorted object) and since we will rarely have many
  1095. // items in this list (most paragraphs will only have a handful of bracket
  1096. // pairs - if that), we use a simple linear lookup and insert most of the
  1097. // time. If there are more that `sortLimit` paired brackets we abort th
  1098. // linear searching/inserting and using List.Sort at the end.
  1099. const int sortLimit = 8;
  1100. // Process all characters in the run, looking for paired brackets
  1101. for (int i = 0, length = _runLength; i < length; i++)
  1102. {
  1103. // Ignore non-neutral characters
  1104. if (_runResolvedClasses[i] != BidiClass.OtherNeutral)
  1105. {
  1106. continue;
  1107. }
  1108. switch (_runBiDiPairedBracketTypes[i])
  1109. {
  1110. case BidiPairedBracketType.Open:
  1111. if (_pendingOpeningBrackets.Count == MaxPairedBracketDepth)
  1112. {
  1113. goto exit;
  1114. }
  1115. _pendingOpeningBrackets.Insert(0, i);
  1116. break;
  1117. case BidiPairedBracketType.Close:
  1118. // see if there is a match
  1119. for (var j = 0; j < _pendingOpeningBrackets.Count; j++)
  1120. {
  1121. if (_runPairedBracketValues[i] != _runPairedBracketValues[_pendingOpeningBrackets[j]])
  1122. {
  1123. continue;
  1124. }
  1125. // Add this paired bracket set
  1126. var opener = _pendingOpeningBrackets[j];
  1127. if (_pairedBrackets.Count < sortLimit)
  1128. {
  1129. var ppi = 0;
  1130. while (ppi < _pairedBrackets.Count && _pairedBrackets[ppi].OpeningIndex < opener)
  1131. {
  1132. ppi++;
  1133. }
  1134. _pairedBrackets.Insert(ppi, new BracketPair(opener, i));
  1135. }
  1136. else
  1137. {
  1138. _pairedBrackets.Add(new BracketPair(opener, i));
  1139. }
  1140. // remove up to and including matched opener
  1141. _pendingOpeningBrackets.RemoveRange(0, j + 1);
  1142. break;
  1143. }
  1144. break;
  1145. }
  1146. }
  1147. exit:
  1148. // Is a sort pending?
  1149. if (_pairedBrackets.Count > sortLimit)
  1150. {
  1151. _pairedBrackets.Sort();
  1152. }
  1153. return _pairedBrackets;
  1154. }
  1155. /// <summary>
  1156. /// Inspect a paired bracket set and determine its strong direction
  1157. /// </summary>
  1158. /// <param name="bracketPair">The paired bracket to be inspected</param>
  1159. /// <returns>The direction of the bracket set content</returns>
  1160. private BidiClass InspectPairedBracket(in BracketPair bracketPair)
  1161. {
  1162. var directionFromLevel = DirectionFromLevel(_runLevel);
  1163. var directionOpposite = BidiClass.OtherNeutral;
  1164. for (var i = bracketPair.OpeningIndex + 1; i < bracketPair.ClosingIndex; i++)
  1165. {
  1166. var dir = GetStrongClassN0(_runResolvedClasses[i]);
  1167. if (dir == BidiClass.OtherNeutral)
  1168. {
  1169. continue;
  1170. }
  1171. if (dir == directionFromLevel)
  1172. {
  1173. return dir;
  1174. }
  1175. directionOpposite = dir;
  1176. }
  1177. return directionOpposite;
  1178. }
  1179. /// <summary>
  1180. /// Look for a strong type before a paired bracket
  1181. /// </summary>
  1182. /// <param name="bracketPair">The paired bracket set to be inspected</param>
  1183. /// <param name="sos">The sos in case nothing found before the bracket</param>
  1184. /// <returns>The strong direction before the brackets</returns>
  1185. private BidiClass InspectBeforePairedBracket(in BracketPair bracketPair, BidiClass sos)
  1186. {
  1187. for (var i = bracketPair.OpeningIndex - 1; i >= 0; --i)
  1188. {
  1189. var direction = GetStrongClassN0(_runResolvedClasses[i]);
  1190. if (direction != BidiClass.OtherNeutral)
  1191. {
  1192. return direction;
  1193. }
  1194. }
  1195. return sos;
  1196. }
  1197. /// <summary>
  1198. /// Sets the direction of a bracket pair, including setting the direction of
  1199. /// NSM's inside the brackets and following.
  1200. /// </summary>
  1201. /// <param name="pairedBracket">The paired brackets</param>
  1202. /// <param name="direction">The resolved direction for the bracket pair</param>
  1203. private void SetPairedBracketDirection(in BracketPair pairedBracket, BidiClass direction)
  1204. {
  1205. // Set the direction of the brackets
  1206. _runResolvedClasses[pairedBracket.OpeningIndex] = direction;
  1207. _runResolvedClasses[pairedBracket.ClosingIndex] = direction;
  1208. // Set the directionality of NSM's inside the brackets
  1209. // BN characters (such as ZWJ or ZWSP) that appear between the base bracket character
  1210. // and the nonspacing mark should be ignored.
  1211. for (int i = pairedBracket.OpeningIndex + 1; i < pairedBracket.ClosingIndex; i++)
  1212. {
  1213. if (_runOriginalClasses[i] == BidiClass.NonspacingMark)
  1214. {
  1215. _runOriginalClasses[i] = direction;
  1216. }
  1217. else if (_runOriginalClasses[i] != BidiClass.BoundaryNeutral)
  1218. {
  1219. break;
  1220. }
  1221. }
  1222. // Set the directionality of NSM's following the brackets
  1223. for (int i = pairedBracket.ClosingIndex + 1; i < _runLength; i++)
  1224. {
  1225. if (_runOriginalClasses[i] == BidiClass.NonspacingMark)
  1226. {
  1227. _runOriginalClasses[i] = direction;
  1228. }
  1229. else if (_runOriginalClasses[i] != BidiClass.BoundaryNeutral)
  1230. {
  1231. break;
  1232. }
  1233. }
  1234. }
  1235. /// <summary>
  1236. /// Resets whitespace levels. Implements rule L1
  1237. /// </summary>
  1238. private void ResetWhitespaceLevels()
  1239. {
  1240. for (var i = 0; i < _resolvedLevels.Length; i++)
  1241. {
  1242. var originalClass = _originalClasses[i];
  1243. switch (originalClass)
  1244. {
  1245. case BidiClass.ParagraphSeparator:
  1246. case BidiClass.SegmentSeparator:
  1247. {
  1248. // Rule L1, clauses one and two.
  1249. _resolvedLevels[i] = _paragraphEmbeddingLevel;
  1250. // Rule L1, clause three.
  1251. for (var j = i - 1; j >= 0; --j)
  1252. {
  1253. if (IsWhitespace(_originalClasses[j]))
  1254. {
  1255. // including format codes
  1256. _resolvedLevels[j] = _paragraphEmbeddingLevel;
  1257. }
  1258. else
  1259. {
  1260. break;
  1261. }
  1262. }
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. // Rule L1, clause four.
  1268. for (var j = _resolvedLevels.Length - 1; j >= 0; j--)
  1269. {
  1270. if (IsWhitespace(_originalClasses[j]))
  1271. { // including format codes
  1272. _resolvedLevels[j] = _paragraphEmbeddingLevel;
  1273. }
  1274. else
  1275. {
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. /// <summary>
  1281. /// Assign levels to any characters that would be have been
  1282. /// removed by rule X9. The idea is to keep level runs together
  1283. /// that would otherwise be broken by an interfering isolate/embedding
  1284. /// control character.
  1285. /// </summary>
  1286. private void AssignLevelsToCodePointsRemovedByX9()
  1287. {
  1288. // Redundant?
  1289. if (!_hasIsolates && !_hasEmbeddings)
  1290. {
  1291. return;
  1292. }
  1293. // No-op?
  1294. if (_workingClasses.Length == 0)
  1295. {
  1296. return;
  1297. }
  1298. // Fix up first character
  1299. if (_resolvedLevels[0] < 0)
  1300. {
  1301. _resolvedLevels[0] = _paragraphEmbeddingLevel;
  1302. }
  1303. if (IsRemovedByX9(_originalClasses[0]))
  1304. {
  1305. _workingClasses[0] = _originalClasses[0];
  1306. }
  1307. for (int i = 1, length = _workingClasses.Length; i < length; i++)
  1308. {
  1309. var originalClass = _originalClasses[i];
  1310. if (IsRemovedByX9(originalClass))
  1311. {
  1312. _workingClasses[i] = originalClass;
  1313. _resolvedLevels[i] = _resolvedLevels[i - 1];
  1314. }
  1315. }
  1316. }
  1317. /// <summary>
  1318. /// Check if a directionality type represents whitespace
  1319. /// </summary>
  1320. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1321. private static bool IsWhitespace(BidiClass biDiClass)
  1322. {
  1323. switch (biDiClass)
  1324. {
  1325. case BidiClass.LeftToRightEmbedding:
  1326. case BidiClass.RightToLeftEmbedding:
  1327. case BidiClass.LeftToRightOverride:
  1328. case BidiClass.RightToLeftOverride:
  1329. case BidiClass.PopDirectionalFormat:
  1330. case BidiClass.LeftToRightIsolate:
  1331. case BidiClass.RightToLeftIsolate:
  1332. case BidiClass.FirstStrongIsolate:
  1333. case BidiClass.PopDirectionalIsolate:
  1334. case BidiClass.BoundaryNeutral:
  1335. case BidiClass.WhiteSpace:
  1336. return true;
  1337. default:
  1338. return false;
  1339. }
  1340. }
  1341. /// <summary>
  1342. /// Convert a level to a direction where odd is RTL and
  1343. /// even is LTR
  1344. /// </summary>
  1345. /// <param name="level">The level to convert</param>
  1346. /// <returns>A directionality</returns>
  1347. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1348. private static BidiClass DirectionFromLevel(int level)
  1349. => ((level & 0x1) == 0) ? BidiClass.LeftToRight : BidiClass.RightToLeft;
  1350. /// <summary>
  1351. /// Helper to check if a directionality is removed by rule X9
  1352. /// </summary>
  1353. /// <param name="biDiClass">The bidi type to check</param>
  1354. /// <returns>True if rule X9 would remove this character; otherwise false</returns>
  1355. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1356. private static bool IsRemovedByX9(BidiClass biDiClass)
  1357. {
  1358. switch (biDiClass)
  1359. {
  1360. case BidiClass.LeftToRightEmbedding:
  1361. case BidiClass.RightToLeftEmbedding:
  1362. case BidiClass.LeftToRightOverride:
  1363. case BidiClass.RightToLeftOverride:
  1364. case BidiClass.PopDirectionalFormat:
  1365. case BidiClass.BoundaryNeutral:
  1366. return true;
  1367. default:
  1368. return false;
  1369. }
  1370. }
  1371. /// <summary>
  1372. /// Check if a a directionality is neutral for rules N1 and N2
  1373. /// </summary>
  1374. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1375. private static bool IsNeutralClass(BidiClass direction)
  1376. {
  1377. switch (direction)
  1378. {
  1379. case BidiClass.ParagraphSeparator:
  1380. case BidiClass.SegmentSeparator:
  1381. case BidiClass.WhiteSpace:
  1382. case BidiClass.OtherNeutral:
  1383. case BidiClass.RightToLeftIsolate:
  1384. case BidiClass.LeftToRightIsolate:
  1385. case BidiClass.FirstStrongIsolate:
  1386. case BidiClass.PopDirectionalIsolate:
  1387. return true;
  1388. default:
  1389. return false;
  1390. }
  1391. }
  1392. /// <summary>
  1393. /// Maps a direction to a strong class for rule N0
  1394. /// </summary>
  1395. /// <param name="direction">The direction to map</param>
  1396. /// <returns>A strong direction - R, L or ON</returns>
  1397. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1398. private static BidiClass GetStrongClassN0(BidiClass direction)
  1399. {
  1400. switch (direction)
  1401. {
  1402. case BidiClass.EuropeanNumber:
  1403. case BidiClass.ArabicNumber:
  1404. case BidiClass.ArabicLetter:
  1405. case BidiClass.RightToLeft:
  1406. return BidiClass.RightToLeft;
  1407. case BidiClass.LeftToRight:
  1408. return BidiClass.LeftToRight;
  1409. default:
  1410. return BidiClass.OtherNeutral;
  1411. }
  1412. }
  1413. /// <summary>
  1414. /// Hold the start and end index of a pair of brackets
  1415. /// </summary>
  1416. private readonly struct BracketPair : IComparable<BracketPair>
  1417. {
  1418. /// <summary>
  1419. /// Initializes a new instance of the <see cref="BracketPair"/> struct.
  1420. /// </summary>
  1421. /// <param name="openingIndex">Index of the opening bracket</param>
  1422. /// <param name="closingIndex">Index of the closing bracket</param>
  1423. public BracketPair(int openingIndex, int closingIndex)
  1424. {
  1425. OpeningIndex = openingIndex;
  1426. ClosingIndex = closingIndex;
  1427. }
  1428. /// <summary>
  1429. /// Gets the index of the opening bracket
  1430. /// </summary>
  1431. public int OpeningIndex { get; }
  1432. /// <summary>
  1433. /// Gets the index of the closing bracket
  1434. /// </summary>
  1435. public int ClosingIndex { get; }
  1436. public int CompareTo(BracketPair other)
  1437. => OpeningIndex.CompareTo(other.OpeningIndex);
  1438. }
  1439. /// <summary>
  1440. /// Status stack entry used while resolving explicit
  1441. /// embedding levels
  1442. /// </summary>
  1443. private readonly struct Status
  1444. {
  1445. public Status(sbyte embeddingLevel, BidiClass overrideStatus, bool isolateStatus)
  1446. {
  1447. EmbeddingLevel = embeddingLevel;
  1448. OverrideStatus = overrideStatus;
  1449. IsolateStatus = isolateStatus;
  1450. }
  1451. public sbyte EmbeddingLevel { get; }
  1452. public BidiClass OverrideStatus { get; }
  1453. public bool IsolateStatus { get; }
  1454. }
  1455. /// <summary>
  1456. /// Provides information about a level run - a continuous
  1457. /// sequence of equal levels.
  1458. /// </summary>
  1459. private readonly struct LevelRun
  1460. {
  1461. public LevelRun(int start, int length, int level, BidiClass sos, BidiClass eos)
  1462. {
  1463. Start = start;
  1464. Length = length;
  1465. Level = level;
  1466. Sos = sos;
  1467. Eos = eos;
  1468. }
  1469. public int Start { get; }
  1470. public int Length { get; }
  1471. public int Level { get; }
  1472. public BidiClass Sos { get; }
  1473. public BidiClass Eos { get; }
  1474. }
  1475. }
  1476. }