HTMLPathElement.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibWeb/DOM/HTMLElement.h>
  29. #include <LibWeb/DOM/SvgContext.h>
  30. namespace Web {
  31. enum class PathInstructionType {
  32. Move,
  33. ClosePath,
  34. Line,
  35. HorizontalLine,
  36. VerticalLine,
  37. Curve,
  38. SmoothCurve,
  39. QuadraticBezierCurve,
  40. SmoothQuadraticBezierCurve,
  41. EllipticalArc,
  42. Invalid,
  43. };
  44. struct PathInstruction {
  45. PathInstructionType type;
  46. bool absolute;
  47. Vector<float> data;
  48. };
  49. class PathDataParser final {
  50. public:
  51. PathDataParser(const String& source);
  52. ~PathDataParser() = default;
  53. Vector<PathInstruction> parse();
  54. private:
  55. void parse_drawto();
  56. void parse_moveto();
  57. void parse_closepath();
  58. void parse_lineto();
  59. void parse_horizontal_lineto();
  60. void parse_vertical_lineto();
  61. void parse_curveto();
  62. void parse_smooth_curveto();
  63. void parse_quadratic_bezier_curveto();
  64. void parse_smooth_quadratic_bezier_curveto();
  65. void parse_elliptical_arc();
  66. float parse_coordinate();
  67. Vector<float> parse_coordinate_pair();
  68. Vector<float> parse_coordinate_sequence();
  69. Vector<Vector<float>> parse_coordinate_pair_sequence();
  70. Vector<float> parse_coordinate_pair_double();
  71. Vector<float> parse_coordinate_pair_triplet();
  72. Vector<float> parse_elliptical_arg_argument();
  73. void parse_whitespace(bool must_match_once = false);
  74. void parse_comma_whitespace();
  75. float parse_fractional_constant();
  76. float parse_number();
  77. float parse_flag();
  78. bool match_whitespace() const;
  79. bool match_comma_whitespace() const;
  80. bool match_number() const;
  81. bool match(char c) const { return !done() && ch() == c; }
  82. bool done() const { return m_cursor >= m_source.length(); }
  83. char ch() const { return m_source[m_cursor]; }
  84. char consume() { return m_source[m_cursor++]; }
  85. String m_source;
  86. size_t m_cursor { 0 };
  87. Vector<PathInstruction> m_instructions;
  88. };
  89. class HTMLPathElement final
  90. : public HTMLElement
  91. , public SvgGraphicElement {
  92. public:
  93. HTMLPathElement(Document&, const FlyString& tag_name);
  94. virtual ~HTMLPathElement() override = default;
  95. virtual void parse_attribute(const FlyString& name, const String& value) override;
  96. virtual void paint(const SvgPaintingContext&, Gfx::Painter& painter) override;
  97. private:
  98. Vector<PathInstruction> m_instructions;
  99. };
  100. template<>
  101. inline bool is<HTMLPathElement>(const Node& node)
  102. {
  103. return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::path;
  104. }
  105. }