12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Runtime.InteropServices;
- using Avalonia.Headless;
- using Avalonia.Media;
- using Avalonia.Media.TextFormatting;
- using Avalonia.Media.TextFormatting.Unicode;
- using Avalonia.UnitTests;
- using Avalonia.Utilities;
- using Xunit;
- namespace Avalonia.Skia.UnitTests.Media.TextFormatting
- {
- public class TextLayoutTests
- {
- private const string SingleLineText = "0123456789";
- private const string MultiLineText = "01 23 45 678\r\rabc def gh ij";
- private const string RightToLeftText = "זה כיף סתם לשמוע איך תנצח קרפד עץ טוב בגן";
- [InlineData("01234\r01234\r", 3)]
- [InlineData("01234\r01234", 2)]
- [Theory]
- public void Should_Break_Lines(string text, int numberOfLines)
- {
- using (Start())
- {
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black);
- Assert.Equal(numberOfLines, layout.TextLines.Count);
- }
- }
- [Fact]
- public void Should_Apply_TextStyleSpan_To_Text_In_Between()
- {
- using (Start())
- {
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(1, 2,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var layout = new TextLayout(
- MultiLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textStyleOverrides: spans);
- var textLine = layout.TextLines[0];
- Assert.Equal(3, textLine.TextRuns.Count);
- var textRun = textLine.TextRuns[1];
- Assert.Equal(2, textRun.Length);
- var actual = textRun.Text.ToString();
- Assert.Equal("1 ", actual);
- Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
- }
- }
- [InlineData(27)]
- [InlineData(22)]
- [Theory]
- public void Should_Wrap_And_Apply_Style(int length)
- {
- using (Start())
- {
- var text = "Multiline TextBox with TextWrapping.";
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var expected = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textWrapping: TextWrapping.Wrap,
- maxWidth: 200);
- var expectedLines = expected.TextLines.Select(x => text.Substring(x.FirstTextSourceIndex,
- x.Length)).ToList();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(0, length,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var actual = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textWrapping: TextWrapping.Wrap,
- maxWidth: 200,
- textStyleOverrides: spans);
- var actualLines = actual.TextLines.Select(x => text.Substring(x.FirstTextSourceIndex,
- x.Length)).ToList();
- Assert.Equal(expectedLines.Count, actualLines.Count);
- for (var j = 0; j < actual.TextLines.Count; j++)
- {
- var expectedText = expectedLines[j];
- var actualText = actualLines[j];
- Assert.Equal(expectedText, actualText);
- }
- }
- }
- [Fact]
- public void Should_Not_Alter_Lines_After_TextStyleSpan_Was_Applied()
- {
- using (Start())
- {
- const string text = "אחד !\ntwo !\nשְׁלוֹשָׁה !";
- var red = new SolidColorBrush(Colors.Red).ToImmutable();
- var black = Brushes.Black.ToImmutable();
- var expected = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- black,
- textWrapping: TextWrapping.Wrap);
- var expectedGlyphs = GetGlyphs(expected);
- var outer = new GraphemeEnumerator(text);
- var inner = new GraphemeEnumerator(text);
- var i = 0;
- var j = 0;
- while (true)
- {
- Grapheme grapheme;
- while (inner.MoveNext(out grapheme))
- {
- j += grapheme.Length;
- if (j + i > text.Length)
- {
- break;
- }
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(i, j,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: red))
- };
- var actual = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- black,
- textWrapping: TextWrapping.Wrap,
- textStyleOverrides: spans);
- var actualGlyphs = GetGlyphs(actual);
- Assert.Equal(expectedGlyphs.Count, actualGlyphs.Count);
- for (var k = 0; k < expectedGlyphs.Count; k++)
- {
- Assert.Equal(expectedGlyphs[k], actualGlyphs[k]);
- }
- }
- if (!outer.MoveNext(out grapheme))
- {
- break;
- }
- inner = new GraphemeEnumerator(text);
- i += grapheme.Length;
- }
- }
- static List<string> GetGlyphs(TextLayout textLayout)
- => textLayout.TextLines
- .Select(line => string.Join('|', line.TextRuns
- .Cast<ShapedTextRun>()
- .SelectMany(run => run.ShapedBuffer, (_, glyph) => glyph.GlyphIndex)))
- .ToList();
- }
- [Fact]
- public void Should_Apply_TextStyleSpan_To_Text_At_Start()
- {
- using (Start())
- {
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(0, 2,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var layout = new TextLayout(
- SingleLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textStyleOverrides: spans);
- var textLine = layout.TextLines[0];
- Assert.Equal(2, textLine.TextRuns.Count);
- var textRun = textLine.TextRuns[0];
- Assert.Equal(2, textRun.Length);
- var actual = SingleLineText[..textRun.Length];
- Assert.Equal("01", actual);
- Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
- }
- }
- [Fact]
- public void Should_Apply_TextStyleSpan_To_Text_At_End()
- {
- using (Start())
- {
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(8, 2,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground)),
- };
- var layout = new TextLayout(
- SingleLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textStyleOverrides: spans);
- var textLine = layout.TextLines[0];
- Assert.Equal(2, textLine.TextRuns.Count);
- var textRun = textLine.TextRuns[1];
- Assert.Equal(2, textRun.Length);
- var actual = textRun.Text.ToString();
- Assert.Equal("89", actual);
- Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
- }
- }
- [Fact]
- public void Should_Apply_TextStyleSpan_To_Single_Character()
- {
- using (Start())
- {
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(0, 1,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var layout = new TextLayout(
- "0",
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textStyleOverrides: spans);
- var textLine = layout.TextLines[0];
- Assert.Equal(1, textLine.TextRuns.Count);
- var textRun = textLine.TextRuns[0];
- Assert.Equal(1, textRun.Length);
- Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
- }
- }
- [Fact]
- public void Should_Apply_TextSpan_To_Unicode_String_In_Between()
- {
- using (Start())
- {
- const string text = "😄😄😄😄";
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(2, 2,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textStyleOverrides: spans);
- var textLine = layout.TextLines[0];
- Assert.Equal(3, textLine.TextRuns.Count);
- var textRun = textLine.TextRuns[1];
- Assert.Equal(2, textRun.Length);
- var actual = textRun.Text.ToString();
- Assert.Equal("😄", actual);
- Assert.Equal(foreground, textRun.Properties.ForegroundBrush);
- }
- }
- [Fact]
- public void TextLength_Should_Be_Equal_To_TextLine_Length_Sum()
- {
- using (Start())
- {
- var layout = new TextLayout(
- MultiLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- Assert.Equal(MultiLineText.Length, layout.TextLines.Sum(x => x.Length));
- }
- }
- [Fact]
- public void TextLength_Should_Be_Equal_To_TextRun_TextLength_Sum()
- {
- using (Start())
- {
- var layout = new TextLayout(
- MultiLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- Assert.Equal(
- MultiLineText.Length,
- layout.TextLines.Select(textLine =>
- textLine.TextRuns.Sum(textRun => textRun.Length))
- .Sum());
- }
- }
- [Fact]
- public void TextLength_Should_Be_Equal_To_TextRun_TextLength_Sum_After_Wrap_With_Style_Applied()
- {
- using (Start())
- {
- const string text =
- "Multiline TextBox with TextWrapping.\r\rLorem ipsum dolor sit amet";
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(0, 24,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textWrapping: TextWrapping.Wrap,
- maxWidth: 180,
- textStyleOverrides: spans);
- Assert.Equal(
- text.Length,
- layout.TextLines.Select(textLine =>
- textLine.TextRuns.Sum(textRun => textRun.Length))
- .Sum());
- }
- }
- [Fact]
- public void Should_Apply_TextStyleSpan_To_MultiLine()
- {
- using (Start())
- {
- var foreground = new SolidColorBrush(Colors.Red).ToImmutable();
- var spans = new[]
- {
- new ValueSpan<TextRunProperties>(5, 20,
- new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
- };
- var layout = new TextLayout(
- MultiLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- maxWidth: 200,
- maxHeight: 125,
- textStyleOverrides: spans);
- Assert.Equal(foreground, layout.TextLines[0].TextRuns[1].Properties.ForegroundBrush);
- Assert.Equal(foreground, layout.TextLines[1].TextRuns[0].Properties.ForegroundBrush);
- Assert.Equal(foreground, layout.TextLines[2].TextRuns[0].Properties.ForegroundBrush);
- }
- }
- [Fact]
- public void Should_Hit_Test_SurrogatePair()
- {
- using (Start())
- {
- const string text = "😄😄";
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- var shapedRun = (ShapedTextRun)layout.TextLines[0].TextRuns[0];
- var glyphRun = shapedRun.GlyphRun;
- var width = glyphRun.Bounds.Width;
- var characterHit = glyphRun.GetCharacterHitFromDistance(width, out _);
- Assert.Equal(2, characterHit.FirstCharacterIndex);
- Assert.Equal(2, characterHit.TrailingLength);
- }
- }
- [Theory]
- [InlineData("☝🏿", new int[] { 0 })]
- [InlineData("☝🏿 ab", new int[] { 0, 3, 4, 5 })]
- [InlineData("ab ☝🏿", new int[] { 0, 1, 2, 3 })]
- public void Should_Create_Valid_Clusters_For_Text(string text, int[] clusters)
- {
- using (Start())
- {
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- var textLine = layout.TextLines[0];
- var index = 0;
- foreach (var textRun in textLine.TextRuns)
- {
- var shapedRun = (ShapedTextRun)textRun;
- var glyphClusters = shapedRun.ShapedBuffer.Select(glyph => glyph.GlyphCluster).ToArray();
- var expected = clusters.Skip(index).Take(glyphClusters.Length).ToArray();
- Assert.Equal(expected, glyphClusters);
- index += glyphClusters.Length;
- }
- }
- }
- [Theory]
- [InlineData("abcde\r\n", 7)] // Carriage Return + Line Feed
- [InlineData("abcde\u000A", 6)] // Line Feed
- [InlineData("abcde\u000B", 6)] // Vertical Tab
- [InlineData("abcde\u000C", 6)] // Form Feed
- [InlineData("abcde\u000D", 6)] // Carriage Return
- public void Should_Break_With_BreakChar(string text, int expectedLength)
- {
- using (Start())
- {
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- Assert.Equal(2, layout.TextLines.Count);
- Assert.Equal(1, layout.TextLines[0].TextRuns.Count);
- Assert.Equal(expectedLength, ((ShapedTextRun)layout.TextLines[0].TextRuns[0]).GlyphRun.GlyphInfos.Count);
- Assert.Equal(5, ((ShapedTextRun)layout.TextLines[0].TextRuns[0]).ShapedBuffer[5].GlyphCluster);
- if (expectedLength == 7)
- {
- Assert.Equal(5, ((ShapedTextRun)layout.TextLines[0].TextRuns[0]).ShapedBuffer[6].GlyphCluster);
- }
- }
- }
- [Fact]
- public void Should_Have_One_Run_With_Common_Script()
- {
- using (Start())
- {
- var layout = new TextLayout(
- "abcde\r\n",
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- Assert.Equal(1, layout.TextLines[0].TextRuns.Count);
- }
- }
- [Fact]
- public void Should_Layout_Corrupted_Text()
- {
- using (Start())
- {
- var text = new string(new[] { '\uD802', '\uD802', '\uD802', '\uD802', '\uD802', '\uD802', '\uD802' });
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12,
- Brushes.Black.ToImmutable());
- var textLine = layout.TextLines[0];
- var textRun = (ShapedTextRun)textLine.TextRuns[0];
- Assert.Equal(7, textRun.Length);
- var replacementGlyph = Typeface.Default.GlyphTypeface.GetGlyph(Codepoint.ReplacementCodepoint);
- foreach (var glyphInfo in textRun.GlyphRun.GlyphInfos)
- {
- Assert.Equal(replacementGlyph, glyphInfo.GlyphIndex);
- }
- }
- }
- [InlineData("0123456789\r0123456789")]
- [InlineData("0123456789")]
- [Theory]
- public void Should_Include_First_Line_When_Constraint_Is_Surpassed(string text)
- {
- using (Start())
- {
- var glyphTypeface = Typeface.Default.GlyphTypeface;
- var emHeight = glyphTypeface.Metrics.DesignEmHeight;
- var lineHeight = glyphTypeface.Metrics.LineSpacing * (12.0 / emHeight);
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12,
- Brushes.Black.ToImmutable(),
- maxHeight: lineHeight - lineHeight * 0.5);
- Assert.Equal(1, layout.TextLines.Count);
- Assert.Equal(lineHeight, layout.Height);
- }
- }
- [InlineData("0123456789\r\n0123456789\r\n0123456789", 0, 3)]
- [InlineData("0123456789\r\n0123456789\r\n0123456789", 1, 1)]
- [InlineData("0123456789\r\n0123456789\r\n0123456789", 4, 3)]
- [Theory]
- public void Should_Not_Exceed_MaxLines(string text, int maxLines, int expectedLines)
- {
- using (Start())
- {
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12,
- Brushes.Black,
- maxWidth: 50,
- maxLines: maxLines);
- Assert.Equal(expectedLines, layout.TextLines.Count);
- }
- }
- [Fact]
- public void Should_Produce_Fixed_Height_Lines()
- {
- using (Start())
- {
- var layout = new TextLayout(
- MultiLineText,
- Typeface.Default,
- 12,
- Brushes.Black,
- lineHeight: 50);
- foreach (var line in layout.TextLines)
- {
- Assert.Equal(50, line.Height);
- }
- }
- }
- private const string Text = "日本でTest一番読まれている英字新聞・ジャパンタイムズが発信する国内外ニュースと、様々なジャンルの特集記事。";
- [Fact(Skip = "Only used for profiling.")]
- public void Should_Wrap()
- {
- using (Start())
- {
- for (var i = 0; i < 2000; i++)
- {
- var layout = new TextLayout(
- Text,
- Typeface.Default,
- 12,
- Brushes.Black,
- textWrapping: TextWrapping.Wrap,
- maxWidth: 50);
- }
- }
- }
- [Fact]
- public void Should_Process_Multiple_NewLines_Properly()
- {
- using (Start())
- {
- var text = "123\r\n\r\n456\r\n\r\n";
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black);
- Assert.Equal(5, layout.TextLines.Count);
- Assert.Equal("123\r\n", layout.TextLines[0].TextRuns[0].Text.ToString());
- Assert.Equal("\r\n", layout.TextLines[1].TextRuns[0].Text.ToString());
- Assert.Equal("456\r\n", layout.TextLines[2].TextRuns[0].Text.ToString());
- Assert.Equal("\r\n", layout.TextLines[3].TextRuns[0].Text.ToString());
- }
- }
- [Fact]
- public void Should_Wrap_Min_OneCharacter_EveryLine()
- {
- using (Start())
- {
- var layout = new TextLayout(
- SingleLineText,
- Typeface.Default,
- 12,
- Brushes.Black,
- textWrapping: TextWrapping.Wrap,
- maxWidth: 3);
- //every character should be new line as there not enough space for even one character
- Assert.Equal(SingleLineText.Length, layout.TextLines.Count);
- }
- }
- [Fact]
- public void Should_HitTestTextRange_RightToLeft()
- {
- using (Start())
- {
- const int start = 0;
- const int length = 10;
- var layout = new TextLayout(
- RightToLeftText,
- Typeface.Default,
- 12,
- Brushes.Black);
- var selectedText = new TextLayout(
- RightToLeftText.Substring(start, length),
- Typeface.Default,
- 12,
- Brushes.Black);
- var rects = layout.HitTestTextRange(start, length).ToArray();
- Assert.Equal(1, rects.Length);
- var selectedRect = rects[0];
- Assert.Equal(selectedText.WidthIncludingTrailingWhitespace, selectedRect.Width, 2);
- }
- }
- [Fact]
- public void Should_HitTestTextRange_BiDi()
- {
- const string text = "זה כיףabcDEFזה כיף";
- using (Start())
- {
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- var textLine = layout.TextLines[0];
- var start = textLine.GetDistanceFromCharacterHit(new CharacterHit(5, 1));
- var end = textLine.GetDistanceFromCharacterHit(new CharacterHit(6, 1));
- var rects = layout.HitTestTextRange(0, 7).ToArray();
- Assert.Equal(1, rects.Length);
- var expected = rects[0];
- Assert.Equal(expected.Left, start);
- Assert.Equal(expected.Right, end);
- }
- }
- [Fact]
- public void Should_HitTestTextRange()
- {
- using (Start())
- {
- var layout = new TextLayout(
- SingleLineText,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable());
- var lineRects = layout.HitTestTextRange(0, SingleLineText.Length).ToList();
- Assert.Equal(layout.TextLines.Count, lineRects.Count);
- for (var i = 0; i < layout.TextLines.Count; i++)
- {
- var textLine = layout.TextLines[i];
- var rect = lineRects[i];
- Assert.Equal(textLine.WidthIncludingTrailingWhitespace, rect.Width);
- }
- var rects = layout.TextLines
- .SelectMany(x => x.TextRuns.Cast<ShapedTextRun>())
- .SelectMany(x => x.ShapedBuffer, (_, glyph) => glyph.GlyphAdvance)
- .ToArray();
- for (var i = 0; i < SingleLineText.Length; i++)
- {
- for (var j = 1; i + j < SingleLineText.Length; j++)
- {
- var expected = rects.AsSpan(i, j).ToArray().Sum();
- var actual = layout.HitTestTextRange(i, j).Sum(x => x.Width);
- Assert.Equal(expected, actual);
- }
- }
- }
- }
- [Fact]
- public void Should_Wrap_RightToLeft()
- {
- const string text =
- "يَجِبُ عَلَى الإنْسَانِ أن يَكُونَ أمِيْنَاً وَصَادِقَاً مَعَ نَفْسِهِ وَمَعَ أَهْلِهِ وَجِيْرَانِهِ وَأَنْ يَبْذُلَ كُلَّ جُهْدٍ فِي إِعْلاءِ شَأْنِ الوَطَنِ وَأَنْ يَعْمَلَ عَلَى مَا يَجْلِبُ السَّعَادَةَ لِلنَّاسِ . ولَن يَتِمَّ لَهُ ذلِك إِلا بِأَنْ يُقَدِّمَ المَنْفَعَةَ العَامَّةَ عَلَى المَنْفَعَةِ الخَاصَّةِ وَهذَا مِثَالٌ لِلتَّضْحِيَةِ .";
- using (Start())
- {
- for (var maxWidth = 366; maxWidth < 900; maxWidth += 33)
- {
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12.0f,
- Brushes.Black.ToImmutable(),
- textWrapping: TextWrapping.Wrap,
- flowDirection: FlowDirection.RightToLeft,
- maxWidth: maxWidth);
- foreach (var textLine in layout.TextLines)
- {
- Assert.True(textLine.Width <= maxWidth);
- var actual = new string(textLine.TextRuns.Cast<ShapedTextRun>()
- .OrderBy(x => TextTestHelper.GetStartCharIndex(x.Text))
- .SelectMany(x => x.Text.ToString())
- .ToArray());
- var expected = text.Substring(textLine.FirstTextSourceIndex, textLine.Length);
- Assert.Equal(expected, actual);
- }
- }
- }
- }
- [Fact]
- public void Should_Layout_Empty_String()
- {
- using (Start())
- {
- var layout = new TextLayout(
- string.Empty,
- Typeface.Default,
- 12,
- Brushes.Black);
- Assert.True(layout.Height > 0);
- }
- }
- [Fact]
- public void Should_HitTestPoint_RightToLeft()
- {
- using (Start())
- {
- var text = "אאא AAA";
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12,
- Brushes.Black,
- flowDirection: FlowDirection.RightToLeft);
- var firstRun = layout.TextLines[0].TextRuns[0] as ShapedTextRun;
- var hit = layout.HitTestPoint(new Point());
- Assert.Equal(4, hit.TextPosition);
- var currentX = 0.0;
- for (var i = 0; i < firstRun.GlyphRun.GlyphInfos.Count; i++)
- {
- var cluster = firstRun.GlyphRun.GlyphInfos[i].GlyphCluster;
- var advance = firstRun.GlyphRun.GlyphInfos[i].GlyphAdvance;
- hit = layout.HitTestPoint(new Point(currentX, 0));
- Assert.Equal(cluster, hit.TextPosition);
- var hitRange = layout.HitTestTextRange(hit.TextPosition, 1);
- var distance = hitRange.First().Left;
- Assert.Equal(currentX, distance, 2);
- currentX += advance;
- }
- var secondRun = layout.TextLines[0].TextRuns[1] as ShapedTextRun;
- hit = layout.HitTestPoint(new Point(firstRun.Size.Width, 0));
- Assert.Equal(7, hit.TextPosition);
- hit = layout.HitTestPoint(new Point(layout.TextLines[0].WidthIncludingTrailingWhitespace, 0));
- Assert.Equal(0, hit.TextPosition);
- currentX = firstRun.Size.Width + 0.5;
- for (var i = 0; i < secondRun.GlyphRun.GlyphInfos.Count; i++)
- {
- var cluster = secondRun.GlyphRun.GlyphInfos[i].GlyphCluster;
- var advance = secondRun.GlyphRun.GlyphInfos[i].GlyphAdvance;
- hit = layout.HitTestPoint(new Point(currentX, 0));
- Assert.Equal(cluster, hit.CharacterHit.FirstCharacterIndex);
- var hitRange = layout.HitTestTextRange(hit.CharacterHit.FirstCharacterIndex, hit.CharacterHit.TrailingLength);
- var distance = hitRange.First().Left + 0.5;
- Assert.Equal(currentX, distance, 2);
- currentX += advance;
- }
- }
- }
- [Fact]
- public void Should_Get_CharacterHit_From_Distance_RTL()
- {
- using (Start())
- {
- var text = "أَبْجَدِيَّة عَرَبِيَّة";
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12,
- Brushes.Black);
- var textLine = layout.TextLines[0];
- var firstRun = (ShapedTextRun)textLine.TextRuns[0];
- var firstCluster = firstRun.ShapedBuffer[0].GlyphCluster;
- var characterHit = textLine.GetCharacterHitFromDistance(0);
- Assert.Equal(firstCluster, characterHit.FirstCharacterIndex);
- Assert.Equal(text.Length, characterHit.FirstCharacterIndex + characterHit.TrailingLength);
- var distance = textLine.GetDistanceFromCharacterHit(characterHit);
- Assert.Equal(0, distance);
- distance = textLine.GetDistanceFromCharacterHit(new CharacterHit(characterHit.FirstCharacterIndex));
- var firstAdvance = firstRun.ShapedBuffer[0].GlyphAdvance;
- Assert.Equal(firstAdvance, distance, 5);
- var rect = layout.HitTestTextPosition(22);
- Assert.Equal(firstAdvance, rect.Left, 5);
- rect = layout.HitTestTextPosition(23);
- Assert.Equal(0, rect.Left, 5);
- }
- }
- [Fact]
- public void Should_Get_CharacterHit_From_Distance_RTL_With_TextStyles()
- {
- using (Start())
- {
- var text = "أَبْجَدِيَّة عَرَبِيَّة";
- var i = 0;
- var graphemeEnumerator = new GraphemeEnumerator(text);
- while (graphemeEnumerator.MoveNext(out var grapheme))
- {
- var textStyleOverrides = new[] { new ValueSpan<TextRunProperties>(i, grapheme.Length, new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: Brushes.Red)) };
- i += grapheme.Length;
- var layout = new TextLayout(
- text,
- Typeface.Default,
- 12,
- Brushes.Black,
- textStyleOverrides: textStyleOverrides);
- var textLine = layout.TextLines[0];
- var shapedRuns = textLine.TextRuns.Cast<ShapedTextRun>().ToList();
- var clusters = shapedRuns.SelectMany(x => x.ShapedBuffer, (_, glyph) => glyph.GlyphCluster).ToList();
- var glyphAdvances = shapedRuns.SelectMany(x => x.ShapedBuffer, (_, glyph) => glyph.GlyphAdvance).ToList();
- var currentX = 0.0;
- var cluster = text.Length;
- for (int j = 0; j < clusters.Count - 1; j++)
- {
- var glyphAdvance = glyphAdvances[j];
- var characterHit = textLine.GetCharacterHitFromDistance(currentX);
- Assert.Equal(cluster, characterHit.FirstCharacterIndex + characterHit.TrailingLength);
- var distance = textLine.GetDistanceFromCharacterHit(new CharacterHit(cluster));
- Assert.Equal(currentX, distance, 5);
- currentX += glyphAdvance;
- if(glyphAdvance > 0)
- {
- cluster = clusters[j];
- }
- }
- }
- }
- }
- [InlineData("mgfg🧐df f sdf", "g🧐d", 20, 40)]
- [InlineData("وه. وقد تعرض لانتقادات", "دات", 5, 30)]
- [InlineData("وه. وقد تعرض لانتقادات", "تعرض", 20, 50)]
- [InlineData(" علمية 😱ومضللة ،", " علمية 😱ومضللة ،", 40, 100)]
- [InlineData("في عام 2018 ، رفعت ل", "في عام 2018 ، رفعت ل", 100, 120)]
- [Theory]
- public void HitTestTextRange_Range_ValidLength(string text, string textToSelect, double minWidth, double maxWidth)
- {
- using (Start())
- {
- var layout = new TextLayout(text, Typeface.Default, 12, Brushes.Black);
- var start = text.IndexOf(textToSelect);
- var selectionRectangles = layout.HitTestTextRange(start, textToSelect.Length);
- Assert.Equal(1, selectionRectangles.Count());
- var rect = selectionRectangles.First();
- Assert.InRange(rect.Width, minWidth, maxWidth);
- }
- }
- [InlineData("012🧐210", 2, 4, FlowDirection.LeftToRight, "14.40234375,40.8046875")]
- [InlineData("210🧐012", 2, 4, FlowDirection.RightToLeft, "0,7.201171875;21.603515625,33.603515625;48.005859375,55.20703125")]
- [InlineData("שנב🧐שנב", 2, 4, FlowDirection.LeftToRight, "11.268,38.208")]
- [InlineData("שנב🧐שנב", 2, 4, FlowDirection.RightToLeft, "11.268,38.208")]
- [Theory]
- public void Should_HitTestTextRangeBetweenRuns(string text, int start, int length,
- FlowDirection flowDirection, string expected)
- {
- using (Start())
- {
- var expectedRects = expected.Split(';').Select(x =>
- {
- var startEnd = x.Split(',');
- var start = double.Parse(startEnd[0], CultureInfo.InvariantCulture);
- var end = double.Parse(startEnd[1], CultureInfo.InvariantCulture);
- return new Rect(start, 0, end - start, 0);
- }).ToArray();
- var textLayout = new TextLayout(text, Typeface.Default, 12, Brushes.Black, flowDirection: flowDirection);
- var rects = textLayout.HitTestTextRange(start, length).ToArray();
- Assert.Equal(expectedRects.Length, rects.Length);
- var endX = textLayout.TextLines[0].GetDistanceFromCharacterHit(new CharacterHit(2));
- var startX = textLayout.TextLines[0].GetDistanceFromCharacterHit(new CharacterHit(5, 1));
- for (int i = 0; i < expectedRects.Length; i++)
- {
- var expectedRect = expectedRects[i];
- Assert.Equal(expectedRect.Left, rects[i].Left, 2);
- Assert.Equal(expectedRect.Right, rects[i].Right, 2);
- }
- }
- }
- [Fact]
- public void Should_HitTestTextRangeWithLineBreaks()
- {
- using (Start())
- {
- var beforeLinebreak = "Line before linebreak";
- var afterLinebreak = "Line after linebreak";
- var text = beforeLinebreak + Environment.NewLine + "" + Environment.NewLine + afterLinebreak;
- var textLayout = new TextLayout(text, Typeface.Default, 12, Brushes.Black);
- var end = text.Length - afterLinebreak.Length + 1;
- var rects = textLayout.HitTestTextRange(0, end).ToArray();
- Assert.Equal(3, rects.Length);
- var endX = textLayout.TextLines[2].GetDistanceFromCharacterHit(new CharacterHit(end));
- //First character should be covered
- Assert.Equal(7.201171875, endX, 2);
- }
- }
- [Fact]
- public void Should_HitTestTextPosition_EndOfLine_RTL()
- {
- var text = "גש\r\n";
- using (Start())
- {
- var textLayout = new TextLayout(text, Typeface.Default, 12, Brushes.Black, flowDirection: FlowDirection.RightToLeft);
- var rect = textLayout.HitTestTextPosition(text.Length);
- Assert.Equal(16.32, rect.Top);
- }
- }
- [Win32Fact("Font only available on Windows")]
- public void Should_Handle_TextStyle_With_Ligature()
- {
- using (Start())
- {
- var text = "fi";
- var typeface = new Typeface("Calibri");
- var textLayout = new TextLayout(text, typeface, 12, Brushes.Black,
- textStyleOverrides: new[]
- {
- new ValueSpan<TextRunProperties>(1, 1,
- new GenericTextRunProperties(typeface, foregroundBrush: Brushes.White))
- });
- Assert.NotNull(textLayout);
- }
- }
- [Fact]
- public void Should_Measure_TextLayoutSymbolWithAndWidthIncludingTrailingWhitespace()
- {
- const string symbolsFont = "resm:Avalonia.Skia.UnitTests.Fonts?assembly=Avalonia.Skia.UnitTests#Symbols";
- using (Start())
- {
- var textLayout = new TextLayout("\ue971", new Typeface(symbolsFont), 12.0, Brushes.White);
- Assert.Equal(new Size(12.0, 12.0), new Size(textLayout.Width, textLayout.Height));
- Assert.Equal(12.0, textLayout.WidthIncludingTrailingWhitespace);
- }
- }
- [Fact]
- public void Should_Wrap_With_LineEnd()
- {
- using (Start())
- {
- var defaultProperties =
- new GenericTextRunProperties(Typeface.Default, 72, foregroundBrush: Brushes.Black);
- var paragraphProperties = new GenericTextParagraphProperties(defaultProperties, textWrapping: TextWrapping.Wrap);
- var textLayout = new TextLayout(new SingleBufferTextSource("01", defaultProperties, true), paragraphProperties, maxWidth: 36);
- Assert.Equal(2, textLayout.TextLines.Count);
- var lastLine = textLayout.TextLines.Last();
- Assert.Equal(2, lastLine.TextRuns.Count);
- var lastRun = lastLine.TextRuns.Last();
- Assert.IsAssignableFrom<TextEndOfLine>(lastRun);
- }
- }
- [Fact]
- public void Should_Measure_TextLayoutSymbolWithAndWidthIncludingTrailingWhitespaceAndMinTextWidth()
- {
- using (Start())
- {
- var typeFace = new Typeface("Courier New");
- var textLayout0 = new TextLayout("aaaa", typeFace, 12.0, Brushes.White);
- Assert.Equal(textLayout0.WidthIncludingTrailingWhitespace, textLayout0.Width);
- var textLayout01 = new TextLayout("a a", typeFace, 12.0, Brushes.White);
- var textLayout1 = new TextLayout("a a ", typeFace, 12.0, Brushes.White);
- Assert.Equal(new Size(textLayout0.Width, textLayout0.Height), new Size(textLayout1.WidthIncludingTrailingWhitespace, textLayout1.Height));
- Assert.Equal(textLayout0.WidthIncludingTrailingWhitespace, textLayout1.WidthIncludingTrailingWhitespace);
- var textLayout2 = new TextLayout(" aa ", typeFace, 12.0, Brushes.White);
- Assert.Equal(new Size(textLayout1.Width, textLayout1.Height), new Size(textLayout2.Width, textLayout2.Height));
- Assert.Equal(textLayout0.WidthIncludingTrailingWhitespace, textLayout2.WidthIncludingTrailingWhitespace);
- Assert.Equal(textLayout01.Width, textLayout2.Width);
-
- var textLayout3 = new TextLayout(" ", typeFace, 12.0, Brushes.White);
- Assert.Equal(new Size(0, textLayout0.Height), new Size(textLayout3.Width, textLayout3.Height));
- Assert.Equal(textLayout0.WidthIncludingTrailingWhitespace, textLayout3.WidthIncludingTrailingWhitespace);
- Assert.Equal(0, textLayout3.Width);
- }
- }
-
- private static void AssertGreaterThan(double x, double y, string message) => Assert.True(x > y, $"{message}. {x} is not > {y}");
- private static IDisposable Start()
- {
- var disposable = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
- .With(renderInterface: new PlatformRenderInterface(null),
- textShaperImpl: new TextShaperImpl(),
- fontManagerImpl: new CustomFontManagerImpl()));
- return disposable;
- }
- }
- }
|