I have been working on a UI framework for the last year and as a part of this process, I came across the need to develop a dynamic font system.
I wanted to design a system that allowed rich text styles and paragraph like properties.
The system allows fonts to be loaded at runtime for less intensive games but also provided a batching system for loading multiple font sizes and styles in a single font description file.
The system includes:
- Runtime font face loading
- Indentation
- Sub and super scripting
- Underlining, strikethrough and bar decorations
- Dynamic line spacing (based on the largest font in the line)
- Paragraph before and after spacing.
- Special paragraph indents like hanging and first line.
Text styling utilises record types introduced in C# 9, allowing for quick copying of the immutable type with modification using the with keyword.
Example
Runtime Loaded Sizing
Paragraph Implementation
Paragraph p = new();
p.Format = new ParagraphFormat() {
AfterSpacing = 0 ,
BeforeSpacing = 0,
LeftIndent = 0,
RightIndent = 0,
LineSpacing = 0
};
Writing to Paragraphs
p.Write("The quick brown fox jumps over the lazy dog.");
p.WriteLine("The quick brown fox jumps over the lazy dog.",
TextStyle.Default with { Face = Fonts.GetFace("Arial", 24) });
p.WriteIndexed("&*Test: &0Hello this \nis &*style&0 0, &1this is style 1",
TextStyle.DefaultBold with { ForegroundColor = Color.Yellow},
TextStyle.Default with { ForegroundColor = Color.Purple, Face = Fonts.GetFace("Arial", 30)},
TextStyle.DefaultSub
);*/