mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 15:47:35 +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:
parent
264543b90a
commit
6a6475673f
13 changed files with 140 additions and 70 deletions
|
@ -63,7 +63,7 @@ public:
|
|||
};
|
||||
|
||||
struct BackgroundLayerData {
|
||||
RefPtr<CSS::StyleValue> background_image { nullptr };
|
||||
RefPtr<CSS::AbstractImageStyleValue> background_image { nullptr };
|
||||
CSS::BackgroundAttachment attachment { CSS::BackgroundAttachment::Scroll };
|
||||
CSS::BackgroundBox origin { CSS::BackgroundBox::PaddingBox };
|
||||
CSS::BackgroundBox clip { CSS::BackgroundBox::BorderBox };
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/GenericShorthands.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/NonnullOwnPtrVector.h>
|
||||
#include <AK/NonnullRefPtrVector.h>
|
||||
|
@ -38,6 +39,7 @@
|
|||
#include <LibWeb/CSS/ValueID.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/Loader/ImageResource.h>
|
||||
#include <LibWeb/Painting/GradientPainting.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
|
@ -153,6 +155,7 @@ public:
|
|||
|
||||
Type type() const { return m_type; }
|
||||
|
||||
bool is_abstract_image() const { return AK::first_is_one_of(type(), Type::Image, Type::LinearGradient); }
|
||||
bool is_angle() const { return type() == Type::Angle; }
|
||||
bool is_background() const { return type() == Type::Background; }
|
||||
bool is_background_repeat() const { return type() == Type::BackgroundRepeat; }
|
||||
|
@ -191,6 +194,7 @@ public:
|
|||
|
||||
bool is_builtin() const { return is_inherit() || is_initial() || is_unset(); }
|
||||
|
||||
AbstractImageStyleValue const& as_abstract_image() const;
|
||||
AngleStyleValue const& as_angle() const;
|
||||
BackgroundStyleValue const& as_background() const;
|
||||
BackgroundRepeatStyleValue const& as_background_repeat() const;
|
||||
|
@ -227,6 +231,7 @@ public:
|
|||
UnsetStyleValue const& as_unset() const;
|
||||
StyleValueList const& as_value_list() const;
|
||||
|
||||
AbstractImageStyleValue& as_abstract_image() { return const_cast<AbstractImageStyleValue&>(const_cast<StyleValue const&>(*this).as_abstract_image()); }
|
||||
AngleStyleValue& as_angle() { return const_cast<AngleStyleValue&>(const_cast<StyleValue const&>(*this).as_angle()); }
|
||||
BackgroundStyleValue& as_background() { return const_cast<BackgroundStyleValue&>(const_cast<StyleValue const&>(*this).as_background()); }
|
||||
BackgroundRepeatStyleValue& as_background_repeat() { return const_cast<BackgroundRepeatStyleValue&>(const_cast<StyleValue const&>(*this).as_background_repeat()); }
|
||||
|
@ -919,8 +924,22 @@ private:
|
|||
CSS::ValueID m_id { CSS::ValueID::Invalid };
|
||||
};
|
||||
|
||||
class AbstractImageStyleValue : public StyleValue {
|
||||
public:
|
||||
using StyleValue::StyleValue;
|
||||
|
||||
virtual Optional<int> natural_width() const { return {}; }
|
||||
virtual Optional<int> natural_height() const { return {}; }
|
||||
|
||||
virtual void load_any_resources(DOM::Document&) {};
|
||||
virtual void resolve_for_size(Layout::Node const&, Gfx::FloatSize const&) const {};
|
||||
|
||||
virtual bool is_paintable() const = 0;
|
||||
virtual void paint(PaintContext& context, Gfx::IntRect const& dest_rect) const = 0;
|
||||
};
|
||||
|
||||
class ImageStyleValue final
|
||||
: public StyleValue
|
||||
: public AbstractImageStyleValue
|
||||
, public ImageResourceClient {
|
||||
public:
|
||||
static NonnullRefPtr<ImageStyleValue> create(AK::URL const& url) { return adopt_ref(*new ImageStyleValue(url)); }
|
||||
|
@ -929,9 +948,16 @@ public:
|
|||
virtual String to_string() const override;
|
||||
virtual bool equals(StyleValue const& other) const override;
|
||||
|
||||
void load_bitmap(DOM::Document& document);
|
||||
virtual void load_any_resources(DOM::Document&) override;
|
||||
|
||||
Gfx::Bitmap const* bitmap() const { return m_bitmap; }
|
||||
|
||||
Optional<int> natural_width() const override;
|
||||
Optional<int> natural_height() const override;
|
||||
|
||||
bool is_paintable() const override { return !m_bitmap.is_null(); }
|
||||
void paint(PaintContext& context, Gfx::IntRect const& dest_rect) const override;
|
||||
|
||||
private:
|
||||
ImageStyleValue(AK::URL const&);
|
||||
|
||||
|
@ -943,7 +969,7 @@ private:
|
|||
RefPtr<Gfx::Bitmap> m_bitmap;
|
||||
};
|
||||
|
||||
class LinearGradientStyleValue final : public StyleValue {
|
||||
class LinearGradientStyleValue final : public AbstractImageStyleValue {
|
||||
public:
|
||||
using GradientDirection = Variant<Angle, SideOrCorner>;
|
||||
|
||||
|
@ -967,11 +993,16 @@ public:
|
|||
return m_color_stop_list;
|
||||
}
|
||||
|
||||
float angle_degrees(Gfx::FloatRect const& gradient_rect) const;
|
||||
float angle_degrees(Gfx::FloatSize const& gradient_rect) const;
|
||||
|
||||
void resolve_for_size(Layout::Node const&, Gfx::FloatSize const&) const override;
|
||||
|
||||
bool is_paintable() const override { return true; }
|
||||
void paint(PaintContext& context, Gfx::IntRect const& dest_rect) const override;
|
||||
|
||||
private:
|
||||
LinearGradientStyleValue(GradientDirection direction, Vector<ColorStopListElement> color_stop_list, GradientType type)
|
||||
: StyleValue(Type::LinearGradient)
|
||||
: AbstractImageStyleValue(Type::LinearGradient)
|
||||
, m_direction(direction)
|
||||
, m_color_stop_list(move(color_stop_list))
|
||||
, m_gradient_type(type)
|
||||
|
@ -981,6 +1012,8 @@ private:
|
|||
GradientDirection m_direction;
|
||||
Vector<ColorStopListElement> m_color_stop_list;
|
||||
GradientType m_gradient_type;
|
||||
|
||||
mutable Optional<Painting::LinearGradientData> m_resolved_data;
|
||||
};
|
||||
|
||||
class InheritStyleValue final : public StyleValue {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue