Painter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "Painter.h"
  2. #include "Font.h"
  3. #include "GraphicsBitmap.h"
  4. #include <AK/Assertions.h>
  5. #include <AK/StdLibExtras.h>
  6. #ifdef LIBGUI
  7. #include <LibGUI/GWidget.h>
  8. #include <LibGUI/GWindow.h>
  9. #include <LibC/gui.h>
  10. #include <LibC/stdio.h>
  11. #endif
  12. #define DEBUG_WIDGET_UNDERDRAW
  13. Painter::Painter(GraphicsBitmap& bitmap)
  14. {
  15. m_font = &Font::default_font();
  16. m_target = &bitmap;
  17. m_clip_rect = { { 0, 0 }, bitmap.size() };
  18. }
  19. #ifdef LIBGUI
  20. Painter::Painter(GWidget& widget)
  21. : m_font(&widget.font())
  22. {
  23. GUI_WindowBackingStoreInfo backing;
  24. int rc = gui_get_window_backing_store(widget.window()->window_id(), &backing);
  25. if (rc < 0) {
  26. perror("gui_get_window_backing_store");
  27. exit(1);
  28. }
  29. m_backing_store_id = backing.backing_store_id;
  30. m_target = GraphicsBitmap::create_wrapper(backing.size, backing.pixels);
  31. ASSERT(m_target);
  32. m_window = widget.window();
  33. m_translation.move_by(widget.relative_position());
  34. // NOTE: m_clip_rect is in Window coordinates since we are painting into its backing store.
  35. m_clip_rect = widget.relative_rect();
  36. m_clip_rect.intersect(m_target->rect());
  37. #ifdef DEBUG_WIDGET_UNDERDRAW
  38. // If the widget is not opaque, let's not mess it up with debugging color.
  39. if (widget.fill_with_background_color() && m_window->main_widget() != &widget)
  40. fill_rect(widget.rect(), Color::Red);
  41. #endif
  42. }
  43. #endif
  44. Painter::~Painter()
  45. {
  46. #ifdef LIBGUI
  47. m_target = nullptr;
  48. int rc = gui_release_window_backing_store(m_backing_store_id);
  49. ASSERT(rc == 0);
  50. #endif
  51. }
  52. void Painter::fill_rect(const Rect& a_rect, Color color)
  53. {
  54. auto rect = a_rect;
  55. rect.move_by(m_translation);
  56. rect.intersect(m_clip_rect);
  57. if (rect.is_empty())
  58. return;
  59. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  60. const unsigned dst_skip = m_target->width();
  61. for (int i = rect.height() - 1; i >= 0; --i) {
  62. fast_dword_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;
  72. rect.move_by(m_translation);
  73. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  74. if (clipped_rect.is_empty())
  75. return;
  76. int x_offset = clipped_rect.x() - rect.x();
  77. RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left();
  78. const unsigned dst_skip = m_target->width();
  79. float increment = (1.0/((rect.width())/255.0));
  80. int r2 = gradient_start.red();
  81. int g2 = gradient_start.green();
  82. int b2 = gradient_start.blue();
  83. int r1 = gradient_end.red();
  84. int g1 = gradient_end.green();
  85. int b1 = gradient_end.blue();
  86. for (int i = clipped_rect.height() - 1; i >= 0; --i) {
  87. float c = x_offset * increment;
  88. for (int j = 0; j < clipped_rect.width(); ++j) {
  89. dst[j] = Color(
  90. r1 / 255.0 * c + r2 / 255.0 * (255 - c),
  91. g1 / 255.0 * c + g2 / 255.0 * (255 - c),
  92. b1 / 255.0 * c + b2 / 255.0 * (255 - c)
  93. ).value();
  94. c += increment;
  95. }
  96. dst += dst_skip;
  97. }
  98. }
  99. void Painter::draw_rect(const Rect& a_rect, Color color)
  100. {
  101. Rect rect = a_rect;
  102. rect.move_by(m_translation);
  103. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  104. if (clipped_rect.is_empty())
  105. return;
  106. int min_y = clipped_rect.top();
  107. int max_y = clipped_rect.bottom();
  108. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  109. fast_dword_fill(m_target->scanline(rect.top()) + clipped_rect.left(), color.value(), clipped_rect.width());
  110. ++min_y;
  111. }
  112. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  113. fast_dword_fill(m_target->scanline(rect.bottom()) + clipped_rect.left(), color.value(), clipped_rect.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. Rect rect { p, bitmap.size() };
  138. rect.move_by(m_translation);
  139. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  140. if (clipped_rect.is_empty())
  141. return;
  142. const int first_row = clipped_rect.top() - rect.top();
  143. const int last_row = clipped_rect.bottom() - rect.top();
  144. const int first_column = clipped_rect.left() - rect.left();
  145. const int last_column = clipped_rect.right() - rect.left();
  146. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  147. const size_t dst_skip = m_target->width();
  148. const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  149. const size_t bitmap_skip = bitmap.width();
  150. for (int row = first_row; row <= last_row; ++row) {
  151. for (int j = 0; j <= (last_column - first_column); ++j) {
  152. char fc = bitmap_row[j];
  153. if (fc == '#')
  154. dst[j] = color.value();
  155. }
  156. bitmap_row += bitmap_skip;
  157. dst += dst_skip;
  158. }
  159. }
  160. void Painter::draw_glyph(const Point& point, char ch, Color color)
  161. {
  162. auto* bitmap = font().glyph_bitmap(ch);
  163. if (!bitmap) {
  164. dbgprintf("Font doesn't have 0x%b ('%c')\n", (byte)ch, ch);
  165. bitmap = font().error_bitmap();
  166. }
  167. int x = point.x();
  168. int y = point.y();
  169. draw_bitmap({ x, y }, *bitmap, color);
  170. }
  171. void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alignment, Color color)
  172. {
  173. Point point;
  174. if (alignment == TextAlignment::TopLeft) {
  175. point = rect.location();
  176. } else if (alignment == TextAlignment::CenterLeft) {
  177. point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
  178. } else if (alignment == TextAlignment::CenterRight) {
  179. int text_width = text.length() * font().glyph_width();
  180. point = { rect.right() - text_width, rect.center().y() - (font().glyph_height() / 2) };
  181. } else if (alignment == TextAlignment::Center) {
  182. int textWidth = text.length() * font().glyph_width();
  183. point = rect.center();
  184. point.move_by(-(textWidth / 2), -(font().glyph_height() / 2));
  185. } else {
  186. ASSERT_NOT_REACHED();
  187. }
  188. for (unsigned i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) {
  189. byte ch = text[i];
  190. if (ch == ' ')
  191. continue;
  192. draw_glyph(point, ch, color);
  193. }
  194. }
  195. void Painter::set_pixel(const Point& p, Color color)
  196. {
  197. auto point = p;
  198. point.move_by(m_translation);
  199. if (!m_clip_rect.contains(point))
  200. return;
  201. m_target->scanline(point.y())[point.x()] = color.value();
  202. }
  203. ALWAYS_INLINE void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
  204. {
  205. if (m_draw_op == DrawOp::Copy)
  206. pixel = color.value();
  207. else if (m_draw_op == DrawOp::Xor)
  208. pixel ^= color.value();
  209. }
  210. void Painter::draw_line(const Point& p1, const Point& p2, Color color)
  211. {
  212. auto point1 = p1;
  213. point1.move_by(m_translation);
  214. auto point2 = p2;
  215. point2.move_by(m_translation);
  216. // Special case: vertical line.
  217. if (point1.x() == point2.x()) {
  218. const int x = point1.x();
  219. if (x < m_clip_rect.left() || x > m_clip_rect.right())
  220. return;
  221. if (point1.y() > point2.y())
  222. swap(point1, point2);
  223. int min_y = max(point1.y(), m_clip_rect.top());
  224. int max_y = min(point2.y(), m_clip_rect.bottom());
  225. for (int y = min_y; y <= max_y; ++y)
  226. set_pixel_with_draw_op(m_target->scanline(y)[x], color);
  227. return;
  228. }
  229. if (point1.x() > point2.x())
  230. swap(point1, point2);
  231. // Special case: horizontal line.
  232. if (point1.y() == point2.y()) {
  233. const int y = point1.y();
  234. if (y < m_clip_rect.top() || y > m_clip_rect.bottom())
  235. return;
  236. if (point1.x() > point2.x())
  237. swap(point1, point2);
  238. int min_x = max(point1.x(), m_clip_rect.left());
  239. int max_x = min(point2.x(), m_clip_rect.right());
  240. auto* pixels = m_target->scanline(point1.y());
  241. if (m_draw_op == DrawOp::Copy) {
  242. fast_dword_fill(pixels + min_x, color.value(), max_x - min_x + 1);
  243. } else {
  244. for (int x = min_x; x <= max_x; ++x)
  245. set_pixel_with_draw_op(pixels[x], color);
  246. }
  247. return;
  248. }
  249. // FIXME: Implement clipping below.
  250. ASSERT_NOT_REACHED();
  251. #if 0
  252. const double dx = point2.x() - point1.x();
  253. const double dy = point2.y() - point1.y();
  254. const double delta_error = fabs(dy / dx);
  255. double error = 0;
  256. const double yStep = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  257. int y = point1.y();
  258. for (int x = point1.x(); x <= point2.x(); ++x) {
  259. m_target->scanline(y)[x] = color.value();
  260. error += delta_error;
  261. if (error >= 0.5) {
  262. y = (double)y + yStep;
  263. error -= 1.0;
  264. }
  265. }
  266. #endif
  267. }
  268. void Painter::draw_focus_rect(const Rect& rect)
  269. {
  270. Rect focus_rect = rect;
  271. focus_rect.move_by(1, 1);
  272. focus_rect.set_width(focus_rect.width() - 2);
  273. focus_rect.set_height(focus_rect.height() - 2);
  274. draw_rect(focus_rect, Color(96, 96, 192));
  275. }
  276. void Painter::blit(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  277. {
  278. Rect dst_rect(position, src_rect.size());
  279. dst_rect.intersect(m_clip_rect);
  280. RGBA32* dst = m_target->scanline(dst_rect.y()) + dst_rect.x();
  281. const RGBA32* src = source.scanline(src_rect.top()) + src_rect.left();
  282. const unsigned dst_skip = m_target->width();
  283. const unsigned src_skip = source.width();
  284. for (int i = dst_rect.height() - 1; i >= 0; --i) {
  285. fast_dword_copy(dst, src, dst_rect.width());
  286. dst += dst_skip;
  287. src += src_skip;
  288. }
  289. }
  290. void Painter::set_clip_rect(const Rect& rect)
  291. {
  292. m_clip_rect = Rect::intersection(rect, m_target->rect());
  293. }
  294. void Painter::clear_clip_rect()
  295. {
  296. m_clip_rect = m_target->rect();
  297. }