File size: 8,362 Bytes
a2132ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace WpfRichTextBox
{
    /// <summary>
    /// 字体管理器,用于处理 TextEditorControl 的字体设置
    /// </summary>
    public static class FontManager
    {
        /// <summary>
        /// 为 TextEditorControl 设置字体
        /// </summary>
        /// <param name="control">文本编辑器控件</param>
        /// <param name="fontFamilyName">字体族名称</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="fontWeight">字体粗细</param>
        /// <param name="fontStyle">字体样式</param>
        public static bool SetFont(Editor control, string fontFamilyName, 
            double fontSize = 14, FontWeight? fontWeight = null, FontStyle? fontStyle = null)
        {
            try
            {
                // 设置字体族
                control.FontFamily = new FontFamily(fontFamilyName);
                
                // 设置字体大小
                control.FontSize = fontSize;
                
                // 设置字体粗细
                if (fontWeight.HasValue)
                    control.FontWeight = fontWeight.Value;
                
                // 设置字体样式
                if (fontStyle.HasValue)
                    control.FontStyle = fontStyle.Value;
                
                // 强制重新渲染
                control.InvalidateVisual();
                
                return true;
            }
            catch (Exception ex)
            {
                // 记录错误或显示消息
                System.Diagnostics.Debug.WriteLine($"设置字体失败: {ex.Message}");
                return false;
            }
        }

        /// <summary>
        /// 从文件加载自定义字体
        /// </summary>
        /// <param name="control">文本编辑器控件</param>
        /// <param name="fontFilePath">字体文件路径</param>
        /// <param name="fontSize">字体大小</param>
        public static bool LoadFontFromFile(Editor control, string fontFilePath, double fontSize = 14)
        {
            try
            {
                if (!File.Exists(fontFilePath))
                {
                    throw new FileNotFoundException($"字体文件不存在: {fontFilePath}");
                }

                // 获取字体文件的目录和文件名
                string fontDirectory = Path.GetDirectoryName(fontFilePath);
                string fontFileName = Path.GetFileNameWithoutExtension(fontFilePath);
                
                // 创建字体族 URI
                string fontUri = $"file:///{fontDirectory.Replace('\\', '/')}#{fontFileName}";
                
                // 设置字体
                control.FontFamily = new FontFamily(fontUri);
                control.FontSize = fontSize;
                
                // 强制重新渲染
                control.InvalidateVisual();
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"从文件加载字体失败: {ex.Message}");
                return false;
            }
        }

        /// <summary>
        /// 从资源加载嵌入的字体
        /// </summary>
        /// <param name="control">文本编辑器控件</param>
        /// <param name="resourcePath">资源路径,例如 "./Fonts/#FontName"</param>
        /// <param name="fontSize">字体大小</param>
        public static bool LoadFontFromResource(Editor control, string resourcePath, double fontSize = 14)
        {
            try
            {
                // 创建字体族,使用 pack URI
                control.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), resourcePath);
                control.FontSize = fontSize;
                
                // 强制重新渲染
                control.InvalidateVisual();
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"从资源加载字体失败: {ex.Message}");
                return false;
            }
        }

        /// <summary>
        /// 获取系统可用字体列表
        /// </summary>
        /// <returns>字体名称列表</returns>
        public static List<string> GetSystemFonts()
        {
            return Fonts.SystemFontFamilies
                .Select(f => f.Source)
                .OrderBy(name => name)
                .ToList();
        }

        /// <summary>
        /// 获取等宽字体列表(适合代码编辑器)
        /// </summary>
        /// <returns>等宽字体名称列表</returns>
        public static List<string> GetMonospaceFonts()
        {
            var monospaceFonts = new List<string>
            {
                "Consolas",
                "Courier New", 
                "Lucida Console",
                "Monaco",
                "Menlo",
                "Source Code Pro",
                "Fira Code",
                "JetBrains Mono",
                "Cascadia Code",
                "DejaVu Sans Mono"
            };

            // 过滤出系统中实际存在的字体
            var systemFonts = GetSystemFonts();
            return monospaceFonts.Where(font => systemFonts.Contains(font)).ToList();
        }

        /// <summary>
        /// 检查字体是否可用
        /// </summary>
        /// <param name="fontFamilyName">字体族名称</param>
        /// <returns>字体是否可用</returns>
        public static bool IsFontAvailable(string fontFamilyName)
        {
            try
            {
                var fontFamily = new FontFamily(fontFamilyName);
                var typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
                return typeface.TryGetGlyphTypeface(out _);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 设置编程字体(推荐的代码编辑器字体设置)
        /// </summary>
        /// <param name="control">文本编辑器控件</param>
        /// <param name="fontSize">字体大小</param>
        public static void SetProgrammingFont(Editor control, double fontSize = 16)
        {
            var preferredFonts = new[]
            {
                "Cascadia Code",
                "JetBrains Mono", 
                "Fira Code",
                "Source Code Pro",
                "Consolas",
                "Courier New"
            };

            foreach (var font in preferredFonts)
            {
                if (IsFontAvailable(font))
                {
                    SetFont(control, font, fontSize, FontWeights.Normal, FontStyles.Normal);
                    return;
                }
            }

            // 如果都不可用,使用系统默认等宽字体
            SetFont(control, "Courier New", fontSize, FontWeights.Normal, FontStyles.Normal);
        }

        /// <summary>
        /// 应用字体设置配置
        /// </summary>
        /// <param name="control">文本编辑器控件</param>
        /// <param name="config">字体配置</param>
        public static bool ApplyFontConfig(Editor control, FontConfig config)
        {
            return SetFont(control, config.FontFamily, config.FontSize, config.FontWeight, config.FontStyle);
        }
    }

    /// <summary>
    /// 字体配置类
    /// </summary>
    public class FontConfig
    {
        public string FontFamily { get; set; } = "Consolas";
        public double FontSize { get; set; } = 14;
        public FontWeight FontWeight { get; set; } = FontWeights.Normal;
        public FontStyle FontStyle { get; set; } = FontStyles.Normal;

        public FontConfig() { }

        public FontConfig(string fontFamily, double fontSize, FontWeight fontWeight = default, FontStyle fontStyle = default)
        {
            FontFamily = fontFamily;
            FontSize = fontSize;
            FontWeight = fontWeight == default ? FontWeights.Normal : fontWeight;
            FontStyle = fontStyle == default ? FontStyles.Normal : fontStyle;
        }
    }
}