AttributeParser.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. enum class FillRule {
  108. Nonzero,
  109. Evenodd
  110. };
  111. enum class TextAnchor {
  112. Start,
  113. Middle,
  114. End
  115. };
  116. class AttributeParser final {
  117. public:
  118. ~AttributeParser() = default;
  119. static Optional<float> parse_coordinate(StringView input);
  120. static Optional<float> parse_length(StringView input);
  121. static Optional<NumberPercentage> parse_number_percentage(StringView input);
  122. static Optional<float> parse_positive_length(StringView input);
  123. static Vector<Gfx::FloatPoint> parse_points(StringView input);
  124. static Vector<PathInstruction> parse_path_data(StringView input);
  125. static Optional<Vector<Transform>> parse_transform(StringView input);
  126. static Optional<PreserveAspectRatio> parse_preserve_aspect_ratio(StringView input);
  127. static Optional<GradientUnits> parse_gradient_units(StringView input);
  128. private:
  129. AttributeParser(StringView source);
  130. void parse_drawto();
  131. void parse_moveto();
  132. void parse_closepath();
  133. void parse_lineto();
  134. void parse_horizontal_lineto();
  135. void parse_vertical_lineto();
  136. void parse_curveto();
  137. void parse_smooth_curveto();
  138. void parse_quadratic_bezier_curveto();
  139. void parse_smooth_quadratic_bezier_curveto();
  140. void parse_elliptical_arc();
  141. Optional<Vector<Transform>> parse_transform();
  142. float parse_length();
  143. float parse_coordinate();
  144. Vector<float> parse_coordinate_pair();
  145. Vector<float> parse_coordinate_sequence();
  146. Vector<Vector<float>> parse_coordinate_pair_sequence();
  147. Vector<float> parse_coordinate_pair_double();
  148. Vector<float> parse_coordinate_pair_triplet();
  149. Vector<float> parse_elliptical_arg_argument();
  150. void parse_whitespace(bool must_match_once = false);
  151. void parse_comma_whitespace();
  152. float parse_number();
  153. float parse_nonnegative_number();
  154. float parse_flag();
  155. // -1 if negative, +1 otherwise
  156. int parse_sign();
  157. bool match_whitespace() const;
  158. bool match_comma_whitespace() const;
  159. bool match_coordinate() const;
  160. bool match_length() const;
  161. bool match_number() const;
  162. bool match(char c) const { return !done() && ch() == c; }
  163. bool done() const { return m_lexer.is_eof(); }
  164. char ch() const { return m_lexer.peek(); }
  165. char consume() { return m_lexer.consume(); }
  166. GenericLexer m_lexer;
  167. Vector<PathInstruction> m_instructions;
  168. };
  169. }