Browse Source

Fix some comments and GlyphRun.GetDistanceFromCharacterHit

Benedikt Schroeder 6 years ago
parent
commit
7901f6f09f

+ 7 - 9
src/Avalonia.Visuals/Media/GlyphRun.cs

@@ -193,16 +193,15 @@ namespace Avalonia.Media
         {
             var distance = 0.0;
 
-            var end = _glyphClusters.AsSpan().BinarySearch((ushort)characterHit.FirstCharacterIndex);
+            var end = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
 
-            if (end < 0)
+            for (var i = 0; i < _glyphClusters.Length; i++)
             {
-                return 0;
-            }
+                if (_glyphClusters[i] >= end)
+                {
+                    break;
+                }
 
-            // If TrailingLength > 0 we have to use the next cluster while TrailingLength != 0
-            for (var i = 0; i < end + characterHit.TrailingLength; i++)
-            {
                 if (GlyphAdvances.IsEmpty)
                 {
                     var glyph = GlyphIndices[i];
@@ -279,7 +278,6 @@ namespace Avalonia.Media
 
         public CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit)
         {
-
             if (characterHit.TrailingLength == 0)
             {
                 return FindNearestCharacterHit(characterHit.FirstCharacterIndex, out _);
@@ -412,7 +410,7 @@ namespace Avalonia.Media
         {
             if (_glyphRunImpl != null)
             {
-                throw new InvalidOperationException("GlyphRun can't be changed after is has been initialized.'");
+                throw new InvalidOperationException("GlyphRun can't be changed after it has been initialized.'");
             }
 
             field = value;

+ 3 - 3
src/Avalonia.Visuals/Rendering/SceneGraph/GlyphRunNode.cs

@@ -10,7 +10,7 @@ using Avalonia.VisualTree;
 namespace Avalonia.Rendering.SceneGraph
 {
     /// <summary>
-    /// A node in the scene graph which represents a text draw.
+    /// A node in the scene graph which represents a glyph run draw.
     /// </summary>
     internal class GlyphRunNode : BrushDrawOperation
     {
@@ -48,7 +48,7 @@ namespace Avalonia.Rendering.SceneGraph
         public IBrush Foreground { get; }
 
         /// <summary>
-        /// Gets the text to draw.
+        /// Gets the glyph run to draw.
         /// </summary>
         public GlyphRun GlyphRun { get; }
 
@@ -72,7 +72,7 @@ namespace Avalonia.Rendering.SceneGraph
         /// </summary>
         /// <param name="transform">The transform of the other draw operation.</param>
         /// <param name="foreground">The foreground of the other draw operation.</param>
-        /// <param name="glyphRun">The text of the other draw operation.</param>
+        /// <param name="glyphRun">The glyph run of the other draw operation.</param>
         /// <returns>True if the draw operations are the same, otherwise false.</returns>
         /// <remarks>
         /// The properties of the other draw operation are passed in as arguments to prevent

+ 19 - 1
tests/Avalonia.Visuals.UnitTests/Media/GlyphRunTests.cs

@@ -13,12 +13,30 @@ namespace Avalonia.Visuals.UnitTests.Media
                 .Bind<IPlatformRenderInterface>().ToSingleton<MockPlatformRenderInterface>();
         }
 
+        [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 0, 0 }, 0, 0, 0)]
+        [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 0, 0 }, 0, 3, 30)]
+        [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 1, 2 }, 1, 0, 10)]
+        [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 1, 2 }, 2, 0, 20)]
+        [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 1, 2 }, 2, 1, 30)]
+        [Theory]
+        public void Should_Get_Distance_From_CharacterHit(double[] advances, ushort[] clusters, int start, int trailingLength, double expectedDistance)
+        {
+            using (var glyphRun = CreateGlyphRun(advances, clusters))
+            {
+                var characterHit = new CharacterHit(start, trailingLength);
+
+                var distance = glyphRun.GetDistanceFromCharacterHit(characterHit);
+
+                Assert.Equal(expectedDistance, distance);
+            }
+        }
+
         [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 0, 0 }, 25.0, 0, 3, true)]
         [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 1, 2 }, 20.0, 2, 0, true)]
         [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 1, 2 }, 26.0, 2, 1, true)]
         [InlineData(new double[] { 10, 10, 10 }, new ushort[] { 0, 1, 2 }, 35.0, 2, 1, false)]
         [Theory]
-        public void Should_Get_TextBounds_FromDistance(double[] advances, ushort[] clusters, double distance, int start,
+        public void Should_Get_CharacterHit_FromDistance(double[] advances, ushort[] clusters, double distance, int start,
             int trailingLengthExpected, bool isInsideExpected)
         {
             using (var glyphRun = CreateGlyphRun(advances, clusters))