fasdfsa commited on
Commit
279340c
·
1 Parent(s): e3fae40

设置编程字体

Browse files
src/WpfEditor/FontManager.cs ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.IO;
4
+ using System.Linq;
5
+ using System.Windows;
6
+ using System.Windows.Media;
7
+
8
+ namespace WpfEditor
9
+ {
10
+ /// <summary>
11
+ /// 字体管理器,用于处理 TextEditorControl 的字体设置
12
+ /// </summary>
13
+ public static class FontManager
14
+ {
15
+ /// <summary>
16
+ /// 为 TextEditorControl 设置字体
17
+ /// </summary>
18
+ /// <param name="control">文本编辑器控件</param>
19
+ /// <param name="fontFamilyName">字体族名称</param>
20
+ /// <param name="fontSize">字体大小</param>
21
+ /// <param name="fontWeight">字体粗细</param>
22
+ /// <param name="fontStyle">字体样式</param>
23
+ public static bool SetFont(TextEditorControl control, string fontFamilyName,
24
+ double fontSize = 14, FontWeight? fontWeight = null, FontStyle? fontStyle = null)
25
+ {
26
+ try
27
+ {
28
+ // 设置字体族
29
+ control.FontFamily = new FontFamily(fontFamilyName);
30
+
31
+ // 设置字体大小
32
+ control.FontSize = fontSize;
33
+
34
+ // 设置字体粗细
35
+ if (fontWeight.HasValue)
36
+ control.FontWeight = fontWeight.Value;
37
+
38
+ // 设置字体样式
39
+ if (fontStyle.HasValue)
40
+ control.FontStyle = fontStyle.Value;
41
+
42
+ // 强制重新渲染
43
+ control.InvalidateVisual();
44
+
45
+ return true;
46
+ }
47
+ catch (Exception ex)
48
+ {
49
+ // 记录错误或显示消息
50
+ System.Diagnostics.Debug.WriteLine($"设置字体失败: {ex.Message}");
51
+ return false;
52
+ }
53
+ }
54
+
55
+ /// <summary>
56
+ /// 从文件加载自定义字体
57
+ /// </summary>
58
+ /// <param name="control">文本编辑器控件</param>
59
+ /// <param name="fontFilePath">字体文件路径</param>
60
+ /// <param name="fontSize">字体大小</param>
61
+ public static bool LoadFontFromFile(TextEditorControl control, string fontFilePath, double fontSize = 14)
62
+ {
63
+ try
64
+ {
65
+ if (!File.Exists(fontFilePath))
66
+ {
67
+ throw new FileNotFoundException($"字体文件不存在: {fontFilePath}");
68
+ }
69
+
70
+ // 获取字体文件的目录和文件名
71
+ string fontDirectory = Path.GetDirectoryName(fontFilePath);
72
+ string fontFileName = Path.GetFileNameWithoutExtension(fontFilePath);
73
+
74
+ // 创建字体族 URI
75
+ string fontUri = $"file:///{fontDirectory.Replace('\\', '/')}#{fontFileName}";
76
+
77
+ // 设置字体
78
+ control.FontFamily = new FontFamily(fontUri);
79
+ control.FontSize = fontSize;
80
+
81
+ // 强制重新渲染
82
+ control.InvalidateVisual();
83
+
84
+ return true;
85
+ }
86
+ catch (Exception ex)
87
+ {
88
+ System.Diagnostics.Debug.WriteLine($"从文件加载字体失败: {ex.Message}");
89
+ return false;
90
+ }
91
+ }
92
+
93
+ /// <summary>
94
+ /// 从资源加载嵌入的字体
95
+ /// </summary>
96
+ /// <param name="control">文本编辑器控件</param>
97
+ /// <param name="resourcePath">资源路径,例如 "./Fonts/#FontName"</param>
98
+ /// <param name="fontSize">字体大小</param>
99
+ public static bool LoadFontFromResource(TextEditorControl control, string resourcePath, double fontSize = 14)
100
+ {
101
+ try
102
+ {
103
+ // 创建字体族,使用 pack URI
104
+ control.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), resourcePath);
105
+ control.FontSize = fontSize;
106
+
107
+ // 强制重新渲染
108
+ control.InvalidateVisual();
109
+
110
+ return true;
111
+ }
112
+ catch (Exception ex)
113
+ {
114
+ System.Diagnostics.Debug.WriteLine($"从资源加载字体失败: {ex.Message}");
115
+ return false;
116
+ }
117
+ }
118
+
119
+ /// <summary>
120
+ /// 获取系统可用字体列表
121
+ /// </summary>
122
+ /// <returns>字体名称列表</returns>
123
+ public static List<string> GetSystemFonts()
124
+ {
125
+ return Fonts.SystemFontFamilies
126
+ .Select(f => f.Source)
127
+ .OrderBy(name => name)
128
+ .ToList();
129
+ }
130
+
131
+ /// <summary>
132
+ /// 获取等宽字体列表(适合代码编辑器)
133
+ /// </summary>
134
+ /// <returns>等宽字体名称列表</returns>
135
+ public static List<string> GetMonospaceFonts()
136
+ {
137
+ var monospaceFonts = new List<string>
138
+ {
139
+ "Consolas",
140
+ "Courier New",
141
+ "Lucida Console",
142
+ "Monaco",
143
+ "Menlo",
144
+ "Source Code Pro",
145
+ "Fira Code",
146
+ "JetBrains Mono",
147
+ "Cascadia Code",
148
+ "DejaVu Sans Mono"
149
+ };
150
+
151
+ // 过滤出系统中实际存在的字体
152
+ var systemFonts = GetSystemFonts();
153
+ return monospaceFonts.Where(font => systemFonts.Contains(font)).ToList();
154
+ }
155
+
156
+ /// <summary>
157
+ /// 检查字体是否可用
158
+ /// </summary>
159
+ /// <param name="fontFamilyName">字体族名称</param>
160
+ /// <returns>字体是否可用</returns>
161
+ public static bool IsFontAvailable(string fontFamilyName)
162
+ {
163
+ try
164
+ {
165
+ var fontFamily = new FontFamily(fontFamilyName);
166
+ var typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
167
+ return typeface.TryGetGlyphTypeface(out _);
168
+ }
169
+ catch
170
+ {
171
+ return false;
172
+ }
173
+ }
174
+
175
+ /// <summary>
176
+ /// 设置编程字体(推荐的代码编辑器字体设置)
177
+ /// </summary>
178
+ /// <param name="control">文本编辑器控件</param>
179
+ /// <param name="fontSize">字体大小</param>
180
+ public static void SetProgrammingFont(TextEditorControl control, double fontSize = 14)
181
+ {
182
+ var preferredFonts = new[]
183
+ {
184
+ "Cascadia Code",
185
+ "JetBrains Mono",
186
+ "Fira Code",
187
+ "Source Code Pro",
188
+ "Consolas",
189
+ "Courier New"
190
+ };
191
+
192
+ foreach (var font in preferredFonts)
193
+ {
194
+ if (IsFontAvailable(font))
195
+ {
196
+ SetFont(control, font, fontSize, FontWeights.Normal, FontStyles.Normal);
197
+ return;
198
+ }
199
+ }
200
+
201
+ // 如果都不可用,使用系统默认等宽字体
202
+ SetFont(control, "Courier New", fontSize, FontWeights.Normal, FontStyles.Normal);
203
+ }
204
+
205
+ /// <summary>
206
+ /// 应用字体设置配置
207
+ /// </summary>
208
+ /// <param name="control">文本编辑器控件</param>
209
+ /// <param name="config">字体配置</param>
210
+ public static bool ApplyFontConfig(TextEditorControl control, FontConfig config)
211
+ {
212
+ return SetFont(control, config.FontFamily, config.FontSize, config.FontWeight, config.FontStyle);
213
+ }
214
+ }
215
+
216
+ /// <summary>
217
+ /// 字体配置类
218
+ /// </summary>
219
+ public class FontConfig
220
+ {
221
+ public string FontFamily { get; set; } = "Consolas";
222
+ public double FontSize { get; set; } = 14;
223
+ public FontWeight FontWeight { get; set; } = FontWeights.Normal;
224
+ public FontStyle FontStyle { get; set; } = FontStyles.Normal;
225
+
226
+ public FontConfig() { }
227
+
228
+ public FontConfig(string fontFamily, double fontSize, FontWeight fontWeight = default, FontStyle fontStyle = default)
229
+ {
230
+ FontFamily = fontFamily;
231
+ FontSize = fontSize;
232
+ FontWeight = fontWeight == default ? FontWeights.Normal : fontWeight;
233
+ FontStyle = fontStyle == default ? FontStyles.Normal : fontStyle;
234
+ }
235
+ }
236
+ }
src/WpfEditor/FontUsageExample.cs ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Windows;
3
+ using System.Windows.Media;
4
+
5
+ namespace WpfEditor
6
+ {
7
+ /// <summary>
8
+ /// 字体使用示例类,展示如何在 TextEditorControl 中设置字体
9
+ /// </summary>
10
+ public class FontUsageExample
11
+ {
12
+ private TextEditorControl textEditor;
13
+
14
+ public FontUsageExample(TextEditorControl editor)
15
+ {
16
+ textEditor = editor;
17
+ }
18
+
19
+ /// <summary>
20
+ /// 示例1:设置系统字体
21
+ /// </summary>
22
+ public void Example1_SetSystemFont()
23
+ {
24
+ // 设置 Consolas 字体,14号大小
25
+ FontManager.SetFont(textEditor, "Consolas", 14);
26
+
27
+ // 或者设置带样式的字体
28
+ FontManager.SetFont(textEditor, "Arial", 16, FontWeights.Bold, FontStyles.Italic);
29
+ }
30
+
31
+ /// <summary>
32
+ /// 示例2:设置编程专用字体
33
+ /// </summary>
34
+ public void Example2_SetProgrammingFont()
35
+ {
36
+ // 自动选择最佳的编程字体
37
+ FontManager.SetProgrammingFont(textEditor, 14);
38
+ }
39
+
40
+ /// <summary>
41
+ /// 示例3:从文件加载自定义字体
42
+ /// </summary>
43
+ public void Example3_LoadCustomFont()
44
+ {
45
+ string fontPath = @"C:\MyFonts\CustomFont.ttf";
46
+
47
+ if (FontManager.LoadFontFromFile(textEditor, fontPath, 14))
48
+ {
49
+ MessageBox.Show("自定义字体加载成功!");
50
+ }
51
+ else
52
+ {
53
+ MessageBox.Show("自定义字体加载失败,请检查文件路径。");
54
+ }
55
+ }
56
+
57
+ /// <summary>
58
+ /// 示例4:从资源加载嵌入字体
59
+ /// </summary>
60
+ public void Example4_LoadEmbeddedFont()
61
+ {
62
+ // 假设你在项目中有一个 Fonts 文件夹,里面有 MyFont.ttf
63
+ // 并且该文件的 Build Action 设置为 Resource
64
+ string resourcePath = "./Fonts/#MyFont";
65
+
66
+ if (FontManager.LoadFontFromResource(textEditor, resourcePath, 14))
67
+ {
68
+ MessageBox.Show("嵌入字体加载成功!");
69
+ }
70
+ else
71
+ {
72
+ MessageBox.Show("嵌入字体加载失败。");
73
+ }
74
+ }
75
+
76
+ /// <summary>
77
+ /// 示例5:使用字体配置
78
+ /// </summary>
79
+ public void Example5_UseFontConfig()
80
+ {
81
+ var config = new FontConfig
82
+ {
83
+ FontFamily = "JetBrains Mono",
84
+ FontSize = 16,
85
+ FontWeight = FontWeights.Medium,
86
+ FontStyle = FontStyles.Normal
87
+ };
88
+
89
+ if (FontManager.ApplyFontConfig(textEditor, config))
90
+ {
91
+ MessageBox.Show("字体配置应用成功!");
92
+ }
93
+ else
94
+ {
95
+ MessageBox.Show("字体配置应用失败,可能字体不存在。");
96
+ }
97
+ }
98
+
99
+ /// <summary>
100
+ /// 示例6:动态字体选择器
101
+ /// </summary>
102
+ public void Example6_FontSelector()
103
+ {
104
+ // 获取系统可用字体
105
+ var systemFonts = FontManager.GetSystemFonts();
106
+
107
+ // 创建字体选择对话框(简化版)
108
+ var fontDialog = new Microsoft.Win32.OpenFileDialog
109
+ {
110
+ Title = "选择字体文件",
111
+ Filter = "字体文件|*.ttf;*.otf;*.woff|所有文件|*.*"
112
+ };
113
+
114
+ if (fontDialog.ShowDialog() == true)
115
+ {
116
+ FontManager.LoadFontFromFile(textEditor, fontDialog.FileName, 14);
117
+ }
118
+ }
119
+
120
+ /// <summary>
121
+ /// 示例7:检查字体可用性
122
+ /// </summary>
123
+ public void Example7_CheckFontAvailability()
124
+ {
125
+ string[] fontsToCheck = { "Consolas", "JetBrains Mono", "Fira Code", "Comic Sans MS" };
126
+
127
+ foreach (string font in fontsToCheck)
128
+ {
129
+ bool available = FontManager.IsFontAvailable(font);
130
+ Console.WriteLine($"字体 '{font}' {(available ? "可用" : "不可用")}");
131
+ }
132
+ }
133
+
134
+ /// <summary>
135
+ /// 示例8:获取等宽字体列表
136
+ /// </summary>
137
+ public void Example8_GetMonospaceFonts()
138
+ {
139
+ var monospaceFonts = FontManager.GetMonospaceFonts();
140
+
141
+ Console.WriteLine("系统中可用的等宽字体:");
142
+ foreach (string font in monospaceFonts)
143
+ {
144
+ Console.WriteLine($"- {font}");
145
+ }
146
+
147
+ // 使用第一个可用的等宽字体
148
+ if (monospaceFonts.Count > 0)
149
+ {
150
+ FontManager.SetFont(textEditor, monospaceFonts[0], 14);
151
+ }
152
+ }
153
+
154
+ /// <summary>
155
+ /// 示例9:在 MainWindow 中的实际应用
156
+ /// </summary>
157
+ public static void IntegrateWithMainWindow(MainWindow mainWindow, TextEditorControl editor)
158
+ {
159
+ // 在 MainWindow 的构造函数或初始化方法中调用
160
+
161
+ // 设置默认编程字体
162
+ FontManager.SetProgrammingFont(editor, 14);
163
+
164
+ // 可以添加菜单项来让用户选择字体
165
+ // 例如在菜单栏添加"字体设置"选项
166
+ }
167
+ }
168
+
169
+ /// <summary>
170
+ /// 扩展 TextEditorControl 类的字体相关方法
171
+ /// 你可以将这些方法直接添加到 TextEditorControl 类中
172
+ /// </summary>
173
+ public static class TextEditorControlFontExtensions
174
+ {
175
+ /// <summary>
176
+ /// 设置编辑器字体的便捷方法
177
+ /// </summary>
178
+ public static void SetEditorFont(this TextEditorControl control, string fontFamily, double fontSize = 14)
179
+ {
180
+ FontManager.SetFont(control, fontFamily, fontSize);
181
+ }
182
+
183
+ /// <summary>
184
+ /// 应用编程字体的便捷方法
185
+ /// </summary>
186
+ public static void ApplyProgrammingFont(this TextEditorControl control, double fontSize = 14)
187
+ {
188
+ FontManager.SetProgrammingFont(control, fontSize);
189
+ }
190
+
191
+ /// <summary>
192
+ /// 从配置文件加载字体设置
193
+ /// </summary>
194
+ public static void LoadFontFromConfig(this TextEditorControl control, string configPath)
195
+ {
196
+ try
197
+ {
198
+ // 这里可以实现从 JSON 或 XML 配置文件读取字体设置
199
+ // 示例:从 config.json 读取字体配置
200
+
201
+ var config = new FontConfig("Consolas", 14); // 默认配置
202
+ FontManager.ApplyFontConfig(control, config);
203
+ }
204
+ catch (Exception ex)
205
+ {
206
+ System.Diagnostics.Debug.WriteLine($"加载字体配置失败: {ex.Message}");
207
+ // 使用默认字体
208
+ FontManager.SetProgrammingFont(control);
209
+ }
210
+ }
211
+ }
212
+ }
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -201,6 +201,9 @@ namespace WpfEditor
201
  // 为目录拆解模式添加示例数据
202
  //InitializeDirectoryTreeData();
203
  }
 
 
 
204
  }
205
 
206
 
 
201
  // 为目录拆解模式添加示例数据
202
  //InitializeDirectoryTreeData();
203
  }
204
+
205
+ // 自动选择最佳的编程字体
206
+ FontManager.SetProgrammingFont(Editor, 14);
207
  }
208
 
209