dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / svg / svg_component_transfer_function_element.lucb
#==============================================================================================
#
# svg_component_transfer_function_element - SVG::SVGComponentTransferFunctionElement
#
# DESCRIPTION:
# Port of LibWeb/SVG/SVGComponentTransferFunctionElement.cpp (lines 1-263): the base of
# <feFuncR>, <feFuncG>, <feFuncB> and <feFuncA>
# (https://www.w3.org/TR/filter-effects-1/#InterfaceSVGComponentTransferFunctionElement):
# the type, tableValues, slope, intercept, amplitude, exponent and offset attributes and
# the 256-entry color table of the transfer function.
#
#==============================================================================================
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:16 Web::SVG::SVGComponentTransferFunctionElement::SVGComponentTransferFunctionElement.
pub func svg_component_transfer_function_element_construct(this: SvgComponentTransferFunctionElement*, document: DomDocument*, qualified_name: DomQualifiedName):
svg_element_construct(this.svg_element(), document, qualified_name)
# The vptrs of the mixin bases move to SVGComponentTransferFunctionElement's tables (a subclass moves them on).
let element = this.svg_element()
element.element.slottable_mixin.vtable = &svg_component_transfer_function_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &svg_component_transfer_function_element_aria_mixin_vtable
element.element.animatable.vtable = &svg_component_transfer_function_element_animatable_vtable
element.global_event_handlers.vtable = &svg_component_transfer_function_element_global_event_handlers_vtable
## `value == "text"sv` of an optional attribute value.
func svg_optional_string_is(value: ak.String?, text: str) -> bool:
let string = value else return false
let view = ak.string_bytes_as_string_view(&string)
return ak.string_view_equals(&view, text)
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:22 Web::SVG::parse_type (static).
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-type
func svg_parse_type(value: ak.String?) -> SvgComponentTransferFunctionElementType:
if svg_optional_string_is(value, "identity"):
return SvgComponentTransferFunctionElementType.identity
if svg_optional_string_is(value, "table"):
return SvgComponentTransferFunctionElementType.table
if svg_optional_string_is(value, "discrete"):
return SvgComponentTransferFunctionElementType.discrete
if svg_optional_string_is(value, "linear"):
return SvgComponentTransferFunctionElementType.linear
if svg_optional_string_is(value, "gamma"):
return SvgComponentTransferFunctionElementType.gamma
return SvgComponentTransferFunctionElementType.unknown
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:38 Web::SVG::SVGComponentTransferFunctionElement::attribute_changed (override).
pub func svg_component_transfer_function_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
let this = (SvgComponentTransferFunctionElement*)base
svg_element_attribute_changed_impl(base, name, old_value, value, namespace_)
# FIXME: Support reflection instead of invalidating the enumeration.
if ak.fly_string_eq_fly_string(&name, &svg_attribute_names_type):
this.m_type = none
# FIXME: Support reflection instead of invalidating the list.
if ak.fly_string_eq_fly_string(&name, &svg_attribute_names_table_values):
this.m_table_values = none
# Clear our cached color table on any attribute change.
this.m_cached_color_table = none
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:55 Web::SVG::SVGComponentTransferFunctionElement::initialize (override).
pub func svg_component_transfer_function_element_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.svg_component_transfer_function_element)
svg_element_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:61 Web::SVG::SVGComponentTransferFunctionElement::visit_edges (override).
pub func svg_component_transfer_function_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (SvgComponentTransferFunctionElement*)base
svg_element_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_type)
gc.cell_visitor_visit_t(visitor, this.m_table_values)
gc.cell_visitor_visit_t(visitor, this.m_slope)
gc.cell_visitor_visit_t(visitor, this.m_intercept)
gc.cell_visitor_visit_t(visitor, this.m_amplitude)
gc.cell_visitor_visit_t(visitor, this.m_exponent)
gc.cell_visitor_visit_t(visitor, this.m_offset)
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:75 Web::SVG::SVGComponentTransferFunctionElement::type.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-type
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-type
pub func svg_component_transfer_function_element_type(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedEnumeration*:
if this.m_type == none:
this.m_type = svg_animated_enumeration_create(dom_element_realm(this.dom_element()), (u16)svg_component_transfer_function_element_type_from_attribute(this))
return this.m_type else trap("VERIFY: m_type")
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:84 Web::SVG::SVGComponentTransferFunctionElement::table_values.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-tablevalues
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-tablevalues
pub func svg_component_transfer_function_element_table_values(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedNumberList*:
if this.m_table_values == none:
let realm = dom_element_realm(this.dom_element())
let attribute = dom_element_get_attribute_value(this.dom_element(), svg_attribute_names_table_values)
let numbers = svg_attribute_parser_parse_table_values(ak.string_bytes_as_string_view(&attribute))
var items = ak.Vector[SvgNumber*]()
ak.vector_ensure_capacity(&items, ak.vector_size(&numbers))
for number in ak.vector_span_const(&numbers):
ak.vector_append_t(&items, svg_number_create(realm, number, SvgNumberReadOnly.yes))
let number_list = svg_number_list_create_vector(realm, items, SvgReadOnlyList.yes)
this.m_table_values = svg_animated_number_list_create(realm, number_list)
return this.m_table_values else trap("VERIFY: m_table_values")
## A lazily created SVGAnimatedNumber reflecting `name` with the initial value `initial_value`.
func svg_component_transfer_function_element_animated_number(this: SvgComponentTransferFunctionElement*, name: ak.FlyString, initial_value: f32) -> SvgAnimatedNumber*:
var qualified_name: DomQualifiedName = ---
dom_qualified_name_construct(&qualified_name, name, none, none)
return svg_animated_number_create(dom_element_realm(this.dom_element()), this.svg_element(), qualified_name, initial_value)
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:102 Web::SVG::SVGComponentTransferFunctionElement::slope.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-slope
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-slope
pub func svg_component_transfer_function_element_slope(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedNumber*:
if this.m_slope == none:
this.m_slope = svg_component_transfer_function_element_animated_number(this, svg_attribute_names_slope, 1.0)
return this.m_slope else trap("VERIFY: m_slope")
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:111 Web::SVG::SVGComponentTransferFunctionElement::intercept.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-intercept
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-intercept
pub func svg_component_transfer_function_element_intercept(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedNumber*:
if this.m_intercept == none:
this.m_intercept = svg_component_transfer_function_element_animated_number(this, svg_attribute_names_intercept, 0.0)
return this.m_intercept else trap("VERIFY: m_intercept")
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:120 Web::SVG::SVGComponentTransferFunctionElement::amplitude.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-amplitude
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-amplitude
pub func svg_component_transfer_function_element_amplitude(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedNumber*:
if this.m_amplitude == none:
this.m_amplitude = svg_component_transfer_function_element_animated_number(this, svg_attribute_names_amplitude, 1.0)
return this.m_amplitude else trap("VERIFY: m_amplitude")
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:129 Web::SVG::SVGComponentTransferFunctionElement::exponent.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-exponent
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-exponent
pub func svg_component_transfer_function_element_exponent(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedNumber*:
if this.m_exponent == none:
this.m_exponent = svg_component_transfer_function_element_animated_number(this, svg_attribute_names_exponent, 1.0)
return this.m_exponent else trap("VERIFY: m_exponent")
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:138 Web::SVG::SVGComponentTransferFunctionElement::offset.
# https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-offset
# https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-offset
pub func svg_component_transfer_function_element_offset(this: SvgComponentTransferFunctionElement*) -> SvgAnimatedNumber*:
if this.m_offset == none:
this.m_offset = svg_component_transfer_function_element_animated_number(this, svg_attribute_names_offset, 0.0)
return this.m_offset else trap("VERIFY: m_offset")
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:145 Web::SVG::SVGComponentTransferFunctionElement::type_from_attribute.
func svg_component_transfer_function_element_type_from_attribute(this: SvgComponentTransferFunctionElement*) -> SvgComponentTransferFunctionElementType:
return svg_parse_type(dom_element_get_attribute_value(this.dom_element(), svg_attribute_names_type))
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:150 Web::SVG::SVGComponentTransferFunctionElement::table_float_values.
pub func svg_component_transfer_function_element_table_float_values(this: SvgComponentTransferFunctionElement*) -> ak.Vector[f32]:
var values = ak.Vector[f32]()
let base_val = svg_animated_number_list_base_val(svg_component_transfer_function_element_table_values(this))
let table_numbers = svg_list_items(&base_val.svg_list)
ak.vector_ensure_capacity(&values, table_numbers.length)
for svg_number in table_numbers:
ak.vector_append_t(&values, svg_number_value(svg_number))
return values
## color_table's to_u8 lambda: `AK::clamp_to<u8>(value * 255.f)`.
func svg_component_transfer_to_u8(value: f32) -> u8:
let scaled = value * 255.0
if scaled >= 255.0:
return 255
if scaled <= 0.0:
return 0
return (u8)ak.rounding_round_to_f32_i32(scaled)
## Ported from Libraries/LibWeb/SVG/SVGComponentTransferFunctionElement.cpp:161 Web::SVG::SVGComponentTransferFunctionElement::color_table.
# https://drafts.fxtf.org/filter-effects/#element-attrdef-fecomponenttransfer-type
pub func svg_component_transfer_function_element_color_table(this: SvgComponentTransferFunctionElement*) -> const u8[]:
if let cached = this.m_cached_color_table:
return ak.byte_buffer_bytes_const(&cached)
var result = ak.ByteBuffer()
ak.byte_buffer_resize(&result, 256)
let data = ak.byte_buffer_span(&result)
match svg_component_transfer_function_element_type_from_attribute(this):
# https://drafts.fxtf.org/filter-effects/#attr-valuedef-type-identity
.unknown, .identity:
for i in 0..<256:
data[i] = (u8)i
# https://drafts.fxtf.org/filter-effects/#attr-valuedef-type-table
.table:
let table_values = svg_component_transfer_function_element_table_float_values(this)
# An empty list results in an identity transfer function.
if ak.vector_is_empty(&table_values):
for i in 0..<256:
data[i] = (u8)i
else:
# For a value C < 1 find k such that: k/n <= C < (k+1)/n
# The result C' is given by: C' = vk + (C - k/n)*n * (vk+1 - vk)
let segments = f32(ak.vector_size(&table_values)) - 1.0
for i in 0..<256:
# If C = 1 then: C' = vn.
if i == 255 or segments == 0.0:
data[i] = svg_component_transfer_to_u8(*ak.vector_last_const(&table_values))
continue
let offset = f32(i) / 255.0
let segment_index = (usize)(offset * segments)
let segment_start = f32(segment_index) / segments
let offset_in_segment = offset - segment_start
let segment_length = 1.0 / segments
let progress_in_segment = offset_in_segment / segment_length
let segment_value = ak.mix_f32(*ak.vector_at_const(&table_values, segment_index), *ak.vector_at_const(&table_values, segment_index + 1), progress_in_segment)
data[i] = svg_component_transfer_to_u8(segment_value)
# https://drafts.fxtf.org/filter-effects/#attr-valuedef-type-discrete
.discrete:
let table_values = svg_component_transfer_function_element_table_float_values(this)
# An empty list results in an identity transfer function.
if ak.vector_is_empty(&table_values):
for i in 0..<256:
data[i] = (u8)i
else:
# For a value C < 1 find k such that: k/n <= C < (k+1)/n
# The result C' is given by: C' = vk + (C - k/n)*n * (vk+1 - vk)
for i in 0..<255:
let index = (usize)(f32(i) / 255.0 * f32(ak.vector_size(&table_values)))
data[i] = svg_component_transfer_to_u8(*ak.vector_at_const(&table_values, index))
# If C = 1 then: C' = vn.
data[255] = svg_component_transfer_to_u8(*ak.vector_last_const(&table_values))
# https://drafts.fxtf.org/filter-effects/#attr-valuedef-type-linear
.linear:
let slope = svg_animated_number_base_val(svg_component_transfer_function_element_slope(this))
let intercept = svg_animated_number_base_val(svg_component_transfer_function_element_intercept(this))
for i in 0..<256:
data[i] = svg_component_transfer_to_u8(slope * f32(i) / 255.0 + intercept)
# https://drafts.fxtf.org/filter-effects/#attr-valuedef-type-gamma
.gamma:
let amplitude = svg_animated_number_base_val(svg_component_transfer_function_element_amplitude(this))
let exponent = svg_animated_number_base_val(svg_component_transfer_function_element_exponent(this))
let offset = svg_animated_number_base_val(svg_component_transfer_function_element_offset(this))
for i in 0..<256:
data[i] = svg_component_transfer_to_u8(amplitude * ak.pow_f32(f32(i) / 255.0, exponent) + offset)
_: trap("VERIFY_NOT_REACHED")
this.m_cached_color_table = result
return ak.byte_buffer_bytes_const(&result)