Browse Source

Fix font fallback for composite keys containing relative URIs (#14943)

* Add failing test for font fallback with relative URI

* Fix font fallback for composite keys containing relative URIs
pull/14952/head
Julien Lebosquain 2 years ago
committed by GitHub
parent
commit
fb632f0460
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 23
      src/Avalonia.Base/Media/FontManager.cs
  2. 8
      tests/Avalonia.Skia.UnitTests/Media/FontManagerTests.cs

23
src/Avalonia.Base/Media/FontManager.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
using Avalonia.Media.Fonts; using Avalonia.Media.Fonts;
@ -141,22 +142,7 @@ namespace Avalonia.Media
private bool TryGetGlyphTypefaceByKeyAndName(Typeface typeface, FontFamilyKey key, string familyName, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) private bool TryGetGlyphTypefaceByKeyAndName(Typeface typeface, FontFamilyKey key, string familyName, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface)
{ {
var source = key.Source; var source = key.Source.EnsureAbsolute(key.BaseUri);
if (!source.IsAbsoluteUri)
{
if (key.BaseUri == null)
{
throw new NotSupportedException($"{nameof(key.BaseUri)} can't be null.");
}
source = new Uri(key.BaseUri, source);
}
if (source.Scheme == SystemFontScheme)
{
return SystemFonts.TryGetGlyphTypeface(familyName, typeface.Style, typeface.Weight, typeface.Stretch, out glyphTypeface);
}
if (TryGetFontCollection(source, out var fontCollection) && if (TryGetFontCollection(source, out var fontCollection) &&
fontCollection.TryGetGlyphTypeface(familyName, typeface.Style, typeface.Weight, typeface.Stretch, out glyphTypeface)) fontCollection.TryGetGlyphTypeface(familyName, typeface.Style, typeface.Weight, typeface.Stretch, out glyphTypeface))
@ -248,8 +234,9 @@ namespace Avalonia.Media
{ {
var key = compositeKey.Keys[i]; var key = compositeKey.Keys[i];
var familyName = fontFamily.FamilyNames[i]; var familyName = fontFamily.FamilyNames[i];
var source = key.Source.EnsureAbsolute(key.BaseUri);
if (TryGetFontCollection(key.Source, out var fontCollection) && if (TryGetFontCollection(source, out var fontCollection) &&
fontCollection.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, familyName, culture, out typeface)) fontCollection.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, familyName, culture, out typeface))
{ {
return true; return true;
@ -263,6 +250,8 @@ namespace Avalonia.Media
private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontCollection? fontCollection) private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontCollection? fontCollection)
{ {
Debug.Assert(source.IsAbsoluteUri);
if (source.Scheme == SystemFontScheme) if (source.Scheme == SystemFontScheme)
{ {
source = SystemFontsKey; source = SystemFontsKey;

8
tests/Avalonia.Skia.UnitTests/Media/FontManagerTests.cs

@ -200,14 +200,16 @@ namespace Avalonia.Skia.UnitTests.Media
} }
} }
[Fact] [Theory]
public void Should_Match_Chararcter_Width_Fallbacks() [InlineData("NotFound, Unknown", null)] // system fonts
[InlineData("/#NotFound, /#Unknown", "avares://some/path")] // embedded fonts
public void Should_Match_Character_With_Fallbacks(string familyName, string baseUri)
{ {
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl()))) using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
{ {
using (AvaloniaLocator.EnterScope()) using (AvaloniaLocator.EnterScope())
{ {
var fontFamily = FontFamily.Parse("NotFound, Unknown"); var fontFamily = FontFamily.Parse(familyName, baseUri is null ? null : new Uri(baseUri));
Assert.True(FontManager.Current.TryMatchCharacter('A', FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, fontFamily, null, out var typeface)); Assert.True(FontManager.Current.TryMatchCharacter('A', FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, fontFamily, null, out var typeface));

Loading…
Cancel
Save