Painter.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Painter.h"
  27. #include "Bitmap.h"
  28. #include "Emoji.h"
  29. #include "Font.h"
  30. #include "Gamma.h"
  31. #include <AK/Assertions.h>
  32. #include <AK/Function.h>
  33. #include <AK/Memory.h>
  34. #include <AK/QuickSort.h>
  35. #include <AK/StdLibExtras.h>
  36. #include <AK/StringBuilder.h>
  37. #include <AK/Utf32View.h>
  38. #include <AK/Utf8View.h>
  39. #include <LibGfx/CharacterBitmap.h>
  40. #include <LibGfx/Palette.h>
  41. #include <LibGfx/Path.h>
  42. #include <math.h>
  43. #include <stdio.h>
  44. #if defined(__GNUC__) && !defined(__clang__)
  45. # pragma GCC optimize("O3")
  46. #endif
  47. namespace Gfx {
  48. template<BitmapFormat format = BitmapFormat::Invalid>
  49. ALWAYS_INLINE Color get_pixel(const Gfx::Bitmap& bitmap, int x, int y)
  50. {
  51. if constexpr (format == BitmapFormat::Indexed8)
  52. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  53. if constexpr (format == BitmapFormat::Indexed4)
  54. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  55. if constexpr (format == BitmapFormat::Indexed2)
  56. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  57. if constexpr (format == BitmapFormat::Indexed1)
  58. return bitmap.palette_color(bitmap.scanline_u8(y)[x]);
  59. if constexpr (format == BitmapFormat::RGB32)
  60. return Color::from_rgb(bitmap.scanline(y)[x]);
  61. if constexpr (format == BitmapFormat::RGBA32)
  62. return Color::from_rgba(bitmap.scanline(y)[x]);
  63. return bitmap.get_pixel(x, y);
  64. }
  65. Painter::Painter(Gfx::Bitmap& bitmap)
  66. : m_target(bitmap)
  67. {
  68. ASSERT(bitmap.format() == Gfx::BitmapFormat::RGB32 || bitmap.format() == Gfx::BitmapFormat::RGBA32);
  69. m_state_stack.append(State());
  70. state().font = &Font::default_font();
  71. state().clip_rect = { { 0, 0 }, bitmap.size() };
  72. m_clip_origin = state().clip_rect;
  73. }
  74. Painter::~Painter()
  75. {
  76. }
  77. void Painter::fill_rect_with_draw_op(const IntRect& a_rect, Color color)
  78. {
  79. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  80. if (rect.is_empty())
  81. return;
  82. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  83. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  84. for (int i = rect.height() - 1; i >= 0; --i) {
  85. for (int j = 0; j < rect.width(); ++j)
  86. set_pixel_with_draw_op(dst[j], color);
  87. dst += dst_skip;
  88. }
  89. }
  90. void Painter::clear_rect(const IntRect& a_rect, Color color)
  91. {
  92. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  93. if (rect.is_empty())
  94. return;
  95. ASSERT(m_target->rect().contains(rect));
  96. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  97. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  98. for (int i = rect.height() - 1; i >= 0; --i) {
  99. fast_u32_fill(dst, color.value(), rect.width());
  100. dst += dst_skip;
  101. }
  102. }
  103. void Painter::fill_rect(const IntRect& a_rect, Color color)
  104. {
  105. if (color.alpha() == 0)
  106. return;
  107. if (draw_op() != DrawOp::Copy) {
  108. fill_rect_with_draw_op(a_rect, color);
  109. return;
  110. }
  111. if (color.alpha() == 0xff) {
  112. clear_rect(a_rect, color);
  113. return;
  114. }
  115. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  116. if (rect.is_empty())
  117. return;
  118. ASSERT(m_target->rect().contains(rect));
  119. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  120. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  121. for (int i = rect.height() - 1; i >= 0; --i) {
  122. for (int j = 0; j < rect.width(); ++j)
  123. dst[j] = Color::from_rgba(dst[j]).blend(color).value();
  124. dst += dst_skip;
  125. }
  126. }
  127. void Painter::fill_rect_with_dither_pattern(const IntRect& a_rect, Color color_a, Color color_b)
  128. {
  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. const size_t 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 = (i & 1) ^ (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(const IntRect& a_rect, const IntSize& cell_size, Color color_dark, Color color_light)
  147. {
  148. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  149. if (rect.is_empty())
  150. return;
  151. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  152. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  153. for (int i = 0; i < rect.height(); ++i) {
  154. for (int j = 0; j < rect.width(); ++j) {
  155. int cell_row = i / cell_size.height();
  156. int cell_col = j / cell_size.width();
  157. dst[j] = ((cell_row % 2) ^ (cell_col % 2)) ? color_light.value() : color_dark.value();
  158. }
  159. dst += dst_skip;
  160. }
  161. }
  162. void Painter::fill_rect_with_gradient(Orientation orientation, const IntRect& a_rect, Color gradient_start, Color gradient_end)
  163. {
  164. #ifdef NO_FPU
  165. return fill_rect(a_rect, gradient_start);
  166. #endif
  167. auto rect = a_rect.translated(translation());
  168. auto clipped_rect = IntRect::intersection(rect, clip_rect());
  169. if (clipped_rect.is_empty())
  170. return;
  171. int offset = clipped_rect.primary_offset_for_orientation(orientation) - rect.primary_offset_for_orientation(orientation);
  172. RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
  173. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  174. float increment = (1.0 / ((rect.primary_size_for_orientation(orientation))));
  175. if (orientation == Orientation::Horizontal) {
  176. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  177. float c = offset * increment;
  178. for (int j = 0; j < clipped_rect.width(); ++j) {
  179. dst[j] = gamma_accurate_blend(gradient_start, gradient_end, c).value();
  180. c += increment;
  181. }
  182. dst += dst_skip;
  183. }
  184. } else {
  185. float c = offset * increment;
  186. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  187. auto color = gamma_accurate_blend(gradient_start, gradient_end, c);
  188. for (int j = 0; j < clipped_rect.width(); ++j) {
  189. dst[j] = color.value();
  190. }
  191. c += increment;
  192. dst += dst_skip;
  193. }
  194. }
  195. }
  196. void Painter::fill_rect_with_gradient(const IntRect& a_rect, Color gradient_start, Color gradient_end)
  197. {
  198. return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end);
  199. }
  200. void Painter::fill_ellipse(const IntRect& a_rect, Color color)
  201. {
  202. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  203. if (rect.is_empty())
  204. return;
  205. ASSERT(m_target->rect().contains(rect));
  206. RGBA32* dst = m_target->scanline(rect.top()) + rect.left() + rect.width() / 2;
  207. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  208. for (int i = 0; i < rect.height(); i++) {
  209. double y = rect.height() * 0.5 - i;
  210. double x = rect.width() * sqrt(0.25 - y * y / rect.height() / rect.height());
  211. fast_u32_fill(dst - (int)x, color.value(), 2 * (int)x);
  212. dst += dst_skip;
  213. }
  214. }
  215. void Painter::draw_ellipse_intersecting(const IntRect& rect, Color color, int thickness)
  216. {
  217. constexpr int number_samples = 100; // FIXME: dynamically work out the number of samples based upon the rect size
  218. double increment = M_PI / number_samples;
  219. auto ellipse_x = [&](double theta) -> int {
  220. return (cos(theta) * rect.width() / sqrt(2)) + rect.center().x();
  221. };
  222. auto ellipse_y = [&](double theta) -> int {
  223. return (sin(theta) * rect.height() / sqrt(2)) + rect.center().y();
  224. };
  225. for (float theta = 0; theta < 2 * M_PI; theta += increment) {
  226. draw_line({ ellipse_x(theta), ellipse_y(theta) }, { ellipse_x(theta + increment), ellipse_y(theta + increment) }, color, thickness);
  227. }
  228. }
  229. template<typename RectType, typename Callback>
  230. static void for_each_pixel_around_rect_clockwise(const RectType& rect, Callback callback)
  231. {
  232. if (rect.is_empty())
  233. return;
  234. for (auto x = rect.left(); x <= rect.right(); ++x) {
  235. callback(x, rect.top());
  236. }
  237. for (auto y = rect.top() + 1; y <= rect.bottom(); ++y) {
  238. callback(rect.right(), y);
  239. }
  240. for (auto x = rect.right() - 1; x >= rect.left(); --x) {
  241. callback(x, rect.bottom());
  242. }
  243. for (auto y = rect.bottom() - 1; y > rect.top(); --y) {
  244. callback(rect.left(), y);
  245. }
  246. }
  247. void Painter::draw_focus_rect(const IntRect& rect, Color color)
  248. {
  249. if (rect.is_empty())
  250. return;
  251. bool state = false;
  252. for_each_pixel_around_rect_clockwise(rect, [&](auto x, auto y) {
  253. if (state)
  254. set_pixel(x, y, color);
  255. state = !state;
  256. });
  257. }
  258. void Painter::draw_rect(const IntRect& a_rect, Color color, bool rough)
  259. {
  260. IntRect rect = a_rect.translated(translation());
  261. auto clipped_rect = rect.intersected(clip_rect());
  262. if (clipped_rect.is_empty())
  263. return;
  264. int min_y = clipped_rect.top();
  265. int max_y = clipped_rect.bottom();
  266. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  267. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  268. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  269. fill_scanline_with_draw_op(rect.top(), start_x, width, color);
  270. ++min_y;
  271. }
  272. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  273. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  274. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  275. fill_scanline_with_draw_op(rect.bottom(), start_x, width, color);
  276. --max_y;
  277. }
  278. bool draw_left_side = rect.left() >= clipped_rect.left();
  279. bool draw_right_side = rect.right() == clipped_rect.right();
  280. if (draw_left_side && draw_right_side) {
  281. // Specialized loop when drawing both sides.
  282. for (int y = min_y; y <= max_y; ++y) {
  283. auto* bits = m_target->scanline(y);
  284. set_pixel_with_draw_op(bits[rect.left()], color);
  285. set_pixel_with_draw_op(bits[rect.right()], color);
  286. }
  287. } else {
  288. for (int y = min_y; y <= max_y; ++y) {
  289. auto* bits = m_target->scanline(y);
  290. if (draw_left_side)
  291. set_pixel_with_draw_op(bits[rect.left()], color);
  292. if (draw_right_side)
  293. set_pixel_with_draw_op(bits[rect.right()], color);
  294. }
  295. }
  296. }
  297. void Painter::draw_bitmap(const IntPoint& p, const CharacterBitmap& bitmap, Color color)
  298. {
  299. auto rect = IntRect(p, bitmap.size()).translated(translation());
  300. auto clipped_rect = rect.intersected(clip_rect());
  301. if (clipped_rect.is_empty())
  302. return;
  303. const int first_row = clipped_rect.top() - rect.top();
  304. const int last_row = clipped_rect.bottom() - rect.top();
  305. const int first_column = clipped_rect.left() - rect.left();
  306. const int last_column = clipped_rect.right() - rect.left();
  307. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  308. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  309. const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  310. const size_t bitmap_skip = bitmap.width();
  311. for (int row = first_row; row <= last_row; ++row) {
  312. for (int j = 0; j <= (last_column - first_column); ++j) {
  313. char fc = bitmap_row[j];
  314. if (fc == '#')
  315. dst[j] = color.value();
  316. }
  317. bitmap_row += bitmap_skip;
  318. dst += dst_skip;
  319. }
  320. }
  321. void Painter::draw_bitmap(const IntPoint& p, const GlyphBitmap& bitmap, Color color)
  322. {
  323. auto dst_rect = IntRect(p, bitmap.size()).translated(translation());
  324. auto clipped_rect = dst_rect.intersected(clip_rect());
  325. if (clipped_rect.is_empty())
  326. return;
  327. const int first_row = clipped_rect.top() - dst_rect.top();
  328. const int last_row = clipped_rect.bottom() - dst_rect.top();
  329. const int first_column = clipped_rect.left() - dst_rect.left();
  330. const int last_column = clipped_rect.right() - dst_rect.left();
  331. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  332. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  333. for (int row = first_row; row <= last_row; ++row) {
  334. for (int j = 0; j <= (last_column - first_column); ++j) {
  335. if (bitmap.bit_at(j + first_column, row))
  336. dst[j] = color.value();
  337. }
  338. dst += dst_skip;
  339. }
  340. }
  341. void Painter::draw_triangle(const IntPoint& a, const IntPoint& b, const IntPoint& c, Color color)
  342. {
  343. RGBA32 rgba = color.value();
  344. IntPoint p0(a);
  345. IntPoint p1(b);
  346. IntPoint p2(c);
  347. if (p0.y() > p1.y())
  348. swap(p0, p1);
  349. if (p0.y() > p2.y())
  350. swap(p0, p2);
  351. if (p1.y() > p2.y())
  352. swap(p1, p2);
  353. auto clip = clip_rect();
  354. if (p0.y() >= clip.bottom())
  355. return;
  356. if (p2.y() < clip.top())
  357. return;
  358. float dx01 = (float)(p1.x() - p0.x()) / (p1.y() - p0.y());
  359. float dx02 = (float)(p2.x() - p0.x()) / (p2.y() - p0.y());
  360. float dx12 = (float)(p2.x() - p1.x()) / (p2.y() - p1.y());
  361. float x01 = p0.x();
  362. float x02 = p0.x();
  363. int top = p0.y();
  364. if (top < clip.top()) {
  365. x01 += dx01 * (clip.top() - top);
  366. x02 += dx02 * (clip.top() - top);
  367. top = clip.top();
  368. }
  369. for (int y = top; y < p1.y() && y < clip.bottom(); ++y) {
  370. int start = x01 > x02 ? max((int)x02, clip.left()) : max((int)x01, clip.left());
  371. int end = x01 > x02 ? min((int)x01, clip.right()) : min((int)x02, clip.right());
  372. auto* scanline = m_target->scanline(y);
  373. for (int x = start; x < end; x++) {
  374. scanline[x] = rgba;
  375. }
  376. x01 += dx01;
  377. x02 += dx02;
  378. }
  379. x02 = p0.x() + dx02 * (p1.y() - p0.y());
  380. float x12 = p1.x();
  381. top = p1.y();
  382. if (top < clip.top()) {
  383. x02 += dx02 * (clip.top() - top);
  384. x12 += dx12 * (clip.top() - top);
  385. top = clip.top();
  386. }
  387. for (int y = top; y < p2.y() && y < clip.bottom(); ++y) {
  388. int start = x12 > x02 ? max((int)x02, clip.left()) : max((int)x12, clip.left());
  389. int end = x12 > x02 ? min((int)x12, clip.right()) : min((int)x02, clip.right());
  390. auto* scanline = m_target->scanline(y);
  391. for (int x = start; x < end; x++) {
  392. scanline[x] = rgba;
  393. }
  394. x02 += dx02;
  395. x12 += dx12;
  396. }
  397. }
  398. void Painter::blit_scaled(const IntRect& dst_rect_raw, const Gfx::Bitmap& source, const IntRect& src_rect, float hscale, float vscale)
  399. {
  400. auto dst_rect = IntRect(dst_rect_raw.location(), dst_rect_raw.size()).translated(translation());
  401. auto clipped_rect = dst_rect.intersected(clip_rect());
  402. if (clipped_rect.is_empty())
  403. return;
  404. const int first_row = (clipped_rect.top() - dst_rect.top());
  405. const int last_row = (clipped_rect.bottom() - dst_rect.top());
  406. const int first_column = (clipped_rect.left() - dst_rect.left());
  407. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  408. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  409. int x_start = first_column + src_rect.left();
  410. for (int row = first_row; row <= last_row; ++row) {
  411. int sr = (row + src_rect.top()) * vscale;
  412. if (sr >= source.size().height() || sr < 0) {
  413. dst += dst_skip;
  414. continue;
  415. }
  416. const RGBA32* sl = source.scanline(sr);
  417. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  418. int sx = x * hscale;
  419. if (sx < source.size().width() && sx >= 0)
  420. dst[x - x_start] = sl[sx];
  421. }
  422. dst += dst_skip;
  423. }
  424. return;
  425. }
  426. void Painter::blit_with_opacity(const IntPoint& position, const Gfx::Bitmap& source, const IntRect& src_rect, float opacity)
  427. {
  428. ASSERT(!m_target->has_alpha_channel());
  429. if (!opacity)
  430. return;
  431. if (opacity >= 1.0f)
  432. return blit(position, source, src_rect);
  433. u8 alpha = 255 * opacity;
  434. IntRect safe_src_rect = IntRect::intersection(src_rect, source.rect());
  435. IntRect dst_rect(position, safe_src_rect.size());
  436. dst_rect.move_by(state().translation);
  437. auto clipped_rect = IntRect::intersection(dst_rect, clip_rect());
  438. if (clipped_rect.is_empty())
  439. return;
  440. const int first_row = clipped_rect.top() - dst_rect.top();
  441. const int last_row = clipped_rect.bottom() - dst_rect.top();
  442. const int first_column = clipped_rect.left() - dst_rect.left();
  443. const int last_column = clipped_rect.right() - dst_rect.left();
  444. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  445. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  446. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  447. const unsigned src_skip = source.pitch() / sizeof(RGBA32);
  448. for (int row = first_row; row <= last_row; ++row) {
  449. for (int x = 0; x <= (last_column - first_column); ++x) {
  450. Color src_color_with_alpha = Color::from_rgb(src[x]);
  451. src_color_with_alpha.set_alpha(alpha);
  452. Color dst_color = Color::from_rgb(dst[x]);
  453. dst[x] = dst_color.blend(src_color_with_alpha).value();
  454. }
  455. dst += dst_skip;
  456. src += src_skip;
  457. }
  458. }
  459. void Painter::blit_filtered(const IntPoint& position, const Gfx::Bitmap& source, const IntRect& src_rect, Function<Color(Color)> filter)
  460. {
  461. IntRect safe_src_rect = src_rect.intersected(source.rect());
  462. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  463. auto clipped_rect = dst_rect.intersected(clip_rect());
  464. if (clipped_rect.is_empty())
  465. return;
  466. const int first_row = clipped_rect.top() - dst_rect.top();
  467. const int last_row = clipped_rect.bottom() - dst_rect.top();
  468. const int first_column = clipped_rect.left() - dst_rect.left();
  469. const int last_column = clipped_rect.right() - dst_rect.left();
  470. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  471. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  472. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  473. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  474. for (int row = first_row; row <= last_row; ++row) {
  475. for (int x = 0; x <= (last_column - first_column); ++x) {
  476. u8 alpha = Color::from_rgba(src[x]).alpha();
  477. if (alpha == 0xff)
  478. dst[x] = filter(Color::from_rgba(src[x])).value();
  479. else if (!alpha)
  480. continue;
  481. else
  482. dst[x] = Color::from_rgba(dst[x]).blend(filter(Color::from_rgba(src[x]))).value();
  483. }
  484. dst += dst_skip;
  485. src += src_skip;
  486. }
  487. }
  488. void Painter::blit_brightened(const IntPoint& position, const Gfx::Bitmap& source, const IntRect& src_rect)
  489. {
  490. return blit_filtered(position, source, src_rect, [](Color src) {
  491. return src.lightened();
  492. });
  493. }
  494. void Painter::blit_dimmed(const IntPoint& position, const Gfx::Bitmap& source, const IntRect& src_rect)
  495. {
  496. return blit_filtered(position, source, src_rect, [](Color src) {
  497. return src.to_grayscale().lightened();
  498. });
  499. }
  500. void Painter::draw_tiled_bitmap(const IntRect& a_dst_rect, const Gfx::Bitmap& source)
  501. {
  502. auto dst_rect = a_dst_rect.translated(translation());
  503. auto clipped_rect = dst_rect.intersected(clip_rect());
  504. if (clipped_rect.is_empty())
  505. return;
  506. const int first_row = (clipped_rect.top() - dst_rect.top());
  507. const int last_row = (clipped_rect.bottom() - dst_rect.top());
  508. const int first_column = (clipped_rect.left() - dst_rect.left());
  509. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  510. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  511. if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) {
  512. int x_start = first_column + a_dst_rect.left();
  513. for (int row = first_row; row <= last_row; ++row) {
  514. const RGBA32* sl = source.scanline((row + a_dst_rect.top())
  515. % source.size().height());
  516. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  517. dst[x - x_start] = sl[x % source.size().width()];
  518. }
  519. dst += dst_skip;
  520. }
  521. return;
  522. }
  523. ASSERT_NOT_REACHED();
  524. }
  525. void Painter::blit_offset(const IntPoint& position,
  526. const Gfx::Bitmap& source,
  527. const IntRect& src_rect,
  528. const IntPoint& offset)
  529. {
  530. auto dst_rect = IntRect(position, src_rect.size()).translated(translation());
  531. auto clipped_rect = dst_rect.intersected(clip_rect());
  532. if (clipped_rect.is_empty())
  533. return;
  534. const int first_row = (clipped_rect.top() - dst_rect.top());
  535. const int last_row = (clipped_rect.bottom() - dst_rect.top());
  536. const int first_column = (clipped_rect.left() - dst_rect.left());
  537. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  538. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  539. if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) {
  540. int x_start = first_column + src_rect.left();
  541. for (int row = first_row; row <= last_row; ++row) {
  542. int sr = row - offset.y() + src_rect.top();
  543. if (sr >= source.size().height() || sr < 0) {
  544. dst += dst_skip;
  545. continue;
  546. }
  547. const RGBA32* sl = source.scanline(sr);
  548. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  549. int sx = x - offset.x();
  550. if (sx < source.size().width() && sx >= 0)
  551. dst[x - x_start] = sl[sx];
  552. }
  553. dst += dst_skip;
  554. }
  555. return;
  556. }
  557. ASSERT_NOT_REACHED();
  558. }
  559. void Painter::blit_with_alpha(const IntPoint& position, const Gfx::Bitmap& source, const IntRect& src_rect)
  560. {
  561. ASSERT(source.has_alpha_channel());
  562. IntRect safe_src_rect = src_rect.intersected(source.rect());
  563. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  564. auto clipped_rect = dst_rect.intersected(clip_rect());
  565. if (clipped_rect.is_empty())
  566. return;
  567. const int first_row = clipped_rect.top() - dst_rect.top();
  568. const int last_row = clipped_rect.bottom() - dst_rect.top();
  569. const int first_column = clipped_rect.left() - dst_rect.left();
  570. const int last_column = clipped_rect.right() - dst_rect.left();
  571. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  572. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  573. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  574. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  575. for (int row = first_row; row <= last_row; ++row) {
  576. for (int x = 0; x <= (last_column - first_column); ++x) {
  577. u8 alpha = Color::from_rgba(src[x]).alpha();
  578. if (alpha == 0xff)
  579. dst[x] = src[x];
  580. else if (!alpha)
  581. continue;
  582. else
  583. dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x])).value();
  584. }
  585. dst += dst_skip;
  586. src += src_skip;
  587. }
  588. }
  589. void Painter::blit(const IntPoint& position, const Gfx::Bitmap& source, const IntRect& src_rect, float opacity)
  590. {
  591. if (opacity < 1.0f)
  592. return blit_with_opacity(position, source, src_rect, opacity);
  593. if (source.has_alpha_channel())
  594. return blit_with_alpha(position, source, src_rect);
  595. auto safe_src_rect = src_rect.intersected(source.rect());
  596. ASSERT(source.rect().contains(safe_src_rect));
  597. auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
  598. auto clipped_rect = dst_rect.intersected(clip_rect());
  599. if (clipped_rect.is_empty())
  600. return;
  601. const int first_row = clipped_rect.top() - dst_rect.top();
  602. const int last_row = clipped_rect.bottom() - dst_rect.top();
  603. const int first_column = clipped_rect.left() - dst_rect.left();
  604. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  605. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  606. if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) {
  607. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  608. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  609. for (int row = first_row; row <= last_row; ++row) {
  610. fast_u32_copy(dst, src, clipped_rect.width());
  611. dst += dst_skip;
  612. src += src_skip;
  613. }
  614. return;
  615. }
  616. if (Bitmap::is_indexed(source.format())) {
  617. const u8* src = source.scanline_u8(src_rect.top() + first_row) + src_rect.left() + first_column;
  618. const size_t src_skip = source.pitch();
  619. for (int row = first_row; row <= last_row; ++row) {
  620. for (int i = 0; i < clipped_rect.width(); ++i)
  621. dst[i] = source.palette_color(src[i]).value();
  622. dst += dst_skip;
  623. src += src_skip;
  624. }
  625. return;
  626. }
  627. ASSERT_NOT_REACHED();
  628. }
  629. template<bool has_alpha_channel, typename GetPixel>
  630. ALWAYS_INLINE static void do_draw_integer_scaled_bitmap(Gfx::Bitmap& target, const IntRect& dst_rect, const Gfx::Bitmap& source, int hfactor, int vfactor, GetPixel get_pixel, float opacity)
  631. {
  632. bool has_opacity = opacity != 1.0f;
  633. for (int y = source.rect().top(); y <= source.rect().bottom(); ++y) {
  634. int dst_y = dst_rect.y() + y * vfactor;
  635. for (int x = source.rect().left(); x <= source.rect().right(); ++x) {
  636. auto src_pixel = get_pixel(source, x, y);
  637. if (has_opacity)
  638. src_pixel.set_alpha(src_pixel.alpha() * opacity);
  639. for (int yo = 0; yo < vfactor; ++yo) {
  640. auto* scanline = (Color*)target.scanline(dst_y + yo);
  641. int dst_x = dst_rect.x() + x * hfactor;
  642. for (int xo = 0; xo < hfactor; ++xo) {
  643. if constexpr (has_alpha_channel)
  644. scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel);
  645. else
  646. scanline[dst_x + xo] = src_pixel;
  647. }
  648. }
  649. }
  650. }
  651. }
  652. template<bool has_alpha_channel, typename GetPixel>
  653. ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, const IntRect& dst_rect, const IntRect& clipped_rect, const Gfx::Bitmap& source, const IntRect& src_rect, int hscale, int vscale, GetPixel get_pixel, float opacity)
  654. {
  655. if (dst_rect == clipped_rect && !(dst_rect.width() % src_rect.width()) && !(dst_rect.height() % src_rect.height())) {
  656. int hfactor = dst_rect.width() / src_rect.width();
  657. int vfactor = dst_rect.height() / src_rect.height();
  658. if (hfactor == 2 && vfactor == 2)
  659. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 2, 2, get_pixel, opacity);
  660. if (hfactor == 3 && vfactor == 3)
  661. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 3, 3, get_pixel, opacity);
  662. if (hfactor == 4 && vfactor == 4)
  663. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 4, 4, get_pixel, opacity);
  664. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, hfactor, vfactor, get_pixel, opacity);
  665. }
  666. bool has_opacity = opacity != 1.0f;
  667. for (int y = clipped_rect.top(); y <= clipped_rect.bottom(); ++y) {
  668. auto* scanline = (Color*)target.scanline(y);
  669. for (int x = clipped_rect.left(); x <= clipped_rect.right(); ++x) {
  670. auto scaled_x = ((x - dst_rect.x()) * hscale) >> 16;
  671. auto scaled_y = ((y - dst_rect.y()) * vscale) >> 16;
  672. auto src_pixel = get_pixel(source, scaled_x, scaled_y);
  673. if (has_opacity)
  674. src_pixel.set_alpha(src_pixel.alpha() * opacity);
  675. if constexpr (has_alpha_channel) {
  676. scanline[x] = scanline[x].blend(src_pixel);
  677. } else
  678. scanline[x] = src_pixel;
  679. }
  680. }
  681. }
  682. void Painter::draw_scaled_bitmap(const IntRect& a_dst_rect, const Gfx::Bitmap& source, const IntRect& src_rect, float opacity)
  683. {
  684. auto dst_rect = a_dst_rect;
  685. if (dst_rect.size() == src_rect.size())
  686. return blit(dst_rect.location(), source, src_rect, opacity);
  687. auto safe_src_rect = src_rect.intersected(source.rect());
  688. ASSERT(source.rect().contains(safe_src_rect));
  689. dst_rect.move_by(state().translation);
  690. auto clipped_rect = dst_rect.intersected(clip_rect());
  691. if (clipped_rect.is_empty())
  692. return;
  693. int hscale = (src_rect.width() << 16) / dst_rect.width();
  694. int vscale = (src_rect.height() << 16) / dst_rect.height();
  695. if (source.has_alpha_channel()) {
  696. switch (source.format()) {
  697. case BitmapFormat::RGB32:
  698. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGB32>, opacity);
  699. break;
  700. case BitmapFormat::RGBA32:
  701. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGBA32>, opacity);
  702. break;
  703. case BitmapFormat::Indexed8:
  704. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>, opacity);
  705. break;
  706. case BitmapFormat::Indexed4:
  707. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed4>, opacity);
  708. break;
  709. case BitmapFormat::Indexed2:
  710. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed2>, opacity);
  711. break;
  712. case BitmapFormat::Indexed1:
  713. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed1>, opacity);
  714. break;
  715. default:
  716. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>, opacity);
  717. break;
  718. }
  719. } else {
  720. switch (source.format()) {
  721. case BitmapFormat::RGB32:
  722. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGB32>, opacity);
  723. break;
  724. case BitmapFormat::RGBA32:
  725. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGBA32>, opacity);
  726. break;
  727. case BitmapFormat::Indexed8:
  728. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>, opacity);
  729. break;
  730. default:
  731. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>, opacity);
  732. break;
  733. }
  734. }
  735. }
  736. FLATTEN void Painter::draw_glyph(const IntPoint& point, u32 code_point, Color color)
  737. {
  738. draw_glyph(point, code_point, font(), color);
  739. }
  740. FLATTEN void Painter::draw_glyph(const IntPoint& point, u32 code_point, const Font& font, Color color)
  741. {
  742. draw_bitmap(point, font.glyph_bitmap(code_point), color);
  743. }
  744. void Painter::draw_emoji(const IntPoint& point, const Gfx::Bitmap& emoji, const Font& font)
  745. {
  746. if (!font.is_fixed_width())
  747. blit(point, emoji, emoji.rect());
  748. else {
  749. IntRect dst_rect {
  750. point.x(),
  751. point.y(),
  752. font.glyph_width('x'),
  753. font.glyph_height()
  754. };
  755. draw_scaled_bitmap(dst_rect, emoji, emoji.rect());
  756. }
  757. }
  758. void Painter::draw_glyph_or_emoji(const IntPoint& point, u32 code_point, const Font& font, Color color)
  759. {
  760. if (code_point < (u32)font.glyph_count()) {
  761. // This looks like a regular character.
  762. draw_glyph(point, (size_t)code_point, font, color);
  763. return;
  764. }
  765. // Perhaps it's an emoji?
  766. auto* emoji = Emoji::emoji_for_code_point(code_point);
  767. if (emoji == nullptr) {
  768. #ifdef EMOJI_DEBUG
  769. dbg() << "Failed to find an emoji for code_point " << code_point;
  770. #endif
  771. draw_glyph(point, '?', font, color);
  772. return;
  773. }
  774. draw_emoji(point, *emoji, font);
  775. }
  776. static void apply_elision(Utf8View& final_text, String& elided_text, size_t offset)
  777. {
  778. StringBuilder builder;
  779. builder.append(final_text.substring_view(0, offset).as_string());
  780. builder.append("...");
  781. elided_text = builder.to_string();
  782. final_text = Utf8View { elided_text };
  783. }
  784. static void apply_elision(Utf32View& final_text, Vector<u32>& elided_text, size_t offset)
  785. {
  786. elided_text.append(final_text.code_points(), offset);
  787. elided_text.append('.');
  788. elided_text.append('.');
  789. elided_text.append('.');
  790. final_text = Utf32View { elided_text.data(), elided_text.size() };
  791. }
  792. template<typename TextType>
  793. struct ElidedText {
  794. };
  795. template<>
  796. struct ElidedText<Utf8View> {
  797. typedef String Type;
  798. };
  799. template<>
  800. struct ElidedText<Utf32View> {
  801. typedef Vector<u32> Type;
  802. };
  803. template<typename TextType, typename DrawGlyphFunction>
  804. void draw_text_line(const IntRect& a_rect, const TextType& text, const Font& font, TextAlignment alignment, TextElision elision, DrawGlyphFunction draw_glyph)
  805. {
  806. auto rect = a_rect;
  807. TextType final_text(text);
  808. typename ElidedText<TextType>::Type elided_text;
  809. if (elision == TextElision::Right) {
  810. int text_width = font.width(final_text);
  811. if (font.width(final_text) > rect.width()) {
  812. int glyph_spacing = font.glyph_spacing();
  813. int new_width = font.width("...");
  814. if (new_width < text_width) {
  815. size_t offset = 0;
  816. for (auto code_point : text) {
  817. int glyph_width = font.glyph_or_emoji_width(code_point);
  818. // NOTE: Glyph spacing should not be added after the last glyph on the line,
  819. // but since we are here because the last glyph does not actually fit on the line,
  820. // we don't have to worry about spacing.
  821. int width_with_this_glyph_included = new_width + glyph_width + glyph_spacing;
  822. if (width_with_this_glyph_included > rect.width())
  823. break;
  824. new_width += glyph_width + glyph_spacing;
  825. offset++;
  826. }
  827. apply_elision(final_text, elided_text, offset);
  828. }
  829. }
  830. }
  831. switch (alignment) {
  832. case TextAlignment::TopLeft:
  833. case TextAlignment::CenterLeft:
  834. break;
  835. case TextAlignment::TopRight:
  836. case TextAlignment::CenterRight:
  837. case TextAlignment::BottomRight:
  838. rect.set_x(rect.right() - font.width(final_text));
  839. break;
  840. case TextAlignment::Center: {
  841. auto shrunken_rect = rect;
  842. shrunken_rect.set_width(font.width(final_text));
  843. shrunken_rect.center_within(rect);
  844. rect = shrunken_rect;
  845. break;
  846. }
  847. default:
  848. ASSERT_NOT_REACHED();
  849. }
  850. if (is_vertically_centered_text_alignment(alignment)) {
  851. int distance_from_baseline_to_bottom = (font.glyph_height() - 1) - font.baseline();
  852. rect.move_by(0, distance_from_baseline_to_bottom / 2);
  853. }
  854. auto point = rect.location();
  855. int space_width = font.glyph_width(' ') + font.glyph_spacing();
  856. for (u32 code_point : final_text) {
  857. if (code_point == ' ') {
  858. point.move_by(space_width, 0);
  859. continue;
  860. }
  861. IntSize glyph_size(font.glyph_or_emoji_width(code_point) + font.glyph_spacing(), font.glyph_height());
  862. draw_glyph({ point, glyph_size }, code_point);
  863. point.move_by(glyph_size.width(), 0);
  864. }
  865. }
  866. static inline size_t draw_text_iterator_offset(const Utf8View& text, const Utf8View::Iterator& it)
  867. {
  868. return text.byte_offset_of(it);
  869. }
  870. static inline size_t draw_text_iterator_offset(const Utf32View& text, const Utf32View::Iterator& it)
  871. {
  872. return it - text.begin();
  873. }
  874. static inline size_t draw_text_get_length(const Utf8View& text)
  875. {
  876. return text.byte_length();
  877. }
  878. static inline size_t draw_text_get_length(const Utf32View& text)
  879. {
  880. return text.length();
  881. }
  882. template<typename TextType, typename DrawGlyphFunction>
  883. void do_draw_text(const IntRect& rect, const TextType& text, const Font& font, TextAlignment alignment, TextElision elision, DrawGlyphFunction draw_glyph)
  884. {
  885. Vector<TextType, 32> lines;
  886. size_t start_of_current_line = 0;
  887. for (auto it = text.begin(); it != text.end(); ++it) {
  888. u32 code_point = *it;
  889. if (code_point == '\n') {
  890. auto offset = draw_text_iterator_offset(text, it);
  891. TextType line = text.substring_view(start_of_current_line, offset - start_of_current_line);
  892. lines.append(line);
  893. start_of_current_line = offset + 1;
  894. }
  895. }
  896. if (start_of_current_line != draw_text_get_length(text)) {
  897. TextType line = text.substring_view(start_of_current_line, draw_text_get_length(text) - start_of_current_line);
  898. lines.append(line);
  899. }
  900. static const int line_spacing = 4;
  901. int line_height = font.glyph_height() + line_spacing;
  902. IntRect bounding_rect { 0, 0, 0, (static_cast<int>(lines.size()) * line_height) - line_spacing };
  903. for (auto& line : lines) {
  904. auto line_width = font.width(line);
  905. if (line_width > bounding_rect.width())
  906. bounding_rect.set_width(line_width);
  907. }
  908. switch (alignment) {
  909. case TextAlignment::TopLeft:
  910. bounding_rect.set_location(rect.location());
  911. break;
  912. case TextAlignment::TopRight:
  913. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.y() });
  914. break;
  915. case TextAlignment::CenterLeft:
  916. bounding_rect.set_location({ rect.x(), rect.center().y() - (bounding_rect.height() / 2) });
  917. break;
  918. case TextAlignment::CenterRight:
  919. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.center().y() - (bounding_rect.height() / 2) });
  920. break;
  921. case TextAlignment::Center:
  922. bounding_rect.center_within(rect);
  923. break;
  924. case TextAlignment::BottomRight:
  925. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), (rect.bottom() + 1) - bounding_rect.height() });
  926. break;
  927. default:
  928. ASSERT_NOT_REACHED();
  929. }
  930. for (size_t i = 0; i < lines.size(); ++i) {
  931. auto& line = lines[i];
  932. IntRect line_rect { bounding_rect.x(), bounding_rect.y() + static_cast<int>(i) * line_height, bounding_rect.width(), line_height };
  933. line_rect.intersect(rect);
  934. draw_text_line(line_rect, line, font, alignment, elision, draw_glyph);
  935. }
  936. }
  937. void Painter::draw_text(const IntRect& rect, const StringView& text, TextAlignment alignment, Color color, TextElision elision)
  938. {
  939. draw_text(rect, text, font(), alignment, color, elision);
  940. }
  941. void Painter::draw_text(const IntRect& rect, const Utf32View& text, TextAlignment alignment, Color color, TextElision elision)
  942. {
  943. draw_text(rect, text, font(), alignment, color, elision);
  944. }
  945. void Painter::draw_text(const IntRect& rect, const StringView& raw_text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
  946. {
  947. Utf8View text { raw_text };
  948. do_draw_text(rect, Utf8View(text), font, alignment, elision, [&](const IntRect& r, u32 code_point) {
  949. draw_glyph_or_emoji(r.location(), code_point, font, color);
  950. });
  951. }
  952. void Painter::draw_text(const IntRect& rect, const Utf32View& text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
  953. {
  954. do_draw_text(rect, text, font, alignment, elision, [&](const IntRect& r, u32 code_point) {
  955. draw_glyph_or_emoji(r.location(), code_point, font, color);
  956. });
  957. }
  958. void Painter::draw_text(Function<void(const IntRect&, u32)> draw_one_glyph, const IntRect& rect, const StringView& raw_text, const Font& font, TextAlignment alignment, TextElision elision)
  959. {
  960. Utf8View text { raw_text };
  961. do_draw_text(rect, text, font, alignment, elision, [&](const IntRect& r, u32 code_point) {
  962. draw_one_glyph(r, code_point);
  963. });
  964. }
  965. void Painter::draw_text(Function<void(const IntRect&, u32)> draw_one_glyph, const IntRect& rect, const Utf8View& text, const Font& font, TextAlignment alignment, TextElision elision)
  966. {
  967. do_draw_text(rect, text, font, alignment, elision, [&](const IntRect& r, u32 code_point) {
  968. draw_one_glyph(r, code_point);
  969. });
  970. }
  971. void Painter::draw_text(Function<void(const IntRect&, u32)> draw_one_glyph, const IntRect& rect, const Utf32View& text, const Font& font, TextAlignment alignment, TextElision elision)
  972. {
  973. do_draw_text(rect, text, font, alignment, elision, [&](const IntRect& r, u32 code_point) {
  974. draw_one_glyph(r, code_point);
  975. });
  976. }
  977. void Painter::set_pixel(const IntPoint& p, Color color)
  978. {
  979. auto point = p;
  980. point.move_by(state().translation);
  981. if (!clip_rect().contains(point))
  982. return;
  983. m_target->scanline(point.y())[point.x()] = color.value();
  984. }
  985. ALWAYS_INLINE void Painter::set_pixel_with_draw_op(u32& pixel, const Color& color)
  986. {
  987. switch (draw_op()) {
  988. case DrawOp::Copy:
  989. pixel = color.value();
  990. break;
  991. case DrawOp::Xor:
  992. pixel = color.xored(Color::from_rgba(pixel)).value();
  993. break;
  994. case DrawOp::Invert:
  995. pixel = Color::from_rgba(pixel).inverted().value();
  996. break;
  997. }
  998. }
  999. ALWAYS_INLINE void Painter::fill_scanline_with_draw_op(int y, int x, int width, const Color& color)
  1000. {
  1001. switch (draw_op()) {
  1002. case DrawOp::Copy:
  1003. fast_u32_fill(m_target->scanline(y) + x, color.value(), width);
  1004. break;
  1005. case DrawOp::Xor: {
  1006. auto* pixel = m_target->scanline(y) + x;
  1007. auto* end = pixel + width;
  1008. while (pixel < end) {
  1009. *pixel = Color::from_rgba(*pixel).xored(color).value();
  1010. pixel++;
  1011. }
  1012. break;
  1013. }
  1014. case DrawOp::Invert: {
  1015. auto* pixel = m_target->scanline(y) + x;
  1016. auto* end = pixel + width;
  1017. while (pixel < end) {
  1018. *pixel = Color::from_rgba(*pixel).inverted().value();
  1019. pixel++;
  1020. }
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. void Painter::draw_pixel(const IntPoint& position, Color color, int thickness)
  1026. {
  1027. ASSERT(draw_op() == DrawOp::Copy);
  1028. if (thickness == 1)
  1029. return set_pixel_with_draw_op(m_target->scanline(position.y())[position.x()], color);
  1030. IntRect rect { position.translated(-(thickness / 2), -(thickness / 2)), { thickness, thickness } };
  1031. fill_rect(rect.translated(-state().translation), color);
  1032. }
  1033. void Painter::draw_line(const IntPoint& p1, const IntPoint& p2, Color color, int thickness, LineStyle style)
  1034. {
  1035. if (color.alpha() == 0)
  1036. return;
  1037. auto clip_rect = this->clip_rect();
  1038. auto point1 = p1;
  1039. point1.move_by(state().translation);
  1040. auto point2 = p2;
  1041. point2.move_by(state().translation);
  1042. // Special case: vertical line.
  1043. if (point1.x() == point2.x()) {
  1044. const int x = point1.x();
  1045. if (x < clip_rect.left() || x > clip_rect.right())
  1046. return;
  1047. if (point1.y() > point2.y())
  1048. swap(point1, point2);
  1049. if (point1.y() > clip_rect.bottom())
  1050. return;
  1051. if (point2.y() < clip_rect.top())
  1052. return;
  1053. int min_y = max(point1.y(), clip_rect.top());
  1054. int max_y = min(point2.y(), clip_rect.bottom());
  1055. if (style == LineStyle::Dotted) {
  1056. for (int y = min_y; y <= max_y; y += thickness * 2)
  1057. draw_pixel({ x, y }, color, thickness);
  1058. } else if (style == LineStyle::Dashed) {
  1059. for (int y = min_y; y <= max_y; y += thickness * 6) {
  1060. draw_pixel({ x, y }, color, thickness);
  1061. draw_pixel({ x, min(y + thickness, max_y) }, color, thickness);
  1062. draw_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness);
  1063. }
  1064. } else {
  1065. for (int y = min_y; y <= max_y; ++y)
  1066. draw_pixel({ x, y }, color, thickness);
  1067. }
  1068. return;
  1069. }
  1070. // Special case: horizontal line.
  1071. if (point1.y() == point2.y()) {
  1072. const int y = point1.y();
  1073. if (y < clip_rect.top() || y > clip_rect.bottom())
  1074. return;
  1075. if (point1.x() > point2.x())
  1076. swap(point1, point2);
  1077. if (point1.x() > clip_rect.right())
  1078. return;
  1079. if (point2.x() < clip_rect.left())
  1080. return;
  1081. int min_x = max(point1.x(), clip_rect.left());
  1082. int max_x = min(point2.x(), clip_rect.right());
  1083. if (style == LineStyle::Dotted) {
  1084. for (int x = min_x; x <= max_x; x += thickness * 2)
  1085. draw_pixel({ x, y }, color, thickness);
  1086. } else if (style == LineStyle::Dashed) {
  1087. for (int x = min_x; x <= max_x; x += thickness * 6) {
  1088. draw_pixel({ x, y }, color, thickness);
  1089. draw_pixel({ min(x + thickness, max_x), y }, color, thickness);
  1090. draw_pixel({ min(x + thickness * 2, max_x), y }, color, thickness);
  1091. }
  1092. } else {
  1093. for (int x = min_x; x <= max_x; ++x)
  1094. draw_pixel({ x, y }, color, thickness);
  1095. }
  1096. return;
  1097. }
  1098. // FIXME: Implement dotted/dashed diagonal lines.
  1099. ASSERT(style == LineStyle::Solid);
  1100. const double adx = abs(point2.x() - point1.x());
  1101. const double ady = abs(point2.y() - point1.y());
  1102. if (adx > ady) {
  1103. if (point1.x() > point2.x())
  1104. swap(point1, point2);
  1105. } else {
  1106. if (point1.y() > point2.y())
  1107. swap(point1, point2);
  1108. }
  1109. // FIXME: Implement clipping below.
  1110. const double dx = point2.x() - point1.x();
  1111. const double dy = point2.y() - point1.y();
  1112. double error = 0;
  1113. if (dx > dy) {
  1114. const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  1115. const double delta_error = fabs(dy / dx);
  1116. int y = point1.y();
  1117. for (int x = point1.x(); x <= point2.x(); ++x) {
  1118. if (clip_rect.contains(x, y))
  1119. draw_pixel({ x, y }, color, thickness);
  1120. error += delta_error;
  1121. if (error >= 0.5) {
  1122. y = (double)y + y_step;
  1123. error -= 1.0;
  1124. }
  1125. }
  1126. } else {
  1127. const double x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1);
  1128. const double delta_error = fabs(dx / dy);
  1129. int x = point1.x();
  1130. for (int y = point1.y(); y <= point2.y(); ++y) {
  1131. if (clip_rect.contains(x, y))
  1132. draw_pixel({ x, y }, color, thickness);
  1133. error += delta_error;
  1134. if (error >= 0.5) {
  1135. x = (double)x + x_step;
  1136. error -= 1.0;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. static void split_quadratic_bezier_curve(const FloatPoint& original_control, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>& callback)
  1142. {
  1143. auto po1_midpoint = original_control + p1;
  1144. po1_midpoint /= 2;
  1145. auto po2_midpoint = original_control + p2;
  1146. po2_midpoint /= 2;
  1147. auto new_segment = po1_midpoint + po2_midpoint;
  1148. new_segment /= 2;
  1149. Painter::for_each_line_segment_on_bezier_curve(po1_midpoint, p1, new_segment, callback);
  1150. Painter::for_each_line_segment_on_bezier_curve(po2_midpoint, new_segment, p2, callback);
  1151. }
  1152. static bool can_approximate_bezier_curve(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& control)
  1153. {
  1154. constexpr static int tolerance = 15;
  1155. auto p1x = 3 * control.x() - 2 * p1.x() - p2.x();
  1156. auto p1y = 3 * control.y() - 2 * p1.y() - p2.y();
  1157. auto p2x = 3 * control.x() - 2 * p2.x() - p1.x();
  1158. auto p2y = 3 * control.y() - 2 * p2.y() - p1.y();
  1159. p1x = p1x * p1x;
  1160. p1y = p1y * p1y;
  1161. p2x = p2x * p2x;
  1162. p2y = p2y * p2y;
  1163. return max(p1x, p2x) + max(p1y, p2y) <= tolerance;
  1164. }
  1165. void Painter::for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>& callback)
  1166. {
  1167. if (can_approximate_bezier_curve(p1, p2, control_point)) {
  1168. callback(p1, p2);
  1169. } else {
  1170. split_quadratic_bezier_curve(control_point, p1, p2, callback);
  1171. }
  1172. }
  1173. void Painter::for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>&& callback)
  1174. {
  1175. for_each_line_segment_on_bezier_curve(control_point, p1, p2, callback);
  1176. }
  1177. static void split_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>& callback)
  1178. {
  1179. auto half_theta_delta = theta_delta / 2;
  1180. auto theta_mid = theta_1 + half_theta_delta;
  1181. auto xc = cosf(x_axis_rotation);
  1182. auto xs = sinf(x_axis_rotation);
  1183. auto tc = cosf(theta_1 + half_theta_delta);
  1184. auto ts = sinf(theta_1 + half_theta_delta);
  1185. auto x2 = xc * radii.x() * tc - xs * radii.y() * ts + center.x();
  1186. auto y2 = xs * radii.x() * tc + xc * radii.y() * ts + center.y();
  1187. FloatPoint mid_point = { x2, y2 };
  1188. Painter::for_each_line_segment_on_elliptical_arc(p1, mid_point, center, radii, x_axis_rotation, theta_1, half_theta_delta, callback);
  1189. Painter::for_each_line_segment_on_elliptical_arc(mid_point, p2, center, radii, x_axis_rotation, theta_mid, half_theta_delta, callback);
  1190. }
  1191. static bool can_approximate_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta)
  1192. {
  1193. constexpr static float tolerance = 1;
  1194. auto half_theta_delta = theta_delta / 2.0f;
  1195. auto xc = cosf(x_axis_rotation);
  1196. auto xs = sinf(x_axis_rotation);
  1197. auto tc = cosf(theta_1 + half_theta_delta);
  1198. auto ts = sinf(theta_1 + half_theta_delta);
  1199. auto x2 = xc * radii.x() * tc - xs * radii.y() * ts + center.x();
  1200. auto y2 = xs * radii.x() * tc + xc * radii.y() * ts + center.y();
  1201. auto ellipse_mid_point = FloatPoint { x2, y2 };
  1202. auto line_mid_point = p1 + (p2 - p1) / 2.0f;
  1203. return ellipse_mid_point.distance_from(line_mid_point) < tolerance;
  1204. }
  1205. void Painter::draw_quadratic_bezier_curve(const IntPoint& control_point, const IntPoint& p1, const IntPoint& p2, Color color, int thickness, LineStyle style)
  1206. {
  1207. for_each_line_segment_on_bezier_curve(FloatPoint(control_point), FloatPoint(p1), FloatPoint(p2), [&](const FloatPoint& fp1, const FloatPoint& fp2) {
  1208. draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
  1209. });
  1210. }
  1211. void Painter::for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>& callback)
  1212. {
  1213. if (can_approximate_elliptical_arc(p1, p2, center, radii, x_axis_rotation, theta_1, theta_delta)) {
  1214. callback(p1, p2);
  1215. } else {
  1216. split_elliptical_arc(p1, p2, center, radii, x_axis_rotation, theta_1, theta_delta, callback);
  1217. }
  1218. }
  1219. void Painter::for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>&& callback)
  1220. {
  1221. for_each_line_segment_on_elliptical_arc(p1, p2, center, radii, x_axis_rotation, theta_1, theta_delta, callback);
  1222. }
  1223. void Painter::draw_elliptical_arc(const IntPoint& p1, const IntPoint& p2, const IntPoint& center, const FloatPoint& radii, float x_axis_rotation, float theta_1, float theta_delta, Color color, int thickness, LineStyle style)
  1224. {
  1225. for_each_line_segment_on_elliptical_arc(FloatPoint(p1), FloatPoint(p2), FloatPoint(center), radii, x_axis_rotation, theta_1, theta_delta, [&](const FloatPoint& fp1, const FloatPoint& fp2) {
  1226. draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
  1227. });
  1228. }
  1229. void Painter::add_clip_rect(const IntRect& rect)
  1230. {
  1231. state().clip_rect.intersect(rect.translated(translation()));
  1232. state().clip_rect.intersect(m_target->rect());
  1233. }
  1234. void Painter::clear_clip_rect()
  1235. {
  1236. state().clip_rect = m_clip_origin;
  1237. }
  1238. PainterStateSaver::PainterStateSaver(Painter& painter)
  1239. : m_painter(painter)
  1240. {
  1241. m_painter.save();
  1242. }
  1243. PainterStateSaver::~PainterStateSaver()
  1244. {
  1245. m_painter.restore();
  1246. }
  1247. void Painter::stroke_path(const Path& path, Color color, int thickness)
  1248. {
  1249. FloatPoint cursor;
  1250. for (auto& segment : path.segments()) {
  1251. switch (segment.type()) {
  1252. case Segment::Type::Invalid:
  1253. ASSERT_NOT_REACHED();
  1254. break;
  1255. case Segment::Type::MoveTo:
  1256. cursor = segment.point();
  1257. break;
  1258. case Segment::Type::LineTo:
  1259. draw_line(cursor.to_type<int>(), segment.point().to_type<int>(), color, thickness);
  1260. cursor = segment.point();
  1261. break;
  1262. case Segment::Type::QuadraticBezierCurveTo: {
  1263. auto& through = static_cast<const QuadraticBezierCurveSegment&>(segment).through();
  1264. draw_quadratic_bezier_curve(through.to_type<int>(), cursor.to_type<int>(), segment.point().to_type<int>(), color, thickness);
  1265. cursor = segment.point();
  1266. break;
  1267. }
  1268. case Segment::Type::EllipticalArcTo:
  1269. auto& arc = static_cast<const EllipticalArcSegment&>(segment);
  1270. 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);
  1271. cursor = segment.point();
  1272. break;
  1273. }
  1274. }
  1275. }
  1276. //#define FILL_PATH_DEBUG
  1277. [[maybe_unused]] static void approximately_place_on_int_grid(FloatPoint ffrom, FloatPoint fto, IntPoint& from, IntPoint& to, Optional<IntPoint> previous_to)
  1278. {
  1279. auto diffs = fto - ffrom;
  1280. // Truncate all first (round down).
  1281. from = ffrom.to_type<int>();
  1282. to = fto.to_type<int>();
  1283. // There are 16 possible configurations, by deciding to round each
  1284. // coord up or down (and there are four coords, from.x from.y to.x to.y)
  1285. // we will simply choose one which most closely matches the correct slope
  1286. // with the following heuristic:
  1287. // - if the x diff is positive or zero (that is, a right-to-left slant), round 'from.x' up and 'to.x' down.
  1288. // - if the x diff is negative (that is, a left-to-right slant), round 'from.x' down and 'to.x' up.
  1289. // Note that we do not need to touch the 'y' attribute, as that is our scanline.
  1290. if (diffs.x() >= 0) {
  1291. from.set_x(from.x() + 1);
  1292. } else {
  1293. to.set_x(to.x() + 1);
  1294. }
  1295. if (previous_to.has_value() && from.x() != previous_to.value().x()) // The points have to line up, since we're using these lines to fill a shape.
  1296. from.set_x(previous_to.value().x());
  1297. }
  1298. void Painter::fill_path(Path& path, Color color, WindingRule winding_rule)
  1299. {
  1300. const auto& segments = path.split_lines();
  1301. if (segments.size() == 0)
  1302. return;
  1303. Vector<Path::SplitLineSegment> active_list;
  1304. active_list.ensure_capacity(segments.size());
  1305. // first, grab the segments for the very first scanline
  1306. int first_y = path.bounding_box().bottom_right().y() + 1;
  1307. int last_y = path.bounding_box().top_left().y() - 1;
  1308. float scanline = first_y;
  1309. size_t last_active_segment { 0 };
  1310. for (auto& segment : segments) {
  1311. if (segment.maximum_y != scanline)
  1312. break;
  1313. active_list.append(segment);
  1314. ++last_active_segment;
  1315. }
  1316. auto is_inside_shape = [winding_rule](int winding_number) {
  1317. if (winding_rule == WindingRule::Nonzero)
  1318. return winding_number != 0;
  1319. if (winding_rule == WindingRule::EvenOdd)
  1320. return winding_number % 2 == 0;
  1321. ASSERT_NOT_REACHED();
  1322. };
  1323. auto increment_winding = [winding_rule](int& winding_number, const IntPoint& from, const IntPoint& to) {
  1324. if (winding_rule == WindingRule::EvenOdd) {
  1325. ++winding_number;
  1326. return;
  1327. }
  1328. if (winding_rule == WindingRule::Nonzero) {
  1329. if (from.dy_relative_to(to) < 0)
  1330. ++winding_number;
  1331. else
  1332. --winding_number;
  1333. return;
  1334. }
  1335. ASSERT_NOT_REACHED();
  1336. };
  1337. while (scanline >= last_y) {
  1338. Optional<IntPoint> previous_to;
  1339. if (active_list.size()) {
  1340. // sort the active list by 'x' from right to left
  1341. quick_sort(active_list, [](const auto& line0, const auto& line1) {
  1342. return line1.x < line0.x;
  1343. });
  1344. #ifdef FILL_PATH_DEBUG
  1345. if ((int)scanline % 10 == 0) {
  1346. draw_text(IntRect(active_list.last().x - 20, scanline, 20, 10), String::format("%d", (int)scanline));
  1347. }
  1348. #endif
  1349. if (active_list.size() > 1) {
  1350. auto winding_number { 0 };
  1351. for (size_t i = 1; i < active_list.size(); ++i) {
  1352. auto& previous = active_list[i - 1];
  1353. auto& current = active_list[i];
  1354. IntPoint from, to;
  1355. IntPoint truncated_from { previous.x, scanline };
  1356. IntPoint truncated_to { current.x, scanline };
  1357. approximately_place_on_int_grid({ previous.x, scanline }, { current.x, scanline }, from, to, previous_to);
  1358. if (is_inside_shape(winding_number)) {
  1359. // The points between this segment and the previous are
  1360. // inside the shape
  1361. #ifdef FILL_PATH_DEBUG
  1362. dbg() << "y=" << scanline << ": " << winding_number << " at " << i << ": " << from << " -- " << to;
  1363. #endif
  1364. draw_line(from, to, color, 1);
  1365. }
  1366. auto is_passing_through_maxima = scanline == previous.maximum_y
  1367. || scanline == previous.minimum_y
  1368. || scanline == current.maximum_y
  1369. || scanline == current.minimum_y;
  1370. auto is_passing_through_vertex = false;
  1371. if (is_passing_through_maxima) {
  1372. is_passing_through_vertex = previous.x == current.x;
  1373. }
  1374. if (!is_passing_through_vertex || previous.inverse_slope * current.inverse_slope < 0)
  1375. increment_winding(winding_number, truncated_from, truncated_to);
  1376. // update the x coord
  1377. active_list[i - 1].x -= active_list[i - 1].inverse_slope;
  1378. }
  1379. active_list.last().x -= active_list.last().inverse_slope;
  1380. } else {
  1381. auto point = IntPoint(active_list[0].x, scanline);
  1382. draw_line(point, point, color);
  1383. // update the x coord
  1384. active_list.first().x -= active_list.first().inverse_slope;
  1385. }
  1386. }
  1387. --scanline;
  1388. // remove any edge that goes out of bound from the active list
  1389. for (size_t i = 0, count = active_list.size(); i < count; ++i) {
  1390. if (scanline <= active_list[i].minimum_y) {
  1391. active_list.remove(i);
  1392. --count;
  1393. --i;
  1394. }
  1395. }
  1396. for (size_t j = last_active_segment; j < segments.size(); ++j, ++last_active_segment) {
  1397. auto& segment = segments[j];
  1398. if (segment.maximum_y < scanline)
  1399. break;
  1400. if (segment.minimum_y >= scanline)
  1401. continue;
  1402. active_list.append(segment);
  1403. }
  1404. }
  1405. #ifdef FILL_PATH_DEBUG
  1406. size_t i { 0 };
  1407. for (auto& segment : segments) {
  1408. draw_line(Point<int>(segment.from), Point<int>(segment.to), Color::from_hsv(i++ * 360.0 / segments.size(), 1.0, 1.0), 1);
  1409. }
  1410. #endif
  1411. }
  1412. void Painter::blit_disabled(const IntPoint& location, const Gfx::Bitmap& bitmap, const IntRect& rect, const Palette& palette)
  1413. {
  1414. auto bright_color = palette.threed_highlight();
  1415. auto dark_color = palette.threed_shadow1();
  1416. blit_filtered(location.translated(1, 1), bitmap, rect, [&](auto) {
  1417. return bright_color;
  1418. });
  1419. blit_filtered(location, bitmap, rect, [&](Color src) {
  1420. int gray = src.to_grayscale().red();
  1421. if (gray > 160)
  1422. return bright_color;
  1423. return dark_color;
  1424. });
  1425. }
  1426. }