-
-
Notifications
You must be signed in to change notification settings - Fork 944
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3fa6b0d
feat: [SystemFontProvider] - Implement GetFontPath for Linux
Jklawreszuk 5c35047
fixup! feat: [SystemFontProvider] - Implement GetFontPath for Linux
Jklawreszuk 8817de3
fixup! feat: [SystemFontProvider] - Implement GetFontPath for Linux
Jklawreszuk a316aba
fixup! feat: [SystemFontProvider] - Implement GetFontPath for Linux
Jklawreszuk 7b4408e
fixup! feat: [SystemFontProvider] - Implement GetFontPath for Linux
Jklawreszuk e976110
fixup! feat: [SystemFontProvider] - Implement GetFontPath for Linux
Jklawreszuk 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 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 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 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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
|
||
using System.Linq; | ||
using SharpDX.DirectWrite; | ||
using Stride.Core.Assets.Compiler; | ||
using SharpFont; | ||
using Stride.Assets.SpriteFont.Compiler; | ||
using Stride.Core; | ||
using Stride.Core.Assets.Compiler; | ||
using Stride.Core.Diagnostics; | ||
using Stride.Assets.SpriteFont.Compiler; | ||
using Stride.Graphics.Font; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Stride.Assets.SpriteFont | ||
{ | ||
|
@@ -17,11 +18,9 @@ public class SystemFontProvider : FontProviderBase | |
{ | ||
private static readonly Logger Log = GlobalLogger.GetLogger("SystemFontProvider"); | ||
|
||
public const string DefaultFontName = "Arial"; | ||
|
||
public SystemFontProvider() | ||
{ | ||
FontName = DefaultFontName; | ||
FontName = GetDefaultFontName(); | ||
} | ||
|
||
public SystemFontProvider(string fontName) | ||
|
@@ -52,72 +51,112 @@ public SystemFontProvider(string fontName) | |
/// <inheritdoc/> | ||
public override FontFace GetFontFace() | ||
{ | ||
var factory = new Factory(); | ||
using var factory = new Factory(); | ||
|
||
SharpDX.DirectWrite.Font font; | ||
Font font; | ||
using (var fontCollection = factory.GetSystemFontCollection(false)) | ||
{ | ||
int index; | ||
if (!fontCollection.FindFamilyName(FontName, out index)) | ||
if (!fontCollection.FindFamilyName(FontName, out var index)) | ||
{ | ||
// Lets try to import System.Drawing for old system bitmap fonts (like MS Sans Serif) | ||
throw new FontNotFoundException(FontName); | ||
} | ||
|
||
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); | ||
} | ||
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); | ||
} | ||
|
||
return new FontFace(font); | ||
} | ||
|
||
public override string GetFontPath(AssetCompilerResult result = null) | ||
{ | ||
using (var factory = new Factory()) | ||
if (OperatingSystem.IsWindows()) | ||
return GetFontPathWindows(result); | ||
if (OperatingSystem.IsLinux()) | ||
{ | ||
Font font; | ||
var fontPath = GetFontPathLinux(result); | ||
if (fontPath == null && FontName != GetDefaultFontName()) | ||
{ | ||
result?.Warning($"Cannot find font family '{FontName}'. Loading default font '{GetDefaultFontName()}' instead"); | ||
FontName = GetDefaultFontName(); | ||
fontPath = GetFontPathLinux(result); | ||
} | ||
return fontPath; | ||
} | ||
return null; | ||
} | ||
|
||
using (var fontCollection = factory.GetSystemFontCollection(false)) | ||
private string GetFontPathLinux(AssetCompilerResult result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIt: code style, empty line between methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I added new line |
||
{ | ||
StyleFlags flags = StyleFlags.None; | ||
if (Style.IsBold()) | ||
flags |= StyleFlags.Bold; | ||
if (Style.IsItalic()) | ||
flags |= StyleFlags.Italic; | ||
|
||
string systemFontDirectory = "/usr/share/fonts"; | ||
var files = System.IO.Directory.EnumerateFiles(systemFontDirectory, "*.ttf", System.IO.SearchOption.AllDirectories); | ||
|
||
var library = new SharpFont.Library(); | ||
foreach (string file in files) | ||
{ | ||
var face = new Face(library, file); | ||
if (face.FamilyName.Contains(FontName) && face.StyleFlags == flags) | ||
{ | ||
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; | ||
} | ||
} | ||
result?.Error($"Cannot find style '{Style}' for font family '{FontName}'. Make sure it is installed on this machine."); | ||
return null; | ||
} | ||
|
||
private string GetFontPathWindows(AssetCompilerResult result) | ||
{ | ||
using var factory = new Factory(); | ||
Font font; | ||
|
||
var fontFace = new FontFace(font); | ||
using (var fontCollection = factory.GetSystemFontCollection(false)) | ||
{ | ||
if (!fontCollection.FindFamilyName(FontName, out var index)) | ||
{ | ||
result?.Error($"Cannot find system font '{FontName}'. Make sure it is installed on this machine."); | ||
return null; | ||
} | ||
|
||
// get the font path on the hard drive | ||
var file = fontFace.GetFiles().First(); | ||
var referenceKey = file.GetReferenceKey(); | ||
var originalLoader = (FontFileLoaderNative)file.Loader; | ||
var loader = originalLoader.QueryInterface<LocalFontFileLoader>(); | ||
return loader.GetFilePath(referenceKey); | ||
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; | ||
} | ||
} | ||
|
||
var fontFace = new FontFace(font); | ||
|
||
// get the font path on the hard drive | ||
var file = fontFace.GetFiles().First(); | ||
var referenceKey = file.GetReferenceKey(); | ||
var originalLoader = (FontFileLoaderNative)file.Loader; | ||
var loader = originalLoader.QueryInterface<LocalFontFileLoader>(); | ||
return loader.GetFilePath(referenceKey); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override string GetFontName() | ||
{ | ||
return FontName; | ||
} | ||
|
||
private static string GetDefaultFontName() | ||
{ | ||
//Note : Both macOS and Windows contains Arial | ||
return OperatingSystem.IsLinux() ? "Liberation" : "Arial"; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: sort usings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
There was a problem hiding this comment.
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.