-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathFontLoadingSample.cs
More file actions
59 lines (48 loc) · 2.05 KB
/
FontLoadingSample.cs
File metadata and controls
59 lines (48 loc) · 2.05 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
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Text;
using SixLabors.Fonts;
using SixLabors.Samples;
namespace SixLabors.Samples.Fonts;
/// <summary>
/// Demonstrates loading a font file and reading its family metadata.
/// </summary>
internal static class FontLoadingSample
{
private const string FontFile = "roboto-regular.ttf";
/// <summary>
/// Writes a report describing the loaded font family.
/// </summary>
public static void Run()
{
string fontPath = SamplePaths.Asset(FontFile);
FontCollection fonts = new();
// Add returns a FontFamily that can create Font instances; the out parameter exposes
// descriptive name-table information read from the font file.
FontFamily family = fonts.Add(fontPath, out FontDescription description);
StringBuilder builder = new();
builder.AppendLine("Font loading");
builder.AppendLine("============");
builder.AppendLine($"File: {Path.GetFileName(fontPath)}");
builder.AppendLine($"Family: {family.Name}");
builder.AppendLine($"Font name: {description.FontNameInvariantCulture}");
builder.AppendLine($"Subfamily: {description.FontSubFamilyNameInvariantCulture}");
builder.AppendLine($"Style: {description.Style}");
builder.AppendLine();
builder.AppendLine("Available styles in this collection:");
foreach (FontStyle style in family.GetAvailableStyles().Span)
{
builder.AppendLine($"- {style}");
}
builder.AppendLine();
builder.AppendLine("Source paths:");
// Families created from files can report the physical font paths that contributed
// their metrics; this is useful when diagnosing which font a collection resolved.
family.TryGetPaths(out ReadOnlyMemory<string> paths);
foreach (string path in paths.Span)
{
builder.AppendLine($"- {path}");
}
File.WriteAllText(SamplePaths.Output("font-loading.txt"), builder.ToString());
}
}