Skip to content

Commit 33ef9a6

Browse files
committed
fix: avoid crash if fontfamily contains consecutive whitespace (#799)
1 parent 0a486a9 commit 33ef9a6

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/ViewModels/Preference.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text;
45
using System.Text.Json;
56
using System.Text.Json.Serialization;
67
using Avalonia.Collections;
@@ -65,8 +66,8 @@ public string DefaultFontFamily
6566
get => _defaultFontFamily;
6667
set
6768
{
68-
var trimmed = value.Trim();
69-
if (SetProperty(ref _defaultFontFamily, trimmed) && !_isLoading)
69+
var name = FixFontFamilyName(value);
70+
if (SetProperty(ref _defaultFontFamily, name) && !_isLoading)
7071
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
7172
}
7273
}
@@ -76,8 +77,8 @@ public string MonospaceFontFamily
7677
get => _monospaceFontFamily;
7778
set
7879
{
79-
var trimmed = value.Trim();
80-
if (SetProperty(ref _monospaceFontFamily, trimmed) && !_isLoading)
80+
var name = FixFontFamilyName(value);
81+
if (SetProperty(ref _monospaceFontFamily, name) && !_isLoading)
8182
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
8283
}
8384
}
@@ -588,6 +589,35 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
588589
return changed;
589590
}
590591

592+
private string FixFontFamilyName(string name)
593+
{
594+
var trimmed = name.Trim();
595+
if (string.IsNullOrEmpty(trimmed))
596+
return string.Empty;
597+
598+
var builder = new StringBuilder();
599+
var lastIsSpace = false;
600+
for (int i = 0; i < trimmed.Length; i++)
601+
{
602+
var c = trimmed[i];
603+
if (char.IsWhiteSpace(c))
604+
{
605+
if (lastIsSpace)
606+
continue;
607+
608+
lastIsSpace = true;
609+
}
610+
else
611+
{
612+
lastIsSpace = false;
613+
}
614+
615+
builder.Append(c);
616+
}
617+
618+
return builder.ToString();
619+
}
620+
591621
private static Preference _instance = null;
592622
private static bool _isLoading = false;
593623

0 commit comments

Comments
 (0)