AttributeParser.cpp 19 KB

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