2021-09-19 14:58:26 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2023-08-08 19:45:38 +00:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2022-07-11 23:30:35 +00:00
|
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
2021-09-19 14:58:26 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-09-09 16:16:25 +00:00
|
|
|
#include <LibWeb/CSS/Sizing.h>
|
2021-11-12 20:12:56 +00:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2024-10-14 14:07:56 +00:00
|
|
|
#include <LibWeb/Layout/TextNode.h>
|
2023-02-25 10:04:29 +00:00
|
|
|
#include <LibWeb/Layout/Viewport.h>
|
2021-09-19 14:58:26 +00:00
|
|
|
#include <LibWeb/Painting/BackgroundPainting.h>
|
2024-04-27 12:26:42 +00:00
|
|
|
#include <LibWeb/Painting/PaintableBox.h>
|
2021-09-19 14:58:26 +00:00
|
|
|
|
|
|
|
namespace Web::Painting {
|
|
|
|
|
2024-08-06 12:26:47 +00:00
|
|
|
static RefPtr<DisplayList> compute_text_clip_paths(PaintContext& context, Paintable const& paintable, CSSPixelPoint containing_block_location)
|
2024-04-23 10:07:58 +00:00
|
|
|
{
|
2024-07-24 19:03:19 +00:00
|
|
|
auto text_clip_paths = DisplayList::create();
|
|
|
|
DisplayListRecorder display_list_recorder(*text_clip_paths);
|
2024-08-06 12:26:47 +00:00
|
|
|
// Remove containing block offset, so executing the display list will produce mask at (0, 0)
|
|
|
|
display_list_recorder.translate(-context.floored_device_point(containing_block_location).to_type<int>());
|
2024-04-23 10:07:58 +00:00
|
|
|
auto add_text_clip_path = [&](PaintableFragment const& fragment) {
|
2024-06-29 15:14:23 +00:00
|
|
|
auto glyph_run = fragment.glyph_run();
|
|
|
|
if (!glyph_run || glyph_run->glyphs().is_empty())
|
|
|
|
return;
|
2024-04-23 10:07:58 +00:00
|
|
|
|
|
|
|
auto fragment_absolute_rect = fragment.absolute_rect();
|
|
|
|
auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
|
|
|
|
|
2024-07-24 19:03:19 +00:00
|
|
|
DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) };
|
|
|
|
auto scale = context.device_pixels_per_css_pixel();
|
2024-10-29 14:29:00 +00:00
|
|
|
display_list_recorder.draw_text_run(baseline_start.to_type<int>(), *glyph_run, Gfx::Color::Black, fragment_absolute_device_rect.to_type<int>(), scale, fragment.orientation());
|
2024-04-23 10:07:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
paintable.for_each_in_inclusive_subtree([&](auto& paintable) {
|
|
|
|
if (is<PaintableWithLines>(paintable)) {
|
|
|
|
auto const& paintable_lines = static_cast<PaintableWithLines const&>(paintable);
|
|
|
|
for (auto const& fragment : paintable_lines.fragments()) {
|
|
|
|
if (is<Layout::TextNode>(fragment.layout_node()))
|
|
|
|
add_text_clip_path(fragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
return text_clip_paths;
|
|
|
|
}
|
|
|
|
|
2024-10-17 20:04:12 +00:00
|
|
|
static BackgroundBox get_box(CSS::BackgroundBox box_clip, BackgroundBox border_box, auto const& paintable_box)
|
2024-08-02 13:58:26 +00:00
|
|
|
{
|
|
|
|
auto box = border_box;
|
|
|
|
switch (box_clip) {
|
|
|
|
case CSS::BackgroundBox::ContentBox: {
|
2024-10-17 20:04:12 +00:00
|
|
|
auto& padding = paintable_box.box_model().padding;
|
2024-08-02 13:58:26 +00:00
|
|
|
box.shrink(padding.top, padding.right, padding.bottom, padding.left);
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case CSS::BackgroundBox::PaddingBox: {
|
2024-10-17 20:04:12 +00:00
|
|
|
auto& border = paintable_box.box_model().border;
|
2024-08-02 13:58:26 +00:00
|
|
|
box.shrink(border.top, border.right, border.bottom, border.left);
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case CSS::BackgroundBox::BorderBox:
|
|
|
|
default:
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:39:52 +00:00
|
|
|
// https://www.w3.org/TR/css-backgrounds-3/#backgrounds
|
2024-10-17 20:04:12 +00:00
|
|
|
void paint_background(PaintContext& context, PaintableBox const& paintable_box, CSS::ImageRendering image_rendering, ResolvedBackground resolved_background, BorderRadiiData const& border_radii)
|
2021-09-19 14:58:26 +00:00
|
|
|
{
|
2024-08-06 12:26:47 +00:00
|
|
|
auto& display_list_recorder = context.display_list_recorder();
|
|
|
|
|
|
|
|
DisplayListRecorderStateSaver state { display_list_recorder };
|
2024-08-02 13:58:26 +00:00
|
|
|
if (resolved_background.needs_text_clip) {
|
2024-10-17 20:04:12 +00:00
|
|
|
auto display_list = compute_text_clip_paths(context, paintable_box, resolved_background.background_rect.location());
|
2024-08-06 12:26:47 +00:00
|
|
|
auto rect = context.rounded_device_rect(resolved_background.background_rect);
|
|
|
|
display_list_recorder.add_mask(move(display_list), rect.to_type<int>());
|
2024-04-23 10:07:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 22:02:36 +00:00
|
|
|
BackgroundBox border_box {
|
2024-08-02 13:58:26 +00:00
|
|
|
resolved_background.background_rect,
|
2022-07-02 22:02:36 +00:00
|
|
|
border_radii
|
|
|
|
};
|
|
|
|
|
2024-08-02 13:58:26 +00:00
|
|
|
auto const& color_box = resolved_background.color_box;
|
2022-06-15 20:22:35 +00:00
|
|
|
|
2024-06-23 16:40:10 +00:00
|
|
|
display_list_recorder.fill_rect_with_rounded_corners(
|
2023-10-15 02:27:48 +00:00
|
|
|
context.rounded_device_rect(color_box.rect).to_type<int>(),
|
2024-08-02 13:58:26 +00:00
|
|
|
resolved_background.color,
|
2023-10-15 02:27:48 +00:00
|
|
|
color_box.radii.top_left.as_corner(context),
|
|
|
|
color_box.radii.top_right.as_corner(context),
|
|
|
|
color_box.radii.bottom_right.as_corner(context),
|
2024-08-06 12:26:47 +00:00
|
|
|
color_box.radii.bottom_left.as_corner(context));
|
2022-06-15 20:22:35 +00:00
|
|
|
|
2022-07-31 00:18:46 +00:00
|
|
|
struct {
|
2023-07-30 14:54:57 +00:00
|
|
|
DevicePixels top { 0 };
|
|
|
|
DevicePixels bottom { 0 };
|
|
|
|
DevicePixels left { 0 };
|
|
|
|
DevicePixels right { 0 };
|
2022-07-31 00:18:46 +00:00
|
|
|
} clip_shrink;
|
|
|
|
|
2024-10-17 20:04:12 +00:00
|
|
|
auto border_top = paintable_box.computed_values().border_top();
|
|
|
|
auto border_bottom = paintable_box.computed_values().border_bottom();
|
|
|
|
auto border_left = paintable_box.computed_values().border_left();
|
|
|
|
auto border_right = paintable_box.computed_values().border_right();
|
2022-07-31 00:18:46 +00:00
|
|
|
|
|
|
|
if (border_top.color.alpha() == 255 && border_bottom.color.alpha() == 255
|
|
|
|
&& border_left.color.alpha() == 255 && border_right.color.alpha() == 255) {
|
2023-07-30 14:54:57 +00:00
|
|
|
clip_shrink.top = context.rounded_device_pixels(border_top.width);
|
|
|
|
clip_shrink.bottom = context.rounded_device_pixels(border_bottom.width);
|
|
|
|
clip_shrink.left = context.rounded_device_pixels(border_left.width);
|
|
|
|
clip_shrink.right = context.rounded_device_pixels(border_right.width);
|
2022-07-31 00:18:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 12:44:22 +00:00
|
|
|
// Note: Background layers are ordered front-to-back, so we paint them in reverse
|
2024-08-02 13:58:26 +00:00
|
|
|
for (auto& layer : resolved_background.layers.in_reverse()) {
|
2024-06-23 16:40:10 +00:00
|
|
|
DisplayListRecorderStateSaver state { display_list_recorder };
|
2021-09-19 14:58:26 +00:00
|
|
|
|
2021-11-12 20:12:56 +00:00
|
|
|
// Clip
|
2024-10-17 20:04:12 +00:00
|
|
|
auto clip_box = get_box(layer.clip, border_box, paintable_box);
|
2022-07-31 00:11:59 +00:00
|
|
|
|
2022-10-27 13:48:52 +00:00
|
|
|
CSSPixelRect const& css_clip_rect = clip_box.rect;
|
2024-08-10 12:24:25 +00:00
|
|
|
auto clip_rect = context.rounded_device_rect(css_clip_rect);
|
2024-06-23 16:40:10 +00:00
|
|
|
display_list_recorder.add_clip_rect(clip_rect.to_type<int>());
|
2024-08-05 18:02:20 +00:00
|
|
|
ScopedCornerRadiusClip corner_clip { context, context.rounded_device_rect(css_clip_rect), clip_box.radii };
|
2021-11-12 20:12:56 +00:00
|
|
|
|
2022-07-31 00:18:46 +00:00
|
|
|
if (layer.clip == CSS::BackgroundBox::BorderBox) {
|
|
|
|
// Shrink the effective clip rect if to account for the bits the borders will definitely paint over
|
|
|
|
// (if they all have alpha == 255).
|
|
|
|
clip_rect.shrink(clip_shrink.top, clip_shrink.right, clip_shrink.bottom, clip_shrink.left);
|
|
|
|
}
|
|
|
|
|
2024-08-02 13:58:26 +00:00
|
|
|
auto const& image = *layer.background_image;
|
|
|
|
auto image_rect = layer.image_rect;
|
|
|
|
auto background_positioning_area = layer.background_positioning_area;
|
2021-11-12 20:12:56 +00:00
|
|
|
|
2022-03-25 17:39:52 +00:00
|
|
|
switch (layer.attachment) {
|
|
|
|
case CSS::BackgroundAttachment::Fixed:
|
2024-10-17 20:04:12 +00:00
|
|
|
background_positioning_area.set_location(paintable_box.layout_node().root().navigable()->viewport_scroll_offset());
|
2022-03-25 17:39:52 +00:00
|
|
|
break;
|
|
|
|
case CSS::BackgroundAttachment::Local:
|
2024-10-17 20:04:12 +00:00
|
|
|
if (!paintable_box.is_viewport()) {
|
|
|
|
auto scroll_offset = paintable_box.scroll_offset();
|
|
|
|
background_positioning_area.translate_by(-scroll_offset.x(), -scroll_offset.y());
|
2023-08-08 19:45:38 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-03-25 17:39:52 +00:00
|
|
|
case CSS::BackgroundAttachment::Scroll:
|
|
|
|
break;
|
|
|
|
}
|
2021-11-16 16:27:03 +00:00
|
|
|
|
2024-08-02 13:58:26 +00:00
|
|
|
if (background_positioning_area.is_empty())
|
2022-08-22 23:34:11 +00:00
|
|
|
continue;
|
|
|
|
|
2021-11-15 19:15:14 +00:00
|
|
|
if (layer.position_edge_x == CSS::PositionEdge::Right) {
|
2024-08-02 13:58:26 +00:00
|
|
|
image_rect.set_right_without_resize(background_positioning_area.right() - layer.offset_x);
|
2021-11-15 19:15:14 +00:00
|
|
|
} else {
|
2024-08-02 13:58:26 +00:00
|
|
|
image_rect.set_left(background_positioning_area.left() + layer.offset_x);
|
2021-11-15 19:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (layer.position_edge_y == CSS::PositionEdge::Bottom) {
|
2024-08-02 13:58:26 +00:00
|
|
|
image_rect.set_bottom_without_resize(background_positioning_area.bottom() - layer.offset_y);
|
2021-11-15 19:15:14 +00:00
|
|
|
} else {
|
2024-08-02 13:58:26 +00:00
|
|
|
image_rect.set_top(background_positioning_area.top() + layer.offset_y);
|
2021-11-15 19:15:14 +00:00
|
|
|
}
|
2021-11-12 20:12:56 +00:00
|
|
|
|
|
|
|
// Repetition
|
2021-11-15 17:05:06 +00:00
|
|
|
bool repeat_x = false;
|
|
|
|
bool repeat_y = false;
|
2024-07-23 12:36:28 +00:00
|
|
|
bool repeat_x_has_gap = false;
|
|
|
|
bool repeat_y_has_gap = false;
|
2022-10-27 13:48:52 +00:00
|
|
|
CSSPixels x_step = 0;
|
|
|
|
CSSPixels y_step = 0;
|
2021-11-15 17:05:06 +00:00
|
|
|
|
2021-11-12 12:44:22 +00:00
|
|
|
switch (layer.repeat_x) {
|
2021-09-19 14:58:26 +00:00
|
|
|
case CSS::Repeat::Round:
|
2021-11-17 12:52:46 +00:00
|
|
|
x_step = image_rect.width();
|
|
|
|
repeat_x = true;
|
2021-11-16 17:01:09 +00:00
|
|
|
break;
|
|
|
|
case CSS::Repeat::Space: {
|
2023-06-12 18:37:35 +00:00
|
|
|
int whole_images = (background_positioning_area.width() / image_rect.width()).to_int();
|
2021-11-16 17:01:09 +00:00
|
|
|
if (whole_images <= 1) {
|
|
|
|
x_step = image_rect.width();
|
|
|
|
repeat_x = false;
|
|
|
|
} else {
|
2023-06-16 13:13:26 +00:00
|
|
|
auto space = fmod(background_positioning_area.width().to_double(), image_rect.width().to_double());
|
2023-08-26 14:57:31 +00:00
|
|
|
x_step = image_rect.width() + CSSPixels::nearest_value_for(space / static_cast<double>(whole_images - 1));
|
2021-11-16 17:01:09 +00:00
|
|
|
repeat_x = true;
|
2024-07-23 12:36:28 +00:00
|
|
|
repeat_x_has_gap = true;
|
2021-11-16 17:01:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-09-19 14:58:26 +00:00
|
|
|
case CSS::Repeat::Repeat:
|
2021-11-15 17:05:06 +00:00
|
|
|
x_step = image_rect.width();
|
|
|
|
repeat_x = true;
|
2021-09-19 14:58:26 +00:00
|
|
|
break;
|
|
|
|
case CSS::Repeat::NoRepeat:
|
2021-11-15 17:05:06 +00:00
|
|
|
repeat_x = false;
|
2021-09-19 14:58:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-15 17:05:06 +00:00
|
|
|
// Move image_rect to the left-most tile position that is still visible
|
2022-10-27 13:48:52 +00:00
|
|
|
if (repeat_x && image_rect.x() > css_clip_rect.x()) {
|
|
|
|
auto x_delta = floor(x_step * ceil((image_rect.x() - css_clip_rect.x()) / x_step));
|
2021-11-15 17:05:06 +00:00
|
|
|
image_rect.set_x(image_rect.x() - x_delta);
|
|
|
|
}
|
2021-09-19 14:58:26 +00:00
|
|
|
|
2021-11-12 12:44:22 +00:00
|
|
|
switch (layer.repeat_y) {
|
2021-09-19 14:58:26 +00:00
|
|
|
case CSS::Repeat::Round:
|
2021-11-17 12:52:46 +00:00
|
|
|
y_step = image_rect.height();
|
|
|
|
repeat_y = true;
|
2021-11-16 17:01:09 +00:00
|
|
|
break;
|
|
|
|
case CSS::Repeat::Space: {
|
2023-06-12 18:37:35 +00:00
|
|
|
int whole_images = (background_positioning_area.height() / image_rect.height()).to_int();
|
2021-11-16 17:01:09 +00:00
|
|
|
if (whole_images <= 1) {
|
|
|
|
y_step = image_rect.height();
|
|
|
|
repeat_y = false;
|
|
|
|
} else {
|
2023-06-12 18:37:35 +00:00
|
|
|
auto space = fmod(background_positioning_area.height().to_float(), image_rect.height().to_float());
|
2023-08-26 14:57:31 +00:00
|
|
|
y_step = image_rect.height() + CSSPixels::nearest_value_for(static_cast<double>(space) / static_cast<double>(whole_images - 1));
|
2021-11-16 17:01:09 +00:00
|
|
|
repeat_y = true;
|
2024-07-23 12:36:28 +00:00
|
|
|
repeat_y_has_gap = true;
|
2021-11-16 17:01:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-09-19 14:58:26 +00:00
|
|
|
case CSS::Repeat::Repeat:
|
2021-11-15 17:05:06 +00:00
|
|
|
y_step = image_rect.height();
|
|
|
|
repeat_y = true;
|
2021-09-19 14:58:26 +00:00
|
|
|
break;
|
|
|
|
case CSS::Repeat::NoRepeat:
|
2021-11-15 17:05:06 +00:00
|
|
|
repeat_y = false;
|
2021-09-19 14:58:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-15 17:05:06 +00:00
|
|
|
// Move image_rect to the top-most tile position that is still visible
|
2022-10-27 13:48:52 +00:00
|
|
|
if (repeat_y && image_rect.y() > css_clip_rect.y()) {
|
|
|
|
auto y_delta = floor(y_step * ceil((image_rect.y() - css_clip_rect.y()) / y_step));
|
2021-11-15 17:05:06 +00:00
|
|
|
image_rect.set_y(image_rect.y() - y_delta);
|
|
|
|
}
|
2021-09-19 14:58:26 +00:00
|
|
|
|
2022-10-27 13:48:52 +00:00
|
|
|
CSSPixels initial_image_x = image_rect.x();
|
|
|
|
CSSPixels image_y = image_rect.y();
|
2022-06-16 23:24:30 +00:00
|
|
|
|
2024-10-17 20:04:12 +00:00
|
|
|
image.resolve_for_size(paintable_box.layout_node_with_style_and_box_metrics(), image_rect.size());
|
2022-07-31 00:11:59 +00:00
|
|
|
|
2024-01-01 12:48:53 +00:00
|
|
|
auto for_each_image_device_rect = [&](auto callback) {
|
|
|
|
while (image_y < css_clip_rect.bottom()) {
|
|
|
|
image_rect.set_y(image_y);
|
|
|
|
|
|
|
|
auto image_x = initial_image_x;
|
|
|
|
while (image_x < css_clip_rect.right()) {
|
|
|
|
image_rect.set_x(image_x);
|
|
|
|
auto image_device_rect = context.rounded_device_rect(image_rect);
|
|
|
|
callback(image_device_rect);
|
|
|
|
if (!repeat_x)
|
|
|
|
break;
|
|
|
|
image_x += x_step;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!repeat_y)
|
2021-11-15 17:05:06 +00:00
|
|
|
break;
|
2024-01-01 12:48:53 +00:00
|
|
|
image_y += y_step;
|
2021-11-15 17:05:06 +00:00
|
|
|
}
|
2024-01-01 12:48:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (auto color = image.color_if_single_pixel_bitmap(); color.has_value()) {
|
|
|
|
// OPTIMIZATION: If the image is a single pixel, we can just fill the whole area with it.
|
|
|
|
// However, we must first figure out the real coverage area, taking repeat etc into account.
|
|
|
|
|
|
|
|
// FIXME: This could be written in a far more efficient way.
|
|
|
|
auto fill_rect = Optional<DevicePixelRect> {};
|
|
|
|
for_each_image_device_rect([&](auto const& image_device_rect) {
|
|
|
|
if (!fill_rect.has_value()) {
|
|
|
|
fill_rect = image_device_rect;
|
|
|
|
} else {
|
|
|
|
fill_rect = fill_rect->united(image_device_rect);
|
|
|
|
}
|
|
|
|
});
|
2024-08-06 12:26:47 +00:00
|
|
|
display_list_recorder.fill_rect(fill_rect->to_type<int>(), color.value());
|
2024-07-23 12:36:28 +00:00
|
|
|
} else if (is<CSS::ImageStyleValue>(image) && repeat_x && repeat_y && !repeat_x_has_gap && !repeat_y_has_gap) {
|
|
|
|
// Use a dedicated painting command for repeated images instead of recording a separate command for each instance
|
|
|
|
// of a repeated background, so the painter has the opportunity to optimize the painting of repeated images.
|
|
|
|
auto dest_rect = context.rounded_device_rect(image_rect);
|
|
|
|
auto const* bitmap = static_cast<CSS::ImageStyleValue const&>(image).current_frame_bitmap(dest_rect);
|
|
|
|
auto scaling_mode = to_gfx_scaling_mode(image_rendering, bitmap->rect(), dest_rect.to_type<int>());
|
2024-08-06 12:26:47 +00:00
|
|
|
context.display_list_recorder().draw_repeated_immutable_bitmap(dest_rect.to_type<int>(), clip_rect.to_type<int>(), *bitmap, scaling_mode, { .x = repeat_x, .y = repeat_y });
|
2024-01-01 12:48:53 +00:00
|
|
|
} else {
|
|
|
|
for_each_image_device_rect([&](auto const& image_device_rect) {
|
2024-08-06 12:26:47 +00:00
|
|
|
image.paint(context, image_device_rect, image_rendering);
|
2024-01-01 12:48:53 +00:00
|
|
|
});
|
2021-11-15 17:05:06 +00:00
|
|
|
}
|
2021-09-19 14:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 20:20:00 +00:00
|
|
|
ResolvedBackground resolve_background_layers(Vector<CSS::BackgroundLayerData> const& layers, PaintableBox const& paintable_box, Color background_color, CSSPixelRect const& border_rect, BorderRadiiData const& border_radii)
|
2024-08-02 13:58:26 +00:00
|
|
|
{
|
|
|
|
auto layer_is_paintable = [&](auto& layer) {
|
|
|
|
return layer.background_image && layer.background_image->is_paintable();
|
|
|
|
};
|
|
|
|
|
|
|
|
BackgroundBox border_box {
|
|
|
|
border_rect,
|
|
|
|
border_radii
|
|
|
|
};
|
|
|
|
|
|
|
|
auto color_box = border_box;
|
|
|
|
if (!layers.is_empty())
|
2024-10-17 20:20:00 +00:00
|
|
|
color_box = get_box(layers.last().clip, border_box, paintable_box);
|
2024-08-02 13:58:26 +00:00
|
|
|
|
|
|
|
Vector<ResolvedBackgroundLayerData> resolved_layers;
|
|
|
|
for (auto const& layer : layers) {
|
|
|
|
if (!layer_is_paintable(layer))
|
|
|
|
continue;
|
|
|
|
|
2024-10-17 20:20:00 +00:00
|
|
|
auto background_positioning_area = get_box(layer.origin, border_box, paintable_box).rect;
|
2024-08-02 13:58:26 +00:00
|
|
|
auto const& image = *layer.background_image;
|
|
|
|
|
|
|
|
Optional<CSSPixels> specified_width {};
|
|
|
|
Optional<CSSPixels> specified_height {};
|
|
|
|
if (layer.size_type == CSS::BackgroundSize::LengthPercentage) {
|
|
|
|
if (!layer.size_x.is_auto())
|
2024-10-17 20:20:00 +00:00
|
|
|
specified_width = layer.size_x.to_px(paintable_box.layout_node(), background_positioning_area.width());
|
2024-08-02 13:58:26 +00:00
|
|
|
if (!layer.size_y.is_auto())
|
2024-10-17 20:20:00 +00:00
|
|
|
specified_height = layer.size_y.to_px(paintable_box.layout_node(), background_positioning_area.height());
|
2024-08-02 13:58:26 +00:00
|
|
|
}
|
2024-09-09 16:16:25 +00:00
|
|
|
auto concrete_image_size = CSS::run_default_sizing_algorithm(
|
2024-08-02 13:58:26 +00:00
|
|
|
specified_width, specified_height,
|
|
|
|
image.natural_width(), image.natural_height(), image.natural_aspect_ratio(),
|
|
|
|
background_positioning_area.size());
|
|
|
|
|
|
|
|
// If any of these are zero, the NaNs will pop up in the painting code.
|
|
|
|
if (background_positioning_area.is_empty() || concrete_image_size.is_empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size
|
|
|
|
CSSPixelRect image_rect;
|
|
|
|
switch (layer.size_type) {
|
|
|
|
case CSS::BackgroundSize::Contain: {
|
|
|
|
double max_width_ratio = background_positioning_area.width().to_double() / concrete_image_size.width().to_double();
|
|
|
|
double max_height_ratio = background_positioning_area.height().to_double() / concrete_image_size.height().to_double();
|
|
|
|
double ratio = min(max_width_ratio, max_height_ratio);
|
|
|
|
image_rect.set_size(concrete_image_size.width().scaled(ratio), concrete_image_size.height().scaled(ratio));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CSS::BackgroundSize::Cover: {
|
|
|
|
double max_width_ratio = background_positioning_area.width().to_double() / concrete_image_size.width().to_double();
|
|
|
|
double max_height_ratio = background_positioning_area.height().to_double() / concrete_image_size.height().to_double();
|
|
|
|
double ratio = max(max_width_ratio, max_height_ratio);
|
|
|
|
image_rect.set_size(concrete_image_size.width().scaled(ratio), concrete_image_size.height().scaled(ratio));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CSS::BackgroundSize::LengthPercentage:
|
|
|
|
image_rect.set_size(concrete_image_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If after sizing we have a 0px image, we're done. Attempting to paint this would be an infinite loop.
|
|
|
|
if (image_rect.is_empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If background-repeat is round for one (or both) dimensions, there is a second step.
|
|
|
|
// The UA must scale the image in that dimension (or both dimensions) so that it fits a
|
|
|
|
// whole number of times in the background positioning area.
|
|
|
|
if (layer.repeat_x == CSS::Repeat::Round || layer.repeat_y == CSS::Repeat::Round) {
|
|
|
|
// If X ≠ 0 is the width of the image after step one and W is the width of the
|
|
|
|
// background positioning area, then the rounded width X' = W / round(W / X)
|
|
|
|
// where round() is a function that returns the nearest natural number
|
|
|
|
// (integer greater than zero).
|
|
|
|
if (layer.repeat_x == CSS::Repeat::Round) {
|
|
|
|
image_rect.set_width(background_positioning_area.width() / round(background_positioning_area.width() / image_rect.width()));
|
|
|
|
}
|
|
|
|
if (layer.repeat_y == CSS::Repeat::Round) {
|
|
|
|
image_rect.set_height(background_positioning_area.height() / round(background_positioning_area.height() / image_rect.height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If background-repeat is round for one dimension only and if background-size is auto
|
|
|
|
// for the other dimension, then there is a third step: that other dimension is scaled
|
|
|
|
// so that the original aspect ratio is restored.
|
|
|
|
if (layer.repeat_x != layer.repeat_y) {
|
|
|
|
if (layer.size_x.is_auto()) {
|
|
|
|
image_rect.set_width(image_rect.height() * (concrete_image_size.width() / concrete_image_size.height()));
|
|
|
|
}
|
|
|
|
if (layer.size_y.is_auto()) {
|
|
|
|
image_rect.set_height(image_rect.width() * (concrete_image_size.height() / concrete_image_size.width()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSPixels space_x = background_positioning_area.width() - image_rect.width();
|
|
|
|
CSSPixels space_y = background_positioning_area.height() - image_rect.height();
|
|
|
|
|
2024-10-17 20:20:00 +00:00
|
|
|
CSSPixels offset_x = layer.position_offset_x.to_px(paintable_box.layout_node(), space_x);
|
|
|
|
CSSPixels offset_y = layer.position_offset_y.to_px(paintable_box.layout_node(), space_y);
|
2024-08-02 13:58:26 +00:00
|
|
|
|
|
|
|
resolved_layers.append({ .background_image = layer.background_image,
|
|
|
|
.attachment = layer.attachment,
|
|
|
|
.clip = layer.clip,
|
|
|
|
.position_edge_x = layer.position_edge_x,
|
|
|
|
.position_edge_y = layer.position_edge_y,
|
|
|
|
.offset_x = offset_x,
|
|
|
|
.offset_y = offset_y,
|
|
|
|
.background_positioning_area = background_positioning_area,
|
|
|
|
.image_rect = image_rect,
|
|
|
|
.repeat_x = layer.repeat_x,
|
|
|
|
.repeat_y = layer.repeat_y });
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResolvedBackground {
|
|
|
|
.color_box = color_box,
|
|
|
|
.layers = move(resolved_layers),
|
|
|
|
.needs_text_clip = !layers.is_empty() && layers.last().clip == CSS::BackgroundBox::Text,
|
|
|
|
.background_rect = border_rect,
|
|
|
|
.color = background_color
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-19 14:58:26 +00:00
|
|
|
}
|