Skip to content

Commit

Permalink
feat(skiawpf): Added support for FontWeight, FontStyle & FontStretch.…
Browse files Browse the repository at this point in the history
… Works only with Font Families, not when specifying font file directly.
  • Loading branch information
carldebilly committed Jul 30, 2020
1 parent 9801c51 commit 03698ab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextVisual.Skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
using Uno.UI.Xaml;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Windows.UI.Composition
{
internal class TextVisual : Visual
{
private SKPaint _paint;
private static Func<string, SKTypeface> _getTypeFace = Funcs.CreateMemoized<string, SKTypeface>(FromFamilyName);
private readonly SKPaint _paint;
private static readonly Func<string, SKFontStyleWeight, SKFontStyleWidth, SKFontStyleSlant, SKTypeface> _getTypeFace =
Funcs.CreateMemoized<string, SKFontStyleWeight, SKFontStyleWidth, SKFontStyleSlant, SKTypeface>(
(nm, wt, wh, sl) => FromFamilyName(nm, wt, wh, sl));

private readonly TextBlock _owner;

private static SKTypeface FromFamilyName(string name)
private static SKTypeface FromFamilyName(
string name,
SKFontStyleWeight weight,
SKFontStyleWidth width,
SKFontStyleSlant slant)
{
if (name.StartsWith(XamlFilePathHelper.AppXIdentifier))
{
Expand All @@ -32,27 +39,34 @@ private static SKTypeface FromFamilyName(string name)
}
else
{
return SKTypeface.FromFamilyName(name);
return SKTypeface.FromFamilyName(name, weight, width, slant);
}
}

public TextVisual(Compositor compositor, TextBlock owner) : base(compositor)
{
_owner = owner;

_paint = new SKPaint();
_paint.TextEncoding = SKTextEncoding.Utf16;
_paint.IsStroke = false;
_paint.IsAntialias = true;
_paint.LcdRenderText = true;
_paint.SubpixelText = true;
_paint = new SKPaint
{
TextEncoding = SKTextEncoding.Utf16,
IsStroke = false,
IsAntialias = true,
LcdRenderText = true,
SubpixelText = true
};
}

internal Size Measure(Size availableSize)
{
if (_owner.FontFamily?.Source != null)
{
_paint.Typeface = _getTypeFace(_owner.FontFamily.Source);
var weight = _owner.FontWeight.ToSkiaWeight();
//var width = _owner.FontStretch.ToSkiaWidth(); -- FontStretch not supported by Uno yet
var width = SKFontStyleWidth.Normal;
var slant = _owner.FontStyle.ToSkiaSlant();
var font = _getTypeFace(_owner.FontFamily.Source, weight, width, slant);
_paint.Typeface = font;
}

_paint.TextSize = (float)_owner.FontSize;
Expand Down
16 changes: 16 additions & 0 deletions src/Uno.UWP/UI/Text/FontStyle.skia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SkiaSharp;

namespace Windows.UI.Text
{
internal static class FontStyleExtensions
{
public static SKFontStyleSlant ToSkiaSlant(this FontStyle style) =>
style switch
{
FontStyle.Italic => SKFontStyleSlant.Italic,
FontStyle.Normal => SKFontStyleSlant.Upright,
FontStyle.Oblique => SKFontStyleSlant.Oblique,
_ => SKFontStyleSlant.Upright
};
}
}
15 changes: 15 additions & 0 deletions src/Uno.UWP/UI/Text/FontWeight.skia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SkiaSharp;

namespace Windows.UI.Text
{
partial struct FontWeight
{
public SKFontStyleWeight ToSkiaWeight()
{
// Uno weight values are using the same system,
// so we can convert directly to Skia system
// without need for a mapping.
return (SKFontStyleWeight)this.Weight;
}
}
}

0 comments on commit 03698ab

Please sign in to comment.