using AngleSharp.Dom;
namespace EvilEngine.Html.Tests.Support;
internal sealed class FixedFont : ITextMeasurer
{
public FontMetrics Metrics(FontKey font)
{
return new FontMetrics(font.Size * 0.8, font.Size * 0.2, font.Size);
}
public double Width(string text, FontKey font)
{
return text.Length * font.Size * 0.5;
}
}
internal sealed class FixedWidgets(WidgetMetrics? metrics) : IWidgets
{
public List Created { get; } = [];
public WidgetMetrics? Create(IElement element, ComputedStyle style)
{
Created.Add(element);
return metrics;
}
}
internal static class Pages
{
public static PageLayout Lay(string html, double width = 800, IWidgets? widgets = null)
{
return PageLayout.Create(HtmlPage.Parse(html).Build(widgets ?? new FixedWidgets(null)), width, new FixedFont());
}
public static string[] Texts(this PageLayout layout)
{
return [.. layout.Runs.Select(run => run.Text)];
}
public static TextRun Run(this PageLayout layout, string text)
{
return layout.Runs.Single(run => run.Text == text);
}
public static double[] Rounded(this IEnumerable values)
{
return [.. values.Select(value => Math.Round(value, 3))];
}
public static double Rounded(this double value)
{
return Math.Round(value, 3);
}
public static (int R, int G, int B) Pixel(this IEnumerable rects, int x, int y)
{
var (r, g, b) = (255.0, 255.0, 255.0);
foreach (var rect in rects)
{
if (x < rect.X || x >= rect.X + rect.Width || y < rect.Y || y >= rect.Y + rect.Height)
continue;
var alpha = rect.Color.A / 255.0 * rect.Coverage;
(r, g, b) = (r + (rect.Color.R - r) * alpha, g + (rect.Color.G - g) * alpha,
b + (rect.Color.B - b) * alpha);
}
return ((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b));
}
}