Skip to content

Commit 0785cf3

Browse files
GillibaldMrJul
andauthored
Make sure font simulations are applied for all collection types (#17399)
Co-authored-by: Julien Lebosquain <[email protected]>
1 parent 8e814f0 commit 0785cf3

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
@@ -287,6 +287,58 @@ public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fon
287287
return PlatformImpl.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, culture, out typeface);
288288
}
289289

290+
/// <summary>
291+
/// Tries to create a synthetic glyph typefacefor specified source glyph typeface and font properties.
292+
/// </summary>
293+
/// <param name="fontManager">The font manager implementation.</param>
294+
/// <param name="glyphTypeface">The source glyph typeface.</param>
295+
/// <param name="style">The requested font style.</param>
296+
/// <param name="weight">The requested font weight.</param>
297+
/// <param name="syntheticGlyphTypeface">The created synthetic glyph typeface.</param>
298+
/// <returns>
299+
/// <c>True</c>, if the <see cref="FontManager"/> could create a synthetic glyph typeface, <c>False</c> otherwise.
300+
/// </returns>
301+
internal static bool TryCreateSyntheticGlyphTypeface(IFontManagerImpl fontManager, IGlyphTypeface glyphTypeface, FontStyle style, FontWeight weight,
302+
[NotNullWhen(true)] out IGlyphTypeface? syntheticGlyphTypeface)
303+
{
304+
if (fontManager == null)
305+
{
306+
syntheticGlyphTypeface = null;
307+
308+
return false;
309+
}
310+
311+
if (glyphTypeface is IGlyphTypeface2 glyphTypeface2)
312+
{
313+
var fontSimulations = FontSimulations.None;
314+
315+
if (style != FontStyle.Normal && glyphTypeface2.Style != style)
316+
{
317+
fontSimulations |= FontSimulations.Oblique;
318+
}
319+
320+
if ((int)weight >= 600 && glyphTypeface2.Weight < weight)
321+
{
322+
fontSimulations |= FontSimulations.Bold;
323+
}
324+
325+
if (fontSimulations != FontSimulations.None && glyphTypeface2.TryGetStream(out var stream))
326+
{
327+
using (stream)
328+
{
329+
fontManager.TryCreateGlyphTypeface(stream, fontSimulations,
330+
out syntheticGlyphTypeface);
331+
332+
return syntheticGlyphTypeface != null;
333+
}
334+
}
335+
}
336+
337+
syntheticGlyphTypeface = null;
338+
339+
return false;
340+
}
341+
290342
private bool TryGetFontCollection(Uri source, [NotNullWhen(true)] out IFontCollection? fontCollection)
291343
{
292344
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)