HTMLPathElement.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibGfx/Path.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Event.h>
  30. #include <LibWeb/DOM/HTMLPathElement.h>
  31. #include <ctype.h>
  32. //#define PATH_DEBUG
  33. namespace Web {
  34. PathDataParser::PathDataParser(const String& source)
  35. : m_source(source)
  36. {
  37. }
  38. Vector<PathInstruction> PathDataParser::parse()
  39. {
  40. parse_whitespace();
  41. while (!done())
  42. parse_drawto();
  43. if (!m_instructions.is_empty() && m_instructions[0].type != PathInstructionType::Move)
  44. ASSERT_NOT_REACHED();
  45. return m_instructions;
  46. }
  47. void PathDataParser::parse_drawto() {
  48. if (match('M') || match('m')) {
  49. parse_moveto();
  50. } else if (match('Z') || match('z')) {
  51. parse_closepath();
  52. } else if (match('L') || match('l')) {
  53. parse_lineto();
  54. } else if (match('H') || match('h')) {
  55. parse_horizontal_lineto();
  56. } else if (match('V') || match('v')) {
  57. parse_vertical_lineto();
  58. } else if (match('C') || match('c')) {
  59. parse_curveto();
  60. } else if (match('S') || match('s')) {
  61. parse_smooth_curveto();
  62. } else if (match('Q') || match('q')) {
  63. parse_quadratic_bezier_curveto();
  64. } else if (match('T') || match('t')) {
  65. parse_smooth_quadratic_bezier_curveto();
  66. } else if (match('A') || match('a')) {
  67. parse_elliptical_arc();
  68. }
  69. }
  70. void PathDataParser::parse_moveto()
  71. {
  72. bool absolute = consume() == 'M';
  73. parse_whitespace();
  74. for (auto& pair : parse_coordinate_pair_sequence())
  75. m_instructions.append({ PathInstructionType::Move, absolute, pair });
  76. }
  77. void PathDataParser::parse_closepath()
  78. {
  79. bool absolute = consume() == 'Z';
  80. m_instructions.append({ PathInstructionType::ClosePath, absolute, {} });
  81. }
  82. void PathDataParser::parse_lineto()
  83. {
  84. bool absolute = consume() == 'L';
  85. parse_whitespace();
  86. for (auto& pair : parse_coordinate_pair_sequence())
  87. m_instructions.append({ PathInstructionType::Line, absolute, pair });
  88. }
  89. void PathDataParser::parse_horizontal_lineto()
  90. {
  91. bool absolute = consume() == 'H';
  92. parse_whitespace();
  93. m_instructions.append({ PathInstructionType::HorizontalLine, absolute, parse_coordinate_sequence() });
  94. }
  95. void PathDataParser::parse_vertical_lineto()
  96. {
  97. bool absolute = consume() == 'V';
  98. parse_whitespace();
  99. m_instructions.append({ PathInstructionType::VerticalLine, absolute, parse_coordinate_sequence() });
  100. }
  101. void PathDataParser::parse_curveto()
  102. {
  103. bool absolute = consume() == 'C';
  104. parse_whitespace();
  105. while (true) {
  106. m_instructions.append({ PathInstructionType::Curve, absolute, parse_coordinate_pair_triplet() });
  107. parse_whitespace();
  108. if (!match_number())
  109. break;
  110. }
  111. }
  112. void PathDataParser::parse_smooth_curveto()
  113. {
  114. bool absolute = consume() == 'S';
  115. parse_whitespace();
  116. while (true) {
  117. m_instructions.append({ PathInstructionType::SmoothCurve, absolute, parse_coordinate_pair_double() });
  118. parse_whitespace();
  119. if (!match_number())
  120. break;
  121. }
  122. }
  123. void PathDataParser::parse_quadratic_bezier_curveto()
  124. {
  125. bool absolute = consume() == 'Q';
  126. parse_whitespace();
  127. while (true) {
  128. m_instructions.append({ PathInstructionType::QuadraticBezierCurve, absolute, parse_coordinate_pair_double() });
  129. parse_whitespace();
  130. if (!match_number())
  131. break;
  132. }
  133. }
  134. void PathDataParser::parse_smooth_quadratic_bezier_curveto()
  135. {
  136. bool absolute = consume() == 'T';
  137. parse_whitespace();
  138. while (true) {
  139. m_instructions.append({ PathInstructionType::SmoothQuadraticBezierCurve, absolute, parse_coordinate_pair_double() });
  140. parse_whitespace();
  141. if (!match_number())
  142. break;
  143. }
  144. }
  145. void PathDataParser::parse_elliptical_arc()
  146. {
  147. bool absolute = consume() == 'A';
  148. parse_whitespace();
  149. while (true) {
  150. m_instructions.append({ PathInstructionType::EllipticalArc, absolute, parse_elliptical_arg_argument() });
  151. parse_whitespace();
  152. if (!match_number())
  153. break;
  154. }
  155. }
  156. float PathDataParser::parse_coordinate()
  157. {
  158. return parse_number();
  159. }
  160. Vector<float> PathDataParser::parse_coordinate_pair()
  161. {
  162. Vector<float> coordinates;
  163. coordinates.append(parse_coordinate());
  164. if (match_comma_whitespace())
  165. parse_comma_whitespace();
  166. coordinates.append(parse_coordinate());
  167. return coordinates;
  168. }
  169. Vector<float> PathDataParser::parse_coordinate_sequence()
  170. {
  171. Vector<float> sequence;
  172. while (true) {
  173. sequence.append(parse_coordinate());
  174. if (match_comma_whitespace())
  175. parse_comma_whitespace();
  176. if (!match_comma_whitespace() && !match_number())
  177. break;
  178. }
  179. return sequence;
  180. }
  181. Vector<Vector<float>> PathDataParser::parse_coordinate_pair_sequence()
  182. {
  183. Vector<Vector<float>> sequence;
  184. while (true) {
  185. sequence.append(parse_coordinate_pair());
  186. if (match_comma_whitespace())
  187. parse_comma_whitespace();
  188. if (!match_comma_whitespace() && !match_number())
  189. break;
  190. }
  191. return sequence;
  192. }
  193. Vector<float> PathDataParser::parse_coordinate_pair_double()
  194. {
  195. Vector<float> coordinates;
  196. coordinates.append(parse_coordinate_pair());
  197. if (match_comma_whitespace())
  198. parse_comma_whitespace();
  199. coordinates.append(parse_coordinate_pair());
  200. return coordinates;
  201. }
  202. Vector<float> PathDataParser::parse_coordinate_pair_triplet()
  203. {
  204. Vector<float> coordinates;
  205. coordinates.append(parse_coordinate_pair());
  206. if (match_comma_whitespace())
  207. parse_comma_whitespace();
  208. coordinates.append(parse_coordinate_pair());
  209. if (match_comma_whitespace())
  210. parse_comma_whitespace();
  211. coordinates.append(parse_coordinate_pair());
  212. return coordinates;
  213. }
  214. Vector<float> PathDataParser::parse_elliptical_arg_argument()
  215. {
  216. Vector<float> numbers;
  217. numbers.append(parse_number());
  218. if (match_comma_whitespace())
  219. parse_comma_whitespace();
  220. numbers.append(parse_number());
  221. if (match_comma_whitespace())
  222. parse_comma_whitespace();
  223. numbers.append(parse_number());
  224. parse_comma_whitespace();
  225. numbers.append(parse_flag());
  226. if (match_comma_whitespace())
  227. parse_comma_whitespace();
  228. numbers.append(parse_flag());
  229. if (match_comma_whitespace())
  230. parse_comma_whitespace();
  231. numbers.append(parse_coordinate_pair());
  232. return numbers;
  233. }
  234. void PathDataParser::parse_whitespace(bool must_match_once)
  235. {
  236. bool matched = false;
  237. while (!done() && match_whitespace()) {
  238. consume();
  239. matched = true;
  240. }
  241. ASSERT(!must_match_once || matched);
  242. }
  243. void PathDataParser::parse_comma_whitespace()
  244. {
  245. if (match(',')) {
  246. consume();
  247. parse_whitespace();
  248. } else {
  249. parse_whitespace(1);
  250. if (match(','))
  251. consume();
  252. parse_whitespace();
  253. }
  254. }
  255. float PathDataParser::parse_fractional_constant()
  256. {
  257. StringBuilder builder;
  258. bool floating_point = false;
  259. while (!done() && isdigit(ch()))
  260. builder.append(consume());
  261. if (match('.')) {
  262. floating_point = true;
  263. builder.append('.');
  264. consume();
  265. while (!done() && isdigit(ch()))
  266. builder.append(consume());
  267. } else {
  268. ASSERT(builder.length() > 0);
  269. }
  270. if (floating_point)
  271. return strtof(builder.to_string().characters(), nullptr);
  272. return builder.to_string().to_int().value();
  273. }
  274. float PathDataParser::parse_number()
  275. {
  276. bool negative = false;
  277. if (match('-')) {
  278. consume();
  279. negative = true;
  280. } else if (match('+')) {
  281. consume();
  282. }
  283. auto number = parse_fractional_constant();
  284. if (match('e') || match('E'))
  285. TODO();
  286. return negative ? number * -1 : number;
  287. }
  288. float PathDataParser::parse_flag()
  289. {
  290. auto number = parse_number();
  291. ASSERT(number == 0 || number == 1);
  292. return number;
  293. }
  294. bool PathDataParser::match_whitespace() const
  295. {
  296. if (done())
  297. return false;
  298. char c = ch();
  299. return c == 0x9 || c == 0x20 || c == 0xa || c == 0xc || c == 0xd;
  300. }
  301. bool PathDataParser::match_comma_whitespace() const
  302. {
  303. return match_whitespace() || match(',');
  304. }
  305. bool PathDataParser::match_number() const
  306. {
  307. return !done() && (isdigit(ch()) || ch() == '-' || ch() == '+');
  308. }
  309. HTMLPathElement::HTMLPathElement(Document& document, const FlyString& tag_name)
  310. : HTMLElement(document, tag_name)
  311. {
  312. }
  313. #ifdef PATH_DEBUG
  314. static void print_instruction(const PathInstruction& instruction)
  315. {
  316. auto& data = instruction.data;
  317. switch (instruction.type) {
  318. case PathInstructionType::Move:
  319. dbg() << "Move (absolute: " << instruction.absolute << ")";
  320. for (size_t i = 0; i < data.size(); i += 2)
  321. dbg() << " x=" << data[i] << ", y=" << data[i + 1];
  322. break;
  323. case PathInstructionType::ClosePath:
  324. dbg() << "ClosePath (absolute=" << instruction.absolute << ")";
  325. break;
  326. case PathInstructionType::Line:
  327. dbg() << "Line (absolute=" << instruction.absolute << ")";
  328. for (size_t i = 0; i < data.size(); i += 2)
  329. dbg() << " x=" << data[i] << ", y=" << data[i + 1];
  330. break;
  331. case PathInstructionType::HorizontalLine:
  332. dbg() << "HorizontalLine (absolute=" << instruction.absolute << ")";
  333. for (size_t i = 0; i < data.size(); ++i)
  334. dbg() << " x=" << data[i];
  335. break;
  336. case PathInstructionType::VerticalLine:
  337. dbg() << "VerticalLine (absolute=" << instruction.absolute << ")";
  338. for (size_t i = 0; i < data.size(); ++i)
  339. dbg() << " y=" << data[i];
  340. break;
  341. case PathInstructionType::Curve:
  342. dbg() << "Curve (absolute=" << instruction.absolute << ")";
  343. for (size_t i = 0; i < data.size(); i += 6)
  344. dbg() << " (x1=" << data[i] << ", y1=" << data[i + 1] << "), (x2=" << data[i + 2] << ", y2=" << data[i + 3] << "), (x=" << data[i + 4] << ", y=" << data[i + 5] << ")";
  345. break;
  346. case PathInstructionType::SmoothCurve:
  347. dbg() << "SmoothCurve (absolute: " << instruction.absolute << ")";
  348. for (size_t i = 0; i < data.size(); i += 4)
  349. dbg() << " (x2=" << data[i] << ", y2=" << data[i + 1] << "), (x=" << data[i + 2] << ", y=" << data[i + 3] << ")";
  350. break;
  351. case PathInstructionType::QuadraticBezierCurve:
  352. dbg() << "QuadraticBezierCurve (absolute: " << instruction.absolute << ")";
  353. for (size_t i = 0; i < data.size(); i += 4)
  354. dbg() << " (x1=" << data[i] << ", y1=" << data[i + 1] << "), (x=" << data[i + 2] << ", y=" << data[i + 3] << ")";
  355. break;
  356. case PathInstructionType::SmoothQuadraticBezierCurve:
  357. dbg() << "SmoothQuadraticBezierCurve (absolute: " << instruction.absolute << ")";
  358. for (size_t i = 0; i < data.size(); i += 2)
  359. dbg() << " x=" << data[i] << ", y=" << data[i + 1];
  360. break;
  361. case PathInstructionType::EllipticalArc:
  362. dbg() << "EllipticalArc (absolute: " << instruction.absolute << ")";
  363. for (size_t i = 0; i < data.size(); i += 7)
  364. dbg() << " (rx=" << data[i] << ", ry=" << data[i + 1] << ") x-axis-rotation=" << data[i + 2] << ", large-arc-flag=" << data[i + 3] << ", sweep-flag=" << data[i + 4] << ", (x=" << data[i + 5] << ", y=" << data[i + 6] << ")";
  365. break;
  366. case PathInstructionType::Invalid:
  367. dbg() << "Invalid (absolute: " << instruction.absolute << ")";
  368. break;
  369. }
  370. }
  371. #endif
  372. void HTMLPathElement::parse_attribute(const FlyString& name, const String& value)
  373. {
  374. HTMLElement::parse_attribute(name, value);
  375. if (name == "d")
  376. m_instructions = PathDataParser(value).parse();
  377. }
  378. void HTMLPathElement::paint(const SvgPaintingContext& context, Gfx::Painter& painter)
  379. {
  380. Gfx::Path path;
  381. for (auto& instruction : m_instructions) {
  382. auto& absolute = instruction.absolute;
  383. auto& data = instruction.data;
  384. #ifdef PATH_DEBUG
  385. print_instruction(instruction);
  386. #endif
  387. switch (instruction.type) {
  388. case PathInstructionType::Move:
  389. if (absolute) {
  390. path.move_to({ data[0], data[1] });
  391. } else {
  392. ASSERT(!path.segments().is_empty());
  393. path.move_to(Gfx::FloatPoint { data[0], data[1] } + path.segments().last().point);
  394. }
  395. break;
  396. case PathInstructionType::ClosePath:
  397. path.close();
  398. break;
  399. case PathInstructionType::Line:
  400. if (absolute) {
  401. path.line_to({ data[0], data[1] });
  402. } else {
  403. ASSERT(!path.segments().is_empty());
  404. path.line_to(Gfx::FloatPoint { data[0], data[1] } + path.segments().last().point);
  405. }
  406. break;
  407. case PathInstructionType::HorizontalLine: {
  408. ASSERT(!path.segments().is_empty());
  409. auto last_point = path.segments().last().point;
  410. if (absolute) {
  411. path.line_to(Gfx::FloatPoint { data[0], last_point.y() });
  412. } else {
  413. path.line_to(Gfx::FloatPoint { data[0] + last_point.x(), last_point.y() });
  414. }
  415. break;
  416. }
  417. case PathInstructionType::VerticalLine: {
  418. ASSERT(!path.segments().is_empty());
  419. auto last_point = path.segments().last().point;
  420. if (absolute) {
  421. path.line_to(Gfx::FloatPoint{ last_point.x(), data[0] });
  422. } else {
  423. path.line_to(Gfx::FloatPoint{ last_point.x(), data[0] + last_point.y() });
  424. }
  425. break;
  426. }
  427. case PathInstructionType::QuadraticBezierCurve:
  428. if (absolute) {
  429. path.quadratic_bezier_curve_to({ data[0], data[1] }, { data[2], data[3] });
  430. } else {
  431. ASSERT(!path.segments().is_empty());
  432. auto last_point = path.segments().last().point;
  433. path.quadratic_bezier_curve_to({ data[0] + last_point.x(), data[1] + last_point.y() }, { data[2] + last_point.x(), data[3] + last_point.y() });
  434. }
  435. break;
  436. case PathInstructionType::Curve:
  437. case PathInstructionType::SmoothCurve:
  438. case PathInstructionType::SmoothQuadraticBezierCurve:
  439. case PathInstructionType::EllipticalArc:
  440. TODO();
  441. case PathInstructionType::Invalid:
  442. ASSERT_NOT_REACHED();
  443. }
  444. }
  445. painter.fill_path(path, context.fill_color, Gfx::Painter::WindingRule::EvenOdd);
  446. painter.stroke_path(path, context.stroke_color, context.stroke_width);
  447. }
  448. }