Skip to content

Commit

Permalink
prefer regular face per family
Browse files Browse the repository at this point in the history
  • Loading branch information
miloush committed Nov 13, 2024
1 parent ad5ed8b commit 7b1f67d
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions DWBox/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ private async void OnAddInput(object sender, RoutedEventArgs e)
else
{
HashSet<string> families = new(entries.Select(e => e.TypographicFamilyName));
SortedList<string, FontSetEntry> bestPerFamily = new SortedList<string, FontSetEntry>(families.Count);

var result = TaskDialog.Show(this, $"Found {entries.Count} matching font faces belonging to {families.Count} typographic families.", Title, "Add fonts", null, TaskDialogButtons.Cancel, TaskDialogImage.Information, [$"Add all {entries.Count} font faces", "Add one font face per family"]);

Expand All @@ -191,8 +192,12 @@ private async void OnAddInput(object sender, RoutedEventArgs e)

case 1:
foreach (var entry in entries)
if (families.Remove(entry.TypographicFamilyName))
_items.Add(entry, AddEmSize);
if (!bestPerFamily.TryGetValue(entry.TypographicFamilyName, out var currentBest) || IsBetter(entry, currentBest))
bestPerFamily[entry.TypographicFamilyName] = entry;

foreach (var entry in bestPerFamily.Values)
_items.Add(entry, AddEmSize);

break;

default:
Expand All @@ -201,6 +206,30 @@ private async void OnAddInput(object sender, RoutedEventArgs e)
}
}

private static bool IsBetter(FontSetEntry proposed, FontSetEntry current)
{
var aValues = current.GetFontFaceReference().GetFontAxisValues().ToDictionary(av => av.AxisTag, av => av.Value);
var bValues = proposed.GetFontFaceReference().GetFontAxisValues().ToDictionary(av => av.AxisTag, av => av.Value);

if (aValues.TryGetValue(FontAxisTag.Weight, out var aWeight) && bValues.TryGetValue(FontAxisTag.Weight, out var bWeight))
if (aWeight != bWeight)
return bWeight == (float)Win32.DWrite.FontWeight.Regular;

if (aValues.TryGetValue(FontAxisTag.Italic, out var aItalic) && bValues.TryGetValue(FontAxisTag.Italic, out var bItalic))
if (aItalic != bItalic)
return bItalic < aItalic;

if (aValues.TryGetValue(FontAxisTag.Width, out var aWidth) && bValues.TryGetValue(FontAxisTag.Width, out var bWidth))
if (aWidth != bWidth)
return Math.Abs(bWidth - 100) < Math.Abs(aWidth - 100);

if (aValues.TryGetValue(FontAxisTag.Slant, out var aSlant) && bValues.TryGetValue(FontAxisTag.Slant, out var bSlant))
if (aSlant != bSlant)
return Math.Abs(bSlant) < Math.Abs(aSlant);

return false;
}

private void OnAddFamily(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement el)
Expand Down

0 comments on commit 7b1f67d

Please sign in to comment.