TextLineImpl.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Utilities;
  4. namespace Avalonia.Media.TextFormatting
  5. {
  6. internal class TextLineImpl : TextLine
  7. {
  8. private readonly List<DrawableTextRun> _textRuns;
  9. private readonly double _paragraphWidth;
  10. private readonly TextParagraphProperties _paragraphProperties;
  11. private TextLineMetrics _textLineMetrics;
  12. private readonly FlowDirection _resolvedFlowDirection;
  13. public TextLineImpl(List<DrawableTextRun> textRuns, int firstTextSourceIndex, int length, double paragraphWidth,
  14. TextParagraphProperties paragraphProperties, FlowDirection resolvedFlowDirection = FlowDirection.LeftToRight,
  15. TextLineBreak? lineBreak = null, bool hasCollapsed = false)
  16. {
  17. FirstTextSourceIndex = firstTextSourceIndex;
  18. Length = length;
  19. TextLineBreak = lineBreak;
  20. HasCollapsed = hasCollapsed;
  21. _textRuns = textRuns;
  22. _paragraphWidth = paragraphWidth;
  23. _paragraphProperties = paragraphProperties;
  24. _resolvedFlowDirection = resolvedFlowDirection;
  25. }
  26. /// <inheritdoc/>
  27. public override IReadOnlyList<TextRun> TextRuns => _textRuns;
  28. /// <inheritdoc/>
  29. public override int FirstTextSourceIndex { get; }
  30. /// <inheritdoc/>
  31. public override int Length { get; }
  32. /// <inheritdoc/>
  33. public override TextLineBreak? TextLineBreak { get; }
  34. /// <inheritdoc/>
  35. public override bool HasCollapsed { get; }
  36. /// <inheritdoc/>
  37. public override bool HasOverflowed => _textLineMetrics.HasOverflowed;
  38. /// <inheritdoc/>
  39. public override double Baseline => _textLineMetrics.TextBaseline;
  40. /// <inheritdoc/>
  41. public override double Extent => _textLineMetrics.Height;
  42. /// <inheritdoc/>
  43. public override double Height => _textLineMetrics.Height;
  44. /// <inheritdoc/>
  45. public override int NewLineLength => _textLineMetrics.NewlineLength;
  46. /// <inheritdoc/>
  47. public override double OverhangAfter => 0;
  48. /// <inheritdoc/>
  49. public override double OverhangLeading => 0;
  50. /// <inheritdoc/>
  51. public override double OverhangTrailing => 0;
  52. /// <inheritdoc/>
  53. public override int TrailingWhitespaceLength => _textLineMetrics.TrailingWhitespaceLength;
  54. /// <inheritdoc/>
  55. public override double Start => _textLineMetrics.Start;
  56. /// <inheritdoc/>
  57. public override double Width => _textLineMetrics.Width;
  58. /// <inheritdoc/>
  59. public override double WidthIncludingTrailingWhitespace => _textLineMetrics.WidthIncludingTrailingWhitespace;
  60. /// <inheritdoc/>
  61. public override void Draw(DrawingContext drawingContext, Point lineOrigin)
  62. {
  63. var (currentX, currentY) = lineOrigin;
  64. foreach (var textRun in _textRuns)
  65. {
  66. var offsetY = GetBaselineOffset(this, textRun);
  67. textRun.Draw(drawingContext, new Point(currentX, currentY + offsetY));
  68. currentX += textRun.Size.Width;
  69. }
  70. }
  71. private static double GetBaselineOffset(TextLine textLine, DrawableTextRun textRun)
  72. {
  73. var baseline = textRun.Baseline;
  74. var baselineAlignment = textRun.Properties?.BaselineAlignment;
  75. switch (baselineAlignment)
  76. {
  77. case BaselineAlignment.Top:
  78. return 0;
  79. case BaselineAlignment.Center:
  80. return textLine.Height / 2 - textRun.Size.Height / 2;
  81. case BaselineAlignment.Bottom:
  82. return textLine.Height - textRun.Size.Height;
  83. case BaselineAlignment.Baseline:
  84. case BaselineAlignment.TextTop:
  85. case BaselineAlignment.TextBottom:
  86. case BaselineAlignment.Subscript:
  87. case BaselineAlignment.Superscript:
  88. return textLine.Baseline - baseline;
  89. default:
  90. throw new ArgumentOutOfRangeException(nameof(baselineAlignment), baselineAlignment, null);
  91. }
  92. }
  93. /// <inheritdoc/>
  94. public override TextLine Collapse(params TextCollapsingProperties?[] collapsingPropertiesList)
  95. {
  96. if (collapsingPropertiesList.Length == 0)
  97. {
  98. return this;
  99. }
  100. var collapsingProperties = collapsingPropertiesList[0];
  101. if (collapsingProperties is null)
  102. {
  103. return this;
  104. }
  105. var collapsedRuns = collapsingProperties.Collapse(this);
  106. if (collapsedRuns is null)
  107. {
  108. return this;
  109. }
  110. var collapsedLine = new TextLineImpl(collapsedRuns, FirstTextSourceIndex, Length, _paragraphWidth, _paragraphProperties,
  111. _resolvedFlowDirection, TextLineBreak, true);
  112. if (collapsedRuns.Count > 0)
  113. {
  114. collapsedLine.FinalizeLine();
  115. }
  116. return collapsedLine;
  117. }
  118. /// <inheritdoc/>
  119. public override void Justify(JustificationProperties justificationProperties)
  120. {
  121. justificationProperties.Justify(this);
  122. _textLineMetrics = CreateLineMetrics();
  123. }
  124. /// <inheritdoc/>
  125. public override CharacterHit GetCharacterHitFromDistance(double distance)
  126. {
  127. if (_textRuns.Count == 0)
  128. {
  129. return new CharacterHit();
  130. }
  131. distance -= Start;
  132. if (distance <= 0)
  133. {
  134. var firstRun = _textRuns[0];
  135. return GetRunCharacterHit(firstRun, FirstTextSourceIndex, 0);
  136. }
  137. if (distance >= WidthIncludingTrailingWhitespace)
  138. {
  139. var lastRun = _textRuns[_textRuns.Count - 1];
  140. return GetRunCharacterHit(lastRun, FirstTextSourceIndex + Length - lastRun.Length, lastRun.Size.Width);
  141. }
  142. // process hit that happens within the line
  143. var characterHit = new CharacterHit();
  144. var currentPosition = FirstTextSourceIndex;
  145. var currentDistance = 0.0;
  146. for (var i = 0; i < _textRuns.Count; i++)
  147. {
  148. var currentRun = _textRuns[i];
  149. if (currentRun is ShapedTextRun shapedRun && !shapedRun.ShapedBuffer.IsLeftToRight)
  150. {
  151. var rightToLeftIndex = i;
  152. currentPosition += currentRun.Length;
  153. while (rightToLeftIndex + 1 <= _textRuns.Count - 1)
  154. {
  155. var nextShaped = _textRuns[++rightToLeftIndex] as ShapedTextRun;
  156. if (nextShaped == null || nextShaped.ShapedBuffer.IsLeftToRight)
  157. {
  158. break;
  159. }
  160. currentPosition += nextShaped.Length;
  161. rightToLeftIndex++;
  162. }
  163. for (var j = i; i <= rightToLeftIndex; j++)
  164. {
  165. if (j > _textRuns.Count - 1)
  166. {
  167. break;
  168. }
  169. currentRun = _textRuns[j];
  170. if (currentDistance + currentRun.Size.Width <= distance)
  171. {
  172. currentDistance += currentRun.Size.Width;
  173. currentPosition -= currentRun.Length;
  174. continue;
  175. }
  176. return GetRunCharacterHit(currentRun, currentPosition, distance - currentDistance);
  177. }
  178. }
  179. characterHit = GetRunCharacterHit(currentRun, currentPosition, distance - currentDistance);
  180. if (i < _textRuns.Count - 1 && currentDistance + currentRun.Size.Width < distance)
  181. {
  182. currentDistance += currentRun.Size.Width;
  183. currentPosition += currentRun.Length;
  184. continue;
  185. }
  186. break;
  187. }
  188. return characterHit;
  189. }
  190. private static CharacterHit GetRunCharacterHit(DrawableTextRun run, int currentPosition, double distance)
  191. {
  192. CharacterHit characterHit;
  193. switch (run)
  194. {
  195. case ShapedTextRun shapedRun:
  196. {
  197. characterHit = shapedRun.GlyphRun.GetCharacterHitFromDistance(distance, out _);
  198. var offset = 0;
  199. if (shapedRun.GlyphRun.IsLeftToRight)
  200. {
  201. offset = Math.Max(0, currentPosition - shapedRun.GlyphRun.Metrics.FirstCluster);
  202. }
  203. characterHit = new CharacterHit(offset + characterHit.FirstCharacterIndex, characterHit.TrailingLength);
  204. break;
  205. }
  206. default:
  207. {
  208. if (distance < run.Size.Width / 2)
  209. {
  210. characterHit = new CharacterHit(currentPosition);
  211. }
  212. else
  213. {
  214. characterHit = new CharacterHit(currentPosition, run.Length);
  215. }
  216. break;
  217. }
  218. }
  219. return characterHit;
  220. }
  221. /// <inheritdoc/>
  222. public override double GetDistanceFromCharacterHit(CharacterHit characterHit)
  223. {
  224. var flowDirection = _paragraphProperties.FlowDirection;
  225. var characterIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
  226. var currentPosition = FirstTextSourceIndex;
  227. var remainingLength = characterIndex - FirstTextSourceIndex;
  228. var currentDistance = Start;
  229. if (flowDirection == FlowDirection.LeftToRight)
  230. {
  231. for (var index = 0; index < _textRuns.Count; index++)
  232. {
  233. var currentRun = _textRuns[index];
  234. if (currentRun is ShapedTextRun shapedRun && !shapedRun.ShapedBuffer.IsLeftToRight)
  235. {
  236. var i = index;
  237. var rightToLeftWidth = currentRun.Size.Width;
  238. while (i + 1 <= _textRuns.Count - 1)
  239. {
  240. var nextRun = _textRuns[i + 1];
  241. if (nextRun is ShapedTextRun nextShapedRun && !nextShapedRun.ShapedBuffer.IsLeftToRight)
  242. {
  243. i++;
  244. rightToLeftWidth += nextRun.Size.Width;
  245. continue;
  246. }
  247. break;
  248. }
  249. if (i > index)
  250. {
  251. while (i >= index)
  252. {
  253. currentRun = _textRuns[i];
  254. rightToLeftWidth -= currentRun.Size.Width;
  255. if (currentPosition + currentRun.Length >= characterIndex)
  256. {
  257. break;
  258. }
  259. currentPosition += currentRun.Length;
  260. remainingLength -= currentRun.Length;
  261. i--;
  262. }
  263. currentDistance += rightToLeftWidth;
  264. }
  265. }
  266. if (currentPosition + currentRun.Length >= characterIndex &&
  267. TryGetDistanceFromCharacterHit(currentRun, characterHit, currentPosition, remainingLength, flowDirection, out var distance, out _))
  268. {
  269. return Math.Max(0, currentDistance + distance);
  270. }
  271. //No hit hit found so we add the full width
  272. currentDistance += currentRun.Size.Width;
  273. currentPosition += currentRun.Length;
  274. remainingLength -= currentRun.Length;
  275. }
  276. }
  277. else
  278. {
  279. currentDistance += WidthIncludingTrailingWhitespace;
  280. for (var index = _textRuns.Count - 1; index >= 0; index--)
  281. {
  282. var currentRun = _textRuns[index];
  283. if (TryGetDistanceFromCharacterHit(currentRun, characterHit, currentPosition, remainingLength,
  284. flowDirection, out var distance, out var currentGlyphRun))
  285. {
  286. if (currentGlyphRun != null)
  287. {
  288. distance = currentGlyphRun.Size.Width - distance;
  289. }
  290. return Math.Max(0, currentDistance - distance);
  291. }
  292. //No hit hit found so we add the full width
  293. currentDistance -= currentRun.Size.Width;
  294. currentPosition += currentRun.Length;
  295. remainingLength -= currentRun.Length;
  296. }
  297. }
  298. return Math.Max(0, currentDistance);
  299. }
  300. private static bool TryGetDistanceFromCharacterHit(
  301. DrawableTextRun currentRun,
  302. CharacterHit characterHit,
  303. int currentPosition,
  304. int remainingLength,
  305. FlowDirection flowDirection,
  306. out double distance,
  307. out GlyphRun? currentGlyphRun)
  308. {
  309. var characterIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
  310. var isTrailingHit = characterHit.TrailingLength > 0;
  311. distance = 0;
  312. currentGlyphRun = null;
  313. switch (currentRun)
  314. {
  315. case ShapedTextRun shapedTextCharacters:
  316. {
  317. currentGlyphRun = shapedTextCharacters.GlyphRun;
  318. if (currentPosition + remainingLength <= currentPosition + currentRun.Length)
  319. {
  320. characterHit = new CharacterHit(currentPosition + remainingLength);
  321. distance = currentGlyphRun.GetDistanceFromCharacterHit(characterHit);
  322. return true;
  323. }
  324. if (currentPosition + remainingLength == currentPosition + currentRun.Length && isTrailingHit)
  325. {
  326. if (currentGlyphRun.IsLeftToRight || flowDirection == FlowDirection.RightToLeft)
  327. {
  328. distance = currentGlyphRun.Size.Width;
  329. }
  330. return true;
  331. }
  332. break;
  333. }
  334. default:
  335. {
  336. if (characterIndex == currentPosition)
  337. {
  338. return true;
  339. }
  340. if (characterIndex == currentPosition + currentRun.Length)
  341. {
  342. distance = currentRun.Size.Width;
  343. return true;
  344. }
  345. break;
  346. }
  347. }
  348. return false;
  349. }
  350. /// <inheritdoc/>
  351. public override CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit)
  352. {
  353. if (_textRuns.Count == 0)
  354. {
  355. return new CharacterHit();
  356. }
  357. if (TryFindNextCharacterHit(characterHit, out var nextCharacterHit))
  358. {
  359. return nextCharacterHit;
  360. }
  361. var lastTextPosition = FirstTextSourceIndex + Length;
  362. // Can't move, we're after the last character
  363. var runIndex = GetRunIndexAtCharacterIndex(lastTextPosition, LogicalDirection.Forward, out var currentPosition);
  364. var currentRun = _textRuns[runIndex];
  365. switch (currentRun)
  366. {
  367. case ShapedTextRun shapedRun:
  368. {
  369. nextCharacterHit = shapedRun.GlyphRun.GetNextCaretCharacterHit(characterHit);
  370. break;
  371. }
  372. default:
  373. {
  374. nextCharacterHit = new CharacterHit(currentPosition + currentRun.Length);
  375. break;
  376. }
  377. }
  378. if (characterHit.FirstCharacterIndex + characterHit.TrailingLength == nextCharacterHit.FirstCharacterIndex + nextCharacterHit.TrailingLength)
  379. {
  380. return characterHit;
  381. }
  382. return nextCharacterHit;
  383. }
  384. /// <inheritdoc/>
  385. public override CharacterHit GetPreviousCaretCharacterHit(CharacterHit characterHit)
  386. {
  387. if (TryFindPreviousCharacterHit(characterHit, out var previousCharacterHit))
  388. {
  389. return previousCharacterHit;
  390. }
  391. if (characterHit.FirstCharacterIndex <= FirstTextSourceIndex)
  392. {
  393. characterHit = new CharacterHit(FirstTextSourceIndex);
  394. }
  395. return characterHit; // Can't move, we're before the first character
  396. }
  397. /// <inheritdoc/>
  398. public override CharacterHit GetBackspaceCaretCharacterHit(CharacterHit characterHit)
  399. {
  400. // same operation as move-to-previous
  401. return GetPreviousCaretCharacterHit(characterHit);
  402. }
  403. private IReadOnlyList<TextBounds> GetTextBoundsLeftToRight(int firstTextSourceIndex, int textLength)
  404. {
  405. var characterIndex = firstTextSourceIndex + textLength;
  406. var result = new List<TextBounds>(TextRuns.Count);
  407. var lastDirection = FlowDirection.LeftToRight;
  408. var currentDirection = lastDirection;
  409. var currentPosition = FirstTextSourceIndex;
  410. var remainingLength = textLength;
  411. var startX = Start;
  412. double currentWidth = 0;
  413. var currentRect = default(Rect);
  414. TextRunBounds lastRunBounds = default;
  415. for (var index = 0; index < TextRuns.Count; index++)
  416. {
  417. if (TextRuns[index] is not DrawableTextRun currentRun)
  418. {
  419. continue;
  420. }
  421. var characterLength = 0;
  422. var endX = startX;
  423. TextRunBounds currentRunBounds;
  424. double combinedWidth;
  425. if (currentRun is ShapedTextRun currentShapedRun)
  426. {
  427. var firstCluster = currentShapedRun.GlyphRun.Metrics.FirstCluster;
  428. if (currentPosition + currentRun.Length <= firstTextSourceIndex)
  429. {
  430. startX += currentRun.Size.Width;
  431. currentPosition += currentRun.Length;
  432. continue;
  433. }
  434. if (currentShapedRun.ShapedBuffer.IsLeftToRight)
  435. {
  436. var startIndex = firstCluster + Math.Max(0, firstTextSourceIndex - currentPosition);
  437. double startOffset;
  438. double endOffset;
  439. startOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
  440. endOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
  441. startX += startOffset;
  442. endX += endOffset;
  443. var endHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(endOffset, out _);
  444. var startHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(startOffset, out _);
  445. characterLength = Math.Abs(endHit.FirstCharacterIndex + endHit.TrailingLength - startHit.FirstCharacterIndex - startHit.TrailingLength);
  446. currentDirection = FlowDirection.LeftToRight;
  447. }
  448. else
  449. {
  450. var rightToLeftIndex = index;
  451. var rightToLeftWidth = currentShapedRun.Size.Width;
  452. while (rightToLeftIndex + 1 <= _textRuns.Count - 1 && _textRuns[rightToLeftIndex + 1] is ShapedTextRun nextShapedRun)
  453. {
  454. if (nextShapedRun == null || nextShapedRun.ShapedBuffer.IsLeftToRight)
  455. {
  456. break;
  457. }
  458. rightToLeftIndex++;
  459. rightToLeftWidth += nextShapedRun.Size.Width;
  460. if (currentPosition + nextShapedRun.Length > firstTextSourceIndex + textLength)
  461. {
  462. break;
  463. }
  464. currentShapedRun = nextShapedRun;
  465. }
  466. startX += rightToLeftWidth;
  467. currentRunBounds = GetRightToLeftTextRunBounds(currentShapedRun, startX, firstTextSourceIndex, characterIndex, currentPosition, remainingLength);
  468. remainingLength -= currentRunBounds.Length;
  469. currentPosition = currentRunBounds.TextSourceCharacterIndex + currentRunBounds.Length;
  470. endX = currentRunBounds.Rectangle.Right;
  471. startX = currentRunBounds.Rectangle.Left;
  472. var rightToLeftRunBounds = new List<TextRunBounds> { currentRunBounds };
  473. for (int i = rightToLeftIndex - 1; i >= index; i--)
  474. {
  475. if (TextRuns[i] is not ShapedTextRun)
  476. {
  477. continue;
  478. }
  479. currentShapedRun = (ShapedTextRun)TextRuns[i];
  480. currentRunBounds = GetRightToLeftTextRunBounds(currentShapedRun, startX, firstTextSourceIndex, characterIndex, currentPosition, remainingLength);
  481. rightToLeftRunBounds.Insert(0, currentRunBounds);
  482. remainingLength -= currentRunBounds.Length;
  483. startX = currentRunBounds.Rectangle.Left;
  484. currentPosition += currentRunBounds.Length;
  485. }
  486. combinedWidth = endX - startX;
  487. currentRect = new Rect(startX, 0, combinedWidth, Height);
  488. currentDirection = FlowDirection.RightToLeft;
  489. if (!MathUtilities.IsZero(combinedWidth))
  490. {
  491. result.Add(new TextBounds(currentRect, currentDirection, rightToLeftRunBounds));
  492. }
  493. startX = endX;
  494. }
  495. }
  496. else
  497. {
  498. if (currentPosition + currentRun.Length <= firstTextSourceIndex)
  499. {
  500. startX += currentRun.Size.Width;
  501. currentPosition += currentRun.Length;
  502. continue;
  503. }
  504. if (currentPosition < firstTextSourceIndex)
  505. {
  506. startX += currentRun.Size.Width;
  507. }
  508. if (currentPosition + currentRun.Length <= characterIndex)
  509. {
  510. endX += currentRun.Size.Width;
  511. characterLength = currentRun.Length;
  512. }
  513. }
  514. if (endX < startX)
  515. {
  516. (endX, startX) = (startX, endX);
  517. }
  518. //Lines that only contain a linebreak need to be covered here
  519. if (characterLength == 0)
  520. {
  521. characterLength = NewLineLength;
  522. }
  523. combinedWidth = endX - startX;
  524. currentRunBounds = new TextRunBounds(new Rect(startX, 0, combinedWidth, Height), currentPosition, characterLength, currentRun);
  525. currentPosition += characterLength;
  526. remainingLength -= characterLength;
  527. startX = endX;
  528. if (currentRunBounds.TextRun != null && !MathUtilities.IsZero(combinedWidth) || NewLineLength > 0)
  529. {
  530. if (result.Count > 0 && lastDirection == currentDirection && MathUtilities.AreClose(currentRect.Left, lastRunBounds.Rectangle.Right))
  531. {
  532. currentRect = currentRect.WithWidth(currentWidth + combinedWidth);
  533. var textBounds = result[result.Count - 1];
  534. textBounds.Rectangle = currentRect;
  535. textBounds.TextRunBounds.Add(currentRunBounds);
  536. }
  537. else
  538. {
  539. currentRect = currentRunBounds.Rectangle;
  540. result.Add(new TextBounds(currentRect, currentDirection, new List<TextRunBounds> { currentRunBounds }));
  541. }
  542. }
  543. lastRunBounds = currentRunBounds;
  544. currentWidth += combinedWidth;
  545. if (remainingLength <= 0 || currentPosition >= characterIndex)
  546. {
  547. break;
  548. }
  549. lastDirection = currentDirection;
  550. }
  551. return result;
  552. }
  553. private IReadOnlyList<TextBounds> GetTextBoundsRightToLeft(int firstTextSourceIndex, int textLength)
  554. {
  555. var characterIndex = firstTextSourceIndex + textLength;
  556. var result = new List<TextBounds>(TextRuns.Count);
  557. var lastDirection = FlowDirection.LeftToRight;
  558. var currentDirection = lastDirection;
  559. var currentPosition = FirstTextSourceIndex;
  560. var remainingLength = textLength;
  561. var startX = WidthIncludingTrailingWhitespace;
  562. double currentWidth = 0;
  563. var currentRect = default(Rect);
  564. for (var index = TextRuns.Count - 1; index >= 0; index--)
  565. {
  566. if (TextRuns[index] is not DrawableTextRun currentRun)
  567. {
  568. continue;
  569. }
  570. if (currentPosition + currentRun.Length < firstTextSourceIndex)
  571. {
  572. startX -= currentRun.Size.Width;
  573. currentPosition += currentRun.Length;
  574. continue;
  575. }
  576. var characterLength = 0;
  577. var endX = startX;
  578. if (currentRun is ShapedTextRun currentShapedRun)
  579. {
  580. var offset = Math.Max(0, firstTextSourceIndex - currentPosition);
  581. currentPosition += offset;
  582. var startIndex = currentPosition;
  583. double startOffset;
  584. double endOffset;
  585. if (currentShapedRun.ShapedBuffer.IsLeftToRight)
  586. {
  587. if (currentPosition < startIndex)
  588. {
  589. startOffset = endOffset = 0;
  590. }
  591. else
  592. {
  593. endOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
  594. startOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
  595. }
  596. }
  597. else
  598. {
  599. endOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
  600. startOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
  601. }
  602. startX -= currentRun.Size.Width - startOffset;
  603. endX -= currentRun.Size.Width - endOffset;
  604. var endHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(endOffset, out _);
  605. var startHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(startOffset, out _);
  606. characterLength = Math.Abs(startHit.FirstCharacterIndex + startHit.TrailingLength - endHit.FirstCharacterIndex - endHit.TrailingLength);
  607. currentDirection = currentShapedRun.ShapedBuffer.IsLeftToRight ?
  608. FlowDirection.LeftToRight :
  609. FlowDirection.RightToLeft;
  610. }
  611. else
  612. {
  613. if (currentPosition + currentRun.Length <= characterIndex)
  614. {
  615. endX -= currentRun.Size.Width;
  616. }
  617. if (currentPosition < firstTextSourceIndex)
  618. {
  619. startX -= currentRun.Size.Width;
  620. characterLength = currentRun.Length;
  621. }
  622. }
  623. if (endX < startX)
  624. {
  625. (endX, startX) = (startX, endX);
  626. }
  627. //Lines that only contain a linebreak need to be covered here
  628. if (characterLength == 0)
  629. {
  630. characterLength = NewLineLength;
  631. }
  632. var runWidth = endX - startX;
  633. var currentRunBounds = new TextRunBounds(new Rect(Start + startX, 0, runWidth, Height), currentPosition, characterLength, currentRun);
  634. if (!MathUtilities.IsZero(runWidth) || NewLineLength > 0)
  635. {
  636. if (lastDirection == currentDirection && result.Count > 0 && MathUtilities.AreClose(currentRect.Right, Start + startX))
  637. {
  638. currentRect = currentRect.WithWidth(currentWidth + runWidth);
  639. var textBounds = result[result.Count - 1];
  640. textBounds.Rectangle = currentRect;
  641. textBounds.TextRunBounds.Add(currentRunBounds);
  642. }
  643. else
  644. {
  645. currentRect = currentRunBounds.Rectangle;
  646. result.Add(new TextBounds(currentRect, currentDirection, new List<TextRunBounds> { currentRunBounds }));
  647. }
  648. }
  649. currentWidth += runWidth;
  650. currentPosition += characterLength;
  651. if (currentPosition > characterIndex)
  652. {
  653. break;
  654. }
  655. lastDirection = currentDirection;
  656. remainingLength -= characterLength;
  657. if (remainingLength <= 0)
  658. {
  659. break;
  660. }
  661. }
  662. result.Reverse();
  663. return result;
  664. }
  665. private TextRunBounds GetRightToLeftTextRunBounds(ShapedTextRun currentRun, double endX, int firstTextSourceIndex, int characterIndex, int currentPosition, int remainingLength)
  666. {
  667. var startX = endX;
  668. var offset = Math.Max(0, firstTextSourceIndex - currentPosition);
  669. currentPosition += offset;
  670. var startIndex = currentPosition;
  671. double startOffset;
  672. double endOffset;
  673. endOffset = currentRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
  674. startOffset = currentRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
  675. startX -= currentRun.Size.Width - startOffset;
  676. endX -= currentRun.Size.Width - endOffset;
  677. var endHit = currentRun.GlyphRun.GetCharacterHitFromDistance(endOffset, out _);
  678. var startHit = currentRun.GlyphRun.GetCharacterHitFromDistance(startOffset, out _);
  679. var characterLength = Math.Abs(startHit.FirstCharacterIndex + startHit.TrailingLength - endHit.FirstCharacterIndex - endHit.TrailingLength);
  680. if (endX < startX)
  681. {
  682. (endX, startX) = (startX, endX);
  683. }
  684. //Lines that only contain a linebreak need to be covered here
  685. if (characterLength == 0)
  686. {
  687. characterLength = NewLineLength;
  688. }
  689. var runWidth = endX - startX;
  690. return new TextRunBounds(new Rect(Start + startX, 0, runWidth, Height), currentPosition, characterLength, currentRun);
  691. }
  692. public override IReadOnlyList<TextBounds> GetTextBounds(int firstTextSourceIndex, int textLength)
  693. {
  694. if (_paragraphProperties.FlowDirection == FlowDirection.LeftToRight)
  695. {
  696. return GetTextBoundsLeftToRight(firstTextSourceIndex, textLength);
  697. }
  698. return GetTextBoundsRightToLeft(firstTextSourceIndex, textLength);
  699. }
  700. public TextLineImpl FinalizeLine()
  701. {
  702. _textLineMetrics = CreateLineMetrics();
  703. BidiReorder();
  704. return this;
  705. }
  706. private static sbyte GetRunBidiLevel(DrawableTextRun run, FlowDirection flowDirection)
  707. {
  708. if (run is ShapedTextRun shapedTextCharacters)
  709. {
  710. return shapedTextCharacters.BidiLevel;
  711. }
  712. var defaultLevel = flowDirection == FlowDirection.LeftToRight ? 0 : 1;
  713. return (sbyte)defaultLevel;
  714. }
  715. private void BidiReorder()
  716. {
  717. if (_textRuns.Count == 0)
  718. {
  719. return;
  720. }
  721. // Build up the collection of ordered runs.
  722. var run = _textRuns[0];
  723. OrderedBidiRun orderedRun = new(run, GetRunBidiLevel(run, _resolvedFlowDirection));
  724. var current = orderedRun;
  725. for (var i = 1; i < _textRuns.Count; i++)
  726. {
  727. run = _textRuns[i];
  728. current.Next = new OrderedBidiRun(run, GetRunBidiLevel(run, _resolvedFlowDirection));
  729. current = current.Next;
  730. }
  731. // Reorder them into visual order.
  732. orderedRun = LinearReOrder(orderedRun);
  733. // Now perform a recursive reversal of each run.
  734. // From the highest level found in the text to the lowest odd level on each line, including intermediate levels
  735. // not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
  736. // https://unicode.org/reports/tr9/#L2
  737. sbyte max = 0;
  738. var min = sbyte.MaxValue;
  739. for (var i = 0; i < _textRuns.Count; i++)
  740. {
  741. var currentRun = _textRuns[i];
  742. var level = GetRunBidiLevel(currentRun, _resolvedFlowDirection);
  743. if (level > max)
  744. {
  745. max = level;
  746. }
  747. if ((level & 1) != 0 && level < min)
  748. {
  749. min = level;
  750. }
  751. }
  752. if (min > max)
  753. {
  754. min = max;
  755. }
  756. if (max == 0 || (min == max && (max & 1) == 0))
  757. {
  758. // Nothing to reverse.
  759. return;
  760. }
  761. // Now apply the reversal and replace the original contents.
  762. var minLevelToReverse = max;
  763. while (minLevelToReverse >= min)
  764. {
  765. current = orderedRun;
  766. while (current != null)
  767. {
  768. if (current.Level >= minLevelToReverse && current.Level % 2 != 0)
  769. {
  770. if (current.Run is ShapedTextRun { IsReversed: false } shapedTextCharacters)
  771. {
  772. shapedTextCharacters.Reverse();
  773. }
  774. }
  775. current = current.Next;
  776. }
  777. minLevelToReverse--;
  778. }
  779. _textRuns.Clear();
  780. current = orderedRun;
  781. while (current != null)
  782. {
  783. _textRuns.Add(current.Run);
  784. current = current.Next;
  785. }
  786. }
  787. /// <summary>
  788. /// Reorders a series of runs from logical to visual order, returning the left most run.
  789. /// <see href="https://github.com/fribidi/linear-reorder/blob/f2f872257d4d8b8e137fcf831f254d6d4db79d3c/linear-reorder.c"/>
  790. /// </summary>
  791. /// <param name="run">The ordered bidi run.</param>
  792. /// <returns>The <see cref="OrderedBidiRun"/>.</returns>
  793. private static OrderedBidiRun LinearReOrder(OrderedBidiRun? run)
  794. {
  795. BidiRange? range = null;
  796. while (run != null)
  797. {
  798. var next = run.Next;
  799. while (range != null && range.Level > run.Level
  800. && range.Previous != null && range.Previous.Level >= run.Level)
  801. {
  802. range = BidiRange.MergeWithPrevious(range);
  803. }
  804. if (range != null && range.Level >= run.Level)
  805. {
  806. // Attach run to the range.
  807. if ((run.Level & 1) != 0)
  808. {
  809. // Odd, range goes to the right of run.
  810. run.Next = range.Left;
  811. range.Left = run;
  812. }
  813. else
  814. {
  815. // Even, range goes to the left of run.
  816. range.Right!.Next = run;
  817. range.Right = run;
  818. }
  819. range.Level = run.Level;
  820. }
  821. else
  822. {
  823. var r = new BidiRange();
  824. r.Left = r.Right = run;
  825. r.Level = run.Level;
  826. r.Previous = range;
  827. range = r;
  828. }
  829. run = next;
  830. }
  831. while (range?.Previous != null)
  832. {
  833. range = BidiRange.MergeWithPrevious(range);
  834. }
  835. // Terminate.
  836. range!.Right!.Next = null;
  837. return range.Left!;
  838. }
  839. /// <summary>
  840. /// Tries to find the next character hit.
  841. /// </summary>
  842. /// <param name="characterHit">The current character hit.</param>
  843. /// <param name="nextCharacterHit">The next character hit.</param>
  844. /// <returns></returns>
  845. private bool TryFindNextCharacterHit(CharacterHit characterHit, out CharacterHit nextCharacterHit)
  846. {
  847. nextCharacterHit = characterHit;
  848. var codepointIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
  849. var lastCodepointIndex = FirstTextSourceIndex + Length;
  850. if (codepointIndex >= lastCodepointIndex)
  851. {
  852. return false; // Cannot go forward anymore
  853. }
  854. if (codepointIndex < FirstTextSourceIndex)
  855. {
  856. codepointIndex = FirstTextSourceIndex;
  857. }
  858. var runIndex = GetRunIndexAtCharacterIndex(codepointIndex, LogicalDirection.Forward, out var currentPosition);
  859. while (runIndex < _textRuns.Count)
  860. {
  861. var currentRun = _textRuns[runIndex];
  862. switch (currentRun)
  863. {
  864. case ShapedTextRun shapedRun:
  865. {
  866. var foundCharacterHit = shapedRun.GlyphRun.FindNearestCharacterHit(characterHit.FirstCharacterIndex + characterHit.TrailingLength, out _);
  867. var isAtEnd = foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength == FirstTextSourceIndex + Length;
  868. if (isAtEnd && !shapedRun.GlyphRun.IsLeftToRight)
  869. {
  870. nextCharacterHit = foundCharacterHit;
  871. return true;
  872. }
  873. //var characterIndex = codepointIndex - shapedRun.Text.Start;
  874. //if (characterIndex < 0 && shapedRun.ShapedBuffer.IsLeftToRight)
  875. //{
  876. // foundCharacterHit = new CharacterHit(foundCharacterHit.FirstCharacterIndex);
  877. //}
  878. nextCharacterHit = isAtEnd || characterHit.TrailingLength != 0 ?
  879. foundCharacterHit :
  880. new CharacterHit(foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength);
  881. if (isAtEnd || nextCharacterHit.FirstCharacterIndex > characterHit.FirstCharacterIndex)
  882. {
  883. return true;
  884. }
  885. break;
  886. }
  887. default:
  888. {
  889. var textPosition = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
  890. if (textPosition == currentPosition)
  891. {
  892. nextCharacterHit = new CharacterHit(currentPosition + currentRun.Length);
  893. return true;
  894. }
  895. break;
  896. }
  897. }
  898. currentPosition += currentRun.Length;
  899. runIndex++;
  900. }
  901. return false;
  902. }
  903. /// <summary>
  904. /// Tries to find the previous character hit.
  905. /// </summary>
  906. /// <param name="characterHit">The current character hit.</param>
  907. /// <param name="previousCharacterHit">The previous character hit.</param>
  908. /// <returns></returns>
  909. private bool TryFindPreviousCharacterHit(CharacterHit characterHit, out CharacterHit previousCharacterHit)
  910. {
  911. var characterIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
  912. if (characterIndex == FirstTextSourceIndex)
  913. {
  914. previousCharacterHit = new CharacterHit(FirstTextSourceIndex);
  915. return true;
  916. }
  917. previousCharacterHit = characterHit;
  918. if (characterIndex < FirstTextSourceIndex)
  919. {
  920. return false; // Cannot go backward anymore.
  921. }
  922. var runIndex = GetRunIndexAtCharacterIndex(characterIndex, LogicalDirection.Backward, out var currentPosition);
  923. while (runIndex >= 0)
  924. {
  925. var currentRun = _textRuns[runIndex];
  926. switch (currentRun)
  927. {
  928. case ShapedTextRun shapedRun:
  929. {
  930. var foundCharacterHit = shapedRun.GlyphRun.FindNearestCharacterHit(characterHit.FirstCharacterIndex - 1, out _);
  931. if (foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength < characterIndex)
  932. {
  933. previousCharacterHit = foundCharacterHit;
  934. return true;
  935. }
  936. var previousPosition = foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength;
  937. if (foundCharacterHit.TrailingLength > 0 && previousPosition == characterIndex)
  938. {
  939. previousCharacterHit = new CharacterHit(foundCharacterHit.FirstCharacterIndex);
  940. }
  941. if (previousCharacterHit != characterHit)
  942. {
  943. return true;
  944. }
  945. break;
  946. }
  947. default:
  948. {
  949. if (characterIndex == currentPosition + currentRun.Length)
  950. {
  951. previousCharacterHit = new CharacterHit(currentPosition);
  952. return true;
  953. }
  954. break;
  955. }
  956. }
  957. currentPosition -= currentRun.Length;
  958. runIndex--;
  959. }
  960. return false;
  961. }
  962. /// <summary>
  963. /// Gets the run index of the specified codepoint index.
  964. /// </summary>
  965. /// <param name="codepointIndex">The codepoint index.</param>
  966. /// <param name="direction">The logical direction.</param>
  967. /// <param name="textPosition">The text position of the found run index.</param>
  968. /// <returns>The text run index.</returns>
  969. private int GetRunIndexAtCharacterIndex(int codepointIndex, LogicalDirection direction, out int textPosition)
  970. {
  971. var runIndex = 0;
  972. textPosition = FirstTextSourceIndex;
  973. DrawableTextRun? previousRun = null;
  974. while (runIndex < _textRuns.Count)
  975. {
  976. var currentRun = _textRuns[runIndex];
  977. switch (currentRun)
  978. {
  979. case ShapedTextRun shapedRun:
  980. {
  981. var firstCluster = shapedRun.GlyphRun.Metrics.FirstCluster;
  982. if (firstCluster > codepointIndex)
  983. {
  984. break;
  985. }
  986. if (previousRun is ShapedTextRun previousShaped && !previousShaped.ShapedBuffer.IsLeftToRight)
  987. {
  988. if (shapedRun.ShapedBuffer.IsLeftToRight)
  989. {
  990. if (firstCluster >= codepointIndex)
  991. {
  992. return --runIndex;
  993. }
  994. }
  995. else
  996. {
  997. if (codepointIndex > firstCluster + currentRun.Length)
  998. {
  999. return --runIndex;
  1000. }
  1001. }
  1002. }
  1003. if (direction == LogicalDirection.Forward)
  1004. {
  1005. if (codepointIndex >= firstCluster && codepointIndex <= firstCluster + currentRun.Length)
  1006. {
  1007. return runIndex;
  1008. }
  1009. }
  1010. else
  1011. {
  1012. if (codepointIndex > firstCluster &&
  1013. codepointIndex <= firstCluster + currentRun.Length)
  1014. {
  1015. return runIndex;
  1016. }
  1017. }
  1018. if (runIndex + 1 >= _textRuns.Count)
  1019. {
  1020. return runIndex;
  1021. }
  1022. textPosition += currentRun.Length;
  1023. break;
  1024. }
  1025. default:
  1026. {
  1027. if (codepointIndex == textPosition)
  1028. {
  1029. return runIndex;
  1030. }
  1031. if (runIndex + 1 >= _textRuns.Count)
  1032. {
  1033. return runIndex;
  1034. }
  1035. textPosition += currentRun.Length;
  1036. break;
  1037. }
  1038. }
  1039. runIndex++;
  1040. previousRun = currentRun;
  1041. }
  1042. return runIndex;
  1043. }
  1044. private TextLineMetrics CreateLineMetrics()
  1045. {
  1046. var fontMetrics = _paragraphProperties.DefaultTextRunProperties.Typeface.GlyphTypeface.Metrics;
  1047. var fontRenderingEmSize = _paragraphProperties.DefaultTextRunProperties.FontRenderingEmSize;
  1048. var scale = fontRenderingEmSize / fontMetrics.DesignEmHeight;
  1049. var width = 0d;
  1050. var widthIncludingWhitespace = 0d;
  1051. var trailingWhitespaceLength = 0;
  1052. var newLineLength = 0;
  1053. var ascent = fontMetrics.Ascent * scale;
  1054. var descent = fontMetrics.Descent * scale;
  1055. var lineGap = fontMetrics.LineGap * scale;
  1056. var height = descent - ascent + lineGap;
  1057. var lineHeight = _paragraphProperties.LineHeight;
  1058. for (var index = 0; index < _textRuns.Count; index++)
  1059. {
  1060. switch (_textRuns[index])
  1061. {
  1062. case ShapedTextRun textRun:
  1063. {
  1064. var textMetrics =
  1065. new TextMetrics(textRun.Properties.Typeface.GlyphTypeface, textRun.Properties.FontRenderingEmSize);
  1066. if (fontRenderingEmSize < textRun.Properties.FontRenderingEmSize)
  1067. {
  1068. fontRenderingEmSize = textRun.Properties.FontRenderingEmSize;
  1069. if (ascent > textMetrics.Ascent)
  1070. {
  1071. ascent = textMetrics.Ascent;
  1072. }
  1073. if (descent < textMetrics.Descent)
  1074. {
  1075. descent = textMetrics.Descent;
  1076. }
  1077. if (lineGap < textMetrics.LineGap)
  1078. {
  1079. lineGap = textMetrics.LineGap;
  1080. }
  1081. if (descent - ascent + lineGap > height)
  1082. {
  1083. height = descent - ascent + lineGap;
  1084. }
  1085. }
  1086. if (index == _textRuns.Count - 1)
  1087. {
  1088. width = widthIncludingWhitespace + textRun.GlyphRun.Metrics.Width;
  1089. trailingWhitespaceLength = textRun.GlyphRun.Metrics.TrailingWhitespaceLength;
  1090. newLineLength = textRun.GlyphRun.Metrics.NewLineLength;
  1091. }
  1092. widthIncludingWhitespace += textRun.GlyphRun.Metrics.WidthIncludingTrailingWhitespace;
  1093. break;
  1094. }
  1095. case { } drawableTextRun:
  1096. {
  1097. widthIncludingWhitespace += drawableTextRun.Size.Width;
  1098. switch (_paragraphProperties.FlowDirection)
  1099. {
  1100. case FlowDirection.LeftToRight:
  1101. {
  1102. if (index == _textRuns.Count - 1)
  1103. {
  1104. width = widthIncludingWhitespace;
  1105. trailingWhitespaceLength = 0;
  1106. newLineLength = 0;
  1107. }
  1108. break;
  1109. }
  1110. case FlowDirection.RightToLeft:
  1111. {
  1112. if (index == _textRuns.Count - 1)
  1113. {
  1114. width = widthIncludingWhitespace;
  1115. trailingWhitespaceLength = 0;
  1116. newLineLength = 0;
  1117. }
  1118. break;
  1119. }
  1120. }
  1121. if (drawableTextRun.Size.Height > height)
  1122. {
  1123. height = drawableTextRun.Size.Height;
  1124. }
  1125. if (ascent > -drawableTextRun.Baseline)
  1126. {
  1127. ascent = -drawableTextRun.Baseline;
  1128. }
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. var start = GetParagraphOffsetX(width, widthIncludingWhitespace);
  1134. if (!double.IsNaN(lineHeight) && !MathUtilities.IsZero(lineHeight))
  1135. {
  1136. if (lineHeight > height)
  1137. {
  1138. height = lineHeight;
  1139. }
  1140. }
  1141. return new TextLineMetrics(widthIncludingWhitespace > _paragraphWidth, height, newLineLength, start,
  1142. -ascent, trailingWhitespaceLength, width, widthIncludingWhitespace);
  1143. }
  1144. /// <summary>
  1145. /// Gets the text line offset x.
  1146. /// </summary>
  1147. /// <param name="width">The line width.</param>
  1148. /// <param name="widthIncludingTrailingWhitespace">The paragraph width including whitespace.</param>
  1149. /// <returns>The paragraph offset.</returns>
  1150. private double GetParagraphOffsetX(double width, double widthIncludingTrailingWhitespace)
  1151. {
  1152. if (double.IsPositiveInfinity(_paragraphWidth))
  1153. {
  1154. return 0;
  1155. }
  1156. var textAlignment = _paragraphProperties.TextAlignment;
  1157. var paragraphFlowDirection = _paragraphProperties.FlowDirection;
  1158. if (textAlignment == TextAlignment.Justify)
  1159. {
  1160. textAlignment = TextAlignment.Start;
  1161. }
  1162. switch (textAlignment)
  1163. {
  1164. case TextAlignment.Start:
  1165. {
  1166. textAlignment = paragraphFlowDirection == FlowDirection.LeftToRight ? TextAlignment.Left : TextAlignment.Right;
  1167. break;
  1168. }
  1169. case TextAlignment.End:
  1170. {
  1171. textAlignment = paragraphFlowDirection == FlowDirection.RightToLeft ? TextAlignment.Left : TextAlignment.Right;
  1172. break;
  1173. }
  1174. case TextAlignment.DetectFromContent:
  1175. {
  1176. textAlignment = _resolvedFlowDirection == FlowDirection.LeftToRight ? TextAlignment.Left : TextAlignment.Right;
  1177. break;
  1178. }
  1179. }
  1180. switch (textAlignment)
  1181. {
  1182. case TextAlignment.Center:
  1183. var start = (_paragraphWidth - width) / 2;
  1184. if (paragraphFlowDirection == FlowDirection.RightToLeft)
  1185. {
  1186. start -= (widthIncludingTrailingWhitespace - width);
  1187. }
  1188. return Math.Max(0, start);
  1189. case TextAlignment.Right:
  1190. return Math.Max(0, _paragraphWidth - widthIncludingTrailingWhitespace);
  1191. default:
  1192. return 0;
  1193. }
  1194. }
  1195. private sealed class OrderedBidiRun
  1196. {
  1197. public OrderedBidiRun(DrawableTextRun run, sbyte level)
  1198. {
  1199. Run = run;
  1200. Level = level;
  1201. }
  1202. public sbyte Level { get; }
  1203. public DrawableTextRun Run { get; }
  1204. public OrderedBidiRun? Next { get; set; }
  1205. }
  1206. private sealed class BidiRange
  1207. {
  1208. public int Level { get; set; }
  1209. public OrderedBidiRun? Left { get; set; }
  1210. public OrderedBidiRun? Right { get; set; }
  1211. public BidiRange? Previous { get; set; }
  1212. public static BidiRange MergeWithPrevious(BidiRange range)
  1213. {
  1214. var previous = range.Previous;
  1215. BidiRange left;
  1216. BidiRange right;
  1217. if ((previous!.Level & 1) != 0)
  1218. {
  1219. // Odd, previous goes to the right of range.
  1220. left = range;
  1221. right = previous;
  1222. }
  1223. else
  1224. {
  1225. // Even, previous goes to the left of range.
  1226. left = previous;
  1227. right = range;
  1228. }
  1229. // Stitch them
  1230. left.Right!.Next = right.Left;
  1231. previous.Left = left.Left;
  1232. previous.Right = right.Right;
  1233. return previous;
  1234. }
  1235. }
  1236. }
  1237. }