DeprecatedPainter.cpp 52 KB

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