/
FontFile.cs
108 lines (96 loc) · 3.1 KB
/
FontFile.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
namespace Xamarin.Forms
{
public class FontFile
{
public string FileName { get; set; }
public string Extension { get; set; }
public string FileNameWithExtension(string extension) => $"{FileName}{extension}";
public string FileNameWithExtension() => FileNameWithExtension(Extension);
public string PostScriptName { get; set; }
public string GetPostScriptNameWithSpaces() =>
string.Join(" ", GetFontName(PostScriptName));
public static readonly string[] Extensions = {
".ttf",
".otf",
};
public static FontFile FromString(string input)
{
var hashIndex = input.IndexOf("#", System.StringComparison.Ordinal);
//UWP names require Spaces. Sometimes people may use those, "CuteFont-Regular#Cute Font" should be "CuteFont-Regular#CuteFont"
var postScriptName = hashIndex > 0 ? input.Substring(hashIndex + 1).Replace(" ", "") : input;
//Get the fontFamily name;
var fontFamilyName = hashIndex > 0 ? input.Substring(0, hashIndex) : input;
var foundExtension = Extensions.
FirstOrDefault(x => fontFamilyName.EndsWith(x, StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrWhiteSpace(foundExtension))
fontFamilyName = fontFamilyName.Substring(0, fontFamilyName.Length - foundExtension.Length);
return new FontFile
{
FileName = fontFamilyName,
Extension = foundExtension,
PostScriptName = postScriptName,
};
}
static IEnumerable<string> GetFontName(string fontFamily)
{
if (fontFamily.Contains(" "))
{
yield return fontFamily;
//We are done, they have spaces, they have it handled.
yield break;
}
string currentString = "";
char lastCharacter = ' ';
var index = fontFamily.LastIndexOf("-", StringComparison.Ordinal);
bool multipleCaps = false;
var cleansedString = index > 0 ? fontFamily.Substring(0, index) : fontFamily;
foreach (var c in cleansedString)
{
//Always break on these characters
if (c == '_' || c == '-')
{
yield return currentString;
//Reset everything,
currentString = "";
lastCharacter = ' ';
multipleCaps = false;
}
else
{
if (char.IsUpper(c))
{
//If the last character is lowercase, we are in a new CamelCase font
if (char.IsLower(lastCharacter))
{
yield return currentString;
currentString = "";
lastCharacter = ' ';
}
else if (char.IsUpper(lastCharacter))
{
multipleCaps = true;
}
}
//Detect multiple UpperCase letters so we can separate things like PTSansNarrow into "PT Sans Narrow"
else if (multipleCaps && currentString.Length > 1)
{
var last = currentString[currentString.Length - 1];
yield return currentString.Substring(0, currentString.Length - 1);
//Reset everything so it doesnt do a space
multipleCaps = false;
lastCharacter = ' ';
currentString = last.ToString();
}
currentString += c;
lastCharacter = c;
}
}
//Send what is left!
if (!string.IsNullOrWhiteSpace(currentString))
yield return currentString.Trim();
}
}
}