| 1 | #include "theme.h" |
| 2 | |
| 3 | #include <cmath> |
| 4 | #include <regex> |
| 5 | |
| 6 | namespace twlite::theme { |
| 7 | |
| 8 | using twidget::Json; |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | const char* const THEME_KEYS[] = { |
| 13 | "bg_color", "text_color", "hint_color", "link_color", "button_color", "button_text_color", |
| 14 | "secondary_bg_color", "accent_text_color", "subtitle_text_color", "destructive_text_color", |
| 15 | "section_bg_color", "section_header_text_color", "section_separator_color", "header_bg_color", "bottom_bar_bg_color", |
| 16 | }; |
| 17 | // DEFAULT_THEME_PARAMS, in THEME_KEYS order. |
| 18 | const char* const LIGHT[] = { |
| 19 | "#ffffff", "#000000", "#707579", "#3390ec", "#3390ec", "#ffffff", "#f4f4f5", "#3390ec", "#707579", "#e53935", |
| 20 | "#ffffff", "#3390ec", "#e7e7e7", "#ffffff", "#ffffff", |
| 21 | }; |
| 22 | const char* const DARK[] = { |
| 23 | "#212121", "#ffffff", "#aaaaaa", "#8774e1", "#8774e1", "#ffffff", "#181818", "#8774e1", "#aaaaaa", "#ff595a", |
| 24 | "#212121", "#8774e1", "#3d3d3d", "#212121", "#212121", |
| 25 | }; |
| 26 | // DEFAULT_TEXT_TOKENS |
| 27 | const char* const TEXT_DEFAULTS[] = {"15px", "1.35", "8px 12px"}; |
| 28 | |
| 29 | const std::regex& LENGTH() { static const std::regex r("^(0|[0-9]{1,4}(\\.[0-9]{1,3})?(px|em|rem))$"); return r; } |
| 30 | const std::regex& COLOR() { static const std::regex r("^#[0-9a-fA-F]{6}$"); return r; } |
| 31 | const std::regex& FONT() { |
| 32 | static const std::regex r("^(?:(normal|italic) )?(?:(normal|bold|[1-9]00) )?[0-9]{1,3}(?:\\.[0-9]{1,2})?(?:px|em|rem)" |
| 33 | "(?:/[0-9]{1,3}(?:\\.[0-9]{1,2})?(?:px|em|rem)?)? ([a-z-]+(?:, ?[a-z-]+)*)$"); |
| 34 | return r; |
| 35 | } |
| 36 | const char* const GENERIC[] = {"system-ui", "sans-serif", "serif", "monospace", "ui-sans-serif", "ui-serif", "ui-monospace", "ui-rounded"}; |
| 37 | |
| 38 | std::string lower(std::string s) { |
| 39 | for (char& c : s) if (c >= 'A' && c <= 'Z') c = char(c - 'A' + 'a'); |
| 40 | return s; |
| 41 | } |
| 42 | |
| 43 | std::string trim(const std::string& s) { |
| 44 | size_t a = s.find_first_not_of(" \t\n\r\f"), b = s.find_last_not_of(" \t\n\r\f"); |
| 45 | return a == std::string::npos ? std::string() : s.substr(a, b - a + 1); |
| 46 | } |
| 47 | |
| 48 | using Check = std::string (*)(const Json&); // "" = refused |
| 49 | |
| 50 | std::string color(const Json& v) { return v.isString() && std::regex_match(v.asString(), COLOR()) ? lower(v. |
| 51 | std::string length(const Json& v) { return v.isString() && std::regex_match(v.asString(), LENGTH()) ? v.asString() |
| 52 | |
| 53 | std::string font(const Json& v) { |
| 54 | std::smatch m; |
| 55 | if (!v.isString() || !std::regex_match(v.asString(), m, FONT())) return ""; |
| 56 | std::string families = m[3].str(); |
| 57 | size_t start = 0; |
| 58 | while (start <= families.size()) { |
| 59 | size_t comma = families.find(',', start); |
| 60 | std::string f = trim(families.substr(start, comma == std::string::npos ? std::string::npos : comma - start)); |
| 61 | bool generic = false; |
| 62 | for (const char* g : GENERIC) generic = generic || f == g; |
| 63 | if (!generic) return ""; |
| 64 | if (comma == std::string::npos) break; |
| 65 | start = comma + 1; |
| 66 | } |
| 67 | return v.asString(); |
| 68 | } |
| 69 | |
| 70 | std::string padding(const Json& v) { |
| 71 | if (!v.isString()) return ""; |
| 72 | const std::string& s = v.asString(); |
| 73 | static const std::regex shape("^\\S+( \\S+){0,3}$"); |
| 74 | if (!std::regex_match(s, shape)) return ""; |
| 75 | size_t start = 0; |
| 76 | while (true) { |
| 77 | size_t sp = s.find(' ', start); |
| 78 | if (!std::regex_match(s.substr(start, sp == std::string::npos ? std::string::npos : sp - start), LENGTH())) return "" |
| 79 | if (sp == std::string::npos) break; |
| 80 | start = sp + 1; |
| 81 | } |
| 82 | return s; |
| 83 | } |
| 84 | |
| 85 | std::string opacity(const Json& v) { |
| 86 | std::string s = v.isNumber() ? Json::number(v.asNumber()) : v.isString() ? v.asString() : ""; |
| 87 | static const std::regex r("^(0(\\.[0-9]{1,3})?|1(\\.0{1,3})?|\\.[0-9]{1,3})$"); |
| 88 | return std::regex_match(s, r) ? s : ""; |
| 89 | } |
| 90 | |
| 91 | /// A number in [min, max] with at most `decimals` decimals, as a number or its plain decimal string (+ unit). |
| 92 | std::string bounded(const Json& v, double min, double max, int decimals, const char* unit) { |
| 93 | double n; |
| 94 | if (v.isNumber()) n = v.asNumber(); |
| 95 | else if (v.isString() && std::regex_match(v.asString(), std::regex("^[0-9]{1,2}(\\.[0-9]{1," + std::to_string(decimals) + |
| 96 | n = Json::parseDouble(v.asString().c_str()); |
| 97 | else return ""; |
| 98 | if (!std::isfinite(n) || n < min || n > max) return ""; |
| 99 | double f = std::pow(10.0, decimals); |
| 100 | return Json::number(std::round(n * f) / f) + unit; |
| 101 | } |
| 102 | |
| 103 | std::string font_size(const Json& v) { return bounded(v, 8, 64, 2, "px"); } |
| 104 | std::string line_height(const Json& v) { return bounded(v, 1, 3, 3, ""); } |
| 105 | |
| 106 | struct Var { const char* field; const char* name; Check check; }; |
| 107 | const Var BUTTON_VARS[] = { |
| 108 | {"bg", "--tg-button-bg", color}, |
| 109 | {"text", "--tg-button-text", color}, |
| 110 | {"radius", "--tg-button-radius", length}, |
| 111 | {"font", "--tg-button-font", font}, |
| 112 | {"padding", "--tg-button-padding", padding}, |
| 113 | {"height", "--tg-button-height", length}, |
| 114 | {"pressedOpacity", "--tg-button-pressed-opacity", opacity}, |
| 115 | {"disabledOpacity", "--tg-button-disabled-opacity", opacity}, |
| 116 | }; |
| 117 | const Var TEXT_VARS[] = { |
| 118 | {"fontSize", "--tg-text-font-size", font_size}, |
| 119 | {"lineHeight", "--tg-text-line-height", line_height}, |
| 120 | {"padding", "--tg-text-padding", padding}, |
| 121 | }; |
| 122 | |
| 123 | struct SafeAreaVar { const char* field; const char* name; double max; }; |
| 124 | const SafeAreaVar SAFE_AREA_VARS[] = { |
| 125 | {"top", "--tg-content-safe-area-inset-top", 64}, |
| 126 | {"right", "--tg-content-safe-area-inset-right", 64}, |
| 127 | {"bottom", "--tg-content-safe-area-inset-bottom", 64}, |
| 128 | {"left", "--tg-content-safe-area-inset-left", 64}, |
| 129 | {"cornerLeft", "--tg-content-safe-area-corner-left", 200}, |
| 130 | {"cornerRight", "--tg-content-safe-area-corner-right", 200}, |
| 131 | }; |
| 132 | |
| 133 | std::string var_name(const char* key) { |
| 134 | std::string name = std::string("--tg-theme-") + key; |
| 135 | for (char& c : name) if (c == '_') c = '-'; |
| 136 | return name; |
| 137 | } |
| 138 | |
| 139 | } // namespace |
| 140 | |
| 141 | std::vector<std::pair<std::string, std::string>> variables(const Json& theme) { |
| 142 | std::vector<std::pair<std::string, std::string>> vars; |
| 143 | bool dark = theme["colorScheme"].asString() == "dark"; |
| 144 | const Json& params = theme["params"]; |
| 145 | for (size_t i = 0; i < std::size(THEME_KEYS); i++) { |
| 146 | std::string given = color(params[THEME_KEYS[i]]); |
| 147 | vars.emplace_back(var_name(THEME_KEYS[i]), given.empty() ? (dark ? DARK[i] : LIGHT[i]) : given); |
| 148 | } |
| 149 | vars.emplace_back("--tg-color-scheme", dark ? "dark" : "light"); |
| 150 | const Json& button = theme["button"]; |
| 151 | for (const Var& v : BUTTON_VARS) { |
| 152 | std::string value = v.check(button[v.field]); |
| 153 | if (!value.empty()) vars.emplace_back(v.name, value); |
| 154 | } |
| 155 | const Json& text = theme["text"]; |
| 156 | for (size_t i = 0; i < std::size(TEXT_VARS); i++) { |
| 157 | std::string value = TEXT_VARS[i].check(text[TEXT_VARS[i].field]); |
| 158 | vars.emplace_back(TEXT_VARS[i].name, value.empty() ? TEXT_DEFAULTS[i] : value); |
| 159 | } |
| 160 | return vars; |
| 161 | } |
| 162 | |
| 163 | std::vector<std::pair<std::string, std::string>> safe_area_variables(const Json& inset) { |
| 164 | std::vector<std::pair<std::string, std::string>> vars; |
| 165 | for (const SafeAreaVar& v : SAFE_AREA_VARS) { |
| 166 | const Json& given = inset[v.field]; |
| 167 | double n = given.isNumber() ? given.asNumber() : 0; |
| 168 | if (!std::isfinite(n) || n < 0 || n > v.max) n = 0; |
| 169 | vars.emplace_back(v.name, Json::number(std::round(n * 100) / 100) + "px"); |
| 170 | } |
| 171 | return vars; |
| 172 | } |
| 173 | |
| 174 | bool is_variable(const std::string& name) { |
| 175 | if (name == "--tg-color-scheme") return true; |
| 176 | for (const SafeAreaVar& v : SAFE_AREA_VARS) if (name == v.name) return true; |
| 177 | for (const char* k : THEME_KEYS) if (name == var_name(k)) return true; |
| 178 | for (const Var& v : BUTTON_VARS) if (name == v.name) return true; |
| 179 | for (const Var& v : TEXT_VARS) if (name == v.name) return true; |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | std::string substitute(const std::string& css, const std::vector<std::pair<std::string, std:: |
| 184 | std::string out; |
| 185 | out.reserve(css.size()); |
| 186 | size_t i = 0; |
| 187 | while (i < css.size()) { |
| 188 | size_t at = css.find("var(", i); |
| 189 | if (at == std::string::npos) break; |
| 190 | // the matching ')' (fallbacks may hold functions: rgba(…)) |
| 191 | size_t end = at + 4; |
| 192 | int depth = 1; |
| 193 | size_t comma = std::string::npos; |
| 194 | for (; end < css.size() && depth; end++) { |
| 195 | char c = css[end]; |
| 196 | if (c == '(') depth++; |
| 197 | else if (c == ')') depth--; |
| 198 | else if (c == ',' && depth == 1 && comma == std::string::npos) comma = end; |
| 199 | } |
| 200 | if (depth) break; |
| 201 | // end is one past ')' |
| 202 | std::string name = trim(css.substr(at + 4, (comma == std::string::npos ? end - 1 : comma) - at - |
| 203 | std::string replacement; |
| 204 | bool found = false; |
| 205 | for (const auto& [k, v] : vars) if (k == name) { replacement = v; found = true; break; } |
| 206 | if (!found && comma != std::string::npos) { replacement = trim(css.substr(comma + 1, end - 1 - comma - |
| 207 | out.append(css, i, at - i); |
| 208 | if (found) out += replacement; |
| 209 | else out.append(css, at, end - at); |
| 210 | i = end; |
| 211 | } |
| 212 | out.append(css, i, std::string::npos); |
| 213 | return out; |
| 214 | } |
| 215 | |
| 216 | std::string unwrap_where(const std::string& css) { |
| 217 | std::string out; |
| 218 | out.reserve(css.size()); |
| 219 | size_t i = 0; |
| 220 | while (i < css.size()) { |
| 221 | size_t at = css.find(":where(", i); |
| 222 | if (at == std::string::npos) break; |
| 223 | size_t end = at + 7; |
| 224 | int depth = 1; |
| 225 | bool list = false; |
| 226 | for (; end < css.size() && depth; end++) { |
| 227 | char c = css[end]; |
| 228 | if (c == '(') depth++; |
| 229 | else if (c == ')') depth--; |
| 230 | else if (c == ',' && depth == 1) list = true; |
| 231 | } |
| 232 | if (depth) break; |
| 233 | // end is one past ')' |
| 234 | char next = end < css.size() ? css[end] : '{'; |
| 235 | out.append(css, i, at - i); |
| 236 | if (list && next != '{' && next != ',') out.append(css, at, end - at); |
| 237 | else out.append(css, at + 7, end - 1 - (at + 7)); |
| 238 | i = end; |
| 239 | } |
| 240 | out.append(css, i, std::string::npos); |
| 241 | return out; |
| 242 | } |
| 243 | |
| 244 | } // namespace twlite::theme |
| 245 | |