diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-03-30 14:37:00 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-30 21:29:50 +0200 |
commit | d64ddeaec4e30dae5ad68c24c5c6bc9cc1496f68 (patch) | |
tree | 5c8fa44aa941b4587336d4957542d8d7954b1936 /Userland/Libraries | |
parent | bcebca62d37e6acc740eda3715fd525c9c230cbf (diff) | |
download | serenity-d64ddeaec4e30dae5ad68c24c5c6bc9cc1496f68.zip |
LibWeb: Move PositionValue into its own files
It's in Position.{h,cpp} because it represents a <position> in CSS, even
though it's currently named PositionValue to avoid collisions.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Position.cpp | 108 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Position.h | 53 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 94 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleValue.h | 39 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleValues/ConicGradientStyleValue.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.h | 1 |
8 files changed, 165 insertions, 133 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 66d63ce502..f7f07dde62 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -52,6 +52,7 @@ set(SOURCES CSS/Parser/Token.cpp CSS/Parser/Tokenizer.cpp CSS/Percentage.cpp + CSS/Position.cpp CSS/PreferredColorScheme.cpp CSS/Ratio.cpp CSS/Resolution.cpp diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 10c8695cea..f5f83af1d8 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -22,6 +22,7 @@ #include <LibWeb/CSS/Parser/Rule.h> #include <LibWeb/CSS/Parser/TokenStream.h> #include <LibWeb/CSS/Parser/Tokenizer.h> +#include <LibWeb/CSS/Position.h> #include <LibWeb/CSS/PropertyID.h> #include <LibWeb/CSS/Ratio.h> #include <LibWeb/CSS/Selector.h> diff --git a/Userland/Libraries/LibWeb/CSS/Position.cpp b/Userland/Libraries/LibWeb/CSS/Position.cpp new file mode 100644 index 0000000000..cfca1e29ac --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/Position.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "Position.h" +#include <LibGfx/Point.h> +#include <LibGfx/Rect.h> +#include <LibWeb/CSS/StyleValue.h> + +namespace Web::CSS { + +CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const +{ + // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense) + CSSPixels x = horizontal_position.visit( + [&](HorizontalPreset preset) -> CSSPixels { + return rect.width() * [&] { + switch (preset) { + case HorizontalPreset::Left: + return 0.0f; + case HorizontalPreset::Center: + return 0.5f; + case HorizontalPreset::Right: + return 1.0f; + default: + VERIFY_NOT_REACHED(); + } + }(); + }, + [&](LengthPercentage length_percentage) -> CSSPixels { + return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node); + }); + CSSPixels y = vertical_position.visit( + [&](VerticalPreset preset) -> CSSPixels { + return rect.height() * [&] { + switch (preset) { + case VerticalPreset::Top: + return 0.0f; + case VerticalPreset::Center: + return 0.5f; + case VerticalPreset::Bottom: + return 1.0f; + default: + VERIFY_NOT_REACHED(); + } + }(); + }, + [&](LengthPercentage length_percentage) -> CSSPixels { + return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node); + }); + if (x_relative_to == HorizontalEdge::Right) + x = rect.width() - x; + if (y_relative_to == VerticalEdge::Bottom) + y = rect.height() - y; + return CSSPixelPoint { rect.x() + x, rect.y() + y }; +} + +ErrorOr<void> PositionValue::serialize(StringBuilder& builder) const +{ + // Note: This means our serialization with simplify any with explicit edges that are just `top left`. + bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom; + if (has_relative_edges) + TRY(builder.try_append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv)); + TRY(horizontal_position.visit( + [&](HorizontalPreset preset) -> ErrorOr<void> { + return builder.try_append([&] { + switch (preset) { + case HorizontalPreset::Left: + return "left"sv; + case HorizontalPreset::Center: + return "center"sv; + case HorizontalPreset::Right: + return "right"sv; + default: + VERIFY_NOT_REACHED(); + } + }()); + }, + [&](LengthPercentage length_percentage) -> ErrorOr<void> { + return builder.try_appendff(TRY(length_percentage.to_string())); + })); + TRY(builder.try_append(' ')); + if (has_relative_edges) + TRY(builder.try_append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv)); + TRY(vertical_position.visit( + [&](VerticalPreset preset) -> ErrorOr<void> { + return builder.try_append([&] { + switch (preset) { + case VerticalPreset::Top: + return "top"sv; + case VerticalPreset::Center: + return "center"sv; + case VerticalPreset::Bottom: + return "bottom"sv; + default: + VERIFY_NOT_REACHED(); + } + }()); + }, + [&](LengthPercentage length_percentage) -> ErrorOr<void> { + return builder.try_append(TRY(length_percentage.to_string())); + })); + return {}; +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/Position.h b/Userland/Libraries/LibWeb/CSS/Position.h new file mode 100644 index 0000000000..f9f7425dcd --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/Position.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibWeb/CSS/Percentage.h> +#include <LibWeb/PixelUnits.h> + +namespace Web::CSS { + +// FIXME: Named PositionValue to avoid conflicts with enums, but this represents a <position> +struct PositionValue { + enum class HorizontalPreset { + Left, + Center, + Right + }; + + enum class VerticalPreset { + Top, + Center, + Bottom + }; + + enum class HorizontalEdge { + Left, + Right + }; + + enum class VerticalEdge { + Top, + Bottom + }; + + static PositionValue center() + { + return PositionValue { HorizontalPreset::Center, VerticalPreset::Center }; + } + + Variant<HorizontalPreset, LengthPercentage> horizontal_position { HorizontalPreset::Left }; + Variant<VerticalPreset, LengthPercentage> vertical_position { VerticalPreset::Top }; + HorizontalEdge x_relative_to { HorizontalEdge::Left }; + VerticalEdge y_relative_to { VerticalEdge::Top }; + + CSSPixelPoint resolved(Layout::Node const& node, CSSPixelRect const& rect) const; + ErrorOr<void> serialize(StringBuilder&) const; + bool operator==(PositionValue const&) const = default; +}; + +} diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 9b75dee93d..a563c12b76 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -1021,100 +1021,6 @@ CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartW return value->resolve(layout_node, percentage_basis); } -CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const -{ - // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense) - CSSPixels x = horizontal_position.visit( - [&](HorizontalPreset preset) -> CSSPixels { - return rect.width() * [&] { - switch (preset) { - case HorizontalPreset::Left: - return 0.0f; - case HorizontalPreset::Center: - return 0.5f; - case HorizontalPreset::Right: - return 1.0f; - default: - VERIFY_NOT_REACHED(); - } - }(); - }, - [&](LengthPercentage length_percentage) -> CSSPixels { - return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node); - }); - CSSPixels y = vertical_position.visit( - [&](VerticalPreset preset) -> CSSPixels { - return rect.height() * [&] { - switch (preset) { - case VerticalPreset::Top: - return 0.0f; - case VerticalPreset::Center: - return 0.5f; - case VerticalPreset::Bottom: - return 1.0f; - default: - VERIFY_NOT_REACHED(); - } - }(); - }, - [&](LengthPercentage length_percentage) -> CSSPixels { - return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node); - }); - if (x_relative_to == HorizontalEdge::Right) - x = rect.width() - x; - if (y_relative_to == VerticalEdge::Bottom) - y = rect.height() - y; - return CSSPixelPoint { rect.x() + x, rect.y() + y }; -} - -ErrorOr<void> PositionValue::serialize(StringBuilder& builder) const -{ - // Note: This means our serialization with simplify any with explicit edges that are just `top left`. - bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom; - if (has_relative_edges) - TRY(builder.try_append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv)); - TRY(horizontal_position.visit( - [&](HorizontalPreset preset) -> ErrorOr<void> { - return builder.try_append([&] { - switch (preset) { - case HorizontalPreset::Left: - return "left"sv; - case HorizontalPreset::Center: - return "center"sv; - case HorizontalPreset::Right: - return "right"sv; - default: - VERIFY_NOT_REACHED(); - } - }()); - }, - [&](LengthPercentage length_percentage) -> ErrorOr<void> { - return builder.try_appendff(TRY(length_percentage.to_string())); - })); - TRY(builder.try_append(' ')); - if (has_relative_edges) - TRY(builder.try_append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv)); - TRY(vertical_position.visit( - [&](VerticalPreset preset) -> ErrorOr<void> { - return builder.try_append([&] { - switch (preset) { - case VerticalPreset::Top: - return "top"sv; - case VerticalPreset::Center: - return "center"sv; - case VerticalPreset::Bottom: - return "bottom"sv; - default: - VERIFY_NOT_REACHED(); - } - }()); - }, - [&](LengthPercentage length_percentage) -> ErrorOr<void> { - return builder.try_append(TRY(length_percentage.to_string())); - })); - return {}; -} - Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) { if (length.is_px()) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index a1e8ea9548..86d140907e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -36,45 +36,6 @@ namespace Web::CSS { -// FIXME: Named PositionValue to avoid conflicts with enums, but this represents a <position> -struct PositionValue { - enum class HorizontalPreset { - Left, - Center, - Right - }; - - enum class VerticalPreset { - Top, - Center, - Bottom - }; - - enum class HorizontalEdge { - Left, - Right - }; - - enum class VerticalEdge { - Top, - Bottom - }; - - static PositionValue center() - { - return PositionValue { HorizontalPreset::Center, VerticalPreset::Center }; - } - - Variant<HorizontalPreset, LengthPercentage> horizontal_position { HorizontalPreset::Left }; - Variant<VerticalPreset, LengthPercentage> vertical_position { VerticalPreset::Top }; - HorizontalEdge x_relative_to { HorizontalEdge::Left }; - VerticalEdge y_relative_to { VerticalEdge::Top }; - - CSSPixelPoint resolved(Layout::Node const& node, CSSPixelRect const& rect) const; - ErrorOr<void> serialize(StringBuilder&) const; - bool operator==(PositionValue const&) const = default; -}; - // FIXME: Find a better place for this helper. inline Gfx::Painter::ScalingMode to_gfx_scaling_mode(CSS::ImageRendering css_value) { diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ConicGradientStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/ConicGradientStyleValue.h index 4cb277afc4..bb2ea86ad6 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ConicGradientStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ConicGradientStyleValue.h @@ -9,6 +9,7 @@ #pragma once +#include <LibWeb/CSS/Position.h> #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h> #include <LibWeb/Painting/GradientPainting.h> diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.h index e28bf66721..aae9e8c864 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.h @@ -11,6 +11,7 @@ #include <AK/Vector.h> #include <LibWeb/CSS/Enums.h> +#include <LibWeb/CSS/Position.h> #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h> #include <LibWeb/Painting/GradientPainting.h> |