From 7330250e951d016e4abedbd2854cbb8e02737f53 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 11 Aug 2016 23:06:02 +0100 Subject: [PATCH 1/2] skia font cache now correctly handles font weight and font style. --- src/Skia/Avalonia.Skia/TypefaceCache.cs | 57 +++++++++++++++++++------ 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/src/Skia/Avalonia.Skia/TypefaceCache.cs b/src/Skia/Avalonia.Skia/TypefaceCache.cs index dedd91ed76..3d1bf8add0 100644 --- a/src/Skia/Avalonia.Skia/TypefaceCache.cs +++ b/src/Skia/Avalonia.Skia/TypefaceCache.cs @@ -8,40 +8,69 @@ namespace Avalonia.Skia { static class TypefaceCache { - static readonly Dictionary> Cache = new Dictionary>(); + static readonly Dictionary> Cache = new Dictionary>(); - unsafe static SKTypeface GetTypeface(string name, SKTypefaceStyle style) + struct FontKey + { + public readonly SKFontStyleSlant Slant; + public readonly SKFontStyleWeight Weight; + + public FontKey(SKFontStyleWeight weight, SKFontStyleSlant slant) + { + Slant = slant; + Weight = weight; + } + + // Equals and GetHashCode ommitted + } + + unsafe static SKTypeface GetTypeface(string name, FontKey key) { if (name == null) + { name = "Arial"; + } + + Dictionary entry; - Dictionary entry; if (!Cache.TryGetValue(name, out entry)) - Cache[name] = entry = new Dictionary(); + { + Cache[name] = entry = new Dictionary(); + } SKTypeface typeface = null; - if (!entry.TryGetValue(style, out typeface)) + + if (!entry.TryGetValue(key, out typeface)) { - typeface = SKTypeface.FromFamilyName(name, style); + typeface = SKTypeface.FromFamilyName(name, key.Weight, SKFontStyleWidth.Normal, key.Slant); + if (typeface == null) { - typeface = SKTypeface.FromFamilyName(null, style); + typeface = SKTypeface.FromFamilyName(null, SKTypefaceStyle.Normal); } - entry[style] = typeface; + + entry[key] = typeface; } + return typeface; } public static SKTypeface GetTypeface(string name, FontStyle style, FontWeight weight) { - SKTypefaceStyle sstyle = SKTypefaceStyle.Normal; - if (style != FontStyle.Normal) - sstyle |= SKTypefaceStyle.Italic; + SKFontStyleSlant skStyle = SKFontStyleSlant.Upright; + + switch(style) + { + case FontStyle.Italic: + skStyle = SKFontStyleSlant.Italic; + break; - if (weight > FontWeight.Normal) - sstyle |= SKTypefaceStyle.Bold; + case FontStyle.Oblique: + skStyle = SKFontStyleSlant.Oblique; + break; + } - return GetTypeface(name, sstyle); + return GetTypeface(name, new FontKey((SKFontStyleWeight)weight, skStyle)); } } From fbfa7fe997cb31e13af1dde4518f571de9e9d8bc Mon Sep 17 00:00:00 2001 From: danwalmsley Date: Fri, 12 Aug 2016 09:50:24 +0100 Subject: [PATCH 2/2] implemented Equals and GetHashCode in FontKey for skia typeface cache. --- src/Skia/Avalonia.Skia/TypefaceCache.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Skia/Avalonia.Skia/TypefaceCache.cs b/src/Skia/Avalonia.Skia/TypefaceCache.cs index 3d1bf8add0..c5f3d371ad 100644 --- a/src/Skia/Avalonia.Skia/TypefaceCache.cs +++ b/src/Skia/Avalonia.Skia/TypefaceCache.cs @@ -20,7 +20,27 @@ namespace Avalonia.Skia Slant = slant; Weight = weight; } - + + public override int GetHashCode() + { + int hash = 17; + hash = hash * 31 + (int)Slant; + hash = hash * 31 + (int)Weight; + + return hash; + } + + public override bool Equals(object other) + { + return other is FontKey ? Equals((FontKey)other) : false; + } + + public bool Equals(FontKey other) + { + return Slant == other.Slant && + Weight == other.Weight; + } + // Equals and GetHashCode ommitted }