Skip to content

Commit f636891

Browse files
authored
Merge pull request #935 from Flow-Launcher/GlyphFontName
Revise ttf/otf code
2 parents 9c92219 + b5f81c8 commit f636891

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 26 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
41+
{
42+
FontFamily = fonts[fontFamilyPath]
43+
};
44+
}
45+
else
46+
{
47+
fontCollection.AddFontFile(fontFamilyPath);
48+
fonts[fontFamilyPath] = $"{Path.GetDirectoryName(fontFamilyPath)}/#{fontCollection.Families[^1].Name}";
49+
Glyph = glyph with
3050
{
31-
FontFamily = Path.Combine(Result.PluginDirectory, fontPath)
51+
FontFamily = fonts[fontFamilyPath]
3252
};
53+
}
3354
}
3455
else
3556
{

0 commit comments

Comments
 (0)