AttributeParser.cpp 12 KB

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