|
2 | 2 | using System.Buffers;
|
3 | 3 | using System.Collections;
|
4 | 4 | using System.Collections.Generic;
|
| 5 | +using System.Linq; |
5 | 6 | using System.Runtime.CompilerServices;
|
6 | 7 | using Avalonia.Utilities;
|
7 | 8 |
|
@@ -89,90 +90,110 @@ public GlyphInfo this[int index]
|
89 | 90 |
|
90 | 91 | public IEnumerator<GlyphInfo> GetEnumerator() => _glyphInfos.GetEnumerator();
|
91 | 92 |
|
| 93 | + internal void ResetBidiLevel(sbyte paragraphEmbeddingLevel) => BidiLevel = paragraphEmbeddingLevel; |
| 94 | + |
| 95 | + int IReadOnlyCollection<GlyphInfo>.Count => _glyphInfos.Length; |
| 96 | + |
| 97 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 98 | + |
92 | 99 | /// <summary>
|
93 |
| - /// Finds a glyph index for given character index. |
| 100 | + /// Splits the <see cref="TextRun"/> at specified length. |
94 | 101 | /// </summary>
|
95 |
| - /// <param name="characterIndex">The character index.</param> |
96 |
| - /// <returns> |
97 |
| - /// The glyph index. |
98 |
| - /// </returns> |
99 |
| - private int FindGlyphIndex(int characterIndex) |
| 102 | + /// <param name="textLength">The text length.</param> |
| 103 | + /// <returns>The split result.</returns> |
| 104 | + public SplitResult<ShapedBuffer> Split(int textLength) |
100 | 105 | {
|
101 |
| - if (characterIndex < _glyphInfos[0].GlyphCluster) |
| 106 | + // make sure we do not overshoot |
| 107 | + textLength = Math.Min(Text.Length, textLength); |
| 108 | + |
| 109 | + if (textLength <= 0) |
102 | 110 | {
|
103 |
| - return 0; |
| 111 | + var emptyBuffer = new ShapedBuffer( |
| 112 | + Text.Slice(0, 0), _glyphInfos.Slice(_glyphInfos.Start, 0), |
| 113 | + GlyphTypeface, FontRenderingEmSize, BidiLevel); |
| 114 | + |
| 115 | + return new SplitResult<ShapedBuffer>(emptyBuffer, this); |
104 | 116 | }
|
105 | 117 |
|
106 |
| - if (characterIndex > _glyphInfos[_glyphInfos.Length - 1].GlyphCluster) |
| 118 | + // nothing to split |
| 119 | + if (textLength == Text.Length) |
107 | 120 | {
|
108 |
| - return _glyphInfos.Length - 1; |
| 121 | + return new SplitResult<ShapedBuffer>(this, null); |
109 | 122 | }
|
110 | 123 |
|
111 |
| - var comparer = GlyphInfo.ClusterAscendingComparer; |
112 |
| - |
| 124 | + var sliceStart = _glyphInfos.Start; |
113 | 125 | var glyphInfos = _glyphInfos.Span;
|
| 126 | + var glyphInfosLength = _glyphInfos.Length; |
114 | 127 |
|
115 |
| - var searchValue = new GlyphInfo(default, characterIndex, default); |
| 128 | + // the first glyph’s cluster is our “zero” for this sub‐buffer. |
| 129 | + // we want an absolute target cluster = baseCluster + textLength |
| 130 | + var baseCluster = glyphInfos[0].GlyphCluster; |
| 131 | + var targetCluster = baseCluster + textLength; |
116 | 132 |
|
117 |
| - var start = glyphInfos.BinarySearch(searchValue, comparer); |
| 133 | + // binary‐search for a dummy with cluster == targetCluster |
| 134 | + var searchValue = new GlyphInfo(0, targetCluster, 0, default); |
| 135 | + var foundIndex = glyphInfos.BinarySearch(searchValue, GlyphInfo.ClusterAscendingComparer); |
118 | 136 |
|
119 |
| - if (start < 0) |
120 |
| - { |
121 |
| - while (characterIndex > 0 && start < 0) |
122 |
| - { |
123 |
| - characterIndex--; |
| 137 | + int splitGlyphIndex; // how many glyph‐slots go into "leading" |
| 138 | + int splitCharCount; // how many chars go into "leading" Text |
124 | 139 |
|
125 |
| - searchValue = new GlyphInfo(default, characterIndex, default); |
126 |
| - |
127 |
| - start = glyphInfos.BinarySearch(searchValue, comparer); |
128 |
| - } |
| 140 | + if (foundIndex >= 0) |
| 141 | + { |
| 142 | + // found a glyph info whose cluster == targetCluster |
| 143 | + // back up to the start of the cluster |
| 144 | + var i = foundIndex; |
129 | 145 |
|
130 |
| - if (start < 0) |
| 146 | + while (i > 0 && glyphInfos[i - 1].GlyphCluster == targetCluster) |
131 | 147 | {
|
132 |
| - return -1; |
| 148 | + i--; |
133 | 149 | }
|
| 150 | + |
| 151 | + splitGlyphIndex = i; |
| 152 | + splitCharCount = targetCluster - baseCluster; |
134 | 153 | }
|
135 |
| - |
136 |
| - while (start > 0 && glyphInfos[start - 1].GlyphCluster == glyphInfos[start].GlyphCluster) |
| 154 | + else |
137 | 155 | {
|
138 |
| - start--; |
139 |
| - } |
140 |
| - |
141 |
| - return start; |
142 |
| - } |
| 156 | + // no exact match need to invert so ~foundIndex is the insertion point |
| 157 | + // the first cluster > targetCluster |
| 158 | + var invertedIndex = ~foundIndex; |
143 | 159 |
|
144 |
| - /// <summary> |
145 |
| - /// Splits the <see cref="TextRun"/> at specified length. |
146 |
| - /// </summary> |
147 |
| - /// <param name="length">The length.</param> |
148 |
| - /// <returns>The split result.</returns> |
149 |
| - internal SplitResult<ShapedBuffer> Split(int length) |
150 |
| - { |
151 |
| - if (Text.Length == length) |
152 |
| - { |
153 |
| - return new SplitResult<ShapedBuffer>(this, null); |
| 160 | + if (invertedIndex >= glyphInfosLength) |
| 161 | + { |
| 162 | + // happens only if targetCluster ≥ lastCluster |
| 163 | + // put everything into leading |
| 164 | + splitGlyphIndex = glyphInfosLength; |
| 165 | + splitCharCount = Text.Length; |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + // snap to the start of that next cluster |
| 170 | + splitGlyphIndex = invertedIndex; |
| 171 | + var nextCluster = glyphInfos[invertedIndex].GlyphCluster; |
| 172 | + splitCharCount = nextCluster - baseCluster; |
| 173 | + } |
154 | 174 | }
|
155 | 175 |
|
156 |
| - var firstCluster = _glyphInfos[0].GlyphCluster; |
157 |
| - var lastCluster = _glyphInfos[_glyphInfos.Length - 1].GlyphCluster; |
| 176 | + var firstGlyphs = _glyphInfos.Slice(sliceStart, splitGlyphIndex); |
| 177 | + var secondGlyphs = _glyphInfos.Slice(sliceStart + splitGlyphIndex, glyphInfosLength - splitGlyphIndex); |
158 | 178 |
|
159 |
| - var start = firstCluster < lastCluster ? firstCluster : lastCluster; |
| 179 | + var firstText = Text.Slice(0, splitCharCount); |
| 180 | + var secondText = Text.Slice(splitCharCount); |
160 | 181 |
|
161 |
| - var glyphCount = FindGlyphIndex(start + length); |
| 182 | + var leading = new ShapedBuffer( |
| 183 | + firstText, firstGlyphs, |
| 184 | + GlyphTypeface, FontRenderingEmSize, BidiLevel); |
162 | 185 |
|
163 |
| - var first = new ShapedBuffer(Text.Slice(0, length), |
164 |
| - _glyphInfos.Take(glyphCount), GlyphTypeface, FontRenderingEmSize, BidiLevel); |
| 186 | + // this happens if we try to find a position inside a cluster and we moved to the end |
| 187 | + if(secondText.Length == 0) |
| 188 | + { |
| 189 | + return new SplitResult<ShapedBuffer>(leading, null); |
| 190 | + } |
165 | 191 |
|
166 |
| - var second = new ShapedBuffer(Text.Slice(length), |
167 |
| - _glyphInfos.Skip(glyphCount), GlyphTypeface, FontRenderingEmSize, BidiLevel); |
| 192 | + var trailing = new ShapedBuffer( |
| 193 | + secondText, secondGlyphs, |
| 194 | + GlyphTypeface, FontRenderingEmSize, BidiLevel); |
168 | 195 |
|
169 |
| - return new SplitResult<ShapedBuffer>(first, second); |
| 196 | + return new SplitResult<ShapedBuffer>(leading, trailing); |
170 | 197 | }
|
171 |
| - |
172 |
| - internal void ResetBidiLevel(sbyte paragraphEmbeddingLevel) => BidiLevel = paragraphEmbeddingLevel; |
173 |
| - |
174 |
| - int IReadOnlyCollection<GlyphInfo>.Count => _glyphInfos.Length; |
175 |
| - |
176 |
| - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
177 | 198 | }
|
178 | 199 | }
|
0 commit comments