AttributeParser.h 4.7 KB

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