Painter.cpp 65 KB

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