forked from moonheart08/Content.AL.UIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontStack.cs
More file actions
129 lines (112 loc) · 4.06 KB
/
Copy pathFontStack.cs
File metadata and controls
129 lines (112 loc) · 4.06 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
namespace Content.AL.UIKit;
[PublicAPI]
public abstract class FontStack
{
[Dependency] private readonly IResourceCache _resourceCache = default!;
/// <summary>
/// The primary font path, with string substitution markers.
/// </summary>
/// <remarks>
/// If using the default GetFont function, the replacements are as follows:
/// 0 is the font kind.
/// 1 is the font kind with BoldItalic replaced with Bold when it occurs.
/// </remarks>
public abstract string FontPrimary { get; }
/// <summary>
/// The symbols font path, with string substitution markers.
/// </summary>
/// <remarks>
/// If using the default GetFont function, the replacements are as follows:
/// 0 is the font kind.
/// 1 is the font kind with BoldItalic replaced with Bold when it occurs.
/// </remarks>
public abstract string FontSymbols { get; }
/// <summary>
/// The fallback font path, exactly. (no string substitutions.)
/// </summary>
public virtual string FontFallback => "/EngineFonts/NotoSans/NotoSans-Regular.ttf";
/// <summary>
/// Any extra fonts that should be stuck in after Symbols but before the fallback.
/// </summary>
public abstract string[] Extra { get; }
public virtual FontKind[] AvailableKinds => new[] {FontKind.Regular, FontKind.Bold, FontKind.Italic, FontKind.BoldItalic};
/// <summary>
/// This should return the paths of every font in this stack given the abstract members.
/// </summary>
/// <param name="kind">Which font kind to use.</param>
/// <returns></returns>
protected virtual string[] GetFontPaths(FontKind kind)
{
var sv = kind.SimplifyCompound().AsFileName();
var s = kind.AsFileName();
var l = new List<string>()
{
string.Format(FontPrimary, s, sv),
string.Format(FontSymbols, s, sv),
};
l.AddRange(Extra);
l.Add(FontFallback);
return l.ToArray();
}
/// <summary>
/// Retrieves an in-style font, of the provided size and kind.
/// </summary>
/// <param name="size">Size of the font to provide.</param>
/// <param name="kind">Optional font kind. Defaults to Regular.</param>
/// <returns>A Font resource.</returns>
public Font GetFont(int size, FontKind kind = FontKind.Regular)
{
//ALDebugTools.AssertContains(AvailableKinds, kind);
var paths = GetFontPaths(kind);
return _resourceCache.GetFont(paths, size);
}
protected FontStack()
{
IoCManager.InjectDependencies(this);
}
/// <summary>
/// The available kinds of font.
/// </summary>
public enum FontKind
{
Regular,
Bold,
Italic,
BoldItalic
}
}
[PublicAPI]
public static class FontKindExtensions
{
public static string AsFileName(this FontStack.FontKind kind)
{
return kind switch
{
FontStack.FontKind.Regular => "Regular",
FontStack.FontKind.Bold => "Bold",
FontStack.FontKind.Italic => "Italic",
FontStack.FontKind.BoldItalic => "BoldItalic",
_ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null)
};
}
public static bool IsBold(this FontStack.FontKind kind)
{
return kind == FontStack.FontKind.Bold || kind == FontStack.FontKind.BoldItalic;
}
public static bool IsItalic(this FontStack.FontKind kind)
{
return kind == FontStack.FontKind.Italic || kind == FontStack.FontKind.BoldItalic;
}
public static FontStack.FontKind SimplifyCompound(this FontStack.FontKind kind)
{
return kind switch
{
FontStack.FontKind.BoldItalic => FontStack.FontKind.Bold,
_ => kind
};
}
}