From 0d1c6915769bd63eea1b175e3eae8c1fc6030dc0 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 22 Jun 2020 16:40:19 -0300 Subject: [PATCH] add headless fontmanager. --- .../AvaloniaHeadlessPlatform.cs | 3 +- .../HeadlessPlatformRenderInterface.cs | 5 ++ .../HeadlessPlatformStubs.cs | 88 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs b/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs index 5857d94182..4f39471645 100644 --- a/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs +++ b/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs @@ -65,7 +65,8 @@ namespace Avalonia.Headless .Bind().ToConstant(new KeyboardDevice()) .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new RenderTimer(60)) - + .Bind().ToSingleton() + .Bind().ToSingleton() .Bind().ToConstant(new HeadlessWindowingPlatform()) .Bind().ToSingleton(); } diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index 5eb91e3c6b..f01d335fc7 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -104,6 +104,11 @@ namespace Avalonia.Headless public Rect GetRenderBounds(IPen pen) { + if(pen is null) + { + return Bounds; + } + return Bounds.Inflate(pen.Thickness / 2); } diff --git a/src/Avalonia.Headless/HeadlessPlatformStubs.cs b/src/Avalonia.Headless/HeadlessPlatformStubs.cs index f8f5f2a046..843865d6a9 100644 --- a/src/Avalonia.Headless/HeadlessPlatformStubs.cs +++ b/src/Avalonia.Headless/HeadlessPlatformStubs.cs @@ -1,12 +1,18 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; +using System.Linq; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Platform; using Avalonia.Input; using Avalonia.Input.Platform; +using Avalonia.Media; +using Avalonia.Media.Fonts; +using Avalonia.Media.TextFormatting; using Avalonia.Platform; +using Avalonia.Utility; namespace Avalonia.Headless { @@ -74,6 +80,88 @@ namespace Avalonia.Headless } } + class HeadlessGlyphTypefaceImpl : IGlyphTypefaceImpl + { + public short DesignEmHeight => 10; + + public int Ascent => 5; + + public int Descent => 5; + + public int LineGap => 2; + + public int UnderlinePosition => 5; + + public int UnderlineThickness => 5; + + public int StrikethroughPosition => 5; + + public int StrikethroughThickness => 2; + + public bool IsFixedPitch => true; + + public void Dispose() + { + } + + public ushort GetGlyph(uint codepoint) + { + return 1; + } + + public int GetGlyphAdvance(ushort glyph) + { + return 1; + } + + public int[] GetGlyphAdvances(ReadOnlySpan glyphs) + { + return glyphs.ToArray().Select(x => (int)x).ToArray(); + } + + public ushort[] GetGlyphs(ReadOnlySpan codepoints) + { + return codepoints.ToArray().Select(x => (ushort)x).ToArray(); + } + } + + class HeadlessTextShaperStub : ITextShaperImpl + { + public GlyphRun ShapeText(ReadOnlySlice text, TextFormat textFormat) + { + return new GlyphRun(new GlyphTypeface(new Typeface("Arial")), textFormat.FontRenderingEmSize, + new ReadOnlySlice(new ushort[] { 1, 2, 3 }), + new ReadOnlySlice(new double[] { 1, 2, 3 }), + new ReadOnlySlice(new Vector[] { new Vector(1, 1), new Vector(2, 2), new Vector(3, 3) }), + text, + new ReadOnlySlice(new ushort[] { 1, 2, 3 })); + } + } + + class HeadlessFontManagerStub : IFontManagerImpl + { + public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface) + { + return new HeadlessGlyphTypefaceImpl(); + } + + public string GetDefaultFontFamilyName() + { + return "Arial"; + } + + public IEnumerable GetInstalledFontFamilyNames(bool checkForUpdates = false) + { + return new List { "Arial" }; + } + + public bool TryMatchCharacter(int codepoint, FontWeight fontWeight, FontStyle fontStyle, FontFamily fontFamily, CultureInfo culture, out FontKey fontKey) + { + fontKey = new FontKey("Arial", fontWeight, fontStyle); + return true; + } + } + class HeadlessIconLoaderStub : IPlatformIconLoader {