Skip to content

Commit

Permalink
LibWeb: Use Optional for previous SVG path control point
Browse files Browse the repository at this point in the history
Previously, a control point at `(0, 0)` would have been considered
absent. USe `Optional<Gfx::FloatPoint>` instead.
  • Loading branch information
gmta authored and trflynn89 committed Dec 29, 2022
1 parent c051532 commit d83f1ea
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Userland/Libraries/LibWeb/SVG/SVGPathElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <AK/Debug.h>
#include <AK/ExtraMathConstants.h>
#include <AK/Optional.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Path.h>
#include <LibWeb/DOM/Document.h>
Expand Down Expand Up @@ -102,7 +103,7 @@ void SVGPathElement::parse_attribute(FlyString const& name, DeprecatedString con
Gfx::Path path_from_path_instructions(Span<PathInstruction const> instructions)
{
Gfx::Path path;
Gfx::FloatPoint previous_control_point;
Optional<Gfx::FloatPoint> previous_control_point;
PathInstructionType last_instruction = PathInstructionType::Invalid;

for (auto& instruction : instructions) {
Expand Down Expand Up @@ -190,13 +191,13 @@ Gfx::Path path_from_path_instructions(Span<PathInstruction const> instructions)
case PathInstructionType::SmoothQuadraticBezierCurve: {
clear_last_control_point = false;

if (previous_control_point.is_null()
if (!previous_control_point.has_value()
|| ((last_instruction != PathInstructionType::QuadraticBezierCurve) && (last_instruction != PathInstructionType::SmoothQuadraticBezierCurve))) {
previous_control_point = last_point;
}

auto dx_end_control = last_point.dx_relative_to(previous_control_point);
auto dy_end_control = last_point.dy_relative_to(previous_control_point);
auto dx_end_control = last_point.dx_relative_to(previous_control_point.value());
auto dy_end_control = last_point.dy_relative_to(previous_control_point.value());
auto control_point = Gfx::FloatPoint { last_point.x() + dx_end_control, last_point.y() + dy_end_control };

Gfx::FloatPoint end_point = { data[0], data[1] };
Expand Down Expand Up @@ -231,7 +232,7 @@ Gfx::Path path_from_path_instructions(Span<PathInstruction const> instructions)
case PathInstructionType::SmoothCurve: {
clear_last_control_point = false;

if (previous_control_point.is_null()
if (!previous_control_point.has_value()
|| ((last_instruction != PathInstructionType::Curve) && (last_instruction != PathInstructionType::SmoothCurve))) {
previous_control_point = last_point;
}
Expand All @@ -240,8 +241,8 @@ Gfx::Path path_from_path_instructions(Span<PathInstruction const> instructions)
// 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))
auto reflected_previous_control_x = last_point.x() - previous_control_point.dx_relative_to(last_point);
auto reflected_previous_control_y = last_point.y() - previous_control_point.dy_relative_to(last_point);
auto reflected_previous_control_x = last_point.x() - previous_control_point.value().dx_relative_to(last_point);
auto reflected_previous_control_y = last_point.y() - previous_control_point.value().dy_relative_to(last_point);
Gfx::FloatPoint c1 = Gfx::FloatPoint { reflected_previous_control_x, reflected_previous_control_y };
Gfx::FloatPoint c2 = { data[0], data[1] };
Gfx::FloatPoint p2 = { data[2], data[3] };
Expand Down

0 comments on commit d83f1ea

Please sign in to comment.