dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / layout / image_box.lucb
#==============================================================================================
#
# image_box - Layout::ImageBox: the box of an image (or of its alt text)
#
# DESCRIPTION:
# Port of LibWeb/Layout/ImageBox.cpp (lines 1-77) and ImageBox.h: the constructor,
# visit_edges, the natural size (the image's, or the alt text's measured at 12pt), the
# alt text cache and the ImagePaintable.
#
# Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/Layout/ImageBox.cpp:18 Web::Layout::ImageBox::ImageBox.
pub func layout_image_box_construct(this: LayoutImageBox*, document: DomDocument*, element: DomElement*?, style: CssComputedProperties*, image_provider: LayoutImageProvider*):
layout_replaced_box_construct(this.layout_replaced_box(), document, element, style)
this.m_image_provider = image_provider
## Ported from Libraries/LibWeb/Layout/ImageBox.cpp:26 Web::Layout::ImageBox::visit_edges (override).
pub func layout_image_box_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (LayoutImageBox*)base
layout_box_visit_edges_impl(base, visitor)
layout_image_provider_image_provider_visit_edges(this.m_image_provider, visitor)
## Ported from Libraries/LibWeb/Layout/ImageBox.cpp:32 Web::Layout::ImageBox::natural_size (override).
pub func layout_image_box_natural_size_impl(base: LayoutBox*) -> CssSizeWithAspectRatio:
let this = (LayoutImageBox*)base
if layout_image_provider_is_image_available(this.m_image_provider):
return CssSizeWithAspectRatio(width = layout_image_provider_intrinsic_width(this.m_image_provider), height = layout_image_provider_intrinsic_height(this.m_image_provider), aspect_ratio = layout_image_provider_intrinsic_aspect_ratio(this.m_image_provider))
var alt = ak.String()
if let element = layout_replaced_box_dom_node(this.layout_replaced_box()):
alt = dom_element_get_attribute_value(element, html_attribute_names_alt)
if ak.string_is_empty(&alt):
return CssSizeWithAspectRatio(width = gfx.CssPixels.from_int(0), height = gfx.CssPixels.from_int(0), aspect_ratio = none)
let font = platform_font_plugin_default_font(platform_font_plugin_the(), 12.0f32) else trap("VERIFY: a default font")
if this.m_cached_alt_text_width == none:
let alt_utf16 = ak.utf16_string_from_utf8_string(&alt)
this.m_cached_alt_text_width = gfx.css_pixels_nearest_value_for_f32(web_fonts.font_width(font, ak.utf16_string_base_utf16_view(&alt_utf16.utf16_string_base)))
let alt_text_width = this.m_cached_alt_text_width else trap("unreachable: the width was just cached")
let width = alt_text_width.add(gfx.CssPixels.from_int(16))
let height = gfx.css_pixels_nearest_value_for_f32(web_fonts.font_pixel_size(font)).add(gfx.CssPixels.from_int(16))
var aspect_ratio: gfx.CssPixelFraction? = none
if gfx.gt_css_pixels_int(height, 0):
var fraction: gfx.CssPixelFraction = ---
gfx.css_pixel_fraction_construct_css_pixels(&fraction, width, height)
aspect_ratio = fraction
return CssSizeWithAspectRatio(width = width, height = height, aspect_ratio = aspect_ratio)
## Ported from Libraries/LibWeb/Layout/ImageBox.cpp:62 Web::Layout::ImageBox::dom_node_did_update_alt_text.
pub func layout_image_box_dom_node_did_update_alt_text(this: LayoutImageBox*):
this.m_cached_alt_text_width = none
## Ported from Libraries/LibWeb/Layout/ImageBox.cpp:67 Web::Layout::ImageBox::renders_as_alt_text.
pub func layout_image_box_renders_as_alt_text(this: LayoutImageBox*) -> bool:
return not layout_image_provider_is_image_available(this.m_image_provider)
## Ported from Libraries/LibWeb/Layout/ImageBox.cpp:72 Web::Layout::ImageBox::create_paintable (override).
pub func layout_image_box_create_paintable_impl(base: LayoutNode*) -> PaintPaintable*?:
return paint_image_paintable_create_layout_image_box((LayoutImageBox*)base).paint_paintable()
## Ported from Libraries/LibWeb/Layout/ImageBox.h:26 Web::Layout::ImageBox::image_provider.
pub func layout_image_box_image_provider_const(this: LayoutImageBox*) -> LayoutImageProvider*:
return this.m_image_provider
## Ported from Libraries/LibWeb/Layout/ImageBox.h:27 Web::Layout::ImageBox::image_provider.
pub func layout_image_box_image_provider(this: LayoutImageBox*) -> LayoutImageProvider*:
return this.m_image_provider