2021-09-19 15:00:47 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-08 14:48:37 +00:00
|
|
|
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
2022-06-20 12:15:04 +01:00
|
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
2021-09-19 15:00:47 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-25 15:44:13 +01:00
|
|
|
#include <AK/NumericLimits.h>
|
2021-09-19 15:00:47 +01:00
|
|
|
#include <LibGfx/DisjointRectSet.h>
|
2022-06-28 11:09:08 +01:00
|
|
|
#include <LibGfx/Filters/StackBlurFilter.h>
|
2023-10-15 04:27:48 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2021-09-19 15:00:47 +01:00
|
|
|
#include <LibGfx/Painter.h>
|
2022-03-24 15:20:25 +00:00
|
|
|
#include <LibWeb/Layout/LineBoxFragment.h>
|
2023-04-01 21:13:18 +01:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2022-06-20 12:09:11 +01:00
|
|
|
#include <LibWeb/Painting/BorderPainting.h>
|
2022-06-20 12:15:04 +01:00
|
|
|
#include <LibWeb/Painting/BorderRadiusCornerClipper.h>
|
2021-09-19 15:00:47 +01:00
|
|
|
#include <LibWeb/Painting/PaintContext.h>
|
2023-10-15 04:27:48 +02:00
|
|
|
#include <LibWeb/Painting/PaintOuterBoxShadowParams.h>
|
2024-01-12 21:25:05 +01:00
|
|
|
#include <LibWeb/Painting/PaintableBox.h>
|
2021-09-19 15:00:47 +01:00
|
|
|
#include <LibWeb/Painting/ShadowPainting.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::Painting {
|
|
|
|
|
|
2023-10-15 04:27:48 +02:00
|
|
|
void paint_inner_box_shadow(Gfx::Painter& painter, PaintOuterBoxShadowParams params)
|
2021-09-19 15:00:47 +01:00
|
|
|
{
|
2023-10-15 04:27:48 +02:00
|
|
|
auto device_content_rect = params.device_content_rect;
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
int offset_x = params.offset_x;
|
|
|
|
|
int offset_y = params.offset_y;
|
|
|
|
|
int blur_radius = params.blur_radius;
|
|
|
|
|
int spread_distance = params.spread_distance;
|
2023-06-30 01:12:52 +00:00
|
|
|
auto shadows_bitmap_rect = device_content_rect.inflated(
|
2024-06-07 16:02:50 +03:00
|
|
|
blur_radius + offset_y,
|
|
|
|
|
blur_radius + abs(offset_x),
|
|
|
|
|
blur_radius + abs(offset_y),
|
|
|
|
|
blur_radius + offset_x);
|
|
|
|
|
auto shadows_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadows_bitmap_rect.size());
|
2023-06-05 02:42:32 +00:00
|
|
|
if (shadows_bitmap.is_error()) {
|
|
|
|
|
dbgln("Unable to allocate temporary bitmap {} for box-shadow rendering: {}", device_content_rect, shadows_bitmap.error());
|
2021-09-19 15:00:47 +01:00
|
|
|
return;
|
2023-06-05 02:42:32 +00:00
|
|
|
}
|
|
|
|
|
auto shadow_bitmap = shadows_bitmap.release_value();
|
|
|
|
|
Gfx::Painter shadow_painter { *shadow_bitmap };
|
|
|
|
|
Gfx::AntiAliasingPainter shadow_aa_painter { shadow_painter };
|
2024-06-07 16:02:50 +03:00
|
|
|
auto device_content_rect_int = device_content_rect;
|
2023-06-30 01:12:52 +00:00
|
|
|
auto origin_device_content_rect = device_content_rect_int.translated(-device_content_rect_int.x(), -device_content_rect_int.y());
|
2024-06-07 16:02:50 +03:00
|
|
|
auto outer_shadow_rect = origin_device_content_rect.translated({ offset_x + blur_radius, offset_y + blur_radius });
|
|
|
|
|
auto spread_distance_value = spread_distance;
|
2023-06-30 01:12:52 +00:00
|
|
|
auto inner_shadow_rect = outer_shadow_rect.inflated(-spread_distance_value, -spread_distance_value, -spread_distance_value, -spread_distance_value);
|
|
|
|
|
outer_shadow_rect.inflate(
|
2024-06-07 16:02:50 +03:00
|
|
|
blur_radius + offset_y,
|
|
|
|
|
blur_radius + abs(offset_x),
|
|
|
|
|
blur_radius + abs(offset_y),
|
|
|
|
|
blur_radius + offset_x);
|
2023-10-15 04:27:48 +02:00
|
|
|
auto top_left_corner = params.corner_radii.top_left;
|
|
|
|
|
auto top_right_corner = params.corner_radii.top_right;
|
|
|
|
|
auto bottom_right_corner = params.corner_radii.bottom_right;
|
|
|
|
|
auto bottom_left_corner = params.corner_radii.bottom_left;
|
2024-06-07 15:42:35 +03:00
|
|
|
shadow_painter.fill_rect(outer_shadow_rect, params.color.with_alpha(0xff));
|
|
|
|
|
if (params.corner_radii.has_any_radius()) {
|
|
|
|
|
shadow_aa_painter.fill_rect_with_rounded_corners(inner_shadow_rect, params.color.with_alpha(0xff),
|
2023-06-05 02:42:32 +00:00
|
|
|
top_left_corner, top_right_corner, bottom_right_corner, bottom_left_corner,
|
|
|
|
|
Gfx::AntiAliasingPainter::BlendMode::AlphaSubtract);
|
|
|
|
|
} else {
|
|
|
|
|
shadow_painter.clear_rect(inner_shadow_rect, Color::Transparent);
|
|
|
|
|
}
|
|
|
|
|
Gfx::StackBlurFilter filter(*shadow_bitmap);
|
2024-06-07 16:02:50 +03:00
|
|
|
filter.process_rgba(blur_radius, params.color);
|
2023-06-05 02:42:32 +00:00
|
|
|
Gfx::PainterStateSaver save { painter };
|
|
|
|
|
painter.add_clip_rect(device_content_rect_int);
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.blit({ device_content_rect_int.left() - blur_radius, device_content_rect_int.top() - blur_radius },
|
2024-06-07 15:42:35 +03:00
|
|
|
*shadow_bitmap, shadow_bitmap->rect(), params.color.alpha() / 255.);
|
2023-06-05 02:42:32 +00:00
|
|
|
}
|
2021-09-19 15:00:47 +01:00
|
|
|
|
2023-10-19 01:16:01 +02:00
|
|
|
struct OuterBoxShadowMetrics {
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect shadow_bitmap_rect;
|
|
|
|
|
Gfx::IntRect non_blurred_shadow_rect;
|
|
|
|
|
Gfx::IntRect inner_bounding_rect;
|
|
|
|
|
int blurred_edge_thickness;
|
|
|
|
|
int double_radius;
|
|
|
|
|
int blur_radius;
|
|
|
|
|
|
|
|
|
|
Gfx::IntRect top_left_corner_rect;
|
|
|
|
|
Gfx::IntRect top_right_corner_rect;
|
|
|
|
|
Gfx::IntRect bottom_right_corner_rect;
|
|
|
|
|
Gfx::IntRect bottom_left_corner_rect;
|
|
|
|
|
|
|
|
|
|
Gfx::IntPoint top_left_corner_blit_pos;
|
|
|
|
|
Gfx::IntPoint top_right_corner_blit_pos;
|
|
|
|
|
Gfx::IntPoint bottom_right_corner_blit_pos;
|
|
|
|
|
Gfx::IntPoint bottom_left_corner_blit_pos;
|
|
|
|
|
|
|
|
|
|
Gfx::IntSize top_left_corner_size;
|
|
|
|
|
Gfx::IntSize top_right_corner_size;
|
|
|
|
|
Gfx::IntSize bottom_right_corner_size;
|
|
|
|
|
Gfx::IntSize bottom_left_corner_size;
|
|
|
|
|
|
|
|
|
|
int left_start;
|
|
|
|
|
int top_start;
|
|
|
|
|
int right_start;
|
|
|
|
|
int bottom_start;
|
|
|
|
|
|
|
|
|
|
Gfx::IntRect left_edge_rect;
|
|
|
|
|
Gfx::IntRect right_edge_rect;
|
|
|
|
|
Gfx::IntRect top_edge_rect;
|
|
|
|
|
Gfx::IntRect bottom_edge_rect;
|
2023-10-19 01:16:01 +02:00
|
|
|
|
|
|
|
|
CornerRadius top_left_shadow_corner;
|
|
|
|
|
CornerRadius top_right_shadow_corner;
|
|
|
|
|
CornerRadius bottom_right_shadow_corner;
|
|
|
|
|
CornerRadius bottom_left_shadow_corner;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static OuterBoxShadowMetrics get_outer_box_shadow_configuration(PaintOuterBoxShadowParams params)
|
2023-06-05 02:42:32 +00:00
|
|
|
{
|
2023-10-15 04:27:48 +02:00
|
|
|
auto device_content_rect = params.device_content_rect;
|
2022-06-20 12:15:04 +01:00
|
|
|
|
2023-10-15 04:27:48 +02:00
|
|
|
auto top_left_corner = params.corner_radii.top_left;
|
|
|
|
|
auto top_right_corner = params.corner_radii.top_right;
|
|
|
|
|
auto bottom_right_corner = params.corner_radii.bottom_right;
|
|
|
|
|
auto bottom_left_corner = params.corner_radii.bottom_left;
|
2022-06-20 12:15:04 +01:00
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto offset_x = params.offset_x;
|
|
|
|
|
auto offset_y = params.offset_y;
|
|
|
|
|
auto blur_radius = params.blur_radius;
|
|
|
|
|
auto spread_distance = params.spread_distance;
|
2023-06-05 02:42:32 +00:00
|
|
|
|
|
|
|
|
// Our blur cannot handle radii over 255 so there's no point trying (255 is silly big anyway)
|
|
|
|
|
blur_radius = clamp(blur_radius, 0, 255);
|
|
|
|
|
|
|
|
|
|
auto top_left_shadow_corner = top_left_corner;
|
|
|
|
|
auto top_right_shadow_corner = top_right_corner;
|
|
|
|
|
auto bottom_right_shadow_corner = bottom_right_corner;
|
|
|
|
|
auto bottom_left_shadow_corner = bottom_left_corner;
|
2022-06-29 00:34:25 +01:00
|
|
|
|
2023-06-05 02:42:32 +00:00
|
|
|
auto spread_corner = [&](auto& corner) {
|
|
|
|
|
if (corner) {
|
2024-06-07 16:02:50 +03:00
|
|
|
corner.horizontal_radius += spread_distance;
|
|
|
|
|
corner.vertical_radius += spread_distance;
|
2022-02-09 17:18:41 +00:00
|
|
|
}
|
2023-06-05 02:42:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
spread_corner(top_left_shadow_corner);
|
|
|
|
|
spread_corner(top_right_shadow_corner);
|
|
|
|
|
spread_corner(bottom_right_shadow_corner);
|
|
|
|
|
spread_corner(bottom_left_shadow_corner);
|
|
|
|
|
|
|
|
|
|
auto expansion = spread_distance - (blur_radius * 2);
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect inner_bounding_rect = {
|
2023-06-05 02:42:32 +00:00
|
|
|
device_content_rect.x() + offset_x - expansion,
|
|
|
|
|
device_content_rect.y() + offset_y - expansion,
|
|
|
|
|
device_content_rect.width() + 2 * expansion,
|
|
|
|
|
device_content_rect.height() + 2 * expansion
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Calculating and blurring the box-shadow full size is expensive, and wasteful - aside from the corners,
|
|
|
|
|
// all vertical strips of the shadow are identical, and the same goes for horizontal ones.
|
|
|
|
|
// So instead, we generate a shadow bitmap that is just large enough to include the corners and 1px of
|
|
|
|
|
// non-corner, and then we repeatedly blit sections of it. This is similar to a NinePatch on Android.
|
|
|
|
|
auto double_radius = blur_radius * 2;
|
|
|
|
|
auto blurred_edge_thickness = blur_radius * 4;
|
|
|
|
|
|
|
|
|
|
auto default_corner_size = Gfx::IntSize { double_radius, double_radius };
|
2024-06-07 16:02:50 +03:00
|
|
|
auto top_left_corner_size = top_left_shadow_corner ? top_left_shadow_corner.as_rect().size() : default_corner_size;
|
|
|
|
|
auto top_right_corner_size = top_right_shadow_corner ? top_right_shadow_corner.as_rect().size() : default_corner_size;
|
|
|
|
|
auto bottom_left_corner_size = bottom_left_shadow_corner ? bottom_left_shadow_corner.as_rect().size() : default_corner_size;
|
|
|
|
|
auto bottom_right_corner_size = bottom_right_shadow_corner ? bottom_right_shadow_corner.as_rect().size() : default_corner_size;
|
2023-06-05 02:42:32 +00:00
|
|
|
|
2023-10-19 01:16:01 +02:00
|
|
|
auto non_blurred_shadow_rect = device_content_rect.inflated(spread_distance, spread_distance, spread_distance, spread_distance);
|
|
|
|
|
|
2023-06-05 02:42:32 +00:00
|
|
|
auto max_edge_width = non_blurred_shadow_rect.width() / 2;
|
|
|
|
|
auto max_edge_height = non_blurred_shadow_rect.height() / 2;
|
|
|
|
|
auto extra_edge_width = non_blurred_shadow_rect.width() % 2;
|
|
|
|
|
auto extra_edge_height = non_blurred_shadow_rect.height() % 2;
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto clip_corner_size = [&](auto& size, auto const& corner, int x_bonus = 0, int y_bonus = 0) {
|
|
|
|
|
auto max_x = max_edge_width + x_bonus;
|
|
|
|
|
auto max_y = max_edge_height + y_bonus;
|
|
|
|
|
auto min_x = max(corner.horizontal_radius, min(double_radius, max_x));
|
|
|
|
|
auto min_y = max(corner.vertical_radius, min(double_radius, max_y));
|
2023-06-05 02:42:32 +00:00
|
|
|
if (min_x <= max_x)
|
|
|
|
|
size.set_width(clamp(size.width(), min_x, max_x));
|
|
|
|
|
if (min_y <= max_y)
|
|
|
|
|
size.set_height(clamp(size.height(), min_y, max_y));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clip_corner_size(top_left_corner_size, top_left_corner, extra_edge_width, extra_edge_height);
|
|
|
|
|
clip_corner_size(top_right_corner_size, top_right_corner, 0, extra_edge_height);
|
|
|
|
|
clip_corner_size(bottom_left_corner_size, bottom_left_corner, extra_edge_width);
|
|
|
|
|
clip_corner_size(bottom_right_corner_size, bottom_right_corner);
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto shadow_bitmap_rect = Gfx::IntRect {
|
2023-06-05 02:42:32 +00:00
|
|
|
0, 0,
|
2023-07-27 04:36:45 +02:00
|
|
|
max(max(
|
|
|
|
|
top_left_corner_size.width() + top_right_corner_size.width(),
|
|
|
|
|
bottom_left_corner_size.width() + bottom_right_corner_size.width()),
|
|
|
|
|
max(top_left_corner_size.width() + bottom_right_corner_size.width(),
|
|
|
|
|
bottom_left_corner_size.width() + top_right_corner_size.width()))
|
2023-06-05 02:42:32 +00:00
|
|
|
+ 1 + blurred_edge_thickness,
|
2023-07-27 04:36:45 +02:00
|
|
|
max(max(
|
|
|
|
|
top_left_corner_size.height() + bottom_left_corner_size.height(),
|
|
|
|
|
top_right_corner_size.height() + bottom_right_corner_size.height()),
|
|
|
|
|
max(top_left_corner_size.height() + bottom_right_corner_size.height(),
|
|
|
|
|
bottom_left_corner_size.height() + top_right_corner_size.height()))
|
2024-06-07 16:02:50 +03:00
|
|
|
+ 1 + blurred_edge_thickness
|
|
|
|
|
};
|
2023-06-05 02:42:32 +00:00
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto top_left_corner_rect = Gfx::IntRect {
|
2023-06-05 02:42:32 +00:00
|
|
|
0, 0,
|
|
|
|
|
top_left_corner_size.width() + double_radius,
|
|
|
|
|
top_left_corner_size.height() + double_radius
|
|
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
auto top_right_corner_rect = Gfx::IntRect {
|
2023-06-05 02:42:32 +00:00
|
|
|
shadow_bitmap_rect.width() - (top_right_corner_size.width() + double_radius), 0,
|
|
|
|
|
top_right_corner_size.width() + double_radius,
|
|
|
|
|
top_right_corner_size.height() + double_radius
|
|
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
auto bottom_right_corner_rect = Gfx::IntRect {
|
2023-06-05 02:42:32 +00:00
|
|
|
shadow_bitmap_rect.width() - (bottom_right_corner_size.width() + double_radius),
|
|
|
|
|
shadow_bitmap_rect.height() - (bottom_right_corner_size.height() + double_radius),
|
|
|
|
|
bottom_right_corner_size.width() + double_radius,
|
|
|
|
|
bottom_right_corner_size.height() + double_radius
|
|
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
auto bottom_left_corner_rect = Gfx::IntRect {
|
2023-06-05 02:42:32 +00:00
|
|
|
0, shadow_bitmap_rect.height() - (bottom_left_corner_size.height() + double_radius),
|
|
|
|
|
bottom_left_corner_size.width() + double_radius,
|
|
|
|
|
bottom_left_corner_size.height() + double_radius
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto horizontal_edge_width = min(max_edge_height, double_radius) + double_radius;
|
|
|
|
|
auto vertical_edge_width = min(max_edge_width, double_radius) + double_radius;
|
|
|
|
|
auto horizontal_top_edge_width = min(max_edge_height + extra_edge_height, double_radius) + double_radius;
|
|
|
|
|
auto vertical_left_edge_width = min(max_edge_width + extra_edge_width, double_radius) + double_radius;
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect left_edge_rect { 0, top_left_corner_rect.height(), vertical_left_edge_width, 1 };
|
|
|
|
|
Gfx::IntRect right_edge_rect { shadow_bitmap_rect.width() - vertical_edge_width, top_right_corner_rect.height(), vertical_edge_width, 1 };
|
|
|
|
|
Gfx::IntRect top_edge_rect { top_left_corner_rect.width(), 0, 1, horizontal_top_edge_width };
|
|
|
|
|
Gfx::IntRect bottom_edge_rect { bottom_left_corner_rect.width(), shadow_bitmap_rect.height() - horizontal_edge_width, 1, horizontal_edge_width };
|
2023-06-05 02:42:32 +00:00
|
|
|
|
2023-10-19 01:16:01 +02:00
|
|
|
auto left_start = inner_bounding_rect.left() - blurred_edge_thickness;
|
|
|
|
|
auto right_start = inner_bounding_rect.left() + inner_bounding_rect.width() + (blurred_edge_thickness - vertical_edge_width);
|
|
|
|
|
auto top_start = inner_bounding_rect.top() - blurred_edge_thickness;
|
|
|
|
|
auto bottom_start = inner_bounding_rect.top() + inner_bounding_rect.height() + (blurred_edge_thickness - horizontal_edge_width);
|
|
|
|
|
|
|
|
|
|
auto top_left_corner_blit_pos = inner_bounding_rect.top_left().translated(-blurred_edge_thickness, -blurred_edge_thickness);
|
|
|
|
|
auto top_right_corner_blit_pos = inner_bounding_rect.top_right().translated(-top_right_corner_size.width() + double_radius, -blurred_edge_thickness);
|
|
|
|
|
auto bottom_left_corner_blit_pos = inner_bounding_rect.bottom_left().translated(-blurred_edge_thickness, -bottom_left_corner_size.height() + double_radius);
|
|
|
|
|
auto bottom_right_corner_blit_pos = inner_bounding_rect.bottom_right().translated(-bottom_right_corner_size.width() + double_radius, -bottom_right_corner_size.height() + double_radius);
|
|
|
|
|
|
|
|
|
|
return OuterBoxShadowMetrics {
|
|
|
|
|
.shadow_bitmap_rect = shadow_bitmap_rect,
|
|
|
|
|
.non_blurred_shadow_rect = non_blurred_shadow_rect,
|
|
|
|
|
.inner_bounding_rect = inner_bounding_rect,
|
|
|
|
|
.blurred_edge_thickness = blurred_edge_thickness,
|
|
|
|
|
.double_radius = double_radius,
|
|
|
|
|
.blur_radius = blur_radius,
|
|
|
|
|
|
|
|
|
|
.top_left_corner_rect = top_left_corner_rect,
|
|
|
|
|
.top_right_corner_rect = top_right_corner_rect,
|
|
|
|
|
.bottom_right_corner_rect = bottom_right_corner_rect,
|
|
|
|
|
.bottom_left_corner_rect = bottom_left_corner_rect,
|
|
|
|
|
|
|
|
|
|
.top_left_corner_blit_pos = top_left_corner_blit_pos,
|
|
|
|
|
.top_right_corner_blit_pos = top_right_corner_blit_pos,
|
|
|
|
|
.bottom_right_corner_blit_pos = bottom_right_corner_blit_pos,
|
|
|
|
|
.bottom_left_corner_blit_pos = bottom_left_corner_blit_pos,
|
|
|
|
|
|
|
|
|
|
.top_left_corner_size = top_left_corner_size,
|
|
|
|
|
.top_right_corner_size = top_right_corner_size,
|
|
|
|
|
.bottom_right_corner_size = bottom_right_corner_size,
|
|
|
|
|
.bottom_left_corner_size = bottom_left_corner_size,
|
|
|
|
|
|
|
|
|
|
.left_start = left_start,
|
|
|
|
|
.top_start = top_start,
|
|
|
|
|
.right_start = right_start,
|
|
|
|
|
.bottom_start = bottom_start,
|
|
|
|
|
|
|
|
|
|
.left_edge_rect = left_edge_rect,
|
|
|
|
|
.right_edge_rect = right_edge_rect,
|
|
|
|
|
.top_edge_rect = top_edge_rect,
|
|
|
|
|
.bottom_edge_rect = bottom_edge_rect,
|
|
|
|
|
|
|
|
|
|
.top_left_shadow_corner = top_left_shadow_corner,
|
|
|
|
|
.top_right_shadow_corner = top_right_shadow_corner,
|
|
|
|
|
.bottom_right_shadow_corner = bottom_right_shadow_corner,
|
|
|
|
|
.bottom_left_shadow_corner = bottom_left_shadow_corner,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::IntRect get_outer_box_shadow_bounding_rect(PaintOuterBoxShadowParams params)
|
|
|
|
|
{
|
|
|
|
|
auto shadow_config = get_outer_box_shadow_configuration(params);
|
|
|
|
|
|
|
|
|
|
auto const& top_left_corner_blit_pos = shadow_config.top_left_corner_blit_pos;
|
|
|
|
|
auto const& top_right_corner_blit_pos = shadow_config.top_right_corner_blit_pos;
|
|
|
|
|
auto const& bottom_left_corner_blit_pos = shadow_config.bottom_left_corner_blit_pos;
|
|
|
|
|
auto const& top_right_corner_rect = shadow_config.top_right_corner_rect;
|
|
|
|
|
auto const& bottom_left_corner_rect = shadow_config.bottom_left_corner_rect;
|
|
|
|
|
|
|
|
|
|
return Gfx::IntRect {
|
|
|
|
|
top_left_corner_blit_pos,
|
|
|
|
|
{ top_right_corner_blit_pos.x() - top_left_corner_blit_pos.x() + top_right_corner_rect.width(),
|
|
|
|
|
bottom_left_corner_blit_pos.y() - top_left_corner_blit_pos.y() + bottom_left_corner_rect.height() }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void paint_outer_box_shadow(Gfx::Painter& painter, PaintOuterBoxShadowParams params)
|
|
|
|
|
{
|
|
|
|
|
auto const& device_content_rect = params.device_content_rect;
|
|
|
|
|
|
|
|
|
|
auto const& top_left_corner = params.corner_radii.top_left;
|
|
|
|
|
auto const& top_right_corner = params.corner_radii.top_right;
|
|
|
|
|
auto const& bottom_right_corner = params.corner_radii.bottom_right;
|
|
|
|
|
auto const& bottom_left_corner = params.corner_radii.bottom_left;
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto offset_x = params.offset_x;
|
|
|
|
|
auto offset_y = params.offset_y;
|
2023-10-19 01:16:01 +02:00
|
|
|
|
|
|
|
|
auto shadow_config = get_outer_box_shadow_configuration(params);
|
|
|
|
|
|
|
|
|
|
auto const& shadow_bitmap_rect = shadow_config.shadow_bitmap_rect;
|
|
|
|
|
auto const& non_blurred_shadow_rect = shadow_config.non_blurred_shadow_rect;
|
|
|
|
|
auto const& inner_bounding_rect = shadow_config.inner_bounding_rect;
|
|
|
|
|
auto const& blurred_edge_thickness = shadow_config.blurred_edge_thickness;
|
|
|
|
|
auto const& double_radius = shadow_config.double_radius;
|
|
|
|
|
auto const& blur_radius = shadow_config.blur_radius;
|
|
|
|
|
|
|
|
|
|
auto const& top_left_corner_rect = shadow_config.top_left_corner_rect;
|
|
|
|
|
auto const& top_right_corner_rect = shadow_config.top_right_corner_rect;
|
|
|
|
|
auto const& bottom_right_corner_rect = shadow_config.bottom_right_corner_rect;
|
|
|
|
|
auto const& bottom_left_corner_rect = shadow_config.bottom_left_corner_rect;
|
|
|
|
|
|
|
|
|
|
auto const& top_left_corner_blit_pos = shadow_config.top_left_corner_blit_pos;
|
|
|
|
|
auto const& top_right_corner_blit_pos = shadow_config.top_right_corner_blit_pos;
|
|
|
|
|
auto const& bottom_right_corner_blit_pos = shadow_config.bottom_right_corner_blit_pos;
|
|
|
|
|
auto const& bottom_left_corner_blit_pos = shadow_config.bottom_left_corner_blit_pos;
|
|
|
|
|
|
|
|
|
|
auto const& top_left_corner_size = shadow_config.top_left_corner_size;
|
|
|
|
|
auto const& top_right_corner_size = shadow_config.top_right_corner_size;
|
|
|
|
|
auto const& bottom_right_corner_size = shadow_config.bottom_right_corner_size;
|
|
|
|
|
auto const& bottom_left_corner_size = shadow_config.bottom_left_corner_size;
|
|
|
|
|
|
|
|
|
|
auto const& left_start = shadow_config.left_start;
|
|
|
|
|
auto const& top_start = shadow_config.top_start;
|
|
|
|
|
auto const& right_start = shadow_config.right_start;
|
|
|
|
|
auto const& bottom_start = shadow_config.bottom_start;
|
|
|
|
|
|
|
|
|
|
auto const& left_edge_rect = shadow_config.left_edge_rect;
|
|
|
|
|
auto const& right_edge_rect = shadow_config.right_edge_rect;
|
|
|
|
|
auto const& top_edge_rect = shadow_config.top_edge_rect;
|
|
|
|
|
auto const& bottom_edge_rect = shadow_config.bottom_edge_rect;
|
|
|
|
|
|
|
|
|
|
auto const& top_left_shadow_corner = shadow_config.top_left_shadow_corner;
|
|
|
|
|
auto const& top_right_shadow_corner = shadow_config.top_right_shadow_corner;
|
|
|
|
|
auto const& bottom_right_shadow_corner = shadow_config.bottom_right_shadow_corner;
|
|
|
|
|
auto const& bottom_left_shadow_corner = shadow_config.bottom_left_shadow_corner;
|
|
|
|
|
|
|
|
|
|
auto fill_rect_masked = [](auto& painter, auto fill_rect, auto mask_rect, auto color) {
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::DisjointRectSet<int> rect_set;
|
2023-10-19 01:16:01 +02:00
|
|
|
rect_set.add(fill_rect);
|
|
|
|
|
auto shattered = rect_set.shatter(mask_rect);
|
|
|
|
|
for (auto& rect : shattered.rects())
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.fill_rect(rect, color);
|
2023-10-19 01:16:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// If there's no blurring, nor rounded corners, we can save a lot of effort.
|
2024-06-07 15:42:35 +03:00
|
|
|
if (blur_radius == 0 && !params.corner_radii.has_any_radius()) {
|
|
|
|
|
fill_rect_masked(painter, non_blurred_shadow_rect.translated(offset_x, offset_y), device_content_rect, params.color);
|
2023-06-05 02:42:32 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto paint_shadow_infill = [&] {
|
2024-06-07 15:42:35 +03:00
|
|
|
if (!params.corner_radii.has_any_radius())
|
2024-06-07 16:02:50 +03:00
|
|
|
return painter.fill_rect(inner_bounding_rect, params.color);
|
2023-06-05 02:42:32 +00:00
|
|
|
|
|
|
|
|
auto top_left_inner_width = top_left_corner_rect.width() - blurred_edge_thickness;
|
|
|
|
|
auto top_left_inner_height = top_left_corner_rect.height() - blurred_edge_thickness;
|
|
|
|
|
auto top_right_inner_width = top_right_corner_rect.width() - blurred_edge_thickness;
|
|
|
|
|
auto top_right_inner_height = top_right_corner_rect.height() - blurred_edge_thickness;
|
|
|
|
|
auto bottom_right_inner_width = bottom_right_corner_rect.width() - blurred_edge_thickness;
|
|
|
|
|
auto bottom_right_inner_height = bottom_right_corner_rect.height() - blurred_edge_thickness;
|
|
|
|
|
auto bottom_left_inner_width = bottom_left_corner_rect.width() - blurred_edge_thickness;
|
|
|
|
|
auto bottom_left_inner_height = bottom_left_corner_rect.height() - blurred_edge_thickness;
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect top_rect {
|
2023-06-05 02:42:32 +00:00
|
|
|
inner_bounding_rect.x() + top_left_inner_width,
|
|
|
|
|
inner_bounding_rect.y(),
|
|
|
|
|
inner_bounding_rect.width() - top_left_inner_width - top_right_inner_width,
|
|
|
|
|
top_left_inner_height
|
2022-06-20 12:15:04 +01:00
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect right_rect {
|
2023-06-05 02:42:32 +00:00
|
|
|
inner_bounding_rect.x() + inner_bounding_rect.width() - top_right_inner_width,
|
|
|
|
|
inner_bounding_rect.y() + top_right_inner_height,
|
|
|
|
|
top_right_inner_width,
|
|
|
|
|
inner_bounding_rect.height() - top_right_inner_height - bottom_right_inner_height
|
2022-06-20 12:15:04 +01:00
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect bottom_rect {
|
2023-06-05 02:42:32 +00:00
|
|
|
inner_bounding_rect.x() + bottom_left_inner_width,
|
|
|
|
|
inner_bounding_rect.y() + inner_bounding_rect.height() - bottom_right_inner_height,
|
|
|
|
|
inner_bounding_rect.width() - bottom_left_inner_width - bottom_right_inner_width,
|
|
|
|
|
bottom_right_inner_height
|
2022-06-20 12:15:04 +01:00
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect left_rect {
|
2023-06-05 02:42:32 +00:00
|
|
|
inner_bounding_rect.x(),
|
|
|
|
|
inner_bounding_rect.y() + top_left_inner_height,
|
|
|
|
|
bottom_left_inner_width,
|
|
|
|
|
inner_bounding_rect.height() - top_left_inner_height - bottom_left_inner_height
|
2022-06-20 12:15:04 +01:00
|
|
|
};
|
2024-06-07 16:02:50 +03:00
|
|
|
Gfx::IntRect inner = {
|
2023-06-05 02:42:32 +00:00
|
|
|
left_rect.x() + left_rect.width(),
|
|
|
|
|
left_rect.y(),
|
|
|
|
|
inner_bounding_rect.width() - left_rect.width() - right_rect.width(),
|
|
|
|
|
inner_bounding_rect.height() - top_rect.height() - bottom_rect.height()
|
2022-06-20 12:15:04 +01:00
|
|
|
};
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.fill_rect(top_rect, params.color);
|
|
|
|
|
painter.fill_rect(right_rect, params.color);
|
|
|
|
|
painter.fill_rect(bottom_rect, params.color);
|
|
|
|
|
painter.fill_rect(left_rect, params.color);
|
|
|
|
|
painter.fill_rect(inner, params.color);
|
2023-06-05 02:42:32 +00:00
|
|
|
};
|
|
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto shadows_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadow_bitmap_rect.size());
|
2023-10-19 01:16:01 +02:00
|
|
|
if (shadows_bitmap.is_error()) {
|
|
|
|
|
dbgln("Unable to allocate temporary bitmap {} for box-shadow rendering: {}", shadow_bitmap_rect, shadows_bitmap.error());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto shadow_bitmap = shadows_bitmap.release_value();
|
|
|
|
|
Gfx::Painter corner_painter { *shadow_bitmap };
|
|
|
|
|
Gfx::AntiAliasingPainter aa_corner_painter { corner_painter };
|
2023-06-05 02:42:32 +00:00
|
|
|
|
2023-10-19 01:16:01 +02:00
|
|
|
aa_corner_painter.fill_rect_with_rounded_corners(
|
2024-06-07 16:02:50 +03:00
|
|
|
shadow_bitmap_rect.shrunken(double_radius, double_radius, double_radius, double_radius),
|
2024-06-07 15:42:35 +03:00
|
|
|
params.color, top_left_shadow_corner, top_right_shadow_corner, bottom_right_shadow_corner, bottom_left_shadow_corner);
|
2023-10-19 01:16:01 +02:00
|
|
|
Gfx::StackBlurFilter filter(*shadow_bitmap);
|
2024-06-07 16:02:50 +03:00
|
|
|
filter.process_rgba(blur_radius, params.color);
|
2023-06-05 02:42:32 +00:00
|
|
|
|
2024-06-07 16:02:50 +03:00
|
|
|
auto paint_shadow = [&](Gfx::IntRect clip_rect) {
|
2023-06-05 02:42:32 +00:00
|
|
|
Gfx::PainterStateSaver save { painter };
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.add_clip_rect(clip_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
|
|
|
|
|
paint_shadow_infill();
|
|
|
|
|
|
|
|
|
|
// Corners
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.blit(top_left_corner_blit_pos, shadow_bitmap, top_left_corner_rect);
|
|
|
|
|
painter.blit(top_right_corner_blit_pos, shadow_bitmap, top_right_corner_rect);
|
|
|
|
|
painter.blit(bottom_left_corner_blit_pos, shadow_bitmap, bottom_left_corner_rect);
|
|
|
|
|
painter.blit(bottom_right_corner_blit_pos, shadow_bitmap, bottom_right_corner_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
|
|
|
|
|
// Horizontal edges
|
|
|
|
|
for (auto x = inner_bounding_rect.left() + (bottom_left_corner_size.width() - double_radius); x < inner_bounding_rect.right() - (bottom_right_corner_size.width() - double_radius); ++x)
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.blit({ x, bottom_start }, shadow_bitmap, bottom_edge_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
for (auto x = inner_bounding_rect.left() + (top_left_corner_size.width() - double_radius); x < inner_bounding_rect.right() - (top_right_corner_size.width() - double_radius); ++x)
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.blit({ x, top_start }, shadow_bitmap, top_edge_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
|
|
|
|
|
// Vertical edges
|
|
|
|
|
for (auto y = inner_bounding_rect.top() + (top_right_corner_size.height() - double_radius); y < inner_bounding_rect.bottom() - (bottom_right_corner_size.height() - double_radius); ++y)
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.blit({ right_start, y }, shadow_bitmap, right_edge_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
for (auto y = inner_bounding_rect.top() + (top_left_corner_size.height() - double_radius); y < inner_bounding_rect.bottom() - (bottom_left_corner_size.height() - double_radius); ++y)
|
2024-06-07 16:02:50 +03:00
|
|
|
painter.blit({ left_start, y }, shadow_bitmap, left_edge_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FIXME: Painter only lets us define a clip-rect which discards drawing outside of it, whereas here we want
|
|
|
|
|
// a rect which discards drawing inside it. So, we run the draw operations 4 to 8 times with clip-rects
|
|
|
|
|
// covering each side of the content_rect exactly once.
|
|
|
|
|
|
|
|
|
|
// If we were painting a shadow without a border radius we'd want to clip everything inside the box below.
|
|
|
|
|
// If painting a shadow with rounded corners (but still rectangular) we want to clip everything inside
|
|
|
|
|
// the box except the corners. This gives us an upper bound of 8 shadow paints now :^(.
|
|
|
|
|
// (However, this does not seem to be the costly part in profiling).
|
|
|
|
|
//
|
|
|
|
|
// ┌───┬────────┬───┐
|
|
|
|
|
// │ │xxxxxxxx│ │
|
|
|
|
|
// ├───┼────────┼───┤
|
|
|
|
|
// │xxx│xxxxxxxx│xxx│
|
|
|
|
|
// │xxx│xxxxxxxx│xxx│
|
|
|
|
|
// │xxx│xxxxxxxx│xxx│
|
|
|
|
|
// │xxx│xxxxxxxx│xxx│
|
|
|
|
|
// │xxx│xxxxxxxx│xxx│
|
|
|
|
|
// ├───┼────────┼───┤
|
|
|
|
|
// │ │ xxxxxx │ │
|
|
|
|
|
// └───┴────────┴───┘
|
|
|
|
|
|
|
|
|
|
// How many times would you like to paint the shadow sir?
|
|
|
|
|
// Yes.
|
|
|
|
|
|
|
|
|
|
// FIXME: Could reduce the shadow paints from 8 to 4 for shadows with all corner radii 50%.
|
|
|
|
|
|
|
|
|
|
// FIXME: We use this since we want the clip rect to include everything after a certain x or y.
|
2024-06-05 10:43:00 +02:00
|
|
|
// Note: Using painter.target().width() or height() does not work, when the painter is a small
|
2023-06-05 02:42:32 +00:00
|
|
|
// translated bitmap rather than full screen, as the clip rect may not intersect.
|
|
|
|
|
constexpr auto really_large_number = NumericLimits<int>::max() / 2;
|
|
|
|
|
|
|
|
|
|
// Everything above content_rect, including sides
|
|
|
|
|
paint_shadow({ 0, 0, really_large_number, device_content_rect.top() });
|
|
|
|
|
|
|
|
|
|
// Everything below content_rect, including sides
|
|
|
|
|
paint_shadow({ 0, device_content_rect.bottom(), really_large_number, really_large_number });
|
|
|
|
|
|
|
|
|
|
// Everything directly to the left of content_rect
|
|
|
|
|
paint_shadow({ 0, device_content_rect.top(), device_content_rect.left(), device_content_rect.height() });
|
|
|
|
|
|
|
|
|
|
// Everything directly to the right of content_rect
|
|
|
|
|
paint_shadow({ device_content_rect.right(), device_content_rect.top(), really_large_number, device_content_rect.height() });
|
|
|
|
|
|
|
|
|
|
if (top_left_corner) {
|
|
|
|
|
// Inside the top left corner (the part outside the border radius)
|
2024-06-07 16:02:50 +03:00
|
|
|
auto top_left = top_left_corner.as_rect().translated(device_content_rect.top_left());
|
2023-06-05 02:42:32 +00:00
|
|
|
paint_shadow(top_left);
|
|
|
|
|
}
|
2022-02-09 17:18:41 +00:00
|
|
|
|
2023-06-05 02:42:32 +00:00
|
|
|
if (top_right_corner) {
|
|
|
|
|
// Inside the top right corner (the part outside the border radius)
|
2024-06-07 16:02:50 +03:00
|
|
|
auto top_right = top_right_corner.as_rect().translated(device_content_rect.top_right().translated(-top_right_corner.horizontal_radius, 0));
|
2023-06-05 02:42:32 +00:00
|
|
|
paint_shadow(top_right);
|
|
|
|
|
}
|
2022-06-20 12:15:04 +01:00
|
|
|
|
2023-06-05 02:42:32 +00:00
|
|
|
if (bottom_right_corner) {
|
|
|
|
|
// Inside the bottom right corner (the part outside the border radius)
|
2024-06-07 16:02:50 +03:00
|
|
|
auto bottom_right = bottom_right_corner.as_rect().translated(device_content_rect.bottom_right().translated(-bottom_right_corner.horizontal_radius, -bottom_right_corner.vertical_radius));
|
2023-06-05 02:42:32 +00:00
|
|
|
paint_shadow(bottom_right);
|
|
|
|
|
}
|
2022-06-20 12:15:04 +01:00
|
|
|
|
2023-06-05 02:42:32 +00:00
|
|
|
if (bottom_left_corner) {
|
|
|
|
|
// Inside the bottom left corner (the part outside the border radius)
|
2024-06-07 16:02:50 +03:00
|
|
|
auto bottom_left = bottom_left_corner.as_rect().translated(device_content_rect.bottom_left().translated(0, -bottom_left_corner.vertical_radius));
|
2023-06-05 02:42:32 +00:00
|
|
|
paint_shadow(bottom_left);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-20 12:15:04 +01:00
|
|
|
|
2023-06-05 02:42:32 +00:00
|
|
|
void paint_box_shadow(PaintContext& context,
|
|
|
|
|
CSSPixelRect const& bordered_content_rect,
|
|
|
|
|
CSSPixelRect const& borderless_content_rect,
|
|
|
|
|
BordersData const& borders_data,
|
|
|
|
|
BorderRadiiData const& border_radii,
|
|
|
|
|
Vector<ShadowData> const& box_shadow_layers)
|
|
|
|
|
{
|
|
|
|
|
// Note: Box-shadow layers are ordered front-to-back, so we paint them in reverse
|
|
|
|
|
for (auto& box_shadow_data : box_shadow_layers.in_reverse()) {
|
2024-06-07 16:02:50 +03:00
|
|
|
auto offset_x = context.rounded_device_pixels(box_shadow_data.offset_x);
|
|
|
|
|
auto offset_y = context.rounded_device_pixels(box_shadow_data.offset_y);
|
|
|
|
|
auto blur_radius = context.rounded_device_pixels(box_shadow_data.blur_radius);
|
|
|
|
|
auto spread_distance = context.rounded_device_pixels(box_shadow_data.spread_distance);
|
2023-10-15 04:27:48 +02:00
|
|
|
|
|
|
|
|
DevicePixelRect device_content_rect;
|
2023-06-05 02:42:32 +00:00
|
|
|
if (box_shadow_data.placement == ShadowPlacement::Inner) {
|
2023-10-15 04:27:48 +02:00
|
|
|
device_content_rect = context.rounded_device_rect(borderless_content_rect);
|
2023-06-05 02:42:32 +00:00
|
|
|
} else {
|
2023-10-15 04:27:48 +02:00
|
|
|
device_content_rect = context.rounded_device_rect(bordered_content_rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto params = PaintOuterBoxShadowParams {
|
2024-06-07 15:42:35 +03:00
|
|
|
.color = box_shadow_data.color,
|
|
|
|
|
.placement = box_shadow_data.placement,
|
2023-10-15 04:27:48 +02:00
|
|
|
.corner_radii = CornerRadii {
|
|
|
|
|
.top_left = border_radii.top_left.as_corner(context),
|
|
|
|
|
.top_right = border_radii.top_right.as_corner(context),
|
|
|
|
|
.bottom_right = border_radii.bottom_right.as_corner(context),
|
|
|
|
|
.bottom_left = border_radii.bottom_left.as_corner(context) },
|
2024-06-07 16:02:50 +03:00
|
|
|
.offset_x = offset_x.value(),
|
|
|
|
|
.offset_y = offset_y.value(),
|
|
|
|
|
.blur_radius = blur_radius.value(),
|
|
|
|
|
.spread_distance = spread_distance.value(),
|
|
|
|
|
.device_content_rect = device_content_rect.to_type<int>(),
|
2023-10-15 04:27:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (box_shadow_data.placement == ShadowPlacement::Inner) {
|
2024-06-07 15:42:35 +03:00
|
|
|
auto shrinked_border_radii = border_radii;
|
|
|
|
|
shrinked_border_radii.shrink(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
|
|
|
|
|
ScopedCornerRadiusClip corner_clipper { context, device_content_rect, shrinked_border_radii, CornerClip::Outside };
|
2023-11-27 18:49:50 +01:00
|
|
|
context.recording_painter().paint_inner_box_shadow_params(params);
|
2023-10-15 04:27:48 +02:00
|
|
|
} else {
|
2023-10-22 16:59:16 +01:00
|
|
|
ScopedCornerRadiusClip corner_clipper { context, device_content_rect, border_radii, CornerClip::Inside };
|
2023-11-27 18:49:50 +01:00
|
|
|
context.recording_painter().paint_outer_box_shadow_params(params);
|
2022-06-20 12:15:04 +01:00
|
|
|
}
|
2022-02-08 14:48:37 +00:00
|
|
|
}
|
2021-09-19 15:00:47 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-12 21:25:05 +01:00
|
|
|
void paint_text_shadow(PaintContext& context, PaintableFragment const& fragment, Vector<ShadowData> const& shadow_layers)
|
2022-03-24 15:20:25 +00:00
|
|
|
{
|
2024-01-12 21:25:05 +01:00
|
|
|
if (shadow_layers.is_empty() || fragment.glyph_run().is_empty())
|
2022-03-24 15:20:25 +00:00
|
|
|
return;
|
|
|
|
|
|
2023-10-24 14:56:21 +02:00
|
|
|
auto fragment_width = context.enclosing_device_pixels(fragment.width()).value();
|
|
|
|
|
auto fragment_height = context.enclosing_device_pixels(fragment.height()).value();
|
|
|
|
|
auto draw_rect = context.enclosing_device_rect(fragment.absolute_rect()).to_type<int>();
|
|
|
|
|
auto fragment_baseline = context.rounded_device_pixels(fragment.baseline()).value();
|
2022-03-24 15:20:25 +00:00
|
|
|
|
2023-12-03 17:46:31 +01:00
|
|
|
Vector<Gfx::DrawGlyphOrEmoji> scaled_glyph_run;
|
2024-03-01 16:37:44 +01:00
|
|
|
scaled_glyph_run.ensure_capacity(fragment.glyph_run().glyphs().size());
|
|
|
|
|
for (auto glyph : fragment.glyph_run().glyphs()) {
|
2023-12-03 17:46:31 +01:00
|
|
|
glyph.visit([&](auto& glyph) {
|
2024-05-06 13:12:16 -04:00
|
|
|
glyph.font = glyph.font->with_size(glyph.font->point_size() * static_cast<float>(context.device_pixels_per_css_pixel()));
|
2023-12-03 17:46:31 +01:00
|
|
|
glyph.position = glyph.position.scaled(context.device_pixels_per_css_pixel());
|
|
|
|
|
});
|
|
|
|
|
scaled_glyph_run.append(move(glyph));
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 15:20:25 +00:00
|
|
|
// Note: Box-shadow layers are ordered front-to-back, so we paint them in reverse
|
|
|
|
|
for (auto& layer : shadow_layers.in_reverse()) {
|
2023-10-24 14:56:21 +02:00
|
|
|
int offset_x = context.rounded_device_pixels(layer.offset_x).value();
|
|
|
|
|
int offset_y = context.rounded_device_pixels(layer.offset_y).value();
|
|
|
|
|
int blur_radius = context.rounded_device_pixels(layer.blur_radius).value();
|
2022-03-24 15:20:25 +00:00
|
|
|
|
|
|
|
|
// Space around the painted text to allow it to blur.
|
|
|
|
|
// FIXME: Include spread in this once we use that.
|
2023-10-24 14:56:21 +02:00
|
|
|
int margin = blur_radius * 2;
|
|
|
|
|
Gfx::IntRect text_rect {
|
2022-03-24 15:20:25 +00:00
|
|
|
margin, margin,
|
2022-10-27 16:22:19 +01:00
|
|
|
fragment_width, fragment_height
|
2022-03-24 15:20:25 +00:00
|
|
|
};
|
2023-10-24 14:56:21 +02:00
|
|
|
Gfx::IntRect bounding_rect {
|
2022-03-24 15:20:25 +00:00
|
|
|
0, 0,
|
|
|
|
|
text_rect.width() + margin + margin,
|
|
|
|
|
text_rect.height() + margin + margin
|
|
|
|
|
};
|
2023-10-24 14:56:21 +02:00
|
|
|
Gfx::IntPoint draw_location {
|
2022-10-27 16:22:19 +01:00
|
|
|
draw_rect.x() + offset_x - margin,
|
|
|
|
|
draw_rect.y() + offset_y - margin
|
2022-03-24 15:20:25 +00:00
|
|
|
};
|
2023-10-15 04:27:48 +02:00
|
|
|
|
2023-12-03 17:46:31 +01:00
|
|
|
context.recording_painter().paint_text_shadow(blur_radius, bounding_rect, text_rect, scaled_glyph_run, layer.color, fragment_baseline, draw_location);
|
2022-03-24 15:20:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 15:00:47 +01:00
|
|
|
}
|