AntiAliasingPainter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2022, Ben Maxwell <macdue@dueutil.tech>
  4. * Copyright (c) 2022, Torsten Engelmann <engelTorsten@gmx.de>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #if defined(AK_COMPILER_GCC)
  9. # pragma GCC optimize("O3")
  10. #endif
  11. #include <AK/Function.h>
  12. #include <AK/NumericLimits.h>
  13. #include <LibGfx/AntiAliasingPainter.h>
  14. #include <LibGfx/Line.h>
  15. namespace Gfx {
  16. void AntiAliasingPainter::draw_anti_aliased_line(FloatPoint actual_from, FloatPoint actual_to, Color color, float thickness, Painter::LineStyle style, Color, LineLengthMode line_length_mode)
  17. {
  18. // FIXME: Implement this :P
  19. VERIFY(style == Painter::LineStyle::Solid);
  20. if (color.alpha() == 0)
  21. return;
  22. // FIMXE:
  23. // This is not a proper line drawing algorithm.
  24. // It's hack-ish AA rotated rectangle painting.
  25. // There's probably more optimal ways to achieve this
  26. // (though this still runs faster than the previous AA-line code)
  27. //
  28. // If you, reading this comment, know a better way that:
  29. // 1. Does not overpaint (i.e. painting a line with transparency looks correct)
  30. // 2. Has square end points (i.e. the line is a rectangle)
  31. // 3. Has good anti-aliasing
  32. // 4. Is less hacky than this
  33. //
  34. // Please delete this code and implement it!
  35. int int_thickness = AK::ceil(thickness);
  36. auto mapped_from = m_transform.map(actual_from);
  37. auto mapped_to = m_transform.map(actual_to);
  38. auto distance = mapped_to.distance_from(mapped_from);
  39. auto length = distance + (line_length_mode == LineLengthMode::PointToPoint);
  40. // Axis-aligned lines:
  41. if (mapped_from.y() == mapped_to.y()) {
  42. auto start_point = (mapped_from.x() < mapped_to.x() ? mapped_from : mapped_to).translated(0, -int_thickness / 2);
  43. return fill_rect(Gfx::FloatRect(start_point, { length, thickness }), color);
  44. }
  45. if (mapped_from.x() == mapped_to.x()) {
  46. auto start_point = (mapped_from.y() < mapped_to.y() ? mapped_from : mapped_to).translated(-int_thickness / 2, 0);
  47. return fill_rect(Gfx::FloatRect(start_point, { thickness, length }), color);
  48. }
  49. // The painting only works for the positive XY quadrant (because that is easier).
  50. // So flip things around until we're there:
  51. bool flip_x = false;
  52. bool flip_y = false;
  53. if (mapped_to.x() < mapped_from.x() && mapped_to.y() < mapped_from.y())
  54. swap(mapped_to, mapped_from);
  55. if ((flip_x = mapped_to.x() < mapped_from.x()))
  56. mapped_to.set_x(2 * mapped_from.x() - mapped_to.x());
  57. if ((flip_y = mapped_to.y() < mapped_from.y()))
  58. mapped_to.set_y(2 * mapped_from.y() - mapped_to.y());
  59. auto delta = mapped_to - mapped_from;
  60. auto line_angle_radians = AK::atan2(delta.y(), delta.x()) - 0.5f * AK::Pi<float>;
  61. float sin_inverse_angle;
  62. float cos_inverse_angle;
  63. AK::sincos(-line_angle_radians, sin_inverse_angle, cos_inverse_angle);
  64. auto inverse_rotate_point = [=](FloatPoint point) {
  65. return Gfx::FloatPoint(
  66. point.x() * cos_inverse_angle - point.y() * sin_inverse_angle,
  67. point.y() * cos_inverse_angle + point.x() * sin_inverse_angle);
  68. };
  69. Gfx::FloatRect line_rect({ -(thickness * 255) / 2.0f, 0 }, Gfx::FloatSize(thickness * 255, length * 255));
  70. auto gradient = delta.y() / delta.x();
  71. // Work out how long we need to scan along the X-axis to reach the other side of the line.
  72. // E.g. for a vertical line this would be `thickness', in general it is this:
  73. int scan_line_length = AK::ceil(AK::sqrt((gradient * gradient + 1) * thickness * thickness) / gradient);
  74. auto x_gradient = 1 / gradient;
  75. int x_step = floorf(x_gradient);
  76. float x_error = 0;
  77. float x_error_per_y = x_gradient - x_step;
  78. auto y_offset = int_thickness + 1;
  79. auto x_offset = int(x_gradient * y_offset);
  80. int const line_start_x = mapped_from.x();
  81. int const line_start_y = mapped_from.y();
  82. int const line_end_x = mapped_to.x();
  83. int const line_end_y = mapped_to.y();
  84. auto set_pixel = [=, this](int x, int y, Gfx::Color color) {
  85. // FIXME: The lines seem slightly off (<= 1px) when flipped.
  86. if (flip_x)
  87. x = 2 * line_start_x - x;
  88. if (flip_y)
  89. y = 2 * line_start_y - y;
  90. m_underlying_painter.set_pixel(x, y, color, true);
  91. };
  92. // Scan a bit extra to avoid issues from the x_error:
  93. int const overscan = max(x_step, 1) * 2 + 1;
  94. int x = line_start_x - x_offset;
  95. int const center_offset = (scan_line_length + 1) / 2;
  96. for (int y = line_start_y - y_offset; y < line_end_y + y_offset; y += 1) {
  97. for (int i = -overscan; i < scan_line_length + overscan; i++) {
  98. int scan_x_pos = x + i - center_offset;
  99. // Avoid scanning over pixels definitely outside the line:
  100. int dx = (line_start_x - int_thickness) - (scan_x_pos + 1);
  101. if (dx > 0) {
  102. i += dx;
  103. continue;
  104. }
  105. if (line_end_x + int_thickness <= scan_x_pos - 1)
  106. break;
  107. auto sample = inverse_rotate_point(Gfx::FloatPoint(scan_x_pos - line_start_x, y - line_start_y));
  108. Gfx::FloatRect sample_px(sample * 255, Gfx::FloatSize(255, 255));
  109. sample_px.intersect(line_rect);
  110. auto alpha = (sample_px.width() * sample_px.height()) / 255.0f;
  111. alpha = (alpha * color.alpha()) / 255;
  112. set_pixel(scan_x_pos, y, color.with_alpha(alpha));
  113. }
  114. x += x_step;
  115. x_error += x_error_per_y;
  116. if (x_error > 1.0f) {
  117. x_error -= 1.0f;
  118. x += 1;
  119. }
  120. }
  121. }
  122. void AntiAliasingPainter::draw_dotted_line(IntPoint point1, IntPoint point2, Color color, int thickness)
  123. {
  124. // AA circles don't really work below a radius of 2px.
  125. if (thickness < 4)
  126. return m_underlying_painter.draw_line(point1, point2, color, thickness, Painter::LineStyle::Dotted);
  127. auto draw_spaced_dots = [&](int start, int end, auto to_point) {
  128. int step = thickness * 2;
  129. if (start > end)
  130. swap(start, end);
  131. int delta = end - start;
  132. int dots = delta / step;
  133. if (dots == 0)
  134. return;
  135. int fudge_per_dot = 0;
  136. int extra_fudge = 0;
  137. if (dots > 3) {
  138. // Fudge the numbers so the last dot is drawn at the `end' point (otherwise you can get lines cuts short).
  139. // You need at least a handful of dots to do this.
  140. int fudge = delta % step;
  141. fudge_per_dot = fudge / dots;
  142. extra_fudge = fudge % dots;
  143. }
  144. for (int dot = start; dot <= end; dot += (step + fudge_per_dot + (extra_fudge > 0))) {
  145. fill_circle(to_point(dot), thickness / 2, color);
  146. --extra_fudge;
  147. }
  148. };
  149. if (point1.y() == point2.y()) {
  150. draw_spaced_dots(point1.x(), point2.x(), [&](int dot_x) {
  151. return IntPoint { dot_x, point1.y() };
  152. });
  153. } else if (point1.x() == point2.x()) {
  154. draw_spaced_dots(point1.y(), point2.y(), [&](int dot_y) {
  155. return IntPoint { point1.x(), dot_y };
  156. });
  157. } else {
  158. TODO();
  159. }
  160. }
  161. void AntiAliasingPainter::draw_line(IntPoint actual_from, IntPoint actual_to, Color color, float thickness, Painter::LineStyle style, Color alternate_color, LineLengthMode line_length_mode)
  162. {
  163. draw_line(actual_from.to_type<float>(), actual_to.to_type<float>(), color, thickness, style, alternate_color, line_length_mode);
  164. }
  165. void AntiAliasingPainter::draw_line(FloatPoint actual_from, FloatPoint actual_to, Color color, float thickness, Painter::LineStyle style, Color alternate_color, LineLengthMode line_length_mode)
  166. {
  167. if (style == Painter::LineStyle::Dotted)
  168. return draw_dotted_line(actual_from.to_rounded<int>(), actual_to.to_rounded<int>(), color, static_cast<int>(round(thickness)));
  169. draw_anti_aliased_line(actual_from, actual_to, color, thickness, style, alternate_color, line_length_mode);
  170. }
  171. void AntiAliasingPainter::stroke_path(Path const& path, Color color, float thickness)
  172. {
  173. // FIXME: Cache this? Probably at a higher level such as in LibWeb?
  174. fill_path(path.stroke_to_fill(thickness), color);
  175. }
  176. void AntiAliasingPainter::fill_rect(FloatRect const& float_rect, Color color)
  177. {
  178. // Draw the integer part of the rectangle:
  179. float right_x = float_rect.x() + float_rect.width();
  180. float bottom_y = float_rect.y() + float_rect.height();
  181. int x1 = ceilf(float_rect.x());
  182. int y1 = ceilf(float_rect.y());
  183. int x2 = floorf(right_x);
  184. int y2 = floorf(bottom_y);
  185. auto solid_rect = Gfx::IntRect::from_two_points({ x1, y1 }, { x2, y2 });
  186. m_underlying_painter.fill_rect(solid_rect, color);
  187. if (float_rect == solid_rect)
  188. return;
  189. // Draw the rest:
  190. float left_subpixel = x1 - float_rect.x();
  191. float top_subpixel = y1 - float_rect.y();
  192. float right_subpixel = right_x - x2;
  193. float bottom_subpixel = bottom_y - y2;
  194. float top_left_subpixel = top_subpixel * left_subpixel;
  195. float top_right_subpixel = top_subpixel * right_subpixel;
  196. float bottom_left_subpixel = bottom_subpixel * left_subpixel;
  197. float bottom_right_subpixel = bottom_subpixel * right_subpixel;
  198. auto subpixel = [&](float alpha) {
  199. return color.with_alpha(color.alpha() * alpha);
  200. };
  201. auto set_pixel = [&](int x, int y, float alpha) {
  202. m_underlying_painter.set_pixel(x, y, subpixel(alpha), true);
  203. };
  204. auto line_to_rect = [&](int x1, int y1, int x2, int y2) {
  205. return IntRect::from_two_points({ x1, y1 }, { x2 + 1, y2 + 1 });
  206. };
  207. set_pixel(x1 - 1, y1 - 1, top_left_subpixel);
  208. set_pixel(x2, y1 - 1, top_right_subpixel);
  209. set_pixel(x2, y2, bottom_right_subpixel);
  210. set_pixel(x1 - 1, y2, bottom_left_subpixel);
  211. m_underlying_painter.fill_rect(line_to_rect(x1, y1 - 1, x2 - 1, y1 - 1), subpixel(top_subpixel));
  212. m_underlying_painter.fill_rect(line_to_rect(x1, y2, x2 - 1, y2), subpixel(bottom_subpixel));
  213. m_underlying_painter.fill_rect(line_to_rect(x1 - 1, y1, x1 - 1, y2 - 1), subpixel(left_subpixel));
  214. m_underlying_painter.fill_rect(line_to_rect(x2, y1, x2, y2 - 1), subpixel(right_subpixel));
  215. }
  216. void AntiAliasingPainter::draw_ellipse(IntRect const& a_rect, Color color, int thickness)
  217. {
  218. // FIXME: Come up with an allocation-free version of this!
  219. // Using draw_line() for segments of an ellipse was attempted but gave really poor results :^(
  220. // There probably is a way to adjust the fill of draw_ellipse_part() to do this, but getting it rendering correctly is tricky.
  221. // The outline of the steps required to paint it efficiently is:
  222. // - Paint the outer ellipse without the fill (from the fill() lambda in draw_ellipse_part())
  223. // - Paint the inner ellipse, but in the set_pixel() invert the alpha values
  224. // - Somehow fill in the gap between the two ellipses (the tricky part to get right)
  225. // - Have to avoid overlapping pixels and accidentally painting over some of the edge pixels
  226. auto color_no_alpha = color;
  227. color_no_alpha.set_alpha(255);
  228. auto outline_ellipse_bitmap = ({
  229. auto bitmap = Bitmap::create(BitmapFormat::BGRA8888, a_rect.size());
  230. if (bitmap.is_error())
  231. return warnln("Failed to allocate temporary bitmap for antialiased outline ellipse!");
  232. bitmap.release_value();
  233. });
  234. auto outer_rect = a_rect;
  235. outer_rect.set_location({ 0, 0 });
  236. auto inner_rect = outer_rect.shrunken(thickness * 2, thickness * 2);
  237. Painter painter { outline_ellipse_bitmap };
  238. AntiAliasingPainter aa_painter { painter };
  239. aa_painter.fill_ellipse(outer_rect, color_no_alpha);
  240. aa_painter.fill_ellipse(inner_rect, color_no_alpha, BlendMode::AlphaSubtract);
  241. m_underlying_painter.blit(a_rect.location(), outline_ellipse_bitmap, outline_ellipse_bitmap->rect(), color.alpha() / 255.);
  242. }
  243. void AntiAliasingPainter::fill_circle(IntPoint center, int radius, Color color, BlendMode blend_mode)
  244. {
  245. if (radius <= 0)
  246. return;
  247. draw_ellipse_part(center, radius, radius, color, false, {}, blend_mode);
  248. }
  249. void AntiAliasingPainter::fill_ellipse(IntRect const& a_rect, Color color, BlendMode blend_mode)
  250. {
  251. auto center = a_rect.center();
  252. auto radius_a = a_rect.width() / 2;
  253. auto radius_b = a_rect.height() / 2;
  254. if (radius_a <= 0 || radius_b <= 0)
  255. return;
  256. if (radius_a == radius_b)
  257. return fill_circle(center, radius_a, color, blend_mode);
  258. auto x_paint_range = draw_ellipse_part(center, radius_a, radius_b, color, false, {}, blend_mode);
  259. // FIXME: This paints some extra fill pixels that are clipped
  260. draw_ellipse_part(center, radius_b, radius_a, color, true, x_paint_range, blend_mode);
  261. }
  262. FLATTEN AntiAliasingPainter::Range AntiAliasingPainter::draw_ellipse_part(
  263. IntPoint center, int radius_a, int radius_b, Color color, bool flip_x_and_y, Optional<Range> x_clip, BlendMode blend_mode)
  264. {
  265. /*
  266. Algorithm from: https://cs.uwaterloo.ca/research/tr/1984/CS-84-38.pdf
  267. This method can draw a whole circle with a whole circle in one call using
  268. 8-way symmetry, or an ellipse in two calls using 4-way symmetry.
  269. */
  270. center *= m_underlying_painter.scale();
  271. radius_a *= m_underlying_painter.scale();
  272. radius_b *= m_underlying_painter.scale();
  273. // If this is a ellipse everything can be drawn in one pass with 8 way symmetry
  274. bool const is_circle = radius_a == radius_b;
  275. // These happen to be the same here, but are treated separately in the paper:
  276. // intensity is the fill alpha
  277. int const intensity = 255;
  278. // 0 to subpixel_resolution is the range of alpha values for the circle edges
  279. int const subpixel_resolution = intensity;
  280. // Current pixel address
  281. int i = 0;
  282. int q = radius_b;
  283. // 1st and 2nd order differences of y
  284. int delta_y = 0;
  285. int delta2_y = 0;
  286. int const a_squared = radius_a * radius_a;
  287. int const b_squared = radius_b * radius_b;
  288. // Exact and predicted values of f(i) -- the ellipse equation scaled by subpixel_resolution
  289. int y = subpixel_resolution * radius_b;
  290. int y_hat = 0;
  291. // The value of f(i)*f(i)
  292. int f_squared = y * y;
  293. // 1st and 2nd order differences of f(i)*f(i)
  294. int delta_f_squared = (static_cast<int64_t>(b_squared) * subpixel_resolution * subpixel_resolution) / a_squared;
  295. int delta2_f_squared = -delta_f_squared - delta_f_squared;
  296. // edge_intersection_area/subpixel_resolution = percentage of pixel intersected by circle
  297. // (aka the alpha for the pixel)
  298. int edge_intersection_area = 0;
  299. int old_area = edge_intersection_area;
  300. auto predict = [&] {
  301. delta_y += delta2_y;
  302. // y_hat is the predicted value of f(i)
  303. y_hat = y + delta_y;
  304. };
  305. auto minimize = [&] {
  306. // Initialize the minimization
  307. delta_f_squared += delta2_f_squared;
  308. f_squared += delta_f_squared;
  309. int min_squared_error = y_hat * y_hat - f_squared;
  310. int prediction_overshot = 1;
  311. y = y_hat;
  312. // Force error negative
  313. if (min_squared_error > 0) {
  314. min_squared_error = -min_squared_error;
  315. prediction_overshot = -1;
  316. }
  317. // Minimize
  318. int previous_error = min_squared_error;
  319. while (min_squared_error < 0) {
  320. y += prediction_overshot;
  321. previous_error = min_squared_error;
  322. min_squared_error += y + y - prediction_overshot;
  323. }
  324. if (min_squared_error + previous_error > 0)
  325. y -= prediction_overshot;
  326. };
  327. auto correct = [&] {
  328. int error = y - y_hat;
  329. delta2_y += error;
  330. delta_y += error;
  331. };
  332. int min_paint_x = NumericLimits<int>::max();
  333. int max_paint_x = NumericLimits<int>::min();
  334. auto pixel = [&](int x, int y, int alpha) {
  335. if (alpha <= 0 || alpha > 255)
  336. return;
  337. if (flip_x_and_y)
  338. swap(x, y);
  339. if (x_clip.has_value() && x_clip->contains_inclusive(x))
  340. return;
  341. min_paint_x = min(x, min_paint_x);
  342. max_paint_x = max(x, max_paint_x);
  343. alpha = (alpha * color.alpha()) / 255;
  344. if (blend_mode == BlendMode::AlphaSubtract)
  345. alpha = ~alpha;
  346. auto pixel_color = color;
  347. pixel_color.set_alpha(alpha);
  348. m_underlying_painter.set_pixel(center + IntPoint { x, y }, pixel_color, blend_mode == BlendMode::Normal);
  349. };
  350. auto fill = [&](int x, int ymax, int ymin, int alpha) {
  351. while (ymin <= ymax) {
  352. pixel(x, ymin, alpha);
  353. ymin += 1;
  354. }
  355. };
  356. auto symmetric_pixel = [&](int x, int y, int alpha) {
  357. pixel(x, y, alpha);
  358. pixel(x, -y - 1, alpha);
  359. pixel(-x - 1, -y - 1, alpha);
  360. pixel(-x - 1, y, alpha);
  361. if (is_circle) {
  362. pixel(y, x, alpha);
  363. pixel(y, -x - 1, alpha);
  364. pixel(-y - 1, -x - 1, alpha);
  365. pixel(-y - 1, x, alpha);
  366. }
  367. };
  368. // These are calculated incrementally (as it is possibly a tiny bit faster)
  369. int ib_squared = 0;
  370. int qa_squared = q * a_squared;
  371. auto in_symmetric_region = [&] {
  372. // Main fix two stop cond here
  373. return is_circle ? i < q : ib_squared < qa_squared;
  374. };
  375. // Draws a 8 octants for a circle or 4 quadrants for a (partial) ellipse
  376. while (in_symmetric_region()) {
  377. predict();
  378. minimize();
  379. correct();
  380. old_area = edge_intersection_area;
  381. edge_intersection_area += delta_y;
  382. if (edge_intersection_area >= 0) {
  383. // Single pixel on perimeter
  384. symmetric_pixel(i, q, (edge_intersection_area + old_area) / 2);
  385. fill(i, q - 1, -q, intensity);
  386. fill(-i - 1, q - 1, -q, intensity);
  387. } else {
  388. // Two pixels on perimeter
  389. edge_intersection_area += subpixel_resolution;
  390. symmetric_pixel(i, q, old_area / 2);
  391. q -= 1;
  392. qa_squared -= a_squared;
  393. fill(i, q - 1, -q, intensity);
  394. fill(-i - 1, q - 1, -q, intensity);
  395. if (!is_circle || in_symmetric_region()) {
  396. symmetric_pixel(i, q, (edge_intersection_area + subpixel_resolution) / 2);
  397. if (is_circle) {
  398. fill(q, i - 1, -i, intensity);
  399. fill(-q - 1, i - 1, -i, intensity);
  400. }
  401. } else {
  402. edge_intersection_area += subpixel_resolution;
  403. }
  404. }
  405. i += 1;
  406. ib_squared += b_squared;
  407. }
  408. if (is_circle) {
  409. int alpha = edge_intersection_area / 2;
  410. pixel(q, q, alpha);
  411. pixel(-q - 1, q, alpha);
  412. pixel(-q - 1, -q - 1, alpha);
  413. pixel(q, -q - 1, alpha);
  414. }
  415. return Range { min_paint_x, max_paint_x };
  416. }
  417. void AntiAliasingPainter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
  418. {
  419. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  420. }
  421. void AntiAliasingPainter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius)
  422. {
  423. fill_rect_with_rounded_corners(a_rect, color,
  424. { top_left_radius, top_left_radius },
  425. { top_right_radius, top_right_radius },
  426. { bottom_right_radius, bottom_right_radius },
  427. { bottom_left_radius, bottom_left_radius });
  428. }
  429. void AntiAliasingPainter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, CornerRadius top_left, CornerRadius top_right, CornerRadius bottom_right, CornerRadius bottom_left, BlendMode blend_mode)
  430. {
  431. if (!top_left && !top_right && !bottom_right && !bottom_left) {
  432. if (blend_mode == BlendMode::Normal)
  433. return m_underlying_painter.fill_rect(a_rect, color);
  434. else if (blend_mode == BlendMode::AlphaSubtract)
  435. return m_underlying_painter.clear_rect(a_rect, Color());
  436. }
  437. if (color.alpha() == 0)
  438. return;
  439. IntPoint top_left_corner {
  440. a_rect.x() + top_left.horizontal_radius,
  441. a_rect.y() + top_left.vertical_radius,
  442. };
  443. IntPoint top_right_corner {
  444. a_rect.x() + a_rect.width() - top_right.horizontal_radius,
  445. a_rect.y() + top_right.vertical_radius,
  446. };
  447. IntPoint bottom_left_corner {
  448. a_rect.x() + bottom_left.horizontal_radius,
  449. a_rect.y() + a_rect.height() - bottom_left.vertical_radius
  450. };
  451. IntPoint bottom_right_corner {
  452. a_rect.x() + a_rect.width() - bottom_right.horizontal_radius,
  453. a_rect.y() + a_rect.height() - bottom_right.vertical_radius
  454. };
  455. // All corners are centered at the same point, so this can be painted as a single ellipse.
  456. if (top_left_corner == top_right_corner && top_right_corner == bottom_left_corner && bottom_left_corner == bottom_right_corner)
  457. return fill_ellipse(a_rect, color, blend_mode);
  458. IntRect top_rect {
  459. a_rect.x() + top_left.horizontal_radius,
  460. a_rect.y(),
  461. a_rect.width() - top_left.horizontal_radius - top_right.horizontal_radius,
  462. top_left.vertical_radius
  463. };
  464. IntRect right_rect {
  465. a_rect.x() + a_rect.width() - top_right.horizontal_radius,
  466. a_rect.y() + top_right.vertical_radius,
  467. top_right.horizontal_radius,
  468. a_rect.height() - top_right.vertical_radius - bottom_right.vertical_radius
  469. };
  470. IntRect bottom_rect {
  471. a_rect.x() + bottom_left.horizontal_radius,
  472. a_rect.y() + a_rect.height() - bottom_right.vertical_radius,
  473. a_rect.width() - bottom_left.horizontal_radius - bottom_right.horizontal_radius,
  474. bottom_right.vertical_radius
  475. };
  476. IntRect left_rect {
  477. a_rect.x(),
  478. a_rect.y() + top_left.vertical_radius,
  479. bottom_left.horizontal_radius,
  480. a_rect.height() - top_left.vertical_radius - bottom_left.vertical_radius
  481. };
  482. IntRect inner = {
  483. left_rect.x() + left_rect.width(),
  484. left_rect.y(),
  485. a_rect.width() - left_rect.width() - right_rect.width(),
  486. a_rect.height() - top_rect.height() - bottom_rect.height()
  487. };
  488. if (blend_mode == BlendMode::Normal) {
  489. m_underlying_painter.fill_rect(top_rect, color);
  490. m_underlying_painter.fill_rect(right_rect, color);
  491. m_underlying_painter.fill_rect(bottom_rect, color);
  492. m_underlying_painter.fill_rect(left_rect, color);
  493. m_underlying_painter.fill_rect(inner, color);
  494. } else if (blend_mode == BlendMode::AlphaSubtract) {
  495. m_underlying_painter.clear_rect(top_rect, Color());
  496. m_underlying_painter.clear_rect(right_rect, Color());
  497. m_underlying_painter.clear_rect(bottom_rect, Color());
  498. m_underlying_painter.clear_rect(left_rect, Color());
  499. m_underlying_painter.clear_rect(inner, Color());
  500. }
  501. auto fill_corner = [&](auto const& ellipse_center, auto const& corner_point, CornerRadius const& corner) {
  502. PainterStateSaver save { m_underlying_painter };
  503. m_underlying_painter.add_clip_rect(IntRect::from_two_points(ellipse_center, corner_point));
  504. fill_ellipse(IntRect::centered_at(ellipse_center, { corner.horizontal_radius * 2, corner.vertical_radius * 2 }), color, blend_mode);
  505. };
  506. auto bounding_rect = a_rect.inflated(0, 1, 1, 0);
  507. if (top_left)
  508. fill_corner(top_left_corner, bounding_rect.top_left(), top_left);
  509. if (top_right)
  510. fill_corner(top_right_corner, bounding_rect.top_right().moved_left(1), top_right);
  511. if (bottom_left)
  512. fill_corner(bottom_left_corner, bounding_rect.bottom_left().moved_up(1), bottom_left);
  513. if (bottom_right)
  514. fill_corner(bottom_right_corner, bounding_rect.bottom_right().translated(-1), bottom_right);
  515. }
  516. }