Skip to content

Commit

Permalink
Revert breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dme-compunet committed Jan 19, 2025
1 parent 99fa99e commit 3bc4b41
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ public abstract class TextCollapsingProperties
public abstract FlowDirection FlowDirection { get; }

/// <summary>
/// Collapses given text runs.
/// Collapses given text line.
/// </summary>
/// <param name="textRuns">The text runs to collapse.</param>
public abstract TextRun[]? Collapse(TextRun[] textRuns);
/// <param name="textLine">Text line to collapse.</param>
public abstract TextRun[]? Collapse(TextLine textLine);

/// <summary>
/// Creates a list of runs for given collapsed length which includes specified symbol at the end.
/// </summary>
/// <param name="textRuns">The text runs</param>
/// <param name="textLine">The text line.</param>
/// <param name="collapsedLength">The collapsed length.</param>
/// <param name="flowDirection">The flow direction.</param>
/// <param name="shapedSymbol">The symbol.</param>
/// <returns>List of remaining runs.</returns>
public static TextRun[] CreateCollapsedRuns(TextRun[] textRuns, int collapsedLength, TextRun shapedSymbol)
public static TextRun[] CreateCollapsedRuns(TextLine textLine, int collapsedLength, FlowDirection flowDirection, TextRun shapedSymbol)
{
if (collapsedLength <= 0 || textRuns.Length == 0)
var textRuns = textLine is TextLineImpl line ? line.LogicalTextRuns : textLine.TextRuns;

if (collapsedLength <= 0 || textRuns.Count == 0)
{
return [shapedSymbol];
}
Expand Down
26 changes: 13 additions & 13 deletions src/Avalonia.Base/Media/TextFormatting/TextEllipsisHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace Avalonia.Media.TextFormatting
{
internal static class TextEllipsisHelper
{
public static TextRun[]? Collapse(TextRun[] textRuns, TextCollapsingProperties properties, bool isWordEllipsis)
public static TextRun[]? Collapse(TextLine textLine, TextCollapsingProperties properties, bool isWordEllipsis)
{
if (textRuns.Length == 0)
var textRuns = textLine is TextLineImpl line ? line.LogicalTextRuns : textLine.TextRuns;

if (textRuns.Count == 0)
{
return null;
}
Expand All @@ -19,18 +21,11 @@ internal static class TextEllipsisHelper
return [];
}

var textLength = 0;

for (var i = 0; i < textRuns.Length; i++)
{
textLength += textRuns[i].Length;
}

var availableWidth = properties.Width - shapedSymbol.Size.Width;

var collapsedLength = 0;

for (var i = 0; i < textRuns.Length; i++)
for (var i = 0; i < textRuns.Count; i++)
{
var currentRun = textRuns[i];

Expand All @@ -42,9 +37,14 @@ internal static class TextEllipsisHelper

if (textRunWidth > availableWidth)
{
if (shapedRun.IsReversed)
{
shapedRun.Reverse();
}

if (shapedRun.TryMeasureCharacters(availableWidth, out var measuredLength))
{
if (isWordEllipsis && measuredLength < textLength)
if (isWordEllipsis && measuredLength < textLine.Length)
{
var currentBreakPosition = 0;

Expand Down Expand Up @@ -73,7 +73,7 @@ internal static class TextEllipsisHelper

collapsedLength += measuredLength;

return TextCollapsingProperties.CreateCollapsedRuns(textRuns, collapsedLength, shapedSymbol);
return TextCollapsingProperties.CreateCollapsedRuns(textLine, collapsedLength, properties.FlowDirection, shapedSymbol);
}

availableWidth -= textRunWidth;
Expand All @@ -86,7 +86,7 @@ internal static class TextEllipsisHelper
//The whole run needs to fit into available space
if (drawableRun.Size.Width > availableWidth)
{
return TextCollapsingProperties.CreateCollapsedRuns(textRuns, collapsedLength, shapedSymbol);
return TextCollapsingProperties.CreateCollapsedRuns(textLine, collapsedLength, properties.FlowDirection, shapedSymbol);
}

availableWidth -= drawableRun.Size.Width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ public TextLeadingPrefixCharacterEllipsis(
public override FlowDirection FlowDirection { get; }

/// <inheritdoc />
public override TextRun[]? Collapse(TextRun[] textRuns)
public override TextRun[]? Collapse(TextLine textLine)
{
if (textRuns.Length == 0)
var textRuns = textLine is TextLineImpl line ? line.LogicalTextRuns : textLine.TextRuns;

for (var i = 0; i < textRuns.Count; i++)
{
return null;
if (textRuns[i] is ShapedTextRun { IsReversed: true } shapedRun)
{
shapedRun.Reverse();
}
}

var runIndex = 0;
Expand All @@ -67,7 +72,7 @@ public TextLeadingPrefixCharacterEllipsis(
// Prefix length run | Ellipsis symbol | Post split run growing from the end |
var availableWidth = Width - shapedSymbol.Size.Width;

while (runIndex < textRuns.Length)
while (runIndex < textRuns.Count)
{
var currentRun = textRuns[runIndex];

Expand Down
53 changes: 29 additions & 24 deletions src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class TextLineImpl : TextLine
Comparer<TextBounds>.Create((x, y) => x.Rectangle.Left.CompareTo(y.Rectangle.Left));

internal IReadOnlyList<IndexedTextRun>? _indexedTextRuns;
internal IReadOnlyList<TextRun>? _logicalTextRuns;
private readonly TextRun[] _textRuns;
private readonly double _paragraphWidth;
private readonly TextParagraphProperties _paragraphProperties;
Expand Down Expand Up @@ -98,6 +99,11 @@ public TextLineImpl(TextRun[] textRuns, int firstTextSourceIndex, int length, do
/// </summary>
internal Rect InkBounds => _inkBounds;

/// <summary>
/// Get the line text-runs in logical order
/// </summary>
internal IReadOnlyList<TextRun> LogicalTextRuns => GetLogicalTextRuns();

/// <inheritdoc/>
public override void Draw(DrawingContext drawingContext, Point lineOrigin)
{
Expand Down Expand Up @@ -170,30 +176,7 @@ public override TextLine Collapse(params TextCollapsingProperties?[] collapsingP
return this;
}

TextRun[] textRuns;

if (_indexedTextRuns == null || _indexedTextRuns.Count == 0)
{
textRuns = _textRuns;
}
else
{
textRuns = new TextRun[_indexedTextRuns.Count];

for (var i = 0; i < textRuns.Length; i++)
{
var textRun = _indexedTextRuns[i].TextRun;

if (textRun is ShapedTextRun { IsReversed: true } shapedTextRun)
{
shapedTextRun.Reverse();
}

textRuns[i] = textRun!;
}
}

var collapsedRuns = collapsingProperties.Collapse(textRuns);
var collapsedRuns = collapsingProperties.Collapse(this);

if (collapsedRuns is null)
{
Expand All @@ -211,6 +194,28 @@ public override TextLine Collapse(params TextCollapsingProperties?[] collapsingP
return collapsedLine;
}

private IReadOnlyList<TextRun> GetLogicalTextRuns()
{
if (_logicalTextRuns != null)
{
return _logicalTextRuns;
}

if (_indexedTextRuns == null || _indexedTextRuns.Count == 0)
{
return _textRuns;
}

var textRuns = new TextRun[_indexedTextRuns.Count];

for (var i = 0; i < _indexedTextRuns.Count; i++)
{
textRuns[i] = _indexedTextRuns[i].TextRun!;
}

return _logicalTextRuns = textRuns;
}

/// <inheritdoc/>
public override void Justify(JustificationProperties justificationProperties)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public TextTrailingCharacterEllipsis(string ellipsis, double width,
public override FlowDirection FlowDirection { get; }

/// <inheritdoc />
public override TextRun[]? Collapse(TextRun[] textRuns)
public override TextRun[]? Collapse(TextLine textLine)
{
return TextEllipsisHelper.Collapse(textRuns, this, false);
return TextEllipsisHelper.Collapse(textLine, this, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ FlowDirection flowDirection
public override FlowDirection FlowDirection { get; }

/// <inheritdoc />
public override TextRun[]? Collapse(TextRun[] textRuns)
public override TextRun[]? Collapse(TextLine textLine)
{
return TextEllipsisHelper.Collapse(textRuns, this, true);
return TextEllipsisHelper.Collapse(textLine, this, true);
}
}
}

0 comments on commit 3bc4b41

Please sign in to comment.