|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace TheArtOfDev.HtmlRenderer.Core.CssEngine |
| 4 | +{ |
| 5 | + /// <summary>One operand of a <c>palette-mix()</c>: a palette reference and an optional mixing percentage.</summary> |
| 6 | + internal sealed class PaletteMixOperand |
| 7 | + { |
| 8 | + public PaletteMixOperand(string palette, double? percentage) |
| 9 | + { |
| 10 | + Palette = palette; |
| 11 | + Percentage = percentage; |
| 12 | + } |
| 13 | + |
| 14 | + public string Palette { get; } |
| 15 | + public double? Percentage { get; } |
| 16 | + } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// A parsed <c>palette-mix()</c> value (CSS Fonts Module Level 4). The two operands are blended per CPAL |
| 20 | + /// entry in <see cref="ColorSpace"/> (with an optional polar <see cref="HueMethod"/>), weighted by the |
| 21 | + /// operand percentages. |
| 22 | + /// </summary> |
| 23 | + internal sealed class ParsedPaletteMix |
| 24 | + { |
| 25 | + public ParsedPaletteMix(string colorSpace, string hueMethod, PaletteMixOperand first, PaletteMixOperand second) |
| 26 | + { |
| 27 | + ColorSpace = colorSpace; |
| 28 | + HueMethod = hueMethod; |
| 29 | + First = first; |
| 30 | + Second = second; |
| 31 | + } |
| 32 | + |
| 33 | + public string ColorSpace { get; } |
| 34 | + public string HueMethod { get; } |
| 35 | + public PaletteMixOperand First { get; } |
| 36 | + public PaletteMixOperand Second { get; } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The single, shared grammar for the CSS <c>palette-mix()</c> function |
| 41 | + /// (<a href="https://www.w3.org/TR/css-fonts-4/#font-palette-values">CSS Fonts 4</a>): |
| 42 | + /// <code>palette-mix( <color-interpolation-method> , [ [ normal | light | dark | <palette-identifier> ] <percentage>? ]#{2} )</code>. |
| 43 | + /// Used by both Layer A (the <c>font-palette</c> value converter, which drops a malformed value at parse |
| 44 | + /// time) and Layer B (<c>ActualFontPalette</c> resolution), so the two layers cannot disagree. Color-space |
| 45 | + /// membership is owned by <see cref="ColorInterpolationMethodGrammar"/> (its predicates classify the space |
| 46 | + /// and hue direction here), keeping a single source of truth for which spaces are valid. |
| 47 | + /// </summary> |
| 48 | + internal static class PaletteMixGrammar |
| 49 | + { |
| 50 | + private const string FunctionName = "palette-mix"; |
| 51 | + private const string In = "in"; |
| 52 | + private const string Hue = "hue"; |
| 53 | + |
| 54 | + public static ParsedPaletteMix TryParse(IReadOnlyList<Token> value) |
| 55 | + { |
| 56 | + var fn = SinglePaletteMixFunction(value); |
| 57 | + if (fn == null) |
| 58 | + return null; |
| 59 | + |
| 60 | + var groups = SplitTopLevelComma(new List<Token>(fn.ArgumentTokens)); |
| 61 | + if (groups.Count != 3) |
| 62 | + return null; |
| 63 | + |
| 64 | + if (!TryParseMethod(groups[0], out var space, out var hue) || |
| 65 | + !TryParseOperand(groups[1], out var first) || |
| 66 | + !TryParseOperand(groups[2], out var second)) |
| 67 | + return null; |
| 68 | + |
| 69 | + // Both percentages explicitly zero has no valid interpretation (mirrors color-mix()). |
| 70 | + if (first.Percentage == 0 && second.Percentage == 0) |
| 71 | + return null; |
| 72 | + |
| 73 | + return new ParsedPaletteMix(space, hue, first, second); |
| 74 | + } |
| 75 | + |
| 76 | + private static FunctionToken SinglePaletteMixFunction(IReadOnlyList<Token> value) |
| 77 | + { |
| 78 | + FunctionToken found = null; |
| 79 | + foreach (var token in value) |
| 80 | + { |
| 81 | + if (token.Type == TokenType.Whitespace) |
| 82 | + continue; |
| 83 | + var fn = token as FunctionToken; |
| 84 | + if (found != null || fn == null || !fn.Data.Isi(FunctionName)) |
| 85 | + return null; |
| 86 | + found = fn; |
| 87 | + } |
| 88 | + |
| 89 | + return found; |
| 90 | + } |
| 91 | + |
| 92 | + // in <color-space> [ <hue-interpolation-method> hue ]? (the hue method only after a polar space) |
| 93 | + private static bool TryParseMethod(List<Token> tokens, out string space, out string hue) |
| 94 | + { |
| 95 | + space = null; |
| 96 | + hue = null; |
| 97 | + |
| 98 | + int i = 0; |
| 99 | + |
| 100 | + if (i >= tokens.Count || !IsIdent(tokens[i], out var inWord) || !inWord.Isi(In)) |
| 101 | + return false; |
| 102 | + i++; |
| 103 | + |
| 104 | + if (i >= tokens.Count || !IsIdent(tokens[i], out var spaceName) || |
| 105 | + !ColorInterpolationMethodGrammar.IsColorSpace(spaceName)) |
| 106 | + return false; |
| 107 | + space = spaceName; |
| 108 | + i++; |
| 109 | + |
| 110 | + if (i < tokens.Count) |
| 111 | + { |
| 112 | + // A trailing "<dir> hue" is valid only for a polar color space. |
| 113 | + if (!ColorInterpolationMethodGrammar.IsPolarColorSpace(spaceName)) |
| 114 | + return false; |
| 115 | + if (!IsIdent(tokens[i], out var dir) || !ColorInterpolationMethodGrammar.IsHueDirection(dir)) |
| 116 | + return false; |
| 117 | + i++; |
| 118 | + if (i >= tokens.Count || !IsIdent(tokens[i], out var hueWord) || !hueWord.Isi(Hue)) |
| 119 | + return false; |
| 120 | + hue = dir; |
| 121 | + i++; |
| 122 | + } |
| 123 | + |
| 124 | + return i == tokens.Count; |
| 125 | + } |
| 126 | + |
| 127 | + // [ normal | light | dark | <dashed-ident> ] <percentage>? |
| 128 | + private static bool TryParseOperand(List<Token> tokens, out PaletteMixOperand operand) |
| 129 | + { |
| 130 | + operand = null; |
| 131 | + |
| 132 | + if (tokens.Count == 0 || tokens.Count > 2) |
| 133 | + return false; |
| 134 | + |
| 135 | + if (!IsIdent(tokens[0], out var palette) || !IsPaletteIdentifier(palette)) |
| 136 | + return false; |
| 137 | + |
| 138 | + double? percentage = null; |
| 139 | + if (tokens.Count == 2) |
| 140 | + { |
| 141 | + var pct = tokens[1] as UnitToken; |
| 142 | + if (pct == null || pct.Type != TokenType.Percentage) |
| 143 | + return false; |
| 144 | + percentage = pct.Value; |
| 145 | + } |
| 146 | + |
| 147 | + operand = new PaletteMixOperand(palette, percentage); |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + private static bool IsPaletteIdentifier(string ident) => |
| 152 | + ident.Isi(Keywords.Normal) || ident.Isi("light") || ident.Isi("dark") || |
| 153 | + ident.StartsWith("--", System.StringComparison.Ordinal); |
| 154 | + |
| 155 | + // Splits the argument tokens on top-level commas, dropping whitespace (insignificant in this grammar). |
| 156 | + private static List<List<Token>> SplitTopLevelComma(List<Token> tokens) |
| 157 | + { |
| 158 | + var groups = new List<List<Token>> { new List<Token>() }; |
| 159 | + foreach (var token in tokens) |
| 160 | + { |
| 161 | + if (token.Type == TokenType.Whitespace) |
| 162 | + continue; |
| 163 | + if (token.Type == TokenType.Comma) |
| 164 | + groups.Add(new List<Token>()); |
| 165 | + else |
| 166 | + groups[groups.Count - 1].Add(token); |
| 167 | + } |
| 168 | + |
| 169 | + return groups; |
| 170 | + } |
| 171 | + |
| 172 | + private static bool IsIdent(Token token, out string data) |
| 173 | + { |
| 174 | + if (token.Type == TokenType.Ident) |
| 175 | + { |
| 176 | + data = token.Data; |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + data = string.Empty; |
| 181 | + return false; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments