AttributeParser.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. enum class GradientUnits {
  83. ObjectBoundingBox,
  84. UserSpaceOnUse
  85. };
  86. class NumberPercentage {
  87. public:
  88. NumberPercentage(float value, bool is_percentage)
  89. : m_value(is_percentage ? value / 100 : value)
  90. , m_is_percentage(is_percentage)
  91. {
  92. }
  93. static NumberPercentage create_percentage(float value)
  94. {
  95. return NumberPercentage(value, true);
  96. }
  97. static NumberPercentage create_number(float value)
  98. {
  99. return NumberPercentage(value, false);
  100. }
  101. float resolve_relative_to(float length) const;
  102. float value() const { return m_value; }
  103. private:
  104. float m_value;
  105. bool m_is_percentage { false };
  106. };
  107. class AttributeParser final {
  108. public:
  109. ~AttributeParser() = default;
  110. static Optional<float> parse_coordinate(StringView input);
  111. static Optional<float> parse_length(StringView input);
  112. static Optional<NumberPercentage> parse_number_percentage(StringView input);
  113. static Optional<float> parse_positive_length(StringView input);
  114. static Vector<Gfx::FloatPoint> parse_points(StringView input);
  115. static Vector<PathInstruction> parse_path_data(StringView input);
  116. static Optional<Vector<Transform>> parse_transform(StringView input);
  117. static Optional<PreserveAspectRatio> parse_preserve_aspect_ratio(StringView input);
  118. static Optional<GradientUnits> parse_gradient_units(StringView input);
  119. private:
  120. AttributeParser(StringView source);
  121. void parse_drawto();
  122. void parse_moveto();
  123. void parse_closepath();
  124. void parse_lineto();
  125. void parse_horizontal_lineto();
  126. void parse_vertical_lineto();
  127. void parse_curveto();
  128. void parse_smooth_curveto();
  129. void parse_quadratic_bezier_curveto();
  130. void parse_smooth_quadratic_bezier_curveto();
  131. void parse_elliptical_arc();
  132. Optional<Vector<Transform>> parse_transform();
  133. float parse_length();
  134. float parse_coordinate();
  135. Vector<float> parse_coordinate_pair();
  136. Vector<float> parse_coordinate_sequence();
  137. Vector<Vector<float>> parse_coordinate_pair_sequence();
  138. Vector<float> parse_coordinate_pair_double();
  139. Vector<float> parse_coordinate_pair_triplet();
  140. Vector<float> parse_elliptical_arg_argument();
  141. void parse_whitespace(bool must_match_once = false);
  142. void parse_comma_whitespace();
  143. float parse_number();
  144. float parse_nonnegative_number();
  145. float parse_flag();
  146. // -1 if negative, +1 otherwise
  147. int parse_sign();
  148. bool match_whitespace() const;
  149. bool match_comma_whitespace() const;
  150. bool match_coordinate() const;
  151. bool match_length() const;
  152. bool match_number() const;
  153. bool match(char c) const { return !done() && ch() == c; }
  154. bool done() const { return m_lexer.is_eof(); }
  155. char ch() const { return m_lexer.peek(); }
  156. char consume() { return m_lexer.consume(); }
  157. GenericLexer m_lexer;
  158. Vector<PathInstruction> m_instructions;
  159. };
  160. }