Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce font related platform implementations for unit tests #7344

Merged
merged 3 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/Avalonia.UnitTests/Avalonia.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<Import Project="..\..\build\Moq.props" />
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\SharedVersion.props" />
<Import Project="..\..\build\HarfBuzzSharp.props" />
</Project>
96 changes: 96 additions & 0 deletions tests/Avalonia.UnitTests/HarfBuzzFontManagerImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Avalonia.Media;
using Avalonia.Media.Fonts;
using Avalonia.Platform;

namespace Avalonia.UnitTests
{
public class HarfBuzzFontManagerImpl : IFontManagerImpl
{
private readonly Typeface[] _customTypefaces;
private readonly string _defaultFamilyName;

private static readonly Typeface _defaultTypeface =
new Typeface("resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Noto Mono");
private static readonly Typeface _italicTypeface =
new Typeface("resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Noto Sans");
private static readonly Typeface _emojiTypeface =
new Typeface("resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Twitter Color Emoji");

public HarfBuzzFontManagerImpl(string defaultFamilyName = "resm:Avalonia.UnitTests.Assets?assembly=Avalonia.UnitTests#Noto Mono")
{
_customTypefaces = new[] { _emojiTypeface, _italicTypeface, _defaultTypeface };
_defaultFamilyName = defaultFamilyName;
}

public string GetDefaultFontFamilyName()
{
return _defaultFamilyName;
}

public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false)
{
return _customTypefaces.Select(x => x.FontFamily!.Name);
}

public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontFamily fontFamily,
CultureInfo culture, out Typeface fontKey)
{
foreach (var customTypeface in _customTypefaces)
{
var glyphTypeface = customTypeface.GlyphTypeface;

if (!glyphTypeface.TryGetGlyph((uint)codepoint, out _))
{
continue;
}

fontKey = customTypeface;

return true;
}

fontKey = default;

return false;
}

public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
{
var fontFamily = typeface.FontFamily;

if (fontFamily == null)
{
return null;
}

if (fontFamily.IsDefault)
{
fontFamily = _defaultTypeface.FontFamily;
}

if (fontFamily!.Key == null)
{
return null;
}

var fontAssets = FontFamilyLoader.LoadFontAssets(fontFamily.Key);

var asset = fontAssets.First();

var assetLoader = AvaloniaLocator.Current.GetService<IAssetLoader>();

if (assetLoader == null)
{
throw new NotSupportedException("IAssetLoader is not registered.");
}

var stream = assetLoader.Open(asset);

return new HarfBuzzGlyphTypefaceImpl(stream);
}
}
}
158 changes: 158 additions & 0 deletions tests/Avalonia.UnitTests/HarfBuzzGlyphTypefaceImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.IO;
using Avalonia.Platform;
using HarfBuzzSharp;

namespace Avalonia.UnitTests
{
public class HarfBuzzGlyphTypefaceImpl : IGlyphTypefaceImpl
{
private bool _isDisposed;
private Blob _blob;

public HarfBuzzGlyphTypefaceImpl(Stream data, bool isFakeBold = false, bool isFakeItalic = false)
{
_blob = Blob.FromStream(data);

Face = new Face(_blob, 0);

Font = new Font(Face);

Font.SetFunctionsOpenType();

Font.GetScale(out var scale, out _);

DesignEmHeight = (short)scale;

var metrics = Font.OpenTypeMetrics;

const double defaultFontRenderingEmSize = 12.0;

Ascent = (int)(metrics.GetXVariation(OpenTypeMetricsTag.HorizontalAscender) / defaultFontRenderingEmSize * DesignEmHeight);

Descent = (int)(metrics.GetXVariation(OpenTypeMetricsTag.HorizontalDescender) / defaultFontRenderingEmSize * DesignEmHeight);

LineGap = (int)(metrics.GetXVariation(OpenTypeMetricsTag.HorizontalLineGap) / defaultFontRenderingEmSize * DesignEmHeight);

UnderlinePosition = (int)(metrics.GetXVariation(OpenTypeMetricsTag.UnderlineOffset) / defaultFontRenderingEmSize * DesignEmHeight);

UnderlineThickness = (int)(metrics.GetXVariation(OpenTypeMetricsTag.UnderlineSize) / defaultFontRenderingEmSize * DesignEmHeight);

StrikethroughPosition = (int)(metrics.GetXVariation(OpenTypeMetricsTag.StrikeoutOffset) / defaultFontRenderingEmSize * DesignEmHeight);

StrikethroughThickness = (int)(metrics.GetXVariation(OpenTypeMetricsTag.StrikeoutSize) / defaultFontRenderingEmSize * DesignEmHeight);

IsFixedPitch = GetGlyphAdvance(GetGlyph('a')) == GetGlyphAdvance(GetGlyph('b'));

IsFakeBold = isFakeBold;

IsFakeItalic = isFakeItalic;
}

public Face Face { get; }

public Font Font { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public short DesignEmHeight { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int Ascent { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int Descent { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int LineGap { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int UnderlinePosition { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int UnderlineThickness { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int StrikethroughPosition { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int StrikethroughThickness { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public bool IsFixedPitch { get; }

public bool IsFakeBold { get; }

public bool IsFakeItalic { get; }

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public ushort GetGlyph(uint codepoint)
{
if (Font.TryGetGlyph(codepoint, out var glyph))
{
return (ushort)glyph;
}

return 0;
}

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public ushort[] GetGlyphs(ReadOnlySpan<uint> codepoints)
{
var glyphs = new ushort[codepoints.Length];

for (var i = 0; i < codepoints.Length; i++)
{
if (Font.TryGetGlyph(codepoints[i], out var glyph))
{
glyphs[i] = (ushort)glyph;
}
}

return glyphs;
}

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int GetGlyphAdvance(ushort glyph)
{
return Font.GetHorizontalGlyphAdvance(glyph);
}

/// <inheritdoc cref="IGlyphTypefaceImpl"/>
public int[] GetGlyphAdvances(ReadOnlySpan<ushort> glyphs)
{
var glyphIndices = new uint[glyphs.Length];

for (var i = 0; i < glyphs.Length; i++)
{
glyphIndices[i] = glyphs[i];
}

return Font.GetHorizontalGlyphAdvances(glyphIndices);
}

private void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}

_isDisposed = true;

if (!disposing)
{
return;
}

Font?.Dispose();
Face?.Dispose();
_blob?.Dispose();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
Loading