1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-10 03:28:43 +02:00

Fixed TextInput not working correctly when using surrogate pairs

This commit is contained in:
Ell 2023-07-17 15:20:36 +02:00
parent 50da081be9
commit fda22de83d
3 changed files with 21 additions and 4 deletions

View file

@ -17,6 +17,9 @@ Jump to version:
Additions
- Added GraphicsExtensions.WithRenderTargets, a multi-target version of WithRenderTarget
Fixes
- Fixed TextInput not working correctly when using surrogate pairs
## 6.2.0
### MLEM

View file

@ -63,6 +63,18 @@ namespace MLEM.Font {
return (curr, 1);
}
/// <summary>
/// Returns an index in this code point source that is as close to <paramref name="index"/> as possible, but not between two members of a surrogate pair. If the <paramref name="index"/> is already not between surrogate pairs, it is returned unchanged.
/// </summary>
/// <param name="index">The index to ensure is not between surrogates.</param>
/// <param name="increase">Whether the returned index should be increased by 1 (instead of decreased by 1) when it is between surrogates.</param>
/// <returns>An index close to <paramref name="index"/>, but not between surrogates.</returns>
public int EnsureSurrogateBoundary(int index, bool increase) {
if (index < this.Length && char.IsLowSurrogate(this[index]))
return increase && index < this.Length - 1 || index <= 0 ? index + 1 : index - 1;
return index;
}
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
/// <filterpriority>1</filterpriority>

View file

@ -105,7 +105,8 @@ namespace MLEM.Input {
set {
var val = (int) MathHelper.Clamp(value, 0F, this.text.Length);
if (this.caretPos != val) {
this.caretPos = val;
// ensure that we don't move to a location that is between high and low surrogates
this.caretPos = new CodePointSource(this.text).EnsureSurrogateBoundary(val, val > this.caretPos);
this.caretBlinkTimer = 0;
this.SetTextDataDirty(false);
}
@ -360,7 +361,7 @@ namespace MLEM.Input {
if (!this.FilterText(ref strg, removeMismatching))
return;
if (this.MaximumCharacters != null && strg.Length > this.MaximumCharacters)
strg = strg.Substring(0, this.MaximumCharacters.Value);
strg = strg.Substring(0, new CodePointSource(strg).EnsureSurrogateBoundary(this.MaximumCharacters.Value, false));
this.text.Clear();
this.text.Append(strg);
this.CaretPos = this.text.Length;
@ -378,7 +379,7 @@ namespace MLEM.Input {
if (!this.FilterText(ref strg, removeMismatching))
return false;
if (this.MaximumCharacters != null && this.text.Length + strg.Length > this.MaximumCharacters)
strg = strg.Substring(0, this.MaximumCharacters.Value - this.text.Length);
strg = strg.Substring(0, new CodePointSource(strg).EnsureSurrogateBoundary(this.MaximumCharacters.Value - this.text.Length, false));
this.text.Insert(this.CaretPos, strg);
this.CaretPos += strg.Length;
this.SetTextDataDirty();
@ -393,7 +394,8 @@ namespace MLEM.Input {
public bool RemoveText(int index, int length) {
if (index < 0 || index >= this.text.Length)
return false;
this.text.Remove(index, length);
var source = new CodePointSource(this.text);
this.text.Remove(source.EnsureSurrogateBoundary(index, false), source.EnsureSurrogateBoundary(index + length, true) - index);
// ensure that caret pos is still in bounds
this.CaretPos = this.CaretPos;
this.SetTextDataDirty();