Skip to content

Typeface simulation fixes #17399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Avalonia.Base/Media/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,58 @@ public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fon
return PlatformImpl.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, culture, out typeface);
}

/// <summary>
/// Tries to create a synthetic glyph typefacefor specified source glyph typeface and font properties.
/// </summary>
/// <param name="fontManager">The font manager implementation.</param>
/// <param name="glyphTypeface">The source glyph typeface.</param>
/// <param name="style">The requested font style.</param>
/// <param name="weight">The requested font weight.</param>
/// <param name="syntheticGlyphTypeface">The created synthetic glyph typeface.</param>
/// <returns>
/// <c>True</c>, if the <see cref="FontManager"/> could create a synthetic glyph typeface, <c>False</c> otherwise.
/// </returns>
internal static bool TryCreateSyntheticGlyphTypeface(IFontManagerImpl fontManager, IGlyphTypeface glyphTypeface, FontStyle style, FontWeight weight,
[NotNullWhen(true)] out IGlyphTypeface? syntheticGlyphTypeface)
{
if (fontManager == null)
{
syntheticGlyphTypeface = null;

return false;
}

if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
{
var fontSimulations = FontSimulations.None;

if (style != FontStyle.Normal && glyphTypeface2.Style != style)
{
fontSimulations |= FontSimulations.Oblique;
}

if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
{
fontSimulations |= FontSimulations.Bold;
}

if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
{
using (stream)
{
fontManager.TryCreateGlyphTypeface(stream, fontSimulations,
out syntheticGlyphTypeface);

return syntheticGlyphTypeface != null;
}
}
}

syntheticGlyphTypeface = null;

return false;
}

private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontCollection? fontCollection)
{
Debug.Assert(source.IsAbsoluteUri);
Expand Down
29 changes: 2 additions & 27 deletions src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -72,33 +71,9 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon

if (TryGetNearestMatch(glyphTypefaces, key, out glyphTypeface))
{
if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
if(_fontManager != null && FontManager.TryCreateSyntheticGlyphTypeface(_fontManager, glyphTypeface, style, weight, out var syntheticGlyphTypeface))
{
var fontSimulations = FontSimulations.None;

if (style != FontStyle.Normal && glyphTypeface2.Style != style)
{
fontSimulations |= FontSimulations.Oblique;
}

if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
{
fontSimulations |= FontSimulations.Bold;
}

if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
{
using (stream)
{
if(_fontManager is not null && _fontManager.TryCreateGlyphTypeface(stream, fontSimulations, out glyphTypeface) &&
glyphTypefaces.TryAdd(key, glyphTypeface))
{
return true;
}

return false;
}
}
glyphTypeface = syntheticGlyphTypeface;
}

return true;
Expand Down
44 changes: 4 additions & 40 deletions src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon
{
glyphTypeface = nearestMatch;
}
else

//Try to create a synthetic glyph typeface
if (FontManager.TryCreateSyntheticGlyphTypeface(_fontManager.PlatformImpl, glyphTypeface, style, weight, out var syntheticGlyphTypeface))
{
//Try to create a synthetic glyph typeface
if (TryCreateSyntheticGlyphTypeface(glyphTypeface, style, weight, out var syntheticGlyphTypeface))
{
glyphTypeface = syntheticGlyphTypeface;
}
glyphTypeface = syntheticGlyphTypeface;
}
}

Expand All @@ -101,40 +99,6 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon
return glyphTypeface != null;
}

private bool TryCreateSyntheticGlyphTypeface(IGlyphTypeface glyphTypeface, FontStyle style, FontWeight weight,
[NotNullWhen(true)] out IGlyphTypeface? syntheticGlyphTypeface)
{
if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
{
var fontSimulations = FontSimulations.None;

if (style != FontStyle.Normal && glyphTypeface2.Style != style)
{
fontSimulations |= FontSimulations.Oblique;
}

if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
{
fontSimulations |= FontSimulations.Bold;
}

if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
{
using (stream)
{
_fontManager.PlatformImpl.TryCreateGlyphTypeface(stream, fontSimulations,
out syntheticGlyphTypeface);

return syntheticGlyphTypeface != null;
}
}
}

syntheticGlyphTypeface = null;

return false;
}

public override void Initialize(IFontManagerImpl fontManager)
{
//We initialize the system font collection during construction.
Expand Down