Skip to content

Commit

Permalink
Merge pull request #17421 from unoplatform/mergify/bp/release/stable/…
Browse files Browse the repository at this point in the history
…5.3/pr-17118

feat: Font manifest (backport #17118)
  • Loading branch information
jeromelaban authored Jul 8, 2024
2 parents 9eef076 + cf47896 commit e6f2a45
Show file tree
Hide file tree
Showing 59 changed files with 1,251 additions and 637 deletions.
1 change: 1 addition & 0 deletions build/nuget/Uno.WinUI.Skia.Gtk.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<IsUnoHead>true</IsUnoHead>
<UnoRuntimeIdentifier>Skia</UnoRuntimeIdentifier>
<SupportsFontManifest>true</SupportsFontManifest>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions build/nuget/Uno.WinUI.Skia.Linux.FrameBuffer.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<IsUnoHead>true</IsUnoHead>
<UnoRuntimeIdentifier>Skia</UnoRuntimeIdentifier>
<SupportsFontManifest>true</SupportsFontManifest>
<UnoIsSkiaLinuxFramebufferHead>true</UnoIsSkiaLinuxFramebufferHead>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions build/nuget/Uno.WinUI.Skia.MacOS.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<IsUnoHead>true</IsUnoHead>
<UnoRuntimeIdentifier>Skia</UnoRuntimeIdentifier>
<SupportsFontManifest>true</SupportsFontManifest>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions build/nuget/Uno.WinUI.Skia.Wpf.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<IsUnoHead>true</IsUnoHead>
<UnoRuntimeIdentifier>Skia</UnoRuntimeIdentifier>
<SupportsFontManifest>true</SupportsFontManifest>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions build/nuget/Uno.WinUI.Skia.X11.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<IsUnoHead>true</IsUnoHead>
<UnoRuntimeIdentifier>Skia</UnoRuntimeIdentifier>
<SupportsFontManifest>true</SupportsFontManifest>
</PropertyGroup>

</Project>
5 changes: 4 additions & 1 deletion build/nuget/uno.winui.common.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
'$(UnoDisableOmnisharpTargetsImport)'==''
and '$(UnoForceSingleTFM)' == 'true'
and Exists($([MSBuild]::GetPathOfFileAbove('uno.vscode.props', '$(MSBuildThisFileDirectory)../.vscode/')))" />


<PropertyGroup>
<SupportsFontManifest Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))=='ios'">true</SupportsFontManifest>
</PropertyGroup>
</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"fonts": [
{
"font_style": "italic",
"font_weight": 400,
"font_stretch": "Condensed",
"family_name": "ms-appx:///Assets/Fonts/OpenSans/OpenSans_Condensed-MediumItalic.ttf"
},
{
"font_style": "normal",
"font_weight": 400,
"font_stretch": "SemiCondensed",
"family_name": "ms-appx:///Assets/Fonts/OpenSans/OpenSans_SemiCondensed-Regular.ttf"
},
{
"font_style": "normal",
"font_weight": 600,
"font_stretch": "SemiCondensed",
"family_name": "ms-appx:///Assets/Fonts/OpenSans/OpenSans_SemiCondensed-SemiBold.ttf"
},
{
"font_style": "normal",
"font_weight": 700,
"font_stretch": "Normal",
"family_name": "ms-appx:///Assets/Fonts/OpenSans/OpenSans-Bold.ttf"
},
{
"font_style": "normal",
"font_weight": 400,
"font_stretch": "Normal",
"family_name": "ms-appx:///Assets/Fonts/OpenSans/OpenSans-Regular.ttf"
},
]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9574,6 +9574,8 @@
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\Even Badder Mofo.ttf" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\FamilyGuy-4grW.ttf" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\Nillambari-K7y1W.ttf" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\OpenSans\*.ttf" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\OpenSans\OpenSans.ttf.manifest" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\Renaiss-Italic.ttf" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Fonts\RoteFlora.ttf" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Formats\animated.gif" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,96 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
[RunsOnUIThread]
public class Given_TextBlock
{
#if __ANDROID__
[Ignore("Visually looks good, but fails :(")]
#endif
[TestMethod]
[DataRow((ushort)400, FontStyle.Italic, FontStretch.Condensed, "ms-appx:///Assets/Fonts/OpenSans/OpenSans_Condensed-MediumItalic.ttf")]
[DataRow((ushort)400, FontStyle.Normal, FontStretch.SemiCondensed, "ms-appx:///Assets/Fonts/OpenSans/OpenSans_SemiCondensed-Regular.ttf")]
[DataRow((ushort)600, FontStyle.Normal, FontStretch.SemiCondensed, "ms-appx:///Assets/Fonts/OpenSans/OpenSans_SemiCondensed-SemiBold.ttf")]
[DataRow((ushort)700, FontStyle.Normal, FontStretch.Normal, "ms-appx:///Assets/Fonts/OpenSans/OpenSans-Bold.ttf")]
[DataRow((ushort)400, FontStyle.Normal, FontStretch.Normal, "ms-appx:///Assets/Fonts/OpenSans/OpenSans-Regular.ttf")]
public async Task When_Font_Has_Manifest(ushort weight, FontStyle style, FontStretch stretch, string ttfFile)
{
if (!ApiInformation.IsTypePresent("Microsoft.UI.Xaml.Media.Imaging.RenderTargetBitmap"))
{
Assert.Inconclusive(); // System.NotImplementedException: RenderTargetBitmap is not supported on this platform.;
}

var SUT = new TextBlock
{
Text = "Hello World!",
FontSize = 18,
FontStyle = style,
FontStretch = stretch,
FontWeight = new FontWeight(weight),
FontFamily = new FontFamily("ms-appx:///Assets/Fonts/OpenSans/OpenSans.ttf"),
};
var SUTContainer = new Border()
{
Width = 200,
Height = 100,
Child = SUT
};

var expectedTB = new TextBlock
{
Text = "Hello World!",
FontSize = 18,
FontFamily = new FontFamily(ttfFile)
};
var expectedTBContainer = new Border()
{
Width = 200,
Height = 100,
Child = expectedTB
};

var differentTtf = "ms-appx:///Assets/Fonts/OpenSans/OpenSans-Bold.ttf";
if (ttfFile == differentTtf)
{
differentTtf = "ms-appx:///Assets/Fonts/OpenSans/OpenSans-Regular.ttf";
}

var differentTB = new TextBlock
{
Text = "Hello World!",
FontSize = 18,
FontFamily = new FontFamily(differentTtf),
};
var differentTBContainer = new Border()
{
Width = 200,
Height = 100,
Child = differentTB
};

var sp = new StackPanel()
{
Children =
{
SUTContainer,
expectedTBContainer,
differentTBContainer,
},
};

await UITestHelper.Load(sp);
var actual = await UITestHelper.ScreenShot(SUTContainer);
var expected = await UITestHelper.ScreenShot(expectedTBContainer);
var different = await UITestHelper.ScreenShot(differentTBContainer);
await ImageAssert.AreEqualAsync(actual, expected);
await ImageAssert.AreNotEqualAsync(actual, different);
}

#if __SKIA__
[TestMethod]
// It looks like CI might not have any installed fonts with Chinese characters which could cause the test to fail
[Ignore("Fails on CI")]
public async Task Check_FontFallback()
{
var SUT = new TextBlock { Text = "示例文本", FontSize = 24 };
var skFont = FontDetailsCache.GetFont(SUT.FontFamily?.Source, (float)SUT.FontSize, SUT.FontWeight, SUT.FontStyle).SKFont;
var skFont = FontDetailsCache.GetFont(SUT.FontFamily?.Source, (float)SUT.FontSize, SUT.FontWeight, SUT.FontStretch, SUT.FontStyle).SKFont;
Assert.IsFalse(skFont.ContainsGlyph(SUT.Text[0]));

var fallbackFont = SKFontManager.Default.MatchCharacter(SUT.Text[0]);
Expand Down
22 changes: 12 additions & 10 deletions src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#nullable enable
using System.Threading;
using System;
using System.Threading.Tasks;
using Uno;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Uno;

namespace Uno.UI.Toolkit;

Expand All @@ -16,14 +16,16 @@ public partial class StorageFileHelper
/// </summary>
/// <param name="fileName">relative file path</param>
/// <returns>A task that will complete with a result of true if file exists, otherwise with a result of false.</returns>
public static async Task<bool> ExistsInPackage(string fileName) => await FileExistsInPackage(fileName);

#if IS_UNIT_TESTS || __NETSTD_REFERENCE__
private static Task<bool> FileExistsInPackage(string fileName)
=> throw new NotImplementedException();
public static async Task<bool> ExistsInPackage(string fileName)
{
#if HAS_UNO
return await Windows.Storage.Helpers.StorageFileHelper.ExistsInPackage(fileName);
#else
return await FileExistsInPackage(fileName);
#endif
}

#if __SKIA__ || WINDOWS || WINAPPSDK || WINDOWS_UWP || WINUI
#if !HAS_UNO
private static Task<bool> FileExistsInPackage(string fileName)
{
var executingPath = Assembly.GetExecutingAssembly().Location;
Expand Down
25 changes: 0 additions & 25 deletions src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs

This file was deleted.

15 changes: 9 additions & 6 deletions src/Uno.UI/Controls/PaintPool.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class TextPaintPool
private record Entry(
FontWeight FontWeight,
FontStyle FontStyle,
FontStretch FontStretch,
FontFamily FontFamily,
double FontSize,
double CharacterSpacing,
Expand All @@ -38,6 +39,7 @@ private class EntryComparer : IEqualityComparer<Entry>
public bool Equals(Entry x, Entry y) =>
x.FontWeight == y.FontWeight
&& x.FontStyle == y.FontStyle
&& x.FontStretch == y.FontStretch
&& x.FontFamily == y.FontFamily
&& x.Foreground == y.Foreground
&& x.FontSize == y.FontSize
Expand All @@ -48,6 +50,7 @@ public bool Equals(Entry x, Entry y) =>
public int GetHashCode(Entry entry) =>
entry.FontWeight.GetHashCode()
^ entry.FontStyle.GetHashCode()
^ entry.FontStretch.GetHashCode()
^ entry.FontFamily?.GetHashCode() ?? 0
^ entry.Foreground.GetHashCode()
^ entry.FontSize.GetHashCode()
Expand All @@ -74,19 +77,19 @@ public int GetHashCode(Entry entry) =>
/// can be tricky to place properly.
/// </remarks>
/// <returns>A <see cref="TextPaint"/> instance.</returns>
public static TextPaint GetPaint(FontWeight fontWeight, FontStyle fontStyle, FontFamily fontFamily, double fontSize, double characterSpacing, Windows.UI.Color foreground, Shader shader, BaseLineAlignment baselineAlignment, TextDecorations textDecorations)
public static TextPaint GetPaint(FontWeight fontWeight, FontStyle fontStyle, FontStretch fontStretch, FontFamily fontFamily, double fontSize, double characterSpacing, Windows.UI.Color foreground, Shader shader, BaseLineAlignment baselineAlignment, TextDecorations textDecorations)
{
if (shader != null)
{
// The "Shader" native object can't be use as a cache key
return InnerBuildPaint(fontWeight, fontStyle, fontFamily, fontSize, characterSpacing, foreground, shader, baselineAlignment, textDecorations);
return InnerBuildPaint(fontWeight, fontStyle, fontStretch, fontFamily, fontSize, characterSpacing, foreground, shader, baselineAlignment, textDecorations);
}

var key = new Entry(fontWeight, fontStyle, fontFamily, fontSize, characterSpacing, foreground, baselineAlignment, textDecorations);
var key = new Entry(fontWeight, fontStyle, fontStretch, fontFamily, fontSize, characterSpacing, foreground, baselineAlignment, textDecorations);

if (!_entries.TryGetValue(key, out var paint))
{
_entries.Add(key, paint = InnerBuildPaint(fontWeight, fontStyle, fontFamily, fontSize, characterSpacing, foreground, shader, baselineAlignment, textDecorations));
_entries.Add(key, paint = InnerBuildPaint(fontWeight, fontStyle, fontStretch, fontFamily, fontSize, characterSpacing, foreground, shader, baselineAlignment, textDecorations));
_entriesList.Add(key);

TryScavenge();
Expand Down Expand Up @@ -123,15 +126,15 @@ private static void TryScavenge()
}
}

private static TextPaint InnerBuildPaint(FontWeight fontWeight, FontStyle fontStyle, FontFamily fontFamily, double fontSize, double characterSpacing, Color foreground, Shader shader, BaseLineAlignment baselineAlignment, TextDecorations textDecorations)
private static TextPaint InnerBuildPaint(FontWeight fontWeight, FontStyle fontStyle, FontStretch fontStretch, FontFamily fontFamily, double fontSize, double characterSpacing, Color foreground, Shader shader, BaseLineAlignment baselineAlignment, TextDecorations textDecorations)
{
var paintSpecs = BuildPaintValueSpecs(fontSize, characterSpacing);

return TextPaintPoolNative.BuildPaint(
paintSpecs.density,
paintSpecs.textSize,
paintSpecs.letterSpacing,
FontHelper.FontFamilyToTypeFace(fontFamily, fontWeight, fontStyle),
FontHelper.FontFamilyToTypeFace(fontFamily, fontWeight, fontStyle, fontStretch),
(int)((Android.Graphics.Color)foreground),
(textDecorations & TextDecorations.Underline) == TextDecorations.Underline,
(textDecorations & TextDecorations.Strikethrough) == TextDecorations.Strikethrough,
Expand Down
Loading

0 comments on commit e6f2a45

Please sign in to comment.