dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / svg / path.lucb
#==============================================================================================
#
# path - SVG::Path: parsed SVG path data and its conversion to a Gfx::Path
#
# DESCRIPTION:
# Port of LibWeb/SVG/Path.cpp (lines 1-306) with the inline members of Path.h: the
# serialization of path instructions (`d` as CSS's path() serializes it), their debug
# dump, and to_gfx_path, which turns relative coordinates, smooth curves and arcs into a
# Gfx::Path (https://svgwg.org/svg2-draft/paths.html).
#
# AK's `appendff(" {}", value)` of a float is ak.format_f32; the dbgln lines of dump are
# built the same way.
#
#==============================================================================================
## AK/Debug.h's PATH_DEBUG (off in the reference build).
let svg_path_debug: bool = false
# mark: PathInstruction ==========================================================================
## Ported from Libraries/LibWeb/SVG/Path.cpp:17 Web::SVG::PathInstruction::serialize.
pub func svg_path_instruction_serialize(this: const SvgPathInstruction*, builder: ak.StringBuilder*):
let absolute = this.absolute
match this.type_:
.move: ak.string_builder_append_u8(builder, 'M' if absolute else 'm')
.close_path:
# NB: This is always canonicalized as Z, not z.
ak.string_builder_append_u8(builder, 'Z')
.line: ak.string_builder_append_u8(builder, 'L' if absolute else 'l')
.horizontal_line: ak.string_builder_append_u8(builder, 'H' if absolute else 'h')
.vertical_line: ak.string_builder_append_u8(builder, 'V' if absolute else 'v')
.curve: ak.string_builder_append_u8(builder, 'C' if absolute else 'c')
.smooth_curve: ak.string_builder_append_u8(builder, 'S' if absolute else 's')
.quadratic_bezier_curve: ak.string_builder_append_u8(builder, 'Q' if absolute else 'q')
.smooth_quadratic_bezier_curve: ak.string_builder_append_u8(builder, 'T' if absolute else 't')
.elliptical_arc: ak.string_builder_append_u8(builder, 'A' if absolute else 'a')
_: svg_path_nothing() # PathInstructionType::Invalid appends no command letter
for value in ak.vector_span_const(&this.data):
ak.string_builder_append(builder, " ")
ak.format_f32(builder, value)
## The empty body of a `case ...: break;`.
func svg_path_nothing():
return
## One dbgln line of dump: `label` followed by `name=value` for each of the data's values from
## `start`, with the C++ format's separators (`(x1=…, y1=…), (x=…, y=…)` and the like).
func svg_path_instruction_dump_line(format_pieces: const str[], data: const ak.Vector[f32]*, start: usize):
var builder = ak.string_builder_new()
for i in 0..<format_pieces.length:
ak.string_builder_append(&builder, format_pieces[i])
if i + 1 < format_pieces.length:
ak.format_f32(&builder, *ak.vector_at_const(data, start + i))
let text = ak.string_builder_to_string_without_validation(&builder)
ak.dbgln(ak.string_view_text(ak.string_bytes_as_string_view(&text)))
## dbgln("Name (absolute={})", absolute).
func svg_path_instruction_dump_header(name: str, absolute: bool):
ak.dbgln(f"{name} (absolute={absolute})")
## Ported from Libraries/LibWeb/SVG/Path.cpp:59 Web::SVG::PathInstruction::dump.
pub func svg_path_instruction_dump(this: const SvgPathInstruction*):
let data = &this.data
let size = ak.vector_size(data)
match this.type_:
.move:
svg_path_instruction_dump_header("Move", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" x=", ", y=", ""], data, i)
i += 2
.close_path:
svg_path_instruction_dump_header("ClosePath", this.absolute)
.line:
svg_path_instruction_dump_header("Line", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" x=", ", y=", ""], data, i)
i += 2
.horizontal_line:
svg_path_instruction_dump_header("HorizontalLine", this.absolute)
for i in 0..<size:
svg_path_instruction_dump_line([" x=", ""], data, i)
.vertical_line:
svg_path_instruction_dump_header("VerticalLine", this.absolute)
for i in 0..<size:
svg_path_instruction_dump_line([" y=", ""], data, i)
.curve:
svg_path_instruction_dump_header("Curve", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" (x1=", ", y1=", ", x2=", ", y2=", "), (x=", ", y=", ")"], data, i)
i += 6
.smooth_curve:
svg_path_instruction_dump_header("SmoothCurve", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" (x2=", ", y2=", "), (x=", ", y=", ")"], data, i)
i += 4
.quadratic_bezier_curve:
svg_path_instruction_dump_header("QuadraticBezierCurve", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" (x1=", ", y1=", "), (x=", ", y=", ")"], data, i)
i += 4
.smooth_quadratic_bezier_curve:
svg_path_instruction_dump_header("SmoothQuadraticBezierCurve", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" x=", ", y=", ""], data, i)
i += 2
.elliptical_arc:
svg_path_instruction_dump_header("EllipticalArc", this.absolute)
var i: usize = 0
while i < size:
svg_path_instruction_dump_line([" (rx=", ", ry=", ") x-axis-rotation=", ", large-arc-flag=", ", sweep-flag=", ", (x=", ", y=", ")"],
data, i)
i += 7
_:
ak.dbgln("Invalid")
# mark: Path =====================================================================================
## Ported from Libraries/LibWeb/SVG/Path.h:48 Web::SVG::Path::Path.
pub func svg_path_construct(this: SvgPath*, instructions: ak.Vector[SvgPathInstruction]):
*this = SvgPath(m_instructions = instructions)
## Ported from Libraries/LibWeb/SVG/Path.h:52 Web::SVG::Path::instructions.
pub func svg_path_instructions(this: const SvgPath*) -> const SvgPathInstruction[]:
return ak.vector_span_const(&this.m_instructions)
## `Gfx::FloatPoint { x, y }`.
func svg_float_point(x: f32, y: f32) -> gfx.FloatPoint:
return gfx.FloatPoint(m_x = x, m_y = y)
## Ported from Libraries/LibWeb/SVG/Path.cpp:123 Web::SVG::Path::to_gfx_path.
pub func svg_path_to_gfx_path(this: const SvgPath*) -> gfx.Path:
var path: gfx.Path
var previous_control_point: gfx.FloatPoint? = none
var last_instruction = SvgPathInstructionType.invalid
let instructions = ak.vector_span_const(&this.m_instructions)
for instruction in &instructions:
# If the first path element uses relative coordinates, we treat them as absolute by making them relative to (0, 0).
let last_point = gfx.path_last_point(&path)
let absolute = instruction.absolute
let data = &instruction.data
if svg_path_debug:
svg_path_instruction_dump(instruction)
var clear_last_control_point = true
match instruction.type_:
.move:
let point = svg_float_point(*ak.vector_at_const(data, 0), *ak.vector_at_const(data, 1))
if absolute:
gfx.path_move_to(&path, point)
else:
gfx.path_move_to(&path, gfx.float_point_add(&point, last_point))
.close_path:
gfx.path_close(&path)
.line:
let point = svg_float_point(*ak.vector_at_const(data, 0), *ak.vector_at_const(data, 1))
if absolute:
gfx.path_line_to(&path, point)
else:
gfx.path_line_to(&path, gfx.float_point_add(&point, last_point))
.horizontal_line:
if absolute:
gfx.path_line_to(&path, svg_float_point(*ak.vector_at_const(data, 0), gfx.float_point_y(&last_point)))
else:
gfx.path_line_to(&path, svg_float_point(*ak.vector_at_const(data, 0) + gfx.float_point_x(&last_point), gfx.float_point_y(&last_point)))
.vertical_line:
if absolute:
gfx.path_line_to(&path, svg_float_point(gfx.float_point_x(&last_point), *ak.vector_at_const(data, 0)))
else:
gfx.path_line_to(&path, svg_float_point(gfx.float_point_x(&last_point), *ak.vector_at_const(data, 0) + gfx.float_point_y(&last_point)))
.elliptical_arc:
let rx = f64(*ak.vector_at_const(data, 0))
let ry = f64(*ak.vector_at_const(data, 1))
let x_axis_rotation = ak.to_radians_f64(f64(*ak.vector_at_const(data, 2)))
let large_arc_flag = f64(*ak.vector_at_const(data, 3))
let sweep_flag = f64(*ak.vector_at_const(data, 4))
var next_point: gfx.FloatPoint
if absolute:
next_point = svg_float_point(*ak.vector_at_const(data, 5), *ak.vector_at_const(data, 6))
else:
next_point = svg_float_point(*ak.vector_at_const(data, 5) + gfx.float_point_x(&last_point), *ak.vector_at_const(data, 6) + gfx.float_point_y(&last_point))
gfx.path_elliptical_arc_to(&path, next_point, gfx.FloatSize(m_width = (f32)rx, m_height = (f32)ry), (f32)x_axis_rotation, large_arc_flag != 0.0,
sweep_flag != 0.0)
.quadratic_bezier_curve:
clear_last_control_point = false
let through = svg_float_point(*ak.vector_at_const(data, 0), *ak.vector_at_const(data, 1))
let point = svg_float_point(*ak.vector_at_const(data, 2), *ak.vector_at_const(data, 3))
if absolute:
gfx.path_quadratic_bezier_curve_to(&path, through, point)
previous_control_point = through
else:
let control_point = gfx.float_point_add(&through, last_point)
gfx.path_quadratic_bezier_curve_to(&path, control_point, gfx.float_point_add(&point, last_point))
previous_control_point = control_point
.smooth_quadratic_bezier_curve:
clear_last_control_point = false
if previous_control_point == none or ((last_instruction != SvgPathInstructionType.quadratic_bezier_curve) and (last_instruction != SvgPathInstructionType.smooth_quadratic_bezier_curve)):
previous_control_point = last_point
let previous = previous_control_point else trap("VERIFY: previous_control_point.has_value()")
let dx_end_control = gfx.float_point_dx_relative_to(&last_point, previous)
let dy_end_control = gfx.float_point_dy_relative_to(&last_point, previous)
let control_point = svg_float_point(gfx.float_point_x(&last_point) + dx_end_control, gfx.float_point_y(&last_point) + dy_end_control)
let end_point = svg_float_point(*ak.vector_at_const(data, 0), *ak.vector_at_const(data, 1))
if absolute:
gfx.path_quadratic_bezier_curve_to(&path, control_point, end_point)
else:
gfx.path_quadratic_bezier_curve_to(&path, control_point, gfx.float_point_add(&end_point, last_point))
previous_control_point = control_point
.curve:
clear_last_control_point = false
var c1 = svg_float_point(*ak.vector_at_const(data, 0), *ak.vector_at_const(data, 1))
var c2 = svg_float_point(*ak.vector_at_const(data, 2), *ak.vector_at_const(data, 3))
var p2 = svg_float_point(*ak.vector_at_const(data, 4), *ak.vector_at_const(data, 5))
if not absolute:
_ = gfx.float_point_add_assign(&p2, last_point)
_ = gfx.float_point_add_assign(&c1, last_point)
_ = gfx.float_point_add_assign(&c2, last_point)
gfx.path_cubic_bezier_curve_to(&path, c1, c2, p2)
previous_control_point = c2
.smooth_curve:
clear_last_control_point = false
if previous_control_point == none or ((last_instruction != SvgPathInstructionType.curve) and (last_instruction != SvgPathInstructionType.smooth_curve)):
previous_control_point = last_point
# 9.5.2. Reflected control points https://svgwg.org/svg2-draft/paths.html#ReflectedControlPoints
# If the current point is (curx, cury) and the final control point of the previous path segment is (oldx2, oldy2),
# then the reflected point (i.e., (newx1, newy1), the first control point of the current path segment) is:
# (newx1, newy1) = (curx - (oldx2 - curx), cury - (oldy2 - cury))
let previous = previous_control_point else trap("VERIFY: previous_control_point.has_value()")
let reflected_previous_control_x = gfx.float_point_x(&last_point) - gfx.float_point_dx_relative_to(&previous, last_point)
let reflected_previous_control_y = gfx.float_point_y(&last_point) - gfx.float_point_dy_relative_to(&previous, last_point)
let c1 = svg_float_point(reflected_previous_control_x, reflected_previous_control_y)
var c2 = svg_float_point(*ak.vector_at_const(data, 0), *ak.vector_at_const(data, 1))
var p2 = svg_float_point(*ak.vector_at_const(data, 2), *ak.vector_at_const(data, 3))
if not absolute:
_ = gfx.float_point_add_assign(&p2, last_point)
_ = gfx.float_point_add_assign(&c2, last_point)
gfx.path_cubic_bezier_curve_to(&path, c1, c2, p2)
previous_control_point = c2
_: trap("VERIFY_NOT_REACHED") # PathInstructionType::Invalid
if clear_last_control_point:
previous_control_point = gfx.FloatPoint()
last_instruction = instruction.type_
return path
## Ported from Libraries/LibWeb/SVG/Path.cpp:291 Web::SVG::Path::serialize.
pub func svg_path_serialize(this: const SvgPath*) -> ak.String:
var builder = ak.string_builder_new()
var first = true
let instructions = ak.vector_span_const(&this.m_instructions)
for instruction in &instructions:
if first:
first = false
else:
ak.string_builder_append_u8(&builder, ' ')
svg_path_instruction_serialize(instruction, &builder)
return ak.string_builder_to_string_without_validation(&builder)