-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Feature: Add font file thumbnail generation and display font names #17843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
workbysaran
wants to merge
2
commits into
files-community:main
Choose a base branch
from
workbysaran:sg/feature-sysfonts-thumbnails
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,281 @@ | ||
| // Copyright (c) Files Community | ||
| // Licensed under the MIT License. | ||
|
|
||
| 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 System.Text; | ||
| 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 {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 {fontPath}."); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static string? GetFontName(string fontPath) | ||
| { | ||
| try | ||
| { | ||
| if (!File.Exists(fontPath)) | ||
| return null; | ||
|
|
||
| var fullName = ExtractFontNameFromTable(fontPath); | ||
| if (!string.IsNullOrEmpty(fullName)) | ||
| return fullName; | ||
|
|
||
| using var fontCollection = new PrivateFontCollection(); | ||
| fontCollection.AddFontFile(fontPath); | ||
|
|
||
| if (fontCollection.Families.Length == 0) | ||
| return null; | ||
|
|
||
| return fontCollection.Families[0].Name; | ||
| } | ||
| catch | ||
| { | ||
| App.Logger.LogError($"Failed to get font name for file: {fontPath}"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static string? ExtractFontNameFromTable(string fontPath) | ||
| { | ||
| try | ||
| { | ||
| using var fileStream = File.OpenRead(fontPath); | ||
| using var reader = new BinaryReader(fileStream); | ||
|
|
||
| // Read TTF header to find table directory | ||
| var sfntVersion = ReadUInt32BigEndian(reader); | ||
|
|
||
| // Check if it's a TrueType Collection (.ttc) | ||
| if (sfntVersion == 0x74746366) // 'ttcf' | ||
| { | ||
| // For TTC files, read the first font in the collection | ||
| reader.ReadUInt32(); // version | ||
| var numFonts = ReadUInt32BigEndian(reader); | ||
| if (numFonts == 0) | ||
| return null; | ||
|
|
||
| // Read offset to first font | ||
| var firstFontOffset = ReadUInt32BigEndian(reader); | ||
| fileStream.Seek(firstFontOffset, SeekOrigin.Begin); | ||
| reader.ReadUInt32(); // Skip sfntVersion of inner font | ||
| } | ||
| else if (sfntVersion != 0x00010000 && sfntVersion != 0x4F54544F) // Not TTF or OTF | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var numTables = ReadUInt16BigEndian(reader); | ||
| reader.ReadUInt16(); // searchRange | ||
| reader.ReadUInt16(); // entrySelector | ||
| reader.ReadUInt16(); // rangeShift | ||
|
|
||
| // Find the 'name' table | ||
| uint nameTableOffset = 0; | ||
| uint nameTableLength = 0; | ||
|
|
||
| for (int i = 0; i < numTables; i++) | ||
| { | ||
| var tag = Encoding.ASCII.GetString(reader.ReadBytes(4)); | ||
| reader.ReadUInt32(); // checksum | ||
| var offset = ReadUInt32BigEndian(reader); | ||
| var length = ReadUInt32BigEndian(reader); | ||
|
|
||
| if (tag == "name") | ||
| { | ||
| nameTableOffset = offset; | ||
| nameTableLength = length; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (nameTableOffset == 0) | ||
| return null; | ||
|
|
||
| fileStream.Seek(nameTableOffset, SeekOrigin.Begin); | ||
|
|
||
| var version = ReadUInt16BigEndian(reader); | ||
| var count = ReadUInt16BigEndian(reader); | ||
| var storageOffset = ReadUInt16BigEndian(reader); | ||
|
|
||
| string? familyName = null; | ||
| string? subfamilyName = null; | ||
| string? fullName = null; | ||
|
|
||
| for (int i = 0; i < count; i++) | ||
| { | ||
| var platformID = ReadUInt16BigEndian(reader); | ||
| var encodingID = ReadUInt16BigEndian(reader); | ||
| var languageID = ReadUInt16BigEndian(reader); | ||
| var nameID = ReadUInt16BigEndian(reader); | ||
| var length = ReadUInt16BigEndian(reader); | ||
| var stringOffset = ReadUInt16BigEndian(reader); | ||
|
|
||
| var isWindows = platformID == 3 && languageID == 0x0409; | ||
| var isUnicode = platformID == 0 && (languageID == 0 || languageID == 0x0409); | ||
|
|
||
| if (!isWindows && !isUnicode) | ||
| continue; | ||
|
|
||
| var currentPos = fileStream.Position; | ||
|
|
||
| fileStream.Seek(nameTableOffset + storageOffset + stringOffset, SeekOrigin.Begin); | ||
| var stringBytes = reader.ReadBytes(length); | ||
|
|
||
| string stringValue; | ||
| if (platformID == 3 || platformID == 0) // Windows or Unicode - UTF-16BE | ||
| { | ||
| stringValue = Encoding.BigEndianUnicode.GetString(stringBytes); | ||
| } | ||
| else // Macintosh - ASCII/Latin1 | ||
| { | ||
| stringValue = Encoding.ASCII.GetString(stringBytes); | ||
| } | ||
|
|
||
| if (nameID == 4) | ||
| fullName = stringValue; | ||
| else if (nameID == 1) | ||
| familyName = stringValue; | ||
| else if (nameID == 2) | ||
| subfamilyName = stringValue; | ||
|
|
||
| fileStream.Seek(currentPos, SeekOrigin.Begin); | ||
| } | ||
|
|
||
| if (!string.IsNullOrEmpty(fullName)) | ||
| return fullName; | ||
|
|
||
| if (!string.IsNullOrEmpty(familyName) && !string.IsNullOrEmpty(subfamilyName)) | ||
| { | ||
| if (subfamilyName.Equals("Regular", StringComparison.OrdinalIgnoreCase)) | ||
| return familyName; | ||
|
|
||
| return $"{familyName} {subfamilyName}"; | ||
| } | ||
|
|
||
| return familyName; | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static uint ReadUInt32BigEndian(BinaryReader reader) | ||
| { | ||
| var bytes = reader.ReadBytes(4); | ||
| if (BitConverter.IsLittleEndian) | ||
| Array.Reverse(bytes); | ||
| return BitConverter.ToUInt32(bytes, 0); | ||
| } | ||
|
|
||
| private static ushort ReadUInt16BigEndian(BinaryReader reader) | ||
| { | ||
| var bytes = reader.ReadBytes(2); | ||
| if (BitConverter.IsLittleEndian) | ||
| Array.Reverse(bytes); | ||
| return BitConverter.ToUInt16(bytes, 0); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.