|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <LibWeb/Bindings/SVGComponentTransferFunctionElementPrototype.h> |
| 8 | +#include <LibWeb/SVG/AttributeNames.h> |
| 9 | +#include <LibWeb/SVG/AttributeParser.h> |
| 10 | +#include <LibWeb/SVG/SVGComponentTransferFunctionElement.h> |
| 11 | +#include <LibWeb/SVG/SVGNumber.h> |
| 12 | +#include <LibWeb/SVG/SVGNumberList.h> |
| 13 | + |
| 14 | +namespace Web::SVG { |
| 15 | + |
| 16 | +SVGComponentTransferFunctionElement::SVGComponentTransferFunctionElement(DOM::Document& document, DOM::QualifiedName qualified_name) |
| 17 | + : SVGElement(document, move(qualified_name)) |
| 18 | +{ |
| 19 | +} |
| 20 | + |
| 21 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-type |
| 22 | +static SVGComponentTransferFunctionElement::Type parse_type(Optional<String> const& value) |
| 23 | +{ |
| 24 | + if (value == "identity"sv) |
| 25 | + return SVGComponentTransferFunctionElement::Type::Identity; |
| 26 | + if (value == "table"sv) |
| 27 | + return SVGComponentTransferFunctionElement::Type::Table; |
| 28 | + if (value == "discrete"sv) |
| 29 | + return SVGComponentTransferFunctionElement::Type::Discrete; |
| 30 | + if (value == "linear"sv) |
| 31 | + return SVGComponentTransferFunctionElement::Type::Linear; |
| 32 | + if (value == "gamma"sv) |
| 33 | + return SVGComponentTransferFunctionElement::Type::Gamma; |
| 34 | + |
| 35 | + return SVGComponentTransferFunctionElement::Type::Unknown; |
| 36 | +} |
| 37 | + |
| 38 | +void SVGComponentTransferFunctionElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, |
| 39 | + Optional<String> const& value, Optional<FlyString> const& namespace_) |
| 40 | +{ |
| 41 | + Base::attribute_changed(name, old_value, value, namespace_); |
| 42 | + |
| 43 | + // FIXME: Support reflection instead of invalidating the enumeration. |
| 44 | + if (name == AttributeNames::type) |
| 45 | + m_type = {}; |
| 46 | + |
| 47 | + // FIXME: Support reflection instead of invalidating the list. |
| 48 | + if (name == AttributeNames::tableValues) |
| 49 | + m_table_values = {}; |
| 50 | +} |
| 51 | + |
| 52 | +void SVGComponentTransferFunctionElement::initialize(JS::Realm& realm) |
| 53 | +{ |
| 54 | + WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGComponentTransferFunctionElement); |
| 55 | + Base::initialize(realm); |
| 56 | +} |
| 57 | + |
| 58 | +void SVGComponentTransferFunctionElement::visit_edges(Visitor& visitor) |
| 59 | +{ |
| 60 | + Base::visit_edges(visitor); |
| 61 | + visitor.visit(m_type); |
| 62 | + visitor.visit(m_table_values); |
| 63 | + visitor.visit(m_slope); |
| 64 | + visitor.visit(m_intercept); |
| 65 | + visitor.visit(m_amplitude); |
| 66 | + visitor.visit(m_exponent); |
| 67 | + visitor.visit(m_offset); |
| 68 | +} |
| 69 | + |
| 70 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-type |
| 71 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-type |
| 72 | +GC::Ref<SVGAnimatedEnumeration> SVGComponentTransferFunctionElement::type() |
| 73 | +{ |
| 74 | + if (!m_type) |
| 75 | + m_type = SVGAnimatedEnumeration::create(realm(), to_underlying(type_from_attribute())); |
| 76 | + return *m_type; |
| 77 | +} |
| 78 | + |
| 79 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-tablevalues |
| 80 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-tablevalues |
| 81 | +GC::Ref<SVGAnimatedNumberList> SVGComponentTransferFunctionElement::table_values() |
| 82 | +{ |
| 83 | + if (!m_table_values) { |
| 84 | + auto numbers = AttributeParser::parse_table_values(get_attribute_value(AttributeNames::tableValues)); |
| 85 | + |
| 86 | + Vector<GC::Ref<SVGNumber>> items; |
| 87 | + items.ensure_capacity(numbers.size()); |
| 88 | + for (auto number : numbers) |
| 89 | + items.unchecked_append(SVGNumber::create(realm(), number, SVGNumber::ReadOnly::Yes)); |
| 90 | + |
| 91 | + auto number_list = SVGNumberList::create(realm(), move(items), ReadOnlyList::Yes); |
| 92 | + m_table_values = SVGAnimatedNumberList::create(realm(), number_list); |
| 93 | + } |
| 94 | + return *m_table_values; |
| 95 | +} |
| 96 | + |
| 97 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-slope |
| 98 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-slope |
| 99 | +GC::Ref<SVGAnimatedNumber> SVGComponentTransferFunctionElement::slope() |
| 100 | +{ |
| 101 | + if (!m_slope) |
| 102 | + m_slope = SVGAnimatedNumber::create(realm(), *this, DOM::QualifiedName { AttributeNames::slope, {}, {} }, 1.f); |
| 103 | + return *m_slope; |
| 104 | +} |
| 105 | + |
| 106 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-intercept |
| 107 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-intercept |
| 108 | +GC::Ref<SVGAnimatedNumber> SVGComponentTransferFunctionElement::intercept() |
| 109 | +{ |
| 110 | + if (!m_intercept) |
| 111 | + m_intercept = SVGAnimatedNumber::create(realm(), *this, DOM::QualifiedName { AttributeNames::intercept, {}, {} }, 0.f); |
| 112 | + return *m_intercept; |
| 113 | +} |
| 114 | + |
| 115 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-amplitude |
| 116 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-amplitude |
| 117 | +GC::Ref<SVGAnimatedNumber> SVGComponentTransferFunctionElement::amplitude() |
| 118 | +{ |
| 119 | + if (!m_amplitude) |
| 120 | + m_amplitude = SVGAnimatedNumber::create(realm(), *this, DOM::QualifiedName { AttributeNames::amplitude, {}, {} }, 1.f); |
| 121 | + return *m_amplitude; |
| 122 | +} |
| 123 | + |
| 124 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-exponent |
| 125 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-exponent |
| 126 | +GC::Ref<SVGAnimatedNumber> SVGComponentTransferFunctionElement::exponent() |
| 127 | +{ |
| 128 | + if (!m_exponent) |
| 129 | + m_exponent = SVGAnimatedNumber::create(realm(), *this, DOM::QualifiedName { AttributeNames::exponent, {}, {} }, 1.f); |
| 130 | + return *m_exponent; |
| 131 | +} |
| 132 | + |
| 133 | +// https://www.w3.org/TR/filter-effects-1/#dom-svgcomponenttransferfunctionelement-offset |
| 134 | +// https://drafts.fxtf.org/filter-effects-1/#element-attrdef-fecomponenttransfer-offset |
| 135 | +GC::Ref<SVGAnimatedNumber> SVGComponentTransferFunctionElement::offset() |
| 136 | +{ |
| 137 | + if (!m_offset) |
| 138 | + m_offset = SVGAnimatedNumber::create(realm(), *this, DOM::QualifiedName { AttributeNames::offset, {}, {} }, 0.f); |
| 139 | + return *m_offset; |
| 140 | +} |
| 141 | + |
| 142 | +SVGComponentTransferFunctionElement::Type SVGComponentTransferFunctionElement::type_from_attribute() const |
| 143 | +{ |
| 144 | + return parse_type(get_attribute_value(AttributeNames::type)); |
| 145 | +} |
| 146 | + |
| 147 | +} |
0 commit comments