/ src / modules / launcher / PowerLauncher / Helper / FontHelper.cs
FontHelper.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  using System.Linq;
  7  using System.Reflection;
  8  using System.Windows;
  9  using System.Windows.Media;
 10  
 11  using Wox.Plugin.Logger;
 12  
 13  namespace PowerLauncher.Helper
 14  {
 15      public static class FontHelper
 16      {
 17          private static readonly FontWeightConverter _fontWeightConverter = new FontWeightConverter();
 18  
 19          public static FontWeight GetFontWeightFromInvariantStringOrNormal(string value)
 20          {
 21              if (value == null)
 22              {
 23                  return FontWeights.Normal;
 24              }
 25  
 26              try
 27              {
 28                  return (FontWeight)_fontWeightConverter.ConvertFromInvariantString(value);
 29              }
 30              catch (NotSupportedException e)
 31              {
 32                  Log.Exception($"Can't convert {value} to FontWeight", e, MethodBase.GetCurrentMethod().DeclaringType);
 33                  return FontWeights.Normal;
 34              }
 35          }
 36  
 37          private static readonly FontStyleConverter _fontStyleConverter = new FontStyleConverter();
 38  
 39          public static FontStyle GetFontStyleFromInvariantStringOrNormal(string value)
 40          {
 41              if (value == null)
 42              {
 43                  return FontStyles.Normal;
 44              }
 45  
 46              try
 47              {
 48                  return (FontStyle)_fontStyleConverter.ConvertFromInvariantString(value);
 49              }
 50              catch (NotSupportedException e)
 51              {
 52                  Log.Exception($"Can't convert {value} to FontStyle", e, MethodBase.GetCurrentMethod().DeclaringType);
 53                  return FontStyles.Normal;
 54              }
 55          }
 56  
 57          private static readonly FontStretchConverter _fontStretchConverter = new FontStretchConverter();
 58  
 59          public static FontStretch GetFontStretchFromInvariantStringOrNormal(string value)
 60          {
 61              if (value == null)
 62              {
 63                  return FontStretches.Normal;
 64              }
 65  
 66              try
 67              {
 68                  return (FontStretch)_fontStretchConverter.ConvertFromInvariantString(value);
 69              }
 70              catch (NotSupportedException e)
 71              {
 72                  Log.Exception($"Can't convert {value} to FontStretch", e, MethodBase.GetCurrentMethod().DeclaringType);
 73                  return FontStretches.Normal;
 74              }
 75          }
 76  
 77          public static FamilyTypeface ChooseRegularFamilyTypeface(this FontFamily family)
 78          {
 79              ArgumentNullException.ThrowIfNull(family);
 80  
 81              return family.FamilyTypefaces.OrderBy(o =>
 82              {
 83                  return (Math.Abs(o.Stretch.ToOpenTypeStretch() - FontStretches.Normal.ToOpenTypeStretch()) * 100) +
 84                      Math.Abs(o.Weight.ToOpenTypeWeight() - FontWeights.Normal.ToOpenTypeWeight()) +
 85                      ((o.Style == FontStyles.Normal ? 0 : o.Style == FontStyles.Oblique ? 1 : 2) * 1000);
 86              }).FirstOrDefault() ?? family.FamilyTypefaces.FirstOrDefault();
 87          }
 88  
 89          public static FamilyTypeface ConvertFromInvariantStringsOrNormal(this FontFamily family, string style, string weight, string stretch)
 90          {
 91              ArgumentNullException.ThrowIfNull(family);
 92  
 93              var styleObj = GetFontStyleFromInvariantStringOrNormal(style);
 94              var weightObj = GetFontWeightFromInvariantStringOrNormal(weight);
 95              var stretchObj = GetFontStretchFromInvariantStringOrNormal(stretch);
 96              return family.FamilyTypefaces.FirstOrDefault(o => o.Style == styleObj && o.Weight == weightObj && o.Stretch == stretchObj)
 97                  ?? family.ChooseRegularFamilyTypeface();
 98          }
 99      }
100  }