Skip to content

Commit

Permalink
fix(fonts): Adjust font loading for ms-appx resources from assemblies
Browse files Browse the repository at this point in the history
- Font loading now uses common ms-appx path building properly to allow for arbitrary font loading.
- Enables dynamic inclusion of font files on apple targets
  • Loading branch information
jeromelaban committed Jan 17, 2023
1 parent 6cf8e3b commit 88be67d
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.Diagnostics.CodeAnalysis
{
#if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48 || (!NET6_0_OR_GREATER && (__IOS__ || __ANDROID__ || __MACOS__))
#if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
#if SYSTEM_PRIVATE_CORELIB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;

#endregion

Expand Down Expand Up @@ -87,22 +88,23 @@ public static LineSplitEnumerator SplitLines(this ReadOnlySpan<char> instance)
return new LineSplitEnumerator(instance);
}

public static bool IsNullOrEmpty(this string instance)

public static bool IsNullOrEmpty([NotNullWhen(false)] this string instance)
{
return string.IsNullOrEmpty(instance);
}

public static bool IsNullOrWhiteSpace(this string instance)
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string instance)
{
return string.IsNullOrWhiteSpace(instance);
}

public static bool HasValue(this string instance)
public static bool HasValue([NotNullWhen(true)] this string instance)
{
return !string.IsNullOrWhiteSpace(instance);
}

public static bool HasValueTrimmed(this string instance)
public static bool HasValueTrimmed([NotNullWhen(true)] this string instance)
{
return !string.IsNullOrWhiteSpace(instance);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Uno.Foundation/Uno.Foundation.Reference.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Extensions\Nullable.cs" />
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Include="**\*.cs" Exclude="bin\**\*.cs;obj\**\*.cs;" Visible="False" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Uno.Foundation/Uno.Foundation.Skia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<ProjectReference Include="..\Uno.Foundation.Logging\Uno.Foundation.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Extensions\Nullable.cs" />
</ItemGroup>

<Import Project="..\Uno.CrossTargetting.props" />
<Import Project="Uno.Core.Extensions\Uno.Core.Extensions.props" />

Expand Down
4 changes: 4 additions & 0 deletions src/Uno.Foundation/Uno.Foundation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<UpToDateCheckInput Include="**\*.cs" Exclude="bin\**\*.cs;obj\**\*.cs;" Visible="False" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Extensions\Nullable.cs" />
</ItemGroup>

<Import Project="..\Uno.CrossTargetting.props" />
<Import Project="Uno.Core.Extensions\Uno.Core.Extensions.props" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
<UpToDateCheckInput Include="**\*.cs" Exclude="bin\**\*.cs;obj\**\*.cs;" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Helpers\Nullable.cs" />
</ItemGroup>

<ItemGroup>
<Content Include="buildTransitive\Uno.UI.RemoteControl.targets">
<PackagePath>build</PackagePath>
Expand Down
6 changes: 1 addition & 5 deletions src/Uno.UI.RemoteControl/Uno.UI.RemoteControl.Skia.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildRuntimeType)'=='Core' or '$(IsRunningInsideVisualStudio)'=='true'">
Expand Down Expand Up @@ -36,10 +36,6 @@
<UpToDateCheckInput Include="**\*.cs" Exclude="bin\**\*.cs;obj\**\*.cs;" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Helpers\Nullable.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="LinkerDefinition.Wasm.xml">
<LogicalName>$(AssemblyName).xml</LogicalName>
Expand Down
4 changes: 0 additions & 4 deletions src/Uno.UI.RemoteControl/Uno.UI.RemoteControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
<UpToDateCheckInput Include="**\*.cs" Exclude="bin\**\*.cs;obj\**\*.cs;" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Helpers\Nullable.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'!='netstandard2.0'">
<EmbeddedResource Include="LinkerDefinition.Xamarin.xml">
<LogicalName>$(AssemblyName).xml</LogicalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
<UpToDateCheckInput Include="**\*.cs" Exclude="bin\**\*.cs;obj\**\*.cs;" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SourceGenerators\SourceGeneratorHelpers\Helpers\Nullable.cs" Link="Helpers\Nullable.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'!='netstandard2.0'">
<EmbeddedResource Include="LinkerDefinition.Xamarin.xml">
<LogicalName>$(AssemblyName).xml</LogicalName>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,35 @@ public void When_Inlines_XamlRoot()
Assert.AreEqual(SUT.XamlRoot, inline.XamlRoot);
}
}

[TestMethod]
[RunsOnUIThread]
public async Task When_FontFamily_In_Separate_Assembly()
{
var SUT = new TextBlock { Text = "\xE102\xE102\xE102\xE102\xE102" };
WindowHelper.WindowContent = SUT;
await WindowHelper.WaitForIdle();

var size = new Size(1000, 1000);
SUT.Measure(size);

var originalSize = SUT.DesiredSize;

Assert.AreNotEqual(0, SUT.DesiredSize.Width);
Assert.AreNotEqual(0, SUT.DesiredSize.Height);

SUT.FontFamily = new Windows.UI.Xaml.Media.FontFamily("ms-appx://Uno.UI.RuntimeTests/Assets/Fonts/uno-fluentui-assets-v2.ttf");

int counter = 3;

do
{
await WindowHelper.WaitForIdle();
await Task.Delay(100);
}
while (SUT.DesiredSize == originalSize && counter-- > 0);

Assert.AreNotEqual(originalSize, SUT.DesiredSize);
}
}
}
4 changes: 4 additions & 0 deletions src/Uno.UI.RuntimeTests/Uno.UI.RuntimeTests.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@

<UpToDateCheckInput Include="$(PlatformItemsBasePath)**\*.xaml" Exclude="$(PlatformItemsBasePath)bin\**\*.xaml;$(PlatformItemsBasePath)obj\**\*.xaml" />
</ItemGroup>

<ItemGroup>
<None Remove="Assets\Fonts\uno-fluentui-assets-v2.ttf" />
</ItemGroup>
</Project>
60 changes: 46 additions & 14 deletions src/Uno.UI/Controls/NSFontHelper.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,32 +199,64 @@ private static NSFont ApplyStyle(NSFont font, nfloat size, FontStyle fontStyle)
private static NSFont? GetFontFromFile(nfloat size, string file)
{
var fileName = Path.GetFileNameWithoutExtension(file);
var fileExtension = Path.GetExtension(file)?.Replace(".", "") ?? "";

var url = NSBundle
.MainBundle
.GetUrlForResource(
name: fileName,
fileExtension: fileExtension,
subdirectory: "Fonts"
);

if (url == null)
var fileExtension = Path.GetExtension(file)!.Substring(1);

var url = file.Contains("/")

// Search the file using the appropriate subdirectory
? NSBundle
.MainBundle
.GetUrlForResource(
name: fileName,
fileExtension: fileExtension,
subdirectory: Path.GetDirectoryName(file))

// Legacy behavior when fonts were located in the fonts folder.
: NSBundle
.MainBundle
.GetUrlForResource(
name: fileName,
fileExtension: fileExtension,
subdirectory: "Fonts");

if (url is null)
{
if (typeof(NSFontHelper).Log().IsEnabled(LogLevel.Debug))
{
typeof(NSFontHelper).Log().Debug($"Unable to find font in bundle using {file}");
}

return null;
}

var fontData = NSData.FromUrl(url);
if (fontData == null)
if (fontData is null)
{
if (typeof(NSFontHelper).Log().IsEnabled(LogLevel.Debug))
{
typeof(NSFontHelper).Log().Debug($"Unable to load font in bundle using {url}");
}

return null;
}

//iOS loads NSFonts based on the PostScriptName of the font file
using var fontProvider = new CGDataProvider(fontData);
using var font = CGFont.CreateFromProvider(fontProvider);
var font = CGFont.CreateFromProvider(fontProvider);

return font != null ? NSFont.FromFontName(font.PostScriptName, size) : null;
if (font is not null && CoreText.CTFontManager.RegisterGraphicsFont(font, out var error))
{
return NSFont.FromFontName(font.PostScriptName, size);
}
else
{
if (typeof(NSFontHelper).Log().IsEnabled(LogLevel.Debug))
{
typeof(NSFontHelper).Log().Debug($"Unable to register font from {file}");
}

return null;
}
}
#endregion

Expand Down
Loading

0 comments on commit 88be67d

Please sign in to comment.