diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-13 22:22:54 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-13 22:22:54 +0200 |
commit | 95d70addd8384197fa9f619b0cc1fdb319280e7f (patch) | |
tree | c9dc5ac7ab2925849857c98c4b993e258a3ba7ad /Libraries/LibWeb/Layout | |
parent | d6d248c3289be80f318f53648a4c6aa1774d9e91 (diff) | |
download | serenity-95d70addd8384197fa9f619b0cc1fdb319280e7f.zip |
LibWeb: Split out image loading logic from HTMLImageElement
Since more DOM nodes are going to want to load images (<object>, ...)
this patch splits out the image loading logic into an ImageLoader class
and then HTMLImageElement simply has an ImageLoader.
LayoutImage is then given a const ImageLoader& at construction and can
then provide layout and rendering for many kinds of DOM nodes.
Diffstat (limited to 'Libraries/LibWeb/Layout')
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutDocument.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutImage.cpp | 44 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutImage.h | 9 |
3 files changed, 40 insertions, 16 deletions
diff --git a/Libraries/LibWeb/Layout/LayoutDocument.cpp b/Libraries/LibWeb/Layout/LayoutDocument.cpp index 646344b054..5b42311047 100644 --- a/Libraries/LibWeb/Layout/LayoutDocument.cpp +++ b/Libraries/LibWeb/Layout/LayoutDocument.cpp @@ -72,7 +72,8 @@ void LayoutDocument::did_set_viewport_rect(Badge<Frame>, const Gfx::IntRect& a_v { Gfx::FloatRect viewport_rect(a_viewport_rect.x(), a_viewport_rect.y(), a_viewport_rect.width(), a_viewport_rect.height()); for_each_in_subtree_of_type<LayoutImage>([&](auto& layout_image) { - const_cast<HTMLImageElement&>(layout_image.node()).set_visible_in_viewport({}, viewport_rect.intersects(layout_image.absolute_rect())); + if (is<HTMLImageElement>(layout_image.node())) + const_cast<HTMLImageElement&>(to<HTMLImageElement>(layout_image.node())).set_visible_in_viewport({}, viewport_rect.intersects(layout_image.absolute_rect())); return IterationDecision::Continue; }); } diff --git a/Libraries/LibWeb/Layout/LayoutImage.cpp b/Libraries/LibWeb/Layout/LayoutImage.cpp index cb14c42a57..c4200faa9e 100644 --- a/Libraries/LibWeb/Layout/LayoutImage.cpp +++ b/Libraries/LibWeb/Layout/LayoutImage.cpp @@ -24,15 +24,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <LibGUI/Painter.h> #include <LibGfx/Font.h> +#include <LibGfx/ImageDecoder.h> #include <LibGfx/StylePainter.h> -#include <LibGUI/Painter.h> #include <LibWeb/Layout/LayoutImage.h> namespace Web { -LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style) +LayoutImage::LayoutImage(const Element& element, NonnullRefPtr<StyleProperties> style, const ImageLoader& image_loader) : LayoutReplaced(element, move(style)) + , m_image_loader(image_loader) { } @@ -40,18 +42,31 @@ LayoutImage::~LayoutImage() { } +int LayoutImage::preferred_width() const +{ + auto* decoder = m_image_loader.image_decoder(); + return node().attribute(HTML::AttributeNames::width).to_int().value_or(decoder ? decoder->width() : 0); +} + +int LayoutImage::preferred_height() const +{ + auto* decoder = m_image_loader.image_decoder(); + return node().attribute(HTML::AttributeNames::height).to_int().value_or(decoder ? decoder->height() : 0); +} + void LayoutImage::layout(LayoutMode layout_mode) { - if (node().preferred_width() && node().preferred_height()) { + if (preferred_width() && preferred_height()) { set_has_intrinsic_width(true); set_has_intrinsic_height(true); - set_intrinsic_width(node().preferred_width()); - set_intrinsic_height(node().preferred_height()); + set_intrinsic_width(preferred_width()); + set_intrinsic_height(preferred_height()); } else if (renders_as_alt_text()) { + auto& image_element = to<HTMLImageElement>(node()); auto& font = Gfx::Font::default_font(); - auto alt = node().alt(); + auto alt = image_element.alt(); if (alt.is_empty()) - alt = node().src(); + alt = image_element.src(); set_width(font.width(alt) + 16); set_height(font.glyph_height() + 16); } else { @@ -74,20 +89,23 @@ void LayoutImage::render(RenderingContext& context) LayoutReplaced::render(context); if (renders_as_alt_text()) { + auto& image_element = to<HTMLImageElement>(node()); context.painter().set_font(Gfx::Font::default_font()); Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2); - auto alt = node().alt(); + auto alt = image_element.alt(); if (alt.is_empty()) - alt = node().src(); + alt = image_element.src(); context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), Gfx::TextElision::Right); - } else if (node().bitmap()) - context.painter().draw_scaled_bitmap(enclosing_int_rect(absolute_rect()), *node().bitmap(), node().bitmap()->rect()); - LayoutReplaced::render(context); + } else if (m_image_loader.bitmap()) { + context.painter().draw_scaled_bitmap(enclosing_int_rect(absolute_rect()), *m_image_loader.bitmap(), m_image_loader.bitmap()->rect()); + } } bool LayoutImage::renders_as_alt_text() const { - return !node().image_decoder(); + if (is<HTMLImageElement>(node())) + return !m_image_loader.image_decoder(); + return false; } } diff --git a/Libraries/LibWeb/Layout/LayoutImage.h b/Libraries/LibWeb/Layout/LayoutImage.h index 2112bab079..a972d51167 100644 --- a/Libraries/LibWeb/Layout/LayoutImage.h +++ b/Libraries/LibWeb/Layout/LayoutImage.h @@ -35,19 +35,24 @@ class HTMLImageElement; class LayoutImage : public LayoutReplaced { public: - LayoutImage(const HTMLImageElement&, NonnullRefPtr<StyleProperties>); + LayoutImage(const Element&, NonnullRefPtr<StyleProperties>, const ImageLoader&); virtual ~LayoutImage() override; virtual void layout(LayoutMode = LayoutMode::Default) override; virtual void render(RenderingContext&) override; - const HTMLImageElement& node() const { return static_cast<const HTMLImageElement&>(LayoutReplaced::node()); } + const Element& node() const { return static_cast<const Element&>(LayoutReplaced::node()); } bool renders_as_alt_text() const; private: virtual const char* class_name() const override { return "LayoutImage"; } virtual bool is_image() const override { return true; } + + int preferred_width() const; + int preferred_height() const; + + const ImageLoader& m_image_loader; }; template<> |