mirror of
https://github.com/RGBCube/serenity
synced 2025-06-24 08:02:11 +00:00

This is a helper class for clipping the corners off a element. This works in a similar way to how (outline) borders are painted. The steps are: 1. A small bitmap that fits only the corners is allocated 2. The corners are painted into the bitmap 3. The existing pixels (where the corners will be painted) are copied using the (inverse) corner bitmap as a mask (done before the element is painted) 4. The element is painted 5. The areas outside the corner radii are restored Like with the borders, this only requires allocation on the first paint.
51 lines
1.4 KiB
C++
51 lines
1.4 KiB
C++
/*
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibGfx/AntiAliasingPainter.h>
|
|
#include <LibWeb/Painting/BorderPainting.h>
|
|
|
|
namespace Web::Painting {
|
|
|
|
class BorderRadiusCornerClipper {
|
|
public:
|
|
static ErrorOr<BorderRadiusCornerClipper> create(Gfx::IntRect const& border_rect, BorderRadiiData const& border_radii);
|
|
|
|
void sample_under_corners(Gfx::Painter& page_painter);
|
|
void blit_corner_clipping(Gfx::Painter& page_painter);
|
|
|
|
private:
|
|
using CornerRadius = Gfx::AntiAliasingPainter::CornerRadius;
|
|
struct CornerData {
|
|
struct CornerRadii {
|
|
CornerRadius top_left;
|
|
CornerRadius top_right;
|
|
CornerRadius bottom_right;
|
|
CornerRadius bottom_left;
|
|
} corner_radii;
|
|
struct CornerLocations {
|
|
Gfx::IntPoint top_left;
|
|
Gfx::IntPoint top_right;
|
|
Gfx::IntPoint bottom_right;
|
|
Gfx::IntPoint bottom_left;
|
|
};
|
|
CornerLocations page_locations;
|
|
CornerLocations bitmap_locations;
|
|
Gfx::IntSize corner_bitmap_size;
|
|
} m_data;
|
|
|
|
NonnullRefPtr<Gfx::Bitmap> m_corner_bitmap;
|
|
bool m_has_sampled { false };
|
|
|
|
BorderRadiusCornerClipper(CornerData corner_data, NonnullRefPtr<Gfx::Bitmap> corner_bitmap)
|
|
: m_data(move(corner_data))
|
|
, m_corner_bitmap(corner_bitmap)
|
|
{
|
|
}
|
|
};
|
|
|
|
}
|