0.2.0-dev Download ZIP
70 lines ·2.0 KB Raw
| 1 | using AngleSharp.Dom; |
| 2 | |
| 3 | namespace EvilEngine.Html.Tests.Support; |
| 4 | |
| 5 | internal sealed class FixedFont : ITextMeasurer |
| 6 | { |
| 7 | public FontMetrics Metrics(FontKey font) |
| 8 | { |
| 9 | return new FontMetrics(font.Size * 0.8, font.Size * 0.2, font.Size); |
| 10 | } |
| 11 | |
| 12 | public double Width(string text, FontKey font) |
| 13 | { |
| 14 | return text.Length * font.Size * 0.5; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | internal sealed class FixedWidgets(WidgetMetrics? metrics) : IWidgets |
| 19 | { |
| 20 | public List<IElement> Created { get; } = []; |
| 21 | |
| 22 | public WidgetMetrics? Create(IElement element, ComputedStyle style) |
| 23 | { |
| 24 | Created.Add(element); |
| 25 | return metrics; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | internal static class Pages |
| 30 | { |
| 31 | public static PageLayout Lay(string html, double width = 800, IWidgets? widgets = null) |
| 32 | { |
| 33 | return PageLayout.Create(HtmlPage.Parse(html).Build(widgets ?? new FixedWidgets(null)), width, new FixedFont()); |
| 34 | } |
| 35 | |
| 36 | public static string[] Texts(this PageLayout layout) |
| 37 | { |
| 38 | return [.. layout.Runs.Select(run => run.Text)]; |
| 39 | } |
| 40 | |
| 41 | public static TextRun Run(this PageLayout layout, string text) |
| 42 | { |
| 43 | return layout.Runs.Single(run => run.Text == text); |
| 44 | } |
| 45 | |
| 46 | public static double[] Rounded(this IEnumerable<double> values) |
| 47 | { |
| 48 | return [.. values.Select(value => Math.Round(value, 3))]; |
| 49 | } |
| 50 | |
| 51 | public static double Rounded(this double value) |
| 52 | { |
| 53 | return Math.Round(value, 3); |
| 54 | } |
| 55 | |
| 56 | public static (int R, int G, int B) Pixel(this IEnumerable<FilledRect> rects, int x, int y) |
| 57 | { |
| 58 | var (r, g, b) = (255.0, 255.0, 255.0); |
| 59 | foreach (var rect in rects) |
| 60 | { |
| 61 | if (x < rect.X || x >= rect.X + rect.Width || y < rect.Y || y >= rect.Y + rect.Height) |
| 62 | continue; |
| 63 | var alpha = rect.Color.A / 255.0 * rect.Coverage; |
| 64 | (r, g, b) = (r + (rect.Color.R - r) * alpha, g + (rect.Color.G - g) * alpha, |
| 65 | b + (rect.Color.B - b) * alpha); |
| 66 | } |
| 67 | |
| 68 | return ((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b)); |
| 69 | } |
| 70 | } |