AttributeParser.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. for (auto coordinate : parse_coordinate_sequence())
  166. m_instructions.append({ PathInstructionType::HorizontalLine, absolute, { coordinate } });
  167. }
  168. void AttributeParser::parse_vertical_lineto()
  169. {
  170. bool absolute = consume() == 'V';
  171. parse_whitespace();
  172. for (auto coordinate : parse_coordinate_sequence())
  173. m_instructions.append({ PathInstructionType::VerticalLine, absolute, { coordinate } });
  174. }
  175. void AttributeParser::parse_curveto()
  176. {
  177. bool absolute = consume() == 'C';
  178. parse_whitespace();
  179. while (true) {
  180. m_instructions.append({ PathInstructionType::Curve, absolute, parse_coordinate_pair_triplet() });
  181. if (match_comma_whitespace())
  182. parse_comma_whitespace();
  183. if (!match_coordinate())
  184. break;
  185. }
  186. }
  187. void AttributeParser::parse_smooth_curveto()
  188. {
  189. bool absolute = consume() == 'S';
  190. parse_whitespace();
  191. while (true) {
  192. m_instructions.append({ PathInstructionType::SmoothCurve, absolute, parse_coordinate_pair_double() });
  193. if (match_comma_whitespace())
  194. parse_comma_whitespace();
  195. if (!match_coordinate())
  196. break;
  197. }
  198. }
  199. void AttributeParser::parse_quadratic_bezier_curveto()
  200. {
  201. bool absolute = consume() == 'Q';
  202. parse_whitespace();
  203. while (true) {
  204. m_instructions.append({ PathInstructionType::QuadraticBezierCurve, absolute, parse_coordinate_pair_double() });
  205. if (match_comma_whitespace())
  206. parse_comma_whitespace();
  207. if (!match_coordinate())
  208. break;
  209. }
  210. }
  211. void AttributeParser::parse_smooth_quadratic_bezier_curveto()
  212. {
  213. bool absolute = consume() == 'T';
  214. parse_whitespace();
  215. while (true) {
  216. m_instructions.append({ PathInstructionType::SmoothQuadraticBezierCurve, absolute, parse_coordinate_pair() });
  217. if (match_comma_whitespace())
  218. parse_comma_whitespace();
  219. if (!match_coordinate())
  220. break;
  221. }
  222. }
  223. void AttributeParser::parse_elliptical_arc()
  224. {
  225. bool absolute = consume() == 'A';
  226. parse_whitespace();
  227. while (true) {
  228. m_instructions.append({ PathInstructionType::EllipticalArc, absolute, parse_elliptical_arg_argument() });
  229. if (match_comma_whitespace())
  230. parse_comma_whitespace();
  231. if (!match_coordinate())
  232. break;
  233. }
  234. }
  235. float AttributeParser::parse_length()
  236. {
  237. // https://www.w3.org/TR/SVG11/types.html#DataTypeLength
  238. return parse_number();
  239. }
  240. float AttributeParser::parse_coordinate()
  241. {
  242. // https://www.w3.org/TR/SVG11/types.html#DataTypeCoordinate
  243. // coordinate ::= length
  244. return parse_length();
  245. }
  246. Vector<float> AttributeParser::parse_coordinate_pair()
  247. {
  248. Vector<float> coordinates;
  249. coordinates.append(parse_coordinate());
  250. if (match_comma_whitespace())
  251. parse_comma_whitespace();
  252. coordinates.append(parse_coordinate());
  253. return coordinates;
  254. }
  255. Vector<float> AttributeParser::parse_coordinate_sequence()
  256. {
  257. Vector<float> sequence;
  258. while (true) {
  259. sequence.append(parse_coordinate());
  260. if (match_comma_whitespace())
  261. parse_comma_whitespace();
  262. if (!match_comma_whitespace() && !match_coordinate())
  263. break;
  264. }
  265. return sequence;
  266. }
  267. Vector<Vector<float>> AttributeParser::parse_coordinate_pair_sequence()
  268. {
  269. Vector<Vector<float>> sequence;
  270. while (true) {
  271. sequence.append(parse_coordinate_pair());
  272. if (match_comma_whitespace())
  273. parse_comma_whitespace();
  274. if (!match_comma_whitespace() && !match_coordinate())
  275. break;
  276. }
  277. return sequence;
  278. }
  279. Vector<float> AttributeParser::parse_coordinate_pair_double()
  280. {
  281. Vector<float> coordinates;
  282. coordinates.extend(parse_coordinate_pair());
  283. if (match_comma_whitespace())
  284. parse_comma_whitespace();
  285. coordinates.extend(parse_coordinate_pair());
  286. return coordinates;
  287. }
  288. Vector<float> AttributeParser::parse_coordinate_pair_triplet()
  289. {
  290. Vector<float> coordinates;
  291. coordinates.extend(parse_coordinate_pair());
  292. if (match_comma_whitespace())
  293. parse_comma_whitespace();
  294. coordinates.extend(parse_coordinate_pair());
  295. if (match_comma_whitespace())
  296. parse_comma_whitespace();
  297. coordinates.extend(parse_coordinate_pair());
  298. return coordinates;
  299. }
  300. Vector<float> AttributeParser::parse_elliptical_arg_argument()
  301. {
  302. Vector<float> numbers;
  303. numbers.append(parse_number());
  304. if (match_comma_whitespace())
  305. parse_comma_whitespace();
  306. numbers.append(parse_number());
  307. if (match_comma_whitespace())
  308. parse_comma_whitespace();
  309. numbers.append(parse_number());
  310. parse_comma_whitespace();
  311. numbers.append(parse_flag());
  312. if (match_comma_whitespace())
  313. parse_comma_whitespace();
  314. numbers.append(parse_flag());
  315. if (match_comma_whitespace())
  316. parse_comma_whitespace();
  317. numbers.extend(parse_coordinate_pair());
  318. return numbers;
  319. }
  320. void AttributeParser::parse_whitespace(bool must_match_once)
  321. {
  322. bool matched = false;
  323. while (!done() && match_whitespace()) {
  324. consume();
  325. matched = true;
  326. }
  327. VERIFY(!must_match_once || matched);
  328. }
  329. void AttributeParser::parse_comma_whitespace()
  330. {
  331. if (match(',')) {
  332. consume();
  333. parse_whitespace();
  334. } else {
  335. parse_whitespace(1);
  336. if (match(','))
  337. consume();
  338. parse_whitespace();
  339. }
  340. }
  341. // https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
  342. float AttributeParser::parse_number()
  343. {
  344. auto sign = parse_sign();
  345. return sign * parse_nonnegative_number();
  346. }
  347. // https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
  348. float AttributeParser::parse_nonnegative_number()
  349. {
  350. // NOTE: The grammar is almost a floating point except we cannot have a sign
  351. // at the start. That condition should have been checked by the caller.
  352. VERIFY(!match('+') && !match('-'));
  353. auto remaining_source_text = m_lexer.remaining();
  354. char const* start = remaining_source_text.characters_without_null_termination();
  355. auto maybe_float = parse_first_floating_point<float>(start, start + remaining_source_text.length());
  356. VERIFY(maybe_float.parsed_value());
  357. m_lexer.ignore(maybe_float.end_ptr - start);
  358. return maybe_float.value;
  359. }
  360. float AttributeParser::parse_flag()
  361. {
  362. if (!match('0') && !match('1'))
  363. VERIFY_NOT_REACHED();
  364. return consume() - '0';
  365. }
  366. int AttributeParser::parse_sign()
  367. {
  368. if (match('-')) {
  369. consume();
  370. return -1;
  371. }
  372. if (match('+'))
  373. consume();
  374. return 1;
  375. }
  376. static bool whitespace(char c)
  377. {
  378. // wsp:
  379. // Either a U+000A LINE FEED, U+000D CARRIAGE RETURN, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
  380. return AK::first_is_one_of(c, '\n', '\r', '\t', '\f', ' ');
  381. }
  382. // https://svgwg.org/svg2-draft/coords.html#PreserveAspectRatioAttribute
  383. Optional<PreserveAspectRatio> AttributeParser::parse_preserve_aspect_ratio(StringView input)
  384. {
  385. // <align> <meetOrSlice>?
  386. GenericLexer lexer { input };
  387. lexer.ignore_while(whitespace);
  388. auto align_string = lexer.consume_until(whitespace);
  389. if (align_string.is_empty())
  390. return {};
  391. lexer.ignore_while(whitespace);
  392. auto meet_or_slice_string = lexer.consume_until(whitespace);
  393. // <align> =
  394. // none
  395. // | xMinYMin | xMidYMin | xMaxYMin
  396. // | xMinYMid | xMidYMid | xMaxYMid
  397. // | xMinYMax | xMidYMax | xMaxYMax
  398. auto align = [&]() -> Optional<PreserveAspectRatio::Align> {
  399. if (align_string == "none"sv)
  400. return PreserveAspectRatio::Align::None;
  401. if (align_string == "xMinYMin"sv)
  402. return PreserveAspectRatio::Align::xMinYMin;
  403. if (align_string == "xMidYMin"sv)
  404. return PreserveAspectRatio::Align::xMidYMin;
  405. if (align_string == "xMaxYMin"sv)
  406. return PreserveAspectRatio::Align::xMaxYMin;
  407. if (align_string == "xMinYMid"sv)
  408. return PreserveAspectRatio::Align::xMinYMid;
  409. if (align_string == "xMidYMid"sv)
  410. return PreserveAspectRatio::Align::xMidYMid;
  411. if (align_string == "xMaxYMid"sv)
  412. return PreserveAspectRatio::Align::xMaxYMid;
  413. if (align_string == "xMinYMax"sv)
  414. return PreserveAspectRatio::Align::xMinYMax;
  415. if (align_string == "xMidYMax"sv)
  416. return PreserveAspectRatio::Align::xMidYMax;
  417. if (align_string == "xMaxYMax"sv)
  418. return PreserveAspectRatio::Align::xMaxYMax;
  419. return {};
  420. }();
  421. if (!align.has_value())
  422. return {};
  423. // <meetOrSlice> = meet | slice
  424. auto meet_or_slice = [&]() -> Optional<PreserveAspectRatio::MeetOrSlice> {
  425. if (meet_or_slice_string.is_empty() || meet_or_slice_string == "meet"sv)
  426. return PreserveAspectRatio::MeetOrSlice::Meet;
  427. if (meet_or_slice_string == "slice"sv)
  428. return PreserveAspectRatio::MeetOrSlice::Slice;
  429. return {};
  430. }();
  431. if (!meet_or_slice.has_value())
  432. return {};
  433. return PreserveAspectRatio { *align, *meet_or_slice };
  434. }
  435. // https://svgwg.org/svg2-draft/pservers.html#LinearGradientElementGradientUnitsAttribute
  436. // https://drafts.fxtf.org/css-masking/#element-attrdef-mask-maskunits
  437. // https://drafts.fxtf.org/css-masking/#element-attrdef-mask-maskcontentunits
  438. Optional<SVGUnits> AttributeParser::parse_units(StringView input)
  439. {
  440. GenericLexer lexer { input };
  441. lexer.ignore_while(whitespace);
  442. auto gradient_units_string = lexer.consume_until(whitespace);
  443. if (gradient_units_string == "userSpaceOnUse"sv)
  444. return SVGUnits::UserSpaceOnUse;
  445. if (gradient_units_string == "objectBoundingBox"sv)
  446. return SVGUnits::ObjectBoundingBox;
  447. return {};
  448. }
  449. // https://svgwg.org/svg2-draft/pservers.html#RadialGradientElementSpreadMethodAttribute
  450. Optional<SpreadMethod> AttributeParser::parse_spread_method(StringView input)
  451. {
  452. GenericLexer lexer { input };
  453. lexer.ignore_while(whitespace);
  454. auto spread_method_string = lexer.consume_until(whitespace);
  455. if (spread_method_string == "pad"sv)
  456. return SpreadMethod::Pad;
  457. if (spread_method_string == "repeat"sv)
  458. return SpreadMethod::Repeat;
  459. if (spread_method_string == "reflect"sv)
  460. return SpreadMethod::Reflect;
  461. return {};
  462. }
  463. // https://drafts.csswg.org/css-transforms/#svg-syntax
  464. Optional<Vector<Transform>> AttributeParser::parse_transform()
  465. {
  466. auto consume_whitespace = [&] {
  467. m_lexer.ignore_while(whitespace);
  468. };
  469. auto consume_comma_whitespace = [&] {
  470. consume_whitespace();
  471. m_lexer.consume_specific(',');
  472. consume_whitespace();
  473. };
  474. // FIXME: This parsing is quite lenient, so will accept (with default values) some transforms that should be rejected.
  475. auto parse_optional_number = [&](float default_value = 0.0f) {
  476. consume_comma_whitespace();
  477. if (match_number())
  478. return parse_number();
  479. return default_value;
  480. };
  481. auto try_parse_number = [&]() -> Optional<float> {
  482. if (match_number())
  483. return parse_number();
  484. return {};
  485. };
  486. auto parse_function = [&](auto body) -> Optional<Transform> {
  487. consume_whitespace();
  488. if (!m_lexer.consume_specific('('))
  489. return {};
  490. consume_whitespace();
  491. auto maybe_operation = body();
  492. if (!maybe_operation.has_value())
  493. return {};
  494. Transform transform { .operation = Transform::Operation { *maybe_operation } };
  495. consume_whitespace();
  496. if (m_lexer.consume_specific(')'))
  497. return transform;
  498. return {};
  499. };
  500. // NOTE: This looks very similar to the CSS transform but the syntax is not compatible.
  501. Vector<Transform> transform_list;
  502. consume_whitespace();
  503. while (!done()) {
  504. Optional<Transform> maybe_transform;
  505. if (m_lexer.consume_specific("translate"sv)) {
  506. maybe_transform = parse_function([&]() -> Optional<Transform::Translate> {
  507. Transform::Translate translate {};
  508. auto maybe_x = try_parse_number();
  509. if (!maybe_x.has_value())
  510. return {};
  511. translate.x = *maybe_x;
  512. translate.y = parse_optional_number();
  513. return translate;
  514. });
  515. } else if (m_lexer.consume_specific("scale"sv)) {
  516. maybe_transform = parse_function([&]() -> Optional<Transform::Scale> {
  517. Transform::Scale scale {};
  518. auto maybe_x = try_parse_number();
  519. if (!maybe_x.has_value())
  520. return {};
  521. scale.x = *maybe_x;
  522. scale.y = parse_optional_number(scale.x);
  523. return scale;
  524. });
  525. } else if (m_lexer.consume_specific("rotate"sv)) {
  526. maybe_transform = parse_function([&]() -> Optional<Transform::Rotate> {
  527. Transform::Rotate rotate {};
  528. auto maybe_a = try_parse_number();
  529. if (!maybe_a.has_value())
  530. return {};
  531. rotate.a = *maybe_a;
  532. rotate.x = parse_optional_number();
  533. rotate.y = parse_optional_number();
  534. return rotate;
  535. });
  536. } else if (m_lexer.consume_specific("skewX"sv)) {
  537. maybe_transform = parse_function([&]() -> Optional<Transform::SkewX> {
  538. Transform::SkewX skew_x {};
  539. auto maybe_a = try_parse_number();
  540. if (!maybe_a.has_value())
  541. return {};
  542. skew_x.a = *maybe_a;
  543. return skew_x;
  544. });
  545. } else if (m_lexer.consume_specific("skewY"sv)) {
  546. maybe_transform = parse_function([&]() -> Optional<Transform::SkewY> {
  547. Transform::SkewY skew_y {};
  548. auto maybe_a = try_parse_number();
  549. if (!maybe_a.has_value())
  550. return {};
  551. skew_y.a = *maybe_a;
  552. return skew_y;
  553. });
  554. } else if (m_lexer.consume_specific("matrix"sv)) {
  555. maybe_transform = parse_function([&]() -> Optional<Transform::Matrix> {
  556. Transform::Matrix matrix;
  557. auto maybe_a = try_parse_number();
  558. if (!maybe_a.has_value())
  559. return {};
  560. matrix.a = *maybe_a;
  561. consume_comma_whitespace();
  562. auto maybe_b = try_parse_number();
  563. if (!maybe_b.has_value())
  564. return {};
  565. matrix.b = *maybe_b;
  566. consume_comma_whitespace();
  567. auto maybe_c = try_parse_number();
  568. if (!maybe_c.has_value())
  569. return {};
  570. matrix.c = *maybe_c;
  571. consume_comma_whitespace();
  572. auto maybe_d = try_parse_number();
  573. if (!maybe_d.has_value())
  574. return {};
  575. matrix.d = *maybe_d;
  576. consume_comma_whitespace();
  577. auto maybe_e = try_parse_number();
  578. if (!maybe_e.has_value())
  579. return {};
  580. matrix.e = *maybe_e;
  581. consume_comma_whitespace();
  582. auto maybe_f = try_parse_number();
  583. if (!maybe_f.has_value())
  584. return {};
  585. matrix.f = *maybe_f;
  586. return matrix;
  587. });
  588. }
  589. if (maybe_transform.has_value())
  590. transform_list.append(*maybe_transform);
  591. else
  592. return {};
  593. consume_comma_whitespace();
  594. }
  595. return transform_list;
  596. }
  597. bool AttributeParser::match_whitespace() const
  598. {
  599. if (done())
  600. return false;
  601. char c = ch();
  602. return c == 0x9 || c == 0x20 || c == 0xa || c == 0xc || c == 0xd;
  603. }
  604. bool AttributeParser::match_comma_whitespace() const
  605. {
  606. return match_whitespace() || match(',');
  607. }
  608. bool AttributeParser::match_coordinate() const
  609. {
  610. return match_length();
  611. }
  612. bool AttributeParser::match_number() const
  613. {
  614. return match_length();
  615. }
  616. bool AttributeParser::match_length() const
  617. {
  618. return !done() && (isdigit(ch()) || ch() == '-' || ch() == '+' || ch() == '.');
  619. }
  620. }