-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
FontExtensions.cs
54 lines (50 loc) · 1.28 KB
/
FontExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Xamarin.Forms.Core;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.Tizen
{
public static class FontExtensions
{
public static string ToNativeFontFamily(this string self)
{
if (string.IsNullOrEmpty(self))
return null;
var cleansedFont = CleanseFontName(self);
int index = cleansedFont.LastIndexOf('-');
if (index != -1)
{
string font = cleansedFont.Substring(0, index);
string style = cleansedFont.Substring(index+1);
return $"{font}:style={style}";
}
else
{
return cleansedFont;
}
}
static string CleanseFontName(string fontName)
{
//First check Alias
var (hasFontAlias, fontPostScriptName) = FontRegistrar.HasFont(fontName);
if (hasFontAlias)
return fontPostScriptName;
var fontFile = FontFile.FromString(fontName);
if (!string.IsNullOrWhiteSpace(fontFile.Extension))
{
var (hasFont, _) = FontRegistrar.HasFont(fontFile.FileNameWithExtension());
if (hasFont)
return fontFile.PostScriptName;
}
else
{
foreach (var ext in FontFile.Extensions)
{
var formated = fontFile.FileNameWithExtension(ext);
var (hasFont, filePath) = FontRegistrar.HasFont(formated);
if (hasFont)
return fontFile.PostScriptName;
}
}
return fontFile.PostScriptName;
}
}
}