Painter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "Painter.h"
  2. #include "Font.h"
  3. #include "GraphicsBitmap.h"
  4. #include <SharedGraphics/CharacterBitmap.h>
  5. #include <AK/Assertions.h>
  6. #include <AK/StdLibExtras.h>
  7. #ifdef LIBGUI
  8. #include <LibGUI/GWidget.h>
  9. #include <LibGUI/GWindow.h>
  10. #include <LibGUI/GEventLoop.h>
  11. #include <LibC/stdio.h>
  12. #include <LibC/errno.h>
  13. #include <LibC/string.h>
  14. #endif
  15. Painter::Painter(GraphicsBitmap& bitmap)
  16. : m_target(bitmap)
  17. {
  18. m_font = &Font::default_font();
  19. m_clip_rect = { { 0, 0 }, bitmap.size() };
  20. }
  21. #ifdef LIBGUI
  22. Painter::Painter(GWidget& widget)
  23. : m_font(&widget.font())
  24. , m_window(widget.window())
  25. , m_target(*m_window->backing())
  26. {
  27. m_translation.move_by(widget.window_relative_rect().location());
  28. // NOTE: m_clip_rect is in Window coordinates since we are painting into its backing store.
  29. m_clip_rect = widget.window_relative_rect();
  30. m_clip_rect.intersect(m_target->rect());
  31. }
  32. #endif
  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;
  39. rect.move_by(m_translation);
  40. rect.intersect(m_clip_rect);
  41. if (rect.is_empty())
  42. return;
  43. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  44. const unsigned dst_skip = m_target->width();
  45. for (int i = rect.height() - 1; i >= 0; --i) {
  46. for (int j = 0; j < rect.width(); ++j)
  47. set_pixel_with_draw_op(dst[j], color);
  48. dst += dst_skip;
  49. }
  50. }
  51. void Painter::fill_rect(const Rect& a_rect, Color color)
  52. {
  53. if (m_draw_op != DrawOp::Copy) {
  54. fill_rect_with_draw_op(a_rect, color);
  55. return;
  56. }
  57. auto rect = a_rect;
  58. rect.move_by(m_translation);
  59. rect.intersect(m_clip_rect);
  60. if (rect.is_empty())
  61. return;
  62. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  63. const unsigned dst_skip = m_target->width();
  64. for (int i = rect.height() - 1; i >= 0; --i) {
  65. fast_dword_fill(dst, color.value(), rect.width());
  66. dst += dst_skip;
  67. }
  68. }
  69. void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end)
  70. {
  71. #ifdef NO_FPU
  72. return fill_rect(a_rect, gradient_start);
  73. #endif
  74. auto rect = a_rect;
  75. rect.move_by(m_translation);
  76. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  77. if (clipped_rect.is_empty())
  78. return;
  79. int x_offset = clipped_rect.x() - rect.x();
  80. RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
  81. const unsigned dst_skip = m_target->width();
  82. float increment = (1.0/((rect.width())/255.0));
  83. int r2 = gradient_start.red();
  84. int g2 = gradient_start.green();
  85. int b2 = gradient_start.blue();
  86. int r1 = gradient_end.red();
  87. int g1 = gradient_end.green();
  88. int b1 = gradient_end.blue();
  89. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  90. float c = x_offset * increment;
  91. for (int j = 0; j < clipped_rect.width(); ++j) {
  92. dst[j] = Color(
  93. r1 / 255.0 * c + r2 / 255.0 * (255 - c),
  94. g1 / 255.0 * c + g2 / 255.0 * (255 - c),
  95. b1 / 255.0 * c + b2 / 255.0 * (255 - c)
  96. ).value();
  97. c += increment;
  98. }
  99. dst += dst_skip;
  100. }
  101. }
  102. void Painter::draw_rect(const Rect& a_rect, Color color, bool rough)
  103. {
  104. Rect rect = a_rect;
  105. rect.move_by(m_translation);
  106. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  107. if (clipped_rect.is_empty())
  108. return;
  109. int min_y = clipped_rect.top();
  110. int max_y = clipped_rect.bottom();
  111. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  112. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  113. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  114. fast_dword_fill(m_target->scanline(rect.top()) + start_x, color.value(), width);
  115. ++min_y;
  116. }
  117. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  118. int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
  119. int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width();
  120. fast_dword_fill(m_target->scanline(rect.bottom()) + start_x, color.value(), width);
  121. --max_y;
  122. }
  123. bool draw_left_side = rect.left() >= clipped_rect.left();
  124. bool draw_right_side = rect.right() == clipped_rect.right();
  125. if (draw_left_side && draw_right_side) {
  126. // Specialized loop when drawing both sides.
  127. for (int y = min_y; y <= max_y; ++y) {
  128. auto* bits = m_target->scanline(y);
  129. bits[rect.left()] = color.value();
  130. bits[rect.right()] = color.value();
  131. }
  132. } else {
  133. for (int y = min_y; y <= max_y; ++y) {
  134. auto* bits = m_target->scanline(y);
  135. if (draw_left_side)
  136. bits[rect.left()] = color.value();
  137. if (draw_right_side)
  138. bits[rect.right()] = color.value();
  139. }
  140. }
  141. }
  142. void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
  143. {
  144. Rect rect { p, bitmap.size() };
  145. rect.move_by(m_translation);
  146. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  147. if (clipped_rect.is_empty())
  148. return;
  149. const int first_row = clipped_rect.top() - rect.top();
  150. const int last_row = clipped_rect.bottom() - rect.top();
  151. const int first_column = clipped_rect.left() - rect.left();
  152. const int last_column = clipped_rect.right() - rect.left();
  153. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  154. const size_t dst_skip = m_target->width();
  155. const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  156. const size_t bitmap_skip = bitmap.width();
  157. for (int row = first_row; row <= last_row; ++row) {
  158. for (int j = 0; j <= (last_column - first_column); ++j) {
  159. char fc = bitmap_row[j];
  160. if (fc == '#')
  161. dst[j] = color.value();
  162. }
  163. bitmap_row += bitmap_skip;
  164. dst += dst_skip;
  165. }
  166. }
  167. void Painter::draw_bitmap(const Point& p, const GlyphBitmap& bitmap, Color color)
  168. {
  169. Rect dst_rect { p, bitmap.size() };
  170. dst_rect.move_by(m_translation);
  171. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  172. if (clipped_rect.is_empty())
  173. return;
  174. const int first_row = clipped_rect.top() - dst_rect.top();
  175. const int last_row = clipped_rect.bottom() - dst_rect.top();
  176. const int first_column = clipped_rect.left() - dst_rect.left();
  177. const int last_column = clipped_rect.right() - dst_rect.left();
  178. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  179. const size_t dst_skip = m_target->width();
  180. for (int row = first_row; row <= last_row; ++row) {
  181. for (int j = 0; j <= (last_column - first_column); ++j) {
  182. if (bitmap.bit_at(j + first_column, row))
  183. dst[j] = color.value();
  184. }
  185. dst += dst_skip;
  186. }
  187. }
  188. void Painter::blit_with_opacity(const Point& position, const GraphicsBitmap& source, const Rect& src_rect, float opacity)
  189. {
  190. ASSERT(!m_target->has_alpha_channel());
  191. if (!opacity)
  192. return;
  193. if (opacity >= 1.0f)
  194. return blit(position, source, src_rect);
  195. byte alpha = 255 * opacity;
  196. Rect safe_src_rect = Rect::intersection(src_rect, source.rect());
  197. Rect dst_rect(position, safe_src_rect.size());
  198. dst_rect.move_by(m_translation);
  199. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  200. if (clipped_rect.is_empty())
  201. return;
  202. const int first_row = clipped_rect.top() - dst_rect.top();
  203. const int last_row = clipped_rect.bottom() - dst_rect.top();
  204. const int first_column = clipped_rect.left() - dst_rect.left();
  205. const int last_column = clipped_rect.right() - dst_rect.left();
  206. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  207. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  208. const size_t dst_skip = m_target->width();
  209. const unsigned src_skip = source.width();
  210. for (int row = first_row; row <= last_row; ++row) {
  211. for (int x = 0; x <= (last_column - first_column); ++x) {
  212. Color src_color_with_alpha = Color::from_rgb(src[x]);
  213. src_color_with_alpha.set_alpha(alpha);
  214. Color dst_color = Color::from_rgb(dst[x]);
  215. dst[x] = dst_color.blend(src_color_with_alpha).value();
  216. }
  217. dst += dst_skip;
  218. src += src_skip;
  219. }
  220. }
  221. void Painter::blit_with_alpha(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  222. {
  223. ASSERT(source.has_alpha_channel());
  224. Rect safe_src_rect = Rect::intersection(src_rect, source.rect());
  225. Rect dst_rect(position, safe_src_rect.size());
  226. dst_rect.move_by(m_translation);
  227. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  228. if (clipped_rect.is_empty())
  229. return;
  230. const int first_row = clipped_rect.top() - dst_rect.top();
  231. const int last_row = clipped_rect.bottom() - dst_rect.top();
  232. const int first_column = clipped_rect.left() - dst_rect.left();
  233. const int last_column = clipped_rect.right() - dst_rect.left();
  234. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  235. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  236. const size_t dst_skip = m_target->width();
  237. const unsigned src_skip = source.width();
  238. for (int row = first_row; row <= last_row; ++row) {
  239. for (int x = 0; x <= (last_column - first_column); ++x) {
  240. byte alpha = Color::from_rgba(src[x]).alpha();
  241. if (alpha == 0xff)
  242. dst[x] = src[x];
  243. else if (!alpha)
  244. continue;
  245. else
  246. dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x])).value();
  247. }
  248. dst += dst_skip;
  249. src += src_skip;
  250. }
  251. }
  252. void Painter::blit(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  253. {
  254. if (source.has_alpha_channel())
  255. return blit_with_alpha(position, source, src_rect);
  256. auto safe_src_rect = Rect::intersection(src_rect, source.rect());
  257. ASSERT(source.rect().contains(safe_src_rect));
  258. Rect dst_rect(position, safe_src_rect.size());
  259. dst_rect.move_by(m_translation);
  260. auto clipped_rect = Rect::intersection(dst_rect, m_clip_rect);
  261. if (clipped_rect.is_empty())
  262. return;
  263. const int first_row = clipped_rect.top() - dst_rect.top();
  264. const int last_row = clipped_rect.bottom() - dst_rect.top();
  265. const int first_column = clipped_rect.left() - dst_rect.left();
  266. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  267. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  268. const size_t dst_skip = m_target->width();
  269. const unsigned src_skip = source.width();
  270. for (int row = first_row; row <= last_row; ++row) {
  271. fast_dword_copy(dst, src, clipped_rect.width());
  272. dst += dst_skip;
  273. src += src_skip;
  274. }
  275. }
  276. [[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, Color color)
  277. {
  278. draw_bitmap(point, font().glyph_bitmap(ch), color);
  279. }
  280. void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alignment, Color color)
  281. {
  282. Point point;
  283. if (alignment == TextAlignment::TopLeft) {
  284. point = rect.location();
  285. } else if (alignment == TextAlignment::CenterLeft) {
  286. point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
  287. } else if (alignment == TextAlignment::CenterRight) {
  288. int text_width = text.length() * font().glyph_width();
  289. point = { rect.right() - text_width, rect.center().y() - (font().glyph_height() / 2) };
  290. } else if (alignment == TextAlignment::Center) {
  291. int text_width = text.length() * font().glyph_width();
  292. point = rect.center();
  293. point.move_by(-(text_width / 2), -(font().glyph_height() / 2));
  294. } else {
  295. ASSERT_NOT_REACHED();
  296. }
  297. for (unsigned i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) {
  298. byte ch = text[i];
  299. if (ch == ' ')
  300. continue;
  301. draw_glyph(point, ch, color);
  302. }
  303. }
  304. void Painter::set_pixel(const Point& p, Color color)
  305. {
  306. auto point = p;
  307. point.move_by(m_translation);
  308. if (!m_clip_rect.contains(point))
  309. return;
  310. m_target->scanline(point.y())[point.x()] = color.value();
  311. }
  312. [[gnu::always_inline]] inline void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
  313. {
  314. if (m_draw_op == DrawOp::Copy)
  315. pixel = color.value();
  316. else if (m_draw_op == DrawOp::Xor)
  317. pixel ^= color.value();
  318. }
  319. void Painter::draw_line(const Point& p1, const Point& p2, Color color)
  320. {
  321. auto point1 = p1;
  322. point1.move_by(m_translation);
  323. auto point2 = p2;
  324. point2.move_by(m_translation);
  325. // Special case: vertical line.
  326. if (point1.x() == point2.x()) {
  327. const int x = point1.x();
  328. if (x < m_clip_rect.left() || x > m_clip_rect.right())
  329. return;
  330. if (point1.y() > point2.y())
  331. swap(point1, point2);
  332. if (point1.y() > m_clip_rect.bottom())
  333. return;
  334. if (point2.y() < m_clip_rect.top())
  335. return;
  336. int min_y = max(point1.y(), m_clip_rect.top());
  337. int max_y = min(point2.y(), m_clip_rect.bottom());
  338. for (int y = min_y; y <= max_y; ++y)
  339. set_pixel_with_draw_op(m_target->scanline(y)[x], color);
  340. return;
  341. }
  342. if (point1.x() > point2.x())
  343. swap(point1, point2);
  344. // Special case: horizontal line.
  345. if (point1.y() == point2.y()) {
  346. const int y = point1.y();
  347. if (y < m_clip_rect.top() || y > m_clip_rect.bottom())
  348. return;
  349. if (point1.x() > point2.x())
  350. swap(point1, point2);
  351. if (point1.x() > m_clip_rect.right())
  352. return;
  353. if (point2.x() < m_clip_rect.left())
  354. return;
  355. int min_x = max(point1.x(), m_clip_rect.left());
  356. int max_x = min(point2.x(), m_clip_rect.right());
  357. auto* pixels = m_target->scanline(point1.y());
  358. if (m_draw_op == DrawOp::Copy) {
  359. fast_dword_fill(pixels + min_x, color.value(), max_x - min_x + 1);
  360. } else {
  361. for (int x = min_x; x <= max_x; ++x)
  362. set_pixel_with_draw_op(pixels[x], color);
  363. }
  364. return;
  365. }
  366. // FIXME: Implement clipping below.
  367. ASSERT_NOT_REACHED();
  368. #if 0
  369. const double dx = point2.x() - point1.x();
  370. const double dy = point2.y() - point1.y();
  371. const double delta_error = fabs(dy / dx);
  372. double error = 0;
  373. const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  374. int y = point1.y();
  375. for (int x = point1.x(); x <= point2.x(); ++x) {
  376. m_target->scanline(y)[x] = color.value();
  377. error += delta_error;
  378. if (error >= 0.5) {
  379. y = (double)y + y_step;
  380. error -= 1.0;
  381. }
  382. }
  383. #endif
  384. }
  385. void Painter::draw_focus_rect(const Rect& rect)
  386. {
  387. Rect focus_rect = rect;
  388. focus_rect.move_by(1, 1);
  389. focus_rect.set_width(focus_rect.width() - 2);
  390. focus_rect.set_height(focus_rect.height() - 2);
  391. draw_rect(focus_rect, Color(96, 96, 192));
  392. }
  393. void Painter::set_clip_rect(const Rect& rect)
  394. {
  395. m_clip_rect = Rect::intersection(rect, m_target->rect());
  396. }
  397. void Painter::clear_clip_rect()
  398. {
  399. m_clip_rect = m_target->rect();
  400. }