-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathFontFileHelper.cs
More file actions
113 lines (94 loc) · 3.05 KB
/
Copy pathFontFileHelper.cs
File metadata and controls
113 lines (94 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Files Community
// SPDX-License-Identifier: MPL-2.0
using Microsoft.Extensions.Logging;
using Microsoft.Graphics.Canvas.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using Windows.Storage;
using Windows.Storage.FileProperties;
namespace Files.App.Utils.Storage
{
public static class FontFileHelper
{
private const float FontSizeRatio = 0.35f;
private const string PreviewText = "Abg";
public static async Task<byte[]?> GetWinRTThumbnailAsync(string fontPath, uint size)
{
StorageFile? file = null;
StorageItemThumbnail? thumbnail = null;
try
{
file = await StorageFile.GetFileFromPathAsync(fontPath);
thumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
if (thumbnail is null || thumbnail.Size == 0)
{
return null;
}
using (var stream = thumbnail.AsStream())
{
using var memoryStream = new MemoryStream((int)thumbnail.Size);
await stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}
catch (Exception ex)
{
App.Logger.LogError(ex, $"Exception while getting WinRT thumbnail for {LogPathHelper.RedactPath(fontPath)}.");
return null;
}
finally
{
thumbnail?.Dispose();
}
}
public static byte[]? GenerateFontThumbnail(string fontPath, int size)
{
try
{
if (!File.Exists(fontPath))
return null;
using var fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(fontPath);
if (fontCollection.Families.Length == 0)
return null;
var fontFamily = fontCollection.Families[0];
var style = GetAvailableFontStyle(fontFamily);
using var bitmap = new Bitmap(size, size);
using var graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
var fontSize = size * FontSizeRatio;
using var font = new Font(fontFamily, fontSize, style, GraphicsUnit.Pixel);
var textSize = graphics.MeasureString(PreviewText, font);
var x = (size - textSize.Width) / 2;
var y = (size - textSize.Height) / 2;
using var brush = new SolidBrush(Color.Black);
graphics.DrawString(PreviewText, font, brush, x, y);
using var ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
catch (Exception ex)
{
App.Logger.LogError(ex, $"Exception while generating font thumbnail for {LogPathHelper.RedactPath(fontPath)}.");
return null;
}
}
private static FontStyle GetAvailableFontStyle(FontFamily fontFamily)
{
if (fontFamily.IsStyleAvailable(FontStyle.Regular))
return FontStyle.Regular;
if (fontFamily.IsStyleAvailable(FontStyle.Bold))
return FontStyle.Bold;
if (fontFamily.IsStyleAvailable(FontStyle.Italic))
return FontStyle.Italic;
if (fontFamily.IsStyleAvailable(FontStyle.Bold | FontStyle.Italic))
return FontStyle.Bold | FontStyle.Italic;
return FontStyle.Regular;
}
}
}