Browse Source

Skia support

pull/1535/head
Benedikt Schroeder 8 years ago
parent
commit
53db6d75e9
  1. 2
      samples/ControlCatalog.Desktop/Program.cs
  2. 4
      src/Skia/Avalonia.Skia/FormattedTextImpl.cs
  3. 43
      src/Skia/Avalonia.Skia/TypefaceCache.cs

2
samples/ControlCatalog.Desktop/Program.cs

@ -23,7 +23,7 @@ namespace ControlCatalog
/// This method is needed for IDE previewer infrastructure
/// </summary>
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>().LogToDebug().UsePlatformDetect();
=> AppBuilder.Configure<App>().LogToDebug().UsePlatformDetect().UseSkia();
private static void ConfigureAssetAssembly(AppBuilder builder)
{

4
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;

43
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<FontKey, SKTypeface> entry;
if (!Cache.TryGetValue(name, out entry))
if (!Cache.TryGetValue(familyKey, out var entry))
{
Cache[name] = entry = new Dictionary<FontKey, SKTypeface>();
Cache[familyKey] = entry = new Dictionary<FontKey, SKTypeface>();
}
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<IAssetLoader>().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));
}
}

Loading…
Cancel
Save