FontHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Text;
  6. using System.Linq;
  7. namespace Optimizer {
  8. internal static class FontHelper {
  9. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  10. private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
  11. IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
  12. static PrivateFontCollection fonts = new PrivateFontCollection();
  13. internal static Font Poppins15;
  14. internal static void LoadFont() {
  15. byte[] fontData = Properties.Resources.Poppins_Regular;
  16. IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
  17. System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
  18. uint dummy = 0;
  19. fonts.AddMemoryFont(fontPtr, Properties.Resources.Poppins_Regular.Length);
  20. AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.Poppins_Regular.Length, IntPtr.Zero, ref dummy);
  21. System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
  22. Poppins15 = new Font(fonts.Families[0], 13f);
  23. }
  24. internal static IEnumerable<string> GetAvailableFonts() {
  25. return new InstalledFontCollection().Families.Select(x => x.Name);
  26. }
  27. internal static void ChangeGlobalFont(string fontName) {
  28. try {
  29. using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", true)) {
  30. fontsKey.SetValue("Segoe UI (TrueType)", "");
  31. fontsKey.SetValue("Segoe UI Bold (TrueType)", "");
  32. fontsKey.SetValue("Segoe UI Bold Italic (TrueType)", "");
  33. fontsKey.SetValue("Segoe UI Italic (TrueType)", "");
  34. fontsKey.SetValue("Segoe UI Light (TrueType)", "");
  35. fontsKey.SetValue("Segoe UI Semibold (TrueType)", "");
  36. fontsKey.SetValue("Segoe UI Symbol (TrueType)", "");
  37. }
  38. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", true)) {
  39. fontSubstitutesKey.SetValue("Segoe UI", fontName);
  40. }
  41. }
  42. catch (Exception ex) {
  43. Logger.LogError("FontHelper.ChangeGlobalFont", ex.Message, ex.StackTrace);
  44. }
  45. }
  46. internal static string GetCurrentGlobalFont() {
  47. try {
  48. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", false)) {
  49. return fontSubstitutesKey.GetValue("Segoe UI", string.Empty) as string;
  50. }
  51. }
  52. catch (Exception ex) {
  53. Logger.LogError("FontHelper.GetCurrentGlobalFont", ex.Message, ex.StackTrace);
  54. return string.Empty;
  55. }
  56. }
  57. internal static void RestoreDefaultGlobalFont() {
  58. try {
  59. using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", true)) {
  60. fontsKey.SetValue("Segoe UI (TrueType)", "segoeui.ttf");
  61. fontsKey.SetValue("Segoe UI Black (TrueType)", "seguibl.ttf");
  62. fontsKey.SetValue("Segoe UI Black Italic (TrueType)", "seguibli.ttf");
  63. fontsKey.SetValue("Segoe UI Bold (TrueType)", "segoeuib.ttf");
  64. fontsKey.SetValue("Segoe UI Bold Italic (TrueType)", "segoeuiz.ttf");
  65. fontsKey.SetValue("Segoe UI Emoji (TrueType)", "seguiemj.ttf");
  66. fontsKey.SetValue("Segoe UI Historic (TrueType)", "seguihis.ttf");
  67. fontsKey.SetValue("Segoe UI Italic (TrueType)", "segoeuii.ttf");
  68. fontsKey.SetValue("Segoe UI Light (TrueType)", "segoeuil.ttf");
  69. fontsKey.SetValue("Segoe UI Light Italic (TrueType)", "seguili.ttf");
  70. fontsKey.SetValue("Segoe UI Semibold (TrueType)", "seguisb.ttf");
  71. fontsKey.SetValue("Segoe UI Semibold Italic (TrueType)", "seguisbi.ttf");
  72. fontsKey.SetValue("Segoe UI Semilight (TrueType)", "segoeuisl.ttf");
  73. fontsKey.SetValue("Segoe UI Semilight Italic (TrueType)", "seguisli.ttf");
  74. fontsKey.SetValue("Segoe UI Symbol (TrueType)", "seguisym.ttf");
  75. fontsKey.SetValue("Segoe MDL2 Assets (TrueType)", "segmdl2.ttf");
  76. fontsKey.SetValue("Segoe Print (TrueType)", "segoepr.ttf");
  77. fontsKey.SetValue("Segoe Print Bold (TrueType)", "segoeprb.ttf");
  78. fontsKey.SetValue("Segoe Script (TrueType)", "segoesc.ttf");
  79. fontsKey.SetValue("Segoe Script Bold (TrueType)", "segoescb.ttf");
  80. }
  81. using (RegistryKey fontSubstitutesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes", true)) {
  82. fontSubstitutesKey.DeleteValue("Segoe UI", false);
  83. }
  84. }
  85. catch (Exception ex) {
  86. Logger.LogError("FontHelper.RestoreDefaultGlobalFont", ex.Message, ex.StackTrace);
  87. }
  88. }
  89. }
  90. }