tangxu
2024-10-24 c7a67ab24f476b0f8593fc61362d3bb8b262aa3e
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Extension.Poison;
using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Resolver.Poison
{
    #region PoisonFontResolverResolver
 
    public class PoisonFontResolver : PoisonFonts.IPoisonFontResolver
    {
        public Font ResolveFont(string familyName, float emSize, FontStyle fontStyle, GraphicsUnit unit)
        {
            Font fontTester = new(familyName, emSize, fontStyle, unit);
            if (fontTester.Name == familyName || !TryResolve(ref familyName, ref fontStyle))
            {
                return fontTester;
            }
 
            fontTester.Dispose();
 
            FontFamily fontFamily = GetFontFamily(familyName);
            return new Font(fontFamily, emSize, fontStyle, unit);
        }
 
        private const string OPEN_SANS_REGULAR = "Open Sans";
        private const string OPEN_SANS_LIGHT = "Open Sans Light";
        private const string OPEN_SANS_BOLD = "Open Sans Bold";
 
        private readonly PrivateFontCollection fontCollection = new();
 
        private static bool TryResolve(ref string familyName, ref FontStyle fontStyle)
        {
            if (familyName == "Segoe UI Light")
            {
                familyName = OPEN_SANS_LIGHT;
                if (fontStyle != FontStyle.Bold)
                {
                    fontStyle = FontStyle.Regular;
                }
 
                return true;
            }
 
            if (familyName == "Segoe UI")
            {
                if (fontStyle == FontStyle.Bold)
                {
                    familyName = OPEN_SANS_BOLD;
                    return true;
                }
 
                familyName = OPEN_SANS_REGULAR;
                return true;
            }
 
            return false;
        }
 
        private FontFamily GetFontFamily(string familyName)
        {
            lock (fontCollection)
            {
                foreach (FontFamily fontFamily in fontCollection.Families)
                {
                    if (fontFamily.Name == familyName)
                    {
                        return fontFamily;
                    }
                }
 
                string resourceName = "DPumpHydr.WinFrmUI.RLT.Resources.Poison." + familyName.Replace(' ', '_') + ".ttf";
 
                Stream fontStream = null;
                IntPtr data = IntPtr.Zero;
                try
                {
                    //fontStream = GetType().Assembly.GetManifestResourceStream(resourceName);
                    fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
                    int bytes = (int)fontStream.Length;
                    data = Marshal.AllocCoTaskMem(bytes);
                    byte[] fontdata = new byte[bytes];
                    fontStream.Read(fontdata, 0, bytes);
                    Marshal.Copy(fontdata, 0, data, bytes);
                    fontCollection.AddMemoryFont(data, bytes);
                    return fontCollection.Families[fontCollection.Families.Length - 1];
                }
                finally
                {
                    if (fontStream != null)
                    {
                        fontStream.Dispose();
                    }
 
                    if (data != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(data);
                    }
                }
            }
        }
    }
 
    #endregion
}