using System.Text.RegularExpressions; using EvilEngine.Tailwind; namespace EvilEngine.Html; public sealed record ThemeFace(string Family, string Source, int Weight, bool Italic); public sealed partial class EngineTheme { private EngineTheme(string css) { Css = css; Faces = ReadFaces(css); System = new Lazy(() => css.Length == 0 ? DesignSystem.Default() : new DesignSystem(DesignSystem.LoadTheme(DesignSystem.ReadResource("theme.css") + "\n" + css))); } public static EngineTheme Stock { get; } = new(""); public static EngineTheme Current { get; private set; } = Stock; public string Css { get; } public IReadOnlyList Faces { get; } internal Lazy System { get; } internal Dictionary Signatures { get; } = []; public static EngineTheme Of(string css) { return css.Length == 0 ? Stock : new EngineTheme(css); } public static void Use(string css) { Current = Of(css); } private static List ReadFaces(string css) { var faces = new List(); if (css.Length == 0) return faces; foreach (var node in CssParser.Parse(css)) { if (node is not AtRule { Name: "@font-face" } rule) continue; var values = rule.Nodes.OfType() .ToDictionary(d => d.Property, d => d.Value ?? "", StringComparer.OrdinalIgnoreCase); if (!values.TryGetValue("font-family", out var family) || !values.TryGetValue("src", out var src) || Url().Match(src) is not { Success: true } url) continue; var weight = values.TryGetValue("font-weight", out var w) && int.TryParse(w.Trim(), out var parsed) ? parsed : w?.Trim() == "bold" ? 700 : 400; var italic = values.TryGetValue("font-style", out var s) && s.Trim() is "italic" or "oblique"; faces.Add(new ThemeFace(family.Trim().Trim('"', '\''), url.Groups[1].Value, weight, italic)); } return faces; } [GeneratedRegex("""url\(\s*["']?([^"')]+)["']?\s*\)""")] private static partial Regex Url(); }