SVGGeometryBox.cpp 5.5 KB

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