Browse Source

Merge pull request #2668 from Gillibald/fixes/DefaultSystemFonLookup

Make sure System font can be used with all face variations
pull/2670/head
Jumar Macato 7 years ago
committed by GitHub
parent
commit
ba22da5a85
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/Skia/Avalonia.Skia/FormattedTextImpl.cs
  2. 2
      src/Skia/Avalonia.Skia/SKTypefaceCollection.cs
  3. 23
      src/Skia/Avalonia.Skia/TypefaceCache.cs

4
src/Skia/Avalonia.Skia/FormattedTextImpl.cs

@ -28,7 +28,7 @@ namespace Avalonia.Skia
// Replace 0 characters with zero-width spaces (200B) // Replace 0 characters with zero-width spaces (200B)
Text = Text.Replace((char)0, (char)0x200B); Text = Text.Replace((char)0, (char)0x200B);
SKTypeface skiaTypeface = TypefaceCache.Default; SKTypeface skiaTypeface = null;
if (typeface.FontFamily.Key != null) if (typeface.FontFamily.Key != null)
{ {
@ -45,7 +45,7 @@ namespace Avalonia.Skia
familyName, familyName,
typeface.Style, typeface.Style,
typeface.Weight); typeface.Weight);
if (skiaTypeface != TypefaceCache.Default) break; if (skiaTypeface.FamilyName != TypefaceCache.DefaultFamilyName) break;
} }
} }
else else

2
src/Skia/Avalonia.Skia/SKTypefaceCollection.cs

@ -47,7 +47,7 @@ namespace Avalonia.Skia
if (!_fontFamilies.TryGetValue(typeface.FontFamily.Name, out var fontFamily)) if (!_fontFamilies.TryGetValue(typeface.FontFamily.Name, out var fontFamily))
{ {
return TypefaceCache.Default; return TypefaceCache.GetTypeface(TypefaceCache.DefaultFamilyName, typeface.Style, typeface.Weight);
} }
var weight = (SKFontStyleWeight)typeface.Weight; var weight = (SKFontStyleWeight)typeface.Weight;

23
src/Skia/Avalonia.Skia/TypefaceCache.cs

@ -12,8 +12,10 @@ namespace Avalonia.Skia
/// </summary> /// </summary>
internal static class TypefaceCache internal static class TypefaceCache
{ {
public static SKTypeface Default = CreateDefaultTypeface(); public static readonly string DefaultFamilyName = CreateDefaultFamilyName();
static readonly Dictionary<string, Dictionary<FontKey, SKTypeface>> Cache = new Dictionary<string, Dictionary<FontKey, SKTypeface>>();
private static readonly Dictionary<string, Dictionary<FontKey, SKTypeface>> s_cache =
new Dictionary<string, Dictionary<FontKey, SKTypeface>>();
struct FontKey struct FontKey
{ {
@ -49,26 +51,26 @@ namespace Avalonia.Skia
// Equals and GetHashCode ommitted // Equals and GetHashCode ommitted
} }
private static SKTypeface CreateDefaultTypeface() private static string CreateDefaultFamilyName()
{ {
var defaultTypeface = SKTypeface.FromFamilyName(FontFamily.Default.Name) ?? SKTypeface.FromFamilyName(null); var defaultTypeface = SKTypeface.CreateDefault();
return defaultTypeface; return defaultTypeface.FamilyName;
} }
private static SKTypeface GetTypeface(string name, FontKey key) private static SKTypeface GetTypeface(string name, FontKey key)
{ {
var familyKey = name; var familyKey = name;
if (!Cache.TryGetValue(familyKey, out var entry)) if (!s_cache.TryGetValue(familyKey, out var entry))
{ {
Cache[familyKey] = entry = new Dictionary<FontKey, SKTypeface>(); s_cache[familyKey] = entry = new Dictionary<FontKey, SKTypeface>();
} }
if (!entry.TryGetValue(key, out var typeface)) if (!entry.TryGetValue(key, out var typeface))
{ {
typeface = SKTypeface.FromFamilyName(familyKey, key.Weight, SKFontStyleWidth.Normal, key.Slant) typeface = SKTypeface.FromFamilyName(familyKey, key.Weight, SKFontStyleWidth.Normal, key.Slant) ??
?? Default; GetTypeface(DefaultFamilyName, key);
entry[key] = typeface; entry[key] = typeface;
} }
@ -78,7 +80,7 @@ namespace Avalonia.Skia
public static SKTypeface GetTypeface(string name, FontStyle style, FontWeight weight) public static SKTypeface GetTypeface(string name, FontStyle style, FontWeight weight)
{ {
SKFontStyleSlant skStyle = SKFontStyleSlant.Upright; var skStyle = SKFontStyleSlant.Upright;
switch (style) switch (style)
{ {
@ -93,6 +95,5 @@ namespace Avalonia.Skia
return GetTypeface(name, new FontKey((SKFontStyleWeight)weight, skStyle)); return GetTypeface(name, new FontKey((SKFontStyleWeight)weight, skStyle));
} }
} }
} }

Loading…
Cancel
Save