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
|
|
|
*/
|
|
|
|
|
2020-12-29 18:25:13 +01:00
|
|
|
#include <LibGfx/FontDatabase.h>
|
2022-02-18 12:21:27 +01:00
|
|
|
#include <LibWeb/CSS/StyleValue.h>
|
2021-11-18 15:01:28 +01:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/ImageBox.h>
|
2022-03-10 14:02:25 +01:00
|
|
|
#include <LibWeb/Painting/ImagePaintable.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
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style, const ImageLoader& image_loader)
|
|
|
|
: ReplacedBox(document, element, move(style))
|
2020-06-13 22:22:54 +02:00
|
|
|
, m_image_loader(image_loader)
|
2019-10-05 22:07:45 +02:00
|
|
|
{
|
2021-05-30 12:36:53 +02:00
|
|
|
browsing_context().register_viewport_client(*this);
|
2019-10-05 22:07:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
ImageBox::~ImageBox()
|
2019-10-05 22:07:45 +02:00
|
|
|
{
|
2021-05-30 12:36:53 +02:00
|
|
|
browsing_context().unregister_viewport_client(*this);
|
2019-10-05 22:07:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
int ImageBox::preferred_width() const
|
2020-06-13 22:22:54 +02:00
|
|
|
{
|
2020-11-22 14:46:36 +01:00
|
|
|
return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(m_image_loader.width());
|
2020-06-13 22:22:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
int ImageBox::preferred_height() const
|
2020-06-13 22:22:54 +02:00
|
|
|
{
|
2020-11-22 14:46:36 +01:00
|
|
|
return dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(m_image_loader.height());
|
2020-06-13 22:22:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
void ImageBox::prepare_for_replaced_layout()
|
2019-10-05 22:07:45 +02:00
|
|
|
{
|
2020-08-12 13:47:55 +02:00
|
|
|
if (!m_image_loader.has_loaded_or_failed()) {
|
|
|
|
set_intrinsic_width(0);
|
|
|
|
set_intrinsic_height(0);
|
2020-07-22 01:36:07 +02:00
|
|
|
} else {
|
2020-08-12 13:47:55 +02:00
|
|
|
if (m_image_loader.width()) {
|
|
|
|
set_intrinsic_width(m_image_loader.width());
|
|
|
|
}
|
|
|
|
if (m_image_loader.height()) {
|
|
|
|
set_intrinsic_height(m_image_loader.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_image_loader.width() && m_image_loader.height()) {
|
2021-10-14 18:48:49 +02:00
|
|
|
set_intrinsic_aspect_ratio((float)m_image_loader.width() / (float)m_image_loader.height());
|
2020-08-12 13:47:55 +02:00
|
|
|
} else {
|
2021-10-14 18:48:49 +02:00
|
|
|
set_intrinsic_aspect_ratio({});
|
2020-08-12 13:47:55 +02:00
|
|
|
}
|
2020-07-22 01:36:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (renders_as_alt_text()) {
|
2021-06-24 19:53:42 +02:00
|
|
|
auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
|
2020-12-29 18:25:13 +01:00
|
|
|
auto& font = Gfx::FontDatabase::default_font();
|
2020-06-13 22:22:54 +02:00
|
|
|
auto alt = image_element.alt();
|
2019-10-06 23:13:54 +11:00
|
|
|
if (alt.is_empty())
|
2020-06-13 22:22:54 +02:00
|
|
|
alt = image_element.src();
|
2021-11-03 10:25:01 +01:00
|
|
|
set_intrinsic_width(font.width(alt) + 16);
|
|
|
|
set_intrinsic_height(font.glyph_height() + 16);
|
2020-07-22 01:36:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_intrinsic_width() && !has_intrinsic_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
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
bool ImageBox::renders_as_alt_text() const
|
2019-10-05 23:20:35 +02:00
|
|
|
{
|
2020-11-22 14:46:36 +01:00
|
|
|
if (is<HTML::HTMLImageElement>(dom_node()))
|
2020-06-22 21:41:10 +02:00
|
|
|
return !m_image_loader.has_image();
|
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
|
|
|
|
2021-09-08 11:12:22 +02:00
|
|
|
void ImageBox::browsing_context_did_set_viewport_rect(Gfx::IntRect const& viewport_rect)
|
2020-06-14 19:32:23 +02:00
|
|
|
{
|
2022-03-10 15:50:16 +01:00
|
|
|
m_image_loader.set_visible_in_viewport(viewport_rect.to_type<float>().intersects(paint_box()->absolute_rect()));
|
2020-06-14 19:32:23 +02:00
|
|
|
}
|
|
|
|
|
2022-03-10 22:38:08 +01:00
|
|
|
RefPtr<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
|
|
|
}
|