1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

LibWeb: Move border-painting code out of Box

The logic here is needed by InlineNode too. Moving it into a
`paint_all_borders()` function makes it available to them both, as well
as anyone else who wants it. :^)
This commit is contained in:
Sam Atkins 2021-09-19 20:47:58 +01:00 committed by Andreas Kling
parent eb07668589
commit e1f3fb0146
3 changed files with 147 additions and 130 deletions

View file

@ -59,119 +59,13 @@ void Box::paint(PaintContext& context, PaintPhase phase)
void Box::paint_border(PaintContext& context)
{
auto const bordered_rect = this->bordered_rect();
auto const border_rect = bordered_rect;
auto const border_radius_data = normalized_border_radius_data();
auto const top_left_radius = border_radius_data.top_left;
auto const top_right_radius = border_radius_data.top_right;
auto const bottom_right_radius = border_radius_data.bottom_right;
auto const bottom_left_radius = border_radius_data.bottom_left;
// FIXME: Support elliptical border radii.
Gfx::FloatRect top_border_rect = {
border_rect.x() + top_left_radius,
border_rect.y(),
border_rect.width() - top_left_radius - top_right_radius,
border_rect.height()
auto borders_data = Painting::BordersData {
.top = computed_values().border_top(),
.right = computed_values().border_right(),
.bottom = computed_values().border_bottom(),
.left = computed_values().border_left(),
};
Gfx::FloatRect right_border_rect = {
border_rect.x(),
border_rect.y() + top_right_radius,
border_rect.width(),
border_rect.height() - top_right_radius - bottom_right_radius
};
Gfx::FloatRect bottom_border_rect = {
border_rect.x() + bottom_left_radius,
border_rect.y(),
border_rect.width() - bottom_left_radius - bottom_right_radius,
border_rect.height()
};
Gfx::FloatRect left_border_rect = {
border_rect.x(),
border_rect.y() + top_left_radius,
border_rect.width(),
border_rect.height() - top_left_radius - bottom_left_radius
};
Painting::paint_border(context, Painting::BorderEdge::Top, top_border_rect, computed_values());
Painting::paint_border(context, Painting::BorderEdge::Right, right_border_rect, computed_values());
Painting::paint_border(context, Painting::BorderEdge::Bottom, bottom_border_rect, computed_values());
Painting::paint_border(context, Painting::BorderEdge::Left, left_border_rect, computed_values());
// Draws a quarter cirle clockwise
auto draw_quarter_circle = [&](Gfx::FloatPoint const& from, Gfx::FloatPoint const& to, Gfx::Color color, int thickness) {
Gfx::FloatPoint center = { 0, 0 };
Gfx::FloatPoint offset = { 0, 0 };
Gfx::FloatPoint circle_position = { 0, 0 };
auto radius = fabsf(from.x() - to.x());
if (from.x() < to.x() && from.y() > to.y()) {
// top-left
center.set_x(radius);
center.set_y(radius);
offset.set_y(1);
} else if (from.x() < to.x() && from.y() < to.y()) {
// top-right
circle_position.set_x(from.x());
center.set_y(radius);
offset.set_x(-1);
offset.set_y(1);
} else if (from.x() > to.x() && from.y() < to.y()) {
// bottom-right
circle_position.set_x(to.x());
circle_position.set_y(from.y());
offset.set_x(-1);
} else if (from.x() > to.x() && from.y() > to.y()) {
// bottom-left
circle_position.set_y(to.y());
center.set_x(radius);
} else {
// You are lying about your intentions of drawing a quarter circle, your coordinates are (partly) the same!
return;
}
Gfx::FloatRect circle_rect = {
border_rect.x() + circle_position.x(),
border_rect.y() + circle_position.y(),
radius,
radius
};
context.painter().draw_circle_arc_intersecting(
Gfx::enclosing_int_rect(circle_rect),
(center + offset).to_rounded<int>(),
radius,
color,
thickness);
};
// FIXME: Which color to use?
if (top_left_radius != 0) {
Gfx::FloatPoint arc_start = { 0, top_left_radius };
Gfx::FloatPoint arc_end = { top_left_radius, 0 };
draw_quarter_circle(arc_start, arc_end, computed_values().border_top().color, computed_values().border_top().width);
}
if (top_right_radius != 0) {
Gfx::FloatPoint arc_start = { top_left_radius + top_border_rect.width(), 0 };
Gfx::FloatPoint arc_end = { bordered_rect.width(), top_right_radius };
draw_quarter_circle(arc_start, arc_end, computed_values().border_top().color, computed_values().border_top().width);
}
if (bottom_right_radius != 0) {
Gfx::FloatPoint arc_start = { bordered_rect.width(), top_right_radius + right_border_rect.height() };
Gfx::FloatPoint arc_end = { bottom_border_rect.width() + bottom_left_radius, bordered_rect.height() };
draw_quarter_circle(arc_start, arc_end, computed_values().border_bottom().color, computed_values().border_bottom().width);
}
if (bottom_left_radius != 0) {
Gfx::FloatPoint arc_start = { bottom_left_radius, bordered_rect.height() };
Gfx::FloatPoint arc_end = { 0, bordered_rect.height() - bottom_left_radius };
draw_quarter_circle(arc_start, arc_end, computed_values().border_bottom().color, computed_values().border_bottom().width);
}
Painting::paint_all_borders(context, bordered_rect(), normalized_border_radius_data(), borders_data);
}
void Box::paint_background(PaintContext& context)