Painter.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 "Emoji.h"
  28. #include "Font.h"
  29. #include "Bitmap.h"
  30. #include <AK/Assertions.h>
  31. #include <AK/StdLibExtras.h>
  32. #include <AK/StringBuilder.h>
  33. #include <AK/Utf8View.h>
  34. #include <LibGfx/CharacterBitmap.h>
  35. #include <math.h>
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #if defined(__GNUC__) && !defined(__clang__)
  39. #pragma GCC optimize("O3")
  40. #endif
  41. #ifndef ALWAYS_INLINE
  42. #if __has_attribute(always_inline)
  43. #define ALWAYS_INLINE __attribute__((always_inline))
  44. #else
  45. #define ALWAYS_INLINE inline
  46. #endif
  47. #endif
  48. namespace Gfx {
  49. template<BitmapFormat format = BitmapFormat::Invalid>
  50. static 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.bits(y)[x]);
  54. if constexpr (format == BitmapFormat::RGB32)
  55. return Color::from_rgb(bitmap.scanline(y)[x]);
  56. if constexpr (format == BitmapFormat::RGBA32)
  57. return Color::from_rgba(bitmap.scanline(y)[x]);
  58. return bitmap.get_pixel(x, y);
  59. }
  60. Painter::Painter(Gfx::Bitmap& bitmap)
  61. : m_target(bitmap)
  62. {
  63. m_state_stack.append(State());
  64. state().font = &Font::default_font();
  65. state().clip_rect = { { 0, 0 }, bitmap.size() };
  66. m_clip_origin = state().clip_rect;
  67. }
  68. Painter::~Painter()
  69. {
  70. }
  71. void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color)
  72. {
  73. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  74. if (rect.is_empty())
  75. return;
  76. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  77. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  78. for (int i = rect.height() - 1; i >= 0; --i) {
  79. for (int j = 0; j < rect.width(); ++j)
  80. set_pixel_with_draw_op(dst[j], color);
  81. dst += dst_skip;
  82. }
  83. }
  84. void Painter::clear_rect(const Rect& a_rect, Color color)
  85. {
  86. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  87. if (rect.is_empty())
  88. return;
  89. ASSERT(m_target->rect().contains(rect));
  90. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  91. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  92. for (int i = rect.height() - 1; i >= 0; --i) {
  93. fast_u32_fill(dst, color.value(), rect.width());
  94. dst += dst_skip;
  95. }
  96. }
  97. void Painter::fill_rect(const Rect& a_rect, Color color)
  98. {
  99. if (color.alpha() == 0)
  100. return;
  101. if (draw_op() != DrawOp::Copy) {
  102. fill_rect_with_draw_op(a_rect, color);
  103. return;
  104. }
  105. if (color.alpha() == 0xff) {
  106. clear_rect(a_rect, color);
  107. return;
  108. }
  109. auto rect = a_rect.translated(translation()).intersected(clip_rect());
  110. if (rect.is_empty())
  111. return;
  112. ASSERT(m_target->rect().contains(rect));
  113. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  114. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  115. for (int i = rect.height() - 1; i >= 0; --i) {
  116. for (int j = 0; j < rect.width(); ++j)
  117. dst[j] = Color::from_rgba(dst[j]).blend(color).value();
  118. dst += dst_skip;
  119. }
  120. }
  121. void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end)
  122. {
  123. #ifdef NO_FPU
  124. return fill_rect(a_rect, gradient_start);
  125. #endif
  126. auto rect = a_rect.translated(translation());
  127. auto clipped_rect = Rect::intersection(rect, clip_rect());
  128. if (clipped_rect.is_empty())
  129. return;
  130. int x_offset = clipped_rect.x() - rect.x();
  131. RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
  132. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  133. float increment = (1.0 / ((rect.width()) / 255.0));
  134. int r2 = gradient_start.red();
  135. int g2 = gradient_start.green();
  136. int b2 = gradient_start.blue();
  137. int r1 = gradient_end.red();
  138. int g1 = gradient_end.green();
  139. int b1 = gradient_end.blue();
  140. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  141. float c = x_offset * increment;
  142. for (int j = 0; j < clipped_rect.width(); ++j) {
  143. dst[j] = Color(
  144. r1 / 255.0 * c + r2 / 255.0 * (255 - c),
  145. g1 / 255.0 * c + g2 / 255.0 * (255 - c),
  146. b1 / 255.0 * c + b2 / 255.0 * (255 - c))
  147. .value();
  148. c += increment;
  149. }
  150. dst += dst_skip;
  151. }
  152. }
  153. void Painter::draw_ellipse_intersecting(const Rect& rect, Color color, int thickness)
  154. {
  155. constexpr int number_samples = 100; // FIXME: dynamically work out the number of samples based upon the rect size
  156. double increment = M_PI / number_samples;
  157. auto ellipse_x = [&](double theta) -> int {
  158. return (cos(theta) * rect.width() / sqrt(2)) + rect.center().x();
  159. };
  160. auto ellipse_y = [&](double theta) -> int {
  161. return (sin(theta) * rect.height() / sqrt(2)) + rect.center().y();
  162. };
  163. for (float theta = 0; theta < 2 * M_PI; theta += increment) {
  164. draw_line({ ellipse_x(theta), ellipse_y(theta) }, { ellipse_x(theta + increment), ellipse_y(theta + increment) }, color, thickness);
  165. }
  166. }
  167. void Painter::draw_rect(const Rect& a_rect, Color color, bool rough)
  168. {
  169. Rect rect = a_rect.translated(translation());
  170. auto clipped_rect = rect.intersected(clip_rect());
  171. if (clipped_rect.is_empty())
  172. return;
  173. int min_y = clipped_rect.top();
  174. int max_y = clipped_rect.bottom();
  175. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  176. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  177. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  178. fast_u32_fill(m_target->scanline(rect.top()) + start_x, color.value(), width);
  179. ++min_y;
  180. }
  181. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  182. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  183. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  184. fast_u32_fill(m_target->scanline(rect.bottom()) + start_x, color.value(), width);
  185. --max_y;
  186. }
  187. bool draw_left_side = rect.left() >= clipped_rect.left();
  188. bool draw_right_side = rect.right() == clipped_rect.right();
  189. if (draw_left_side && draw_right_side) {
  190. // Specialized loop when drawing both sides.
  191. for (int y = min_y; y <= max_y; ++y) {
  192. auto* bits = m_target->scanline(y);
  193. bits[rect.left()] = color.value();
  194. bits[rect.right()] = color.value();
  195. }
  196. } else {
  197. for (int y = min_y; y <= max_y; ++y) {
  198. auto* bits = m_target->scanline(y);
  199. if (draw_left_side)
  200. bits[rect.left()] = color.value();
  201. if (draw_right_side)
  202. bits[rect.right()] = color.value();
  203. }
  204. }
  205. }
  206. void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
  207. {
  208. auto rect = Rect(p, bitmap.size()).translated(translation());
  209. auto clipped_rect = rect.intersected(clip_rect());
  210. if (clipped_rect.is_empty())
  211. return;
  212. const int first_row = clipped_rect.top() - rect.top();
  213. const int last_row = clipped_rect.bottom() - rect.top();
  214. const int first_column = clipped_rect.left() - rect.left();
  215. const int last_column = clipped_rect.right() - rect.left();
  216. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  217. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  218. const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  219. const size_t bitmap_skip = bitmap.width();
  220. for (int row = first_row; row <= last_row; ++row) {
  221. for (int j = 0; j <= (last_column - first_column); ++j) {
  222. char fc = bitmap_row[j];
  223. if (fc == '#')
  224. dst[j] = color.value();
  225. }
  226. bitmap_row += bitmap_skip;
  227. dst += dst_skip;
  228. }
  229. }
  230. void Painter::draw_bitmap(const Point& p, const GlyphBitmap& bitmap, Color color)
  231. {
  232. auto dst_rect = Rect(p, bitmap.size()).translated(translation());
  233. auto clipped_rect = dst_rect.intersected(clip_rect());
  234. if (clipped_rect.is_empty())
  235. return;
  236. const int first_row = clipped_rect.top() - dst_rect.top();
  237. const int last_row = clipped_rect.bottom() - dst_rect.top();
  238. const int first_column = clipped_rect.left() - dst_rect.left();
  239. const int last_column = clipped_rect.right() - dst_rect.left();
  240. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  241. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  242. for (int row = first_row; row <= last_row; ++row) {
  243. for (int j = 0; j <= (last_column - first_column); ++j) {
  244. if (bitmap.bit_at(j + first_column, row))
  245. dst[j] = color.value();
  246. }
  247. dst += dst_skip;
  248. }
  249. }
  250. void Painter::blit_scaled(const Rect& dst_rect_raw, const Gfx::Bitmap& source, const Rect& src_rect, float hscale, float vscale)
  251. {
  252. auto dst_rect = Rect(dst_rect_raw.location(), dst_rect_raw.size()).translated(translation());
  253. auto clipped_rect = dst_rect.intersected(clip_rect());
  254. if (clipped_rect.is_empty())
  255. return;
  256. const int first_row = (clipped_rect.top() - dst_rect.top());
  257. const int last_row = (clipped_rect.bottom() - dst_rect.top());
  258. const int first_column = (clipped_rect.left() - dst_rect.left());
  259. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  260. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  261. int x_start = first_column + src_rect.left();
  262. for (int row = first_row; row <= last_row; ++row) {
  263. int sr = (row + src_rect.top()) * vscale;
  264. if (sr >= source.size().height() || sr < 0) {
  265. dst += dst_skip;
  266. continue;
  267. }
  268. const RGBA32* sl = source.scanline(sr);
  269. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  270. int sx = x * hscale;
  271. if (sx < source.size().width() && sx >= 0)
  272. dst[x - x_start] = sl[sx];
  273. }
  274. dst += dst_skip;
  275. }
  276. return;
  277. }
  278. void Painter::blit_with_opacity(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect, float opacity)
  279. {
  280. ASSERT(!m_target->has_alpha_channel());
  281. if (!opacity)
  282. return;
  283. if (opacity >= 1.0f)
  284. return blit(position, source, src_rect);
  285. u8 alpha = 255 * opacity;
  286. Rect safe_src_rect = Rect::intersection(src_rect, source.rect());
  287. Rect dst_rect(position, safe_src_rect.size());
  288. dst_rect.move_by(state().translation);
  289. auto clipped_rect = Rect::intersection(dst_rect, clip_rect());
  290. if (clipped_rect.is_empty())
  291. return;
  292. const int first_row = clipped_rect.top() - dst_rect.top();
  293. const int last_row = clipped_rect.bottom() - dst_rect.top();
  294. const int first_column = clipped_rect.left() - dst_rect.left();
  295. const int last_column = clipped_rect.right() - dst_rect.left();
  296. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  297. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  298. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  299. const unsigned src_skip = source.pitch() / sizeof(RGBA32);
  300. for (int row = first_row; row <= last_row; ++row) {
  301. for (int x = 0; x <= (last_column - first_column); ++x) {
  302. Color src_color_with_alpha = Color::from_rgb(src[x]);
  303. src_color_with_alpha.set_alpha(alpha);
  304. Color dst_color = Color::from_rgb(dst[x]);
  305. dst[x] = dst_color.blend(src_color_with_alpha).value();
  306. }
  307. dst += dst_skip;
  308. src += src_skip;
  309. }
  310. }
  311. void Painter::blit_dimmed(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect)
  312. {
  313. Rect safe_src_rect = src_rect.intersected(source.rect());
  314. auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation());
  315. auto clipped_rect = dst_rect.intersected(clip_rect());
  316. if (clipped_rect.is_empty())
  317. return;
  318. const int first_row = clipped_rect.top() - dst_rect.top();
  319. const int last_row = clipped_rect.bottom() - dst_rect.top();
  320. const int first_column = clipped_rect.left() - dst_rect.left();
  321. const int last_column = clipped_rect.right() - dst_rect.left();
  322. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  323. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  324. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  325. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  326. for (int row = first_row; row <= last_row; ++row) {
  327. for (int x = 0; x <= (last_column - first_column); ++x) {
  328. u8 alpha = Color::from_rgba(src[x]).alpha();
  329. if (alpha == 0xff)
  330. dst[x] = Color::from_rgba(src[x]).to_grayscale().lightened().value();
  331. else if (!alpha)
  332. continue;
  333. else
  334. dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x]).to_grayscale().lightened()).value();
  335. }
  336. dst += dst_skip;
  337. src += src_skip;
  338. }
  339. }
  340. void Painter::draw_tiled_bitmap(const Rect& a_dst_rect, const Gfx::Bitmap& source)
  341. {
  342. auto dst_rect = a_dst_rect.translated(translation());
  343. auto clipped_rect = dst_rect.intersected(clip_rect());
  344. if (clipped_rect.is_empty())
  345. return;
  346. const int first_row = (clipped_rect.top() - dst_rect.top());
  347. const int last_row = (clipped_rect.bottom() - dst_rect.top());
  348. const int first_column = (clipped_rect.left() - dst_rect.left());
  349. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  350. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  351. if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) {
  352. int x_start = first_column + a_dst_rect.left();
  353. for (int row = first_row; row <= last_row; ++row) {
  354. const RGBA32* sl = source.scanline((row + a_dst_rect.top())
  355. % source.size().height());
  356. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  357. dst[x - x_start] = sl[x % source.size().width()];
  358. }
  359. dst += dst_skip;
  360. }
  361. return;
  362. }
  363. ASSERT_NOT_REACHED();
  364. }
  365. void Painter::blit_offset(const Point& position,
  366. const Gfx::Bitmap& source,
  367. const Rect& src_rect,
  368. const Point& offset)
  369. {
  370. auto dst_rect = Rect(position, src_rect.size()).translated(translation());
  371. auto clipped_rect = dst_rect.intersected(clip_rect());
  372. if (clipped_rect.is_empty())
  373. return;
  374. const int first_row = (clipped_rect.top() - dst_rect.top());
  375. const int last_row = (clipped_rect.bottom() - dst_rect.top());
  376. const int first_column = (clipped_rect.left() - dst_rect.left());
  377. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  378. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  379. if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) {
  380. int x_start = first_column + src_rect.left();
  381. for (int row = first_row; row <= last_row; ++row) {
  382. int sr = row - offset.y() + src_rect.top();
  383. if (sr >= source.size().height() || sr < 0) {
  384. dst += dst_skip;
  385. continue;
  386. }
  387. const RGBA32* sl = source.scanline(sr);
  388. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  389. int sx = x - offset.x();
  390. if (sx < source.size().width() && sx >= 0)
  391. dst[x - x_start] = sl[sx];
  392. }
  393. dst += dst_skip;
  394. }
  395. return;
  396. }
  397. ASSERT_NOT_REACHED();
  398. }
  399. void Painter::blit_with_alpha(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect)
  400. {
  401. ASSERT(source.has_alpha_channel());
  402. Rect safe_src_rect = src_rect.intersected(source.rect());
  403. auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation());
  404. auto clipped_rect = dst_rect.intersected(clip_rect());
  405. if (clipped_rect.is_empty())
  406. return;
  407. const int first_row = clipped_rect.top() - dst_rect.top();
  408. const int last_row = clipped_rect.bottom() - dst_rect.top();
  409. const int first_column = clipped_rect.left() - dst_rect.left();
  410. const int last_column = clipped_rect.right() - dst_rect.left();
  411. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  412. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  413. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  414. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  415. for (int row = first_row; row <= last_row; ++row) {
  416. for (int x = 0; x <= (last_column - first_column); ++x) {
  417. u8 alpha = Color::from_rgba(src[x]).alpha();
  418. if (alpha == 0xff)
  419. dst[x] = src[x];
  420. else if (!alpha)
  421. continue;
  422. else
  423. dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x])).value();
  424. }
  425. dst += dst_skip;
  426. src += src_skip;
  427. }
  428. }
  429. void Painter::blit(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect, float opacity)
  430. {
  431. if (opacity < 1.0f)
  432. return blit_with_opacity(position, source, src_rect, opacity);
  433. if (source.has_alpha_channel())
  434. return blit_with_alpha(position, source, src_rect);
  435. auto safe_src_rect = src_rect.intersected(source.rect());
  436. ASSERT(source.rect().contains(safe_src_rect));
  437. auto dst_rect = Rect(position, safe_src_rect.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. if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) {
  447. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  448. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  449. for (int row = first_row; row <= last_row; ++row) {
  450. fast_u32_copy(dst, src, clipped_rect.width());
  451. dst += dst_skip;
  452. src += src_skip;
  453. }
  454. return;
  455. }
  456. if (source.format() == BitmapFormat::Indexed8) {
  457. const u8* src = source.bits(src_rect.top() + first_row) + src_rect.left() + first_column;
  458. const size_t src_skip = source.pitch();
  459. for (int row = first_row; row <= last_row; ++row) {
  460. for (int i = 0; i < clipped_rect.width(); ++i)
  461. dst[i] = source.palette_color(src[i]).value();
  462. dst += dst_skip;
  463. src += src_skip;
  464. }
  465. return;
  466. }
  467. ASSERT_NOT_REACHED();
  468. }
  469. template<bool has_alpha_channel, typename GetPixel>
  470. ALWAYS_INLINE static void do_draw_integer_scaled_bitmap(Gfx::Bitmap& target, const Rect& dst_rect, const Gfx::Bitmap& source, int hfactor, int vfactor, GetPixel get_pixel)
  471. {
  472. for (int y = source.rect().top(); y <= source.rect().bottom(); ++y) {
  473. int dst_y = dst_rect.y() + y * vfactor;
  474. for (int x = source.rect().left(); x <= source.rect().right(); ++x) {
  475. auto src_pixel = get_pixel(source, x, y);
  476. for (int yo = 0; yo < vfactor; ++yo) {
  477. auto* scanline = (Color*)target.scanline(dst_y + yo);
  478. int dst_x = dst_rect.x() + x * hfactor;
  479. for (int xo = 0; xo < hfactor; ++xo) {
  480. if constexpr (has_alpha_channel)
  481. scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel);
  482. else
  483. scanline[dst_x + xo] = src_pixel;
  484. }
  485. }
  486. }
  487. }
  488. }
  489. template<bool has_alpha_channel, typename GetPixel>
  490. ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, const Rect& dst_rect, const Rect& clipped_rect, const Gfx::Bitmap& source, const Rect& src_rect, int hscale, int vscale, GetPixel get_pixel)
  491. {
  492. if (dst_rect == clipped_rect && !(dst_rect.width() % src_rect.width()) && !(dst_rect.height() % src_rect.height())) {
  493. int hfactor = dst_rect.width() / src_rect.width();
  494. int vfactor = dst_rect.height() / src_rect.height();
  495. if (hfactor == 2 && vfactor == 2)
  496. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 2, 2, get_pixel);
  497. if (hfactor == 3 && vfactor == 3)
  498. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 3, 3, get_pixel);
  499. if (hfactor == 4 && vfactor == 4)
  500. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 4, 4, get_pixel);
  501. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, hfactor, vfactor, get_pixel);
  502. }
  503. for (int y = clipped_rect.top(); y <= clipped_rect.bottom(); ++y) {
  504. auto* scanline = (Color*)target.scanline(y);
  505. for (int x = clipped_rect.left(); x <= clipped_rect.right(); ++x) {
  506. auto scaled_x = ((x - dst_rect.x()) * hscale) >> 16;
  507. auto scaled_y = ((y - dst_rect.y()) * vscale) >> 16;
  508. auto src_pixel = get_pixel(source, scaled_x, scaled_y);
  509. if constexpr (has_alpha_channel) {
  510. scanline[x] = scanline[x].blend(src_pixel);
  511. } else
  512. scanline[x] = src_pixel;
  513. }
  514. }
  515. }
  516. void Painter::draw_scaled_bitmap(const Rect& a_dst_rect, const Gfx::Bitmap& source, const Rect& src_rect)
  517. {
  518. auto dst_rect = a_dst_rect;
  519. if (dst_rect.size() == src_rect.size())
  520. return blit(dst_rect.location(), source, src_rect);
  521. auto safe_src_rect = src_rect.intersected(source.rect());
  522. ASSERT(source.rect().contains(safe_src_rect));
  523. dst_rect.move_by(state().translation);
  524. auto clipped_rect = dst_rect.intersected(clip_rect());
  525. if (clipped_rect.is_empty())
  526. return;
  527. int hscale = (src_rect.width() << 16) / dst_rect.width();
  528. int vscale = (src_rect.height() << 16) / dst_rect.height();
  529. if (source.has_alpha_channel()) {
  530. switch (source.format()) {
  531. case BitmapFormat::RGB32:
  532. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGB32>);
  533. break;
  534. case BitmapFormat::RGBA32:
  535. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGBA32>);
  536. break;
  537. case BitmapFormat::Indexed8:
  538. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>);
  539. break;
  540. default:
  541. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>);
  542. break;
  543. }
  544. } else {
  545. switch (source.format()) {
  546. case BitmapFormat::RGB32:
  547. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGB32>);
  548. break;
  549. case BitmapFormat::RGBA32:
  550. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGBA32>);
  551. break;
  552. case BitmapFormat::Indexed8:
  553. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>);
  554. break;
  555. default:
  556. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>);
  557. break;
  558. }
  559. }
  560. }
  561. [[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, Color color)
  562. {
  563. draw_glyph(point, ch, font(), color);
  564. }
  565. [[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, const Font& font, Color color)
  566. {
  567. draw_bitmap(point, font.glyph_bitmap(ch), color);
  568. }
  569. void Painter::draw_emoji(const Point& point, const Gfx::Bitmap& emoji, const Font& font)
  570. {
  571. if (!font.is_fixed_width())
  572. blit(point, emoji, emoji.rect());
  573. else {
  574. Rect dst_rect {
  575. point.x(),
  576. point.y(),
  577. font.glyph_width('x'),
  578. font.glyph_height()
  579. };
  580. draw_scaled_bitmap(dst_rect, emoji, emoji.rect());
  581. }
  582. }
  583. void Painter::draw_glyph_or_emoji(const Point& point, u32 codepoint, const Font& font, Color color)
  584. {
  585. if (codepoint < 256) {
  586. // This looks like a regular character.
  587. draw_glyph(point, (char)codepoint, font, color);
  588. return;
  589. }
  590. // Perhaps it's an emoji?
  591. auto* emoji = Emoji::emoji_for_codepoint(codepoint);
  592. if (emoji == nullptr) {
  593. #ifdef EMOJI_DEBUG
  594. dbg() << "Failed to find an emoji for codepoint " << codepoint;
  595. #endif
  596. draw_glyph(point, '?', font, color);
  597. return;
  598. }
  599. draw_emoji(point, *emoji, font);
  600. }
  601. void Painter::draw_text_line(const Rect& a_rect, const Utf8View& text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
  602. {
  603. auto rect = a_rect;
  604. Utf8View final_text(text);
  605. String elided_text;
  606. if (elision == TextElision::Right) {
  607. int text_width = font.width(final_text);
  608. if (font.width(final_text) > rect.width()) {
  609. int glyph_spacing = font.glyph_spacing();
  610. int byte_offset = 0;
  611. int new_width = font.width("...");
  612. if (new_width < text_width) {
  613. for (auto it = final_text.begin(); it != final_text.end(); ++it) {
  614. u32 codepoint = *it;
  615. int glyph_width = font.glyph_or_emoji_width(codepoint);
  616. // NOTE: Glyph spacing should not be added after the last glyph on the line,
  617. // but since we are here because the last glyph does not actually fit on the line,
  618. // we don't have to worry about spacing.
  619. int width_with_this_glyph_included = new_width + glyph_width + glyph_spacing;
  620. if (width_with_this_glyph_included > rect.width())
  621. break;
  622. byte_offset = final_text.byte_offset_of(it);
  623. new_width += glyph_width + glyph_spacing;
  624. }
  625. StringBuilder builder;
  626. builder.append(final_text.substring_view(0, byte_offset).as_string());
  627. builder.append("...");
  628. elided_text = builder.to_string();
  629. final_text = Utf8View { elided_text };
  630. }
  631. }
  632. }
  633. switch (alignment) {
  634. case TextAlignment::TopLeft:
  635. case TextAlignment::CenterLeft:
  636. break;
  637. case TextAlignment::TopRight:
  638. case TextAlignment::CenterRight:
  639. rect.set_x(rect.right() - font.width(final_text));
  640. break;
  641. case TextAlignment::Center: {
  642. auto shrunken_rect = rect;
  643. shrunken_rect.set_width(font.width(final_text));
  644. shrunken_rect.center_within(rect);
  645. rect = shrunken_rect;
  646. break;
  647. }
  648. default:
  649. ASSERT_NOT_REACHED();
  650. }
  651. auto point = rect.location();
  652. int space_width = font.glyph_width(' ') + font.glyph_spacing();
  653. for (u32 codepoint : final_text) {
  654. if (codepoint == ' ') {
  655. point.move_by(space_width, 0);
  656. continue;
  657. }
  658. draw_glyph_or_emoji(point, codepoint, font, color);
  659. point.move_by(font.glyph_or_emoji_width(codepoint) + font.glyph_spacing(), 0);
  660. }
  661. }
  662. void Painter::draw_text(const Rect& rect, const StringView& text, TextAlignment alignment, Color color, TextElision elision)
  663. {
  664. draw_text(rect, text, font(), alignment, color, elision);
  665. }
  666. void Painter::draw_text(const Rect& rect, const StringView& raw_text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
  667. {
  668. Utf8View text { raw_text };
  669. Vector<Utf8View, 32> lines;
  670. int start_of_current_line = 0;
  671. for (auto it = text.begin(); it != text.end(); ++it) {
  672. u32 codepoint = *it;
  673. if (codepoint == '\n') {
  674. int byte_offset = text.byte_offset_of(it);
  675. Utf8View line = text.substring_view(start_of_current_line, byte_offset - start_of_current_line);
  676. lines.append(line);
  677. start_of_current_line = byte_offset + 1;
  678. }
  679. }
  680. if (start_of_current_line != text.byte_length()) {
  681. Utf8View line = text.substring_view(start_of_current_line, text.byte_length() - start_of_current_line);
  682. lines.append(line);
  683. }
  684. static const int line_spacing = 4;
  685. int line_height = font.glyph_height() + line_spacing;
  686. Rect bounding_rect { 0, 0, 0, (static_cast<int>(lines.size()) * line_height) - line_spacing };
  687. for (auto& line : lines) {
  688. auto line_width = font.width(line);
  689. if (line_width > bounding_rect.width())
  690. bounding_rect.set_width(line_width);
  691. }
  692. switch (alignment) {
  693. case TextAlignment::TopLeft:
  694. bounding_rect.set_location(rect.location());
  695. break;
  696. case TextAlignment::TopRight:
  697. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.y() });
  698. break;
  699. case TextAlignment::CenterLeft:
  700. bounding_rect.set_location({ rect.x(), rect.center().y() - (bounding_rect.height() / 2) });
  701. break;
  702. case TextAlignment::CenterRight:
  703. bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.center().y() - (bounding_rect.height() / 2) });
  704. break;
  705. case TextAlignment::Center:
  706. bounding_rect.center_within(rect);
  707. break;
  708. default:
  709. ASSERT_NOT_REACHED();
  710. }
  711. for (size_t i = 0; i < lines.size(); ++i) {
  712. auto& line = lines[i];
  713. Rect line_rect { bounding_rect.x(), bounding_rect.y() + static_cast<int>(i) * line_height, bounding_rect.width(), line_height };
  714. line_rect.intersect(rect);
  715. draw_text_line(line_rect, line, font, alignment, color, elision);
  716. }
  717. }
  718. void Painter::set_pixel(const Point& p, Color color)
  719. {
  720. auto point = p;
  721. point.move_by(state().translation);
  722. if (!clip_rect().contains(point))
  723. return;
  724. m_target->scanline(point.y())[point.x()] = color.value();
  725. }
  726. [[gnu::always_inline]] inline void Painter::set_pixel_with_draw_op(u32& pixel, const Color& color)
  727. {
  728. if (draw_op() == DrawOp::Copy)
  729. pixel = color.value();
  730. else if (draw_op() == DrawOp::Xor)
  731. pixel ^= color.value();
  732. }
  733. void Painter::draw_pixel(const Point& position, Color color, int thickness)
  734. {
  735. ASSERT(draw_op() == DrawOp::Copy);
  736. if (thickness == 1)
  737. return set_pixel_with_draw_op(m_target->scanline(position.y())[position.x()], color);
  738. Rect rect { position.translated(-(thickness / 2), -(thickness / 2)), { thickness, thickness } };
  739. fill_rect(rect.translated(-state().translation), color);
  740. }
  741. void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thickness, bool dotted)
  742. {
  743. auto clip_rect = this->clip_rect();
  744. auto point1 = p1;
  745. point1.move_by(state().translation);
  746. auto point2 = p2;
  747. point2.move_by(state().translation);
  748. // Special case: vertical line.
  749. if (point1.x() == point2.x()) {
  750. const int x = point1.x();
  751. if (x < clip_rect.left() || x > clip_rect.right())
  752. return;
  753. if (point1.y() > point2.y())
  754. swap(point1, point2);
  755. if (point1.y() > clip_rect.bottom())
  756. return;
  757. if (point2.y() < clip_rect.top())
  758. return;
  759. int min_y = max(point1.y(), clip_rect.top());
  760. int max_y = min(point2.y(), clip_rect.bottom());
  761. if (dotted) {
  762. for (int y = min_y; y <= max_y; y += 2)
  763. draw_pixel({ x, y }, color, thickness);
  764. } else {
  765. for (int y = min_y; y <= max_y; ++y)
  766. draw_pixel({ x, y }, color, thickness);
  767. }
  768. return;
  769. }
  770. // Special case: horizontal line.
  771. if (point1.y() == point2.y()) {
  772. const int y = point1.y();
  773. if (y < clip_rect.top() || y > clip_rect.bottom())
  774. return;
  775. if (point1.x() > point2.x())
  776. swap(point1, point2);
  777. if (point1.x() > clip_rect.right())
  778. return;
  779. if (point2.x() < clip_rect.left())
  780. return;
  781. int min_x = max(point1.x(), clip_rect.left());
  782. int max_x = min(point2.x(), clip_rect.right());
  783. if (dotted) {
  784. for (int x = min_x; x <= max_x; x += 2)
  785. draw_pixel({ x, y }, color, thickness);
  786. } else {
  787. for (int x = min_x; x <= max_x; ++x)
  788. draw_pixel({ x, y }, color, thickness);
  789. }
  790. return;
  791. }
  792. // FIXME: Implement dotted diagonal lines.
  793. ASSERT(!dotted);
  794. const double adx = abs(point2.x() - point1.x());
  795. const double ady = abs(point2.y() - point1.y());
  796. if (adx > ady) {
  797. if (point1.x() > point2.x())
  798. swap(point1, point2);
  799. } else {
  800. if (point1.y() > point2.y())
  801. swap(point1, point2);
  802. }
  803. // FIXME: Implement clipping below.
  804. const double dx = point2.x() - point1.x();
  805. const double dy = point2.y() - point1.y();
  806. double error = 0;
  807. if (dx > dy) {
  808. const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  809. const double delta_error = fabs(dy / dx);
  810. int y = point1.y();
  811. for (int x = point1.x(); x <= point2.x(); ++x) {
  812. if (clip_rect.contains(x, y))
  813. draw_pixel({ x, y }, color, thickness);
  814. error += delta_error;
  815. if (error >= 0.5) {
  816. y = (double)y + y_step;
  817. error -= 1.0;
  818. }
  819. }
  820. } else {
  821. const double x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1);
  822. const double delta_error = fabs(dx / dy);
  823. int x = point1.x();
  824. for (int y = point1.y(); y <= point2.y(); ++y) {
  825. if (clip_rect.contains(x, y))
  826. draw_pixel({ x, y }, color, thickness);
  827. error += delta_error;
  828. if (error >= 0.5) {
  829. x = (double)x + x_step;
  830. error -= 1.0;
  831. }
  832. }
  833. }
  834. }
  835. void Painter::add_clip_rect(const Rect& rect)
  836. {
  837. state().clip_rect.intersect(rect.translated(m_clip_origin.location()));
  838. state().clip_rect.intersect(m_target->rect());
  839. }
  840. void Painter::clear_clip_rect()
  841. {
  842. state().clip_rect = m_clip_origin;
  843. }
  844. PainterStateSaver::PainterStateSaver(Painter& painter)
  845. : m_painter(painter)
  846. {
  847. m_painter.save();
  848. }
  849. PainterStateSaver::~PainterStateSaver()
  850. {
  851. m_painter.restore();
  852. }
  853. }