Optimize Font.FromName parser
For some fonts a valid registered name can include the font settings (weight, size, etc.). The name parser should try to validate the full name including provided settings first.
This commit is contained in:
Родитель
08c400ae34
Коммит
68d967199f
|
@ -131,13 +131,18 @@ namespace Xwt.Drawing
|
|||
int i = name.LastIndexOf (' ');
|
||||
int lasti = name.Length;
|
||||
do {
|
||||
if (lasti > 0 && IsFontSupported (name.Substring (0, lasti)))
|
||||
break;
|
||||
|
||||
string token = name.Substring (i + 1, lasti - i - 1);
|
||||
FontStyle st;
|
||||
FontWeight fw;
|
||||
FontStretch fs;
|
||||
double siz;
|
||||
if (double.TryParse (token, NumberStyles.Any, CultureInfo.InvariantCulture, out siz)) // Try parsing the number first, since Enum.TryParse can also parse numbers
|
||||
size = siz;
|
||||
if (double.TryParse (token, NumberStyles.Any, CultureInfo.InvariantCulture, out siz)) { // Try parsing the number first, since Enum.TryParse can also parse numbers
|
||||
if (size == -1) // take only first number
|
||||
size = siz;
|
||||
}
|
||||
else if (Enum.TryParse<FontStyle> (token, true, out st) && st != FontStyle.Normal)
|
||||
style = st;
|
||||
else if (Enum.TryParse<FontWeight> (token, true, out fw) && fw != FontWeight.Normal)
|
||||
|
@ -167,6 +172,22 @@ namespace Xwt.Drawing
|
|||
return Font.SystemFont;
|
||||
}
|
||||
|
||||
static bool IsFontSupported (string fontNames)
|
||||
{
|
||||
LoadInstalledFonts ();
|
||||
|
||||
string[] names = fontNames.Split (new [] {','}, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (names.Length == 0)
|
||||
throw new ArgumentException ("Font family name not provided");
|
||||
|
||||
foreach (var name in names) {
|
||||
var n = name.Trim ();
|
||||
if (installedFonts.ContainsKey (n))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static string GetSupportedFont (string fontNames)
|
||||
{
|
||||
LoadInstalledFonts ();
|
||||
|
@ -195,7 +216,8 @@ namespace Xwt.Drawing
|
|||
|
||||
static string GetDefaultFont (string unknownFont)
|
||||
{
|
||||
Console.WriteLine ("Font '" + unknownFont + "' not available in the system. Using '" + Font.SystemFont.Family + "' instead");
|
||||
if (unknownFont != Font.SystemFont.Family) // ignore rare case when the default system font is not registered
|
||||
Console.WriteLine ("Font '" + unknownFont + "' not available in the system. Using '" + Font.SystemFont.Family + "' instead");
|
||||
return Font.SystemFont.Family;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче