|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.ComponentModel; |
3 | 4 | using System.Diagnostics; |
4 | 5 | using System.Globalization; |
5 | 6 | using System.IO; |
| 7 | +using System.Linq; |
6 | 8 | using System.Runtime.InteropServices; |
7 | 9 | using System.Threading; |
8 | 10 | using System.Windows; |
9 | 11 | using System.Windows.Interop; |
| 12 | +using System.Windows.Markup; |
10 | 13 | using System.Windows.Media; |
11 | 14 | using Flow.Launcher.Infrastructure.UserSettings; |
12 | 15 | using Microsoft.Win32; |
|
18 | 21 | using WindowsInput; |
19 | 22 | using WindowsInput.Native; |
20 | 23 | using Point = System.Windows.Point; |
| 24 | +using SystemFonts = System.Windows.SystemFonts; |
21 | 25 |
|
22 | 26 | namespace Flow.Launcher.Infrastructure |
23 | 27 | { |
@@ -746,6 +750,73 @@ public static unsafe bool GetWindowRect(nint handle, out Rect outRect) |
746 | 750 | rect.bottom - rect.top |
747 | 751 | ); |
748 | 752 | return true; |
| 753 | + |
| 754 | + #endregion |
| 755 | + |
| 756 | + #region System Font |
| 757 | + |
| 758 | + private static readonly Dictionary<string, string> _languageToNotoSans = new() |
| 759 | + { |
| 760 | + { "ko", "Noto Sans KR" }, |
| 761 | + { "ja", "Noto Sans JP" }, |
| 762 | + { "zh-CN", "Noto Sans SC" }, |
| 763 | + { "zh-SG", "Noto Sans SC" }, |
| 764 | + { "zh-Hans", "Noto Sans SC" }, |
| 765 | + { "zh-TW", "Noto Sans TC" }, |
| 766 | + { "zh-HK", "Noto Sans TC" }, |
| 767 | + { "zh-MO", "Noto Sans TC" }, |
| 768 | + { "zh-Hant", "Noto Sans TC" }, |
| 769 | + { "th", "Noto Sans Thai" }, |
| 770 | + { "ar", "Noto Sans Arabic" }, |
| 771 | + { "he", "Noto Sans Hebrew" }, |
| 772 | + { "hi", "Noto Sans Devanagari" }, |
| 773 | + { "bn", "Noto Sans Bengali" }, |
| 774 | + { "ta", "Noto Sans Tamil" }, |
| 775 | + { "el", "Noto Sans Greek" }, |
| 776 | + { "ru", "Noto Sans" }, |
| 777 | + { "en", "Noto Sans" }, |
| 778 | + { "fr", "Noto Sans" }, |
| 779 | + { "de", "Noto Sans" }, |
| 780 | + { "es", "Noto Sans" }, |
| 781 | + { "pt", "Noto Sans" } |
| 782 | + }; |
| 783 | + |
| 784 | + public static string GetSystemDefaultFont() |
| 785 | + { |
| 786 | + try |
| 787 | + { |
| 788 | + var culture = CultureInfo.CurrentCulture; |
| 789 | + var language = culture.Name; // e.g., "zh-TW" |
| 790 | + var langPrefix = language.Split('-')[0]; // e.g., "zh" |
| 791 | + |
| 792 | + // First, try to find by full name, and if not found, fallback to prefix |
| 793 | + if (TryGetNotoFont(language, out var notoFont) || TryGetNotoFont(langPrefix, out notoFont)) |
| 794 | + { |
| 795 | + // If the font is installed, return it |
| 796 | + if (Fonts.SystemFontFamilies.Any(f => f.Source.Equals(notoFont))) |
| 797 | + { |
| 798 | + return notoFont; |
| 799 | + } |
| 800 | + } |
| 801 | + |
| 802 | + // If Noto font is not found, fallback to the system default font |
| 803 | + var font = SystemFonts.MessageFontFamily; |
| 804 | + if (font.FamilyNames.TryGetValue(XmlLanguage.GetLanguage("en-US"), out var englishName)) |
| 805 | + { |
| 806 | + return englishName; |
| 807 | + } |
| 808 | + |
| 809 | + return font.Source ?? "Segoe UI"; |
| 810 | + } |
| 811 | + catch |
| 812 | + { |
| 813 | + return "Segoe UI"; |
| 814 | + } |
| 815 | + } |
| 816 | + |
| 817 | + private static bool TryGetNotoFont(string langKey, out string notoFont) |
| 818 | + { |
| 819 | + return _languageToNotoSans.TryGetValue(langKey, out notoFont); |
749 | 820 | } |
750 | 821 |
|
751 | 822 | #endregion |
|
0 commit comments