Painter.cpp 31 KB

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