AttributeParser.cpp 12 KB

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