dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / svg / attribute_parser_1.lucb
#==============================================================================================
#
# attribute_parser_1 - SVG::AttributeParser: the static entry points and path data
#
# DESCRIPTION:
# Port of LibWeb/SVG/AttributeParser.cpp (lines 1-302) with the inline members of
# AttributeParser.h (NumberPercentage's constructors and accessors, match, done, ch,
# consume): construction, the static parse_* entry points for transforms, path data,
# coordinates, lengths, integers, number-percentages and points, and the path data
# commands (https://www.w3.org/TR/SVG2/paths.html#PathDataBNF).
#
# C++ ErrorOr<T> is `T!` with ak's error code; the parser is a stack value made with
# svg_attribute_parser_construct, as `AttributeParser parser { input };` is.
#
#==============================================================================================
# mark: NumberPercentage (AttributeParser.h) ====================================================
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:99 Web::SVG::NumberPercentage::NumberPercentage.
pub func svg_number_percentage_construct(this: SvgNumberPercentage*, value: f32, is_percentage: bool):
*this = SvgNumberPercentage(m_value = value / 100.0 if is_percentage else value, m_is_percentage = is_percentage)
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:105 Web::SVG::NumberPercentage::create_percentage.
pub func svg_number_percentage_create_percentage(value: f32) -> SvgNumberPercentage:
var result: SvgNumberPercentage
svg_number_percentage_construct(&result, value, true)
return result
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:110 Web::SVG::NumberPercentage::create_number.
pub func svg_number_percentage_create_number(value: f32) -> SvgNumberPercentage:
var result: SvgNumberPercentage
svg_number_percentage_construct(&result, value, false)
return result
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:117 Web::SVG::NumberPercentage::value.
pub func svg_number_percentage_value(this: const SvgNumberPercentage*) -> f32:
return this.m_value
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:118 Web::SVG::NumberPercentage::is_percentage.
pub func svg_number_percentage_is_percentage(this: const SvgNumberPercentage*) -> bool:
return this.m_is_percentage
# mark: AttributeParser's inline members (AttributeParser.h) ====================================
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:201 Web::SVG::AttributeParser::match.
func svg_attribute_parser_match(this: const SvgAttributeParser*, c_: u8) -> bool:
return not svg_attribute_parser_done(this) and svg_attribute_parser_ch(this) == c_
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:203 Web::SVG::AttributeParser::done.
func svg_attribute_parser_done(this: const SvgAttributeParser*) -> bool:
return ak.generic_lexer_is_eof(&this.m_lexer)
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:204 Web::SVG::AttributeParser::ch.
func svg_attribute_parser_ch(this: const SvgAttributeParser*, offset: usize = 0) -> u8:
return ak.generic_lexer_peek(&this.m_lexer, offset)
## Ported from Libraries/LibWeb/SVG/AttributeParser.h:205 Web::SVG::AttributeParser::consume.
func svg_attribute_parser_consume(this: SvgAttributeParser*) -> u8:
return ak.generic_lexer_consume(&this.m_lexer)
# mark: Construction and the static entry points =================================================
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:17 Web::SVG::AttributeParser::AttributeParser.
func svg_attribute_parser_construct(this: SvgAttributeParser*, source: ak.StringView):
*this = SvgAttributeParser(m_lexer = ak.generic_lexer_new(source), m_instructions = ak.Vector[SvgPathInstruction]())
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:22 Web::SVG::AttributeParser::parse_transform.
pub func svg_attribute_parser_parse_transform_string_view(input: ak.StringView) -> ak.Vector[SvgTransform]?:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
return svg_attribute_parser_parse_transform(&parser)
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:28 Web::SVG::AttributeParser::parse_path_data.
pub func svg_attribute_parser_parse_path_data(input: ak.StringView) -> SvgPath:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
svg_attribute_parser_parse_whitespace(&parser)
while not svg_attribute_parser_done(&parser):
svg_attribute_parser_parse_drawto(&parser) catch:
break
if not ak.vector_is_empty(&parser.m_instructions) and ak.vector_at(&parser.m_instructions, 0).type_ != SvgPathInstructionType.move:
# Invalid. "A path data segment (if there is one) must begin with a "moveto" command."
return SvgPath(m_instructions = ak.Vector[SvgPathInstruction]())
return SvgPath(m_instructions = parser.m_instructions)
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:44 Web::SVG::AttributeParser::parse_coordinate.
pub func svg_attribute_parser_parse_coordinate_string_view(input: ak.StringView) -> f32?:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
svg_attribute_parser_parse_whitespace(&parser)
let result = svg_attribute_parser_parse_coordinate(&parser) else return none
svg_attribute_parser_parse_whitespace(&parser)
if svg_attribute_parser_done(&parser):
return result
return none
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:57 Web::SVG::AttributeParser::parse_length.
pub func svg_attribute_parser_parse_length_string_view(input: ak.StringView) -> f32?:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
svg_attribute_parser_parse_whitespace(&parser)
let result = svg_attribute_parser_parse_length(&parser) else return none
svg_attribute_parser_parse_whitespace(&parser)
if svg_attribute_parser_done(&parser):
return result
return none
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:71 Web::SVG::AttributeParser::parse_integer.
pub func svg_attribute_parser_parse_integer_string_view(input: ak.StringView) -> i32?:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
svg_attribute_parser_parse_whitespace(&parser)
let result = svg_attribute_parser_parse_integer(&parser) else return none
svg_attribute_parser_parse_whitespace(&parser)
if svg_attribute_parser_done(&parser):
return result
return none
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:85 Web::SVG::NumberPercentage::resolve_relative_to.
pub func svg_number_percentage_resolve_relative_to(this: const SvgNumberPercentage*, length: f32) -> f32:
if not this.m_is_percentage:
return this.m_value
return this.m_value * length
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:92 Web::SVG::AttributeParser::parse_number_percentage.
pub func svg_attribute_parser_parse_number_percentage(input: ak.StringView) -> SvgNumberPercentage?:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
svg_attribute_parser_parse_whitespace(&parser)
let number = svg_attribute_parser_parse_number(&parser) else return none
let is_percentage = svg_attribute_parser_match(&parser, '%')
if is_percentage:
_ = svg_attribute_parser_consume(&parser)
svg_attribute_parser_parse_whitespace(&parser)
if svg_attribute_parser_done(&parser):
var result: SvgNumberPercentage
svg_number_percentage_construct(&result, number, is_percentage)
return result
return none
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:110 Web::SVG::AttributeParser::parse_positive_length.
pub func svg_attribute_parser_parse_positive_length(input: ak.StringView) -> f32?:
# 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.
var result = svg_attribute_parser_parse_length_string_view(input)
if let value = result:
if value < 0.0:
result = none
return result
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:120 Web::SVG::AttributeParser::parse_points.
pub func svg_attribute_parser_parse_points(input: ak.StringView) -> ak.Vector[gfx.FloatPoint]:
var parser: SvgAttributeParser = ---
svg_attribute_parser_construct(&parser, input)
svg_attribute_parser_parse_whitespace(&parser)
let coordinate_pair_sequence = svg_attribute_parser_parse_coordinate_pair_sequence(&parser) else return ak.Vector[gfx.FloatPoint]()
# FIXME: This is awkward. Can we return Gfx::FloatPoints from some of these parsing methods instead of Vector<float>?
var points = ak.Vector[gfx.FloatPoint]()
ak.vector_ensure_capacity(&points, ak.vector_size(&coordinate_pair_sequence))
for pair in ak.vector_span_const(&coordinate_pair_sequence):
ak.vector_append_t(&points, gfx.FloatPoint(m_x = *ak.vector_at_const(&pair, 0), m_y = *ak.vector_at_const(&pair, 1)))
return points
# mark: Path data commands =======================================================================
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:142 Web::SVG::AttributeParser::parse_drawto.
func svg_attribute_parser_parse_drawto(this: SvgAttributeParser*) -> !:
if svg_attribute_parser_match(this, 'M') or svg_attribute_parser_match(this, 'm'):
return try svg_attribute_parser_parse_moveto(this)
elif svg_attribute_parser_match(this, 'Z') or svg_attribute_parser_match(this, 'z'):
svg_attribute_parser_parse_closepath(this)
return
elif svg_attribute_parser_match(this, 'L') or svg_attribute_parser_match(this, 'l'):
return try svg_attribute_parser_parse_lineto(this)
elif svg_attribute_parser_match(this, 'H') or svg_attribute_parser_match(this, 'h'):
return try svg_attribute_parser_parse_horizontal_lineto(this)
elif svg_attribute_parser_match(this, 'V') or svg_attribute_parser_match(this, 'v'):
return try svg_attribute_parser_parse_vertical_lineto(this)
elif svg_attribute_parser_match(this, 'C') or svg_attribute_parser_match(this, 'c'):
return try svg_attribute_parser_parse_curveto(this)
elif svg_attribute_parser_match(this, 'S') or svg_attribute_parser_match(this, 's'):
return try svg_attribute_parser_parse_smooth_curveto(this)
elif svg_attribute_parser_match(this, 'Q') or svg_attribute_parser_match(this, 'q'):
return try svg_attribute_parser_parse_quadratic_bezier_curveto(this)
elif svg_attribute_parser_match(this, 'T') or svg_attribute_parser_match(this, 't'):
return try svg_attribute_parser_parse_smooth_quadratic_bezier_curveto(this)
elif svg_attribute_parser_match(this, 'A') or svg_attribute_parser_match(this, 'a'):
return try svg_attribute_parser_parse_elliptical_arc(this)
var message = ak.string_builder_new()
ak.string_builder_append(&message, "AttributeParser::parse_drawto failed to match: '")
ak.string_builder_append_u8(&message, svg_attribute_parser_ch(this))
ak.string_builder_append(&message, "'")
let text = ak.string_builder_to_string_without_validation(&message)
ak.dbgln(ak.string_view_text(ak.string_bytes_as_string_view(&text)))
error(ak.ak_error, "Invalid drawto command")
## Appends `{ type, absolute, data }` to the parser's instructions.
func svg_attribute_parser_append_instruction(this: SvgAttributeParser*, type_: SvgPathInstructionType, absolute: bool, data: ak.Vector[f32]):
ak.vector_append_t(&this.m_instructions, SvgPathInstruction(type_ = type_, absolute = absolute, data = data))
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:172 Web::SVG::AttributeParser::parse_moveto.
# https://www.w3.org/TR/SVG2/paths.html#PathDataMovetoCommands
func svg_attribute_parser_parse_moveto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'M'
svg_attribute_parser_parse_whitespace(this)
var is_first = true
let pairs = try svg_attribute_parser_parse_coordinate_pair_sequence(this)
for pair in ak.vector_span_const(&pairs):
# NOTE: "M 1 2 3 4" is equivalent to "M 1 2 L 3 4".
let type_ = SvgPathInstructionType.move if is_first else SvgPathInstructionType.line
svg_attribute_parser_append_instruction(this, type_, absolute, pair)
is_first = false
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:188 Web::SVG::AttributeParser::parse_closepath.
func svg_attribute_parser_parse_closepath(this: SvgAttributeParser*):
let absolute = svg_attribute_parser_consume(this) == 'Z'
svg_attribute_parser_parse_whitespace(this)
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.close_path, absolute, ak.Vector[f32]())
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:195 Web::SVG::AttributeParser::parse_lineto.
func svg_attribute_parser_parse_lineto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'L'
svg_attribute_parser_parse_whitespace(this)
let pairs = try svg_attribute_parser_parse_coordinate_pair_sequence(this)
for pair in ak.vector_span_const(&pairs):
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.line, absolute, pair)
return
## The one-coordinate data `{ coordinate }` of a horizontal or vertical lineto.
func svg_attribute_parser_single_value(value: f32) -> ak.Vector[f32]:
var data = ak.Vector[f32]()
ak.vector_append_t(&data, value)
return data
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:205 Web::SVG::AttributeParser::parse_horizontal_lineto.
func svg_attribute_parser_parse_horizontal_lineto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'H'
svg_attribute_parser_parse_whitespace(this)
let coordinates = try svg_attribute_parser_parse_coordinate_sequence(this)
for coordinate in ak.vector_span_const(&coordinates):
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.horizontal_line, absolute, svg_attribute_parser_single_value(coordinate))
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:215 Web::SVG::AttributeParser::parse_vertical_lineto.
func svg_attribute_parser_parse_vertical_lineto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'V'
svg_attribute_parser_parse_whitespace(this)
let coordinates = try svg_attribute_parser_parse_coordinate_sequence(this)
for coordinate in ak.vector_span_const(&coordinates):
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.vertical_line, absolute, svg_attribute_parser_single_value(coordinate))
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:225 Web::SVG::AttributeParser::parse_curveto.
func svg_attribute_parser_parse_curveto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'C'
svg_attribute_parser_parse_whitespace(this)
while true:
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.curve, absolute, try svg_attribute_parser_parse_coordinate_pair_triplet(this))
if svg_attribute_parser_match_comma_whitespace(this):
svg_attribute_parser_parse_comma_whitespace(this)
if not svg_attribute_parser_match_coordinate(this):
break
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:241 Web::SVG::AttributeParser::parse_smooth_curveto.
func svg_attribute_parser_parse_smooth_curveto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'S'
svg_attribute_parser_parse_whitespace(this)
while true:
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.smooth_curve, absolute, try svg_attribute_parser_parse_coordinate_pair_double(this))
if svg_attribute_parser_match_comma_whitespace(this):
svg_attribute_parser_parse_comma_whitespace(this)
if not svg_attribute_parser_match_coordinate(this):
break
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:257 Web::SVG::AttributeParser::parse_quadratic_bezier_curveto.
func svg_attribute_parser_parse_quadratic_bezier_curveto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'Q'
svg_attribute_parser_parse_whitespace(this)
while true:
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.quadratic_bezier_curve, absolute, try svg_attribute_parser_parse_coordinate_pair_double(this))
if svg_attribute_parser_match_comma_whitespace(this):
svg_attribute_parser_parse_comma_whitespace(this)
if not svg_attribute_parser_match_coordinate(this):
break
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:273 Web::SVG::AttributeParser::parse_smooth_quadratic_bezier_curveto.
func svg_attribute_parser_parse_smooth_quadratic_bezier_curveto(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'T'
svg_attribute_parser_parse_whitespace(this)
while true:
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.smooth_quadratic_bezier_curve, absolute, try svg_attribute_parser_parse_coordinate_pair(this))
if svg_attribute_parser_match_comma_whitespace(this):
svg_attribute_parser_parse_comma_whitespace(this)
if not svg_attribute_parser_match_coordinate(this):
break
return
## Ported from Libraries/LibWeb/SVG/AttributeParser.cpp:289 Web::SVG::AttributeParser::parse_elliptical_arc.
func svg_attribute_parser_parse_elliptical_arc(this: SvgAttributeParser*) -> !:
let absolute = svg_attribute_parser_consume(this) == 'A'
svg_attribute_parser_parse_whitespace(this)
while true:
let argument = try svg_attribute_parser_parse_elliptical_arc_argument(this)
svg_attribute_parser_append_instruction(this, SvgPathInstructionType.elliptical_arc, absolute, argument)
if svg_attribute_parser_match_comma_whitespace(this):
svg_attribute_parser_parse_comma_whitespace(this)
if not svg_attribute_parser_match_coordinate(this):
break
return