Painter.cpp 34 KB

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