Skip to content

Commit ded4940

Browse files
committed
Revise ttf/otf code
1 parent 47c4023 commit ded4940

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
using Flow.Launcher.Infrastructure.UserSettings;
88
using Flow.Launcher.Plugin;
99
using System.IO;
10+
using System.Drawing.Text;
11+
using System.Collections.Generic;
1012

1113
namespace Flow.Launcher.ViewModel
1214
{
1315
public class ResultViewModel : BaseModel
1416
{
17+
private static PrivateFontCollection fontCollection = new();
18+
private static Dictionary<string, string> fonts = new();
19+
1520
public ResultViewModel(Result result, Settings settings)
1621
{
1722
if (result != null)
@@ -23,13 +28,29 @@ public ResultViewModel(Result result, Settings settings)
2328
// Checks if it's a system installed font, which does not require path to be provided.
2429
if (glyph.FontFamily.EndsWith(".ttf") || glyph.FontFamily.EndsWith(".otf"))
2530
{
26-
var fontPath = Result.Glyph.FontFamily;
27-
Glyph = Path.IsPathRooted(fontPath)
28-
? Result.Glyph
29-
: Result.Glyph with
31+
string fontFamilyPath = glyph.FontFamily;
32+
33+
if (!Path.IsPathRooted(fontFamilyPath))
34+
{
35+
fontFamilyPath = Path.Combine(Result.PluginDirectory, fontFamilyPath);
36+
}
37+
38+
if (fonts.ContainsKey(fontFamilyPath))
39+
{
40+
Glyph = glyph with
3041
{
31-
FontFamily = Path.Combine(Result.PluginDirectory, fontPath)
42+
FontFamily = fonts[fontFamilyPath]
3243
};
44+
}
45+
else
46+
{
47+
fontCollection.AddFontFile(fontFamilyPath);
48+
fonts[fontFamilyPath] = $"{Path.GetDirectoryName(fontFamilyPath)}/#{fontCollection.Families[^1].Name}";
49+
Glyph = glyph with
50+
{
51+
FontFamily = fonts[fontFamilyPath]
52+
};
53+
}
3354
}
3455
else
3556
{
@@ -41,6 +62,8 @@ public ResultViewModel(Result result, Settings settings)
4162
Settings = settings;
4263
}
4364

65+
66+
4467
private Settings Settings { get; }
4568

4669
public Visibility ShowOpenResultHotkey =>

0 commit comments

Comments
 (0)