File size: 6,955 Bytes
279340c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Windows;
using System.Windows.Media;

namespace WpfEditor
{
    /// <summary>
    /// 字体使用示例类,展示如何在 TextEditorControl 中设置字体
    /// </summary>
    public class FontUsageExample
    {
        private TextEditorControl textEditor;

        public FontUsageExample(TextEditorControl editor)
        {
            textEditor = editor;
        }

        /// <summary>
        /// 示例1:设置系统字体
        /// </summary>
        public void Example1_SetSystemFont()
        {
            // 设置 Consolas 字体,14号大小
            FontManager.SetFont(textEditor, "Consolas", 14);

            // 或者设置带样式的字体
            FontManager.SetFont(textEditor, "Arial", 16, FontWeights.Bold, FontStyles.Italic);
        }

        /// <summary>
        /// 示例2:设置编程专用字体
        /// </summary>
        public void Example2_SetProgrammingFont()
        {
            // 自动选择最佳的编程字体
            FontManager.SetProgrammingFont(textEditor, 14);
        }

        /// <summary>
        /// 示例3:从文件加载自定义字体
        /// </summary>
        public void Example3_LoadCustomFont()
        {
            string fontPath = @"C:\MyFonts\CustomFont.ttf";
            
            if (FontManager.LoadFontFromFile(textEditor, fontPath, 14))
            {
                MessageBox.Show("自定义字体加载成功!");
            }
            else
            {
                MessageBox.Show("自定义字体加载失败,请检查文件路径。");
            }
        }

        /// <summary>
        /// 示例4:从资源加载嵌入字体
        /// </summary>
        public void Example4_LoadEmbeddedFont()
        {
            // 假设你在项目中有一个 Fonts 文件夹,里面有 MyFont.ttf
            // 并且该文件的 Build Action 设置为 Resource
            string resourcePath = "./Fonts/#MyFont";
            
            if (FontManager.LoadFontFromResource(textEditor, resourcePath, 14))
            {
                MessageBox.Show("嵌入字体加载成功!");
            }
            else
            {
                MessageBox.Show("嵌入字体加载失败。");
            }
        }

        /// <summary>
        /// 示例5:使用字体配置
        /// </summary>
        public void Example5_UseFontConfig()
        {
            var config = new FontConfig
            {
                FontFamily = "JetBrains Mono",
                FontSize = 16,
                FontWeight = FontWeights.Medium,
                FontStyle = FontStyles.Normal
            };

            if (FontManager.ApplyFontConfig(textEditor, config))
            {
                MessageBox.Show("字体配置应用成功!");
            }
            else
            {
                MessageBox.Show("字体配置应用失败,可能字体不存在。");
            }
        }

        /// <summary>
        /// 示例6:动态字体选择器
        /// </summary>
        public void Example6_FontSelector()
        {
            // 获取系统可用字体
            var systemFonts = FontManager.GetSystemFonts();
            
            // 创建字体选择对话框(简化版)
            var fontDialog = new Microsoft.Win32.OpenFileDialog
            {
                Title = "选择字体文件",
                Filter = "字体文件|*.ttf;*.otf;*.woff|所有文件|*.*"
            };

            if (fontDialog.ShowDialog() == true)
            {
                FontManager.LoadFontFromFile(textEditor, fontDialog.FileName, 14);
            }
        }

        /// <summary>
        /// 示例7:检查字体可用性
        /// </summary>
        public void Example7_CheckFontAvailability()
        {
            string[] fontsToCheck = { "Consolas", "JetBrains Mono", "Fira Code", "Comic Sans MS" };

            foreach (string font in fontsToCheck)
            {
                bool available = FontManager.IsFontAvailable(font);
                Console.WriteLine($"字体 '{font}' {(available ? "可用" : "不可用")}");
            }
        }

        /// <summary>
        /// 示例8:获取等宽字体列表
        /// </summary>
        public void Example8_GetMonospaceFonts()
        {
            var monospaceFonts = FontManager.GetMonospaceFonts();
            
            Console.WriteLine("系统中可用的等宽字体:");
            foreach (string font in monospaceFonts)
            {
                Console.WriteLine($"- {font}");
            }

            // 使用第一个可用的等宽字体
            if (monospaceFonts.Count > 0)
            {
                FontManager.SetFont(textEditor, monospaceFonts[0], 14);
            }
        }

        /// <summary>
        /// 示例9:在 MainWindow 中的实际应用
        /// </summary>
        public static void IntegrateWithMainWindow(MainWindow mainWindow, TextEditorControl editor)
        {
            // 在 MainWindow 的构造函数或初始化方法中调用
            
            // 设置默认编程字体
            FontManager.SetProgrammingFont(editor, 14);

            // 可以添加菜单项来让用户选择字体
            // 例如在菜单栏添加"字体设置"选项
        }
    }

    /// <summary>
    /// 扩展 TextEditorControl 类的字体相关方法
    /// 你可以将这些方法直接添加到 TextEditorControl 类中
    /// </summary>
    public static class TextEditorControlFontExtensions
    {
        /// <summary>
        /// 设置编辑器字体的便捷方法
        /// </summary>
        public static void SetEditorFont(this TextEditorControl control, string fontFamily, double fontSize = 14)
        {
            FontManager.SetFont(control, fontFamily, fontSize);
        }

        /// <summary>
        /// 应用编程字体的便捷方法
        /// </summary>
        public static void ApplyProgrammingFont(this TextEditorControl control, double fontSize = 14)
        {
            FontManager.SetProgrammingFont(control, fontSize);
        }

        /// <summary>
        /// 从配置文件加载字体设置
        /// </summary>
        public static void LoadFontFromConfig(this TextEditorControl control, string configPath)
        {
            try
            {
                // 这里可以实现从 JSON 或 XML 配置文件读取字体设置
                // 示例:从 config.json 读取字体配置
                
                var config = new FontConfig("Consolas", 14); // 默认配置
                FontManager.ApplyFontConfig(control, config);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"加载字体配置失败: {ex.Message}");
                // 使用默认字体
                FontManager.SetProgrammingFont(control);
            }
        }
    }
}