Skip to content

Commit

Permalink
Do not change public API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gillibald committed Nov 1, 2024
1 parent 6a0e432 commit 43faf03
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
53 changes: 52 additions & 1 deletion src/Avalonia.Base/Media/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Avalonia.Logging;
using Avalonia.Media.Fonts;
using Avalonia.Platform;
Expand Down Expand Up @@ -287,6 +286,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
4 changes: 2 additions & 2 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 All @@ -10,6 +9,7 @@ namespace Avalonia.Media.Fonts
{
public class EmbeddedFontCollection : FontCollectionBase
{
private IFontManagerImpl? _fontManager;
private readonly List<FontFamily> _fontFamilies = new List<FontFamily>(1);

private readonly Uri _key;
Expand Down Expand Up @@ -70,7 +70,7 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon

if (TryGetNearestMatch(glyphTypefaces, key, out glyphTypeface))
{
if(TryCreateSyntheticGlyphTypeface(glyphTypeface, style, weight, out var syntheticGlyphTypeface))
if(_fontManager != null && FontManager.TryCreateSyntheticGlyphTypeface(_fontManager, glyphTypeface, style, weight, out var syntheticGlyphTypeface))
{
glyphTypeface = syntheticGlyphTypeface;
}
Expand Down
42 changes: 0 additions & 42 deletions src/Avalonia.Base/Media/Fonts/FontCollectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Avalonia.Media.Fonts
public abstract class FontCollectionBase : IFontCollection
{
protected readonly ConcurrentDictionary<string, ConcurrentDictionary<FontCollectionKey, IGlyphTypeface?>> _glyphTypefaceCache = new();
protected IFontManagerImpl? _fontManager;

public abstract Uri Key { get; }

Expand Down Expand Up @@ -81,47 +80,6 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}

protected bool TryCreateSyntheticGlyphTypeface(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;
}

internal static bool TryGetNearestMatch(
ConcurrentDictionary<FontCollectionKey,
IGlyphTypeface?> glyphTypefaces,
Expand Down
3 changes: 2 additions & 1 deletion src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Avalonia.Media.Fonts
{
internal class SystemFontCollection : FontCollectionBase
{
private readonly IFontManagerImpl _fontManager;
private readonly List<string> _familyNames;

public SystemFontCollection(IFontManagerImpl fontManager)
Expand Down Expand Up @@ -87,7 +88,7 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon
}

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

0 comments on commit 43faf03

Please sign in to comment.