AttributeParser.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/GenericLexer.h>
  9. #include <AK/Variant.h>
  10. #include <AK/Vector.h>
  11. #include <LibGfx/Point.h>
  12. namespace Web::SVG {
  13. enum class PathInstructionType {
  14. Move,
  15. ClosePath,
  16. Line,
  17. HorizontalLine,
  18. VerticalLine,
  19. Curve,
  20. SmoothCurve,
  21. QuadraticBezierCurve,
  22. SmoothQuadraticBezierCurve,
  23. EllipticalArc,
  24. Invalid,
  25. };
  26. struct PathInstruction {
  27. PathInstructionType type;
  28. bool absolute;
  29. Vector<float> data;
  30. };
  31. struct Transform {
  32. struct Translate {
  33. float x;
  34. float y;
  35. };
  36. struct Scale {
  37. float x;
  38. float y;
  39. };
  40. struct Rotate {
  41. float a;
  42. float x;
  43. float y;
  44. };
  45. struct SkewX {
  46. float a;
  47. };
  48. struct SkewY {
  49. float a;
  50. };
  51. struct Matrix {
  52. float a;
  53. float b;
  54. float c;
  55. float d;
  56. float e;
  57. float f;
  58. };
  59. using Operation = Variant<Translate, Scale, Rotate, SkewX, SkewY, Matrix>;
  60. Operation operation;
  61. };
  62. struct PreserveAspectRatio {
  63. enum class Align {
  64. None,
  65. xMinYMin,
  66. xMidYMin,
  67. xMaxYMin,
  68. xMinYMid,
  69. xMidYMid,
  70. xMaxYMid,
  71. xMinYMax,
  72. xMidYMax,
  73. xMaxYMax
  74. };
  75. enum class MeetOrSlice {
  76. Meet,
  77. Slice
  78. };
  79. Align align { Align::xMidYMid };
  80. MeetOrSlice meet_or_slice { MeetOrSlice::Meet };
  81. };
  82. class AttributeParser final {
  83. public:
  84. ~AttributeParser() = default;
  85. static Optional<float> parse_coordinate(StringView input);
  86. static Optional<float> parse_length(StringView input);
  87. static Optional<float> parse_positive_length(StringView input);
  88. static Vector<Gfx::FloatPoint> parse_points(StringView input);
  89. static Vector<PathInstruction> parse_path_data(StringView input);
  90. static Optional<Vector<Transform>> parse_transform(StringView input);
  91. static Optional<PreserveAspectRatio> parse_preserve_aspect_ratio(StringView input);
  92. private:
  93. AttributeParser(StringView source);
  94. void parse_drawto();
  95. void parse_moveto();
  96. void parse_closepath();
  97. void parse_lineto();
  98. void parse_horizontal_lineto();
  99. void parse_vertical_lineto();
  100. void parse_curveto();
  101. void parse_smooth_curveto();
  102. void parse_quadratic_bezier_curveto();
  103. void parse_smooth_quadratic_bezier_curveto();
  104. void parse_elliptical_arc();
  105. Optional<Vector<Transform>> parse_transform();
  106. float parse_length();
  107. float parse_coordinate();
  108. Vector<float> parse_coordinate_pair();
  109. Vector<float> parse_coordinate_sequence();
  110. Vector<Vector<float>> parse_coordinate_pair_sequence();
  111. Vector<float> parse_coordinate_pair_double();
  112. Vector<float> parse_coordinate_pair_triplet();
  113. Vector<float> parse_elliptical_arg_argument();
  114. void parse_whitespace(bool must_match_once = false);
  115. void parse_comma_whitespace();
  116. float parse_number();
  117. float parse_nonnegative_number();
  118. float parse_flag();
  119. // -1 if negative, +1 otherwise
  120. int parse_sign();
  121. bool match_whitespace() const;
  122. bool match_comma_whitespace() const;
  123. bool match_coordinate() const;
  124. bool match_length() const;
  125. bool match(char c) const { return !done() && ch() == c; }
  126. bool done() const { return m_lexer.is_eof(); }
  127. char ch() const { return m_lexer.peek(); }
  128. char consume() { return m_lexer.consume(); }
  129. GenericLexer m_lexer;
  130. Vector<PathInstruction> m_instructions;
  131. };
  132. }