Serenity Operating System
1/*
2 * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
3 * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibWeb/Layout/SVGGeometryBox.h>
9#include <LibWeb/Painting/SVGGeometryPaintable.h>
10#include <LibWeb/SVG/SVGPathElement.h>
11#include <LibWeb/SVG/SVGSVGElement.h>
12
13namespace Web::Layout {
14
15SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
16 : SVGGraphicsBox(document, element, properties)
17{
18}
19
20float SVGGeometryBox::viewbox_scaling() const
21{
22 auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
23
24 if (!svg_box || !svg_box->view_box().has_value())
25 return 1;
26
27 auto view_box = svg_box->view_box().value();
28
29 bool has_specified_width = svg_box->has_attribute(HTML::AttributeNames::width);
30 auto specified_width = paint_box()->content_width().value();
31
32 bool has_specified_height = svg_box->has_attribute(HTML::AttributeNames::height);
33 auto specified_height = paint_box()->content_height().value();
34
35 auto scale_width = has_specified_width ? specified_width / view_box.width : 1;
36 auto scale_height = has_specified_height ? specified_height / view_box.height : 1;
37
38 return min(scale_width, scale_height);
39}
40
41CSSPixelPoint SVGGeometryBox::viewbox_origin() const
42{
43 auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
44 if (!svg_box || !svg_box->view_box().has_value())
45 return { 0, 0 };
46 return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
47}
48
49JS::GCPtr<Painting::Paintable> SVGGeometryBox::create_paintable() const
50{
51 return Painting::SVGGeometryPaintable::create(*this);
52}
53
54}