|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include <cstdint> |
| 4 | +#include <string> |
| 5 | +#include <stdexcept> |
| 6 | +#include <cassert> |
4 | 7 | #include <client/cssom/style_traits.hpp> |
5 | 8 |
|
6 | 9 | namespace client_cssom::values::specified |
@@ -159,4 +162,178 @@ namespace client_cssom::values::specified |
159 | 162 | protected: |
160 | 163 | Tag tag_ = Tag::kLTR; |
161 | 164 | }; |
| 165 | + |
| 166 | + class VerticalAlign : public Parse, |
| 167 | + public ToCss |
| 168 | + { |
| 169 | + friend class Parse; |
| 170 | + |
| 171 | + public: |
| 172 | + enum Tag : uint8_t |
| 173 | + { |
| 174 | + kBaseline, |
| 175 | + kSub, |
| 176 | + kSuper, |
| 177 | + kTop, |
| 178 | + kTextTop, |
| 179 | + kMiddle, |
| 180 | + kBottom, |
| 181 | + kTextBottom, |
| 182 | + kLength, // Custom length value |
| 183 | + kPercentage, // Percentage value |
| 184 | + }; |
| 185 | + |
| 186 | + public: |
| 187 | + static VerticalAlign Baseline() |
| 188 | + { |
| 189 | + return VerticalAlign(Tag::kBaseline); |
| 190 | + } |
| 191 | + static VerticalAlign Sub() |
| 192 | + { |
| 193 | + return VerticalAlign(Tag::kSub); |
| 194 | + } |
| 195 | + static VerticalAlign Super() |
| 196 | + { |
| 197 | + return VerticalAlign(Tag::kSuper); |
| 198 | + } |
| 199 | + static VerticalAlign Top() |
| 200 | + { |
| 201 | + return VerticalAlign(Tag::kTop); |
| 202 | + } |
| 203 | + static VerticalAlign TextTop() |
| 204 | + { |
| 205 | + return VerticalAlign(Tag::kTextTop); |
| 206 | + } |
| 207 | + static VerticalAlign Middle() |
| 208 | + { |
| 209 | + return VerticalAlign(Tag::kMiddle); |
| 210 | + } |
| 211 | + static VerticalAlign Bottom() |
| 212 | + { |
| 213 | + return VerticalAlign(Tag::kBottom); |
| 214 | + } |
| 215 | + static VerticalAlign TextBottom() |
| 216 | + { |
| 217 | + return VerticalAlign(Tag::kTextBottom); |
| 218 | + } |
| 219 | + static VerticalAlign Length(float value) |
| 220 | + { |
| 221 | + return VerticalAlign(Tag::kLength, value); |
| 222 | + } |
| 223 | + static VerticalAlign Percentage(float value) |
| 224 | + { |
| 225 | + return VerticalAlign(Tag::kPercentage, value); |
| 226 | + } |
| 227 | + |
| 228 | + public: |
| 229 | + VerticalAlign() |
| 230 | + : tag_(Tag::kBaseline) |
| 231 | + , value_(0.0f) |
| 232 | + { |
| 233 | + } |
| 234 | + |
| 235 | + protected: |
| 236 | + VerticalAlign(Tag tag, float value = 0.0f) |
| 237 | + : tag_(tag) |
| 238 | + , value_(value) |
| 239 | + { |
| 240 | + } |
| 241 | + |
| 242 | + public: |
| 243 | + Tag tag() const |
| 244 | + { |
| 245 | + return tag_; |
| 246 | + } |
| 247 | + float value() const |
| 248 | + { |
| 249 | + return value_; |
| 250 | + } |
| 251 | + std::string toCss() const override |
| 252 | + { |
| 253 | + switch (tag_) |
| 254 | + { |
| 255 | + case Tag::kBaseline: |
| 256 | + return "baseline"; |
| 257 | + case Tag::kSub: |
| 258 | + return "sub"; |
| 259 | + case Tag::kSuper: |
| 260 | + return "super"; |
| 261 | + case Tag::kTop: |
| 262 | + return "top"; |
| 263 | + case Tag::kTextTop: |
| 264 | + return "text-top"; |
| 265 | + case Tag::kMiddle: |
| 266 | + return "middle"; |
| 267 | + case Tag::kBottom: |
| 268 | + return "bottom"; |
| 269 | + case Tag::kTextBottom: |
| 270 | + return "text-bottom"; |
| 271 | + case Tag::kLength: |
| 272 | + return std::to_string(value_) + "px"; |
| 273 | + case Tag::kPercentage: |
| 274 | + return std::to_string(value_) + "%"; |
| 275 | + } |
| 276 | + assert(false && "Invalid tag."); |
| 277 | + } |
| 278 | + |
| 279 | + private: |
| 280 | + bool parse(const std::string &input) override |
| 281 | + { |
| 282 | + if (input == "baseline") |
| 283 | + tag_ = Tag::kBaseline; |
| 284 | + else if (input == "sub") |
| 285 | + tag_ = Tag::kSub; |
| 286 | + else if (input == "super") |
| 287 | + tag_ = Tag::kSuper; |
| 288 | + else if (input == "top") |
| 289 | + tag_ = Tag::kTop; |
| 290 | + else if (input == "text-top") |
| 291 | + tag_ = Tag::kTextTop; |
| 292 | + else if (input == "middle") |
| 293 | + tag_ = Tag::kMiddle; |
| 294 | + else if (input == "bottom") |
| 295 | + tag_ = Tag::kBottom; |
| 296 | + else if (input == "text-bottom") |
| 297 | + tag_ = Tag::kTextBottom; |
| 298 | + else |
| 299 | + { |
| 300 | + // Try to parse as length or percentage |
| 301 | + if (input.back() == '%') |
| 302 | + { |
| 303 | + try |
| 304 | + { |
| 305 | + float percentage = std::stof(input.substr(0, input.size() - 1)); |
| 306 | + tag_ = Tag::kPercentage; |
| 307 | + value_ = percentage; |
| 308 | + } |
| 309 | + catch (const std::exception &) |
| 310 | + { |
| 311 | + return false; |
| 312 | + } |
| 313 | + } |
| 314 | + else if (input.find("px") != std::string::npos) |
| 315 | + { |
| 316 | + try |
| 317 | + { |
| 318 | + float length = std::stof(input.substr(0, input.size() - 2)); |
| 319 | + tag_ = Tag::kLength; |
| 320 | + value_ = length; |
| 321 | + } |
| 322 | + catch (const std::exception &) |
| 323 | + { |
| 324 | + return false; |
| 325 | + } |
| 326 | + } |
| 327 | + else |
| 328 | + { |
| 329 | + return false; |
| 330 | + } |
| 331 | + } |
| 332 | + return true; |
| 333 | + } |
| 334 | + |
| 335 | + protected: |
| 336 | + Tag tag_ = Tag::kBaseline; |
| 337 | + float value_ = 0.0f; |
| 338 | + }; |
162 | 339 | } |
0 commit comments