DeprecatedPainter.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  5. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  6. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  7. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  8. * Copyright (c) 2022, Jelle Raaijmakers <jelle@ladybird.org>
  9. *
  10. * SPDX-License-Identifier: BSD-2-Clause
  11. */
  12. #include "DeprecatedPainter.h"
  13. #include "Bitmap.h"
  14. #include <AK/Assertions.h>
  15. #include <AK/Function.h>
  16. #include <AK/Math.h>
  17. #include <AK/Memory.h>
  18. #include <AK/StdLibExtras.h>
  19. #include <LibGfx/DeprecatedPath.h>
  20. #include <LibGfx/ScalingMode.h>
  21. #include <stdio.h>
  22. #if defined(AK_COMPILER_GCC)
  23. # pragma GCC optimize("O3")
  24. #endif
  25. namespace Gfx {
  26. template<BitmapFormat format = BitmapFormat::Invalid>
  27. ALWAYS_INLINE Color get_pixel(Gfx::Bitmap const& bitmap, int x, int y)
  28. {
  29. if constexpr (format == BitmapFormat::BGRx8888)
  30. return Color::from_rgb(bitmap.scanline(y)[x]);
  31. if constexpr (format == BitmapFormat::BGRA8888)
  32. return Color::from_argb(bitmap.scanline(y)[x]);
  33. return bitmap.get_pixel(x, y);
  34. }
  35. DeprecatedPainter::DeprecatedPainter(Gfx::Bitmap& bitmap)
  36. : m_target(bitmap)
  37. {
  38. VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRx8888 || bitmap.format() == Gfx::BitmapFormat::BGRA8888);
  39. m_state_stack.append(State());
  40. state().clip_rect = { { 0, 0 }, bitmap.size() };
  41. }
  42. void DeprecatedPainter::clear_rect(IntRect const& a_rect, Color color)
  43. {
  44. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  45. if (rect.is_empty())
  46. return;
  47. VERIFY(target().rect().contains(rect));
  48. ARGB32* dst = target().scanline(rect.top()) + rect.left();
  49. size_t const dst_skip = target().pitch() / sizeof(ARGB32);
  50. for (int i = rect.height() - 1; i >= 0; --i) {
  51. fast_u32_fill(dst, color.value(), rect.width());
  52. dst += dst_skip;
  53. }
  54. }
  55. void DeprecatedPainter::fill_physical_rect(IntRect const& physical_rect, Color color)
  56. {
  57. // Callers must do clipping.
  58. ARGB32* dst = target().scanline(physical_rect.top()) + physical_rect.left();
  59. size_t const dst_skip = target().pitch() / sizeof(ARGB32);
  60. auto dst_format = target().format();
  61. for (int i = physical_rect.height() - 1; i >= 0; --i) {
  62. for (int j = 0; j < physical_rect.width(); ++j)
  63. dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
  64. dst += dst_skip;
  65. }
  66. }
  67. void DeprecatedPainter::fill_rect(IntRect const& a_rect, Color color)
  68. {
  69. if (color.alpha() == 0)
  70. return;
  71. if (color.alpha() == 0xff) {
  72. clear_rect(a_rect, color);
  73. return;
  74. }
  75. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  76. if (rect.is_empty())
  77. return;
  78. VERIFY(target().rect().contains(rect));
  79. fill_physical_rect(rect, color);
  80. }
  81. void DeprecatedPainter::fill_rect(IntRect const& rect, PaintStyle const& paint_style)
  82. {
  83. auto a_rect = rect.translated(translation());
  84. auto clipped_rect = a_rect.intersected(clip_rect());
  85. if (clipped_rect.is_empty())
  86. return;
  87. auto start_offset = clipped_rect.location() - a_rect.location();
  88. paint_style.paint(a_rect, [&](PaintStyle::SamplerFunction sample) {
  89. for (int y = 0; y < clipped_rect.height(); ++y) {
  90. for (int x = 0; x < clipped_rect.width(); ++x) {
  91. IntPoint point(x, y);
  92. set_physical_pixel(point + clipped_rect.location(), sample(point + start_offset), true);
  93. }
  94. }
  95. });
  96. }
  97. void DeprecatedPainter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
  98. {
  99. return fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  100. }
  101. void DeprecatedPainter::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)
  102. {
  103. // Fasttrack for rects without any border radii
  104. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius)
  105. return fill_rect(a_rect, color);
  106. // Fully transparent, dont care.
  107. if (color.alpha() == 0)
  108. return;
  109. // FIXME: Allow for elliptically rounded corners
  110. IntRect top_left_corner = {
  111. a_rect.x(),
  112. a_rect.y(),
  113. top_left_radius,
  114. top_left_radius
  115. };
  116. IntRect top_right_corner = {
  117. a_rect.x() + a_rect.width() - top_right_radius,
  118. a_rect.y(),
  119. top_right_radius,
  120. top_right_radius
  121. };
  122. IntRect bottom_right_corner = {
  123. a_rect.x() + a_rect.width() - bottom_right_radius,
  124. a_rect.y() + a_rect.height() - bottom_right_radius,
  125. bottom_right_radius,
  126. bottom_right_radius
  127. };
  128. IntRect bottom_left_corner = {
  129. a_rect.x(),
  130. a_rect.y() + a_rect.height() - bottom_left_radius,
  131. bottom_left_radius,
  132. bottom_left_radius
  133. };
  134. IntRect top_rect = {
  135. a_rect.x() + top_left_radius,
  136. a_rect.y(),
  137. a_rect.width() - top_left_radius - top_right_radius, top_left_radius
  138. };
  139. IntRect right_rect = {
  140. a_rect.x() + a_rect.width() - top_right_radius,
  141. a_rect.y() + top_right_radius,
  142. top_right_radius,
  143. a_rect.height() - top_right_radius - bottom_right_radius
  144. };
  145. IntRect bottom_rect = {
  146. a_rect.x() + bottom_left_radius,
  147. a_rect.y() + a_rect.height() - bottom_right_radius,
  148. a_rect.width() - bottom_left_radius - bottom_right_radius,
  149. bottom_right_radius
  150. };
  151. IntRect left_rect = {
  152. a_rect.x(),
  153. a_rect.y() + top_left_radius,
  154. bottom_left_radius,
  155. a_rect.height() - top_left_radius - bottom_left_radius
  156. };
  157. IntRect inner = {
  158. left_rect.x() + left_rect.width(),
  159. left_rect.y(),
  160. a_rect.width() - left_rect.width() - right_rect.width(),
  161. a_rect.height() - top_rect.height() - bottom_rect.height()
  162. };
  163. fill_rect(top_rect, color);
  164. fill_rect(right_rect, color);
  165. fill_rect(bottom_rect, color);
  166. fill_rect(left_rect, color);
  167. fill_rect(inner, color);
  168. if (top_left_radius)
  169. fill_rounded_corner(top_left_corner, top_left_radius, color, CornerOrientation::TopLeft);
  170. if (top_right_radius)
  171. fill_rounded_corner(top_right_corner, top_right_radius, color, CornerOrientation::TopRight);
  172. if (bottom_left_radius)
  173. fill_rounded_corner(bottom_left_corner, bottom_left_radius, color, CornerOrientation::BottomLeft);
  174. if (bottom_right_radius)
  175. fill_rounded_corner(bottom_right_corner, bottom_right_radius, color, CornerOrientation::BottomRight);
  176. }
  177. void DeprecatedPainter::fill_rounded_corner(IntRect const& a_rect, int radius, Color color, CornerOrientation orientation)
  178. {
  179. // Care about clipping
  180. auto translated_a_rect = a_rect.translated(translation());
  181. auto rect = translated_a_rect.intersected(clip_rect());
  182. if (rect.is_empty())
  183. return;
  184. VERIFY(target().rect().contains(rect));
  185. // We got cut on the top!
  186. // FIXME: Also account for clipping on the x-axis
  187. int clip_offset = 0;
  188. if (translated_a_rect.y() < rect.y())
  189. clip_offset = rect.y() - translated_a_rect.y();
  190. ARGB32* dst = target().scanline(rect.top()) + rect.left();
  191. size_t const dst_skip = target().pitch() / sizeof(ARGB32);
  192. IntPoint circle_center;
  193. switch (orientation) {
  194. case CornerOrientation::TopLeft:
  195. circle_center = { radius, radius + 1 };
  196. break;
  197. case CornerOrientation::TopRight:
  198. circle_center = { -1, radius + 1 };
  199. break;
  200. case CornerOrientation::BottomRight:
  201. circle_center = { -1, 0 };
  202. break;
  203. case CornerOrientation::BottomLeft:
  204. circle_center = { radius, 0 };
  205. break;
  206. default:
  207. VERIFY_NOT_REACHED();
  208. }
  209. int radius2 = radius * radius;
  210. auto is_in_circle = [&](int x, int y) {
  211. int distance2 = (circle_center.x() - x) * (circle_center.x() - x) + (circle_center.y() - y) * (circle_center.y() - y);
  212. // To reflect the grid and be compatible with the draw_circle_arc_intersecting algorithm
  213. // add 1/2 to the radius
  214. return distance2 <= (radius2 + radius + 0.25);
  215. };
  216. auto dst_format = target().format();
  217. for (int i = rect.height() - 1; i >= 0; --i) {
  218. for (int j = 0; j < rect.width(); ++j)
  219. if (is_in_circle(j, rect.height() - i + clip_offset))
  220. dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
  221. dst += dst_skip;
  222. }
  223. }
  224. // The callback will only be called for a quarter of the ellipse, the user is intended to deduce other points.
  225. // As the coordinate space is relative to the center of the rectangle, it's simply (x, y), (x, -y), (-x, y) and (-x, -y).
  226. static void on_each_ellipse_point(IntRect const& rect, Function<void(IntPoint)>&& callback)
  227. {
  228. // Note: This is an implementation of the Midpoint Ellipse Algorithm.
  229. double const a = rect.width() / 2;
  230. double const a_square = a * a;
  231. double const b = rect.height() / 2;
  232. double const b_square = b * b;
  233. int x = 0;
  234. auto y = static_cast<int>(b);
  235. double dx = 2 * b_square * x;
  236. double dy = 2 * a_square * y;
  237. // For region 1:
  238. auto decision_parameter = b_square - a_square * b + .25 * a_square;
  239. while (dx < dy) {
  240. callback({ x, y });
  241. if (decision_parameter >= 0) {
  242. y--;
  243. dy -= 2 * a_square;
  244. decision_parameter -= dy;
  245. }
  246. x++;
  247. dx += 2 * b_square;
  248. decision_parameter += dx + b_square;
  249. }
  250. // For region 2:
  251. decision_parameter = b_square * ((x + 0.5) * (x + 0.5)) + a_square * ((y - 1) * (y - 1)) - a_square * b_square;
  252. while (y >= 0) {
  253. callback({ x, y });
  254. if (decision_parameter <= 0) {
  255. x++;
  256. dx += 2 * b_square;
  257. decision_parameter += dx;
  258. }
  259. y--;
  260. dy -= 2 * a_square;
  261. decision_parameter += a_square - dy;
  262. }
  263. }
  264. void DeprecatedPainter::fill_ellipse(IntRect const& a_rect, Color color)
  265. {
  266. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  267. if (rect.is_empty())
  268. return;
  269. VERIFY(target().rect().contains(rect));
  270. auto const center = a_rect.center();
  271. on_each_ellipse_point(rect, [this, &color, center](IntPoint position) {
  272. IntPoint const directions[4] = { { position.x(), position.y() }, { -position.x(), position.y() }, { position.x(), -position.y() }, { -position.x(), -position.y() } };
  273. draw_line(center + directions[0], center + directions[1], color);
  274. draw_line(center + directions[2], center + directions[3], color);
  275. });
  276. }
  277. template<typename RectType, typename Callback>
  278. static void for_each_pixel_around_rect_clockwise(RectType const& rect, Callback callback)
  279. {
  280. if (rect.is_empty())
  281. return;
  282. for (auto x = rect.left(); x < rect.right(); ++x)
  283. callback(x, rect.top());
  284. for (auto y = rect.top() + 1; y < rect.bottom(); ++y)
  285. callback(rect.right() - 1, y);
  286. for (auto x = rect.right() - 2; x >= rect.left(); --x)
  287. callback(x, rect.bottom() - 1);
  288. for (auto y = rect.bottom() - 2; y > rect.top(); --y)
  289. callback(rect.left(), y);
  290. }
  291. void DeprecatedPainter::draw_rect(IntRect const& a_rect, Color color, bool rough)
  292. {
  293. IntRect rect = a_rect.translated(translation());
  294. auto clipped_rect = rect.intersected(clip_rect());
  295. if (clipped_rect.is_empty())
  296. return;
  297. int min_y = clipped_rect.top();
  298. int max_y = clipped_rect.bottom() - 1;
  299. if (rect.top() >= clipped_rect.top() && rect.top() < clipped_rect.bottom()) {
  300. int width = rough ? max(0, min(rect.width() - 2, clipped_rect.width())) : clipped_rect.width();
  301. if (width > 0) {
  302. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  303. fill_physical_scanline(rect.top(), start_x, width, color);
  304. }
  305. ++min_y;
  306. }
  307. if (rect.bottom() > clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  308. int width = rough ? max(0, min(rect.width() - 2, clipped_rect.width())) : clipped_rect.width();
  309. if (width > 0) {
  310. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  311. fill_physical_scanline(max_y, start_x, width, color);
  312. }
  313. --max_y;
  314. }
  315. bool draw_left_side = rect.left() >= clipped_rect.left();
  316. bool draw_right_side = rect.right() == clipped_rect.right();
  317. if (draw_left_side && draw_right_side) {
  318. // Specialized loop when drawing both sides.
  319. for (int y = min_y; y <= max_y; ++y) {
  320. auto* bits = target().scanline(y);
  321. set_physical_pixel(bits[rect.left()], color);
  322. set_physical_pixel(bits[(rect.right() - 1)], color);
  323. }
  324. } else {
  325. for (int y = min_y; y <= max_y; ++y) {
  326. auto* bits = target().scanline(y);
  327. if (draw_left_side)
  328. set_physical_pixel(bits[rect.left()], color);
  329. if (draw_right_side)
  330. set_physical_pixel(bits[(rect.right() - 1)], color);
  331. }
  332. }
  333. }
  334. struct BlitState {
  335. enum AlphaState {
  336. NoAlpha = 0,
  337. SrcAlpha = 1,
  338. DstAlpha = 2,
  339. BothAlpha = SrcAlpha | DstAlpha
  340. };
  341. ARGB32 const* src;
  342. ARGB32* dst;
  343. size_t src_pitch;
  344. size_t dst_pitch;
  345. int row_count;
  346. int column_count;
  347. float opacity;
  348. BitmapFormat src_format;
  349. };
  350. // FIXME: This is a hack to support blit_with_opacity() with RGBA8888 source.
  351. // Ideally we'd have a more generic solution that allows any source format.
  352. static void swap_red_and_blue_channels(Color& color)
  353. {
  354. u32 rgba = color.value();
  355. u32 bgra = (rgba & 0xff00ff00)
  356. | ((rgba & 0x000000ff) << 16)
  357. | ((rgba & 0x00ff0000) >> 16);
  358. color = Color::from_argb(bgra);
  359. }
  360. // FIXME: This function is very unoptimized.
  361. template<BlitState::AlphaState has_alpha>
  362. static void do_blit_with_opacity(BlitState& state)
  363. {
  364. for (int row = 0; row < state.row_count; ++row) {
  365. for (int x = 0; x < state.column_count; ++x) {
  366. Color dest_color = (has_alpha & BlitState::DstAlpha) ? Color::from_argb(state.dst[x]) : Color::from_rgb(state.dst[x]);
  367. if constexpr (has_alpha & BlitState::SrcAlpha) {
  368. Color src_color_with_alpha = Color::from_argb(state.src[x]);
  369. if (state.src_format == BitmapFormat::RGBA8888)
  370. swap_red_and_blue_channels(src_color_with_alpha);
  371. float pixel_opacity = src_color_with_alpha.alpha() / 255.0;
  372. src_color_with_alpha.set_alpha(255 * (state.opacity * pixel_opacity));
  373. state.dst[x] = dest_color.blend(src_color_with_alpha).value();
  374. } else {
  375. Color src_color_with_alpha = Color::from_rgb(state.src[x]);
  376. if (state.src_format == BitmapFormat::RGBA8888)
  377. swap_red_and_blue_channels(src_color_with_alpha);
  378. src_color_with_alpha.set_alpha(state.opacity * 255);
  379. state.dst[x] = dest_color.blend(src_color_with_alpha).value();
  380. }
  381. }
  382. state.dst += state.dst_pitch;
  383. state.src += state.src_pitch;
  384. }
  385. }
  386. void DeprecatedPainter::blit_with_opacity(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect, float opacity, bool apply_alpha)
  387. {
  388. if (opacity >= 1.0f && !(source.has_alpha_channel() && apply_alpha))
  389. return blit(position, source, src_rect);
  390. IntRect safe_src_rect = IntRect::intersection(src_rect, source.rect());
  391. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  392. auto clipped_rect = dst_rect.intersected(clip_rect());
  393. if (clipped_rect.is_empty())
  394. return;
  395. int const first_row = clipped_rect.top() - dst_rect.top();
  396. int const last_row = clipped_rect.bottom() - dst_rect.top();
  397. int const first_column = clipped_rect.left() - dst_rect.left();
  398. int const last_column = clipped_rect.right() - dst_rect.left();
  399. BlitState blit_state {
  400. .src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column,
  401. .dst = target().scanline(clipped_rect.y()) + clipped_rect.x(),
  402. .src_pitch = source.pitch() / sizeof(ARGB32),
  403. .dst_pitch = target().pitch() / sizeof(ARGB32),
  404. .row_count = last_row - first_row,
  405. .column_count = last_column - first_column,
  406. .opacity = opacity,
  407. .src_format = source.format(),
  408. };
  409. if (source.has_alpha_channel() && apply_alpha) {
  410. if (target().has_alpha_channel())
  411. do_blit_with_opacity<BlitState::BothAlpha>(blit_state);
  412. else
  413. do_blit_with_opacity<BlitState::SrcAlpha>(blit_state);
  414. } else {
  415. if (target().has_alpha_channel())
  416. do_blit_with_opacity<BlitState::DstAlpha>(blit_state);
  417. else
  418. do_blit_with_opacity<BlitState::NoAlpha>(blit_state);
  419. }
  420. }
  421. void DeprecatedPainter::blit_filtered(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect, Function<Color(Color)> const& filter, bool apply_alpha)
  422. {
  423. IntRect safe_src_rect = src_rect.intersected(source.rect());
  424. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  425. auto clipped_rect = dst_rect.intersected(clip_rect());
  426. if (clipped_rect.is_empty())
  427. return;
  428. int const first_row = clipped_rect.top() - dst_rect.top();
  429. int const last_row = clipped_rect.bottom() - dst_rect.top();
  430. int const first_column = clipped_rect.left() - dst_rect.left();
  431. int const last_column = clipped_rect.right() - dst_rect.left();
  432. ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
  433. size_t const dst_skip = target().pitch() / sizeof(ARGB32);
  434. auto dst_format = target().format();
  435. auto src_format = source.format();
  436. ARGB32 const* src = source.scanline(safe_src_rect.top() + first_row) + safe_src_rect.left() + first_column;
  437. size_t const src_skip = source.pitch() / sizeof(ARGB32);
  438. for (int row = first_row; row < last_row; ++row) {
  439. for (int x = 0; x < (last_column - first_column); ++x) {
  440. auto source_color = color_for_format(src_format, src[x]);
  441. if (source_color.alpha() == 0)
  442. continue;
  443. auto filtered_color = filter(source_color);
  444. if (!apply_alpha || filtered_color.alpha() == 0xff)
  445. dst[x] = filtered_color.value();
  446. else
  447. dst[x] = color_for_format(dst_format, dst[x]).blend(filtered_color).value();
  448. }
  449. dst += dst_skip;
  450. src += src_skip;
  451. }
  452. }
  453. void DeprecatedPainter::blit(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect, float opacity, bool apply_alpha)
  454. {
  455. if (opacity < 1.0f || (source.has_alpha_channel() && apply_alpha))
  456. return blit_with_opacity(position, source, src_rect, opacity, apply_alpha);
  457. auto safe_src_rect = src_rect.intersected(source.rect());
  458. // If we get here, the DeprecatedPainter might have a scale factor, but the source bitmap has the same scale factor.
  459. // We need to transform from logical to physical coordinates, but we can just copy pixels without resampling.
  460. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  461. auto clipped_rect = dst_rect.intersected(clip_rect());
  462. if (clipped_rect.is_empty())
  463. return;
  464. int const first_row = clipped_rect.top() - dst_rect.top();
  465. int const last_row = clipped_rect.bottom() - dst_rect.top();
  466. int const first_column = clipped_rect.left() - dst_rect.left();
  467. ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
  468. size_t const dst_skip = target().pitch() / sizeof(ARGB32);
  469. if (source.format() == BitmapFormat::BGRx8888 || source.format() == BitmapFormat::BGRA8888) {
  470. ARGB32 const* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  471. size_t const src_skip = source.pitch() / sizeof(ARGB32);
  472. for (int row = first_row; row < last_row; ++row) {
  473. memcpy(dst, src, sizeof(ARGB32) * clipped_rect.width());
  474. dst += dst_skip;
  475. src += src_skip;
  476. }
  477. return;
  478. }
  479. if (source.format() == BitmapFormat::RGBA8888) {
  480. u32 const* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  481. size_t const src_skip = source.pitch() / sizeof(u32);
  482. for (int row = first_row; row < last_row; ++row) {
  483. for (int i = 0; i < clipped_rect.width(); ++i) {
  484. u32 rgba = src[i];
  485. u32 bgra = (rgba & 0xff00ff00)
  486. | ((rgba & 0x000000ff) << 16)
  487. | ((rgba & 0x00ff0000) >> 16);
  488. dst[i] = bgra;
  489. }
  490. dst += dst_skip;
  491. src += src_skip;
  492. }
  493. return;
  494. }
  495. VERIFY_NOT_REACHED();
  496. }
  497. template<bool has_alpha_channel, typename GetPixel>
  498. ALWAYS_INLINE static void do_draw_integer_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& src_rect, Gfx::Bitmap const& source, int hfactor, int vfactor, GetPixel get_pixel, float opacity)
  499. {
  500. bool has_opacity = opacity != 1.0f;
  501. for (int y = 0; y < src_rect.height(); ++y) {
  502. int dst_y = dst_rect.y() + y * vfactor;
  503. for (int x = 0; x < src_rect.width(); ++x) {
  504. auto src_pixel = get_pixel(source, x + src_rect.left(), y + src_rect.top());
  505. if (has_opacity)
  506. src_pixel.set_alpha(src_pixel.alpha() * opacity);
  507. for (int yo = 0; yo < vfactor; ++yo) {
  508. auto* scanline = (Color*)target.scanline(dst_y + yo);
  509. int dst_x = dst_rect.x() + x * hfactor;
  510. for (int xo = 0; xo < hfactor; ++xo) {
  511. if constexpr (has_alpha_channel)
  512. scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel);
  513. else
  514. scanline[dst_x + xo] = src_pixel;
  515. }
  516. }
  517. }
  518. }
  519. }
  520. template<bool has_alpha_channel, typename GetPixel>
  521. ALWAYS_INLINE static void do_draw_box_sampled_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity)
  522. {
  523. float source_pixel_width = src_rect.width() / dst_rect.width();
  524. float source_pixel_height = src_rect.height() / dst_rect.height();
  525. float source_pixel_area = source_pixel_width * source_pixel_height;
  526. FloatRect const pixel_box = { 0.f, 0.f, 1.f, 1.f };
  527. for (int y = clipped_rect.top(); y < clipped_rect.bottom(); ++y) {
  528. auto* scanline = reinterpret_cast<Color*>(target.scanline(y));
  529. for (int x = clipped_rect.left(); x < clipped_rect.right(); ++x) {
  530. // Project the destination pixel in the source image
  531. FloatRect const source_box = {
  532. src_rect.left() + (x - dst_rect.x()) * source_pixel_width,
  533. src_rect.top() + (y - dst_rect.y()) * source_pixel_height,
  534. source_pixel_width,
  535. source_pixel_height,
  536. };
  537. IntRect enclosing_source_box = enclosing_int_rect(source_box).intersected(source.rect());
  538. // Sum the contribution of all source pixels inside the projected pixel
  539. float red_accumulator = 0.f;
  540. float green_accumulator = 0.f;
  541. float blue_accumulator = 0.f;
  542. float total_area = 0.f;
  543. for (int sy = enclosing_source_box.y(); sy < enclosing_source_box.bottom(); ++sy) {
  544. for (int sx = enclosing_source_box.x(); sx < enclosing_source_box.right(); ++sx) {
  545. float area = source_box.intersected(pixel_box.translated(sx, sy)).size().area();
  546. auto pixel = get_pixel(source, sx, sy);
  547. area *= pixel.alpha() / 255.f;
  548. red_accumulator += pixel.red() * area;
  549. green_accumulator += pixel.green() * area;
  550. blue_accumulator += pixel.blue() * area;
  551. total_area += area;
  552. }
  553. }
  554. Color src_pixel = {
  555. round_to<u8>(min(red_accumulator / total_area, 255.f)),
  556. round_to<u8>(min(green_accumulator / total_area, 255.f)),
  557. round_to<u8>(min(blue_accumulator / total_area, 255.f)),
  558. round_to<u8>(min(total_area * 255.f / source_pixel_area * opacity, 255.f)),
  559. };
  560. if constexpr (has_alpha_channel)
  561. scanline[x] = scanline[x].blend(src_pixel);
  562. else
  563. scanline[x] = src_pixel;
  564. }
  565. }
  566. }
  567. template<bool has_alpha_channel, ScalingMode scaling_mode, typename GetPixel>
  568. ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity)
  569. {
  570. auto int_src_rect = enclosing_int_rect(src_rect);
  571. auto clipped_src_rect = int_src_rect.intersected(source.rect());
  572. if (clipped_src_rect.is_empty())
  573. return;
  574. if constexpr (scaling_mode == ScalingMode::NearestNeighbor || scaling_mode == ScalingMode::SmoothPixels) {
  575. if (dst_rect == clipped_rect && int_src_rect == src_rect && !(dst_rect.width() % int_src_rect.width()) && !(dst_rect.height() % int_src_rect.height())) {
  576. int hfactor = dst_rect.width() / int_src_rect.width();
  577. int vfactor = dst_rect.height() / int_src_rect.height();
  578. if (hfactor == 2 && vfactor == 2)
  579. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 2, 2, get_pixel, opacity);
  580. if (hfactor == 3 && vfactor == 3)
  581. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 3, 3, get_pixel, opacity);
  582. if (hfactor == 4 && vfactor == 4)
  583. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 4, 4, get_pixel, opacity);
  584. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, hfactor, vfactor, get_pixel, opacity);
  585. }
  586. }
  587. if constexpr (scaling_mode == ScalingMode::BoxSampling)
  588. return do_draw_box_sampled_scaled_bitmap<has_alpha_channel>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
  589. bool has_opacity = opacity != 1.f;
  590. i64 shift = 1ll << 32;
  591. i64 fractional_mask = shift - 1;
  592. i64 bilinear_offset_x = (1ll << 31) * (src_rect.width() / dst_rect.width() - 1);
  593. i64 bilinear_offset_y = (1ll << 31) * (src_rect.height() / dst_rect.height() - 1);
  594. i64 hscale = src_rect.width() * shift / dst_rect.width();
  595. i64 vscale = src_rect.height() * shift / dst_rect.height();
  596. i64 src_left = src_rect.left() * shift;
  597. i64 src_top = src_rect.top() * shift;
  598. for (int y = clipped_rect.top(); y < clipped_rect.bottom(); ++y) {
  599. auto* scanline = reinterpret_cast<Color*>(target.scanline(y));
  600. auto desired_y = (y - dst_rect.y()) * vscale + src_top;
  601. for (int x = clipped_rect.left(); x < clipped_rect.right(); ++x) {
  602. auto desired_x = (x - dst_rect.x()) * hscale + src_left;
  603. Color src_pixel;
  604. if constexpr (scaling_mode == ScalingMode::BilinearBlend) {
  605. auto shifted_x = desired_x + bilinear_offset_x;
  606. auto shifted_y = desired_y + bilinear_offset_y;
  607. auto scaled_x0 = clamp(shifted_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1);
  608. auto scaled_x1 = clamp((shifted_x >> 32) + 1, clipped_src_rect.left(), clipped_src_rect.right() - 1);
  609. auto scaled_y0 = clamp(shifted_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
  610. auto scaled_y1 = clamp((shifted_y >> 32) + 1, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
  611. float x_ratio = (shifted_x & fractional_mask) / static_cast<float>(shift);
  612. float y_ratio = (shifted_y & fractional_mask) / static_cast<float>(shift);
  613. auto top_left = get_pixel(source, scaled_x0, scaled_y0);
  614. auto top_right = get_pixel(source, scaled_x1, scaled_y0);
  615. auto bottom_left = get_pixel(source, scaled_x0, scaled_y1);
  616. auto bottom_right = get_pixel(source, scaled_x1, scaled_y1);
  617. auto top = top_left.mixed_with(top_right, x_ratio);
  618. auto bottom = bottom_left.mixed_with(bottom_right, x_ratio);
  619. src_pixel = top.mixed_with(bottom, y_ratio);
  620. } else if constexpr (scaling_mode == ScalingMode::SmoothPixels) {
  621. auto scaled_x1 = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1);
  622. auto scaled_x0 = clamp(scaled_x1 - 1, clipped_src_rect.left(), clipped_src_rect.right() - 1);
  623. auto scaled_y1 = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
  624. auto scaled_y0 = clamp(scaled_y1 - 1, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
  625. float x_ratio = (desired_x & fractional_mask) / (float)shift;
  626. float y_ratio = (desired_y & fractional_mask) / (float)shift;
  627. float scaled_x_ratio = clamp(x_ratio * dst_rect.width() / (float)src_rect.width(), 0.f, 1.f);
  628. float scaled_y_ratio = clamp(y_ratio * dst_rect.height() / (float)src_rect.height(), 0.f, 1.f);
  629. auto top_left = get_pixel(source, scaled_x0, scaled_y0);
  630. auto top_right = get_pixel(source, scaled_x1, scaled_y0);
  631. auto bottom_left = get_pixel(source, scaled_x0, scaled_y1);
  632. auto bottom_right = get_pixel(source, scaled_x1, scaled_y1);
  633. auto top = top_left.mixed_with(top_right, scaled_x_ratio);
  634. auto bottom = bottom_left.mixed_with(bottom_right, scaled_x_ratio);
  635. src_pixel = top.mixed_with(bottom, scaled_y_ratio);
  636. } else {
  637. auto scaled_x = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1);
  638. auto scaled_y = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
  639. src_pixel = get_pixel(source, scaled_x, scaled_y);
  640. }
  641. if (has_opacity)
  642. src_pixel.set_alpha(src_pixel.alpha() * opacity);
  643. if constexpr (has_alpha_channel)
  644. scanline[x] = scanline[x].blend(src_pixel);
  645. else
  646. scanline[x] = src_pixel;
  647. }
  648. }
  649. }
  650. template<bool has_alpha_channel, typename GetPixel>
  651. ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity, ScalingMode scaling_mode)
  652. {
  653. switch (scaling_mode) {
  654. case ScalingMode::NearestNeighbor:
  655. do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::NearestNeighbor>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
  656. break;
  657. case ScalingMode::SmoothPixels:
  658. do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::SmoothPixels>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
  659. break;
  660. case ScalingMode::BilinearBlend:
  661. do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::BilinearBlend>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
  662. break;
  663. case ScalingMode::BoxSampling:
  664. do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::BoxSampling>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
  665. break;
  666. case ScalingMode::None:
  667. do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::None>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
  668. break;
  669. }
  670. }
  671. void DeprecatedPainter::set_physical_pixel(IntPoint physical_point, Color color, bool blend)
  672. {
  673. // This function should only be called after translation, clipping, etc has been handled elsewhere
  674. // if not use set_pixel().
  675. auto& dst = target().scanline(physical_point.y())[physical_point.x()];
  676. if (!blend || color.alpha() == 255)
  677. dst = color.value();
  678. else if (color.alpha())
  679. dst = color_for_format(target().format(), dst).blend(color).value();
  680. }
  681. Optional<Color> DeprecatedPainter::get_pixel(IntPoint p)
  682. {
  683. auto point = p;
  684. point.translate_by(state().translation);
  685. if (!clip_rect().contains(point))
  686. return {};
  687. return target().get_pixel(point);
  688. }
  689. ALWAYS_INLINE void DeprecatedPainter::set_physical_pixel(u32& pixel, Color color)
  690. {
  691. // This always sets a single physical pixel, independent of scale().
  692. // This should only be called by routines that already handle scale.
  693. pixel = color.value();
  694. }
  695. ALWAYS_INLINE void DeprecatedPainter::fill_physical_scanline(int y, int x, int width, Color color)
  696. {
  697. // This always draws a single physical scanline, independent of scale().
  698. // This should only be called by routines that already handle scale.
  699. fast_u32_fill(target().scanline(y) + x, color.value(), width);
  700. }
  701. void DeprecatedPainter::draw_physical_pixel(IntPoint physical_position, Color color, int thickness)
  702. {
  703. // This always draws a single physical pixel, independent of scale().
  704. // This should only be called by routines that already handle scale
  705. // (including scaling thickness).
  706. if (thickness <= 0)
  707. return;
  708. if (thickness == 1) { // Implies scale() == 1.
  709. auto& pixel = target().scanline(physical_position.y())[physical_position.x()];
  710. return set_physical_pixel(pixel, color_for_format(target().format(), pixel).blend(color));
  711. }
  712. IntRect rect { physical_position, { thickness, thickness } };
  713. rect.intersect(clip_rect());
  714. fill_physical_rect(rect, color);
  715. }
  716. void DeprecatedPainter::draw_line(IntPoint a_p1, IntPoint a_p2, Color color, int thickness, LineStyle style, Color alternate_color)
  717. {
  718. if (clip_rect().is_empty())
  719. return;
  720. if (thickness <= 0)
  721. return;
  722. if (color.alpha() == 0)
  723. return;
  724. auto clip_rect = this->clip_rect();
  725. auto const p1 = thickness > 1 ? a_p1.translated(-(thickness / 2), -(thickness / 2)) : a_p1;
  726. auto const p2 = thickness > 1 ? a_p2.translated(-(thickness / 2), -(thickness / 2)) : a_p2;
  727. auto point1 = to_physical(p1);
  728. auto point2 = to_physical(p2);
  729. auto alternate_color_is_transparent = alternate_color == Color::Transparent;
  730. // Special case: vertical line.
  731. if (point1.x() == point2.x()) {
  732. int const x = point1.x();
  733. if (x < clip_rect.left() || x >= clip_rect.right())
  734. return;
  735. if (point1.y() > point2.y())
  736. swap(point1, point2);
  737. if (point1.y() >= clip_rect.bottom())
  738. return;
  739. if (point2.y() < clip_rect.top())
  740. return;
  741. int min_y = max(point1.y(), clip_rect.top());
  742. int max_y = min(point2.y(), clip_rect.bottom() - 1);
  743. if (style == LineStyle::Dotted) {
  744. for (int y = min_y; y <= max_y; y += thickness * 2)
  745. draw_physical_pixel({ x, y }, color, thickness);
  746. } else if (style == LineStyle::Dashed) {
  747. for (int y = min_y; y <= max_y; y += thickness * 6) {
  748. draw_physical_pixel({ x, y }, color, thickness);
  749. draw_physical_pixel({ x, min(y + thickness, max_y) }, color, thickness);
  750. draw_physical_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness);
  751. if (!alternate_color_is_transparent) {
  752. draw_physical_pixel({ x, min(y + thickness * 3, max_y) }, alternate_color, thickness);
  753. draw_physical_pixel({ x, min(y + thickness * 4, max_y) }, alternate_color, thickness);
  754. draw_physical_pixel({ x, min(y + thickness * 5, max_y) }, alternate_color, thickness);
  755. }
  756. }
  757. } else {
  758. for (int y = min_y; y <= max_y; y += thickness)
  759. draw_physical_pixel({ x, y }, color, thickness);
  760. draw_physical_pixel({ x, max_y }, color, thickness);
  761. }
  762. return;
  763. }
  764. // Special case: horizontal line.
  765. if (point1.y() == point2.y()) {
  766. int const y = point1.y();
  767. if (y < clip_rect.top() || y >= clip_rect.bottom())
  768. return;
  769. if (point1.x() > point2.x())
  770. swap(point1, point2);
  771. if (point1.x() >= clip_rect.right())
  772. return;
  773. if (point2.x() < clip_rect.left())
  774. return;
  775. int min_x = max(point1.x(), clip_rect.left());
  776. int max_x = min(point2.x(), clip_rect.right() - 1);
  777. if (style == LineStyle::Dotted) {
  778. for (int x = min_x; x <= max_x; x += thickness * 2)
  779. draw_physical_pixel({ x, y }, color, thickness);
  780. } else if (style == LineStyle::Dashed) {
  781. for (int x = min_x; x <= max_x; x += thickness * 6) {
  782. draw_physical_pixel({ x, y }, color, thickness);
  783. draw_physical_pixel({ min(x + thickness, max_x), y }, color, thickness);
  784. draw_physical_pixel({ min(x + thickness * 2, max_x), y }, color, thickness);
  785. if (!alternate_color_is_transparent) {
  786. draw_physical_pixel({ min(x + thickness * 3, max_x), y }, alternate_color, thickness);
  787. draw_physical_pixel({ min(x + thickness * 4, max_x), y }, alternate_color, thickness);
  788. draw_physical_pixel({ min(x + thickness * 5, max_x), y }, alternate_color, thickness);
  789. }
  790. }
  791. } else {
  792. for (int x = min_x; x <= max_x; x += thickness)
  793. draw_physical_pixel({ x, y }, color, thickness);
  794. draw_physical_pixel({ max_x, y }, color, thickness);
  795. }
  796. return;
  797. }
  798. int const adx = abs(point2.x() - point1.x());
  799. int const ady = abs(point2.y() - point1.y());
  800. if (adx > ady) {
  801. if (point1.x() > point2.x())
  802. swap(point1, point2);
  803. } else {
  804. if (point1.y() > point2.y())
  805. swap(point1, point2);
  806. }
  807. int const dx = point2.x() - point1.x();
  808. int const dy = point2.y() - point1.y();
  809. int error = 0;
  810. size_t number_of_pixels_drawn = 0;
  811. auto draw_pixel_in_line = [&](int x, int y) {
  812. bool should_draw_line = true;
  813. if (style == LineStyle::Dotted && number_of_pixels_drawn % 2 == 1)
  814. should_draw_line = false;
  815. else if (style == LineStyle::Dashed && number_of_pixels_drawn % 6 >= 3)
  816. should_draw_line = false;
  817. if (should_draw_line)
  818. draw_physical_pixel({ x, y }, color, thickness);
  819. else if (!alternate_color_is_transparent)
  820. draw_physical_pixel({ x, y }, alternate_color, thickness);
  821. number_of_pixels_drawn++;
  822. };
  823. if (dx > dy) {
  824. int const y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  825. int const delta_error = 2 * abs(dy);
  826. int y = point1.y();
  827. for (int x = point1.x(); x <= point2.x(); ++x) {
  828. if (clip_rect.contains(x, y))
  829. draw_pixel_in_line(x, y);
  830. error += delta_error;
  831. if (error >= dx) {
  832. y += y_step;
  833. error -= 2 * dx;
  834. }
  835. }
  836. } else {
  837. int const x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1);
  838. int const delta_error = 2 * abs(dx);
  839. int x = point1.x();
  840. for (int y = point1.y(); y <= point2.y(); ++y) {
  841. if (clip_rect.contains(x, y))
  842. draw_pixel_in_line(x, y);
  843. error += delta_error;
  844. if (error >= dy) {
  845. x += x_step;
  846. error -= 2 * dy;
  847. }
  848. }
  849. }
  850. }
  851. static bool can_approximate_bezier_curve(FloatPoint p1, FloatPoint p2, FloatPoint control)
  852. {
  853. // TODO: Somehow calculate the required number of splits based on the curve (and its size).
  854. constexpr float tolerance = 0.5f;
  855. auto p1x = 3 * control.x() - 2 * p1.x() - p2.x();
  856. auto p1y = 3 * control.y() - 2 * p1.y() - p2.y();
  857. auto p2x = 3 * control.x() - 2 * p2.x() - p1.x();
  858. auto p2y = 3 * control.y() - 2 * p2.y() - p1.y();
  859. p1x = p1x * p1x;
  860. p1y = p1y * p1y;
  861. p2x = p2x * p2x;
  862. p2y = p2y * p2y;
  863. auto error = max(p1x, p2x) + max(p1y, p2y);
  864. VERIFY(isfinite(error));
  865. return error <= tolerance;
  866. }
  867. void DeprecatedPainter::for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>& callback)
  868. {
  869. struct SegmentDescriptor {
  870. FloatPoint control_point;
  871. FloatPoint p1;
  872. FloatPoint p2;
  873. };
  874. static constexpr auto split_quadratic_bezier_curve = [](FloatPoint original_control, FloatPoint p1, FloatPoint p2, auto& segments) {
  875. auto po1_midpoint = original_control + p1;
  876. po1_midpoint /= 2;
  877. auto po2_midpoint = original_control + p2;
  878. po2_midpoint /= 2;
  879. auto new_segment = po1_midpoint + po2_midpoint;
  880. new_segment /= 2;
  881. segments.append({ po2_midpoint, new_segment, p2 });
  882. segments.append({ po1_midpoint, p1, new_segment });
  883. };
  884. Vector<SegmentDescriptor> segments;
  885. segments.append({ control_point, p1, p2 });
  886. while (!segments.is_empty()) {
  887. auto segment = segments.take_last();
  888. if (can_approximate_bezier_curve(segment.p1, segment.p2, segment.control_point))
  889. callback(segment.p1, segment.p2);
  890. else
  891. split_quadratic_bezier_curve(segment.control_point, segment.p1, segment.p2, segments);
  892. }
  893. }
  894. void DeprecatedPainter::for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&& callback)
  895. {
  896. for_each_line_segment_on_bezier_curve(control_point, p1, p2, callback);
  897. }
  898. void DeprecatedPainter::for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&& callback)
  899. {
  900. for_each_line_segment_on_cubic_bezier_curve(control_point_0, control_point_1, p1, p2, callback);
  901. }
  902. static bool can_approximate_cubic_bezier_curve(FloatPoint p1, FloatPoint p2, FloatPoint control_0, FloatPoint control_1)
  903. {
  904. // TODO: Somehow calculate the required number of splits based on the curve (and its size).
  905. constexpr float tolerance = 0.5f;
  906. auto ax = 3 * control_0.x() - 2 * p1.x() - p2.x();
  907. auto ay = 3 * control_0.y() - 2 * p1.y() - p2.y();
  908. auto bx = 3 * control_1.x() - p1.x() - 2 * p2.x();
  909. auto by = 3 * control_1.y() - p1.y() - 2 * p2.y();
  910. ax *= ax;
  911. ay *= ay;
  912. bx *= bx;
  913. by *= by;
  914. auto error = max(ax, bx) + max(ay, by);
  915. VERIFY(isfinite(error));
  916. return error <= tolerance;
  917. }
  918. // static
  919. void DeprecatedPainter::for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>& callback)
  920. {
  921. struct ControlPair {
  922. FloatPoint control_point_0;
  923. FloatPoint control_point_1;
  924. };
  925. struct SegmentDescriptor {
  926. ControlPair control_points;
  927. FloatPoint p1;
  928. FloatPoint p2;
  929. };
  930. static constexpr auto split_cubic_bezier_curve = [](ControlPair const& original_controls, FloatPoint p1, FloatPoint p2, auto& segments) {
  931. Array level_1_midpoints {
  932. (p1 + original_controls.control_point_0) / 2,
  933. (original_controls.control_point_0 + original_controls.control_point_1) / 2,
  934. (original_controls.control_point_1 + p2) / 2,
  935. };
  936. Array level_2_midpoints {
  937. (level_1_midpoints[0] + level_1_midpoints[1]) / 2,
  938. (level_1_midpoints[1] + level_1_midpoints[2]) / 2,
  939. };
  940. auto level_3_midpoint = (level_2_midpoints[0] + level_2_midpoints[1]) / 2;
  941. segments.append({ { level_2_midpoints[1], level_1_midpoints[2] }, level_3_midpoint, p2 });
  942. segments.append({ { level_1_midpoints[0], level_2_midpoints[0] }, p1, level_3_midpoint });
  943. };
  944. Vector<SegmentDescriptor> segments;
  945. segments.append({ { control_point_0, control_point_1 }, p1, p2 });
  946. while (!segments.is_empty()) {
  947. auto segment = segments.take_last();
  948. if (can_approximate_cubic_bezier_curve(segment.p1, segment.p2, segment.control_points.control_point_0, segment.control_points.control_point_1))
  949. callback(segment.p1, segment.p2);
  950. else
  951. split_cubic_bezier_curve(segment.control_points, segment.p1, segment.p2, segments);
  952. }
  953. }
  954. void DeprecatedPainter::add_clip_rect(IntRect const& rect)
  955. {
  956. state().clip_rect.intersect(rect.translated(translation()));
  957. state().clip_rect.intersect(target().rect()); // FIXME: This shouldn't be necessary?
  958. }
  959. void DeprecatedPainter::stroke_path(DeprecatedPath const& path, Color color, int thickness)
  960. {
  961. if (thickness <= 0)
  962. return;
  963. fill_path(path.stroke_to_fill(thickness), color);
  964. }
  965. }