BorderPainting.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/CircularQueue.h>
  9. #include <LibGfx/AntiAliasingPainter.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/Layout/Node.h>
  12. namespace Web::Painting {
  13. static constexpr double dark_light_absolute_value_difference = 1. / 3;
  14. static Color light_color_for_inset_and_outset(Color const& color)
  15. {
  16. auto hsv = color.to_hsv();
  17. if (hsv.value >= dark_light_absolute_value_difference)
  18. return Color::from_hsv(hsv);
  19. return Color::from_hsv({ hsv.hue, hsv.saturation, hsv.value + dark_light_absolute_value_difference });
  20. }
  21. static Color dark_color_for_inset_and_outset(Color const& color)
  22. {
  23. auto hsv = color.to_hsv();
  24. if (hsv.value < dark_light_absolute_value_difference)
  25. return Color::from_hsv(hsv);
  26. return Color::from_hsv({ hsv.hue, hsv.saturation, hsv.value - dark_light_absolute_value_difference });
  27. }
  28. Gfx::Color border_color(BorderEdge edge, BordersDataDevicePixels const& borders_data)
  29. {
  30. auto const& border_data = [&] {
  31. switch (edge) {
  32. case BorderEdge::Top:
  33. return borders_data.top;
  34. case BorderEdge::Right:
  35. return borders_data.right;
  36. case BorderEdge::Bottom:
  37. return borders_data.bottom;
  38. case BorderEdge::Left:
  39. return borders_data.left;
  40. default:
  41. VERIFY_NOT_REACHED();
  42. }
  43. }();
  44. if (border_data.line_style == CSS::LineStyle::Inset) {
  45. auto top_left_color = dark_color_for_inset_and_outset(border_data.color);
  46. auto bottom_right_color = light_color_for_inset_and_outset(border_data.color);
  47. return (edge == BorderEdge::Left || edge == BorderEdge::Top) ? top_left_color : bottom_right_color;
  48. } else if (border_data.line_style == CSS::LineStyle::Outset) {
  49. auto top_left_color = light_color_for_inset_and_outset(border_data.color);
  50. auto bottom_right_color = dark_color_for_inset_and_outset(border_data.color);
  51. return (edge == BorderEdge::Left || edge == BorderEdge::Top) ? top_left_color : bottom_right_color;
  52. }
  53. return border_data.color;
  54. }
  55. void paint_border(RecordingPainter& painter, BorderEdge edge, DevicePixelRect const& rect, Gfx::AntiAliasingPainter::CornerRadius const& radius, Gfx::AntiAliasingPainter::CornerRadius const& opposite_radius, BordersDataDevicePixels const& borders_data, Gfx::Path& path, bool last)
  56. {
  57. auto const& border_data = [&] {
  58. switch (edge) {
  59. case BorderEdge::Top:
  60. return borders_data.top;
  61. case BorderEdge::Right:
  62. return borders_data.right;
  63. case BorderEdge::Bottom:
  64. return borders_data.bottom;
  65. default: // BorderEdge::Left:
  66. return borders_data.left;
  67. }
  68. }();
  69. if (border_data.width <= 0)
  70. return;
  71. auto color = border_color(edge, borders_data);
  72. auto border_style = border_data.line_style;
  73. struct Points {
  74. DevicePixelPoint p1;
  75. DevicePixelPoint p2;
  76. };
  77. auto points_for_edge = [](BorderEdge edge, DevicePixelRect const& rect) -> Points {
  78. switch (edge) {
  79. case BorderEdge::Top:
  80. return { rect.top_left(), rect.top_right().moved_left(1) };
  81. case BorderEdge::Right:
  82. return { rect.top_right().moved_left(1), rect.bottom_right().translated(-1) };
  83. case BorderEdge::Bottom:
  84. return { rect.bottom_left().moved_up(1), rect.bottom_right().translated(-1) };
  85. default: // Edge::Left
  86. return { rect.top_left(), rect.bottom_left().moved_up(1) };
  87. }
  88. };
  89. auto gfx_line_style = Gfx::LineStyle::Solid;
  90. switch (border_style) {
  91. case CSS::LineStyle::None:
  92. case CSS::LineStyle::Hidden:
  93. return;
  94. case CSS::LineStyle::Dotted:
  95. gfx_line_style = Gfx::LineStyle::Dotted;
  96. break;
  97. case CSS::LineStyle::Dashed:
  98. gfx_line_style = Gfx::LineStyle::Dashed;
  99. break;
  100. case CSS::LineStyle::Solid:
  101. gfx_line_style = Gfx::LineStyle::Solid;
  102. break;
  103. case CSS::LineStyle::Double:
  104. case CSS::LineStyle::Groove:
  105. case CSS::LineStyle::Ridge:
  106. case CSS::LineStyle::Inset:
  107. case CSS::LineStyle::Outset:
  108. // FIXME: Implement these
  109. break;
  110. }
  111. if (gfx_line_style != Gfx::LineStyle::Solid) {
  112. auto [p1, p2] = points_for_edge(edge, rect);
  113. switch (edge) {
  114. case BorderEdge::Top:
  115. p1.translate_by(border_data.width / 2, border_data.width / 2);
  116. p2.translate_by(-border_data.width / 2, border_data.width / 2);
  117. break;
  118. case BorderEdge::Right:
  119. p1.translate_by(-border_data.width / 2, border_data.width / 2);
  120. p2.translate_by(-border_data.width / 2, -border_data.width / 2);
  121. break;
  122. case BorderEdge::Bottom:
  123. p1.translate_by(border_data.width / 2, -border_data.width / 2);
  124. p2.translate_by(-border_data.width / 2, -border_data.width / 2);
  125. break;
  126. case BorderEdge::Left:
  127. p1.translate_by(border_data.width / 2, border_data.width / 2);
  128. p2.translate_by(border_data.width / 2, -border_data.width / 2);
  129. break;
  130. }
  131. if (border_style == CSS::LineStyle::Dotted) {
  132. painter.draw_line(p1.to_type<int>(), p2.to_type<int>(), color, border_data.width.value(), gfx_line_style);
  133. return;
  134. }
  135. painter.draw_line(p1.to_type<int>(), p2.to_type<int>(), color, border_data.width.value(), gfx_line_style);
  136. return;
  137. }
  138. auto draw_border = [&](Vector<Gfx::FloatPoint> const& points, bool joined_corner_has_inner_corner, bool opposite_joined_corner_has_inner_corner, Gfx::FloatSize joined_inner_corner_offset, Gfx::FloatSize opposite_joined_inner_corner_offset, bool ready_to_draw) {
  139. int current = 0;
  140. path.move_to(points[current++]);
  141. path.elliptical_arc_to(
  142. points[current++],
  143. Gfx::FloatSize(radius.horizontal_radius, radius.vertical_radius),
  144. 0,
  145. false,
  146. false);
  147. path.line_to(points[current++]);
  148. if (joined_corner_has_inner_corner) {
  149. path.elliptical_arc_to(
  150. points[current++],
  151. Gfx::FloatSize(radius.horizontal_radius - joined_inner_corner_offset.width(), radius.vertical_radius - joined_inner_corner_offset.height()),
  152. 0,
  153. false,
  154. true);
  155. }
  156. path.line_to(points[current++]);
  157. if (opposite_joined_corner_has_inner_corner) {
  158. path.elliptical_arc_to(
  159. points[current++],
  160. Gfx::FloatSize(opposite_radius.horizontal_radius - opposite_joined_inner_corner_offset.width(), opposite_radius.vertical_radius - opposite_joined_inner_corner_offset.height()),
  161. 0,
  162. false,
  163. true);
  164. }
  165. path.line_to(points[current++]);
  166. path.elliptical_arc_to(
  167. points[current++],
  168. Gfx::FloatSize(opposite_radius.horizontal_radius, opposite_radius.vertical_radius),
  169. 0,
  170. false,
  171. false);
  172. // If joined borders have the same color, combine them to draw together.
  173. if (ready_to_draw) {
  174. path.close_all_subpaths();
  175. painter.fill_path({ .path = path,
  176. .color = color,
  177. .winding_rule = Gfx::WindingRule::EvenOdd });
  178. path.clear();
  179. }
  180. };
  181. auto compute_midpoint = [&](int horizontal_radius, int vertical_radius, int joined_border_width) {
  182. if (horizontal_radius == 0 && vertical_radius == 0) {
  183. return Gfx::FloatPoint(0, 0);
  184. }
  185. if (joined_border_width == 0) {
  186. switch (edge) {
  187. case BorderEdge::Top:
  188. case BorderEdge::Bottom:
  189. return Gfx::FloatPoint(horizontal_radius, 0);
  190. case BorderEdge::Right:
  191. case BorderEdge::Left:
  192. return Gfx::FloatPoint(0, vertical_radius);
  193. default:
  194. VERIFY_NOT_REACHED();
  195. }
  196. }
  197. // FIXME: this middle point rule seems not exacly the same as main browsers
  198. // compute the midpoint based on point whose tangent slope of 1
  199. // https://math.stackexchange.com/questions/3325134/find-the-points-on-the-ellipse-where-the-slope-of-the-tangent-line-is-1
  200. return Gfx::FloatPoint(
  201. (horizontal_radius * horizontal_radius) / AK::sqrt(1.0f * horizontal_radius * horizontal_radius + vertical_radius * vertical_radius),
  202. (vertical_radius * vertical_radius) / AK::sqrt(1.0f * horizontal_radius * horizontal_radius + vertical_radius * vertical_radius));
  203. };
  204. /**
  205. * 0 /-------------\ 7
  206. * / /-----------\ \
  207. * /-/ 3 4 \-\
  208. * 1 2 5 6
  209. * For each border edge, need to compute 8 points at most, then paint them as closed path.
  210. * 8 points are the most complicated case, it happens when the joined border width is not 0 and border radius larger than border width on both side.
  211. * If border radius is smaller than the border width, then the inner corner of the border corner is a right angle.
  212. */
  213. switch (edge) {
  214. case BorderEdge::Top: {
  215. auto joined_border_width = borders_data.left.width;
  216. auto opposite_joined_border_width = borders_data.right.width;
  217. bool joined_corner_has_inner_corner = border_data.width < radius.vertical_radius && joined_border_width < radius.horizontal_radius;
  218. bool opposite_joined_corner_has_inner_corner = border_data.width < opposite_radius.vertical_radius && opposite_joined_border_width < opposite_radius.horizontal_radius;
  219. Gfx::FloatPoint joined_corner_endpoint_offset;
  220. Gfx::FloatPoint opposite_joined_border_corner_offset;
  221. {
  222. auto midpoint = compute_midpoint(radius.horizontal_radius, radius.vertical_radius, joined_border_width.value());
  223. joined_corner_endpoint_offset = Gfx::FloatPoint(-midpoint.x(), radius.vertical_radius - midpoint.y());
  224. }
  225. {
  226. auto midpoint = compute_midpoint(opposite_radius.horizontal_radius, opposite_radius.vertical_radius, opposite_joined_border_width.value());
  227. opposite_joined_border_corner_offset = Gfx::FloatPoint(midpoint.x(), opposite_radius.vertical_radius - midpoint.y());
  228. }
  229. Vector<Gfx::FloatPoint, 8> points;
  230. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()));
  231. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()) + joined_corner_endpoint_offset);
  232. if (joined_corner_has_inner_corner) {
  233. Gfx::FloatPoint midpoint = compute_midpoint(
  234. radius.horizontal_radius - joined_border_width.value(),
  235. radius.vertical_radius - border_data.width.value(),
  236. joined_border_width.value());
  237. Gfx::FloatPoint inner_corner_endpoint_offset = Gfx::FloatPoint(
  238. -midpoint.x(),
  239. radius.vertical_radius - border_data.width.value() - midpoint.y());
  240. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()) + inner_corner_endpoint_offset);
  241. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()));
  242. } else {
  243. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(
  244. joined_border_width.value() - radius.horizontal_radius,
  245. 0);
  246. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()) + inner_right_angle_offset);
  247. }
  248. if (opposite_joined_corner_has_inner_corner) {
  249. Gfx::FloatPoint midpoint = compute_midpoint(
  250. opposite_radius.horizontal_radius - opposite_joined_border_width.value(),
  251. opposite_radius.vertical_radius - border_data.width.value(),
  252. opposite_joined_border_width.value());
  253. Gfx::FloatPoint inner_corner_endpoint_offset = Gfx::FloatPoint(
  254. midpoint.x(),
  255. opposite_radius.vertical_radius - border_data.width.value() - midpoint.y());
  256. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()));
  257. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()) + inner_corner_endpoint_offset);
  258. } else {
  259. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(
  260. opposite_joined_border_width.value() - opposite_radius.horizontal_radius,
  261. 0);
  262. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()) - inner_right_angle_offset);
  263. }
  264. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()) + opposite_joined_border_corner_offset);
  265. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()));
  266. draw_border(
  267. points,
  268. joined_corner_has_inner_corner,
  269. opposite_joined_corner_has_inner_corner,
  270. Gfx::FloatSize(joined_border_width.value(), border_data.width.value()),
  271. Gfx::FloatSize(opposite_joined_border_width.value(), border_data.width.value()),
  272. last || color != border_color(BorderEdge::Right, borders_data));
  273. break;
  274. }
  275. case BorderEdge::Right: {
  276. auto joined_border_width = borders_data.top.width;
  277. auto opposite_joined_border_width = borders_data.bottom.width;
  278. bool joined_corner_has_inner_corner = border_data.width < radius.horizontal_radius && joined_border_width < radius.vertical_radius;
  279. bool opposite_joined_corner_has_inner_corner = border_data.width < opposite_radius.horizontal_radius && opposite_joined_border_width < opposite_radius.vertical_radius;
  280. Gfx::FloatPoint joined_corner_endpoint_offset;
  281. Gfx::FloatPoint opposite_joined_border_corner_offset;
  282. {
  283. auto midpoint = compute_midpoint(radius.horizontal_radius, radius.vertical_radius, joined_border_width.value());
  284. joined_corner_endpoint_offset = Gfx::FloatPoint(midpoint.x() - radius.horizontal_radius, -midpoint.y());
  285. }
  286. {
  287. auto midpoint = compute_midpoint(opposite_radius.horizontal_radius, opposite_radius.vertical_radius, opposite_joined_border_width.value());
  288. opposite_joined_border_corner_offset = Gfx::FloatPoint(midpoint.x() - opposite_radius.horizontal_radius, midpoint.y());
  289. }
  290. Vector<Gfx::FloatPoint, 8> points;
  291. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()));
  292. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()) + joined_corner_endpoint_offset);
  293. if (joined_corner_has_inner_corner) {
  294. auto midpoint = compute_midpoint(
  295. radius.horizontal_radius - border_data.width.value(),
  296. radius.vertical_radius - joined_border_width.value(),
  297. joined_border_width.value());
  298. Gfx::FloatPoint inner_corner = Gfx::FloatPoint(
  299. -(radius.horizontal_radius - midpoint.x() - border_data.width.value()),
  300. -midpoint.y());
  301. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()) + inner_corner);
  302. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()));
  303. } else {
  304. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(0, joined_border_width.value() - radius.horizontal_radius);
  305. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()) + inner_right_angle_offset);
  306. }
  307. if (opposite_joined_corner_has_inner_corner) {
  308. auto midpoint = compute_midpoint(
  309. opposite_radius.horizontal_radius - border_data.width.value(),
  310. opposite_radius.vertical_radius - opposite_joined_border_width.value(),
  311. opposite_joined_border_width.value());
  312. Gfx::FloatPoint inner_corner = Gfx::FloatPoint(
  313. -(opposite_radius.horizontal_radius - midpoint.x() - border_data.width.value()),
  314. midpoint.y());
  315. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()));
  316. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()) + inner_corner);
  317. } else {
  318. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(0, opposite_joined_border_width.value() - opposite_radius.horizontal_radius);
  319. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()) - inner_right_angle_offset);
  320. }
  321. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()) + opposite_joined_border_corner_offset);
  322. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()));
  323. draw_border(
  324. points,
  325. joined_corner_has_inner_corner,
  326. opposite_joined_corner_has_inner_corner,
  327. Gfx::FloatSize(border_data.width.value(), joined_border_width.value()),
  328. Gfx::FloatSize(border_data.width.value(), opposite_joined_border_width.value()),
  329. last || color != border_color(BorderEdge::Bottom, borders_data));
  330. break;
  331. }
  332. case BorderEdge::Bottom: {
  333. auto joined_border_width = borders_data.right.width;
  334. auto opposite_joined_border_width = borders_data.left.width;
  335. bool joined_corner_has_inner_corner = border_data.width < radius.vertical_radius && joined_border_width < radius.horizontal_radius;
  336. bool opposite_joined_corner_has_inner_corner = border_data.width < opposite_radius.vertical_radius && opposite_joined_border_width < opposite_radius.horizontal_radius;
  337. Gfx::FloatPoint joined_corner_endpoint_offset;
  338. Gfx::FloatPoint opposite_joined_border_corner_offset;
  339. {
  340. auto midpoint = compute_midpoint(radius.horizontal_radius, radius.vertical_radius, joined_border_width.value());
  341. joined_corner_endpoint_offset = Gfx::FloatPoint(midpoint.x(), midpoint.y() - radius.vertical_radius);
  342. }
  343. {
  344. auto midpoint = compute_midpoint(opposite_radius.horizontal_radius, opposite_radius.vertical_radius, opposite_joined_border_width.value());
  345. opposite_joined_border_corner_offset = Gfx::FloatPoint(-midpoint.x(), midpoint.y() - opposite_radius.vertical_radius);
  346. }
  347. Vector<Gfx::FloatPoint, 8> points;
  348. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()));
  349. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()) + joined_corner_endpoint_offset);
  350. if (joined_corner_has_inner_corner) {
  351. auto midpoint = compute_midpoint(
  352. radius.horizontal_radius - joined_border_width.value(),
  353. radius.vertical_radius - border_data.width.value(),
  354. joined_border_width.value());
  355. Gfx::FloatPoint inner_corner = Gfx::FloatPoint(midpoint.x(), -(radius.vertical_radius - midpoint.y() - border_data.width.value()));
  356. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()) + inner_corner);
  357. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()));
  358. } else {
  359. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(joined_border_width.value() - radius.horizontal_radius, 0);
  360. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()) - inner_right_angle_offset);
  361. }
  362. if (opposite_joined_corner_has_inner_corner) {
  363. auto midpoint = compute_midpoint(
  364. opposite_radius.horizontal_radius - opposite_joined_border_width.value(),
  365. opposite_radius.vertical_radius - border_data.width.value(),
  366. opposite_joined_border_width.value());
  367. Gfx::FloatPoint inner_corner = Gfx::FloatPoint(
  368. -midpoint.x(),
  369. -(opposite_radius.vertical_radius - midpoint.y() - border_data.width.value()));
  370. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()));
  371. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()) + inner_corner);
  372. } else {
  373. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(opposite_joined_border_width.value() - opposite_radius.horizontal_radius, 0);
  374. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()) + inner_right_angle_offset);
  375. }
  376. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()) + opposite_joined_border_corner_offset);
  377. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()));
  378. draw_border(
  379. points,
  380. joined_corner_has_inner_corner,
  381. opposite_joined_corner_has_inner_corner,
  382. Gfx::FloatSize(joined_border_width.value(), border_data.width.value()),
  383. Gfx::FloatSize(opposite_joined_border_width.value(), border_data.width.value()),
  384. last || color != border_color(BorderEdge::Left, borders_data));
  385. break;
  386. }
  387. case BorderEdge::Left: {
  388. auto joined_border_width = borders_data.bottom.width;
  389. auto opposite_joined_border_width = borders_data.top.width;
  390. bool joined_corner_has_inner_corner = border_data.width < radius.horizontal_radius && joined_border_width < radius.vertical_radius;
  391. bool opposite_joined_corner_has_inner_corner = border_data.width < opposite_radius.horizontal_radius && opposite_joined_border_width < opposite_radius.vertical_radius;
  392. Gfx::FloatPoint joined_corner_endpoint_offset;
  393. Gfx::FloatPoint opposite_joined_border_corner_offset;
  394. {
  395. auto midpoint = compute_midpoint(radius.horizontal_radius, radius.vertical_radius, joined_border_width.value());
  396. joined_corner_endpoint_offset = Gfx::FloatPoint(radius.horizontal_radius - midpoint.x(), midpoint.y());
  397. }
  398. {
  399. auto midpoint = compute_midpoint(opposite_radius.horizontal_radius, opposite_radius.vertical_radius, opposite_joined_border_width.value());
  400. opposite_joined_border_corner_offset = Gfx::FloatPoint(opposite_radius.horizontal_radius - midpoint.x(), -midpoint.y());
  401. }
  402. Vector<Gfx::FloatPoint, 8> points;
  403. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()));
  404. points.append(Gfx::FloatPoint(rect.bottom_left().to_type<int>()) + joined_corner_endpoint_offset);
  405. if (joined_corner_has_inner_corner) {
  406. auto midpoint = compute_midpoint(
  407. radius.horizontal_radius - border_data.width.value(),
  408. radius.vertical_radius - joined_border_width.value(),
  409. joined_border_width.value());
  410. Gfx::FloatPoint inner_corner = Gfx::FloatPoint(radius.horizontal_radius - border_data.width.value() - midpoint.x(), midpoint.y());
  411. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()) + inner_corner);
  412. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()));
  413. } else {
  414. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(0, joined_border_width.value() - radius.vertical_radius);
  415. points.append(Gfx::FloatPoint(rect.bottom_right().to_type<int>()) - inner_right_angle_offset);
  416. }
  417. if (opposite_joined_corner_has_inner_corner) {
  418. auto midpoint = compute_midpoint(
  419. opposite_radius.horizontal_radius - border_data.width.value(),
  420. opposite_radius.vertical_radius - opposite_joined_border_width.value(),
  421. opposite_joined_border_width.value());
  422. Gfx::FloatPoint inner_corner = Gfx::FloatPoint(
  423. opposite_radius.horizontal_radius - border_data.width.value() - midpoint.x(),
  424. -midpoint.y());
  425. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()));
  426. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()) + inner_corner);
  427. } else {
  428. Gfx::FloatPoint inner_right_angle_offset = Gfx::FloatPoint(0, opposite_joined_border_width.value() - opposite_radius.vertical_radius);
  429. points.append(Gfx::FloatPoint(rect.top_right().to_type<int>()) + inner_right_angle_offset);
  430. }
  431. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()) + opposite_joined_border_corner_offset);
  432. points.append(Gfx::FloatPoint(rect.top_left().to_type<int>()));
  433. draw_border(
  434. points,
  435. joined_corner_has_inner_corner,
  436. opposite_joined_corner_has_inner_corner,
  437. Gfx::FloatSize(border_data.width.value(), joined_border_width.value()),
  438. Gfx::FloatSize(border_data.width.value(), opposite_joined_border_width.value()),
  439. last || color != border_color(BorderEdge::Top, borders_data));
  440. break;
  441. }
  442. }
  443. }
  444. void paint_all_borders(RecordingPainter& painter, DevicePixelRect const& border_rect, CornerRadii const& corner_radii, BordersDataDevicePixels const& borders_data)
  445. {
  446. if (borders_data.top.width <= 0 && borders_data.right.width <= 0 && borders_data.left.width <= 0 && borders_data.bottom.width <= 0)
  447. return;
  448. auto top_left = corner_radii.top_left;
  449. auto top_right = corner_radii.top_right;
  450. auto bottom_right = corner_radii.bottom_right;
  451. auto bottom_left = corner_radii.bottom_left;
  452. // Disable border radii if the corresponding borders don't exist:
  453. if (borders_data.bottom.width <= 0 && borders_data.left.width <= 0)
  454. bottom_left = { 0, 0 };
  455. if (borders_data.bottom.width <= 0 && borders_data.right.width <= 0)
  456. bottom_right = { 0, 0 };
  457. if (borders_data.top.width <= 0 && borders_data.left.width <= 0)
  458. top_left = { 0, 0 };
  459. if (borders_data.top.width <= 0 && borders_data.right.width <= 0)
  460. top_right = { 0, 0 };
  461. DevicePixelRect top_border_rect = {
  462. border_rect.x() + top_left.horizontal_radius,
  463. border_rect.y(),
  464. border_rect.width() - top_left.horizontal_radius - top_right.horizontal_radius,
  465. borders_data.top.width
  466. };
  467. DevicePixelRect right_border_rect = {
  468. border_rect.x() + (border_rect.width() - borders_data.right.width),
  469. border_rect.y() + top_right.vertical_radius,
  470. borders_data.right.width,
  471. border_rect.height() - top_right.vertical_radius - bottom_right.vertical_radius
  472. };
  473. DevicePixelRect bottom_border_rect = {
  474. border_rect.x() + bottom_left.horizontal_radius,
  475. border_rect.y() + (border_rect.height() - borders_data.bottom.width),
  476. border_rect.width() - bottom_left.horizontal_radius - bottom_right.horizontal_radius,
  477. borders_data.bottom.width
  478. };
  479. DevicePixelRect left_border_rect = {
  480. border_rect.x(),
  481. border_rect.y() + top_left.vertical_radius,
  482. borders_data.left.width,
  483. border_rect.height() - top_left.vertical_radius - bottom_left.vertical_radius
  484. };
  485. AK::CircularQueue<BorderEdge, 4> borders;
  486. borders.enqueue(BorderEdge::Top);
  487. borders.enqueue(BorderEdge::Right);
  488. borders.enqueue(BorderEdge::Bottom);
  489. borders.enqueue(BorderEdge::Left);
  490. // Try to find the first border that has a different color than the previous one,
  491. // then start painting from that border.
  492. for (size_t i = 0; i < borders.size(); i++) {
  493. if (border_color(borders.at(0), borders_data) != border_color(borders.at(1), borders_data)) {
  494. borders.enqueue(borders.dequeue());
  495. break;
  496. }
  497. borders.enqueue(borders.dequeue());
  498. }
  499. Gfx::Path path;
  500. for (BorderEdge edge : borders) {
  501. switch (edge) {
  502. case BorderEdge::Top:
  503. paint_border(painter, BorderEdge::Top, top_border_rect, top_left, top_right, borders_data, path, edge == borders.last());
  504. break;
  505. case BorderEdge::Right:
  506. paint_border(painter, BorderEdge::Right, right_border_rect, top_right, bottom_right, borders_data, path, edge == borders.last());
  507. break;
  508. case BorderEdge::Bottom:
  509. paint_border(painter, BorderEdge::Bottom, bottom_border_rect, bottom_right, bottom_left, borders_data, path, edge == borders.last());
  510. break;
  511. case BorderEdge::Left:
  512. paint_border(painter, BorderEdge::Left, left_border_rect, bottom_left, top_left, borders_data, path, edge == borders.last());
  513. break;
  514. default:
  515. VERIFY_NOT_REACHED();
  516. }
  517. }
  518. }
  519. Optional<BordersData> borders_data_for_outline(Layout::Node const& layout_node, Color outline_color, CSS::OutlineStyle outline_style, CSSPixels outline_width)
  520. {
  521. CSS::LineStyle line_style;
  522. if (outline_style == CSS::OutlineStyle::Auto) {
  523. // `auto` lets us do whatever we want for the outline. 2px of the link colour seems reasonable.
  524. line_style = CSS::LineStyle::Dotted;
  525. outline_color = layout_node.document().normal_link_color();
  526. outline_width = 2;
  527. } else {
  528. line_style = CSS::value_id_to_line_style(CSS::to_value_id(outline_style)).value_or(CSS::LineStyle::None);
  529. }
  530. if (outline_color.alpha() == 0 || line_style == CSS::LineStyle::None || outline_width == 0)
  531. return {};
  532. CSS::BorderData border_data {
  533. .color = outline_color,
  534. .line_style = line_style,
  535. .width = outline_width,
  536. };
  537. return BordersData { border_data, border_data, border_data, border_data };
  538. }
  539. }