-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFontHelper.cs
More file actions
70 lines (60 loc) · 2.06 KB
/
FontHelper.cs
File metadata and controls
70 lines (60 loc) · 2.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
namespace JarrettVance.ChapterTools
{
public class FontHelper
{
private static PrivateFontCollection sCustomFonts;
private static Dictionary<string, FontFamily> sFamilies;
static FontHelper()
{
sCustomFonts = new PrivateFontCollection();
sFamilies = new Dictionary<string, FontFamily>(
StringComparer.InvariantCultureIgnoreCase);
}
public static void RegisterFont(byte[] font)
{
var buffer = Marshal.AllocCoTaskMem(font.Length);
Marshal.Copy(font, 0, buffer, font.Length);
sCustomFonts.AddMemoryFont(buffer, font.Length);
}
public static void RegisterFont(string fontFilePath)
{
sCustomFonts.AddFontFile(fontFilePath);
}
public static FontFamily[] GetAllFamilies()
{
var families = new List<FontFamily>();
families.AddRange(FontFamily.Families);
families.AddRange(sCustomFonts.Families);
families.Sort((f1, f2) => { return f1.Name.CompareTo(f2.Name); });
return families.ToArray();
}
public static FontFamily GetFamily(string family)
{
try
{
// cache the family in a dictionary for fast lookup.
if (!sFamilies.ContainsKey(family))
sFamilies.Add(family, new FontFamily(family, sCustomFonts));
}
catch (ArgumentException)
{
sFamilies.Add(family, new FontFamily(family));
}
return sFamilies[family];
}
public static Font Create(
string family,
float emSize,
FontStyle style = FontStyle.Regular,
GraphicsUnit unit = GraphicsUnit.Pixel)
{
var fam = GetFamily(family);
return new Font(family, emSize, style, unit);
}
}
}