Improve FontCollection customization#19756
Conversation
|
You can test this PR using the following package version. |
85b525f to
c06c943
Compare
|
You can test this PR using the following package version. |
|
It might also be useful to add whole font collection into another |
|
A copy can be done via: But we can also introduce some API to make this easier |
|
I was thinking of |
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
API diff between 12.0.999-cibuild0060140-alpha and 12.0.999Avalonia.Base (net6.0, net8.0, netstandard2.0) namespace Avalonia.Media
{
public sealed class Typeface
{
+ public Avalonia.Media.Typeface Normalize(out string normalizedFamilyName);
}
}
namespace Avalonia.Media.Fonts
{
public class EmbeddedFontCollection : Avalonia.Media.Fonts.FontCollectionBase
{
- public override System.Collections.Generic.IEnumerator<Avalonia.Media.FontFamily> GetEnumerator();
- public override void Initialize(Avalonia.Platform.IFontManagerImpl fontManager);
- public bool TryGetFamilyTypefaces(string familyName, out System.Collections.Generic.IReadOnlyList<Avalonia.Media.Typeface?>? familyTypefaces);
- public override bool TryGetGlyphTypeface(string familyName, Avalonia.Media.FontStyle style, Avalonia.Media.FontWeight weight, Avalonia.Media.FontStretch stretch, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
- public override int Count { get; }
- public override Avalonia.Media.FontFamily this[int index] {
- get { }
- }
}
public abstract class FontCollectionBase : Avalonia.Media.Fonts.IFontCollection
{
- protected readonly System.Collections.Concurrent.ConcurrentDictionary<string, System.Collections.Concurrent.ConcurrentDictionary<Avalonia.Media.Fonts.FontCollectionKey, Avalonia.Media.IGlyphTypeface>> _glyphTypefaceCache;
+ public System.Collections.Generic.IEnumerator<Avalonia.Media.FontFamily> GetEnumerator();
- public abstract System.Collections.Generic.IEnumerator<Avalonia.Media.FontFamily> GetEnumerator();
- public abstract void Initialize(Avalonia.Platform.IFontManagerImpl fontManager);
- public abstract bool TryGetGlyphTypeface(string familyName, Avalonia.Media.FontStyle style, Avalonia.Media.FontWeight weight, Avalonia.Media.FontStretch stretch, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
+ public virtual bool TryGetGlyphTypeface(string familyName, Avalonia.Media.FontStyle style, Avalonia.Media.FontWeight weight, Avalonia.Media.FontStretch stretch, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
- public abstract int Count { get; }
+ public int Count { get; }
- public abstract Avalonia.Media.FontFamily this[int index] { get; }
+ public Avalonia.Media.FontFamily this[int index] {
+ get { }
+ }
+ protected void AddFontFamily(Avalonia.Media.FontFamily fontFamily);
+ public bool TryAddFontSource(System.Uri source);
+ public bool TryAddGlyphTypeface(Avalonia.Media.IGlyphTypeface glyphTypeface);
+ public bool TryAddGlyphTypeface(System.IO.Stream stream, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
+ protected bool TryAddGlyphTypeface(string familyName, Avalonia.Media.Fonts.FontCollectionKey key, Avalonia.Media.IGlyphTypeface? glyphTypeface);
+ public virtual bool TryGetFamilyTypefaces(string familyName, out System.Collections.Generic.IReadOnlyList<Avalonia.Media.Typeface?>? familyTypefaces);
+ protected bool TryGetGlyphTypeface(string familyName, Avalonia.Media.Fonts.FontCollectionKey key, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
+ protected bool? TryGetNearestMatch(System.Collections.Generic.IDictionary<Avalonia.Media.Fonts.FontCollectionKey, Avalonia.Media.IGlyphTypeface> glyphTypefaces, Avalonia.Media.Fonts.FontCollectionKey? key, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
+ public bool TryGetNearestMatch(string familyName, Avalonia.Media.FontStyle style, Avalonia.Media.FontWeight weight, Avalonia.Media.FontStretch stretch, out Avalonia.Media.IGlyphTypeface? glyphTypeface);
}
- public static class FontFamilyLoader
- {
- public static System.Collections.Generic.IEnumerable<System.Uri> LoadFontAssets(System.Uri source);
- }
public interface IFontCollection
{
- void Initialize(Avalonia.Platform.IFontManagerImpl fontManager);
}
}
namespace Avalonia.Platform
{
public interface IFontManagerImpl
{
- bool? TryMatchCharacter(int? codepoint, Avalonia.Media.FontStyle? fontStyle, Avalonia.Media.FontWeight? fontWeight, Avalonia.Media.FontStretch? fontStretch, System.Globalization.CultureInfo? culture, out Avalonia.Media.Typeface? typeface);
+ bool? TryMatchCharacter(int? codepoint, Avalonia.Media.FontStyle? fontStyle, Avalonia.Media.FontWeight? fontWeight, Avalonia.Media.FontStretch? fontStretch, string? familyName, System.Globalization.CultureInfo? culture, out Avalonia.Media.Typeface? typeface);
}
} |
5af34bd to
7c6637e
Compare
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
…bald/Avalonia into feature/publicFontCollectionHelper
Make tests happy again
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
…nHelper # Conflicts: # api/Avalonia.nupkg.xml # src/Windows/Avalonia.Direct2D1/Media/FontManagerImpl.cs # tests/Avalonia.Direct2D1.UnitTests/Media/FontManagerImplTests.cs
|
You can test this PR using the following package version. |
What does the pull request do?
This change enhances font collection matching, loading and synthetic glyph typeface creation. It improves nearest-match logic for style/weight/stretch, adds prefix-family lookup when an exact family is missing, better caches fallback matches and supports loading fonts from resource and file sources (including directories). Several platform font manager and glyph typeface implementations and unit tests were updated to align with the new behavior.
Key highlights
style,weightandstretch.TryCreateSyntheticGlyphTypefacewhen exact-match missing.TryAddFontSourcesupportsavares/resmresource schemes andfilescheme for files and directories.TryAddGlyphTypefacehelper added.AddFontFamilymaintains a sorted snapshot array published atomically for readers.Tests
Notes & migration
IFontManagerImplimplementations are compatible with the updatedTryCreateGlyphTypefaceandIGlyphTypeface2usage.What is the current behavior?
Example for TryAddFontSource
How was the solution implemented (if it's not obvious)?
Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues