设置字体
Browse files- Editor.cs +8 -5
- FontManager.cs +236 -0
Editor.cs
CHANGED
|
@@ -9,10 +9,13 @@ namespace WpfRichTextBox
|
|
| 9 |
|
| 10 |
public class Editor : RichTextBox
|
| 11 |
{
|
|
|
|
|
|
|
| 12 |
public List<RichChar> RichChars { get; private set; } = new List<RichChar>();
|
| 13 |
|
| 14 |
public Editor()
|
| 15 |
{
|
|
|
|
| 16 |
TextChanged += Editor_TextChanged;
|
| 17 |
DataObject.AddPastingHandler(this, OnPasteHandler);
|
| 18 |
}
|
|
@@ -33,14 +36,14 @@ namespace WpfRichTextBox
|
|
| 33 |
{
|
| 34 |
RichChars.Clear();
|
| 35 |
var text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
|
| 36 |
-
int maxChars = (int)(ActualWidth /
|
| 37 |
int count = Math.Min(text.Length, maxChars);
|
| 38 |
for (int i = 0; i < count; i++)
|
| 39 |
{
|
| 40 |
RichChars.Add(new RichChar
|
| 41 |
{
|
| 42 |
Character = text[i],
|
| 43 |
-
Position = new Point(i *
|
| 44 |
Color = Colors.Black
|
| 45 |
});
|
| 46 |
}
|
|
@@ -51,14 +54,14 @@ namespace WpfRichTextBox
|
|
| 51 |
base.OnRender(drawingContext);
|
| 52 |
foreach (var richChar in RichChars)
|
| 53 |
{
|
| 54 |
-
if (richChar.Position.X >= 0 && richChar.Position.X +
|
| 55 |
{
|
| 56 |
var formattedText = new FormattedText(
|
| 57 |
richChar.Character.ToString(),
|
| 58 |
System.Globalization.CultureInfo.CurrentCulture,
|
| 59 |
FlowDirection.LeftToRight,
|
| 60 |
-
new Typeface(
|
| 61 |
-
|
| 62 |
new SolidColorBrush(richChar.Color),
|
| 63 |
VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
| 64 |
|
|
|
|
| 9 |
|
| 10 |
public class Editor : RichTextBox
|
| 11 |
{
|
| 12 |
+
public int _fontsize = 16;
|
| 13 |
+
|
| 14 |
public List<RichChar> RichChars { get; private set; } = new List<RichChar>();
|
| 15 |
|
| 16 |
public Editor()
|
| 17 |
{
|
| 18 |
+
FontManager.SetProgrammingFont(this, _fontsize);
|
| 19 |
TextChanged += Editor_TextChanged;
|
| 20 |
DataObject.AddPastingHandler(this, OnPasteHandler);
|
| 21 |
}
|
|
|
|
| 36 |
{
|
| 37 |
RichChars.Clear();
|
| 38 |
var text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
|
| 39 |
+
int maxChars = (int)(ActualWidth / _fontsize);
|
| 40 |
int count = Math.Min(text.Length, maxChars);
|
| 41 |
for (int i = 0; i < count; i++)
|
| 42 |
{
|
| 43 |
RichChars.Add(new RichChar
|
| 44 |
{
|
| 45 |
Character = text[i],
|
| 46 |
+
Position = new Point(i * _fontsize, 10),
|
| 47 |
Color = Colors.Black
|
| 48 |
});
|
| 49 |
}
|
|
|
|
| 54 |
base.OnRender(drawingContext);
|
| 55 |
foreach (var richChar in RichChars)
|
| 56 |
{
|
| 57 |
+
if (richChar.Position.X >= 0 && richChar.Position.X + _fontsize <= ActualWidth && richChar.Position.Y >= 0 && richChar.Position.Y + _fontsize <= ActualHeight)
|
| 58 |
{
|
| 59 |
var formattedText = new FormattedText(
|
| 60 |
richChar.Character.ToString(),
|
| 61 |
System.Globalization.CultureInfo.CurrentCulture,
|
| 62 |
FlowDirection.LeftToRight,
|
| 63 |
+
new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch),
|
| 64 |
+
_fontsize,
|
| 65 |
new SolidColorBrush(richChar.Color),
|
| 66 |
VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
| 67 |
|
FontManager.cs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 WpfRichTextBox
|
| 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(Editor 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(Editor 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(Editor 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(Editor control, double fontSize = 16)
|
| 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(Editor 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 |
+
}
|