|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <utility> |
| 5 | +#include <type_traits> |
| 6 | +#include <vector> |
| 7 | +#include <string> |
| 8 | +#include <optional> |
| 9 | +#include <algorithm> |
| 10 | +#include <cctype> |
| 11 | + |
| 12 | +#include "Effect.hpp" |
| 13 | +#include "StyleRule.hpp" |
| 14 | +#include "Environment.hpp" |
| 15 | + |
| 16 | +namespace margelo::nitro::cssnitro { |
| 17 | + |
| 18 | + // Trait to detect std::vector<T, Alloc> |
| 19 | + template<typename T> |
| 20 | + struct is_std_vector : std::false_type { |
| 21 | + }; |
| 22 | + template<typename T, typename Alloc> |
| 23 | + struct is_std_vector<std::vector<T, Alloc>> : std::true_type { |
| 24 | + }; |
| 25 | + |
| 26 | + class Rules { |
| 27 | + public: |
| 28 | + static bool testRule(const StyleRule &rule, reactnativecss::Effect::GetProxy &get) { |
| 29 | + if (rule.m && !rule.m->empty() && !testMediaQuery(*rule.m, get)) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + template<class Cond> |
| 36 | + static bool |
| 37 | + testMediaQuery(const std::vector<Cond> &media, reactnativecss::Effect::GetProxy &get) { |
| 38 | + for (const auto &cond: media) { |
| 39 | + if (!testMediaConditionDispatch(cond, get)) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + template<class L, class R> |
| 47 | + static bool testMediaComparison(const std::string &op, const L &left, const R &right, |
| 48 | + reactnativecss::Effect::GetProxy &get) { |
| 49 | + auto lhs = toNumber(left, get); |
| 50 | + auto rhs = toNumber(right, get); |
| 51 | + if (!lhs.has_value() || !rhs.has_value()) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + const double a = *lhs; |
| 55 | + const double b = *rhs; |
| 56 | + if (op == "=") return nearlyEqual(a, b); |
| 57 | + if (op == ">") return a > b; |
| 58 | + if (op == ">=") return a >= b || nearlyEqual(a, b); |
| 59 | + if (op == "<") return a < b; |
| 60 | + if (op == "<=") return a <= b || nearlyEqual(a, b); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + template<class Elem> |
| 65 | + static bool testMediaCondition(const std::vector<Elem> &condition, |
| 66 | + reactnativecss::Effect::GetProxy &get) { |
| 67 | + if (condition.empty()) return true; |
| 68 | + const std::string &op = condition[0]; |
| 69 | + if (op == "[]" || op == "!!") return false; |
| 70 | + if (op == "=" || op == ">" || op == ">=" || op == "<" || op == "<=") { |
| 71 | + if (condition.size() >= 3) { |
| 72 | + const auto &left = condition[1]; |
| 73 | + const auto &right = condition[2]; |
| 74 | + return testMediaComparison(op, left, right, get); |
| 75 | + } |
| 76 | + return false; |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + private: |
| 82 | + static bool nearlyEqual(double a, double b, double eps = 1e-9) { |
| 83 | + return std::abs(a - b) <= eps * std::max(1.0, std::max(std::abs(a), std::abs(b))); |
| 84 | + } |
| 85 | + |
| 86 | + static std::string lower(std::string s) { |
| 87 | + std::transform(s.begin(), s.end(), s.begin(), |
| 88 | + [](unsigned char c) { return (char) std::tolower(c); }); |
| 89 | + return s; |
| 90 | + } |
| 91 | + |
| 92 | + static std::optional<double> |
| 93 | + tokenToNumberFromEnv(const std::string &tok, reactnativecss::Effect::GetProxy &get) { |
| 94 | + const auto t = lower(tok); |
| 95 | + if (t == "w" || t == "width" || t == "screenwidth") { |
| 96 | + return get(reactnativecss::env::windowWidth()); |
| 97 | + } |
| 98 | + if (t == "h" || t == "height" || t == "screenheight") { |
| 99 | + return get(reactnativecss::env::windowHeight()); |
| 100 | + } |
| 101 | + if (t == "scale" || t == "dpr" || t == "pixelratio") { |
| 102 | + return get(reactnativecss::env::windowScale()); |
| 103 | + } |
| 104 | + if (t == "fontscale" || t == "fs") { |
| 105 | + return get(reactnativecss::env::windowFontScale()); |
| 106 | + } |
| 107 | + return std::nullopt; |
| 108 | + } |
| 109 | + |
| 110 | + static std::optional<double> parseNumberString(const std::string &s) { |
| 111 | + size_t start = 0, end = s.size(); |
| 112 | + while (start < end && std::isspace((unsigned char) s[start])) ++start; |
| 113 | + while (end > start && std::isspace((unsigned char) s[end - 1])) --end; |
| 114 | + if (start >= end) return std::nullopt; |
| 115 | + size_t i = start; |
| 116 | + if (s[i] == '+' || s[i] == '-') ++i; |
| 117 | + bool dot = false; |
| 118 | + while (i < end) { |
| 119 | + char c = s[i]; |
| 120 | + if (std::isdigit((unsigned char) c)) ++i; |
| 121 | + else if (c == '.' && !dot) { |
| 122 | + dot = true; |
| 123 | + ++i; |
| 124 | + } |
| 125 | + else break; |
| 126 | + } |
| 127 | + if (i == start || (i == start + 1 && (s[start] == '+' || s[start] == '-'))) |
| 128 | + return std::nullopt; |
| 129 | + try { return std::stod(std::string{s.begin() + (long) start, s.begin() + (long) i}); } |
| 130 | + catch (...) { return std::nullopt; } |
| 131 | + } |
| 132 | + |
| 133 | + template<class T> |
| 134 | + static std::optional<double> |
| 135 | + toNumber(const T &value, reactnativecss::Effect::GetProxy &get) { |
| 136 | + using Decayed = std::decay_t<T>; |
| 137 | + if constexpr (std::is_arithmetic_v<Decayed>) { |
| 138 | + return static_cast<double>(value); |
| 139 | + } else if constexpr (std::is_same_v<Decayed, bool>) { |
| 140 | + return value ? 1.0 : 0.0; |
| 141 | + } else if constexpr (std::is_same_v<Decayed, std::string>) { |
| 142 | + if (auto env = tokenToNumberFromEnv(value, get)) return env; |
| 143 | + return parseNumberString(value); |
| 144 | + } else if constexpr (std::is_same_v<Decayed, const char *>) { |
| 145 | + return toNumber(std::string(value), get); |
| 146 | + } else { |
| 147 | + return std::nullopt; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + template<class Cond> |
| 152 | + static bool |
| 153 | + testMediaConditionDispatch(const Cond &cond, reactnativecss::Effect::GetProxy &get) { |
| 154 | + if constexpr (is_std_vector<std::decay_t<Cond>>::value) { |
| 155 | + return testMediaCondition(cond, get); |
| 156 | + } else { |
| 157 | + return true; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + }; |
| 162 | + |
| 163 | +} // namespace margelo::nitro::cssnitro |
0 commit comments