Skip to content

Commit 612bafb

Browse files
GillibaldMrJul
authored andcommitted
Make sure font simulations are applied for all collection types (#17399)
Co-authored-by: Julien Lebosquain <[email protected]>
1 parent e5cdf3c commit 612bafb

File tree

3 files changed

+58
-67
lines changed

3 files changed

+58
-67
lines changed

src/Avalonia.Base/Media/FontManager.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,58 @@ public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fon
261261
return PlatformImpl.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, culture, out typeface);
262262
}
263263

264+
/// <summary>
265+
/// Tries to create a synthetic glyph typefacefor specified source glyph typeface and font properties.
266+
/// </summary>
267+
/// <param name="fontManager">The font manager implementation.</param>
268+
/// <param name="glyphTypeface">The source glyph typeface.</param>
269+
/// <param name="style">The requested font style.</param>
270+
/// <param name="weight">The requested font weight.</param>
271+
/// <param name="syntheticGlyphTypeface">The created synthetic glyph typeface.</param>
272+
/// <returns>
273+
/// <c>True</c>, if the <see cref="FontManager"/> could create a synthetic glyph typeface, <c>False</c> otherwise.
274+
/// </returns>
275+
internal static bool TryCreateSyntheticGlyphTypeface(IFontManagerImpl fontManager, IGlyphTypeface glyphTypeface, FontStyle style, FontWeight weight,
276+
[NotNullWhen(true)] out IGlyphTypeface? syntheticGlyphTypeface)
277+
{
278+
if (fontManager == null)
279+
{
280+
syntheticGlyphTypeface = null;
281+
282+
return false;
283+
}
284+
285+
if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
286+
{
287+
var fontSimulations = FontSimulations.None;
288+
289+
if (style != FontStyle.Normal && glyphTypeface2.Style != style)
290+
{
291+
fontSimulations |= FontSimulations.Oblique;
292+
}
293+
294+
if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
295+
{
296+
fontSimulations |= FontSimulations.Bold;
297+
}
298+
299+
if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
300+
{
301+
using (stream)
302+
{
303+
fontManager.TryCreateGlyphTypeface(stream, fontSimulations,
304+
out syntheticGlyphTypeface);
305+
306+
return syntheticGlyphTypeface != null;
307+
}
308+
}
309+
}
310+
311+
syntheticGlyphTypeface = null;
312+
313+
return false;
314+
}
315+
264316
private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontCollection? fontCollection)
265317
{
266318
Debug.Assert(source.IsAbsoluteUri);

src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Diagnostics.CodeAnalysis;
@@ -72,33 +71,9 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon
7271

7372
if (TryGetNearestMatch(glyphTypefaces, key, out glyphTypeface))
7473
{
75-
if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
74+
if(_fontManager != null && FontManager.TryCreateSyntheticGlyphTypeface(_fontManager, glyphTypeface, style, weight, out var syntheticGlyphTypeface))
7675
{
77-
var fontSimulations = FontSimulations.None;
78-
79-
if (style != FontStyle.Normal && glyphTypeface2.Style != style)
80-
{
81-
fontSimulations |= FontSimulations.Oblique;
82-
}
83-
84-
if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
85-
{
86-
fontSimulations |= FontSimulations.Bold;
87-
}
88-
89-
if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
90-
{
91-
using (stream)
92-
{
93-
if(_fontManager is not null && _fontManager.TryCreateGlyphTypeface(stream, fontSimulations, out glyphTypeface) &&
94-
glyphTypefaces.TryAdd(key, glyphTypeface))
95-
{
96-
return true;
97-
}
98-
99-
return false;
100-
}
101-
}
76+
glyphTypeface = syntheticGlyphTypeface;
10277
}
10378

10479
return true;

src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,11 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon
8686
{
8787
glyphTypeface = nearestMatch;
8888
}
89-
else
89+
90+
//Try to create a synthetic glyph typeface
91+
if (FontManager.TryCreateSyntheticGlyphTypeface(_fontManager.PlatformImpl, glyphTypeface, style, weight, out var syntheticGlyphTypeface))
9092
{
91-
//Try to create a synthetic glyph typeface
92-
if (TryCreateSyntheticGlyphTypeface(glyphTypeface, style, weight, out var syntheticGlyphTypeface))
93-
{
94-
glyphTypeface = syntheticGlyphTypeface;
95-
}
93+
glyphTypeface = syntheticGlyphTypeface;
9694
}
9795
}
9896

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

104-
private bool TryCreateSyntheticGlyphTypeface(IGlyphTypeface glyphTypeface, FontStyle style, FontWeight weight,
105-
[NotNullWhen(true)] out IGlyphTypeface? syntheticGlyphTypeface)
106-
{
107-
if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
108-
{
109-
var fontSimulations = FontSimulations.None;
110-
111-
if (style != FontStyle.Normal && glyphTypeface2.Style != style)
112-
{
113-
fontSimulations |= FontSimulations.Oblique;
114-
}
115-
116-
if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
117-
{
118-
fontSimulations |= FontSimulations.Bold;
119-
}
120-
121-
if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
122-
{
123-
using (stream)
124-
{
125-
_fontManager.PlatformImpl.TryCreateGlyphTypeface(stream, fontSimulations,
126-
out syntheticGlyphTypeface);
127-
128-
return syntheticGlyphTypeface != null;
129-
}
130-
}
131-
}
132-
133-
syntheticGlyphTypeface = null;
134-
135-
return false;
136-
}
137-
138102
public override void Initialize(IFontManagerImpl fontManager)
139103
{
140104
//We initialize the system font collection during construction.

0 commit comments

Comments
 (0)