/src/serenity/Userland/Libraries/LibWeb/SVG/ViewBox.cpp
Line | Count | Source |
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 | | |
13 | | namespace Web::SVG { |
14 | | |
15 | | Optional<ViewBox> try_parse_view_box(StringView string) |
16 | 0 | { |
17 | | // FIXME: This should handle all valid viewBox values. |
18 | |
|
19 | 0 | GenericLexer lexer(string); |
20 | |
|
21 | 0 | enum State { |
22 | 0 | MinX, |
23 | 0 | MinY, |
24 | 0 | Width, |
25 | 0 | Height, |
26 | 0 | }; |
27 | 0 | int state { State::MinX }; |
28 | 0 | ViewBox view_box; |
29 | |
|
30 | 0 | while (!lexer.is_eof()) { |
31 | 0 | lexer.consume_while([](auto ch) { return is_ascii_space(ch); }); |
32 | 0 | auto token = lexer.consume_until([](auto ch) { return is_ascii_space(ch) && ch != ','; }); |
33 | 0 | auto maybe_number = token.to_number<float>(); |
34 | 0 | if (!maybe_number.has_value()) |
35 | 0 | return {}; |
36 | 0 | switch (state) { |
37 | 0 | case State::MinX: |
38 | 0 | view_box.min_x = maybe_number.value(); |
39 | 0 | break; |
40 | 0 | case State::MinY: |
41 | 0 | view_box.min_y = maybe_number.value(); |
42 | 0 | break; |
43 | 0 | case State::Width: |
44 | 0 | view_box.width = maybe_number.value(); |
45 | 0 | break; |
46 | 0 | case State::Height: |
47 | 0 | view_box.height = maybe_number.value(); |
48 | 0 | break; |
49 | 0 | default: |
50 | 0 | return {}; |
51 | 0 | } |
52 | 0 | state += 1; |
53 | 0 | } |
54 | | |
55 | 0 | return view_box; |
56 | 0 | } |
57 | | |
58 | | } |