dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_values / cursor_style_value.lucb
#==============================================================================================
#
# cursor_style_value - CursorStyleValue: an image cursor and its hotspot
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/CursorStyleValue.cpp (lines 1-146) and the inline members
# of CursorStyleValue.h.
#
# Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.h:20 Web::CSS::CursorStyleValue::create.
pub func css_cursor_style_value_create(image: CssAbstractImageStyleValue*, x: CssStyleValue*?, y: CssStyleValue*?) -> CssCursorStyleValue*:
# We require either both or neither the X and Y parameters
assert((x == none and y == none) or (x != none and y != none))
let this = &(new CssCursorStyleValue[1] --- else trap("out of memory"))[0]
css_cursor_style_value_construct(this, image, x, y)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.h:39 Web::CSS::CursorStyleValue::CursorStyleValue.
pub func css_cursor_style_value_construct(this: CssCursorStyleValue*, image: CssAbstractImageStyleValue*, x: CssStyleValue*?, y: CssStyleValue*?):
css_style_value_construct(this.css_style_value(), CssStyleValueType.cursor)
css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_cursor_style_value_vtable, &css_cursor_style_value_class)
this.m_properties = CssCursorStyleValueProperties(image = image, x = x, y = y)
this.m_cache_key = none
this.m_cached_bitmap = none
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.cpp:23 Web::CSS::CursorStyleValue::serialize (override).
pub func css_cursor_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
let this = (CssCursorStyleValue*)base
css_style_value_serialize(this.m_properties.image.css_style_value(), builder, mode)
if let x = this.m_properties.x:
let y = this.m_properties.y else trap("VERIFY: m_properties.y")
ak.string_builder_append_u8(builder, ' ')
css_style_value_serialize(x, builder, mode)
ak.string_builder_append_u8(builder, ' ')
css_style_value_serialize(y, builder, mode)
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.cpp:36 Web::CSS::CursorStyleValue::absolutized (override).
pub func css_cursor_style_value_absolutized_impl(base: CssStyleValue*, computation_context: CssComputationContext) -> CssStyleValue*:
let this = (CssCursorStyleValue*)base
var absolutized_x: CssStyleValue*? = none
var absolutized_y: CssStyleValue*? = none
if let x = this.m_properties.x:
absolutized_x = css_style_value_absolutized(x, computation_context)
if let y = this.m_properties.y:
absolutized_y = css_style_value_absolutized(y, computation_context)
return css_cursor_style_value_create(css_style_value_as_abstract_image(css_style_value_absolutized(this.m_properties.image.css_style_value(), computation_context)), absolutized_x, absolutized_y).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.h:34 Web::CSS::CursorStyleValue::properties_equal.
pub func css_cursor_style_value_properties_equal(this: CssCursorStyleValue*, other: CssCursorStyleValue*) -> bool:
return css_style_values_equal(this.m_properties.image.css_style_value(), other.m_properties.image.css_style_value()) and css_optional_style_values_equal(this.m_properties.x, other.m_properties.x) and css_optional_style_values_equal(this.m_properties.y, other.m_properties.y)
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::CursorStyleValue (CRTP helper) (override).
pub func css_cursor_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
if css_style_value_type(base) != css_style_value_type(other):
return false
return css_cursor_style_value_properties_equal((CssCursorStyleValue*)base, (CssCursorStyleValue*)other)
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.cpp:50 Web::CSS::CursorStyleValue::is_computationally_independent (override).
pub func css_cursor_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
let this = (CssCursorStyleValue*)base
return css_style_value_is_computationally_independent(this.m_properties.image.css_style_value()) and css_is_null_or_computationally_independent(this.m_properties.x) and css_is_null_or_computationally_independent(this.m_properties.y)
## Ported from Libraries/LibWeb/CSS/StyleValues/CursorStyleValue.cpp:57 Web::CSS::CursorStyleValue::make_image_cursor.
pub func css_cursor_style_value_make_image_cursor(this: CssCursorStyleValue*, layout_node: LayoutNodeWithStyle*) -> gfx.ImageCursor?:
let image = this.m_properties.image
if not css_abstract_image_style_value_is_paintable(image):
css_abstract_image_style_value_load_any_resources_dom_document(image, layout_node_document((LayoutNode*)layout_node))
return none
let document = layout_node_document((LayoutNode*)layout_node)
let computed_values = layout_node_with_style_computed_values(layout_node)
let cache_key = CssCursorStyleValueCacheKey(length_resolution_context = css_length_resolution_context_for_layout_node((LayoutNode*)layout_node), current_color = css_computed_values_color(&computed_values.computed_values))
# Create a bitmap if needed.
# The cursor size for a given image never changes. It's based either on the image itself, or our default size,
# neither of which is affected by what layout node it's for.
if this.m_cached_bitmap == none:
# Determine the size of the cursor.
# "The default object size for cursor images is a UA-defined size that should be based on the size of a
# typical cursor on the UA’s operating system.
# The concrete object size is determined using the default sizing algorithm. If an operating system is
# incapable of rendering a cursor above a given size, cursors larger than that size must be shrunk to
# within the OS-supported size bounds, while maintaining the cursor image’s natural aspect ratio, if any."
# https://drafts.csswg.org/css-ui-3/#cursor
# 32x32 is selected arbitrarily.
# FIXME: Ask the OS for the default size?
let default_cursor_size = gfx.CssPixelSize(m_width = gfx.CssPixels.from_int(32), m_height = gfx.CssPixels.from_int(32))
let natural = CssSizeWithAspectRatio(width = css_abstract_image_style_value_natural_width(image), height = css_abstract_image_style_value_natural_height(image), aspect_ratio = css_abstract_image_style_value_natural_aspect_ratio(image))
let cursor_css_size = css_run_default_sizing_algorithm(none, none, natural, default_cursor_size)
# FIXME: How do we determine what cursor sizes the OS allows?
# We don't multiply by the pixel ratio, because we want to use the image's actual pixel size.
let cursor_device_size = gfx.IntSize(m_width = ak.rounding_round_to_f64_i32(cursor_css_size.m_width.to_double()), m_height = ak.rounding_round_to_f64_i32(cursor_css_size.m_height.to_double()))
let bitmap = gfx.bitmap_create_shareable(gfx.BitmapFormat.bgra8888, gfx.AlphaType.premultiplied, cursor_device_size) catch failure:
ak.dbgln(f"Failed to create cursor bitmap: {failure.message}")
return none
this.m_cached_bitmap = gfx.bitmap_to_shareable_bitmap(bitmap)
# Repaint the bitmap if necessary
if this.m_cache_key != cache_key:
this.m_cache_key = cache_key
# Clear whatever was in the bitmap before.
var cached_bitmap = this.m_cached_bitmap else trap("VERIFY: a cached bitmap")
let bitmap = gfx.shareable_bitmap_bitmap(&cached_bitmap) else trap("VERIFY: a shareable bitmap's bitmap")
let painter = gfx.painter_create(bitmap)
let bitmap_rect = gfx.bitmap_rect(bitmap)
gfx.painter_clear_rect(painter, gfx.device_pixel_rect_to_type_f32(&bitmap_rect), gfx.color_new_named_color(gfx.ColorNamedColor.transparent))
# Paint the cursor into a bitmap.
let recorded_display_list = display_list.display_list_create(display_list.accumulated_visual_context_tree_create())
var display_list_recorder = display_list.display_list_recorder_new(recorded_display_list)
let page = dom_document_page(document)
var paint_context: DisplayListRecordingContext = ---
display_list_recording_context_construct(&paint_context, &display_list_recorder, page_palette(page), page_client_device_pixels_per_css_pixel(page_client(page)), page_chrome_metrics(page))
let bitmap_size = gfx.bitmap_size(bitmap)
css_abstract_image_style_value_resolve_for_size(image, layout_node, gfx.CssPixelSize(m_width = gfx.CssPixels.from_int((i64)bitmap_size.m_width), m_height = gfx.CssPixels.from_int((i64)bitmap_size.m_height)))
css_abstract_image_style_value_paint(image, &paint_context, gfx.bitmap_rect(bitmap), css_data.ImageRendering.auto)
match page_client_display_list_player_type(page_client(page)):
.skia_gpu_if_available, .skia_cpu:
# NB: DisplayListPlayerSkia is replaced by the CPU player over the Luce rasterizer (DESIGN.md §7.3).
let painting_surface = gfx.painting_surface_wrap_bitmap(bitmap)
let display_list_player = display_list.display_list_player_cpu_create()
let scroll_state_snapshot = display_list.ScrollStateSnapshot(m_device_offsets = ak.Vector[gfx.FloatPoint]())
display_list.display_list_player_execute((display_list.DisplayListPlayer*)display_list_player, recorded_display_list, &scroll_state_snapshot, painting_surface)
_:
trap("VERIFY_NOT_REACHED")
# "If the values are unspecified, then the natural hotspot defined inside the image resource itself is used.
# If both the values are unspecific and the referenced cursor has no defined hotspot, the effect is as if a
# value of "0 0" were specified."
# FIXME: Make use of embedded hotspots.
var hotspot = gfx.IntPoint(m_x = 0, m_y = 0)
if let x = this.m_properties.x:
if let y = this.m_properties.y:
assert(dom_document_window(document) != none)
hotspot = gfx.IntPoint(m_x = (i32)css_number_from_style_value(x, none), m_y = (i32)css_number_from_style_value(y, none))
return gfx.ImageCursor(bitmap = this.m_cached_bitmap else trap("VERIFY: a cached bitmap"), hotspot = hotspot)