-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support reading fonts from files (#14783)
- Loading branch information
1 parent
05d68fd
commit 55091d4
Showing
6 changed files
with
116 additions
and
34 deletions.
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
Binary file added
BIN
+2.05 MB
src/Core/tests/DeviceTests/Platforms/Windows/Assets/Fonts/insubfolder.ttf
Binary file not shown.
40 changes: 40 additions & 0 deletions
40
src/Core/tests/DeviceTests/Services/FontManagerTests.Windows.cs
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Maui.DeviceTests; | ||
|
||
public partial class FontManagerTests : TestBase | ||
{ | ||
[Theory] | ||
[InlineData("dokdo_regular", "ms-appx:///dokdo_regular.ttf#Dokdo")] | ||
[InlineData("dokdo_regular.ttf", "ms-appx:///dokdo_regular.ttf#Dokdo")] | ||
[InlineData("dokdo_regular#dokdo", "ms-appx:///dokdo_regular.ttf#Dokdo")] | ||
[InlineData("dokdo_regular.ttf#dokdo", "ms-appx:///dokdo_regular.ttf#Dokdo")] | ||
[InlineData("myalias", "ms-appx:///dokdo_regular.ttf#Dokdo")] | ||
public Task CanLoadFonts(string fontName, string actualFontFamily) => | ||
InvokeOnMainThreadAsync(() => | ||
{ | ||
var registrar = new FontRegistrar(fontLoader: null); | ||
registrar.Register("dokdo_regular.ttf", "myalias"); | ||
var manager = new FontManager(registrar); | ||
|
||
var actual = manager.GetFontFamily(Font.OfSize(fontName, 12, FontWeight.Regular)); | ||
|
||
Assert.Equal(actualFontFamily, actual.Source); | ||
}); | ||
|
||
// TODO: this is not going to work in unpackaged | ||
[Fact] | ||
public Task CanLoadEmbeddedFont() => | ||
InvokeOnMainThreadAsync(() => | ||
{ | ||
var registrar = new FontRegistrar(new EmbeddedFontLoader()); | ||
registrar.Register("dokdo_regular.ttf", "myalias", GetType().Assembly); | ||
var manager = new FontManager(registrar); | ||
|
||
var actual = manager.GetFontFamily(Font.OfSize("myalias", 12, FontWeight.Regular)); | ||
|
||
Assert.Equal("ms-appdata:///local/fonts/dokdo_regular.ttf#Dokdo", actual.Source); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#nullable enable | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using Microsoft.Maui.ApplicationModel; | ||
using Package = Windows.ApplicationModel.Package; | ||
|
||
namespace Microsoft.Maui.Storage | ||
{ | ||
static partial class FileSystemUtils | ||
{ | ||
public static bool AppPackageFileExists(string filename) | ||
{ | ||
var file = PlatformGetFullAppPackageFilePath(filename); | ||
return File.Exists(file); | ||
} | ||
|
||
public static string PlatformGetFullAppPackageFilePath(string filename) | ||
{ | ||
if (filename is null) | ||
throw new ArgumentNullException(nameof(filename)); | ||
|
||
filename = NormalizePath(filename); | ||
|
||
string root; | ||
if (AppInfoUtils.IsPackagedApp) | ||
root = Package.Current.InstalledLocation.Path; | ||
else | ||
root = AppContext.BaseDirectory; | ||
|
||
return Path.Combine(root, filename); | ||
} | ||
|
||
public static bool TryGetAppPackageFileUri(string filename, [NotNullWhen(true)] out string? uri) | ||
{ | ||
var path = PlatformGetFullAppPackageFilePath(filename); | ||
|
||
if (File.Exists(path)) | ||
{ | ||
if (AppInfoUtils.IsPackagedApp) | ||
uri = $"ms-appx:///{filename.Replace('\\', '/')}"; | ||
else | ||
uri = $"file:///{path.Replace('\\', '/')}"; | ||
|
||
return true; | ||
} | ||
|
||
uri = null; | ||
return false; | ||
} | ||
} | ||
} |