| using System; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.Linq; |
| using System.Windows; |
| using System.Windows.Media; |
|
|
| namespace WpfRichTextBox |
| { |
| |
| |
| |
| public static class FontManager |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| 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; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| 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); |
| |
| |
| 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; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static bool LoadFontFromResource(Editor control, string resourcePath, double fontSize = 14) |
| { |
| try |
| { |
| |
| 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; |
| } |
| } |
|
|
| |
| |
| |
| |
| public static List<string> GetSystemFonts() |
| { |
| return Fonts.SystemFontFamilies |
| .Select(f => f.Source) |
| .OrderBy(name => name) |
| .ToList(); |
| } |
|
|
| |
| |
| |
| |
| 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(); |
| } |
|
|
| |
| |
| |
| |
| |
| 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; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| 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); |
| } |
|
|
| |
| |
| |
| |
| |
| public static bool ApplyFontConfig(Editor control, FontConfig config) |
| { |
| return SetFont(control, config.FontFamily, config.FontSize, config.FontWeight, config.FontStyle); |
| } |
| } |
|
|
| |
| |
| |
| 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; |
| } |
| } |
| } |