Painter.cpp 32 KB

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