Skip to content

Commit 7b1f67d

Browse files
committed
prefer regular face per family
1 parent ad5ed8b commit 7b1f67d

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

DWBox/MainWindow.xaml.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ private async void OnAddInput(object sender, RoutedEventArgs e)
179179
else
180180
{
181181
HashSet<string> families = new(entries.Select(e => e.TypographicFamilyName));
182+
SortedList<string, FontSetEntry> bestPerFamily = new SortedList<string, FontSetEntry>(families.Count);
182183

183184
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"]);
184185

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

192193
case 1:
193194
foreach (var entry in entries)
194-
if (families.Remove(entry.TypographicFamilyName))
195-
_items.Add(entry, AddEmSize);
195+
if (!bestPerFamily.TryGetValue(entry.TypographicFamilyName, out var currentBest) || IsBetter(entry, currentBest))
196+
bestPerFamily[entry.TypographicFamilyName] = entry;
197+
198+
foreach (var entry in bestPerFamily.Values)
199+
_items.Add(entry, AddEmSize);
200+
196201
break;
197202

198203
default:
@@ -201,6 +206,30 @@ private async void OnAddInput(object sender, RoutedEventArgs e)
201206
}
202207
}
203208

209+
private static bool IsBetter(FontSetEntry proposed, FontSetEntry current)
210+
{
211+
var aValues = current.GetFontFaceReference().GetFontAxisValues().ToDictionary(av => av.AxisTag, av => av.Value);
212+
var bValues = proposed.GetFontFaceReference().GetFontAxisValues().ToDictionary(av => av.AxisTag, av => av.Value);
213+
214+
if (aValues.TryGetValue(FontAxisTag.Weight, out var aWeight) && bValues.TryGetValue(FontAxisTag.Weight, out var bWeight))
215+
if (aWeight != bWeight)
216+
return bWeight == (float)Win32.DWrite.FontWeight.Regular;
217+
218+
if (aValues.TryGetValue(FontAxisTag.Italic, out var aItalic) && bValues.TryGetValue(FontAxisTag.Italic, out var bItalic))
219+
if (aItalic != bItalic)
220+
return bItalic < aItalic;
221+
222+
if (aValues.TryGetValue(FontAxisTag.Width, out var aWidth) && bValues.TryGetValue(FontAxisTag.Width, out var bWidth))
223+
if (aWidth != bWidth)
224+
return Math.Abs(bWidth - 100) < Math.Abs(aWidth - 100);
225+
226+
if (aValues.TryGetValue(FontAxisTag.Slant, out var aSlant) && bValues.TryGetValue(FontAxisTag.Slant, out var bSlant))
227+
if (aSlant != bSlant)
228+
return Math.Abs(bSlant) < Math.Abs(aSlant);
229+
230+
return false;
231+
}
232+
204233
private void OnAddFamily(object sender, RoutedEventArgs e)
205234
{
206235
if (sender is FrameworkElement el)

0 commit comments

Comments
 (0)