From c70182432ba144696506e79d20c4d0b31168c8d6 Mon Sep 17 00:00:00 2001 From: JieXu Date: Mon, 17 Nov 2025 19:31:01 +0800 Subject: [PATCH] Update AppBuilderExtension.cs --- .../Common/AppBuilderExtension.cs | 77 ++++++++++++++++++- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/v2rayN/v2rayN.Desktop/Common/AppBuilderExtension.cs b/v2rayN/v2rayN.Desktop/Common/AppBuilderExtension.cs index c0702938..3f2ca52c 100644 --- a/v2rayN/v2rayN.Desktop/Common/AppBuilderExtension.cs +++ b/v2rayN/v2rayN.Desktop/Common/AppBuilderExtension.cs @@ -1,14 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using Avalonia.Media; + namespace v2rayN.Desktop.Common; public static class AppBuilderExtension { public static AppBuilder WithFontByDefault(this AppBuilder appBuilder) { - var uri = Path.Combine(Global.AvaAssets, "Fonts#Noto Sans SC"); - return appBuilder.With(new FontManagerOptions() + if (!OperatingSystem.IsLinux()) { - //DefaultFamilyName = uri, - FontFallbacks = new[] { new FontFallback { FontFamily = new FontFamily(uri) } } + var uri = Path.Combine(Global.AvaAssets, "Fonts#Noto Sans SC"); + return appBuilder.With(new FontManagerOptions + { + FontFallbacks = new[] + { + new FontFallback { FontFamily = new FontFamily(uri) } + } + }); + } + + var fallbacks = new List(); + + string? zhFont = RunFcMatch("sans:lang=zh-cn"); + + if (!string.IsNullOrWhiteSpace(zhFont)) + { + fallbacks.Add(new FontFallback + { + FontFamily = new FontFamily(zhFont) + }); + } + + string? emojiFont = RunFcMatch("emoji"); + + if (!string.IsNullOrWhiteSpace(emojiFont)) + { + fallbacks.Add(new FontFallback + { + FontFamily = new FontFamily(emojiFont) + }); + } + + var notoUri = Path.Combine(Global.AvaAssets, "Fonts#Noto Sans SC"); + fallbacks.Add(new FontFallback + { + FontFamily = new FontFamily(notoUri) + }); + + return appBuilder.With(new FontManagerOptions + { + FontFallbacks = fallbacks.ToArray() }); } + + private static string? RunFcMatch(string pattern) + { + try + { + var psi = new ProcessStartInfo + { + FileName = "/bin/bash", + ArgumentList = { "-c", $"fc-match -f \"%{{file}}\\n\" \"{pattern}\" | head -n 1" }, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false + }; + + using var p = Process.Start(psi); + if (p == null) return null; + + string output = p.StandardOutput.ReadToEnd().Trim(); + return string.IsNullOrWhiteSpace(output) ? null : output; + } + catch + { + return null; + } + } }