Serenity Operating System
at master 58 lines 1.4 kB view raw
1/* 2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/CharacterTypes.h> 8#include <AK/GenericLexer.h> 9#include <AK/Optional.h> 10#include <AK/StringView.h> 11#include <LibWeb/SVG/ViewBox.h> 12 13namespace Web::SVG { 14 15Optional<ViewBox> try_parse_view_box(StringView string) 16{ 17 // FIXME: This should handle all valid viewBox values. 18 19 GenericLexer lexer(string); 20 21 enum State { 22 MinX, 23 MinY, 24 Width, 25 Height, 26 }; 27 int state { State::MinX }; 28 ViewBox view_box; 29 30 while (!lexer.is_eof()) { 31 lexer.consume_while([](auto ch) { return is_ascii_space(ch); }); 32 auto token = lexer.consume_until([](auto ch) { return is_ascii_space(ch) && ch != ','; }); 33 auto maybe_number = token.to_int(); 34 if (!maybe_number.has_value()) 35 return {}; 36 switch (state) { 37 case State::MinX: 38 view_box.min_x = maybe_number.value(); 39 break; 40 case State::MinY: 41 view_box.min_y = maybe_number.value(); 42 break; 43 case State::Width: 44 view_box.width = maybe_number.value(); 45 break; 46 case State::Height: 47 view_box.height = maybe_number.value(); 48 break; 49 default: 50 return {}; 51 } 52 state += 1; 53 } 54 55 return view_box; 56} 57 58}