1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-27 06:58:34 +01:00

Compare commits

...

2 commits

3 changed files with 22 additions and 5 deletions

View file

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

View file

@ -63,6 +63,18 @@ namespace MLEM.Font {
return (curr, 1); 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 <= 0 ? index + 1 : index - 1;
return index;
}
/// <summary>Returns an enumerator that iterates through the collection.</summary> /// <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> /// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
/// <filterpriority>1</filterpriority> /// <filterpriority>1</filterpriority>

View file

@ -105,7 +105,8 @@ namespace MLEM.Input {
set { set {
var val = (int) MathHelper.Clamp(value, 0F, this.text.Length); var val = (int) MathHelper.Clamp(value, 0F, this.text.Length);
if (this.caretPos != val) { 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.caretBlinkTimer = 0;
this.SetTextDataDirty(false); this.SetTextDataDirty(false);
} }
@ -360,7 +361,7 @@ namespace MLEM.Input {
if (!this.FilterText(ref strg, removeMismatching)) if (!this.FilterText(ref strg, removeMismatching))
return; return;
if (this.MaximumCharacters != null && strg.Length > this.MaximumCharacters) 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.Clear();
this.text.Append(strg); this.text.Append(strg);
this.CaretPos = this.text.Length; this.CaretPos = this.text.Length;
@ -378,7 +379,7 @@ namespace MLEM.Input {
if (!this.FilterText(ref strg, removeMismatching)) if (!this.FilterText(ref strg, removeMismatching))
return false; return false;
if (this.MaximumCharacters != null && this.text.Length + strg.Length > this.MaximumCharacters) 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.text.Insert(this.CaretPos, strg);
this.CaretPos += strg.Length; this.CaretPos += strg.Length;
this.SetTextDataDirty(); this.SetTextDataDirty();
@ -393,7 +394,8 @@ namespace MLEM.Input {
public bool RemoveText(int index, int length) { public bool RemoveText(int index, int length) {
if (index < 0 || index >= this.text.Length) if (index < 0 || index >= this.text.Length)
return false; 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 // ensure that caret pos is still in bounds
this.CaretPos = this.CaretPos; this.CaretPos = this.CaretPos;
this.SetTextDataDirty(); this.SetTextDataDirty();
@ -417,7 +419,7 @@ namespace MLEM.Input {
this.CaretPos = destStart + destAccum.Length; this.CaretPos = destStart + destAccum.Length;
return true; return true;
} }
destAccum += this.text[destStart + destAccum.Length]; destAccum += CodePointSource.ToString(new CodePointSource(this.text).GetCodePoint(destStart + destAccum.Length).CodePoint);
} }
// if we don't find a proper position, just move to the end of the destination line // if we don't find a proper position, just move to the end of the destination line
this.CaretPos = destEnd; this.CaretPos = destEnd;