Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/SVG/SVGFilterElement.cpp (C++)

Code before:
            auto input = resolve_input_filter(blur_primitive->in1()->base_val());

            auto radius_x = blur_primitive->std_deviation_x()->base_val();
            auto radius_y = blur_primitive->std_deviation_y()->base_val();

            root_filter = Gfx::Filter::blur(radius_x, radius_y, input);
            update_result_map(*blur_primitive);
        } else if (auto* image_primitive = as_if<SVGFEImageElement>(node)) {
            auto bitmap = image_primitive->current_image_bitmap({});
            if (!bitmap)
                return IterationDecision::Continue;

            auto width = image_primitive->width()->base_val()->value();
            if (width == 0)
                width = bitmap->width();

            auto height = image_primitive->height()->base_val()->value();
            if (height == 0)
                height = bitmap->height();

            Gfx::IntRect src_rect {
                image_primitive->x()->base_val()->value(),
                image_primitive->y()->base_val()->value(),
                width,
                height
            };

            auto* dom_node = referenced_node.dom_node();
            if (!dom_node)
                return IterationDecision::Continue;

            auto* paintable_box = dom_node->paintable_box();
            if (!paintable_box)
                return IterationDecision::Continue;

            auto dest_rect = Gfx::enclosing_int_rect(paintable_box->absolute_rect().to_type<float>());

            auto scaling_mode = to_gfx_scaling_mode(paintable_box->computed_values().image_rendering(), src_rect, dest_rect);
            root_filter = Gfx::Filter::image(*bitmap, src_rect, dest_rect, scaling_mode);
            update_result_map(*image_primitive);
        } else if (auto* merge_primitive = as_if<SVGFEMergeElement>(node)) {
            Vector<Optional<Gfx::Filter>> merge_inputs;
            merge_primitive->template for_each_child_of_type<SVGFEMergeNodeElement>([&](auto& merge_node) {
                merge_inputs.append(resolve_input_filter(merge_node.in1()->base_val()));
                return IterationDecision::Continue;
            });

            root_filter = Gfx::Filter::merge(merge_inputs);
            update_result_map(*merge_primitive);
        } else if (auto* offset_primitive = as_if<SVGFEOffsetElement>(node)) {
            auto input = resolve_input_filter(offset_primitive->in1()->base_val());

Review comment (suggestion):
Maybe all this `src_rect` logic should go into `SVGFEImageElement`? That would keep the filter logic here a bit simpler.

Code after:
            auto input = resolve_input_filter(blur_primitive->in1()->base_val());

            auto radius_x = blur_primitive->std_deviation_x()->base_val();
            auto radius_y = blur_primitive->std_deviation_y()->base_val();

            root_filter = Gfx::Filter::blur(radius_x, radius_y, input);
            update_result_map(*blur_primitive);
        } else if (auto* image_primitive = as_if<SVGFEImageElement>(node)) {
            auto bitmap = image_primitive->current_image_bitmap({});
            if (!bitmap)
                return IterationDecision::Continue;

            auto src_rect = image_primitive->content_rect();
            if (!src_rect.has_value())
                return IterationDecision::Continue;

            auto* dom_node = referenced_node.dom_node();
            if (!dom_node)
                return IterationDecision::Continue;

            auto* paintable_box = dom_node->paintable_box();
            if (!paintable_box)
                return IterationDecision::Continue;

            auto dest_rect = Gfx::enclosing_int_rect(paintable_box->absolute_rect().to_type<float>());
            auto scaling_mode = CSS::to_gfx_scaling_mode(paintable_box->computed_values().image_rendering(), *src_rect, dest_rect);
            root_filter = Gfx::Filter::image(*bitmap, *src_rect, dest_rect, scaling_mode);
            update_result_map(*image_primitive);
        } else if (auto* merge_primitive = as_if<SVGFEMergeElement>(node)) {
            Vector<Optional<Gfx::Filter>> merge_inputs;
            merge_primitive->template for_each_child_of_type<SVGFEMergeNodeElement>([&](auto& merge_node) {
                merge_inputs.append(resolve_input_filter(merge_node.in1()->base_val()));
                return IterationDecision::Continue;
            });

            root_filter = Gfx::Filter::merge(merge_inputs);
            update_result_map(*merge_primitive);
        } else if (auto* offset_primitive = as_if<SVGFEOffsetElement>(node)) {
            auto input = resolve_input_filter(offset_primitive->in1()->base_val());

            auto dx = offset_primitive->dx()->base_val();
            auto dy = offset_primitive->dy()->base_val();

            root_filter = Gfx::Filter::offset(dx, dy, input);
            update_result_map(*offset_primitive);
        } else {
            dbgln("SVGFilterElement::gfx_filter(): Unknown or unsupported filter element '{}'", node.debug_description());
        }

        return IterationDecision::Continue;
    });