2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2022-03-10 14:02:25 +01:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2021-11-18 15:01:28 +01:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.h>
|
2023-05-11 19:23:36 +02:00
|
|
|
#include <LibWeb/HTML/DecodedImageData.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/ImageBox.h>
|
2024-02-18 20:10:37 -05:00
|
|
|
#include <LibWeb/Layout/ImageProvider.h>
|
2022-03-10 14:02:25 +01:00
|
|
|
#include <LibWeb/Painting/ImagePaintable.h>
|
2022-09-17 21:25:50 +02:00
|
|
|
#include <LibWeb/Platform/FontPlugin.h>
|
2019-10-05 22:07:45 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
namespace Web::Layout {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2024-04-06 10:16:04 -07:00
|
|
|
JS_DEFINE_ALLOCATOR(ImageBox);
|
|
|
|
|
2023-05-12 07:17:01 +02:00
|
|
|
ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, ImageProvider const& image_provider)
|
2020-11-22 15:53:01 +01:00
|
|
|
: ReplacedBox(document, element, move(style))
|
2023-05-12 07:17:01 +02:00
|
|
|
, m_image_provider(image_provider)
|
2019-10-05 22:07:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:38:25 +02:00
|
|
|
ImageBox::~ImageBox() = default;
|
|
|
|
|
2024-02-26 17:36:48 +01:00
|
|
|
void ImageBox::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_image_provider.to_html_element());
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
void ImageBox::prepare_for_replaced_layout()
|
2019-10-05 22:07:45 +02:00
|
|
|
{
|
2023-06-08 15:56:28 +01:00
|
|
|
set_natural_width(m_image_provider.intrinsic_width());
|
|
|
|
set_natural_height(m_image_provider.intrinsic_height());
|
|
|
|
set_natural_aspect_ratio(m_image_provider.intrinsic_aspect_ratio());
|
2020-07-22 01:36:07 +02:00
|
|
|
|
|
|
|
if (renders_as_alt_text()) {
|
2024-02-18 20:10:37 -05:00
|
|
|
auto const& element = verify_cast<HTML::HTMLElement>(dom_node());
|
|
|
|
auto alt = element.get_attribute_value(HTML::AttributeNames::alt);
|
2022-09-07 16:46:05 +02:00
|
|
|
|
2024-03-29 07:16:53 +01:00
|
|
|
if (alt.is_empty()) {
|
|
|
|
set_natural_width(0);
|
|
|
|
set_natural_height(0);
|
|
|
|
} else {
|
|
|
|
auto const& font = Platform::FontPlugin::the().default_font();
|
|
|
|
CSSPixels alt_text_width = 0;
|
|
|
|
if (!m_cached_alt_text_width.has_value())
|
|
|
|
m_cached_alt_text_width = CSSPixels::nearest_value_for(font.width(alt));
|
|
|
|
alt_text_width = m_cached_alt_text_width.value();
|
2022-09-07 16:46:05 +02:00
|
|
|
|
2024-03-29 07:16:53 +01:00
|
|
|
set_natural_width(alt_text_width + 16);
|
|
|
|
set_natural_height(CSSPixels::nearest_value_for(font.pixel_size()) + 16);
|
|
|
|
}
|
2020-07-22 01:36:07 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 15:56:28 +01:00
|
|
|
if (!has_natural_width() && !has_natural_height()) {
|
2022-03-09 23:53:41 +01:00
|
|
|
// FIXME: Do something.
|
2019-10-05 23:20:35 +02:00
|
|
|
}
|
2019-10-05 22:07:45 +02:00
|
|
|
}
|
|
|
|
|
2024-02-18 20:10:37 -05:00
|
|
|
void ImageBox::dom_node_did_update_alt_text(Badge<ImageProvider>)
|
2022-09-07 16:46:05 +02:00
|
|
|
{
|
|
|
|
m_cached_alt_text_width = {};
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
bool ImageBox::renders_as_alt_text() const
|
2019-10-05 23:20:35 +02:00
|
|
|
{
|
2024-02-18 20:10:37 -05:00
|
|
|
if (auto const* image_provider = dynamic_cast<ImageProvider const*>(&dom_node()))
|
|
|
|
return !image_provider->is_image_available();
|
2020-06-13 22:22:54 +02:00
|
|
|
return false;
|
2019-10-05 23:20:35 +02:00
|
|
|
}
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2023-01-11 12:51:49 +01:00
|
|
|
JS::GCPtr<Painting::Paintable> ImageBox::create_paintable() const
|
2022-03-10 14:02:25 +01:00
|
|
|
{
|
|
|
|
return Painting::ImagePaintable::create(*this);
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|