mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 08:18:12 +00:00
LibWeb: Move box-shadow painting out of Box to its own file
This makes the code accessible to things that aren't a Box, such as InlineNode.
This commit is contained in:
parent
3b6325e787
commit
b047c1bc97
4 changed files with 83 additions and 37 deletions
53
Userland/Libraries/LibWeb/Painting/ShadowPainting.cpp
Normal file
53
Userland/Libraries/LibWeb/Painting/ShadowPainting.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* 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/DisjointRectSet.h>
|
||||
#include <LibGfx/Filters/FastBoxBlurFilter.h>
|
||||
#include <LibGfx/Painter.h>
|
||||
#include <LibWeb/Painting/PaintContext.h>
|
||||
#include <LibWeb/Painting/ShadowPainting.h>
|
||||
|
||||
namespace Web::Painting {
|
||||
|
||||
void paint_box_shadow(PaintContext& context, Gfx::IntRect const& content_rect, BoxShadowData const& box_shadow_data)
|
||||
{
|
||||
Gfx::IntRect bitmap_rect = {
|
||||
0,
|
||||
0,
|
||||
content_rect.width() + 4 * box_shadow_data.blur_radius,
|
||||
content_rect.height() + 4 * box_shadow_data.blur_radius
|
||||
};
|
||||
|
||||
Gfx::IntPoint blur_rect_position = {
|
||||
content_rect.x() - 2 * box_shadow_data.blur_radius + box_shadow_data.offset_x,
|
||||
content_rect.y() - 2 * box_shadow_data.blur_radius + box_shadow_data.offset_y
|
||||
};
|
||||
|
||||
if (bitmap_rect.is_empty())
|
||||
return;
|
||||
|
||||
auto new_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, bitmap_rect.size());
|
||||
if (!new_bitmap) {
|
||||
dbgln("Unable to allocate temporary bitmap for box-shadow rendering");
|
||||
return;
|
||||
}
|
||||
|
||||
Gfx::Painter painter(*new_bitmap);
|
||||
painter.fill_rect({ { 2 * box_shadow_data.blur_radius, 2 * box_shadow_data.blur_radius }, content_rect.size() }, box_shadow_data.color);
|
||||
|
||||
Gfx::FastBoxBlurFilter filter(*new_bitmap);
|
||||
filter.apply_three_passes(box_shadow_data.blur_radius);
|
||||
|
||||
Gfx::DisjointRectSet rect_set;
|
||||
rect_set.add(bitmap_rect);
|
||||
auto shattered = rect_set.shatter({ content_rect.location() - blur_rect_position, content_rect.size() });
|
||||
|
||||
for (auto& rect : shattered.rects())
|
||||
context.painter().blit(rect.location() + blur_rect_position, *new_bitmap, rect);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue