diff --git a/samples/ControlCatalog.Desktop/Program.cs b/samples/ControlCatalog.Desktop/Program.cs index daa6ba69db..ae458f52c6 100644 --- a/samples/ControlCatalog.Desktop/Program.cs +++ b/samples/ControlCatalog.Desktop/Program.cs @@ -23,7 +23,7 @@ namespace ControlCatalog /// This method is needed for IDE previewer infrastructure /// public static AppBuilder BuildAvaloniaApp() - => AppBuilder.Configure().LogToDebug().UsePlatformDetect(); + => AppBuilder.Configure().LogToDebug().UsePlatformDetect().UseSkia(); private static void ConfigureAssetAssembly(AppBuilder builder) { diff --git a/src/Skia/Avalonia.Skia/FormattedTextImpl.cs b/src/Skia/Avalonia.Skia/FormattedTextImpl.cs index baf3a210d5..23a9ec615e 100644 --- a/src/Skia/Avalonia.Skia/FormattedTextImpl.cs +++ b/src/Skia/Avalonia.Skia/FormattedTextImpl.cs @@ -26,7 +26,7 @@ namespace Avalonia.Skia Text = Text.Replace((char)0, (char)0x200B); var skiaTypeface = TypefaceCache.GetTypeface( - typeface.FontFamily.Name ?? "monospace", + typeface.FontFamily, typeface.Style, typeface.Weight); @@ -37,7 +37,7 @@ namespace Avalonia.Skia _paint.TextEncoding = SKTextEncoding.Utf16; _paint.IsStroke = false; _paint.IsAntialias = true; - _paint.LcdRenderText = true; + _paint.LcdRenderText = true; _paint.SubpixelText = true; _paint.Typeface = skiaTypeface; _paint.TextSize = (float)typeface.FontSize; diff --git a/src/Skia/Avalonia.Skia/TypefaceCache.cs b/src/Skia/Avalonia.Skia/TypefaceCache.cs index c5f3d371ad..ed84d4818e 100644 --- a/src/Skia/Avalonia.Skia/TypefaceCache.cs +++ b/src/Skia/Avalonia.Skia/TypefaceCache.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text; using Avalonia.Media; +using Avalonia.Platform; using SkiaSharp; namespace Avalonia.Skia @@ -15,9 +16,9 @@ namespace Avalonia.Skia public readonly SKFontStyleSlant Slant; public readonly SKFontStyleWeight Weight; - public FontKey(SKFontStyleWeight weight, SKFontStyleSlant slant) + public FontKey(SKFontStyleWeight weight, SKFontStyleSlant slant) { - Slant = slant; + Slant = slant; Weight = weight; } @@ -44,29 +45,31 @@ namespace Avalonia.Skia // Equals and GetHashCode ommitted } - unsafe static SKTypeface GetTypeface(string name, FontKey key) + unsafe static SKTypeface GetTypeface(FontFamily fontFamily, FontKey key) { - if (name == null) - { - name = "Arial"; - } + var familyKey = fontFamily.Key.ToString(); - Dictionary entry; - - if (!Cache.TryGetValue(name, out entry)) + if (!Cache.TryGetValue(familyKey, out var entry)) { - Cache[name] = entry = new Dictionary(); + Cache[familyKey] = entry = new Dictionary(); } - SKTypeface typeface = null; - - if (!entry.TryGetValue(key, out typeface)) + if (!entry.TryGetValue(key, out var typeface)) { - typeface = SKTypeface.FromFamilyName(name, key.Weight, SKFontStyleWidth.Normal, key.Slant); + if (fontFamily.BaseUri != null) + { + var stream = AvaloniaLocator.Current.GetService().Open(fontFamily.BaseUri); - if (typeface == null) + typeface = SKTypeface.FromStream(stream); + } + else { - typeface = SKTypeface.FromFamilyName(null, SKTypefaceStyle.Normal); + typeface = SKTypeface.FromFamilyName(familyKey, key.Weight, SKFontStyleWidth.Normal, key.Slant); + + if (typeface == null) + { + typeface = SKTypeface.FromFamilyName(null, SKTypefaceStyle.Normal); + } } entry[key] = typeface; @@ -75,11 +78,11 @@ namespace Avalonia.Skia return typeface; } - public static SKTypeface GetTypeface(string name, FontStyle style, FontWeight weight) + public static SKTypeface GetTypeface(FontFamily fontFamily, FontStyle style, FontWeight weight) { SKFontStyleSlant skStyle = SKFontStyleSlant.Upright; - switch(style) + switch (style) { case FontStyle.Italic: skStyle = SKFontStyleSlant.Italic; @@ -90,7 +93,7 @@ namespace Avalonia.Skia break; } - return GetTypeface(name, new FontKey((SKFontStyleWeight)weight, skStyle)); + return GetTypeface(fontFamily, new FontKey((SKFontStyleWeight)weight, skStyle)); } }