AntiAliasingPainter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. if (thickness <= 0)
  174. return;
  175. // FIXME: Cache this? Probably at a higher level such as in LibWeb?
  176. fill_path(path.stroke_to_fill(thickness), color);
  177. }
  178. void AntiAliasingPainter::stroke_path(Path const& path, Gfx::PaintStyle const& paint_style, float thickness, float opacity)
  179. {
  180. if (thickness <= 0)
  181. return;
  182. // FIXME: Cache this? Probably at a higher level such as in LibWeb?
  183. fill_path(path.stroke_to_fill(thickness), paint_style, opacity);
  184. }
  185. void AntiAliasingPainter::fill_rect(FloatRect const& float_rect, Color color)
  186. {
  187. // Draw the integer part of the rectangle:
  188. float right_x = float_rect.x() + float_rect.width();
  189. float bottom_y = float_rect.y() + float_rect.height();
  190. int x1 = ceilf(float_rect.x());
  191. int y1 = ceilf(float_rect.y());
  192. int x2 = floorf(right_x);
  193. int y2 = floorf(bottom_y);
  194. auto solid_rect = Gfx::IntRect::from_two_points({ x1, y1 }, { x2, y2 });
  195. m_underlying_painter.fill_rect(solid_rect, color);
  196. if (float_rect == solid_rect)
  197. return;
  198. // Draw the rest:
  199. float left_subpixel = x1 - float_rect.x();
  200. float top_subpixel = y1 - float_rect.y();
  201. float right_subpixel = right_x - x2;
  202. float bottom_subpixel = bottom_y - y2;
  203. float top_left_subpixel = top_subpixel * left_subpixel;
  204. float top_right_subpixel = top_subpixel * right_subpixel;
  205. float bottom_left_subpixel = bottom_subpixel * left_subpixel;
  206. float bottom_right_subpixel = bottom_subpixel * right_subpixel;
  207. auto subpixel = [&](float alpha) {
  208. return color.with_alpha(color.alpha() * alpha);
  209. };
  210. auto set_pixel = [&](int x, int y, float alpha) {
  211. m_underlying_painter.set_pixel(x, y, subpixel(alpha), true);
  212. };
  213. auto line_to_rect = [&](int x1, int y1, int x2, int y2) {
  214. return IntRect::from_two_points({ x1, y1 }, { x2 + 1, y2 + 1 });
  215. };
  216. set_pixel(x1 - 1, y1 - 1, top_left_subpixel);
  217. set_pixel(x2, y1 - 1, top_right_subpixel);
  218. set_pixel(x2, y2, bottom_right_subpixel);
  219. set_pixel(x1 - 1, y2, bottom_left_subpixel);
  220. m_underlying_painter.fill_rect(line_to_rect(x1, y1 - 1, x2 - 1, y1 - 1), subpixel(top_subpixel));
  221. m_underlying_painter.fill_rect(line_to_rect(x1, y2, x2 - 1, y2), subpixel(bottom_subpixel));
  222. m_underlying_painter.fill_rect(line_to_rect(x1 - 1, y1, x1 - 1, y2 - 1), subpixel(left_subpixel));
  223. m_underlying_painter.fill_rect(line_to_rect(x2, y1, x2, y2 - 1), subpixel(right_subpixel));
  224. }
  225. void AntiAliasingPainter::draw_ellipse(IntRect const& a_rect, Color color, int thickness)
  226. {
  227. // FIXME: Come up with an allocation-free version of this!
  228. // Using draw_line() for segments of an ellipse was attempted but gave really poor results :^(
  229. // There probably is a way to adjust the fill of draw_ellipse_part() to do this, but getting it rendering correctly is tricky.
  230. // The outline of the steps required to paint it efficiently is:
  231. // - Paint the outer ellipse without the fill (from the fill() lambda in draw_ellipse_part())
  232. // - Paint the inner ellipse, but in the set_pixel() invert the alpha values
  233. // - Somehow fill in the gap between the two ellipses (the tricky part to get right)
  234. // - Have to avoid overlapping pixels and accidentally painting over some of the edge pixels
  235. auto color_no_alpha = color;
  236. color_no_alpha.set_alpha(255);
  237. auto outline_ellipse_bitmap = ({
  238. auto bitmap = Bitmap::create(BitmapFormat::BGRA8888, a_rect.size());
  239. if (bitmap.is_error())
  240. return warnln("Failed to allocate temporary bitmap for antialiased outline ellipse!");
  241. bitmap.release_value();
  242. });
  243. auto outer_rect = a_rect;
  244. outer_rect.set_location({ 0, 0 });
  245. auto inner_rect = outer_rect.shrunken(thickness * 2, thickness * 2);
  246. Painter painter { outline_ellipse_bitmap };
  247. AntiAliasingPainter aa_painter { painter };
  248. aa_painter.fill_ellipse(outer_rect, color_no_alpha);
  249. aa_painter.fill_ellipse(inner_rect, color_no_alpha, BlendMode::AlphaSubtract);
  250. m_underlying_painter.blit(a_rect.location(), outline_ellipse_bitmap, outline_ellipse_bitmap->rect(), color.alpha() / 255.);
  251. }
  252. void AntiAliasingPainter::fill_circle(IntPoint center, int radius, Color color, BlendMode blend_mode)
  253. {
  254. if (radius <= 0)
  255. return;
  256. draw_ellipse_part(center, radius, radius, color, false, {}, blend_mode);
  257. }
  258. void AntiAliasingPainter::fill_ellipse(IntRect const& a_rect, Color color, BlendMode blend_mode)
  259. {
  260. auto center = a_rect.center();
  261. auto radius_a = a_rect.width() / 2;
  262. auto radius_b = a_rect.height() / 2;
  263. if (radius_a <= 0 || radius_b <= 0)
  264. return;
  265. if (radius_a == radius_b)
  266. return fill_circle(center, radius_a, color, blend_mode);
  267. auto x_paint_range = draw_ellipse_part(center, radius_a, radius_b, color, false, {}, blend_mode);
  268. // FIXME: This paints some extra fill pixels that are clipped
  269. draw_ellipse_part(center, radius_b, radius_a, color, true, x_paint_range, blend_mode);
  270. }
  271. FLATTEN AntiAliasingPainter::Range AntiAliasingPainter::draw_ellipse_part(
  272. IntPoint center, int radius_a, int radius_b, Color color, bool flip_x_and_y, Optional<Range> x_clip, BlendMode blend_mode)
  273. {
  274. /*
  275. Algorithm from: https://cs.uwaterloo.ca/research/tr/1984/CS-84-38.pdf
  276. This method can draw a whole circle with a whole circle in one call using
  277. 8-way symmetry, or an ellipse in two calls using 4-way symmetry.
  278. */
  279. center *= m_underlying_painter.scale();
  280. radius_a *= m_underlying_painter.scale();
  281. radius_b *= m_underlying_painter.scale();
  282. // If this is a ellipse everything can be drawn in one pass with 8 way symmetry
  283. bool const is_circle = radius_a == radius_b;
  284. // These happen to be the same here, but are treated separately in the paper:
  285. // intensity is the fill alpha
  286. int const intensity = 255;
  287. // 0 to subpixel_resolution is the range of alpha values for the circle edges
  288. int const subpixel_resolution = intensity;
  289. // Current pixel address
  290. int i = 0;
  291. int q = radius_b;
  292. // 1st and 2nd order differences of y
  293. int delta_y = 0;
  294. int delta2_y = 0;
  295. int const a_squared = radius_a * radius_a;
  296. int const b_squared = radius_b * radius_b;
  297. // Exact and predicted values of f(i) -- the ellipse equation scaled by subpixel_resolution
  298. int y = subpixel_resolution * radius_b;
  299. int y_hat = 0;
  300. // The value of f(i)*f(i)
  301. int f_squared = y * y;
  302. // 1st and 2nd order differences of f(i)*f(i)
  303. int delta_f_squared = (static_cast<int64_t>(b_squared) * subpixel_resolution * subpixel_resolution) / a_squared;
  304. int delta2_f_squared = -delta_f_squared - delta_f_squared;
  305. // edge_intersection_area/subpixel_resolution = percentage of pixel intersected by circle
  306. // (aka the alpha for the pixel)
  307. int edge_intersection_area = 0;
  308. int old_area = edge_intersection_area;
  309. auto predict = [&] {
  310. delta_y += delta2_y;
  311. // y_hat is the predicted value of f(i)
  312. y_hat = y + delta_y;
  313. };
  314. auto minimize = [&] {
  315. // Initialize the minimization
  316. delta_f_squared += delta2_f_squared;
  317. f_squared += delta_f_squared;
  318. int min_squared_error = y_hat * y_hat - f_squared;
  319. int prediction_overshot = 1;
  320. y = y_hat;
  321. // Force error negative
  322. if (min_squared_error > 0) {
  323. min_squared_error = -min_squared_error;
  324. prediction_overshot = -1;
  325. }
  326. // Minimize
  327. int previous_error = min_squared_error;
  328. while (min_squared_error < 0) {
  329. y += prediction_overshot;
  330. previous_error = min_squared_error;
  331. min_squared_error += y + y - prediction_overshot;
  332. }
  333. if (min_squared_error + previous_error > 0)
  334. y -= prediction_overshot;
  335. };
  336. auto correct = [&] {
  337. int error = y - y_hat;
  338. delta2_y += error;
  339. delta_y += error;
  340. };
  341. int min_paint_x = NumericLimits<int>::max();
  342. int max_paint_x = NumericLimits<int>::min();
  343. auto pixel = [&](int x, int y, int alpha) {
  344. if (alpha <= 0 || alpha > 255)
  345. return;
  346. if (flip_x_and_y)
  347. swap(x, y);
  348. if (x_clip.has_value() && x_clip->contains_inclusive(x))
  349. return;
  350. min_paint_x = min(x, min_paint_x);
  351. max_paint_x = max(x, max_paint_x);
  352. alpha = (alpha * color.alpha()) / 255;
  353. if (blend_mode == BlendMode::AlphaSubtract)
  354. alpha = ~alpha;
  355. auto pixel_color = color;
  356. pixel_color.set_alpha(alpha);
  357. m_underlying_painter.set_pixel(center + IntPoint { x, y }, pixel_color, blend_mode == BlendMode::Normal);
  358. };
  359. auto fill = [&](int x, int ymax, int ymin, int alpha) {
  360. while (ymin <= ymax) {
  361. pixel(x, ymin, alpha);
  362. ymin += 1;
  363. }
  364. };
  365. auto symmetric_pixel = [&](int x, int y, int alpha) {
  366. pixel(x, y, alpha);
  367. pixel(x, -y - 1, alpha);
  368. pixel(-x - 1, -y - 1, alpha);
  369. pixel(-x - 1, y, alpha);
  370. if (is_circle) {
  371. pixel(y, x, alpha);
  372. pixel(y, -x - 1, alpha);
  373. pixel(-y - 1, -x - 1, alpha);
  374. pixel(-y - 1, x, alpha);
  375. }
  376. };
  377. // These are calculated incrementally (as it is possibly a tiny bit faster)
  378. int ib_squared = 0;
  379. int qa_squared = q * a_squared;
  380. auto in_symmetric_region = [&] {
  381. // Main fix two stop cond here
  382. return is_circle ? i < q : ib_squared < qa_squared;
  383. };
  384. // Draws a 8 octants for a circle or 4 quadrants for a (partial) ellipse
  385. while (in_symmetric_region()) {
  386. predict();
  387. minimize();
  388. correct();
  389. old_area = edge_intersection_area;
  390. edge_intersection_area += delta_y;
  391. if (edge_intersection_area >= 0) {
  392. // Single pixel on perimeter
  393. symmetric_pixel(i, q, (edge_intersection_area + old_area) / 2);
  394. fill(i, q - 1, -q, intensity);
  395. fill(-i - 1, q - 1, -q, intensity);
  396. } else {
  397. // Two pixels on perimeter
  398. edge_intersection_area += subpixel_resolution;
  399. symmetric_pixel(i, q, old_area / 2);
  400. q -= 1;
  401. qa_squared -= a_squared;
  402. fill(i, q - 1, -q, intensity);
  403. fill(-i - 1, q - 1, -q, intensity);
  404. if (!is_circle || in_symmetric_region()) {
  405. symmetric_pixel(i, q, (edge_intersection_area + subpixel_resolution) / 2);
  406. if (is_circle) {
  407. fill(q, i - 1, -i, intensity);
  408. fill(-q - 1, i - 1, -i, intensity);
  409. }
  410. } else {
  411. edge_intersection_area += subpixel_resolution;
  412. }
  413. }
  414. i += 1;
  415. ib_squared += b_squared;
  416. }
  417. if (is_circle) {
  418. int alpha = edge_intersection_area / 2;
  419. pixel(q, q, alpha);
  420. pixel(-q - 1, q, alpha);
  421. pixel(-q - 1, -q - 1, alpha);
  422. pixel(q, -q - 1, alpha);
  423. }
  424. return Range { min_paint_x, max_paint_x };
  425. }
  426. void AntiAliasingPainter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
  427. {
  428. fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  429. }
  430. 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)
  431. {
  432. fill_rect_with_rounded_corners(a_rect, color,
  433. { top_left_radius, top_left_radius },
  434. { top_right_radius, top_right_radius },
  435. { bottom_right_radius, bottom_right_radius },
  436. { bottom_left_radius, bottom_left_radius });
  437. }
  438. 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)
  439. {
  440. if (!top_left && !top_right && !bottom_right && !bottom_left) {
  441. if (blend_mode == BlendMode::Normal)
  442. return m_underlying_painter.fill_rect(a_rect, color);
  443. else if (blend_mode == BlendMode::AlphaSubtract)
  444. return m_underlying_painter.clear_rect(a_rect, Color());
  445. }
  446. if (color.alpha() == 0)
  447. return;
  448. IntPoint top_left_corner {
  449. a_rect.x() + top_left.horizontal_radius,
  450. a_rect.y() + top_left.vertical_radius,
  451. };
  452. IntPoint top_right_corner {
  453. a_rect.x() + a_rect.width() - top_right.horizontal_radius,
  454. a_rect.y() + top_right.vertical_radius,
  455. };
  456. IntPoint bottom_left_corner {
  457. a_rect.x() + bottom_left.horizontal_radius,
  458. a_rect.y() + a_rect.height() - bottom_left.vertical_radius
  459. };
  460. IntPoint bottom_right_corner {
  461. a_rect.x() + a_rect.width() - bottom_right.horizontal_radius,
  462. a_rect.y() + a_rect.height() - bottom_right.vertical_radius
  463. };
  464. // All corners are centered at the same point, so this can be painted as a single ellipse.
  465. if (top_left_corner == top_right_corner && top_right_corner == bottom_left_corner && bottom_left_corner == bottom_right_corner)
  466. return fill_ellipse(a_rect, color, blend_mode);
  467. IntRect top_rect {
  468. a_rect.x() + top_left.horizontal_radius,
  469. a_rect.y(),
  470. a_rect.width() - top_left.horizontal_radius - top_right.horizontal_radius,
  471. top_left.vertical_radius
  472. };
  473. IntRect right_rect {
  474. a_rect.x() + a_rect.width() - top_right.horizontal_radius,
  475. a_rect.y() + top_right.vertical_radius,
  476. top_right.horizontal_radius,
  477. a_rect.height() - top_right.vertical_radius - bottom_right.vertical_radius
  478. };
  479. IntRect bottom_rect {
  480. a_rect.x() + bottom_left.horizontal_radius,
  481. a_rect.y() + a_rect.height() - bottom_right.vertical_radius,
  482. a_rect.width() - bottom_left.horizontal_radius - bottom_right.horizontal_radius,
  483. bottom_right.vertical_radius
  484. };
  485. IntRect left_rect {
  486. a_rect.x(),
  487. a_rect.y() + top_left.vertical_radius,
  488. bottom_left.horizontal_radius,
  489. a_rect.height() - top_left.vertical_radius - bottom_left.vertical_radius
  490. };
  491. IntRect inner = {
  492. left_rect.x() + left_rect.width(),
  493. left_rect.y(),
  494. a_rect.width() - left_rect.width() - right_rect.width(),
  495. a_rect.height() - top_rect.height() - bottom_rect.height()
  496. };
  497. if (blend_mode == BlendMode::Normal) {
  498. m_underlying_painter.fill_rect(top_rect, color);
  499. m_underlying_painter.fill_rect(right_rect, color);
  500. m_underlying_painter.fill_rect(bottom_rect, color);
  501. m_underlying_painter.fill_rect(left_rect, color);
  502. m_underlying_painter.fill_rect(inner, color);
  503. } else if (blend_mode == BlendMode::AlphaSubtract) {
  504. m_underlying_painter.clear_rect(top_rect, Color());
  505. m_underlying_painter.clear_rect(right_rect, Color());
  506. m_underlying_painter.clear_rect(bottom_rect, Color());
  507. m_underlying_painter.clear_rect(left_rect, Color());
  508. m_underlying_painter.clear_rect(inner, Color());
  509. }
  510. auto fill_corner = [&](auto const& ellipse_center, auto const& corner_point, CornerRadius const& corner) {
  511. PainterStateSaver save { m_underlying_painter };
  512. m_underlying_painter.add_clip_rect(IntRect::from_two_points(ellipse_center, corner_point));
  513. fill_ellipse(IntRect::centered_at(ellipse_center, { corner.horizontal_radius * 2, corner.vertical_radius * 2 }), color, blend_mode);
  514. };
  515. auto bounding_rect = a_rect.inflated(0, 1, 1, 0);
  516. if (top_left)
  517. fill_corner(top_left_corner, bounding_rect.top_left(), top_left);
  518. if (top_right)
  519. fill_corner(top_right_corner, bounding_rect.top_right().moved_left(1), top_right);
  520. if (bottom_left)
  521. fill_corner(bottom_left_corner, bounding_rect.bottom_left().moved_up(1), bottom_left);
  522. if (bottom_right)
  523. fill_corner(bottom_right_corner, bounding_rect.bottom_right().translated(-1), bottom_right);
  524. }
  525. }