2021-09-19 15:58:26 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGfx/Painter.h>
|
2021-11-12 20:12:56 +00:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2021-09-19 15:58:26 +01:00
|
|
|
#include <LibWeb/Painting/BackgroundPainting.h>
|
|
|
|
#include <LibWeb/Painting/PaintContext.h>
|
|
|
|
|
|
|
|
namespace Web::Painting {
|
|
|
|
|
2021-11-12 20:12:56 +00:00
|
|
|
void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMetrics const& layout_node, Gfx::IntRect const& border_rect, Color background_color, Vector<CSS::BackgroundLayerData> const* background_layers, BorderRadiusData const& border_radius)
|
2021-09-19 15:58:26 +01:00
|
|
|
{
|
2021-11-12 20:12:56 +00:00
|
|
|
auto& painter = context.painter();
|
|
|
|
|
|
|
|
auto get_box = [&](CSS::BackgroundBox box) {
|
|
|
|
auto box_rect = border_rect;
|
|
|
|
switch (box) {
|
|
|
|
case CSS::BackgroundBox::ContentBox: {
|
|
|
|
auto& padding = layout_node.box_model().padding;
|
|
|
|
box_rect.shrink(padding.top, padding.right, padding.bottom, padding.left);
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case CSS::BackgroundBox::PaddingBox: {
|
|
|
|
auto& border = layout_node.box_model().border;
|
|
|
|
box_rect.shrink(border.top, border.right, border.bottom, border.left);
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case CSS::BackgroundBox::BorderBox:
|
|
|
|
default:
|
|
|
|
return box_rect;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto color_rect = border_rect;
|
|
|
|
if (background_layers && !background_layers->is_empty())
|
|
|
|
color_rect = get_box(background_layers->last().clip);
|
2021-09-19 15:58:26 +01:00
|
|
|
// FIXME: Support elliptical corners
|
2021-11-12 20:12:56 +00:00
|
|
|
painter.fill_rect_with_rounded_corners(color_rect, background_color, border_radius.top_left, border_radius.top_right, border_radius.bottom_right, border_radius.bottom_left);
|
2021-11-12 12:44:22 +00:00
|
|
|
|
|
|
|
if (!background_layers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Note: Background layers are ordered front-to-back, so we paint them in reverse
|
|
|
|
for (int layer_index = background_layers->size() - 1; layer_index >= 0; layer_index--) {
|
|
|
|
auto& layer = background_layers->at(layer_index);
|
|
|
|
if (!layer.image || !layer.image->bitmap())
|
|
|
|
continue;
|
|
|
|
auto& image = *layer.image->bitmap();
|
2021-09-19 15:58:26 +01:00
|
|
|
|
2021-11-12 20:12:56 +00:00
|
|
|
// Clip
|
|
|
|
auto clip_rect = get_box(layer.clip);
|
|
|
|
painter.save();
|
|
|
|
painter.add_clip_rect(clip_rect);
|
|
|
|
|
|
|
|
auto painting_rect = border_rect;
|
|
|
|
|
|
|
|
// FIXME: Attachment
|
|
|
|
// FIXME: Size
|
|
|
|
int scaled_width = image.width();
|
|
|
|
int scaled_height = image.height();
|
|
|
|
|
|
|
|
// FIXME: Origin
|
|
|
|
// FIXME: Position
|
|
|
|
|
|
|
|
// Repetition
|
2021-11-12 12:44:22 +00:00
|
|
|
switch (layer.repeat_x) {
|
2021-09-19 15:58:26 +01:00
|
|
|
case CSS::Repeat::Round:
|
|
|
|
case CSS::Repeat::Space:
|
|
|
|
// FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
|
|
|
|
case CSS::Repeat::Repeat:
|
|
|
|
// The background rect is already sized to align with 'repeat'.
|
|
|
|
break;
|
|
|
|
case CSS::Repeat::NoRepeat:
|
2021-11-12 20:12:56 +00:00
|
|
|
painting_rect.set_width(min(painting_rect.width(), scaled_width));
|
2021-09-19 15:58:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-12 12:44:22 +00:00
|
|
|
switch (layer.repeat_y) {
|
2021-09-19 15:58:26 +01:00
|
|
|
case CSS::Repeat::Round:
|
|
|
|
case CSS::Repeat::Space:
|
|
|
|
// FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
|
|
|
|
case CSS::Repeat::Repeat:
|
|
|
|
// The background rect is already sized to align with 'repeat'.
|
|
|
|
break;
|
|
|
|
case CSS::Repeat::NoRepeat:
|
2021-11-12 20:12:56 +00:00
|
|
|
painting_rect.set_height(min(painting_rect.height(), scaled_height));
|
2021-09-19 15:58:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle rounded corners
|
2021-11-12 20:12:56 +00:00
|
|
|
painter.blit_tiled(painting_rect, image, image.rect());
|
|
|
|
painter.restore();
|
2021-09-19 15:58:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|