1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

LibWeb: Convert images to common AbstractImageStyleValue base

This commit moves both the ImageStyleValue and LinearGradientStyleValue
to a common base class of AbstractImageStyleValue. This abstracts
getting the natural_width/height, loading/resolving, and painting
the image.

Now for 'free' you get:

 - Linear gradients working with the various background sizing/repeat
   properties.
 - Linear gradients working as list-markers :^) -- best feature ever!

P.s. This commit is a little large as it's tricky to make this change
incrementally without breaking things.
This commit is contained in:
MacDue 2022-07-31 01:11:59 +01:00 committed by Andreas Kling
parent 264543b90a
commit 6a6475673f
13 changed files with 140 additions and 70 deletions

View file

@ -15,6 +15,7 @@
#include <LibWeb/Loader/LoadRequest.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Painting/GradientPainting.h>
namespace Web::CSS {
@ -23,6 +24,12 @@ StyleValue::StyleValue(Type type)
{
}
AbstractImageStyleValue const& StyleValue::as_abstract_image() const
{
VERIFY(is_abstract_image());
return static_cast<AbstractImageStyleValue const&>(*this);
}
AngleStyleValue const& StyleValue::as_angle() const
{
VERIFY(is_angle());
@ -1399,12 +1406,12 @@ Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
}
ImageStyleValue::ImageStyleValue(AK::URL const& url)
: StyleValue(Type::Image)
: AbstractImageStyleValue(Type::Image)
, m_url(url)
{
}
void ImageStyleValue::load_bitmap(DOM::Document& document)
void ImageStyleValue::load_any_resources(DOM::Document& document)
{
if (m_bitmap)
return;
@ -1436,6 +1443,26 @@ bool ImageStyleValue::equals(StyleValue const& other) const
return m_url == other.as_image().m_url;
}
Optional<int> ImageStyleValue::natural_width() const
{
if (m_bitmap)
return m_bitmap->width();
return {};
}
Optional<int> ImageStyleValue::natural_height() const
{
if (m_bitmap)
return m_bitmap->height();
return {};
}
void ImageStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect) const
{
if (m_bitmap)
context.painter().draw_scaled_bitmap(dest_rect, *m_bitmap, m_bitmap->rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
}
String LinearGradientStyleValue::to_string() const
{
StringBuilder builder;
@ -1537,10 +1564,10 @@ bool LinearGradientStyleValue::equals(StyleValue const& other_) const
return true;
}
float LinearGradientStyleValue::angle_degrees(Gfx::FloatRect const& gradient_rect) const
float LinearGradientStyleValue::angle_degrees(Gfx::FloatSize const& gradient_size) const
{
auto corner_angle_degrees = [&] {
return static_cast<float>(atan2(gradient_rect.height(), gradient_rect.width())) * 180 / AK::Pi<float>;
return static_cast<float>(atan2(gradient_size.height(), gradient_size.width())) * 180 / AK::Pi<float>;
};
return m_direction.visit(
[&](SideOrCorner side_or_corner) {
@ -1576,6 +1603,17 @@ float LinearGradientStyleValue::angle_degrees(Gfx::FloatRect const& gradient_rec
});
}
void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, Gfx::FloatSize const& size) const
{
m_resolved_data = Painting::resolve_linear_gradient_data(node, size, *this);
}
void LinearGradientStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect) const
{
VERIFY(m_resolved_data.has_value());
Painting::paint_linear_gradient(context, dest_rect, *m_resolved_data);
}
bool InheritStyleValue::equals(StyleValue const& other) const
{
return type() == other.type();