12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613 |
- using System;
- using System.Collections.Generic;
- using Avalonia.Utilities;
- namespace Avalonia.Media.TextFormatting
- {
- internal class TextLineImpl : TextLine
- {
- private readonly List<DrawableTextRun> _textRuns;
- private readonly double _paragraphWidth;
- private readonly TextParagraphProperties _paragraphProperties;
- private TextLineMetrics _textLineMetrics;
- private readonly FlowDirection _resolvedFlowDirection;
- public TextLineImpl(List<DrawableTextRun> textRuns, int firstTextSourceIndex, int length, double paragraphWidth,
- TextParagraphProperties paragraphProperties, FlowDirection resolvedFlowDirection = FlowDirection.LeftToRight,
- TextLineBreak? lineBreak = null, bool hasCollapsed = false)
- {
- FirstTextSourceIndex = firstTextSourceIndex;
- Length = length;
- TextLineBreak = lineBreak;
- HasCollapsed = hasCollapsed;
- _textRuns = textRuns;
- _paragraphWidth = paragraphWidth;
- _paragraphProperties = paragraphProperties;
- _resolvedFlowDirection = resolvedFlowDirection;
- }
- /// <inheritdoc/>
- public override IReadOnlyList<TextRun> TextRuns => _textRuns;
- /// <inheritdoc/>
- public override int FirstTextSourceIndex { get; }
- /// <inheritdoc/>
- public override int Length { get; }
- /// <inheritdoc/>
- public override TextLineBreak? TextLineBreak { get; }
- /// <inheritdoc/>
- public override bool HasCollapsed { get; }
- /// <inheritdoc/>
- public override bool HasOverflowed => _textLineMetrics.HasOverflowed;
- /// <inheritdoc/>
- public override double Baseline => _textLineMetrics.TextBaseline;
- /// <inheritdoc/>
- public override double Extent => _textLineMetrics.Height;
- /// <inheritdoc/>
- public override double Height => _textLineMetrics.Height;
- /// <inheritdoc/>
- public override int NewLineLength => _textLineMetrics.NewlineLength;
- /// <inheritdoc/>
- public override double OverhangAfter => 0;
- /// <inheritdoc/>
- public override double OverhangLeading => 0;
- /// <inheritdoc/>
- public override double OverhangTrailing => 0;
- /// <inheritdoc/>
- public override int TrailingWhitespaceLength => _textLineMetrics.TrailingWhitespaceLength;
- /// <inheritdoc/>
- public override double Start => _textLineMetrics.Start;
- /// <inheritdoc/>
- public override double Width => _textLineMetrics.Width;
- /// <inheritdoc/>
- public override double WidthIncludingTrailingWhitespace => _textLineMetrics.WidthIncludingTrailingWhitespace;
- /// <inheritdoc/>
- public override void Draw(DrawingContext drawingContext, Point lineOrigin)
- {
- var (currentX, currentY) = lineOrigin;
- foreach (var textRun in _textRuns)
- {
- var offsetY = GetBaselineOffset(this, textRun);
- textRun.Draw(drawingContext, new Point(currentX, currentY + offsetY));
- currentX += textRun.Size.Width;
- }
- }
- private static double GetBaselineOffset(TextLine textLine, DrawableTextRun textRun)
- {
- var baseline = textRun.Baseline;
- var baselineAlignment = textRun.Properties?.BaselineAlignment;
- switch (baselineAlignment)
- {
- case BaselineAlignment.Top:
- return 0;
- case BaselineAlignment.Center:
- return textLine.Height / 2 - textRun.Size.Height / 2;
- case BaselineAlignment.Bottom:
- return textLine.Height - textRun.Size.Height;
- case BaselineAlignment.Baseline:
- case BaselineAlignment.TextTop:
- case BaselineAlignment.TextBottom:
- case BaselineAlignment.Subscript:
- case BaselineAlignment.Superscript:
- return textLine.Baseline - baseline;
- default:
- throw new ArgumentOutOfRangeException(nameof(baselineAlignment), baselineAlignment, null);
- }
- }
- /// <inheritdoc/>
- public override TextLine Collapse(params TextCollapsingProperties?[] collapsingPropertiesList)
- {
- if (collapsingPropertiesList.Length == 0)
- {
- return this;
- }
- var collapsingProperties = collapsingPropertiesList[0];
- if (collapsingProperties is null)
- {
- return this;
- }
- var collapsedRuns = collapsingProperties.Collapse(this);
- if (collapsedRuns is null)
- {
- return this;
- }
- var collapsedLine = new TextLineImpl(collapsedRuns, FirstTextSourceIndex, Length, _paragraphWidth, _paragraphProperties,
- _resolvedFlowDirection, TextLineBreak, true);
- if (collapsedRuns.Count > 0)
- {
- collapsedLine.FinalizeLine();
- }
- return collapsedLine;
- }
- /// <inheritdoc/>
- public override void Justify(JustificationProperties justificationProperties)
- {
- justificationProperties.Justify(this);
- _textLineMetrics = CreateLineMetrics();
- }
- /// <inheritdoc/>
- public override CharacterHit GetCharacterHitFromDistance(double distance)
- {
- if (_textRuns.Count == 0)
- {
- return new CharacterHit();
- }
- distance -= Start;
- if (distance <= 0)
- {
- var firstRun = _textRuns[0];
- return GetRunCharacterHit(firstRun, FirstTextSourceIndex, 0);
- }
- if (distance >= WidthIncludingTrailingWhitespace)
- {
- var lastRun = _textRuns[_textRuns.Count - 1];
- return GetRunCharacterHit(lastRun, FirstTextSourceIndex + Length - lastRun.Length, lastRun.Size.Width);
- }
- // process hit that happens within the line
- var characterHit = new CharacterHit();
- var currentPosition = FirstTextSourceIndex;
- var currentDistance = 0.0;
- for (var i = 0; i < _textRuns.Count; i++)
- {
- var currentRun = _textRuns[i];
- if (currentRun is ShapedTextRun shapedRun && !shapedRun.ShapedBuffer.IsLeftToRight)
- {
- var rightToLeftIndex = i;
- currentPosition += currentRun.Length;
- while (rightToLeftIndex + 1 <= _textRuns.Count - 1)
- {
- var nextShaped = _textRuns[++rightToLeftIndex] as ShapedTextRun;
- if (nextShaped == null || nextShaped.ShapedBuffer.IsLeftToRight)
- {
- break;
- }
- currentPosition += nextShaped.Length;
- rightToLeftIndex++;
- }
- for (var j = i; i <= rightToLeftIndex; j++)
- {
- if (j > _textRuns.Count - 1)
- {
- break;
- }
- currentRun = _textRuns[j];
- if (currentDistance + currentRun.Size.Width <= distance)
- {
- currentDistance += currentRun.Size.Width;
- currentPosition -= currentRun.Length;
- continue;
- }
- return GetRunCharacterHit(currentRun, currentPosition, distance - currentDistance);
- }
- }
- characterHit = GetRunCharacterHit(currentRun, currentPosition, distance - currentDistance);
- if (i < _textRuns.Count - 1 && currentDistance + currentRun.Size.Width < distance)
- {
- currentDistance += currentRun.Size.Width;
- currentPosition += currentRun.Length;
- continue;
- }
- break;
- }
- return characterHit;
- }
- private static CharacterHit GetRunCharacterHit(DrawableTextRun run, int currentPosition, double distance)
- {
- CharacterHit characterHit;
- switch (run)
- {
- case ShapedTextRun shapedRun:
- {
- characterHit = shapedRun.GlyphRun.GetCharacterHitFromDistance(distance, out _);
- var offset = 0;
- if (shapedRun.GlyphRun.IsLeftToRight)
- {
- offset = Math.Max(0, currentPosition - shapedRun.GlyphRun.Metrics.FirstCluster);
- }
- characterHit = new CharacterHit(offset + characterHit.FirstCharacterIndex, characterHit.TrailingLength);
- break;
- }
- default:
- {
- if (distance < run.Size.Width / 2)
- {
- characterHit = new CharacterHit(currentPosition);
- }
- else
- {
- characterHit = new CharacterHit(currentPosition, run.Length);
- }
- break;
- }
- }
- return characterHit;
- }
- /// <inheritdoc/>
- public override double GetDistanceFromCharacterHit(CharacterHit characterHit)
- {
- var flowDirection = _paragraphProperties.FlowDirection;
- var characterIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
- var currentPosition = FirstTextSourceIndex;
- var remainingLength = characterIndex - FirstTextSourceIndex;
- var currentDistance = Start;
- if (flowDirection == FlowDirection.LeftToRight)
- {
- for (var index = 0; index < _textRuns.Count; index++)
- {
- var currentRun = _textRuns[index];
- if (currentRun is ShapedTextRun shapedRun && !shapedRun.ShapedBuffer.IsLeftToRight)
- {
- var i = index;
- var rightToLeftWidth = currentRun.Size.Width;
- while (i + 1 <= _textRuns.Count - 1)
- {
- var nextRun = _textRuns[i + 1];
- if (nextRun is ShapedTextRun nextShapedRun && !nextShapedRun.ShapedBuffer.IsLeftToRight)
- {
- i++;
- rightToLeftWidth += nextRun.Size.Width;
- continue;
- }
- break;
- }
- if (i > index)
- {
- while (i >= index)
- {
- currentRun = _textRuns[i];
- rightToLeftWidth -= currentRun.Size.Width;
- if (currentPosition + currentRun.Length >= characterIndex)
- {
- break;
- }
- currentPosition += currentRun.Length;
- remainingLength -= currentRun.Length;
- i--;
- }
- currentDistance += rightToLeftWidth;
- }
- }
- if (currentPosition + currentRun.Length >= characterIndex &&
- TryGetDistanceFromCharacterHit(currentRun, characterHit, currentPosition, remainingLength, flowDirection, out var distance, out _))
- {
- return Math.Max(0, currentDistance + distance);
- }
- //No hit hit found so we add the full width
- currentDistance += currentRun.Size.Width;
- currentPosition += currentRun.Length;
- remainingLength -= currentRun.Length;
- }
- }
- else
- {
- currentDistance += WidthIncludingTrailingWhitespace;
- for (var index = _textRuns.Count - 1; index >= 0; index--)
- {
- var currentRun = _textRuns[index];
- if (TryGetDistanceFromCharacterHit(currentRun, characterHit, currentPosition, remainingLength,
- flowDirection, out var distance, out var currentGlyphRun))
- {
- if (currentGlyphRun != null)
- {
- distance = currentGlyphRun.Size.Width - distance;
- }
- return Math.Max(0, currentDistance - distance);
- }
- //No hit hit found so we add the full width
- currentDistance -= currentRun.Size.Width;
- currentPosition += currentRun.Length;
- remainingLength -= currentRun.Length;
- }
- }
- return Math.Max(0, currentDistance);
- }
- private static bool TryGetDistanceFromCharacterHit(
- DrawableTextRun currentRun,
- CharacterHit characterHit,
- int currentPosition,
- int remainingLength,
- FlowDirection flowDirection,
- out double distance,
- out GlyphRun? currentGlyphRun)
- {
- var characterIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
- var isTrailingHit = characterHit.TrailingLength > 0;
- distance = 0;
- currentGlyphRun = null;
- switch (currentRun)
- {
- case ShapedTextRun shapedTextCharacters:
- {
- currentGlyphRun = shapedTextCharacters.GlyphRun;
- if (currentPosition + remainingLength <= currentPosition + currentRun.Length)
- {
- characterHit = new CharacterHit(currentPosition + remainingLength);
- distance = currentGlyphRun.GetDistanceFromCharacterHit(characterHit);
- return true;
- }
- if (currentPosition + remainingLength == currentPosition + currentRun.Length && isTrailingHit)
- {
- if (currentGlyphRun.IsLeftToRight || flowDirection == FlowDirection.RightToLeft)
- {
- distance = currentGlyphRun.Size.Width;
- }
- return true;
- }
- break;
- }
- default:
- {
- if (characterIndex == currentPosition)
- {
- return true;
- }
- if (characterIndex == currentPosition + currentRun.Length)
- {
- distance = currentRun.Size.Width;
- return true;
- }
- break;
- }
- }
- return false;
- }
- /// <inheritdoc/>
- public override CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit)
- {
- if (_textRuns.Count == 0)
- {
- return new CharacterHit();
- }
- if (TryFindNextCharacterHit(characterHit, out var nextCharacterHit))
- {
- return nextCharacterHit;
- }
- var lastTextPosition = FirstTextSourceIndex + Length;
- // Can't move, we're after the last character
- var runIndex = GetRunIndexAtCharacterIndex(lastTextPosition, LogicalDirection.Forward, out var currentPosition);
- var currentRun = _textRuns[runIndex];
- switch (currentRun)
- {
- case ShapedTextRun shapedRun:
- {
- nextCharacterHit = shapedRun.GlyphRun.GetNextCaretCharacterHit(characterHit);
- break;
- }
- default:
- {
- nextCharacterHit = new CharacterHit(currentPosition + currentRun.Length);
- break;
- }
- }
- if (characterHit.FirstCharacterIndex + characterHit.TrailingLength == nextCharacterHit.FirstCharacterIndex + nextCharacterHit.TrailingLength)
- {
- return characterHit;
- }
- return nextCharacterHit;
- }
- /// <inheritdoc/>
- public override CharacterHit GetPreviousCaretCharacterHit(CharacterHit characterHit)
- {
- if (TryFindPreviousCharacterHit(characterHit, out var previousCharacterHit))
- {
- return previousCharacterHit;
- }
- if (characterHit.FirstCharacterIndex <= FirstTextSourceIndex)
- {
- characterHit = new CharacterHit(FirstTextSourceIndex);
- }
- return characterHit; // Can't move, we're before the first character
- }
- /// <inheritdoc/>
- public override CharacterHit GetBackspaceCaretCharacterHit(CharacterHit characterHit)
- {
- // same operation as move-to-previous
- return GetPreviousCaretCharacterHit(characterHit);
- }
- private IReadOnlyList<TextBounds> GetTextBoundsLeftToRight(int firstTextSourceIndex, int textLength)
- {
- var characterIndex = firstTextSourceIndex + textLength;
- var result = new List<TextBounds>(TextRuns.Count);
- var lastDirection = FlowDirection.LeftToRight;
- var currentDirection = lastDirection;
- var currentPosition = FirstTextSourceIndex;
- var remainingLength = textLength;
- var startX = Start;
- double currentWidth = 0;
- var currentRect = default(Rect);
- TextRunBounds lastRunBounds = default;
- for (var index = 0; index < TextRuns.Count; index++)
- {
- if (TextRuns[index] is not DrawableTextRun currentRun)
- {
- continue;
- }
- var characterLength = 0;
- var endX = startX;
- TextRunBounds currentRunBounds;
- double combinedWidth;
- if (currentRun is ShapedTextRun currentShapedRun)
- {
- var firstCluster = currentShapedRun.GlyphRun.Metrics.FirstCluster;
- if (currentPosition + currentRun.Length <= firstTextSourceIndex)
- {
- startX += currentRun.Size.Width;
- currentPosition += currentRun.Length;
- continue;
- }
- if (currentShapedRun.ShapedBuffer.IsLeftToRight)
- {
- var startIndex = firstCluster + Math.Max(0, firstTextSourceIndex - currentPosition);
- double startOffset;
- double endOffset;
- startOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
- endOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
- startX += startOffset;
- endX += endOffset;
- var endHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(endOffset, out _);
- var startHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(startOffset, out _);
- characterLength = Math.Abs(endHit.FirstCharacterIndex + endHit.TrailingLength - startHit.FirstCharacterIndex - startHit.TrailingLength);
- currentDirection = FlowDirection.LeftToRight;
- }
- else
- {
- var rightToLeftIndex = index;
- var rightToLeftWidth = currentShapedRun.Size.Width;
- while (rightToLeftIndex + 1 <= _textRuns.Count - 1 && _textRuns[rightToLeftIndex + 1] is ShapedTextRun nextShapedRun)
- {
- if (nextShapedRun == null || nextShapedRun.ShapedBuffer.IsLeftToRight)
- {
- break;
- }
- rightToLeftIndex++;
- rightToLeftWidth += nextShapedRun.Size.Width;
- if (currentPosition + nextShapedRun.Length > firstTextSourceIndex + textLength)
- {
- break;
- }
- currentShapedRun = nextShapedRun;
- }
- startX += rightToLeftWidth;
- currentRunBounds = GetRightToLeftTextRunBounds(currentShapedRun, startX, firstTextSourceIndex, characterIndex, currentPosition, remainingLength);
- remainingLength -= currentRunBounds.Length;
- currentPosition = currentRunBounds.TextSourceCharacterIndex + currentRunBounds.Length;
- endX = currentRunBounds.Rectangle.Right;
- startX = currentRunBounds.Rectangle.Left;
- var rightToLeftRunBounds = new List<TextRunBounds> { currentRunBounds };
- for (int i = rightToLeftIndex - 1; i >= index; i--)
- {
- if (TextRuns[i] is not ShapedTextRun)
- {
- continue;
- }
- currentShapedRun = (ShapedTextRun)TextRuns[i];
- currentRunBounds = GetRightToLeftTextRunBounds(currentShapedRun, startX, firstTextSourceIndex, characterIndex, currentPosition, remainingLength);
- rightToLeftRunBounds.Insert(0, currentRunBounds);
- remainingLength -= currentRunBounds.Length;
- startX = currentRunBounds.Rectangle.Left;
- currentPosition += currentRunBounds.Length;
- }
- combinedWidth = endX - startX;
- currentRect = new Rect(startX, 0, combinedWidth, Height);
- currentDirection = FlowDirection.RightToLeft;
- if (!MathUtilities.IsZero(combinedWidth))
- {
- result.Add(new TextBounds(currentRect, currentDirection, rightToLeftRunBounds));
- }
- startX = endX;
- }
- }
- else
- {
- if (currentPosition + currentRun.Length <= firstTextSourceIndex)
- {
- startX += currentRun.Size.Width;
- currentPosition += currentRun.Length;
- continue;
- }
- if (currentPosition < firstTextSourceIndex)
- {
- startX += currentRun.Size.Width;
- }
- if (currentPosition + currentRun.Length <= characterIndex)
- {
- endX += currentRun.Size.Width;
- characterLength = currentRun.Length;
- }
- }
- if (endX < startX)
- {
- (endX, startX) = (startX, endX);
- }
- //Lines that only contain a linebreak need to be covered here
- if (characterLength == 0)
- {
- characterLength = NewLineLength;
- }
- combinedWidth = endX - startX;
- currentRunBounds = new TextRunBounds(new Rect(startX, 0, combinedWidth, Height), currentPosition, characterLength, currentRun);
- currentPosition += characterLength;
- remainingLength -= characterLength;
- startX = endX;
- if (currentRunBounds.TextRun != null && !MathUtilities.IsZero(combinedWidth) || NewLineLength > 0)
- {
- if (result.Count > 0 && lastDirection == currentDirection && MathUtilities.AreClose(currentRect.Left, lastRunBounds.Rectangle.Right))
- {
- currentRect = currentRect.WithWidth(currentWidth + combinedWidth);
- var textBounds = result[result.Count - 1];
- textBounds.Rectangle = currentRect;
- textBounds.TextRunBounds.Add(currentRunBounds);
- }
- else
- {
- currentRect = currentRunBounds.Rectangle;
- result.Add(new TextBounds(currentRect, currentDirection, new List<TextRunBounds> { currentRunBounds }));
- }
- }
- lastRunBounds = currentRunBounds;
- currentWidth += combinedWidth;
- if (remainingLength <= 0 || currentPosition >= characterIndex)
- {
- break;
- }
- lastDirection = currentDirection;
- }
- return result;
- }
- private IReadOnlyList<TextBounds> GetTextBoundsRightToLeft(int firstTextSourceIndex, int textLength)
- {
- var characterIndex = firstTextSourceIndex + textLength;
- var result = new List<TextBounds>(TextRuns.Count);
- var lastDirection = FlowDirection.LeftToRight;
- var currentDirection = lastDirection;
- var currentPosition = FirstTextSourceIndex;
- var remainingLength = textLength;
- var startX = WidthIncludingTrailingWhitespace;
- double currentWidth = 0;
- var currentRect = default(Rect);
- for (var index = TextRuns.Count - 1; index >= 0; index--)
- {
- if (TextRuns[index] is not DrawableTextRun currentRun)
- {
- continue;
- }
- if (currentPosition + currentRun.Length < firstTextSourceIndex)
- {
- startX -= currentRun.Size.Width;
- currentPosition += currentRun.Length;
- continue;
- }
- var characterLength = 0;
- var endX = startX;
- if (currentRun is ShapedTextRun currentShapedRun)
- {
- var offset = Math.Max(0, firstTextSourceIndex - currentPosition);
- currentPosition += offset;
- var startIndex = currentPosition;
- double startOffset;
- double endOffset;
- if (currentShapedRun.ShapedBuffer.IsLeftToRight)
- {
- if (currentPosition < startIndex)
- {
- startOffset = endOffset = 0;
- }
- else
- {
- endOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
- startOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
- }
- }
- else
- {
- endOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
- startOffset = currentShapedRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
- }
- startX -= currentRun.Size.Width - startOffset;
- endX -= currentRun.Size.Width - endOffset;
- var endHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(endOffset, out _);
- var startHit = currentShapedRun.GlyphRun.GetCharacterHitFromDistance(startOffset, out _);
- characterLength = Math.Abs(startHit.FirstCharacterIndex + startHit.TrailingLength - endHit.FirstCharacterIndex - endHit.TrailingLength);
- currentDirection = currentShapedRun.ShapedBuffer.IsLeftToRight ?
- FlowDirection.LeftToRight :
- FlowDirection.RightToLeft;
- }
- else
- {
- if (currentPosition + currentRun.Length <= characterIndex)
- {
- endX -= currentRun.Size.Width;
- }
- if (currentPosition < firstTextSourceIndex)
- {
- startX -= currentRun.Size.Width;
- characterLength = currentRun.Length;
- }
- }
- if (endX < startX)
- {
- (endX, startX) = (startX, endX);
- }
- //Lines that only contain a linebreak need to be covered here
- if (characterLength == 0)
- {
- characterLength = NewLineLength;
- }
- var runWidth = endX - startX;
- var currentRunBounds = new TextRunBounds(new Rect(Start + startX, 0, runWidth, Height), currentPosition, characterLength, currentRun);
- if (!MathUtilities.IsZero(runWidth) || NewLineLength > 0)
- {
- if (lastDirection == currentDirection && result.Count > 0 && MathUtilities.AreClose(currentRect.Right, Start + startX))
- {
- currentRect = currentRect.WithWidth(currentWidth + runWidth);
- var textBounds = result[result.Count - 1];
- textBounds.Rectangle = currentRect;
- textBounds.TextRunBounds.Add(currentRunBounds);
- }
- else
- {
- currentRect = currentRunBounds.Rectangle;
- result.Add(new TextBounds(currentRect, currentDirection, new List<TextRunBounds> { currentRunBounds }));
- }
- }
- currentWidth += runWidth;
- currentPosition += characterLength;
- if (currentPosition > characterIndex)
- {
- break;
- }
- lastDirection = currentDirection;
- remainingLength -= characterLength;
- if (remainingLength <= 0)
- {
- break;
- }
- }
- result.Reverse();
- return result;
- }
- private TextRunBounds GetRightToLeftTextRunBounds(ShapedTextRun currentRun, double endX, int firstTextSourceIndex, int characterIndex, int currentPosition, int remainingLength)
- {
- var startX = endX;
- var offset = Math.Max(0, firstTextSourceIndex - currentPosition);
- currentPosition += offset;
- var startIndex = currentPosition;
- double startOffset;
- double endOffset;
- endOffset = currentRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex));
- startOffset = currentRun.GlyphRun.GetDistanceFromCharacterHit(new CharacterHit(startIndex + remainingLength));
- startX -= currentRun.Size.Width - startOffset;
- endX -= currentRun.Size.Width - endOffset;
- var endHit = currentRun.GlyphRun.GetCharacterHitFromDistance(endOffset, out _);
- var startHit = currentRun.GlyphRun.GetCharacterHitFromDistance(startOffset, out _);
- var characterLength = Math.Abs(startHit.FirstCharacterIndex + startHit.TrailingLength - endHit.FirstCharacterIndex - endHit.TrailingLength);
- if (endX < startX)
- {
- (endX, startX) = (startX, endX);
- }
- //Lines that only contain a linebreak need to be covered here
- if (characterLength == 0)
- {
- characterLength = NewLineLength;
- }
- var runWidth = endX - startX;
- return new TextRunBounds(new Rect(Start + startX, 0, runWidth, Height), currentPosition, characterLength, currentRun);
- }
- public override IReadOnlyList<TextBounds> GetTextBounds(int firstTextSourceIndex, int textLength)
- {
- if (_paragraphProperties.FlowDirection == FlowDirection.LeftToRight)
- {
- return GetTextBoundsLeftToRight(firstTextSourceIndex, textLength);
- }
- return GetTextBoundsRightToLeft(firstTextSourceIndex, textLength);
- }
- public TextLineImpl FinalizeLine()
- {
- _textLineMetrics = CreateLineMetrics();
- BidiReorder();
- return this;
- }
- private static sbyte GetRunBidiLevel(DrawableTextRun run, FlowDirection flowDirection)
- {
- if (run is ShapedTextRun shapedTextCharacters)
- {
- return shapedTextCharacters.BidiLevel;
- }
- var defaultLevel = flowDirection == FlowDirection.LeftToRight ? 0 : 1;
- return (sbyte)defaultLevel;
- }
- private void BidiReorder()
- {
- if (_textRuns.Count == 0)
- {
- return;
- }
- // Build up the collection of ordered runs.
- var run = _textRuns[0];
- OrderedBidiRun orderedRun = new(run, GetRunBidiLevel(run, _resolvedFlowDirection));
- var current = orderedRun;
- for (var i = 1; i < _textRuns.Count; i++)
- {
- run = _textRuns[i];
- current.Next = new OrderedBidiRun(run, GetRunBidiLevel(run, _resolvedFlowDirection));
- current = current.Next;
- }
- // Reorder them into visual order.
- orderedRun = LinearReOrder(orderedRun);
- // Now perform a recursive reversal of each run.
- // From the highest level found in the text to the lowest odd level on each line, including intermediate levels
- // not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
- // https://unicode.org/reports/tr9/#L2
- sbyte max = 0;
- var min = sbyte.MaxValue;
- for (var i = 0; i < _textRuns.Count; i++)
- {
- var currentRun = _textRuns[i];
- var level = GetRunBidiLevel(currentRun, _resolvedFlowDirection);
- if (level > max)
- {
- max = level;
- }
- if ((level & 1) != 0 && level < min)
- {
- min = level;
- }
- }
- if (min > max)
- {
- min = max;
- }
- if (max == 0 || (min == max && (max & 1) == 0))
- {
- // Nothing to reverse.
- return;
- }
- // Now apply the reversal and replace the original contents.
- var minLevelToReverse = max;
- while (minLevelToReverse >= min)
- {
- current = orderedRun;
- while (current != null)
- {
- if (current.Level >= minLevelToReverse && current.Level % 2 != 0)
- {
- if (current.Run is ShapedTextRun { IsReversed: false } shapedTextCharacters)
- {
- shapedTextCharacters.Reverse();
- }
- }
- current = current.Next;
- }
- minLevelToReverse--;
- }
- _textRuns.Clear();
- current = orderedRun;
- while (current != null)
- {
- _textRuns.Add(current.Run);
- current = current.Next;
- }
- }
- /// <summary>
- /// Reorders a series of runs from logical to visual order, returning the left most run.
- /// <see href="https://github.com/fribidi/linear-reorder/blob/f2f872257d4d8b8e137fcf831f254d6d4db79d3c/linear-reorder.c"/>
- /// </summary>
- /// <param name="run">The ordered bidi run.</param>
- /// <returns>The <see cref="OrderedBidiRun"/>.</returns>
- private static OrderedBidiRun LinearReOrder(OrderedBidiRun? run)
- {
- BidiRange? range = null;
- while (run != null)
- {
- var next = run.Next;
- while (range != null && range.Level > run.Level
- && range.Previous != null && range.Previous.Level >= run.Level)
- {
- range = BidiRange.MergeWithPrevious(range);
- }
- if (range != null && range.Level >= run.Level)
- {
- // Attach run to the range.
- if ((run.Level & 1) != 0)
- {
- // Odd, range goes to the right of run.
- run.Next = range.Left;
- range.Left = run;
- }
- else
- {
- // Even, range goes to the left of run.
- range.Right!.Next = run;
- range.Right = run;
- }
- range.Level = run.Level;
- }
- else
- {
- var r = new BidiRange();
- r.Left = r.Right = run;
- r.Level = run.Level;
- r.Previous = range;
- range = r;
- }
- run = next;
- }
- while (range?.Previous != null)
- {
- range = BidiRange.MergeWithPrevious(range);
- }
- // Terminate.
- range!.Right!.Next = null;
- return range.Left!;
- }
- /// <summary>
- /// Tries to find the next character hit.
- /// </summary>
- /// <param name="characterHit">The current character hit.</param>
- /// <param name="nextCharacterHit">The next character hit.</param>
- /// <returns></returns>
- private bool TryFindNextCharacterHit(CharacterHit characterHit, out CharacterHit nextCharacterHit)
- {
- nextCharacterHit = characterHit;
- var codepointIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
- var lastCodepointIndex = FirstTextSourceIndex + Length;
- if (codepointIndex >= lastCodepointIndex)
- {
- return false; // Cannot go forward anymore
- }
- if (codepointIndex < FirstTextSourceIndex)
- {
- codepointIndex = FirstTextSourceIndex;
- }
- var runIndex = GetRunIndexAtCharacterIndex(codepointIndex, LogicalDirection.Forward, out var currentPosition);
- while (runIndex < _textRuns.Count)
- {
- var currentRun = _textRuns[runIndex];
- switch (currentRun)
- {
- case ShapedTextRun shapedRun:
- {
- var foundCharacterHit = shapedRun.GlyphRun.FindNearestCharacterHit(characterHit.FirstCharacterIndex + characterHit.TrailingLength, out _);
- var isAtEnd = foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength == FirstTextSourceIndex + Length;
- if (isAtEnd && !shapedRun.GlyphRun.IsLeftToRight)
- {
- nextCharacterHit = foundCharacterHit;
- return true;
- }
- //var characterIndex = codepointIndex - shapedRun.Text.Start;
- //if (characterIndex < 0 && shapedRun.ShapedBuffer.IsLeftToRight)
- //{
- // foundCharacterHit = new CharacterHit(foundCharacterHit.FirstCharacterIndex);
- //}
- nextCharacterHit = isAtEnd || characterHit.TrailingLength != 0 ?
- foundCharacterHit :
- new CharacterHit(foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength);
- if (isAtEnd || nextCharacterHit.FirstCharacterIndex > characterHit.FirstCharacterIndex)
- {
- return true;
- }
- break;
- }
- default:
- {
- var textPosition = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
- if (textPosition == currentPosition)
- {
- nextCharacterHit = new CharacterHit(currentPosition + currentRun.Length);
- return true;
- }
- break;
- }
- }
- currentPosition += currentRun.Length;
- runIndex++;
- }
- return false;
- }
- /// <summary>
- /// Tries to find the previous character hit.
- /// </summary>
- /// <param name="characterHit">The current character hit.</param>
- /// <param name="previousCharacterHit">The previous character hit.</param>
- /// <returns></returns>
- private bool TryFindPreviousCharacterHit(CharacterHit characterHit, out CharacterHit previousCharacterHit)
- {
- var characterIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
- if (characterIndex == FirstTextSourceIndex)
- {
- previousCharacterHit = new CharacterHit(FirstTextSourceIndex);
- return true;
- }
- previousCharacterHit = characterHit;
- if (characterIndex < FirstTextSourceIndex)
- {
- return false; // Cannot go backward anymore.
- }
- var runIndex = GetRunIndexAtCharacterIndex(characterIndex, LogicalDirection.Backward, out var currentPosition);
- while (runIndex >= 0)
- {
- var currentRun = _textRuns[runIndex];
- switch (currentRun)
- {
- case ShapedTextRun shapedRun:
- {
- var foundCharacterHit = shapedRun.GlyphRun.FindNearestCharacterHit(characterHit.FirstCharacterIndex - 1, out _);
- if (foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength < characterIndex)
- {
- previousCharacterHit = foundCharacterHit;
- return true;
- }
- var previousPosition = foundCharacterHit.FirstCharacterIndex + foundCharacterHit.TrailingLength;
- if (foundCharacterHit.TrailingLength > 0 && previousPosition == characterIndex)
- {
- previousCharacterHit = new CharacterHit(foundCharacterHit.FirstCharacterIndex);
- }
- if (previousCharacterHit != characterHit)
- {
- return true;
- }
- break;
- }
- default:
- {
- if (characterIndex == currentPosition + currentRun.Length)
- {
- previousCharacterHit = new CharacterHit(currentPosition);
- return true;
- }
- break;
- }
- }
- currentPosition -= currentRun.Length;
- runIndex--;
- }
- return false;
- }
- /// <summary>
- /// Gets the run index of the specified codepoint index.
- /// </summary>
- /// <param name="codepointIndex">The codepoint index.</param>
- /// <param name="direction">The logical direction.</param>
- /// <param name="textPosition">The text position of the found run index.</param>
- /// <returns>The text run index.</returns>
- private int GetRunIndexAtCharacterIndex(int codepointIndex, LogicalDirection direction, out int textPosition)
- {
- var runIndex = 0;
- textPosition = FirstTextSourceIndex;
- DrawableTextRun? previousRun = null;
- while (runIndex < _textRuns.Count)
- {
- var currentRun = _textRuns[runIndex];
- switch (currentRun)
- {
- case ShapedTextRun shapedRun:
- {
- var firstCluster = shapedRun.GlyphRun.Metrics.FirstCluster;
- if (firstCluster > codepointIndex)
- {
- break;
- }
- if (previousRun is ShapedTextRun previousShaped && !previousShaped.ShapedBuffer.IsLeftToRight)
- {
- if (shapedRun.ShapedBuffer.IsLeftToRight)
- {
- if (firstCluster >= codepointIndex)
- {
- return --runIndex;
- }
- }
- else
- {
- if (codepointIndex > firstCluster + currentRun.Length)
- {
- return --runIndex;
- }
- }
- }
- if (direction == LogicalDirection.Forward)
- {
- if (codepointIndex >= firstCluster && codepointIndex <= firstCluster + currentRun.Length)
- {
- return runIndex;
- }
- }
- else
- {
- if (codepointIndex > firstCluster &&
- codepointIndex <= firstCluster + currentRun.Length)
- {
- return runIndex;
- }
- }
- if (runIndex + 1 >= _textRuns.Count)
- {
- return runIndex;
- }
- textPosition += currentRun.Length;
- break;
- }
- default:
- {
- if (codepointIndex == textPosition)
- {
- return runIndex;
- }
- if (runIndex + 1 >= _textRuns.Count)
- {
- return runIndex;
- }
- textPosition += currentRun.Length;
- break;
- }
- }
- runIndex++;
- previousRun = currentRun;
- }
- return runIndex;
- }
- private TextLineMetrics CreateLineMetrics()
- {
- var fontMetrics = _paragraphProperties.DefaultTextRunProperties.Typeface.GlyphTypeface.Metrics;
- var fontRenderingEmSize = _paragraphProperties.DefaultTextRunProperties.FontRenderingEmSize;
- var scale = fontRenderingEmSize / fontMetrics.DesignEmHeight;
- var width = 0d;
- var widthIncludingWhitespace = 0d;
- var trailingWhitespaceLength = 0;
- var newLineLength = 0;
- var ascent = fontMetrics.Ascent * scale;
- var descent = fontMetrics.Descent * scale;
- var lineGap = fontMetrics.LineGap * scale;
- var height = descent - ascent + lineGap;
- var lineHeight = _paragraphProperties.LineHeight;
- for (var index = 0; index < _textRuns.Count; index++)
- {
- switch (_textRuns[index])
- {
- case ShapedTextRun textRun:
- {
- var textMetrics =
- new TextMetrics(textRun.Properties.Typeface.GlyphTypeface, textRun.Properties.FontRenderingEmSize);
- if (fontRenderingEmSize < textRun.Properties.FontRenderingEmSize)
- {
- fontRenderingEmSize = textRun.Properties.FontRenderingEmSize;
- if (ascent > textMetrics.Ascent)
- {
- ascent = textMetrics.Ascent;
- }
- if (descent < textMetrics.Descent)
- {
- descent = textMetrics.Descent;
- }
- if (lineGap < textMetrics.LineGap)
- {
- lineGap = textMetrics.LineGap;
- }
- if (descent - ascent + lineGap > height)
- {
- height = descent - ascent + lineGap;
- }
- }
- if (index == _textRuns.Count - 1)
- {
- width = widthIncludingWhitespace + textRun.GlyphRun.Metrics.Width;
- trailingWhitespaceLength = textRun.GlyphRun.Metrics.TrailingWhitespaceLength;
- newLineLength = textRun.GlyphRun.Metrics.NewLineLength;
- }
- widthIncludingWhitespace += textRun.GlyphRun.Metrics.WidthIncludingTrailingWhitespace;
- break;
- }
- case { } drawableTextRun:
- {
- widthIncludingWhitespace += drawableTextRun.Size.Width;
- switch (_paragraphProperties.FlowDirection)
- {
- case FlowDirection.LeftToRight:
- {
- if (index == _textRuns.Count - 1)
- {
- width = widthIncludingWhitespace;
- trailingWhitespaceLength = 0;
- newLineLength = 0;
- }
- break;
- }
- case FlowDirection.RightToLeft:
- {
- if (index == _textRuns.Count - 1)
- {
- width = widthIncludingWhitespace;
- trailingWhitespaceLength = 0;
- newLineLength = 0;
- }
- break;
- }
- }
- if (drawableTextRun.Size.Height > height)
- {
- height = drawableTextRun.Size.Height;
- }
- if (ascent > -drawableTextRun.Baseline)
- {
- ascent = -drawableTextRun.Baseline;
- }
- break;
- }
- }
- }
- var start = GetParagraphOffsetX(width, widthIncludingWhitespace);
- if (!double.IsNaN(lineHeight) && !MathUtilities.IsZero(lineHeight))
- {
- if (lineHeight > height)
- {
- height = lineHeight;
- }
- }
- return new TextLineMetrics(widthIncludingWhitespace > _paragraphWidth, height, newLineLength, start,
- -ascent, trailingWhitespaceLength, width, widthIncludingWhitespace);
- }
- /// <summary>
- /// Gets the text line offset x.
- /// </summary>
- /// <param name="width">The line width.</param>
- /// <param name="widthIncludingTrailingWhitespace">The paragraph width including whitespace.</param>
- /// <returns>The paragraph offset.</returns>
- private double GetParagraphOffsetX(double width, double widthIncludingTrailingWhitespace)
- {
- if (double.IsPositiveInfinity(_paragraphWidth))
- {
- return 0;
- }
- var textAlignment = _paragraphProperties.TextAlignment;
- var paragraphFlowDirection = _paragraphProperties.FlowDirection;
- if (textAlignment == TextAlignment.Justify)
- {
- textAlignment = TextAlignment.Start;
- }
- switch (textAlignment)
- {
- case TextAlignment.Start:
- {
- textAlignment = paragraphFlowDirection == FlowDirection.LeftToRight ? TextAlignment.Left : TextAlignment.Right;
- break;
- }
- case TextAlignment.End:
- {
- textAlignment = paragraphFlowDirection == FlowDirection.RightToLeft ? TextAlignment.Left : TextAlignment.Right;
- break;
- }
- case TextAlignment.DetectFromContent:
- {
- textAlignment = _resolvedFlowDirection == FlowDirection.LeftToRight ? TextAlignment.Left : TextAlignment.Right;
- break;
- }
- }
- switch (textAlignment)
- {
- case TextAlignment.Center:
- var start = (_paragraphWidth - width) / 2;
- if (paragraphFlowDirection == FlowDirection.RightToLeft)
- {
- start -= (widthIncludingTrailingWhitespace - width);
- }
- return Math.Max(0, start);
- case TextAlignment.Right:
- return Math.Max(0, _paragraphWidth - widthIncludingTrailingWhitespace);
- default:
- return 0;
- }
- }
- private sealed class OrderedBidiRun
- {
- public OrderedBidiRun(DrawableTextRun run, sbyte level)
- {
- Run = run;
- Level = level;
- }
- public sbyte Level { get; }
- public DrawableTextRun Run { get; }
- public OrderedBidiRun? Next { get; set; }
- }
- private sealed class BidiRange
- {
- public int Level { get; set; }
- public OrderedBidiRun? Left { get; set; }
- public OrderedBidiRun? Right { get; set; }
- public BidiRange? Previous { get; set; }
- public static BidiRange MergeWithPrevious(BidiRange range)
- {
- var previous = range.Previous;
- BidiRange left;
- BidiRange right;
- if ((previous!.Level & 1) != 0)
- {
- // Odd, previous goes to the right of range.
- left = range;
- right = previous;
- }
- else
- {
- // Even, previous goes to the left of range.
- left = previous;
- right = range;
- }
- // Stitch them
- left.Right!.Next = right.Left;
- previous.Left = left.Left;
- previous.Right = right.Right;
- return previous;
- }
- }
- }
- }
|