2022-02-11 13:08:27 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2023-04-10 12:22:06 +01:00
|
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
2022-02-11 13:08:27 +00:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "AttributeParser.h"
|
2022-10-12 23:50:54 +02:00
|
|
|
|
#include <AK/FloatingPointStringConversions.h>
|
2023-04-10 12:22:06 +01:00
|
|
|
|
#include <AK/GenericShorthands.h>
|
2022-02-11 13:08:27 +00:00
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
|
2022-02-11 19:10:09 +00:00
|
|
|
|
AttributeParser::AttributeParser(StringView source)
|
2023-04-10 12:22:06 +01:00
|
|
|
|
: m_lexer(source)
|
2022-02-11 13:08:27 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Optional<Vector<Transform>> AttributeParser::parse_transform(StringView input)
|
|
|
|
|
{
|
|
|
|
|
AttributeParser parser { input };
|
|
|
|
|
return parser.parse_transform();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 19:20:24 +00:00
|
|
|
|
Vector<PathInstruction> AttributeParser::parse_path_data(StringView input)
|
2022-02-11 13:08:27 +00:00
|
|
|
|
{
|
2022-02-11 19:20:24 +00:00
|
|
|
|
AttributeParser parser { input };
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
while (!parser.done())
|
|
|
|
|
parser.parse_drawto();
|
|
|
|
|
if (!parser.m_instructions.is_empty() && parser.m_instructions[0].type != PathInstructionType::Move) {
|
|
|
|
|
// Invalid. "A path data segment (if there is one) must begin with a "moveto" command."
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
return parser.m_instructions;
|
2022-02-11 13:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 15:48:42 +00:00
|
|
|
|
Optional<float> AttributeParser::parse_coordinate(StringView input)
|
|
|
|
|
{
|
|
|
|
|
AttributeParser parser { input };
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (parser.match_coordinate()) {
|
|
|
|
|
float result = parser.parse_coordinate();
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (parser.done())
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<float> AttributeParser::parse_length(StringView input)
|
|
|
|
|
{
|
|
|
|
|
AttributeParser parser { input };
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (parser.match_coordinate()) {
|
|
|
|
|
float result = parser.parse_length();
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (parser.done())
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 22:39:07 +01:00
|
|
|
|
float NumberPercentage::resolve_relative_to(float length) const
|
2023-04-19 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (!m_is_percentage)
|
|
|
|
|
return m_value;
|
|
|
|
|
return m_value * length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<NumberPercentage> AttributeParser::parse_number_percentage(StringView input)
|
|
|
|
|
{
|
|
|
|
|
AttributeParser parser { input };
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (parser.match_number()) {
|
|
|
|
|
float number = parser.parse_number();
|
|
|
|
|
bool is_percentage = parser.match('%');
|
|
|
|
|
if (is_percentage)
|
|
|
|
|
parser.consume();
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (parser.done())
|
|
|
|
|
return NumberPercentage(number, is_percentage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 15:48:42 +00:00
|
|
|
|
Optional<float> AttributeParser::parse_positive_length(StringView input)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Where this is used, the spec usually (always?) says "A negative value is an error (see Error processing)."
|
|
|
|
|
// So, implement error processing! Maybe this should return ErrorOr.
|
|
|
|
|
auto result = parse_length(input);
|
|
|
|
|
if (result.has_value() && result.value() < 0)
|
|
|
|
|
result.clear();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 17:28:24 +00:00
|
|
|
|
Vector<Gfx::FloatPoint> AttributeParser::parse_points(StringView input)
|
|
|
|
|
{
|
|
|
|
|
AttributeParser parser { input };
|
|
|
|
|
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
|
|
|
|
|
// FIXME: "If an odd number of coordinates is provided, then the element is in error, with the same user agent behavior
|
|
|
|
|
// as occurs with an incorrectly specified ‘path’ element. In such error cases the user agent will drop the last,
|
|
|
|
|
// odd coordinate and otherwise render the shape."
|
|
|
|
|
// The parser currently doesn't notice that there is a missing coordinate, so make it notice!
|
|
|
|
|
auto coordinate_pair_sequence = parser.parse_coordinate_pair_sequence();
|
|
|
|
|
|
|
|
|
|
parser.parse_whitespace();
|
|
|
|
|
if (!parser.done())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// FIXME: This is awkward. Can we return Gfx::FloatPoints from some of these parsing methods instead of Vector<float>?
|
|
|
|
|
Vector<Gfx::FloatPoint> points;
|
|
|
|
|
points.ensure_capacity(coordinate_pair_sequence.size());
|
|
|
|
|
|
|
|
|
|
for (auto const& pair : coordinate_pair_sequence)
|
|
|
|
|
points.empend(pair[0], pair[1]);
|
|
|
|
|
|
|
|
|
|
return points;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:08:27 +00:00
|
|
|
|
void AttributeParser::parse_drawto()
|
|
|
|
|
{
|
|
|
|
|
if (match('M') || match('m')) {
|
|
|
|
|
parse_moveto();
|
|
|
|
|
} else if (match('Z') || match('z')) {
|
|
|
|
|
parse_closepath();
|
|
|
|
|
} else if (match('L') || match('l')) {
|
|
|
|
|
parse_lineto();
|
|
|
|
|
} else if (match('H') || match('h')) {
|
|
|
|
|
parse_horizontal_lineto();
|
|
|
|
|
} else if (match('V') || match('v')) {
|
|
|
|
|
parse_vertical_lineto();
|
|
|
|
|
} else if (match('C') || match('c')) {
|
|
|
|
|
parse_curveto();
|
|
|
|
|
} else if (match('S') || match('s')) {
|
|
|
|
|
parse_smooth_curveto();
|
|
|
|
|
} else if (match('Q') || match('q')) {
|
|
|
|
|
parse_quadratic_bezier_curveto();
|
|
|
|
|
} else if (match('T') || match('t')) {
|
|
|
|
|
parse_smooth_quadratic_bezier_curveto();
|
|
|
|
|
} else if (match('A') || match('a')) {
|
|
|
|
|
parse_elliptical_arc();
|
|
|
|
|
} else {
|
|
|
|
|
dbgln("AttributeParser::parse_drawto failed to match: '{}'", ch());
|
|
|
|
|
TODO();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 12:34:00 +02:00
|
|
|
|
// https://www.w3.org/TR/SVG2/paths.html#PathDataMovetoCommands
|
2022-02-11 13:08:27 +00:00
|
|
|
|
void AttributeParser::parse_moveto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'M';
|
|
|
|
|
parse_whitespace();
|
2023-04-17 12:34:00 +02:00
|
|
|
|
|
|
|
|
|
bool is_first = true;
|
|
|
|
|
for (auto& pair : parse_coordinate_pair_sequence()) {
|
|
|
|
|
// NOTE: "M 1 2 3 4" is equivalent to "M 1 2 L 3 4".
|
|
|
|
|
auto type = is_first ? PathInstructionType::Move : PathInstructionType::Line;
|
|
|
|
|
m_instructions.append({ type, absolute, pair });
|
|
|
|
|
is_first = false;
|
|
|
|
|
}
|
2022-02-11 13:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_closepath()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'Z';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
m_instructions.append({ PathInstructionType::ClosePath, absolute, {} });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_lineto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'L';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
for (auto& pair : parse_coordinate_pair_sequence())
|
|
|
|
|
m_instructions.append({ PathInstructionType::Line, absolute, pair });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_horizontal_lineto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'H';
|
|
|
|
|
parse_whitespace();
|
2023-06-14 21:22:13 +01:00
|
|
|
|
for (auto coordinate : parse_coordinate_sequence())
|
|
|
|
|
m_instructions.append({ PathInstructionType::HorizontalLine, absolute, { coordinate } });
|
2022-02-11 13:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_vertical_lineto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'V';
|
|
|
|
|
parse_whitespace();
|
2023-06-14 21:22:13 +01:00
|
|
|
|
for (auto coordinate : parse_coordinate_sequence())
|
|
|
|
|
m_instructions.append({ PathInstructionType::VerticalLine, absolute, { coordinate } });
|
2022-02-11 13:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_curveto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'C';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
m_instructions.append({ PathInstructionType::Curve, absolute, parse_coordinate_pair_triplet() });
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_smooth_curveto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'S';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
m_instructions.append({ PathInstructionType::SmoothCurve, absolute, parse_coordinate_pair_double() });
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_quadratic_bezier_curveto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'Q';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
m_instructions.append({ PathInstructionType::QuadraticBezierCurve, absolute, parse_coordinate_pair_double() });
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_smooth_quadratic_bezier_curveto()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'T';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
m_instructions.append({ PathInstructionType::SmoothQuadraticBezierCurve, absolute, parse_coordinate_pair() });
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_elliptical_arc()
|
|
|
|
|
{
|
|
|
|
|
bool absolute = consume() == 'A';
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2024-03-19 23:45:34 +00:00
|
|
|
|
m_instructions.append({ PathInstructionType::EllipticalArc, absolute, parse_elliptical_arc_argument() });
|
2022-02-11 13:08:27 +00:00
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 15:48:42 +00:00
|
|
|
|
float AttributeParser::parse_length()
|
2022-02-11 13:08:27 +00:00
|
|
|
|
{
|
2022-02-18 19:00:36 +01:00
|
|
|
|
// https://www.w3.org/TR/SVG11/types.html#DataTypeLength
|
|
|
|
|
return parse_number();
|
2022-02-11 13:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 15:48:42 +00:00
|
|
|
|
float AttributeParser::parse_coordinate()
|
|
|
|
|
{
|
|
|
|
|
// https://www.w3.org/TR/SVG11/types.html#DataTypeCoordinate
|
|
|
|
|
// coordinate ::= length
|
|
|
|
|
return parse_length();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:08:27 +00:00
|
|
|
|
Vector<float> AttributeParser::parse_coordinate_pair()
|
|
|
|
|
{
|
|
|
|
|
Vector<float> coordinates;
|
|
|
|
|
coordinates.append(parse_coordinate());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
coordinates.append(parse_coordinate());
|
|
|
|
|
return coordinates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<float> AttributeParser::parse_coordinate_sequence()
|
|
|
|
|
{
|
|
|
|
|
Vector<float> sequence;
|
|
|
|
|
while (true) {
|
|
|
|
|
sequence.append(parse_coordinate());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_comma_whitespace() && !match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return sequence;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<Vector<float>> AttributeParser::parse_coordinate_pair_sequence()
|
|
|
|
|
{
|
|
|
|
|
Vector<Vector<float>> sequence;
|
|
|
|
|
while (true) {
|
|
|
|
|
sequence.append(parse_coordinate_pair());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
if (!match_comma_whitespace() && !match_coordinate())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return sequence;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<float> AttributeParser::parse_coordinate_pair_double()
|
|
|
|
|
{
|
|
|
|
|
Vector<float> coordinates;
|
|
|
|
|
coordinates.extend(parse_coordinate_pair());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
coordinates.extend(parse_coordinate_pair());
|
|
|
|
|
return coordinates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<float> AttributeParser::parse_coordinate_pair_triplet()
|
|
|
|
|
{
|
|
|
|
|
Vector<float> coordinates;
|
|
|
|
|
coordinates.extend(parse_coordinate_pair());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
coordinates.extend(parse_coordinate_pair());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
coordinates.extend(parse_coordinate_pair());
|
|
|
|
|
return coordinates;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 23:45:34 +00:00
|
|
|
|
Vector<float> AttributeParser::parse_elliptical_arc_argument()
|
2022-02-11 13:08:27 +00:00
|
|
|
|
{
|
|
|
|
|
Vector<float> numbers;
|
2023-05-26 21:53:13 +10:00
|
|
|
|
numbers.append(parse_number());
|
2022-02-11 13:08:27 +00:00
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
2023-05-26 21:53:13 +10:00
|
|
|
|
numbers.append(parse_number());
|
2022-02-11 13:08:27 +00:00
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
numbers.append(parse_number());
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
numbers.append(parse_flag());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
numbers.append(parse_flag());
|
|
|
|
|
if (match_comma_whitespace())
|
|
|
|
|
parse_comma_whitespace();
|
|
|
|
|
numbers.extend(parse_coordinate_pair());
|
|
|
|
|
|
|
|
|
|
return numbers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_whitespace(bool must_match_once)
|
|
|
|
|
{
|
|
|
|
|
bool matched = false;
|
|
|
|
|
while (!done() && match_whitespace()) {
|
|
|
|
|
consume();
|
|
|
|
|
matched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VERIFY(!must_match_once || matched);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AttributeParser::parse_comma_whitespace()
|
|
|
|
|
{
|
|
|
|
|
if (match(',')) {
|
|
|
|
|
consume();
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
} else {
|
|
|
|
|
parse_whitespace(1);
|
|
|
|
|
if (match(','))
|
|
|
|
|
consume();
|
|
|
|
|
parse_whitespace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 19:00:36 +01:00
|
|
|
|
// https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
|
2022-02-11 13:08:27 +00:00
|
|
|
|
float AttributeParser::parse_number()
|
2022-02-18 19:00:36 +01:00
|
|
|
|
{
|
|
|
|
|
auto sign = parse_sign();
|
|
|
|
|
return sign * parse_nonnegative_number();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
|
|
|
|
|
float AttributeParser::parse_nonnegative_number()
|
2022-02-11 13:08:27 +00:00
|
|
|
|
{
|
2022-10-12 23:50:54 +02:00
|
|
|
|
// NOTE: The grammar is almost a floating point except we cannot have a sign
|
|
|
|
|
// at the start. That condition should have been checked by the caller.
|
|
|
|
|
VERIFY(!match('+') && !match('-'));
|
2022-02-11 13:08:27 +00:00
|
|
|
|
|
2023-04-10 12:22:06 +01:00
|
|
|
|
auto remaining_source_text = m_lexer.remaining();
|
2022-10-12 23:50:54 +02:00
|
|
|
|
char const* start = remaining_source_text.characters_without_null_termination();
|
2022-02-11 13:08:27 +00:00
|
|
|
|
|
2022-10-12 23:50:54 +02:00
|
|
|
|
auto maybe_float = parse_first_floating_point<float>(start, start + remaining_source_text.length());
|
|
|
|
|
VERIFY(maybe_float.parsed_value());
|
2023-04-10 12:22:06 +01:00
|
|
|
|
m_lexer.ignore(maybe_float.end_ptr - start);
|
2022-02-11 13:08:27 +00:00
|
|
|
|
|
2022-10-12 23:50:54 +02:00
|
|
|
|
return maybe_float.value;
|
2022-02-11 13:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float AttributeParser::parse_flag()
|
|
|
|
|
{
|
|
|
|
|
if (!match('0') && !match('1'))
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
return consume() - '0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AttributeParser::parse_sign()
|
|
|
|
|
{
|
|
|
|
|
if (match('-')) {
|
|
|
|
|
consume();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (match('+'))
|
|
|
|
|
consume();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 01:20:24 +01:00
|
|
|
|
static bool whitespace(char c)
|
2023-04-10 12:22:06 +01:00
|
|
|
|
{
|
|
|
|
|
// wsp:
|
|
|
|
|
// Either a U+000A LINE FEED, U+000D CARRIAGE RETURN, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
|
2023-04-17 01:20:24 +01:00
|
|
|
|
return AK::first_is_one_of(c, '\n', '\r', '\t', '\f', ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/coords.html#PreserveAspectRatioAttribute
|
|
|
|
|
Optional<PreserveAspectRatio> AttributeParser::parse_preserve_aspect_ratio(StringView input)
|
|
|
|
|
{
|
|
|
|
|
// <align> <meetOrSlice>?
|
|
|
|
|
GenericLexer lexer { input };
|
|
|
|
|
lexer.ignore_while(whitespace);
|
|
|
|
|
auto align_string = lexer.consume_until(whitespace);
|
|
|
|
|
if (align_string.is_empty())
|
|
|
|
|
return {};
|
|
|
|
|
lexer.ignore_while(whitespace);
|
|
|
|
|
auto meet_or_slice_string = lexer.consume_until(whitespace);
|
|
|
|
|
|
|
|
|
|
// <align> =
|
|
|
|
|
// none
|
|
|
|
|
// | xMinYMin | xMidYMin | xMaxYMin
|
|
|
|
|
// | xMinYMid | xMidYMid | xMaxYMid
|
|
|
|
|
// | xMinYMax | xMidYMax | xMaxYMax
|
|
|
|
|
auto align = [&]() -> Optional<PreserveAspectRatio::Align> {
|
|
|
|
|
if (align_string == "none"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::None;
|
|
|
|
|
if (align_string == "xMinYMin"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMinYMin;
|
|
|
|
|
if (align_string == "xMidYMin"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMidYMin;
|
|
|
|
|
if (align_string == "xMaxYMin"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMaxYMin;
|
|
|
|
|
if (align_string == "xMinYMid"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMinYMid;
|
|
|
|
|
if (align_string == "xMidYMid"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMidYMid;
|
|
|
|
|
if (align_string == "xMaxYMid"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMaxYMid;
|
|
|
|
|
if (align_string == "xMinYMax"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMinYMax;
|
|
|
|
|
if (align_string == "xMidYMax"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMidYMax;
|
|
|
|
|
if (align_string == "xMaxYMax"sv)
|
|
|
|
|
return PreserveAspectRatio::Align::xMaxYMax;
|
|
|
|
|
return {};
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
if (!align.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// <meetOrSlice> = meet | slice
|
|
|
|
|
auto meet_or_slice = [&]() -> Optional<PreserveAspectRatio::MeetOrSlice> {
|
|
|
|
|
if (meet_or_slice_string.is_empty() || meet_or_slice_string == "meet"sv)
|
|
|
|
|
return PreserveAspectRatio::MeetOrSlice::Meet;
|
|
|
|
|
if (meet_or_slice_string == "slice"sv)
|
|
|
|
|
return PreserveAspectRatio::MeetOrSlice::Slice;
|
|
|
|
|
return {};
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
if (!meet_or_slice.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return PreserveAspectRatio { *align, *meet_or_slice };
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 18:51:00 +01:00
|
|
|
|
// https://svgwg.org/svg2-draft/pservers.html#LinearGradientElementGradientUnitsAttribute
|
2023-09-10 14:00:47 +01:00
|
|
|
|
// https://drafts.fxtf.org/css-masking/#element-attrdef-mask-maskunits
|
|
|
|
|
// https://drafts.fxtf.org/css-masking/#element-attrdef-mask-maskcontentunits
|
|
|
|
|
Optional<SVGUnits> AttributeParser::parse_units(StringView input)
|
2023-04-21 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
GenericLexer lexer { input };
|
|
|
|
|
lexer.ignore_while(whitespace);
|
|
|
|
|
auto gradient_units_string = lexer.consume_until(whitespace);
|
|
|
|
|
if (gradient_units_string == "userSpaceOnUse"sv)
|
2023-09-10 14:00:47 +01:00
|
|
|
|
return SVGUnits::UserSpaceOnUse;
|
2023-04-21 18:51:00 +01:00
|
|
|
|
if (gradient_units_string == "objectBoundingBox"sv)
|
2023-09-10 14:00:47 +01:00
|
|
|
|
return SVGUnits::ObjectBoundingBox;
|
2023-04-21 18:51:00 +01:00
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 15:45:02 +01:00
|
|
|
|
// https://svgwg.org/svg2-draft/pservers.html#RadialGradientElementSpreadMethodAttribute
|
|
|
|
|
Optional<SpreadMethod> AttributeParser::parse_spread_method(StringView input)
|
|
|
|
|
{
|
|
|
|
|
GenericLexer lexer { input };
|
|
|
|
|
lexer.ignore_while(whitespace);
|
|
|
|
|
auto spread_method_string = lexer.consume_until(whitespace);
|
|
|
|
|
if (spread_method_string == "pad"sv)
|
|
|
|
|
return SpreadMethod::Pad;
|
|
|
|
|
if (spread_method_string == "repeat"sv)
|
|
|
|
|
return SpreadMethod::Repeat;
|
|
|
|
|
if (spread_method_string == "reflect"sv)
|
|
|
|
|
return SpreadMethod::Reflect;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 01:20:24 +01:00
|
|
|
|
// https://drafts.csswg.org/css-transforms/#svg-syntax
|
|
|
|
|
Optional<Vector<Transform>> AttributeParser::parse_transform()
|
|
|
|
|
{
|
2023-04-10 12:22:06 +01:00
|
|
|
|
auto consume_whitespace = [&] {
|
2023-04-17 01:20:24 +01:00
|
|
|
|
m_lexer.ignore_while(whitespace);
|
2023-04-10 12:22:06 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto consume_comma_whitespace = [&] {
|
|
|
|
|
consume_whitespace();
|
|
|
|
|
m_lexer.consume_specific(',');
|
|
|
|
|
consume_whitespace();
|
|
|
|
|
};
|
2023-05-27 16:37:32 +01:00
|
|
|
|
|
|
|
|
|
// FIXME: This parsing is quite lenient, so will accept (with default values) some transforms that should be rejected.
|
2023-04-10 12:22:06 +01:00
|
|
|
|
auto parse_optional_number = [&](float default_value = 0.0f) {
|
|
|
|
|
consume_comma_whitespace();
|
2023-04-19 18:51:00 +01:00
|
|
|
|
if (match_number())
|
2023-04-10 12:22:06 +01:00
|
|
|
|
return parse_number();
|
|
|
|
|
return default_value;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto try_parse_number = [&]() -> Optional<float> {
|
|
|
|
|
if (match_number())
|
|
|
|
|
return parse_number();
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-10 12:22:06 +01:00
|
|
|
|
auto parse_function = [&](auto body) -> Optional<Transform> {
|
|
|
|
|
consume_whitespace();
|
|
|
|
|
if (!m_lexer.consume_specific('('))
|
|
|
|
|
return {};
|
|
|
|
|
consume_whitespace();
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_operation = body();
|
|
|
|
|
if (!maybe_operation.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
Transform transform { .operation = Transform::Operation { *maybe_operation } };
|
2023-04-10 12:22:06 +01:00
|
|
|
|
consume_whitespace();
|
|
|
|
|
if (m_lexer.consume_specific(')'))
|
|
|
|
|
return transform;
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// NOTE: This looks very similar to the CSS transform but the syntax is not compatible.
|
|
|
|
|
Vector<Transform> transform_list;
|
|
|
|
|
consume_whitespace();
|
|
|
|
|
while (!done()) {
|
|
|
|
|
Optional<Transform> maybe_transform;
|
|
|
|
|
if (m_lexer.consume_specific("translate"sv)) {
|
2023-05-27 16:37:32 +01:00
|
|
|
|
maybe_transform = parse_function([&]() -> Optional<Transform::Translate> {
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Transform::Translate translate {};
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_x = try_parse_number();
|
|
|
|
|
if (!maybe_x.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
translate.x = *maybe_x;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
translate.y = parse_optional_number();
|
|
|
|
|
return translate;
|
|
|
|
|
});
|
|
|
|
|
} else if (m_lexer.consume_specific("scale"sv)) {
|
2023-05-27 16:37:32 +01:00
|
|
|
|
maybe_transform = parse_function([&]() -> Optional<Transform::Scale> {
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Transform::Scale scale {};
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_x = try_parse_number();
|
|
|
|
|
if (!maybe_x.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
scale.x = *maybe_x;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
scale.y = parse_optional_number(scale.x);
|
|
|
|
|
return scale;
|
|
|
|
|
});
|
|
|
|
|
} else if (m_lexer.consume_specific("rotate"sv)) {
|
2023-05-27 16:37:32 +01:00
|
|
|
|
maybe_transform = parse_function([&]() -> Optional<Transform::Rotate> {
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Transform::Rotate rotate {};
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_a = try_parse_number();
|
|
|
|
|
if (!maybe_a.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
rotate.a = *maybe_a;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
rotate.x = parse_optional_number();
|
|
|
|
|
rotate.y = parse_optional_number();
|
|
|
|
|
return rotate;
|
|
|
|
|
});
|
|
|
|
|
} else if (m_lexer.consume_specific("skewX"sv)) {
|
2023-05-27 16:37:32 +01:00
|
|
|
|
maybe_transform = parse_function([&]() -> Optional<Transform::SkewX> {
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Transform::SkewX skew_x {};
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_a = try_parse_number();
|
|
|
|
|
if (!maybe_a.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
skew_x.a = *maybe_a;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
return skew_x;
|
|
|
|
|
});
|
|
|
|
|
} else if (m_lexer.consume_specific("skewY"sv)) {
|
2023-05-27 16:37:32 +01:00
|
|
|
|
maybe_transform = parse_function([&]() -> Optional<Transform::SkewY> {
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Transform::SkewY skew_y {};
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_a = try_parse_number();
|
|
|
|
|
if (!maybe_a.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
skew_y.a = *maybe_a;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
return skew_y;
|
|
|
|
|
});
|
|
|
|
|
} else if (m_lexer.consume_specific("matrix"sv)) {
|
2023-05-27 16:37:32 +01:00
|
|
|
|
maybe_transform = parse_function([&]() -> Optional<Transform::Matrix> {
|
2023-04-10 12:22:06 +01:00
|
|
|
|
Transform::Matrix matrix;
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_a = try_parse_number();
|
|
|
|
|
if (!maybe_a.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
matrix.a = *maybe_a;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
consume_comma_whitespace();
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_b = try_parse_number();
|
|
|
|
|
if (!maybe_b.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
matrix.b = *maybe_b;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
consume_comma_whitespace();
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_c = try_parse_number();
|
|
|
|
|
if (!maybe_c.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
matrix.c = *maybe_c;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
consume_comma_whitespace();
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_d = try_parse_number();
|
|
|
|
|
if (!maybe_d.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
matrix.d = *maybe_d;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
consume_comma_whitespace();
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_e = try_parse_number();
|
|
|
|
|
if (!maybe_e.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
matrix.e = *maybe_e;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
consume_comma_whitespace();
|
2023-05-27 16:37:32 +01:00
|
|
|
|
auto maybe_f = try_parse_number();
|
|
|
|
|
if (!maybe_f.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
matrix.f = *maybe_f;
|
2023-04-10 12:22:06 +01:00
|
|
|
|
return matrix;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (maybe_transform.has_value())
|
|
|
|
|
transform_list.append(*maybe_transform);
|
|
|
|
|
else
|
|
|
|
|
return {};
|
|
|
|
|
consume_comma_whitespace();
|
|
|
|
|
}
|
|
|
|
|
return transform_list;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:08:27 +00:00
|
|
|
|
bool AttributeParser::match_whitespace() const
|
|
|
|
|
{
|
|
|
|
|
if (done())
|
|
|
|
|
return false;
|
|
|
|
|
char c = ch();
|
|
|
|
|
return c == 0x9 || c == 0x20 || c == 0xa || c == 0xc || c == 0xd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AttributeParser::match_comma_whitespace() const
|
|
|
|
|
{
|
|
|
|
|
return match_whitespace() || match(',');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AttributeParser::match_coordinate() const
|
2022-02-11 15:48:42 +00:00
|
|
|
|
{
|
|
|
|
|
return match_length();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 18:51:00 +01:00
|
|
|
|
bool AttributeParser::match_number() const
|
|
|
|
|
{
|
|
|
|
|
return match_length();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 15:48:42 +00:00
|
|
|
|
bool AttributeParser::match_length() const
|
2022-02-11 13:08:27 +00:00
|
|
|
|
{
|
|
|
|
|
return !done() && (isdigit(ch()) || ch() == '-' || ch() == '+' || ch() == '.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|