Skip to content
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

feat: [SystemFontProvider] - Implement GetFontPath for Linux #2295

Merged
merged 6 commits into from
May 29, 2024

Conversation

Jklawreszuk
Copy link
Collaborator

PR Details

This PR refactors GetFontPath to find system fonts for Linux for Asset Compiler.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

Copy link
Collaborator

@Eideren Eideren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a bunch @Jklawreszuk

Comment on lines 89 to 101
}
private string GetFontPathLinux(AssetCompilerResult result)
{
var bold = Style.IsBold() ? "Bold" : "";
var italic = Style.IsItalic() ? "Italic" : "";
string systemFontDirectory = "/usr/share/fonts";
string[] files = System.IO.Directory.GetFiles(systemFontDirectory, "*.ttf", System.IO.SearchOption.AllDirectories);

using (var fontCollection = factory.GetSystemFontCollection(false))
foreach (string file in files)
{
if (file.Contains(FontName) && file.Contains(bold) && file.Contains(italic))
{
int index;
if (!fontCollection.FindFamilyName(FontName, out index))
{
result?.Error($"Cannot find system font '{FontName}'. Make sure it is installed on this machine.");
return null;
}

using (var fontFamily = fontCollection.GetFontFamily(index))
{
var weight = Style.IsBold() ? FontWeight.Bold : FontWeight.Regular;
var style = Style.IsItalic() ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal;
font = fontFamily.GetFirstMatchingFont(weight, FontStretch.Normal, style);
if (font == null)
{
result?.Error($"Cannot find style '{Style}' for font family {FontName}. Make sure it is installed on this machine.");
return null;
}
}
return file;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing there are less hacky ways to get the style from a font, probably have to read the .ttf though ? If that's the case, can you add a todo here for this ?

Insert a line break between this function and the previous one.
GetFiles is fairly inefficient for this specific use case, best replace it with a Directory.EnumerateFiles(systemFontDirectory, "*.ttf", System.IO.SearchOption.AllDirectories) instead.

Copy link
Collaborator Author

@Jklawreszuk Jklawreszuk May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing there are less hacky ways to get the style from a font, probably have to read the .ttf though

Yeah, I'll try to see if it is possible to verify the style with our current FreeType binding, but common convetion I noticed is also separating styles into multiple files. I'm not though sure is always the case - see C:\Windows\Fonts where all fonts have single file possibly containing multiple styles. Looks like separating font styles into multiple files is standard but assuming font has 'Bold' or 'Italics' in their name is bad idea

Perhaps I should also consider adding a specific font to the Search regex. Something like this:
Directory.EnumerateFiles(systemFontDirectory, $"{FontName}*.ttf", System.IO.SearchOption.AllDirectories)

@Jklawreszuk Jklawreszuk marked this pull request as draft May 29, 2024 11:44
@Jklawreszuk Jklawreszuk marked this pull request as ready for review May 29, 2024 14:37
@Jklawreszuk Jklawreszuk requested a review from Eideren May 29, 2024 14:37
using System.Linq;
using SharpDX.DirectWrite;
using Stride.Core.Assets.Compiler;
using Stride.Core;
using Stride.Core.Diagnostics;
using Stride.Assets.SpriteFont.Compiler;
using Stride.Graphics.Font;
using System;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: sort usings

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually. System usings are first and then the rest in dictionary order. But that's ok here.

We might even use implicit usings later which would hide those.

}

return new FontFace(font);
}

public override string GetFontPath(AssetCompilerResult result = null)
{
using (var factory = new Factory())
if(OperatingSystem.IsWindows())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: code style, space after opening bracket

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant if ( instead of if(

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right 🤦 Done, I also updated other if statements

}
return null;
}
private string GetFontPathLinux(AssetCompilerResult result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIt: code style, empty line between methods

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I added new line

@Eideren Eideren merged commit 190da51 into stride3d:master May 29, 2024
13 checks passed
@Eideren
Copy link
Collaborator

Eideren commented May 29, 2024

Thanks !

@Jklawreszuk Jklawreszuk deleted the system-font-linux branch May 29, 2024 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants