SVGGeometryBox.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <LibGfx/AntiAliasingPainter.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibWeb/Layout/SVGGeometryBox.h>
  10. #include <LibWeb/SVG/SVGPathElement.h>
  11. #include <LibWeb/SVG/SVGSVGElement.h>
  12. namespace Web::Layout {
  13. SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  14. : SVGGraphicsBox(document, element, properties)
  15. {
  16. }
  17. void SVGGeometryBox::paint(PaintContext& context, PaintPhase phase)
  18. {
  19. if (!is_visible())
  20. return;
  21. SVGGraphicsBox::paint(context, phase);
  22. if (phase != PaintPhase::Foreground)
  23. return;
  24. auto& geometry_element = dom_node();
  25. Gfx::AntiAliasingPainter painter { context.painter() };
  26. auto& svg_context = context.svg_context();
  27. auto offset = svg_context.svg_element_position();
  28. painter.translate(offset);
  29. SVG::SVGSVGElement* svg_element = geometry_element.first_ancestor_of_type<SVG::SVGSVGElement>();
  30. auto maybe_view_box = svg_element->view_box();
  31. context.painter().add_clip_rect((Gfx::Rect<int>)absolute_rect());
  32. Gfx::Path path = geometry_element.get_path();
  33. if (maybe_view_box.has_value()) {
  34. Gfx::Path new_path;
  35. auto scaling = viewbox_scaling();
  36. auto origin = viewbox_origin();
  37. auto transform_point = [&scaling, &origin](Gfx::FloatPoint const& point) -> Gfx::FloatPoint {
  38. auto new_point = point;
  39. new_point.translate_by({ -origin.x(), -origin.y() });
  40. new_point.scale_by(scaling);
  41. return new_point;
  42. };
  43. for (auto& segment : path.segments()) {
  44. switch (segment.type()) {
  45. case Gfx::Segment::Type::Invalid:
  46. break;
  47. case Gfx::Segment::Type::MoveTo:
  48. new_path.move_to(transform_point(segment.point()));
  49. break;
  50. case Gfx::Segment::Type::LineTo:
  51. new_path.line_to(transform_point(segment.point()));
  52. break;
  53. case Gfx::Segment::Type::QuadraticBezierCurveTo: {
  54. auto& quadratic_bezier_segment = static_cast<Gfx::QuadraticBezierCurveSegment const&>(segment);
  55. new_path.quadratic_bezier_curve_to(transform_point(quadratic_bezier_segment.through()), transform_point(quadratic_bezier_segment.point()));
  56. break;
  57. }
  58. case Gfx::Segment::Type::CubicBezierCurveTo: {
  59. auto& cubic_bezier_segment = static_cast<Gfx::CubicBezierCurveSegment const&>(segment);
  60. new_path.cubic_bezier_curve_to(transform_point(cubic_bezier_segment.through_0()), transform_point(cubic_bezier_segment.through_1()), transform_point(cubic_bezier_segment.point()));
  61. break;
  62. }
  63. case Gfx::Segment::Type::EllipticalArcTo: {
  64. auto& elliptical_arc_segment = static_cast<Gfx::EllipticalArcSegment const&>(segment);
  65. new_path.elliptical_arc_to(transform_point(elliptical_arc_segment.point()), elliptical_arc_segment.radii().scaled(scaling, scaling), elliptical_arc_segment.x_axis_rotation(), false, false);
  66. break;
  67. }
  68. }
  69. }
  70. path = new_path;
  71. }
  72. if (auto fill_color = geometry_element.fill_color().value_or(svg_context.fill_color()); fill_color.alpha() > 0) {
  73. // We need to fill the path before applying the stroke, however the filled
  74. // path must be closed, whereas the stroke path may not necessary be closed.
  75. // Copy the path and close it for filling, but use the previous path for stroke
  76. auto closed_path = path;
  77. closed_path.close();
  78. // Fills are computed as though all paths are closed (https://svgwg.org/svg2-draft/painting.html#FillProperties)
  79. painter.fill_path(
  80. closed_path,
  81. fill_color,
  82. Gfx::Painter::WindingRule::EvenOdd);
  83. }
  84. if (auto stroke_color = geometry_element.stroke_color().value_or(svg_context.stroke_color()); stroke_color.alpha() > 0) {
  85. painter.stroke_path(
  86. path,
  87. stroke_color,
  88. geometry_element.stroke_width().value_or(svg_context.stroke_width()));
  89. }
  90. painter.translate(-offset);
  91. context.painter().clear_clip_rect();
  92. }
  93. float SVGGeometryBox::viewbox_scaling() const
  94. {
  95. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  96. if (!svg_box || !svg_box->view_box().has_value())
  97. return 1;
  98. auto view_box = svg_box->view_box().value();
  99. bool has_specified_width = svg_box->has_attribute(HTML::AttributeNames::width);
  100. auto specified_width = content_width();
  101. bool has_specified_height = svg_box->has_attribute(HTML::AttributeNames::height);
  102. auto specified_height = content_height();
  103. auto scale_width = has_specified_width ? specified_width / view_box.width : 1;
  104. auto scale_height = has_specified_height ? specified_height / view_box.height : 1;
  105. return min(scale_width, scale_height);
  106. }
  107. Gfx::FloatPoint SVGGeometryBox::viewbox_origin() const
  108. {
  109. auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
  110. if (!svg_box || !svg_box->view_box().has_value())
  111. return { 0, 0 };
  112. return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
  113. }
  114. }