AttributeParser.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "AttributeParser.h"
  9. #include <AK/FloatingPointStringConversions.h>
  10. #include <AK/GenericShorthands.h>
  11. #include <AK/StringBuilder.h>
  12. #include <ctype.h>
  13. namespace Web::SVG {
  14. AttributeParser::AttributeParser(StringView source)
  15. : m_lexer(source)
  16. {
  17. }
  18. Optional<Vector<Transform>> AttributeParser::parse_transform(StringView input)
  19. {
  20. AttributeParser parser { input };
  21. return parser.parse_transform();
  22. }
  23. Vector<PathInstruction> AttributeParser::parse_path_data(StringView input)
  24. {
  25. AttributeParser parser { input };
  26. parser.parse_whitespace();
  27. while (!parser.done())
  28. parser.parse_drawto();
  29. if (!parser.m_instructions.is_empty() && parser.m_instructions[0].type != PathInstructionType::Move) {
  30. // Invalid. "A path data segment (if there is one) must begin with a "moveto" command."
  31. return {};
  32. }
  33. return parser.m_instructions;
  34. }
  35. Optional<float> AttributeParser::parse_coordinate(StringView input)
  36. {
  37. AttributeParser parser { input };
  38. parser.parse_whitespace();
  39. if (parser.match_coordinate()) {
  40. float result = parser.parse_coordinate();
  41. parser.parse_whitespace();
  42. if (parser.done())
  43. return result;
  44. }
  45. return {};
  46. }
  47. Optional<float> AttributeParser::parse_length(StringView input)
  48. {
  49. AttributeParser parser { input };
  50. parser.parse_whitespace();
  51. if (parser.match_coordinate()) {
  52. float result = parser.parse_length();
  53. parser.parse_whitespace();
  54. if (parser.done())
  55. return result;
  56. }
  57. return {};
  58. }
  59. Optional<float> AttributeParser::parse_positive_length(StringView input)
  60. {
  61. // FIXME: Where this is used, the spec usually (always?) says "A negative value is an error (see Error processing)."
  62. // So, implement error processing! Maybe this should return ErrorOr.
  63. auto result = parse_length(input);
  64. if (result.has_value() && result.value() < 0)
  65. result.clear();
  66. return result;
  67. }
  68. Vector<Gfx::FloatPoint> AttributeParser::parse_points(StringView input)
  69. {
  70. AttributeParser parser { input };
  71. parser.parse_whitespace();
  72. // FIXME: "If an odd number of coordinates is provided, then the element is in error, with the same user agent behavior
  73. // as occurs with an incorrectly specified ‘path’ element. In such error cases the user agent will drop the last,
  74. // odd coordinate and otherwise render the shape."
  75. // The parser currently doesn't notice that there is a missing coordinate, so make it notice!
  76. auto coordinate_pair_sequence = parser.parse_coordinate_pair_sequence();
  77. parser.parse_whitespace();
  78. if (!parser.done())
  79. return {};
  80. // FIXME: This is awkward. Can we return Gfx::FloatPoints from some of these parsing methods instead of Vector<float>?
  81. Vector<Gfx::FloatPoint> points;
  82. points.ensure_capacity(coordinate_pair_sequence.size());
  83. for (auto const& pair : coordinate_pair_sequence)
  84. points.empend(pair[0], pair[1]);
  85. return points;
  86. }
  87. void AttributeParser::parse_drawto()
  88. {
  89. if (match('M') || match('m')) {
  90. parse_moveto();
  91. } else if (match('Z') || match('z')) {
  92. parse_closepath();
  93. } else if (match('L') || match('l')) {
  94. parse_lineto();
  95. } else if (match('H') || match('h')) {
  96. parse_horizontal_lineto();
  97. } else if (match('V') || match('v')) {
  98. parse_vertical_lineto();
  99. } else if (match('C') || match('c')) {
  100. parse_curveto();
  101. } else if (match('S') || match('s')) {
  102. parse_smooth_curveto();
  103. } else if (match('Q') || match('q')) {
  104. parse_quadratic_bezier_curveto();
  105. } else if (match('T') || match('t')) {
  106. parse_smooth_quadratic_bezier_curveto();
  107. } else if (match('A') || match('a')) {
  108. parse_elliptical_arc();
  109. } else {
  110. dbgln("AttributeParser::parse_drawto failed to match: '{}'", ch());
  111. TODO();
  112. }
  113. }
  114. void AttributeParser::parse_moveto()
  115. {
  116. bool absolute = consume() == 'M';
  117. parse_whitespace();
  118. for (auto& pair : parse_coordinate_pair_sequence())
  119. m_instructions.append({ PathInstructionType::Move, absolute, pair });
  120. }
  121. void AttributeParser::parse_closepath()
  122. {
  123. bool absolute = consume() == 'Z';
  124. parse_whitespace();
  125. m_instructions.append({ PathInstructionType::ClosePath, absolute, {} });
  126. }
  127. void AttributeParser::parse_lineto()
  128. {
  129. bool absolute = consume() == 'L';
  130. parse_whitespace();
  131. for (auto& pair : parse_coordinate_pair_sequence())
  132. m_instructions.append({ PathInstructionType::Line, absolute, pair });
  133. }
  134. void AttributeParser::parse_horizontal_lineto()
  135. {
  136. bool absolute = consume() == 'H';
  137. parse_whitespace();
  138. m_instructions.append({ PathInstructionType::HorizontalLine, absolute, parse_coordinate_sequence() });
  139. }
  140. void AttributeParser::parse_vertical_lineto()
  141. {
  142. bool absolute = consume() == 'V';
  143. parse_whitespace();
  144. m_instructions.append({ PathInstructionType::VerticalLine, absolute, parse_coordinate_sequence() });
  145. }
  146. void AttributeParser::parse_curveto()
  147. {
  148. bool absolute = consume() == 'C';
  149. parse_whitespace();
  150. while (true) {
  151. m_instructions.append({ PathInstructionType::Curve, absolute, parse_coordinate_pair_triplet() });
  152. if (match_comma_whitespace())
  153. parse_comma_whitespace();
  154. if (!match_coordinate())
  155. break;
  156. }
  157. }
  158. void AttributeParser::parse_smooth_curveto()
  159. {
  160. bool absolute = consume() == 'S';
  161. parse_whitespace();
  162. while (true) {
  163. m_instructions.append({ PathInstructionType::SmoothCurve, absolute, parse_coordinate_pair_double() });
  164. if (match_comma_whitespace())
  165. parse_comma_whitespace();
  166. if (!match_coordinate())
  167. break;
  168. }
  169. }
  170. void AttributeParser::parse_quadratic_bezier_curveto()
  171. {
  172. bool absolute = consume() == 'Q';
  173. parse_whitespace();
  174. while (true) {
  175. m_instructions.append({ PathInstructionType::QuadraticBezierCurve, absolute, parse_coordinate_pair_double() });
  176. if (match_comma_whitespace())
  177. parse_comma_whitespace();
  178. if (!match_coordinate())
  179. break;
  180. }
  181. }
  182. void AttributeParser::parse_smooth_quadratic_bezier_curveto()
  183. {
  184. bool absolute = consume() == 'T';
  185. parse_whitespace();
  186. while (true) {
  187. m_instructions.append({ PathInstructionType::SmoothQuadraticBezierCurve, absolute, parse_coordinate_pair() });
  188. if (match_comma_whitespace())
  189. parse_comma_whitespace();
  190. if (!match_coordinate())
  191. break;
  192. }
  193. }
  194. void AttributeParser::parse_elliptical_arc()
  195. {
  196. bool absolute = consume() == 'A';
  197. parse_whitespace();
  198. while (true) {
  199. m_instructions.append({ PathInstructionType::EllipticalArc, absolute, parse_elliptical_arg_argument() });
  200. if (match_comma_whitespace())
  201. parse_comma_whitespace();
  202. if (!match_coordinate())
  203. break;
  204. }
  205. }
  206. float AttributeParser::parse_length()
  207. {
  208. // https://www.w3.org/TR/SVG11/types.html#DataTypeLength
  209. return parse_number();
  210. }
  211. float AttributeParser::parse_coordinate()
  212. {
  213. // https://www.w3.org/TR/SVG11/types.html#DataTypeCoordinate
  214. // coordinate ::= length
  215. return parse_length();
  216. }
  217. Vector<float> AttributeParser::parse_coordinate_pair()
  218. {
  219. Vector<float> coordinates;
  220. coordinates.append(parse_coordinate());
  221. if (match_comma_whitespace())
  222. parse_comma_whitespace();
  223. coordinates.append(parse_coordinate());
  224. return coordinates;
  225. }
  226. Vector<float> AttributeParser::parse_coordinate_sequence()
  227. {
  228. Vector<float> sequence;
  229. while (true) {
  230. sequence.append(parse_coordinate());
  231. if (match_comma_whitespace())
  232. parse_comma_whitespace();
  233. if (!match_comma_whitespace() && !match_coordinate())
  234. break;
  235. }
  236. return sequence;
  237. }
  238. Vector<Vector<float>> AttributeParser::parse_coordinate_pair_sequence()
  239. {
  240. Vector<Vector<float>> sequence;
  241. while (true) {
  242. sequence.append(parse_coordinate_pair());
  243. if (match_comma_whitespace())
  244. parse_comma_whitespace();
  245. if (!match_comma_whitespace() && !match_coordinate())
  246. break;
  247. }
  248. return sequence;
  249. }
  250. Vector<float> AttributeParser::parse_coordinate_pair_double()
  251. {
  252. Vector<float> coordinates;
  253. coordinates.extend(parse_coordinate_pair());
  254. if (match_comma_whitespace())
  255. parse_comma_whitespace();
  256. coordinates.extend(parse_coordinate_pair());
  257. return coordinates;
  258. }
  259. Vector<float> AttributeParser::parse_coordinate_pair_triplet()
  260. {
  261. Vector<float> coordinates;
  262. coordinates.extend(parse_coordinate_pair());
  263. if (match_comma_whitespace())
  264. parse_comma_whitespace();
  265. coordinates.extend(parse_coordinate_pair());
  266. if (match_comma_whitespace())
  267. parse_comma_whitespace();
  268. coordinates.extend(parse_coordinate_pair());
  269. return coordinates;
  270. }
  271. Vector<float> AttributeParser::parse_elliptical_arg_argument()
  272. {
  273. Vector<float> numbers;
  274. numbers.append(parse_nonnegative_number());
  275. if (match_comma_whitespace())
  276. parse_comma_whitespace();
  277. numbers.append(parse_nonnegative_number());
  278. if (match_comma_whitespace())
  279. parse_comma_whitespace();
  280. numbers.append(parse_number());
  281. parse_comma_whitespace();
  282. numbers.append(parse_flag());
  283. if (match_comma_whitespace())
  284. parse_comma_whitespace();
  285. numbers.append(parse_flag());
  286. if (match_comma_whitespace())
  287. parse_comma_whitespace();
  288. numbers.extend(parse_coordinate_pair());
  289. return numbers;
  290. }
  291. void AttributeParser::parse_whitespace(bool must_match_once)
  292. {
  293. bool matched = false;
  294. while (!done() && match_whitespace()) {
  295. consume();
  296. matched = true;
  297. }
  298. VERIFY(!must_match_once || matched);
  299. }
  300. void AttributeParser::parse_comma_whitespace()
  301. {
  302. if (match(',')) {
  303. consume();
  304. parse_whitespace();
  305. } else {
  306. parse_whitespace(1);
  307. if (match(','))
  308. consume();
  309. parse_whitespace();
  310. }
  311. }
  312. // https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
  313. float AttributeParser::parse_number()
  314. {
  315. auto sign = parse_sign();
  316. return sign * parse_nonnegative_number();
  317. }
  318. // https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
  319. float AttributeParser::parse_nonnegative_number()
  320. {
  321. // NOTE: The grammar is almost a floating point except we cannot have a sign
  322. // at the start. That condition should have been checked by the caller.
  323. VERIFY(!match('+') && !match('-'));
  324. auto remaining_source_text = m_lexer.remaining();
  325. char const* start = remaining_source_text.characters_without_null_termination();
  326. auto maybe_float = parse_first_floating_point<float>(start, start + remaining_source_text.length());
  327. VERIFY(maybe_float.parsed_value());
  328. m_lexer.ignore(maybe_float.end_ptr - start);
  329. return maybe_float.value;
  330. }
  331. float AttributeParser::parse_flag()
  332. {
  333. if (!match('0') && !match('1'))
  334. VERIFY_NOT_REACHED();
  335. return consume() - '0';
  336. }
  337. int AttributeParser::parse_sign()
  338. {
  339. if (match('-')) {
  340. consume();
  341. return -1;
  342. }
  343. if (match('+'))
  344. consume();
  345. return 1;
  346. }
  347. static bool whitespace(char c)
  348. {
  349. // wsp:
  350. // Either a U+000A LINE FEED, U+000D CARRIAGE RETURN, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
  351. return AK::first_is_one_of(c, '\n', '\r', '\t', '\f', ' ');
  352. }
  353. // https://svgwg.org/svg2-draft/coords.html#PreserveAspectRatioAttribute
  354. Optional<PreserveAspectRatio> AttributeParser::parse_preserve_aspect_ratio(StringView input)
  355. {
  356. // <align> <meetOrSlice>?
  357. GenericLexer lexer { input };
  358. lexer.ignore_while(whitespace);
  359. auto align_string = lexer.consume_until(whitespace);
  360. if (align_string.is_empty())
  361. return {};
  362. lexer.ignore_while(whitespace);
  363. auto meet_or_slice_string = lexer.consume_until(whitespace);
  364. // <align> =
  365. // none
  366. // | xMinYMin | xMidYMin | xMaxYMin
  367. // | xMinYMid | xMidYMid | xMaxYMid
  368. // | xMinYMax | xMidYMax | xMaxYMax
  369. auto align = [&]() -> Optional<PreserveAspectRatio::Align> {
  370. if (align_string == "none"sv)
  371. return PreserveAspectRatio::Align::None;
  372. if (align_string == "xMinYMin"sv)
  373. return PreserveAspectRatio::Align::xMinYMin;
  374. if (align_string == "xMidYMin"sv)
  375. return PreserveAspectRatio::Align::xMidYMin;
  376. if (align_string == "xMaxYMin"sv)
  377. return PreserveAspectRatio::Align::xMaxYMin;
  378. if (align_string == "xMinYMid"sv)
  379. return PreserveAspectRatio::Align::xMinYMid;
  380. if (align_string == "xMidYMid"sv)
  381. return PreserveAspectRatio::Align::xMidYMid;
  382. if (align_string == "xMaxYMid"sv)
  383. return PreserveAspectRatio::Align::xMaxYMid;
  384. if (align_string == "xMinYMax"sv)
  385. return PreserveAspectRatio::Align::xMinYMax;
  386. if (align_string == "xMidYMax"sv)
  387. return PreserveAspectRatio::Align::xMidYMax;
  388. if (align_string == "xMaxYMax"sv)
  389. return PreserveAspectRatio::Align::xMaxYMax;
  390. return {};
  391. }();
  392. if (!align.has_value())
  393. return {};
  394. // <meetOrSlice> = meet | slice
  395. auto meet_or_slice = [&]() -> Optional<PreserveAspectRatio::MeetOrSlice> {
  396. if (meet_or_slice_string.is_empty() || meet_or_slice_string == "meet"sv)
  397. return PreserveAspectRatio::MeetOrSlice::Meet;
  398. if (meet_or_slice_string == "slice"sv)
  399. return PreserveAspectRatio::MeetOrSlice::Slice;
  400. return {};
  401. }();
  402. if (!meet_or_slice.has_value())
  403. return {};
  404. return PreserveAspectRatio { *align, *meet_or_slice };
  405. }
  406. // https://drafts.csswg.org/css-transforms/#svg-syntax
  407. Optional<Vector<Transform>> AttributeParser::parse_transform()
  408. {
  409. auto consume_whitespace = [&] {
  410. m_lexer.ignore_while(whitespace);
  411. };
  412. auto consume_comma_whitespace = [&] {
  413. consume_whitespace();
  414. m_lexer.consume_specific(',');
  415. consume_whitespace();
  416. };
  417. // FIXME: AttributeParser currently does not handle invalid parses in most cases (e.g. parse_number()) and just crashes.
  418. auto parse_optional_number = [&](float default_value = 0.0f) {
  419. consume_comma_whitespace();
  420. if (m_lexer.next_is(isdigit))
  421. return parse_number();
  422. return default_value;
  423. };
  424. auto parse_function = [&](auto body) -> Optional<Transform> {
  425. consume_whitespace();
  426. if (!m_lexer.consume_specific('('))
  427. return {};
  428. consume_whitespace();
  429. Transform transform { .operation = Transform::Operation { body() } };
  430. consume_whitespace();
  431. if (m_lexer.consume_specific(')'))
  432. return transform;
  433. return {};
  434. };
  435. // NOTE: This looks very similar to the CSS transform but the syntax is not compatible.
  436. Vector<Transform> transform_list;
  437. consume_whitespace();
  438. while (!done()) {
  439. Optional<Transform> maybe_transform;
  440. if (m_lexer.consume_specific("translate"sv)) {
  441. maybe_transform = parse_function([&] {
  442. Transform::Translate translate {};
  443. translate.x = parse_number();
  444. translate.y = parse_optional_number();
  445. return translate;
  446. });
  447. } else if (m_lexer.consume_specific("scale"sv)) {
  448. maybe_transform = parse_function([&] {
  449. Transform::Scale scale {};
  450. scale.x = parse_number();
  451. scale.y = parse_optional_number(scale.x);
  452. return scale;
  453. });
  454. } else if (m_lexer.consume_specific("rotate"sv)) {
  455. maybe_transform = parse_function([&] {
  456. Transform::Rotate rotate {};
  457. rotate.a = parse_number();
  458. rotate.x = parse_optional_number();
  459. rotate.y = parse_optional_number();
  460. return rotate;
  461. });
  462. } else if (m_lexer.consume_specific("skewX"sv)) {
  463. maybe_transform = parse_function([&] {
  464. Transform::SkewX skew_x {};
  465. skew_x.a = parse_number();
  466. return skew_x;
  467. });
  468. } else if (m_lexer.consume_specific("skewY"sv)) {
  469. maybe_transform = parse_function([&] {
  470. Transform::SkewY skew_y {};
  471. skew_y.a = parse_number();
  472. return skew_y;
  473. });
  474. } else if (m_lexer.consume_specific("matrix"sv)) {
  475. maybe_transform = parse_function([&] {
  476. Transform::Matrix matrix;
  477. matrix.a = parse_number();
  478. consume_comma_whitespace();
  479. matrix.b = parse_number();
  480. consume_comma_whitespace();
  481. matrix.c = parse_number();
  482. consume_comma_whitespace();
  483. matrix.d = parse_number();
  484. consume_comma_whitespace();
  485. matrix.e = parse_number();
  486. consume_comma_whitespace();
  487. matrix.f = parse_number();
  488. return matrix;
  489. });
  490. }
  491. if (maybe_transform.has_value())
  492. transform_list.append(*maybe_transform);
  493. else
  494. return {};
  495. consume_comma_whitespace();
  496. }
  497. return transform_list;
  498. }
  499. bool AttributeParser::match_whitespace() const
  500. {
  501. if (done())
  502. return false;
  503. char c = ch();
  504. return c == 0x9 || c == 0x20 || c == 0xa || c == 0xc || c == 0xd;
  505. }
  506. bool AttributeParser::match_comma_whitespace() const
  507. {
  508. return match_whitespace() || match(',');
  509. }
  510. bool AttributeParser::match_coordinate() const
  511. {
  512. return match_length();
  513. }
  514. bool AttributeParser::match_length() const
  515. {
  516. return !done() && (isdigit(ch()) || ch() == '-' || ch() == '+' || ch() == '.');
  517. }
  518. }