Painter.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. /*
  2. * Copyright (c) 2018-2021, 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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Painter.h"
  9. #include "Bitmap.h"
  10. #include "Emoji.h"
  11. #include "Font.h"
  12. #include "FontDatabase.h"
  13. #include "Gamma.h"
  14. #include <AK/Assertions.h>
  15. #include <AK/Debug.h>
  16. #include <AK/Function.h>
  17. #include <AK/Math.h>
  18. #include <AK/Memory.h>
  19. #include <AK/Queue.h>
  20. #include <AK/QuickSort.h>
  21. #include <AK/StdLibExtras.h>
  22. #include <AK/StringBuilder.h>
  23. #include <AK/Utf32View.h>
  24. #include <AK/Utf8View.h>
  25. #include <LibGfx/CharacterBitmap.h>
  26. #include <LibGfx/FillPathImplementation.h>
  27. #include <LibGfx/Palette.h>
  28. #include <LibGfx/Path.h>
  29. #include <LibGfx/TextDirection.h>
  30. #include <LibGfx/TextLayout.h>
  31. #include <stdio.h>
  32. #if defined(__GNUC__) && !defined(__clang__)
  33. # pragma GCC optimize("O3")
  34. #endif
  35. namespace Gfx {
  36. template<BitmapFormat format = BitmapFormat::Invalid>
  37. ALWAYS_INLINE Color get_pixel(Gfx::Bitmap const& bitmap, int x, int y)
  38. {
  39. if constexpr (format == BitmapFormat::Indexed8)
  40. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  41. if constexpr (format == BitmapFormat::Indexed4)
  42. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  43. if constexpr (format == BitmapFormat::Indexed2)
  44. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  45. if constexpr (format == BitmapFormat::Indexed1)
  46. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  47. if constexpr (format == BitmapFormat::BGRx8888)
  48. return Color::from_rgb(bitmap.scanline(y)[x]);
  49. if constexpr (format == BitmapFormat::BGRA8888)
  50. return Color::from_rgba(bitmap.scanline(y)[x]);
  51. return bitmap.get_pixel(x, y);
  52. }
  53. Painter::Painter(Gfx::Bitmap& bitmap)
  54. : m_target(bitmap)
  55. {
  56. int scale = bitmap.scale();
  57. VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRx8888 || bitmap.format() == Gfx::BitmapFormat::BGRA8888);
  58. VERIFY(bitmap.physical_width() % scale == 0);
  59. VERIFY(bitmap.physical_height() % scale == 0);
  60. m_state_stack.append(State());
  61. state().font = &FontDatabase::default_font();
  62. state().clip_rect = { { 0, 0 }, bitmap.size() };
  63. state().scale = scale;
  64. m_clip_origin = state().clip_rect;
  65. }
  66. Painter::~Painter()
  67. {
  68. }
  69. void Painter::fill_rect_with_draw_op(IntRect const& a_rect, Color color)
  70. {
  71. VERIFY(scale() == 1); // FIXME: Add scaling support.
  72. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  73. if (rect.is_empty())
  74. return;
  75. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  76. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  77. for (int i = rect.height() - 1; i >= 0; --i) {
  78. for (int j = 0; j < rect.width(); ++j)
  79. set_physical_pixel_with_draw_op(dst[j], color);
  80. dst += dst_skip;
  81. }
  82. }
  83. void Painter::clear_rect(IntRect const& a_rect, Color color)
  84. {
  85. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  86. if (rect.is_empty())
  87. return;
  88. VERIFY(m_target->rect().contains(rect));
  89. rect *= scale();
  90. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  91. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  92. for (int i = rect.height() - 1; i >= 0; --i) {
  93. fast_u32_fill(dst, color.value(), rect.width());
  94. dst += dst_skip;
  95. }
  96. }
  97. void Painter::fill_physical_rect(IntRect const& physical_rect, Color color)
  98. {
  99. // Callers must do clipping.
  100. RGBA32* dst = m_target->scanline(physical_rect.top()) + physical_rect.left();
  101. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  102. for (int i = physical_rect.height() - 1; i >= 0; --i) {
  103. for (int j = 0; j < physical_rect.width(); ++j)
  104. dst[j] = Color::from_rgba(dst[j]).blend(color).value();
  105. dst += dst_skip;
  106. }
  107. }
  108. void Painter::fill_rect(IntRect const& a_rect, Color color)
  109. {
  110. if (color.alpha() == 0)
  111. return;
  112. if (draw_op() != DrawOp::Copy) {
  113. fill_rect_with_draw_op(a_rect, color);
  114. return;
  115. }
  116. if (color.alpha() == 0xff) {
  117. clear_rect(a_rect, color);
  118. return;
  119. }
  120. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  121. if (rect.is_empty())
  122. return;
  123. VERIFY(m_target->rect().contains(rect));
  124. fill_physical_rect(rect * scale(), color);
  125. }
  126. void Painter::fill_rect_with_dither_pattern(IntRect const& a_rect, Color color_a, Color color_b)
  127. {
  128. VERIFY(scale() == 1); // FIXME: Add scaling support.
  129. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  130. if (rect.is_empty())
  131. return;
  132. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  133. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  134. for (int i = 0; i < rect.height(); ++i) {
  135. for (int j = 0; j < rect.width(); ++j) {
  136. bool checkboard_use_a = ((rect.left() + i) & 1) ^ ((rect.top() + j) & 1);
  137. if (checkboard_use_a && !color_a.alpha())
  138. continue;
  139. if (!checkboard_use_a && !color_b.alpha())
  140. continue;
  141. dst[j] = checkboard_use_a ? color_a.value() : color_b.value();
  142. }
  143. dst += dst_skip;
  144. }
  145. }
  146. void Painter::fill_rect_with_checkerboard(IntRect const& a_rect, IntSize const& cell_size, Color color_dark, Color color_light)
  147. {
  148. VERIFY(scale() == 1); // FIXME: Add scaling support.
  149. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  150. if (rect.is_empty())
  151. return;
  152. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  153. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  154. int first_cell_column = rect.x() / cell_size.width();
  155. int prologue_length = min(rect.width(), cell_size.width() - (rect.x() % cell_size.width()));
  156. int number_of_aligned_strips = (rect.width() - prologue_length) / cell_size.width();
  157. for (int i = 0; i < rect.height(); ++i) {
  158. int y = rect.y() + i;
  159. int cell_row = y / cell_size.height();
  160. bool odd_row = cell_row & 1;
  161. // Prologue: Paint the unaligned part up to the first intersection.
  162. int j = 0;
  163. int cell_column = first_cell_column;
  164. {
  165. bool odd_cell = cell_column & 1;
  166. auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
  167. fast_u32_fill(&dst[j], color, prologue_length);
  168. j += prologue_length;
  169. }
  170. // Aligned run: Paint the maximum number of aligned cell strips.
  171. for (int strip = 0; strip < number_of_aligned_strips; ++strip) {
  172. ++cell_column;
  173. bool odd_cell = cell_column & 1;
  174. auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
  175. fast_u32_fill(&dst[j], color, cell_size.width());
  176. j += cell_size.width();
  177. }
  178. // Epilogue: Paint the unaligned part until the end of the rect.
  179. if (j != rect.width()) {
  180. ++cell_column;
  181. bool odd_cell = cell_column & 1;
  182. auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
  183. int epilogue_length = rect.width() - j;
  184. fast_u32_fill(&dst[j], color, epilogue_length);
  185. j += epilogue_length;
  186. }
  187. dst += dst_skip;
  188. }
  189. }
  190. void Painter::fill_rect_with_gradient(Orientation orientation, IntRect const& a_rect, Color gradient_start, Color gradient_end)
  191. {
  192. if (gradient_start == gradient_end) {
  193. fill_rect(a_rect, gradient_start);
  194. return;
  195. }
  196. auto rect = to_physical(a_rect);
  197. auto clipped_rect = IntRect::intersection(rect, clip_rect() * scale());
  198. if (clipped_rect.is_empty())
  199. return;
  200. int offset = clipped_rect.primary_offset_for_orientation(orientation) - rect.primary_offset_for_orientation(orientation);
  201. RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
  202. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  203. float increment = (1.0 / ((rect.primary_size_for_orientation(orientation))));
  204. float alpha_increment = increment * ((float)gradient_end.alpha() - (float)gradient_start.alpha());
  205. if (orientation == Orientation::Horizontal) {
  206. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  207. float c = offset * increment;
  208. float c_alpha = gradient_start.alpha() + offset * alpha_increment;
  209. for (int j = 0; j < clipped_rect.width(); ++j) {
  210. auto color = gamma_accurate_blend(gradient_start, gradient_end, c);
  211. color.set_alpha(c_alpha);
  212. dst[j] = color.value();
  213. c_alpha += alpha_increment;
  214. c += increment;
  215. }
  216. dst += dst_skip;
  217. }
  218. } else {
  219. float c = offset * increment;
  220. float c_alpha = gradient_start.alpha() + offset * alpha_increment;
  221. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  222. auto color = gamma_accurate_blend(gradient_end, gradient_start, c);
  223. color.set_alpha(c_alpha);
  224. for (int j = 0; j < clipped_rect.width(); ++j) {
  225. dst[j] = color.value();
  226. }
  227. c_alpha += alpha_increment;
  228. c += increment;
  229. dst += dst_skip;
  230. }
  231. }
  232. }
  233. void Painter::fill_rect_with_gradient(IntRect const& a_rect, Color gradient_start, Color gradient_end)
  234. {
  235. return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end);
  236. }
  237. void Painter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
  238. {
  239. return fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
  240. }
  241. void Painter::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)
  242. {
  243. // Fasttrack for rects without any border radii
  244. if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius)
  245. return fill_rect(a_rect, color);
  246. // Fully transparent, dont care.
  247. if (color.alpha() == 0)
  248. return;
  249. // FIXME: Allow for elliptically rounded corners
  250. IntRect top_left_corner = {
  251. a_rect.x(),
  252. a_rect.y(),
  253. top_left_radius,
  254. top_left_radius
  255. };
  256. IntRect top_right_corner = {
  257. a_rect.x() + a_rect.width() - top_right_radius,
  258. a_rect.y(),
  259. top_right_radius,
  260. top_right_radius
  261. };
  262. IntRect bottom_right_corner = {
  263. a_rect.x() + a_rect.width() - bottom_right_radius,
  264. a_rect.y() + a_rect.height() - bottom_right_radius,
  265. bottom_right_radius,
  266. bottom_right_radius
  267. };
  268. IntRect bottom_left_corner = {
  269. a_rect.x(),
  270. a_rect.y() + a_rect.height() - bottom_left_radius,
  271. bottom_left_radius,
  272. bottom_left_radius
  273. };
  274. IntRect top_rect = {
  275. a_rect.x() + top_left_radius,
  276. a_rect.y(),
  277. a_rect.width() - top_left_radius - top_right_radius, top_left_radius
  278. };
  279. IntRect right_rect = {
  280. a_rect.x() + a_rect.width() - top_right_radius,
  281. a_rect.y() + top_right_radius,
  282. top_right_radius,
  283. a_rect.height() - top_right_radius - bottom_right_radius
  284. };
  285. IntRect bottom_rect = {
  286. a_rect.x() + bottom_left_radius,
  287. a_rect.y() + a_rect.height() - bottom_right_radius,
  288. a_rect.width() - bottom_left_radius - bottom_right_radius,
  289. bottom_right_radius
  290. };
  291. IntRect left_rect = {
  292. a_rect.x(),
  293. a_rect.y() + top_left_radius,
  294. bottom_left_radius,
  295. a_rect.height() - top_left_radius - bottom_left_radius
  296. };
  297. IntRect inner = {
  298. left_rect.x() + left_rect.width(),
  299. left_rect.y(),
  300. a_rect.width() - left_rect.width() - right_rect.width(),
  301. a_rect.height() - top_rect.height() - bottom_rect.height()
  302. };
  303. fill_rect(top_rect, color);
  304. fill_rect(right_rect, color);
  305. fill_rect(bottom_rect, color);
  306. fill_rect(left_rect, color);
  307. fill_rect(inner, color);
  308. if (top_left_radius)
  309. fill_rounded_corner(top_left_corner, top_left_radius, color, CornerOrientation::TopLeft);
  310. if (top_right_radius)
  311. fill_rounded_corner(top_right_corner, top_right_radius, color, CornerOrientation::TopRight);
  312. if (bottom_left_radius)
  313. fill_rounded_corner(bottom_left_corner, bottom_left_radius, color, CornerOrientation::BottomLeft);
  314. if (bottom_right_radius)
  315. fill_rounded_corner(bottom_right_corner, bottom_right_radius, color, CornerOrientation::BottomRight);
  316. }
  317. void Painter::fill_rounded_corner(IntRect const& a_rect, int radius, Color color, CornerOrientation orientation)
  318. {
  319. // Care about clipping
  320. auto translated_a_rect = a_rect.translated(translation());
  321. auto rect = translated_a_rect.intersected(clip_rect());
  322. if (rect.is_empty())
  323. return;
  324. VERIFY(m_target->rect().contains(rect));
  325. // We got cut on the top!
  326. // FIXME: Also account for clipping on the x-axis
  327. int clip_offset = 0;
  328. if (translated_a_rect.y() < rect.y())
  329. clip_offset = rect.y() - translated_a_rect.y();
  330. radius *= scale();
  331. rect *= scale();
  332. clip_offset *= scale();
  333. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  334. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  335. IntPoint circle_center;
  336. switch (orientation) {
  337. case CornerOrientation::TopLeft:
  338. circle_center = { radius, radius + 1 };
  339. break;
  340. case CornerOrientation::TopRight:
  341. circle_center = { -1, radius + 1 };
  342. break;
  343. case CornerOrientation::BottomRight:
  344. circle_center = { -1, 0 };
  345. break;
  346. case CornerOrientation::BottomLeft:
  347. circle_center = { radius, 0 };
  348. break;
  349. default:
  350. VERIFY_NOT_REACHED();
  351. }
  352. int radius2 = radius * radius;
  353. auto is_in_circle = [&](int x, int y) {
  354. int distance2 = (circle_center.x() - x) * (circle_center.x() - x) + (circle_center.y() - y) * (circle_center.y() - y);
  355. // To reflect the grid and be compatible with the draw_circle_arc_intersecting algorithm
  356. // add 1/2 to the radius
  357. return distance2 <= (radius2 + radius + 0.25);
  358. };
  359. for (int i = rect.height() - 1; i >= 0; --i) {
  360. for (int j = 0; j < rect.width(); ++j)
  361. if (is_in_circle(j, rect.height() - i + clip_offset))
  362. dst[j] = Color::from_rgba(dst[j]).blend(color).value();
  363. dst += dst_skip;
  364. }
  365. }
  366. void Painter::draw_circle_arc_intersecting(IntRect const& a_rect, IntPoint const& center, int radius, Color color, int thickness)
  367. {
  368. if (thickness <= 0)
  369. return;
  370. // Care about clipping
  371. auto translated_a_rect = a_rect.translated(translation());
  372. auto rect = translated_a_rect.intersected(clip_rect());
  373. if (rect.is_empty())
  374. return;
  375. VERIFY(m_target->rect().contains(rect));
  376. // We got cut on the top!
  377. // FIXME: Also account for clipping on the x-axis
  378. int clip_offset = 0;
  379. if (translated_a_rect.y() < rect.y())
  380. clip_offset = rect.y() - translated_a_rect.y();
  381. if (thickness > radius)
  382. thickness = radius;
  383. int radius2 = radius * radius;
  384. auto is_on_arc = [&](int x, int y) {
  385. int distance2 = (center.x() - x) * (center.x() - x) + (center.y() - y) * (center.y() - y);
  386. // Is within a circle of radius 1/2 around (x,y), so basically within the current pixel.
  387. // Technically this is angle-dependent and should be between 1/2 and sqrt(2)/2, but this works.
  388. return distance2 <= (radius2 + radius + 0.25) && distance2 >= (radius2 - radius + 0.25);
  389. };
  390. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  391. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  392. for (int i = rect.height() - 1; i >= 0; --i) {
  393. for (int j = 0; j < rect.width(); ++j)
  394. if (is_on_arc(j, rect.height() - i + clip_offset))
  395. dst[j] = Color::from_rgba(dst[j]).blend(color).value();
  396. dst += dst_skip;
  397. }
  398. return draw_circle_arc_intersecting(a_rect, center, radius - 1, color, thickness - 1);
  399. }
  400. void Painter::fill_ellipse(IntRect const& a_rect, Color color)
  401. {
  402. VERIFY(scale() == 1); // FIXME: Add scaling support.
  403. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  404. if (rect.is_empty())
  405. return;
  406. VERIFY(m_target->rect().contains(rect));
  407. for (int i = 1; i < a_rect.height(); i++) {
  408. double y = a_rect.height() * 0.5 - i;
  409. double x = a_rect.width() * sqrt(0.25 - y * y / a_rect.height() / a_rect.height());
  410. draw_line({ a_rect.x() + a_rect.width() / 2 - (int)x, a_rect.y() + i }, { a_rect.x() + a_rect.width() / 2 + (int)x - 1, a_rect.y() + i }, color);
  411. }
  412. }
  413. void Painter::draw_ellipse_intersecting(IntRect const& rect, Color color, int thickness)
  414. {
  415. VERIFY(scale() == 1); // FIXME: Add scaling support.
  416. if (thickness <= 0)
  417. return;
  418. constexpr int number_samples = 100; // FIXME: dynamically work out the number of samples based upon the rect size
  419. double increment = M_PI / number_samples;
  420. auto ellipse_x = [&](double theta) -> int {
  421. return (AK::cos(theta) * rect.width() / AK::sqrt(2.)) + rect.center().x();
  422. };
  423. auto ellipse_y = [&](double theta) -> int {
  424. return (AK::sin(theta) * rect.height() / AK::sqrt(2.)) + rect.center().y();
  425. };
  426. for (auto theta = 0.0; theta < 2 * M_PI; theta += increment) {
  427. draw_line({ ellipse_x(theta), ellipse_y(theta) }, { ellipse_x(theta + increment), ellipse_y(theta + increment) }, color, thickness);
  428. }
  429. }
  430. template<typename RectType, typename Callback>
  431. static void for_each_pixel_around_rect_clockwise(RectType const& rect, Callback callback)
  432. {
  433. if (rect.is_empty())
  434. return;
  435. for (auto x = rect.left(); x <= rect.right(); ++x) {
  436. callback(x, rect.top());
  437. }
  438. for (auto y = rect.top() + 1; y <= rect.bottom(); ++y) {
  439. callback(rect.right(), y);
  440. }
  441. for (auto x = rect.right() - 1; x >= rect.left(); --x) {
  442. callback(x, rect.bottom());
  443. }
  444. for (auto y = rect.bottom() - 1; y > rect.top(); --y) {
  445. callback(rect.left(), y);
  446. }
  447. }
  448. void Painter::draw_focus_rect(IntRect const& rect, Color color)
  449. {
  450. VERIFY(scale() == 1); // FIXME: Add scaling support.
  451. if (rect.is_empty())
  452. return;
  453. bool state = false;
  454. for_each_pixel_around_rect_clockwise(rect, [&](auto x, auto y) {
  455. if (state)
  456. set_pixel(x, y, color);
  457. state = !state;
  458. });
  459. }
  460. void Painter::draw_rect(IntRect const& a_rect, Color color, bool rough)
  461. {
  462. IntRect rect = a_rect.translated(translation());
  463. auto clipped_rect = rect.intersected(clip_rect());
  464. if (clipped_rect.is_empty())
  465. return;
  466. int min_y = clipped_rect.top();
  467. int max_y = clipped_rect.bottom();
  468. int scale = this->scale();
  469. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  470. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  471. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  472. for (int i = 0; i < scale; ++i)
  473. fill_physical_scanline_with_draw_op(rect.top() * scale + i, start_x * scale, width * scale, color);
  474. ++min_y;
  475. }
  476. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  477. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  478. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  479. for (int i = 0; i < scale; ++i)
  480. fill_physical_scanline_with_draw_op(max_y * scale + i, start_x * scale, width * scale, color);
  481. --max_y;
  482. }
  483. bool draw_left_side = rect.left() >= clipped_rect.left();
  484. bool draw_right_side = rect.right() == clipped_rect.right();
  485. if (draw_left_side && draw_right_side) {
  486. // Specialized loop when drawing both sides.
  487. for (int y = min_y * scale; y <= max_y * scale; ++y) {
  488. auto* bits = m_target->scanline(y);
  489. for (int i = 0; i < scale; ++i)
  490. set_physical_pixel_with_draw_op(bits[rect.left() * scale + i], color);
  491. for (int i = 0; i < scale; ++i)
  492. set_physical_pixel_with_draw_op(bits[rect.right() * scale + i], color);
  493. }
  494. } else {
  495. for (int y = min_y * scale; y <= max_y * scale; ++y) {
  496. auto* bits = m_target->scanline(y);
  497. if (draw_left_side)
  498. for (int i = 0; i < scale; ++i)
  499. set_physical_pixel_with_draw_op(bits[rect.left() * scale + i], color);
  500. if (draw_right_side)
  501. for (int i = 0; i < scale; ++i)
  502. set_physical_pixel_with_draw_op(bits[rect.right() * scale + i], color);
  503. }
  504. }
  505. }
  506. void Painter::draw_rect_with_thickness(IntRect const& rect, Color color, int thickness)
  507. {
  508. VERIFY(scale() == 1); // FIXME: Add scaling support.
  509. if (thickness <= 0)
  510. return;
  511. IntPoint p1 = rect.location();
  512. IntPoint p2 = { rect.location().x() + rect.width(), rect.location().y() };
  513. IntPoint p3 = { rect.location().x() + rect.width(), rect.location().y() + rect.height() };
  514. IntPoint p4 = { rect.location().x(), rect.location().y() + rect.height() };
  515. draw_line(p1, p2, color, thickness);
  516. draw_line(p2, p3, color, thickness);
  517. draw_line(p3, p4, color, thickness);
  518. draw_line(p4, p1, color, thickness);
  519. }
  520. void Painter::draw_bitmap(IntPoint const& p, CharacterBitmap const& bitmap, Color color)
  521. {
  522. VERIFY(scale() == 1); // FIXME: Add scaling support.
  523. auto rect = IntRect(p, bitmap.size()).translated(translation());
  524. auto clipped_rect = rect.intersected(clip_rect());
  525. if (clipped_rect.is_empty())
  526. return;
  527. int const first_row = clipped_rect.top() - rect.top();
  528. int const last_row = clipped_rect.bottom() - rect.top();
  529. int const first_column = clipped_rect.left() - rect.left();
  530. int const last_column = clipped_rect.right() - rect.left();
  531. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  532. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  533. char const* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  534. size_t const bitmap_skip = bitmap.width();
  535. for (int row = first_row; row <= last_row; ++row) {
  536. for (int j = 0; j <= (last_column - first_column); ++j) {
  537. char fc = bitmap_row[j];
  538. if (fc == '#')
  539. dst[j] = color.value();
  540. }
  541. bitmap_row += bitmap_skip;
  542. dst += dst_skip;
  543. }
  544. }
  545. void Painter::draw_bitmap(IntPoint const& p, GlyphBitmap const& bitmap, Color color)
  546. {
  547. auto dst_rect = IntRect(p, bitmap.size()).translated(translation());
  548. auto clipped_rect = dst_rect.intersected(clip_rect());
  549. if (clipped_rect.is_empty())
  550. return;
  551. int const first_row = clipped_rect.top() - dst_rect.top();
  552. int const last_row = clipped_rect.bottom() - dst_rect.top();
  553. int const first_column = clipped_rect.left() - dst_rect.left();
  554. int const last_column = clipped_rect.right() - dst_rect.left();
  555. int scale = this->scale();
  556. RGBA32* dst = m_target->scanline(clipped_rect.y() * scale) + clipped_rect.x() * scale;
  557. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  558. if (scale == 1) {
  559. for (int row = first_row; row <= last_row; ++row) {
  560. for (int j = 0; j <= (last_column - first_column); ++j) {
  561. if (bitmap.bit_at(j + first_column, row))
  562. dst[j] = color.value();
  563. }
  564. dst += dst_skip;
  565. }
  566. } else {
  567. for (int row = first_row; row <= last_row; ++row) {
  568. for (int j = 0; j <= (last_column - first_column); ++j) {
  569. if (bitmap.bit_at((j + first_column), row)) {
  570. for (int iy = 0; iy < scale; ++iy)
  571. for (int ix = 0; ix < scale; ++ix)
  572. dst[j * scale + ix + iy * dst_skip] = color.value();
  573. }
  574. }
  575. dst += dst_skip * scale;
  576. }
  577. }
  578. }
  579. void Painter::draw_triangle(IntPoint const& a, IntPoint const& b, IntPoint const& c, Color color)
  580. {
  581. IntPoint p0(to_physical(a));
  582. IntPoint p1(to_physical(b));
  583. IntPoint p2(to_physical(c));
  584. // sort points from top to bottom
  585. if (p0.y() > p1.y())
  586. swap(p0, p1);
  587. if (p0.y() > p2.y())
  588. swap(p0, p2);
  589. if (p1.y() > p2.y())
  590. swap(p1, p2);
  591. // return if top and bottom points are on same line
  592. if (p0.y() == p2.y())
  593. return;
  594. // return if top is below clip rect or bottom is above clip rect
  595. auto clip = clip_rect();
  596. if (p0.y() >= clip.bottom())
  597. return;
  598. if (p2.y() < clip.top())
  599. return;
  600. int rgba = color.value();
  601. float dx02 = (float)(p2.x() - p0.x()) / (p2.y() - p0.y());
  602. float x01 = p0.x();
  603. float x02 = p0.x();
  604. if (p0.y() != p1.y()) { // p0 and p1 are on different lines
  605. float dx01 = (float)(p1.x() - p0.x()) / (p1.y() - p0.y());
  606. int top = p0.y();
  607. if (top < clip.top()) {
  608. x01 += dx01 * (clip.top() - top);
  609. x02 += dx02 * (clip.top() - top);
  610. top = clip.top();
  611. }
  612. for (int y = top; y < p1.y() && y < clip.bottom(); ++y) { // XXX <=?
  613. int start = x01 > x02 ? max((int)x02, clip.left()) : max((int)x01, clip.left());
  614. int end = x01 > x02 ? min((int)x01, clip.right()) : min((int)x02, clip.right());
  615. auto* scanline = m_target->scanline(y);
  616. for (int x = start; x < end; x++) {
  617. scanline[x] = rgba;
  618. }
  619. x01 += dx01;
  620. x02 += dx02;
  621. }
  622. }
  623. // return if middle point and bottom point are on same line
  624. if (p1.y() == p2.y())
  625. return;
  626. float x12 = p1.x();
  627. float dx12 = (float)(p2.x() - p1.x()) / (p2.y() - p1.y());
  628. int top = p1.y();
  629. if (top < clip.top()) {
  630. x02 += dx02 * (clip.top() - top);
  631. x12 += dx12 * (clip.top() - top);
  632. top = clip.top();
  633. }
  634. for (int y = top; y < p2.y() && y < clip.bottom(); ++y) { // XXX <=?
  635. int start = x12 > x02 ? max((int)x02, clip.left()) : max((int)x12, clip.left());
  636. int end = x12 > x02 ? min((int)x12, clip.right()) : min((int)x02, clip.right());
  637. auto* scanline = m_target->scanline(y);
  638. for (int x = start; x < end; x++) {
  639. scanline[x] = rgba;
  640. }
  641. x02 += dx02;
  642. x12 += dx12;
  643. }
  644. }
  645. struct BlitState {
  646. enum AlphaState {
  647. NoAlpha = 0,
  648. SrcAlpha = 1,
  649. DstAlpha = 2,
  650. BothAlpha = SrcAlpha | DstAlpha
  651. };
  652. RGBA32 const* src;
  653. RGBA32* dst;
  654. size_t src_pitch;
  655. size_t dst_pitch;
  656. int row_count;
  657. int column_count;
  658. float opacity;
  659. };
  660. template<BlitState::AlphaState has_alpha>
  661. static void do_blit_with_opacity(BlitState& state)
  662. {
  663. for (int row = 0; row < state.row_count; ++row) {
  664. for (int x = 0; x < state.column_count; ++x) {
  665. Color dest_color = (has_alpha & BlitState::DstAlpha) ? Color::from_rgba(state.dst[x]) : Color::from_rgb(state.dst[x]);
  666. if constexpr (has_alpha & BlitState::SrcAlpha) {
  667. Color src_color_with_alpha = Color::from_rgba(state.src[x]);
  668. float pixel_opacity = src_color_with_alpha.alpha() / 255.0;
  669. src_color_with_alpha.set_alpha(255 * (state.opacity * pixel_opacity));
  670. state.dst[x] = dest_color.blend(src_color_with_alpha).value();
  671. } else {
  672. Color src_color_with_alpha = Color::from_rgb(state.src[x]);
  673. src_color_with_alpha.set_alpha(state.opacity * 255);
  674. state.dst[x] = dest_color.blend(src_color_with_alpha).value();
  675. }
  676. }
  677. state.dst += state.dst_pitch;
  678. state.src += state.src_pitch;
  679. }
  680. }
  681. void Painter::blit_with_opacity(IntPoint const& position, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, bool apply_alpha)
  682. {
  683. VERIFY(scale() >= source.scale() && "painter doesn't support downsampling scale factors");
  684. if (opacity >= 1.0f && !(source.has_alpha_channel() && apply_alpha))
  685. return blit(position, source, a_src_rect);
  686. IntRect safe_src_rect = IntRect::intersection(a_src_rect, source.rect());
  687. if (scale() != source.scale())
  688. return draw_scaled_bitmap({ position, safe_src_rect.size() }, source, safe_src_rect, opacity);
  689. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  690. auto clipped_rect = dst_rect.intersected(clip_rect());
  691. if (clipped_rect.is_empty())
  692. return;
  693. int scale = this->scale();
  694. auto src_rect = a_src_rect * scale;
  695. clipped_rect *= scale;
  696. dst_rect *= scale;
  697. int const first_row = clipped_rect.top() - dst_rect.top();
  698. int const last_row = clipped_rect.bottom() - dst_rect.top();
  699. int const first_column = clipped_rect.left() - dst_rect.left();
  700. int const last_column = clipped_rect.right() - dst_rect.left();
  701. BlitState blit_state {
  702. .src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column,
  703. .dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(),
  704. .src_pitch = source.pitch() / sizeof(RGBA32),
  705. .dst_pitch = m_target->pitch() / sizeof(RGBA32),
  706. .row_count = last_row - first_row + 1,
  707. .column_count = last_column - first_column + 1,
  708. .opacity = opacity
  709. };
  710. if (source.has_alpha_channel() && apply_alpha) {
  711. if (m_target->has_alpha_channel())
  712. do_blit_with_opacity<BlitState::BothAlpha>(blit_state);
  713. else
  714. do_blit_with_opacity<BlitState::SrcAlpha>(blit_state);
  715. } else {
  716. if (m_target->has_alpha_channel())
  717. do_blit_with_opacity<BlitState::DstAlpha>(blit_state);
  718. else
  719. do_blit_with_opacity<BlitState::NoAlpha>(blit_state);
  720. }
  721. }
  722. void Painter::blit_filtered(IntPoint const& position, Gfx::Bitmap const& source, IntRect const& src_rect, Function<Color(Color)> filter)
  723. {
  724. VERIFY((source.scale() == 1 || source.scale() == scale()) && "blit_filtered only supports integer upsampling");
  725. IntRect safe_src_rect = src_rect.intersected(source.rect());
  726. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  727. auto clipped_rect = dst_rect.intersected(clip_rect());
  728. if (clipped_rect.is_empty())
  729. return;
  730. int scale = this->scale();
  731. clipped_rect *= scale;
  732. dst_rect *= scale;
  733. safe_src_rect *= source.scale();
  734. int const first_row = clipped_rect.top() - dst_rect.top();
  735. int const last_row = clipped_rect.bottom() - dst_rect.top();
  736. int const first_column = clipped_rect.left() - dst_rect.left();
  737. int const last_column = clipped_rect.right() - dst_rect.left();
  738. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  739. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  740. int s = scale / source.scale();
  741. if (s == 1) {
  742. RGBA32 const* src = source.scanline(safe_src_rect.top() + first_row) + safe_src_rect.left() + first_column;
  743. size_t const src_skip = source.pitch() / sizeof(RGBA32);
  744. for (int row = first_row; row <= last_row; ++row) {
  745. for (int x = 0; x <= (last_column - first_column); ++x) {
  746. u8 alpha = Color::from_rgba(src[x]).alpha();
  747. if (alpha == 0xff) {
  748. auto color = filter(Color::from_rgba(src[x]));
  749. if (color.alpha() == 0xff)
  750. dst[x] = color.value();
  751. else
  752. dst[x] = Color::from_rgba(dst[x]).blend(color).value();
  753. } else if (!alpha)
  754. continue;
  755. else
  756. dst[x] = Color::from_rgba(dst[x]).blend(filter(Color::from_rgba(src[x]))).value();
  757. }
  758. dst += dst_skip;
  759. src += src_skip;
  760. }
  761. } else {
  762. for (int row = first_row; row <= last_row; ++row) {
  763. RGBA32 const* src = source.scanline(safe_src_rect.top() + row / s) + safe_src_rect.left() + first_column / s;
  764. for (int x = 0; x <= (last_column - first_column); ++x) {
  765. u8 alpha = Color::from_rgba(src[x / s]).alpha();
  766. if (alpha == 0xff) {
  767. auto color = filter(Color::from_rgba(src[x / s]));
  768. if (color.alpha() == 0xff)
  769. dst[x] = color.value();
  770. else
  771. dst[x] = Color::from_rgba(dst[x]).blend(color).value();
  772. } else if (!alpha)
  773. continue;
  774. else
  775. dst[x] = Color::from_rgba(dst[x]).blend(filter(Color::from_rgba(src[x / s]))).value();
  776. }
  777. dst += dst_skip;
  778. }
  779. }
  780. }
  781. void Painter::blit_brightened(IntPoint const& position, Gfx::Bitmap const& source, IntRect const& src_rect)
  782. {
  783. return blit_filtered(position, source, src_rect, [](Color src) {
  784. return src.lightened();
  785. });
  786. }
  787. void Painter::blit_dimmed(IntPoint const& position, Gfx::Bitmap const& source, IntRect const& src_rect)
  788. {
  789. return blit_filtered(position, source, src_rect, [](Color src) {
  790. return src.to_grayscale().lightened();
  791. });
  792. }
  793. void Painter::draw_tiled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source)
  794. {
  795. VERIFY((source.scale() == 1 || source.scale() == scale()) && "draw_tiled_bitmap only supports integer upsampling");
  796. auto dst_rect = a_dst_rect.translated(translation());
  797. auto clipped_rect = dst_rect.intersected(clip_rect());
  798. if (clipped_rect.is_empty())
  799. return;
  800. int scale = this->scale();
  801. clipped_rect *= scale;
  802. dst_rect *= scale;
  803. int const first_row = (clipped_rect.top() - dst_rect.top());
  804. int const last_row = (clipped_rect.bottom() - dst_rect.top());
  805. int const first_column = (clipped_rect.left() - dst_rect.left());
  806. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  807. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  808. if (source.format() == BitmapFormat::BGRx8888 || source.format() == BitmapFormat::BGRA8888) {
  809. int s = scale / source.scale();
  810. if (s == 1) {
  811. int x_start = first_column + a_dst_rect.left() * scale;
  812. for (int row = first_row; row <= last_row; ++row) {
  813. RGBA32 const* sl = source.scanline((row + a_dst_rect.top() * scale) % source.physical_height());
  814. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  815. dst[x - x_start] = sl[x % source.physical_width()];
  816. }
  817. dst += dst_skip;
  818. }
  819. } else {
  820. int x_start = first_column + a_dst_rect.left() * scale;
  821. for (int row = first_row; row <= last_row; ++row) {
  822. RGBA32 const* sl = source.scanline(((row + a_dst_rect.top() * scale) / s) % source.physical_height());
  823. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  824. dst[x - x_start] = sl[(x / s) % source.physical_width()];
  825. }
  826. dst += dst_skip;
  827. }
  828. }
  829. return;
  830. }
  831. VERIFY_NOT_REACHED();
  832. }
  833. void Painter::blit_offset(IntPoint const& a_position, Gfx::Bitmap const& source, IntRect const& a_src_rect, IntPoint const& offset)
  834. {
  835. auto src_rect = IntRect { a_src_rect.location() - offset, a_src_rect.size() };
  836. auto position = a_position;
  837. if (src_rect.x() < 0) {
  838. position.set_x(position.x() - src_rect.x());
  839. src_rect.set_x(0);
  840. }
  841. if (src_rect.y() < 0) {
  842. position.set_y(position.y() - src_rect.y());
  843. src_rect.set_y(0);
  844. }
  845. blit(position, source, src_rect);
  846. }
  847. void Painter::blit(IntPoint const& position, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, bool apply_alpha)
  848. {
  849. VERIFY(scale() >= source.scale() && "painter doesn't support downsampling scale factors");
  850. if (opacity < 1.0f || (source.has_alpha_channel() && apply_alpha))
  851. return blit_with_opacity(position, source, a_src_rect, opacity, apply_alpha);
  852. auto safe_src_rect = a_src_rect.intersected(source.rect());
  853. if (scale() != source.scale())
  854. return draw_scaled_bitmap({ position, safe_src_rect.size() }, source, safe_src_rect, opacity);
  855. // If we get here, the Painter might have a scale factor, but the source bitmap has the same scale factor.
  856. // We need to transform from logical to physical coordinates, but we can just copy pixels without resampling.
  857. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  858. auto clipped_rect = dst_rect.intersected(clip_rect());
  859. if (clipped_rect.is_empty())
  860. return;
  861. // All computations below are in physical coordinates.
  862. int scale = this->scale();
  863. auto src_rect = a_src_rect * scale;
  864. clipped_rect *= scale;
  865. dst_rect *= scale;
  866. int const first_row = clipped_rect.top() - dst_rect.top();
  867. int const last_row = clipped_rect.bottom() - dst_rect.top();
  868. int const first_column = clipped_rect.left() - dst_rect.left();
  869. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  870. size_t const dst_skip = m_target->pitch() / sizeof(RGBA32);
  871. if (source.format() == BitmapFormat::BGRx8888 || source.format() == BitmapFormat::BGRA8888) {
  872. RGBA32 const* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  873. size_t const src_skip = source.pitch() / sizeof(RGBA32);
  874. for (int row = first_row; row <= last_row; ++row) {
  875. fast_u32_copy(dst, src, clipped_rect.width());
  876. dst += dst_skip;
  877. src += src_skip;
  878. }
  879. return;
  880. }
  881. if (source.format() == BitmapFormat::RGBA8888) {
  882. u32 const* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  883. size_t const src_skip = source.pitch() / sizeof(u32);
  884. for (int row = first_row; row <= last_row; ++row) {
  885. for (int i = 0; i < clipped_rect.width(); ++i) {
  886. u32 rgba = src[i];
  887. u32 bgra = (rgba & 0xff00ff00)
  888. | ((rgba & 0x000000ff) << 16)
  889. | ((rgba & 0x00ff0000) >> 16);
  890. dst[i] = bgra;
  891. }
  892. dst += dst_skip;
  893. src += src_skip;
  894. }
  895. return;
  896. }
  897. if (Bitmap::is_indexed(source.format())) {
  898. u8 const* src = source.scanline_u8(src_rect.top() + first_row) + src_rect.left() + first_column;
  899. size_t const src_skip = source.pitch();
  900. for (int row = first_row; row <= last_row; ++row) {
  901. for (int i = 0; i < clipped_rect.width(); ++i)
  902. dst[i] = source.palette_color(src[i]).value();
  903. dst += dst_skip;
  904. src += src_skip;
  905. }
  906. return;
  907. }
  908. VERIFY_NOT_REACHED();
  909. }
  910. template<bool has_alpha_channel, typename GetPixel>
  911. 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)
  912. {
  913. bool has_opacity = opacity != 1.0f;
  914. for (int y = 0; y < src_rect.height(); ++y) {
  915. int dst_y = dst_rect.y() + y * vfactor;
  916. for (int x = 0; x < src_rect.width(); ++x) {
  917. auto src_pixel = get_pixel(source, x + src_rect.left(), y + src_rect.top());
  918. if (has_opacity)
  919. src_pixel.set_alpha(src_pixel.alpha() * opacity);
  920. for (int yo = 0; yo < vfactor; ++yo) {
  921. auto* scanline = (Color*)target.scanline(dst_y + yo);
  922. int dst_x = dst_rect.x() + x * hfactor;
  923. for (int xo = 0; xo < hfactor; ++xo) {
  924. if constexpr (has_alpha_channel)
  925. scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel);
  926. else
  927. scanline[dst_x + xo] = src_pixel;
  928. }
  929. }
  930. }
  931. }
  932. }
  933. template<bool has_alpha_channel, typename GetPixel>
  934. 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)
  935. {
  936. IntRect int_src_rect = enclosing_int_rect(src_rect);
  937. if (dst_rect == clipped_rect && int_src_rect == src_rect && !(dst_rect.width() % int_src_rect.width()) && !(dst_rect.height() % int_src_rect.height())) {
  938. int hfactor = dst_rect.width() / int_src_rect.width();
  939. int vfactor = dst_rect.height() / int_src_rect.height();
  940. if (hfactor == 2 && vfactor == 2)
  941. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 2, 2, get_pixel, opacity);
  942. if (hfactor == 3 && vfactor == 3)
  943. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 3, 3, get_pixel, opacity);
  944. if (hfactor == 4 && vfactor == 4)
  945. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 4, 4, get_pixel, opacity);
  946. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, hfactor, vfactor, get_pixel, opacity);
  947. }
  948. bool has_opacity = opacity != 1.0f;
  949. i64 shift = (i64)1 << 32;
  950. i64 hscale = (src_rect.width() * shift) / dst_rect.width();
  951. i64 vscale = (src_rect.height() * shift) / dst_rect.height();
  952. i64 src_left = src_rect.left() * shift;
  953. i64 src_top = src_rect.top() * shift;
  954. for (int y = clipped_rect.top(); y <= clipped_rect.bottom(); ++y) {
  955. auto* scanline = (Color*)target.scanline(y);
  956. for (int x = clipped_rect.left(); x <= clipped_rect.right(); ++x) {
  957. auto scaled_x = ((x - dst_rect.x()) * hscale + src_left) >> 32;
  958. auto scaled_y = ((y - dst_rect.y()) * vscale + src_top) >> 32;
  959. auto src_pixel = get_pixel(source, scaled_x, scaled_y);
  960. if (has_opacity)
  961. src_pixel.set_alpha(src_pixel.alpha() * opacity);
  962. if constexpr (has_alpha_channel) {
  963. scanline[x] = scanline[x].blend(src_pixel);
  964. } else
  965. scanline[x] = src_pixel;
  966. }
  967. }
  968. }
  969. void Painter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity)
  970. {
  971. draw_scaled_bitmap(a_dst_rect, source, FloatRect { a_src_rect }, opacity);
  972. }
  973. void Painter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source, FloatRect const& a_src_rect, float opacity)
  974. {
  975. IntRect int_src_rect = enclosing_int_rect(a_src_rect);
  976. if (scale() == source.scale() && a_src_rect == int_src_rect && a_dst_rect.size() == int_src_rect.size())
  977. return blit(a_dst_rect.location(), source, int_src_rect, opacity);
  978. auto dst_rect = to_physical(a_dst_rect);
  979. auto src_rect = a_src_rect * source.scale();
  980. auto clipped_rect = dst_rect.intersected(clip_rect() * scale());
  981. if (clipped_rect.is_empty())
  982. return;
  983. if (source.has_alpha_channel() || opacity != 1.0f) {
  984. switch (source.format()) {
  985. case BitmapFormat::BGRx8888:
  986. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::BGRx8888>, opacity);
  987. break;
  988. case BitmapFormat::BGRA8888:
  989. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::BGRA8888>, opacity);
  990. break;
  991. case BitmapFormat::Indexed8:
  992. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Indexed8>, opacity);
  993. break;
  994. case BitmapFormat::Indexed4:
  995. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Indexed4>, opacity);
  996. break;
  997. case BitmapFormat::Indexed2:
  998. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Indexed2>, opacity);
  999. break;
  1000. case BitmapFormat::Indexed1:
  1001. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Indexed1>, opacity);
  1002. break;
  1003. default:
  1004. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Invalid>, opacity);
  1005. break;
  1006. }
  1007. } else {
  1008. switch (source.format()) {
  1009. case BitmapFormat::BGRx8888:
  1010. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::BGRx8888>, opacity);
  1011. break;
  1012. case BitmapFormat::Indexed8:
  1013. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Indexed8>, opacity);
  1014. break;
  1015. default:
  1016. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel<BitmapFormat::Invalid>, opacity);
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. FLATTEN void Painter::draw_glyph(IntPoint const& point, u32 code_point, Color color)
  1022. {
  1023. draw_glyph(point, code_point, font(), color);
  1024. }
  1025. FLATTEN void Painter::draw_glyph(IntPoint const& point, u32 code_point, Font const& font, Color color)
  1026. {
  1027. auto glyph = font.glyph(code_point);
  1028. auto top_left = point + IntPoint(glyph.left_bearing(), 0);
  1029. if (glyph.is_glyph_bitmap()) {
  1030. draw_bitmap(top_left, glyph.glyph_bitmap(), color);
  1031. } else {
  1032. blit_filtered(top_left, *glyph.bitmap(), glyph.bitmap()->rect(), [color](Color pixel) -> Color {
  1033. return pixel.multiply(color);
  1034. });
  1035. }
  1036. }
  1037. void Painter::draw_emoji(IntPoint const& point, Gfx::Bitmap const& emoji, Font const& font)
  1038. {
  1039. if (!font.is_fixed_width())
  1040. blit(point, emoji, emoji.rect());
  1041. else {
  1042. IntRect dst_rect {
  1043. point.x(),
  1044. point.y(),
  1045. font.glyph_width('x'),
  1046. font.glyph_height()
  1047. };
  1048. draw_scaled_bitmap(dst_rect, emoji, emoji.rect());
  1049. }
  1050. }
  1051. void Painter::draw_glyph_or_emoji(IntPoint const& point, u32 code_point, Font const& font, Color color)
  1052. {
  1053. if (font.contains_glyph(code_point)) {
  1054. draw_glyph(point, code_point, font, color);
  1055. return;
  1056. }
  1057. // Perhaps it's an emoji?
  1058. auto* emoji = Emoji::emoji_for_code_point(code_point);
  1059. if (emoji == nullptr) {
  1060. dbgln_if(EMOJI_DEBUG, "Failed to find an emoji for code_point {}", code_point);
  1061. draw_glyph(point, '?', font, color);
  1062. return;
  1063. }
  1064. draw_emoji(point, *emoji, font);
  1065. }
  1066. template<typename DrawGlyphFunction>
  1067. void draw_text_line(IntRect const& a_rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextDirection direction, DrawGlyphFunction draw_glyph)
  1068. {
  1069. auto rect = a_rect;
  1070. switch (alignment) {
  1071. case TextAlignment::TopLeft:
  1072. case TextAlignment::CenterLeft:
  1073. case TextAlignment::BottomLeft:
  1074. break;
  1075. case TextAlignment::TopRight:
  1076. case TextAlignment::CenterRight:
  1077. case TextAlignment::BottomRight:
  1078. rect.set_x(rect.right() - font.width(text));
  1079. break;
  1080. case TextAlignment::Center: {
  1081. auto shrunken_rect = rect;
  1082. shrunken_rect.set_width(font.width(text));
  1083. shrunken_rect.center_within(rect);
  1084. rect = shrunken_rect;
  1085. break;
  1086. }
  1087. default:
  1088. VERIFY_NOT_REACHED();
  1089. }
  1090. if (is_vertically_centered_text_alignment(alignment)) {
  1091. int distance_from_baseline_to_bottom = (font.glyph_height() - 1) - font.baseline();
  1092. rect.translate_by(0, distance_from_baseline_to_bottom / 2);
  1093. }
  1094. auto point = rect.location();
  1095. int space_width = font.glyph_width(' ') + font.glyph_spacing();
  1096. if (direction == TextDirection::RTL) {
  1097. point.translate_by(rect.width(), 0); // Start drawing from the end
  1098. space_width = -space_width; // Draw spaces backwards
  1099. }
  1100. for (u32 code_point : text) {
  1101. if (code_point == ' ') {
  1102. point.translate_by(space_width, 0);
  1103. continue;
  1104. }
  1105. IntSize glyph_size(font.glyph_or_emoji_width(code_point) + font.glyph_spacing(), font.glyph_height());
  1106. if (direction == TextDirection::RTL)
  1107. point.translate_by(-glyph_size.width(), 0); // If we are drawing right to left, we have to move backwards before drawing the glyph
  1108. draw_glyph({ point, glyph_size }, code_point);
  1109. if (direction == TextDirection::LTR)
  1110. point.translate_by(glyph_size.width(), 0);
  1111. }
  1112. }
  1113. static inline size_t draw_text_get_length(Utf8View const& text)
  1114. {
  1115. return text.byte_length();
  1116. }
  1117. Vector<DirectionalRun> Painter::split_text_into_directional_runs(Utf8View const& text, TextDirection initial_direction)
  1118. {
  1119. // FIXME: This is a *very* simplified version of the UNICODE BIDIRECTIONAL ALGORITHM (https://www.unicode.org/reports/tr9/), that can render most bidirectional text
  1120. // but also produces awkward results in a large amount of edge cases. This should probably be replaced with a fully spec compliant implementation at some point.
  1121. // FIXME: Support HTML "dir" attribute (how?)
  1122. u8 paragraph_embedding_level = initial_direction == TextDirection::LTR ? 0 : 1;
  1123. Vector<u8> embedding_levels;
  1124. embedding_levels.ensure_capacity(text.length());
  1125. for (size_t i = 0; i < text.length(); i++)
  1126. embedding_levels.unchecked_append(paragraph_embedding_level);
  1127. // FIXME: Support Explicit Directional Formatting Characters
  1128. Vector<BidirectionalClass> character_classes;
  1129. character_classes.ensure_capacity(text.length());
  1130. for (u32 code_point : text)
  1131. character_classes.unchecked_append(get_char_bidi_class(code_point));
  1132. // resolving weak types
  1133. BidirectionalClass paragraph_class = initial_direction == TextDirection::LTR ? BidirectionalClass::STRONG_LTR : BidirectionalClass::STRONG_RTL;
  1134. for (size_t i = 0; i < character_classes.size(); i++) {
  1135. if (character_classes[i] != BidirectionalClass::WEAK_SEPARATORS)
  1136. continue;
  1137. for (ssize_t j = i - 1; j >= 0; j--) {
  1138. auto character_class = character_classes[j];
  1139. if (character_class != BidirectionalClass::STRONG_RTL && character_class != BidirectionalClass::STRONG_LTR)
  1140. continue;
  1141. character_classes[i] = character_class;
  1142. break;
  1143. }
  1144. if (character_classes[i] == BidirectionalClass::WEAK_SEPARATORS)
  1145. character_classes[i] = paragraph_class;
  1146. }
  1147. // resolving neutral types
  1148. auto left_side = BidirectionalClass::NEUTRAL;
  1149. auto sequence_length = 0;
  1150. for (size_t i = 0; i < character_classes.size(); i++) {
  1151. auto character_class = character_classes[i];
  1152. if (left_side == BidirectionalClass::NEUTRAL) {
  1153. if (character_class != BidirectionalClass::NEUTRAL)
  1154. left_side = character_class;
  1155. else
  1156. character_classes[i] = paragraph_class;
  1157. continue;
  1158. }
  1159. if (character_class != BidirectionalClass::NEUTRAL) {
  1160. BidirectionalClass sequence_class;
  1161. if (bidi_class_to_direction(left_side) == bidi_class_to_direction(character_class)) {
  1162. sequence_class = left_side == BidirectionalClass::STRONG_RTL ? BidirectionalClass::STRONG_RTL : BidirectionalClass::STRONG_LTR;
  1163. } else {
  1164. sequence_class = paragraph_class;
  1165. }
  1166. for (auto j = 0; j < sequence_length; j++) {
  1167. character_classes[i - j - 1] = sequence_class;
  1168. }
  1169. sequence_length = 0;
  1170. left_side = character_class;
  1171. } else {
  1172. sequence_length++;
  1173. }
  1174. }
  1175. for (auto i = 0; i < sequence_length; i++)
  1176. character_classes[character_classes.size() - i - 1] = paragraph_class;
  1177. // resolving implicit levels
  1178. for (size_t i = 0; i < character_classes.size(); i++) {
  1179. auto character_class = character_classes[i];
  1180. if ((embedding_levels[i] % 2) == 0) {
  1181. if (character_class == BidirectionalClass::STRONG_RTL)
  1182. embedding_levels[i] += 1;
  1183. else if (character_class == BidirectionalClass::WEAK_NUMBERS || character_class == BidirectionalClass::WEAK_SEPARATORS)
  1184. embedding_levels[i] += 2;
  1185. } else {
  1186. if (character_class == BidirectionalClass::STRONG_LTR || character_class == BidirectionalClass::WEAK_NUMBERS || character_class == BidirectionalClass::WEAK_SEPARATORS)
  1187. embedding_levels[i] += 1;
  1188. }
  1189. }
  1190. // splitting into runs
  1191. auto run_code_points_start = text.begin();
  1192. auto next_code_points_slice = [&](auto length) {
  1193. Vector<u32> run_code_points;
  1194. run_code_points.ensure_capacity(length);
  1195. for (size_t j = 0; j < length; ++j, ++run_code_points_start)
  1196. run_code_points.unchecked_append(*run_code_points_start);
  1197. return run_code_points;
  1198. };
  1199. Vector<DirectionalRun> runs;
  1200. size_t start = 0;
  1201. u8 level = embedding_levels[0];
  1202. for (size_t i = 1; i < embedding_levels.size(); ++i) {
  1203. if (embedding_levels[i] == level)
  1204. continue;
  1205. auto code_points_slice = next_code_points_slice(i - start);
  1206. runs.append({ move(code_points_slice), level });
  1207. start = i;
  1208. level = embedding_levels[i];
  1209. }
  1210. auto code_points_slice = next_code_points_slice(embedding_levels.size() - start);
  1211. runs.append({ move(code_points_slice), level });
  1212. // reordering resolved levels
  1213. // FIXME: missing special cases for trailing whitespace characters
  1214. u8 minimum_level = 128;
  1215. u8 maximum_level = 0;
  1216. for (auto& run : runs) {
  1217. minimum_level = min(minimum_level, run.embedding_level());
  1218. maximum_level = max(minimum_level, run.embedding_level());
  1219. }
  1220. if ((minimum_level % 2) == 0)
  1221. minimum_level++;
  1222. auto runs_count = runs.size() - 1;
  1223. while (maximum_level <= minimum_level) {
  1224. size_t run_index = 0;
  1225. while (run_index < runs_count) {
  1226. while (run_index < runs_count && runs[run_index].embedding_level() < maximum_level)
  1227. run_index++;
  1228. auto reverse_start = run_index;
  1229. while (run_index <= runs_count && runs[run_index].embedding_level() >= maximum_level)
  1230. run_index++;
  1231. auto reverse_end = run_index - 1;
  1232. while (reverse_start < reverse_end) {
  1233. swap(runs[reverse_start], runs[reverse_end]);
  1234. reverse_start++;
  1235. reverse_end--;
  1236. }
  1237. }
  1238. maximum_level--;
  1239. }
  1240. // mirroring RTL mirror characters
  1241. for (auto& run : runs) {
  1242. if (run.direction() == TextDirection::LTR)
  1243. continue;
  1244. for (auto& code_point : run.code_points()) {
  1245. code_point = get_mirror_char(code_point);
  1246. }
  1247. }
  1248. return runs;
  1249. }
  1250. bool Painter::text_contains_bidirectional_text(Utf8View const& text, TextDirection initial_direction)
  1251. {
  1252. for (u32 code_point : text) {
  1253. auto char_class = get_char_bidi_class(code_point);
  1254. if (char_class == BidirectionalClass::NEUTRAL)
  1255. continue;
  1256. if (bidi_class_to_direction(char_class) != initial_direction)
  1257. return true;
  1258. }
  1259. return false;
  1260. }
  1261. template<typename DrawGlyphFunction>
  1262. void Painter::do_draw_text(IntRect const& rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping, DrawGlyphFunction draw_glyph)
  1263. {
  1264. if (draw_text_get_length(text) == 0)
  1265. return;
  1266. TextLayout layout(&font, text, rect);
  1267. static int const line_spacing = 4;
  1268. int line_height = font.glyph_height() + line_spacing;
  1269. auto lines = layout.lines(elision, wrapping, line_spacing);
  1270. auto bounding_rect = layout.bounding_rect(wrapping, line_spacing);
  1271. switch (alignment) {
  1272. case TextAlignment::TopLeft:
  1273. bounding_rect.set_location(rect.location());
  1274. break;
  1275. case TextAlignment::TopRight:
  1276. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.y() });
  1277. break;
  1278. case TextAlignment::CenterLeft:
  1279. bounding_rect.set_location({ rect.x(), rect.center().y() - (bounding_rect.height() / 2) });
  1280. break;
  1281. case TextAlignment::CenterRight:
  1282. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.center().y() - (bounding_rect.height() / 2) });
  1283. break;
  1284. case TextAlignment::Center:
  1285. bounding_rect.center_within(rect);
  1286. break;
  1287. case TextAlignment::BottomLeft:
  1288. bounding_rect.set_location({ rect.x(), (rect.bottom() + 1) - bounding_rect.height() });
  1289. break;
  1290. case TextAlignment::BottomRight:
  1291. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), (rect.bottom() + 1) - bounding_rect.height() });
  1292. break;
  1293. default:
  1294. VERIFY_NOT_REACHED();
  1295. }
  1296. bounding_rect.intersect(rect);
  1297. for (size_t i = 0; i < lines.size(); ++i) {
  1298. auto line = Utf8View { lines[i] };
  1299. IntRect line_rect { bounding_rect.x(), bounding_rect.y() + static_cast<int>(i) * line_height, bounding_rect.width(), line_height };
  1300. line_rect.intersect(rect);
  1301. TextDirection line_direction = get_text_direction(line);
  1302. if (text_contains_bidirectional_text(line, line_direction)) { // Slow Path: The line contains mixed BiDi classes
  1303. auto directional_runs = split_text_into_directional_runs(line, line_direction);
  1304. auto current_dx = line_direction == TextDirection::LTR ? 0 : line_rect.width();
  1305. for (auto& directional_run : directional_runs) {
  1306. auto run_width = font.width(directional_run.text());
  1307. if (line_direction == TextDirection::RTL)
  1308. current_dx -= run_width;
  1309. auto run_rect = line_rect.translated(current_dx, 0);
  1310. run_rect.set_width(run_width);
  1311. // NOTE: DirectionalRun returns Utf32View which isn't
  1312. // compatible with draw_text_line.
  1313. StringBuilder builder;
  1314. builder.append(directional_run.text());
  1315. auto line_text = Utf8View { builder.string_view() };
  1316. draw_text_line(run_rect, line_text, font, alignment, directional_run.direction(), draw_glyph);
  1317. if (line_direction == TextDirection::LTR)
  1318. current_dx += run_width;
  1319. }
  1320. } else {
  1321. draw_text_line(line_rect, line, font, alignment, line_direction, draw_glyph);
  1322. }
  1323. }
  1324. }
  1325. void Painter::draw_text(IntRect const& rect, StringView const& text, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
  1326. {
  1327. draw_text(rect, text, font(), alignment, color, elision, wrapping);
  1328. }
  1329. void Painter::draw_text(IntRect const& rect, Utf32View const& text, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
  1330. {
  1331. draw_text(rect, text, font(), alignment, color, elision, wrapping);
  1332. }
  1333. void Painter::draw_text(IntRect const& rect, StringView const& raw_text, Font const& font, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
  1334. {
  1335. Utf8View text { raw_text };
  1336. do_draw_text(rect, text, font, alignment, elision, wrapping, [&](IntRect const& r, u32 code_point) {
  1337. draw_glyph_or_emoji(r.location(), code_point, font, color);
  1338. });
  1339. }
  1340. void Painter::draw_text(IntRect const& rect, Utf32View const& raw_text, Font const& font, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
  1341. {
  1342. // FIXME: UTF-32 should eventually be completely removed, but for the time
  1343. // being some places might depend on it, so we do some internal conversion.
  1344. StringBuilder builder;
  1345. builder.append(raw_text);
  1346. auto text = Utf8View { builder.string_view() };
  1347. do_draw_text(rect, text, font, alignment, elision, wrapping, [&](IntRect const& r, u32 code_point) {
  1348. draw_glyph_or_emoji(r.location(), code_point, font, color);
  1349. });
  1350. }
  1351. void Painter::draw_text(Function<void(IntRect const&, u32)> draw_one_glyph, IntRect const& rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
  1352. {
  1353. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1354. do_draw_text(rect, text, font, alignment, elision, wrapping, [&](IntRect const& r, u32 code_point) {
  1355. draw_one_glyph(r, code_point);
  1356. });
  1357. }
  1358. void Painter::draw_text(Function<void(IntRect const&, u32)> draw_one_glyph, IntRect const& rect, StringView const& raw_text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
  1359. {
  1360. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1361. Utf8View text { raw_text };
  1362. do_draw_text(rect, text, font, alignment, elision, wrapping, [&](IntRect const& r, u32 code_point) {
  1363. draw_one_glyph(r, code_point);
  1364. });
  1365. }
  1366. void Painter::draw_text(Function<void(IntRect const&, u32)> draw_one_glyph, IntRect const& rect, Utf32View const& raw_text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
  1367. {
  1368. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1369. // FIXME: UTF-32 should eventually be completely removed, but for the time
  1370. // being some places might depend on it, so we do some internal conversion.
  1371. StringBuilder builder;
  1372. builder.append(raw_text);
  1373. auto text = Utf8View { builder.string_view() };
  1374. do_draw_text(rect, text, font, alignment, elision, wrapping, [&](IntRect const& r, u32 code_point) {
  1375. draw_one_glyph(r, code_point);
  1376. });
  1377. }
  1378. void Painter::set_pixel(IntPoint const& p, Color color)
  1379. {
  1380. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1381. auto point = p;
  1382. point.translate_by(state().translation);
  1383. if (!clip_rect().contains(point))
  1384. return;
  1385. m_target->scanline(point.y())[point.x()] = color.value();
  1386. }
  1387. ALWAYS_INLINE void Painter::set_physical_pixel_with_draw_op(u32& pixel, Color const& color)
  1388. {
  1389. // This always sets a single physical pixel, independent of scale().
  1390. // This should only be called by routines that already handle scale.
  1391. switch (draw_op()) {
  1392. case DrawOp::Copy:
  1393. pixel = color.value();
  1394. break;
  1395. case DrawOp::Xor:
  1396. pixel = color.xored(Color::from_rgba(pixel)).value();
  1397. break;
  1398. case DrawOp::Invert:
  1399. pixel = Color::from_rgba(pixel).inverted().value();
  1400. break;
  1401. }
  1402. }
  1403. ALWAYS_INLINE void Painter::fill_physical_scanline_with_draw_op(int y, int x, int width, Color const& color)
  1404. {
  1405. // This always draws a single physical scanline, independent of scale().
  1406. // This should only be called by routines that already handle scale.
  1407. switch (draw_op()) {
  1408. case DrawOp::Copy:
  1409. fast_u32_fill(m_target->scanline(y) + x, color.value(), width);
  1410. break;
  1411. case DrawOp::Xor: {
  1412. auto* pixel = m_target->scanline(y) + x;
  1413. auto* end = pixel + width;
  1414. while (pixel < end) {
  1415. *pixel = Color::from_rgba(*pixel).xored(color).value();
  1416. pixel++;
  1417. }
  1418. break;
  1419. }
  1420. case DrawOp::Invert: {
  1421. auto* pixel = m_target->scanline(y) + x;
  1422. auto* end = pixel + width;
  1423. while (pixel < end) {
  1424. *pixel = Color::from_rgba(*pixel).inverted().value();
  1425. pixel++;
  1426. }
  1427. break;
  1428. }
  1429. }
  1430. }
  1431. void Painter::draw_physical_pixel(IntPoint const& physical_position, Color color, int thickness)
  1432. {
  1433. // This always draws a single physical pixel, independent of scale().
  1434. // This should only be called by routines that already handle scale
  1435. // (including scaling thickness).
  1436. VERIFY(draw_op() == DrawOp::Copy);
  1437. if (thickness <= 0)
  1438. return;
  1439. if (thickness == 1) { // Implies scale() == 1.
  1440. auto& pixel = m_target->scanline(physical_position.y())[physical_position.x()];
  1441. return set_physical_pixel_with_draw_op(pixel, Color::from_rgba(pixel).blend(color));
  1442. }
  1443. IntRect rect { physical_position, { thickness, thickness } };
  1444. rect.intersect(clip_rect() * scale());
  1445. fill_physical_rect(rect, color);
  1446. }
  1447. void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color, int thickness, LineStyle style, Color alternate_color)
  1448. {
  1449. if (thickness <= 0)
  1450. return;
  1451. if (color.alpha() == 0)
  1452. return;
  1453. auto clip_rect = this->clip_rect() * scale();
  1454. auto const p1 = thickness > 1 ? a_p1.translated(-(thickness / 2), -(thickness / 2)) : a_p1;
  1455. auto const p2 = thickness > 1 ? a_p2.translated(-(thickness / 2), -(thickness / 2)) : a_p2;
  1456. auto point1 = to_physical(p1);
  1457. auto point2 = to_physical(p2);
  1458. thickness *= scale();
  1459. auto alternate_color_is_transparent = alternate_color == Color::Transparent;
  1460. // Special case: vertical line.
  1461. if (point1.x() == point2.x()) {
  1462. int const x = point1.x();
  1463. if (x < clip_rect.left() || x > clip_rect.right())
  1464. return;
  1465. if (point1.y() > point2.y())
  1466. swap(point1, point2);
  1467. if (point1.y() > clip_rect.bottom())
  1468. return;
  1469. if (point2.y() < clip_rect.top())
  1470. return;
  1471. int min_y = max(point1.y(), clip_rect.top());
  1472. int max_y = min(point2.y(), clip_rect.bottom());
  1473. if (style == LineStyle::Dotted) {
  1474. for (int y = min_y; y <= max_y; y += thickness * 2)
  1475. draw_physical_pixel({ x, y }, color, thickness);
  1476. } else if (style == LineStyle::Dashed) {
  1477. for (int y = min_y; y <= max_y; y += thickness * 6) {
  1478. draw_physical_pixel({ x, y }, color, thickness);
  1479. draw_physical_pixel({ x, min(y + thickness, max_y) }, color, thickness);
  1480. draw_physical_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness);
  1481. if (!alternate_color_is_transparent) {
  1482. draw_physical_pixel({ x, min(y + thickness * 3, max_y) }, alternate_color, thickness);
  1483. draw_physical_pixel({ x, min(y + thickness * 4, max_y) }, alternate_color, thickness);
  1484. draw_physical_pixel({ x, min(y + thickness * 5, max_y) }, alternate_color, thickness);
  1485. }
  1486. }
  1487. } else {
  1488. for (int y = min_y; y <= max_y; y += thickness)
  1489. draw_physical_pixel({ x, y }, color, thickness);
  1490. }
  1491. return;
  1492. }
  1493. // Special case: horizontal line.
  1494. if (point1.y() == point2.y()) {
  1495. int const y = point1.y();
  1496. if (y < clip_rect.top() || y > clip_rect.bottom())
  1497. return;
  1498. if (point1.x() > point2.x())
  1499. swap(point1, point2);
  1500. if (point1.x() > clip_rect.right())
  1501. return;
  1502. if (point2.x() < clip_rect.left())
  1503. return;
  1504. int min_x = max(point1.x(), clip_rect.left());
  1505. int max_x = min(point2.x(), clip_rect.right());
  1506. if (style == LineStyle::Dotted) {
  1507. for (int x = min_x; x <= max_x; x += thickness * 2)
  1508. draw_physical_pixel({ x, y }, color, thickness);
  1509. } else if (style == LineStyle::Dashed) {
  1510. for (int x = min_x; x <= max_x; x += thickness * 6) {
  1511. draw_physical_pixel({ x, y }, color, thickness);
  1512. draw_physical_pixel({ min(x + thickness, max_x), y }, color, thickness);
  1513. draw_physical_pixel({ min(x + thickness * 2, max_x), y }, color, thickness);
  1514. if (!alternate_color_is_transparent) {
  1515. draw_physical_pixel({ min(x + thickness * 3, max_x), y }, alternate_color, thickness);
  1516. draw_physical_pixel({ min(x + thickness * 4, max_x), y }, alternate_color, thickness);
  1517. draw_physical_pixel({ min(x + thickness * 5, max_x), y }, alternate_color, thickness);
  1518. }
  1519. }
  1520. } else {
  1521. for (int x = min_x; x <= max_x; x += thickness)
  1522. draw_physical_pixel({ x, y }, color, thickness);
  1523. }
  1524. return;
  1525. }
  1526. // FIXME: Implement dotted/dashed diagonal lines.
  1527. VERIFY(style == LineStyle::Solid);
  1528. int const adx = abs(point2.x() - point1.x());
  1529. int const ady = abs(point2.y() - point1.y());
  1530. if (adx > ady) {
  1531. if (point1.x() > point2.x())
  1532. swap(point1, point2);
  1533. } else {
  1534. if (point1.y() > point2.y())
  1535. swap(point1, point2);
  1536. }
  1537. // FIXME: Implement clipping below.
  1538. int const dx = point2.x() - point1.x();
  1539. int const dy = point2.y() - point1.y();
  1540. int error = 0;
  1541. if (dx > dy) {
  1542. int const y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  1543. int const delta_error = 2 * abs(dy);
  1544. int y = point1.y();
  1545. for (int x = point1.x(); x <= point2.x(); ++x) {
  1546. if (clip_rect.contains(x, y))
  1547. draw_physical_pixel({ x, y }, color, thickness);
  1548. error += delta_error;
  1549. if (error >= dx) {
  1550. y += y_step;
  1551. error -= 2 * dx;
  1552. }
  1553. }
  1554. } else {
  1555. int const x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1);
  1556. int const delta_error = 2 * abs(dx);
  1557. int x = point1.x();
  1558. for (int y = point1.y(); y <= point2.y(); ++y) {
  1559. if (clip_rect.contains(x, y))
  1560. draw_physical_pixel({ x, y }, color, thickness);
  1561. error += delta_error;
  1562. if (error >= dy) {
  1563. x += x_step;
  1564. error -= 2 * dy;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. static bool can_approximate_bezier_curve(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& control)
  1570. {
  1571. constexpr static int tolerance = 15;
  1572. auto p1x = 3 * control.x() - 2 * p1.x() - p2.x();
  1573. auto p1y = 3 * control.y() - 2 * p1.y() - p2.y();
  1574. auto p2x = 3 * control.x() - 2 * p2.x() - p1.x();
  1575. auto p2y = 3 * control.y() - 2 * p2.y() - p1.y();
  1576. p1x = p1x * p1x;
  1577. p1y = p1y * p1y;
  1578. p2x = p2x * p2x;
  1579. p2y = p2y * p2y;
  1580. return max(p1x, p2x) + max(p1y, p2y) <= tolerance;
  1581. }
  1582. // static
  1583. void Painter::for_each_line_segment_on_bezier_curve(FloatPoint const& control_point, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>& callback)
  1584. {
  1585. struct SegmentDescriptor {
  1586. FloatPoint control_point;
  1587. FloatPoint p1;
  1588. FloatPoint p2;
  1589. };
  1590. static constexpr auto split_quadratic_bezier_curve = [](FloatPoint const& original_control, FloatPoint const& p1, FloatPoint const& p2, auto& segments) {
  1591. auto po1_midpoint = original_control + p1;
  1592. po1_midpoint /= 2;
  1593. auto po2_midpoint = original_control + p2;
  1594. po2_midpoint /= 2;
  1595. auto new_segment = po1_midpoint + po2_midpoint;
  1596. new_segment /= 2;
  1597. segments.enqueue({ po1_midpoint, p1, new_segment });
  1598. segments.enqueue({ po2_midpoint, new_segment, p2 });
  1599. };
  1600. Queue<SegmentDescriptor> segments;
  1601. segments.enqueue({ control_point, p1, p2 });
  1602. while (!segments.is_empty()) {
  1603. auto segment = segments.dequeue();
  1604. if (can_approximate_bezier_curve(segment.p1, segment.p2, segment.control_point))
  1605. callback(segment.p1, segment.p2);
  1606. else
  1607. split_quadratic_bezier_curve(segment.control_point, segment.p1, segment.p2, segments);
  1608. }
  1609. }
  1610. void Painter::for_each_line_segment_on_bezier_curve(FloatPoint const& control_point, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>&& callback)
  1611. {
  1612. for_each_line_segment_on_bezier_curve(control_point, p1, p2, callback);
  1613. }
  1614. void Painter::draw_quadratic_bezier_curve(IntPoint const& control_point, IntPoint const& p1, IntPoint const& p2, Color color, int thickness, LineStyle style)
  1615. {
  1616. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1617. if (thickness <= 0)
  1618. return;
  1619. for_each_line_segment_on_bezier_curve(FloatPoint(control_point), FloatPoint(p1), FloatPoint(p2), [&](FloatPoint const& fp1, FloatPoint const& fp2) {
  1620. draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
  1621. });
  1622. }
  1623. void Painter::for_each_line_segment_on_cubic_bezier_curve(FloatPoint const& control_point_0, FloatPoint const& control_point_1, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>&& callback)
  1624. {
  1625. for_each_line_segment_on_cubic_bezier_curve(control_point_0, control_point_1, p1, p2, callback);
  1626. }
  1627. static bool can_approximate_cubic_bezier_curve(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& control_0, FloatPoint const& control_1)
  1628. {
  1629. constexpr float tolerance = 15; // Arbitrary, seems like 10-30 produces nice results.
  1630. auto ax = 3 * control_0.x() - 2 * p1.x() - p2.x();
  1631. auto ay = 3 * control_0.y() - 2 * p1.y() - p2.y();
  1632. auto bx = 3 * control_1.x() - p1.x() - 2 * p2.x();
  1633. auto by = 3 * control_1.y() - p1.y() - 2 * p2.y();
  1634. ax *= ax;
  1635. ay *= ay;
  1636. bx *= bx;
  1637. by *= by;
  1638. return max(ax, bx) + max(ay, by) <= tolerance;
  1639. }
  1640. // static
  1641. void Painter::for_each_line_segment_on_cubic_bezier_curve(FloatPoint const& control_point_0, FloatPoint const& control_point_1, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>& callback)
  1642. {
  1643. struct ControlPair {
  1644. FloatPoint control_point_0;
  1645. FloatPoint control_point_1;
  1646. };
  1647. struct SegmentDescriptor {
  1648. ControlPair control_points;
  1649. FloatPoint p1;
  1650. FloatPoint p2;
  1651. };
  1652. static constexpr auto split_cubic_bezier_curve = [](ControlPair const& original_controls, FloatPoint const& p1, FloatPoint const& p2, auto& segments) {
  1653. Array level_1_midpoints {
  1654. (p1 + original_controls.control_point_0) / 2,
  1655. (original_controls.control_point_0 + original_controls.control_point_1) / 2,
  1656. (original_controls.control_point_1 + p2) / 2,
  1657. };
  1658. Array level_2_midpoints {
  1659. (level_1_midpoints[0] + level_1_midpoints[1]) / 2,
  1660. (level_1_midpoints[1] + level_1_midpoints[2]) / 2,
  1661. };
  1662. auto level_3_midpoint = (level_2_midpoints[0] + level_2_midpoints[1]) / 2;
  1663. segments.enqueue({ { level_1_midpoints[0], level_2_midpoints[0] }, p1, level_3_midpoint });
  1664. segments.enqueue({ { level_2_midpoints[1], level_1_midpoints[2] }, level_3_midpoint, p2 });
  1665. };
  1666. Queue<SegmentDescriptor> segments;
  1667. segments.enqueue({ { control_point_0, control_point_1 }, p1, p2 });
  1668. while (!segments.is_empty()) {
  1669. auto segment = segments.dequeue();
  1670. if (can_approximate_cubic_bezier_curve(segment.p1, segment.p2, segment.control_points.control_point_0, segment.control_points.control_point_1))
  1671. callback(segment.p1, segment.p2);
  1672. else
  1673. split_cubic_bezier_curve(segment.control_points, segment.p1, segment.p2, segments);
  1674. }
  1675. }
  1676. void Painter::draw_cubic_bezier_curve(IntPoint const& control_point_0, IntPoint const& control_point_1, IntPoint const& p1, IntPoint const& p2, Color color, int thickness, Painter::LineStyle style)
  1677. {
  1678. for_each_line_segment_on_cubic_bezier_curve(FloatPoint(control_point_0), FloatPoint(control_point_1), FloatPoint(p1), FloatPoint(p2), [&](FloatPoint const& fp1, FloatPoint const& fp2) {
  1679. draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
  1680. });
  1681. }
  1682. // static
  1683. void Painter::for_each_line_segment_on_elliptical_arc(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& center, FloatPoint const radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint const&, FloatPoint const&)>& callback)
  1684. {
  1685. if (radii.x() <= 0 || radii.y() <= 0)
  1686. return;
  1687. auto start = p1;
  1688. auto end = p2;
  1689. if (theta_delta < 0) {
  1690. swap(start, end);
  1691. theta_1 = theta_1 + theta_delta;
  1692. theta_delta = fabsf(theta_delta);
  1693. }
  1694. auto relative_start = start - center;
  1695. auto a = radii.x();
  1696. auto b = radii.y();
  1697. // The segments are at most 1 long
  1698. auto largest_radius = max(a, b);
  1699. double theta_step = atan(1 / (double)largest_radius);
  1700. FloatPoint current_point = relative_start;
  1701. FloatPoint next_point = { 0, 0 };
  1702. auto sin_x_axis = AK::sin(x_axis_rotation);
  1703. auto cos_x_axis = AK::cos(x_axis_rotation);
  1704. auto rotate_point = [sin_x_axis, cos_x_axis](FloatPoint& p) {
  1705. auto original_x = p.x();
  1706. auto original_y = p.y();
  1707. p.set_x(original_x * cos_x_axis - original_y * sin_x_axis);
  1708. p.set_y(original_x * sin_x_axis + original_y * cos_x_axis);
  1709. };
  1710. for (double theta = theta_1; theta <= ((double)theta_1 + (double)theta_delta); theta += theta_step) {
  1711. next_point.set_x(a * AK::cos<float>(theta));
  1712. next_point.set_y(b * AK::sin<float>(theta));
  1713. rotate_point(next_point);
  1714. callback(current_point + center, next_point + center);
  1715. current_point = next_point;
  1716. }
  1717. callback(current_point + center, end);
  1718. }
  1719. // static
  1720. void Painter::for_each_line_segment_on_elliptical_arc(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& center, FloatPoint const radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint const&, FloatPoint const&)>&& callback)
  1721. {
  1722. for_each_line_segment_on_elliptical_arc(p1, p2, center, radii, x_axis_rotation, theta_1, theta_delta, callback);
  1723. }
  1724. void Painter::draw_elliptical_arc(IntPoint const& p1, IntPoint const& p2, IntPoint const& center, FloatPoint const& radii, float x_axis_rotation, float theta_1, float theta_delta, Color color, int thickness, LineStyle style)
  1725. {
  1726. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1727. if (thickness <= 0)
  1728. return;
  1729. for_each_line_segment_on_elliptical_arc(FloatPoint(p1), FloatPoint(p2), FloatPoint(center), radii, x_axis_rotation, theta_1, theta_delta, [&](FloatPoint const& fp1, FloatPoint const& fp2) {
  1730. draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
  1731. });
  1732. }
  1733. void Painter::add_clip_rect(IntRect const& rect)
  1734. {
  1735. state().clip_rect.intersect(rect.translated(translation()));
  1736. state().clip_rect.intersect(m_target->rect()); // FIXME: This shouldn't be necessary?
  1737. }
  1738. void Painter::clear_clip_rect()
  1739. {
  1740. state().clip_rect = m_clip_origin;
  1741. }
  1742. PainterStateSaver::PainterStateSaver(Painter& painter)
  1743. : m_painter(painter)
  1744. {
  1745. m_painter.save();
  1746. }
  1747. PainterStateSaver::~PainterStateSaver()
  1748. {
  1749. m_painter.restore();
  1750. }
  1751. void Painter::stroke_path(Path const& path, Color color, int thickness)
  1752. {
  1753. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1754. if (thickness <= 0)
  1755. return;
  1756. FloatPoint cursor;
  1757. for (auto& segment : path.segments()) {
  1758. switch (segment.type()) {
  1759. case Segment::Type::Invalid:
  1760. VERIFY_NOT_REACHED();
  1761. break;
  1762. case Segment::Type::MoveTo:
  1763. cursor = segment.point();
  1764. break;
  1765. case Segment::Type::LineTo:
  1766. draw_line(cursor.to_type<int>(), segment.point().to_type<int>(), color, thickness);
  1767. cursor = segment.point();
  1768. break;
  1769. case Segment::Type::QuadraticBezierCurveTo: {
  1770. auto& through = static_cast<QuadraticBezierCurveSegment const&>(segment).through();
  1771. draw_quadratic_bezier_curve(through.to_type<int>(), cursor.to_type<int>(), segment.point().to_type<int>(), color, thickness);
  1772. cursor = segment.point();
  1773. break;
  1774. }
  1775. case Segment::Type::CubicBezierCurveTo: {
  1776. auto& curve = static_cast<CubicBezierCurveSegment const&>(segment);
  1777. auto& through_0 = curve.through_0();
  1778. auto& through_1 = curve.through_1();
  1779. draw_cubic_bezier_curve(through_0.to_type<int>(), through_1.to_type<int>(), cursor.to_type<int>(), segment.point().to_type<int>(), color, thickness);
  1780. cursor = segment.point();
  1781. break;
  1782. }
  1783. case Segment::Type::EllipticalArcTo:
  1784. auto& arc = static_cast<EllipticalArcSegment const&>(segment);
  1785. draw_elliptical_arc(cursor.to_type<int>(), segment.point().to_type<int>(), arc.center().to_type<int>(), arc.radii(), arc.x_axis_rotation(), arc.theta_1(), arc.theta_delta(), color, thickness);
  1786. cursor = segment.point();
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. void Painter::fill_path(Path const& path, Color color, WindingRule winding_rule)
  1792. {
  1793. VERIFY(scale() == 1); // FIXME: Add scaling support.
  1794. Detail::fill_path<Detail::FillPathMode::PlaceOnIntGrid>(*this, path, color, winding_rule);
  1795. }
  1796. void Painter::blit_disabled(IntPoint const& location, Gfx::Bitmap const& bitmap, IntRect const& rect, Palette const& palette)
  1797. {
  1798. auto bright_color = palette.threed_highlight();
  1799. auto dark_color = palette.threed_shadow1();
  1800. blit_filtered(location.translated(1, 1), bitmap, rect, [&](auto) {
  1801. return bright_color;
  1802. });
  1803. blit_filtered(location, bitmap, rect, [&](Color src) {
  1804. int gray = src.to_grayscale().red();
  1805. if (gray > 160)
  1806. return bright_color;
  1807. return dark_color;
  1808. });
  1809. }
  1810. void Painter::blit_tiled(IntRect const& dst_rect, Gfx::Bitmap const& bitmap, IntRect const& rect)
  1811. {
  1812. auto tile_width = rect.width();
  1813. auto tile_height = rect.height();
  1814. auto dst_right = dst_rect.right();
  1815. auto dst_bottom = dst_rect.bottom();
  1816. for (int tile_y = dst_rect.top(); tile_y < dst_bottom; tile_y += tile_height) {
  1817. for (int tile_x = dst_rect.left(); tile_x < dst_right; tile_x += tile_width) {
  1818. IntRect tile_src_rect = rect;
  1819. auto tile_x_overflow = tile_x + tile_width - dst_right;
  1820. if (tile_x_overflow > 0) {
  1821. tile_src_rect.set_width(tile_width - tile_x_overflow);
  1822. }
  1823. auto tile_y_overflow = tile_y + tile_height - dst_bottom;
  1824. if (tile_y_overflow > 0) {
  1825. tile_src_rect.set_height(tile_height - tile_y_overflow);
  1826. }
  1827. blit(IntPoint(tile_x, tile_y), bitmap, tile_src_rect);
  1828. }
  1829. }
  1830. }
  1831. String parse_ampersand_string(StringView const& raw_text, Optional<size_t>* underline_offset)
  1832. {
  1833. if (raw_text.is_empty())
  1834. return String::empty();
  1835. StringBuilder builder;
  1836. for (size_t i = 0; i < raw_text.length(); ++i) {
  1837. if (raw_text[i] == '&') {
  1838. if (i != (raw_text.length() - 1) && raw_text[i + 1] == '&') {
  1839. builder.append(raw_text[i]);
  1840. ++i;
  1841. } else if (underline_offset && !(*underline_offset).has_value()) {
  1842. *underline_offset = i;
  1843. }
  1844. continue;
  1845. }
  1846. builder.append(raw_text[i]);
  1847. }
  1848. return builder.to_string();
  1849. }
  1850. void Gfx::Painter::draw_ui_text(Gfx::IntRect const& rect, StringView const& text, Gfx::Font const& font, Gfx::TextAlignment text_alignment, Gfx::Color color)
  1851. {
  1852. Optional<size_t> underline_offset;
  1853. auto name_to_draw = parse_ampersand_string(text, &underline_offset);
  1854. Gfx::IntRect text_rect { 0, 0, font.width(name_to_draw), font.glyph_height() };
  1855. text_rect.align_within(rect, text_alignment);
  1856. draw_text(text_rect, name_to_draw, font, text_alignment, color);
  1857. if (underline_offset.has_value()) {
  1858. Utf8View utf8_view { name_to_draw };
  1859. int width = 0;
  1860. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  1861. if (utf8_view.byte_offset_of(it) >= underline_offset.value()) {
  1862. int y = text_rect.bottom() + 1;
  1863. int x1 = text_rect.left() + width;
  1864. int x2 = x1 + font.glyph_or_emoji_width(*it);
  1865. draw_line({ x1, y }, { x2, y }, color);
  1866. break;
  1867. }
  1868. width += font.glyph_or_emoji_width(*it) + font.glyph_spacing();
  1869. }
  1870. }
  1871. }
  1872. }