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