AttributeParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #include "AttributeParser.h"
  8. #include <AK/FloatingPointStringConversions.h>
  9. #include <AK/StringBuilder.h>
  10. #include <ctype.h>
  11. namespace Web::SVG {
  12. AttributeParser::AttributeParser(StringView source)
  13. : m_source(move(source))
  14. {
  15. }
  16. Vector<PathInstruction> AttributeParser::parse_path_data(StringView input)
  17. {
  18. AttributeParser parser { input };
  19. parser.parse_whitespace();
  20. while (!parser.done())
  21. parser.parse_drawto();
  22. if (!parser.m_instructions.is_empty() && parser.m_instructions[0].type != PathInstructionType::Move) {
  23. // Invalid. "A path data segment (if there is one) must begin with a "moveto" command."
  24. return {};
  25. }
  26. return parser.m_instructions;
  27. }
  28. Optional<float> AttributeParser::parse_coordinate(StringView input)
  29. {
  30. AttributeParser parser { input };
  31. parser.parse_whitespace();
  32. if (parser.match_coordinate()) {
  33. float result = parser.parse_coordinate();
  34. parser.parse_whitespace();
  35. if (parser.done())
  36. return result;
  37. }
  38. return {};
  39. }
  40. Optional<float> AttributeParser::parse_length(StringView input)
  41. {
  42. AttributeParser parser { input };
  43. parser.parse_whitespace();
  44. if (parser.match_coordinate()) {
  45. float result = parser.parse_length();
  46. parser.parse_whitespace();
  47. if (parser.done())
  48. return result;
  49. }
  50. return {};
  51. }
  52. Optional<float> AttributeParser::parse_positive_length(StringView input)
  53. {
  54. // FIXME: Where this is used, the spec usually (always?) says "A negative value is an error (see Error processing)."
  55. // So, implement error processing! Maybe this should return ErrorOr.
  56. auto result = parse_length(input);
  57. if (result.has_value() && result.value() < 0)
  58. result.clear();
  59. return result;
  60. }
  61. Vector<Gfx::FloatPoint> AttributeParser::parse_points(StringView input)
  62. {
  63. AttributeParser parser { input };
  64. parser.parse_whitespace();
  65. // FIXME: "If an odd number of coordinates is provided, then the element is in error, with the same user agent behavior
  66. // as occurs with an incorrectly specified ‘path’ element. In such error cases the user agent will drop the last,
  67. // odd coordinate and otherwise render the shape."
  68. // The parser currently doesn't notice that there is a missing coordinate, so make it notice!
  69. auto coordinate_pair_sequence = parser.parse_coordinate_pair_sequence();
  70. parser.parse_whitespace();
  71. if (!parser.done())
  72. return {};
  73. // FIXME: This is awkward. Can we return Gfx::FloatPoints from some of these parsing methods instead of Vector<float>?
  74. Vector<Gfx::FloatPoint> points;
  75. points.ensure_capacity(coordinate_pair_sequence.size());
  76. for (auto const& pair : coordinate_pair_sequence)
  77. points.empend(pair[0], pair[1]);
  78. return points;
  79. }
  80. void AttributeParser::parse_drawto()
  81. {
  82. if (match('M') || match('m')) {
  83. parse_moveto();
  84. } else if (match('Z') || match('z')) {
  85. parse_closepath();
  86. } else if (match('L') || match('l')) {
  87. parse_lineto();
  88. } else if (match('H') || match('h')) {
  89. parse_horizontal_lineto();
  90. } else if (match('V') || match('v')) {
  91. parse_vertical_lineto();
  92. } else if (match('C') || match('c')) {
  93. parse_curveto();
  94. } else if (match('S') || match('s')) {
  95. parse_smooth_curveto();
  96. } else if (match('Q') || match('q')) {
  97. parse_quadratic_bezier_curveto();
  98. } else if (match('T') || match('t')) {
  99. parse_smooth_quadratic_bezier_curveto();
  100. } else if (match('A') || match('a')) {
  101. parse_elliptical_arc();
  102. } else {
  103. dbgln("AttributeParser::parse_drawto failed to match: '{}'", ch());
  104. TODO();
  105. }
  106. }
  107. void AttributeParser::parse_moveto()
  108. {
  109. bool absolute = consume() == 'M';
  110. parse_whitespace();
  111. for (auto& pair : parse_coordinate_pair_sequence())
  112. m_instructions.append({ PathInstructionType::Move, absolute, pair });
  113. }
  114. void AttributeParser::parse_closepath()
  115. {
  116. bool absolute = consume() == 'Z';
  117. parse_whitespace();
  118. m_instructions.append({ PathInstructionType::ClosePath, absolute, {} });
  119. }
  120. void AttributeParser::parse_lineto()
  121. {
  122. bool absolute = consume() == 'L';
  123. parse_whitespace();
  124. for (auto& pair : parse_coordinate_pair_sequence())
  125. m_instructions.append({ PathInstructionType::Line, absolute, pair });
  126. }
  127. void AttributeParser::parse_horizontal_lineto()
  128. {
  129. bool absolute = consume() == 'H';
  130. parse_whitespace();
  131. m_instructions.append({ PathInstructionType::HorizontalLine, absolute, parse_coordinate_sequence() });
  132. }
  133. void AttributeParser::parse_vertical_lineto()
  134. {
  135. bool absolute = consume() == 'V';
  136. parse_whitespace();
  137. m_instructions.append({ PathInstructionType::VerticalLine, absolute, parse_coordinate_sequence() });
  138. }
  139. void AttributeParser::parse_curveto()
  140. {
  141. bool absolute = consume() == 'C';
  142. parse_whitespace();
  143. while (true) {
  144. m_instructions.append({ PathInstructionType::Curve, absolute, parse_coordinate_pair_triplet() });
  145. if (match_comma_whitespace())
  146. parse_comma_whitespace();
  147. if (!match_coordinate())
  148. break;
  149. }
  150. }
  151. void AttributeParser::parse_smooth_curveto()
  152. {
  153. bool absolute = consume() == 'S';
  154. parse_whitespace();
  155. while (true) {
  156. m_instructions.append({ PathInstructionType::SmoothCurve, absolute, parse_coordinate_pair_double() });
  157. if (match_comma_whitespace())
  158. parse_comma_whitespace();
  159. if (!match_coordinate())
  160. break;
  161. }
  162. }
  163. void AttributeParser::parse_quadratic_bezier_curveto()
  164. {
  165. bool absolute = consume() == 'Q';
  166. parse_whitespace();
  167. while (true) {
  168. m_instructions.append({ PathInstructionType::QuadraticBezierCurve, absolute, parse_coordinate_pair_double() });
  169. if (match_comma_whitespace())
  170. parse_comma_whitespace();
  171. if (!match_coordinate())
  172. break;
  173. }
  174. }
  175. void AttributeParser::parse_smooth_quadratic_bezier_curveto()
  176. {
  177. bool absolute = consume() == 'T';
  178. parse_whitespace();
  179. while (true) {
  180. m_instructions.append({ PathInstructionType::SmoothQuadraticBezierCurve, absolute, parse_coordinate_pair() });
  181. if (match_comma_whitespace())
  182. parse_comma_whitespace();
  183. if (!match_coordinate())
  184. break;
  185. }
  186. }
  187. void AttributeParser::parse_elliptical_arc()
  188. {
  189. bool absolute = consume() == 'A';
  190. parse_whitespace();
  191. while (true) {
  192. m_instructions.append({ PathInstructionType::EllipticalArc, absolute, parse_elliptical_arg_argument() });
  193. if (match_comma_whitespace())
  194. parse_comma_whitespace();
  195. if (!match_coordinate())
  196. break;
  197. }
  198. }
  199. float AttributeParser::parse_length()
  200. {
  201. // https://www.w3.org/TR/SVG11/types.html#DataTypeLength
  202. return parse_number();
  203. }
  204. float AttributeParser::parse_coordinate()
  205. {
  206. // https://www.w3.org/TR/SVG11/types.html#DataTypeCoordinate
  207. // coordinate ::= length
  208. return parse_length();
  209. }
  210. Vector<float> AttributeParser::parse_coordinate_pair()
  211. {
  212. Vector<float> coordinates;
  213. coordinates.append(parse_coordinate());
  214. if (match_comma_whitespace())
  215. parse_comma_whitespace();
  216. coordinates.append(parse_coordinate());
  217. return coordinates;
  218. }
  219. Vector<float> AttributeParser::parse_coordinate_sequence()
  220. {
  221. Vector<float> sequence;
  222. while (true) {
  223. sequence.append(parse_coordinate());
  224. if (match_comma_whitespace())
  225. parse_comma_whitespace();
  226. if (!match_comma_whitespace() && !match_coordinate())
  227. break;
  228. }
  229. return sequence;
  230. }
  231. Vector<Vector<float>> AttributeParser::parse_coordinate_pair_sequence()
  232. {
  233. Vector<Vector<float>> sequence;
  234. while (true) {
  235. sequence.append(parse_coordinate_pair());
  236. if (match_comma_whitespace())
  237. parse_comma_whitespace();
  238. if (!match_comma_whitespace() && !match_coordinate())
  239. break;
  240. }
  241. return sequence;
  242. }
  243. Vector<float> AttributeParser::parse_coordinate_pair_double()
  244. {
  245. Vector<float> coordinates;
  246. coordinates.extend(parse_coordinate_pair());
  247. if (match_comma_whitespace())
  248. parse_comma_whitespace();
  249. coordinates.extend(parse_coordinate_pair());
  250. return coordinates;
  251. }
  252. Vector<float> AttributeParser::parse_coordinate_pair_triplet()
  253. {
  254. Vector<float> coordinates;
  255. coordinates.extend(parse_coordinate_pair());
  256. if (match_comma_whitespace())
  257. parse_comma_whitespace();
  258. coordinates.extend(parse_coordinate_pair());
  259. if (match_comma_whitespace())
  260. parse_comma_whitespace();
  261. coordinates.extend(parse_coordinate_pair());
  262. return coordinates;
  263. }
  264. Vector<float> AttributeParser::parse_elliptical_arg_argument()
  265. {
  266. Vector<float> numbers;
  267. numbers.append(parse_nonnegative_number());
  268. if (match_comma_whitespace())
  269. parse_comma_whitespace();
  270. numbers.append(parse_nonnegative_number());
  271. if (match_comma_whitespace())
  272. parse_comma_whitespace();
  273. numbers.append(parse_number());
  274. parse_comma_whitespace();
  275. numbers.append(parse_flag());
  276. if (match_comma_whitespace())
  277. parse_comma_whitespace();
  278. numbers.append(parse_flag());
  279. if (match_comma_whitespace())
  280. parse_comma_whitespace();
  281. numbers.extend(parse_coordinate_pair());
  282. return numbers;
  283. }
  284. void AttributeParser::parse_whitespace(bool must_match_once)
  285. {
  286. bool matched = false;
  287. while (!done() && match_whitespace()) {
  288. consume();
  289. matched = true;
  290. }
  291. VERIFY(!must_match_once || matched);
  292. }
  293. void AttributeParser::parse_comma_whitespace()
  294. {
  295. if (match(',')) {
  296. consume();
  297. parse_whitespace();
  298. } else {
  299. parse_whitespace(1);
  300. if (match(','))
  301. consume();
  302. parse_whitespace();
  303. }
  304. }
  305. // https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
  306. float AttributeParser::parse_number()
  307. {
  308. auto sign = parse_sign();
  309. return sign * parse_nonnegative_number();
  310. }
  311. // https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
  312. float AttributeParser::parse_nonnegative_number()
  313. {
  314. // NOTE: The grammar is almost a floating point except we cannot have a sign
  315. // at the start. That condition should have been checked by the caller.
  316. VERIFY(!match('+') && !match('-'));
  317. auto remaining_source_text = m_source.substring_view(m_cursor);
  318. char const* start = remaining_source_text.characters_without_null_termination();
  319. auto maybe_float = parse_first_floating_point<float>(start, start + remaining_source_text.length());
  320. VERIFY(maybe_float.parsed_value());
  321. m_cursor += maybe_float.end_ptr - start;
  322. return maybe_float.value;
  323. }
  324. float AttributeParser::parse_flag()
  325. {
  326. if (!match('0') && !match('1'))
  327. VERIFY_NOT_REACHED();
  328. return consume() - '0';
  329. }
  330. int AttributeParser::parse_sign()
  331. {
  332. if (match('-')) {
  333. consume();
  334. return -1;
  335. }
  336. if (match('+'))
  337. consume();
  338. return 1;
  339. }
  340. bool AttributeParser::match_whitespace() const
  341. {
  342. if (done())
  343. return false;
  344. char c = ch();
  345. return c == 0x9 || c == 0x20 || c == 0xa || c == 0xc || c == 0xd;
  346. }
  347. bool AttributeParser::match_comma_whitespace() const
  348. {
  349. return match_whitespace() || match(',');
  350. }
  351. bool AttributeParser::match_coordinate() const
  352. {
  353. return match_length();
  354. }
  355. bool AttributeParser::match_length() const
  356. {
  357. return !done() && (isdigit(ch()) || ch() == '-' || ch() == '+' || ch() == '.');
  358. }
  359. }