Painter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_with_draw_op(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. for (int j = 0; j < rect.width(); ++j)
  63. set_pixel_with_draw_op(dst[j], color.value());
  64. dst += dst_skip;
  65. }
  66. }
  67. void Painter::fill_rect(const Rect& a_rect, Color color)
  68. {
  69. if (m_draw_op != DrawOp::Copy) {
  70. fill_rect_with_draw_op(a_rect, color);
  71. return;
  72. }
  73. auto rect = a_rect;
  74. rect.move_by(m_translation);
  75. rect.intersect(m_clip_rect);
  76. if (rect.is_empty())
  77. return;
  78. RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
  79. const unsigned dst_skip = m_target->width();
  80. for (int i = rect.height() - 1; i >= 0; --i) {
  81. fast_dword_fill(dst, color.value(), rect.width());
  82. dst += dst_skip;
  83. }
  84. }
  85. void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end)
  86. {
  87. #ifdef NO_FPU
  88. return fill_rect(a_rect, gradient_start);
  89. #endif
  90. auto rect = a_rect;
  91. rect.move_by(m_translation);
  92. auto clipped_rect = Rect::intersection(rect, m_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 unsigned dst_skip = m_target->width();
  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_rect(const Rect& a_rect, Color color)
  119. {
  120. Rect rect = a_rect;
  121. rect.move_by(m_translation);
  122. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  123. if (clipped_rect.is_empty())
  124. return;
  125. int min_y = clipped_rect.top();
  126. int max_y = clipped_rect.bottom();
  127. if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) {
  128. fast_dword_fill(m_target->scanline(rect.top()) + clipped_rect.left(), color.value(), clipped_rect.width());
  129. ++min_y;
  130. }
  131. if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
  132. fast_dword_fill(m_target->scanline(rect.bottom()) + clipped_rect.left(), color.value(), clipped_rect.width());
  133. --max_y;
  134. }
  135. bool draw_left_side = rect.left() >= clipped_rect.left();
  136. bool draw_right_side = rect.right() == clipped_rect.right();
  137. if (draw_left_side && draw_right_side) {
  138. // Specialized loop when drawing both sides.
  139. for (int y = min_y; y <= max_y; ++y) {
  140. auto* bits = m_target->scanline(y);
  141. bits[rect.left()] = color.value();
  142. bits[rect.right()] = color.value();
  143. }
  144. } else {
  145. for (int y = min_y; y <= max_y; ++y) {
  146. auto* bits = m_target->scanline(y);
  147. if (draw_left_side)
  148. bits[rect.left()] = color.value();
  149. if (draw_right_side)
  150. bits[rect.right()] = color.value();
  151. }
  152. }
  153. }
  154. void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
  155. {
  156. Rect rect { p, bitmap.size() };
  157. rect.move_by(m_translation);
  158. auto clipped_rect = Rect::intersection(rect, m_clip_rect);
  159. if (clipped_rect.is_empty())
  160. return;
  161. const int first_row = clipped_rect.top() - rect.top();
  162. const int last_row = clipped_rect.bottom() - rect.top();
  163. const int first_column = clipped_rect.left() - rect.left();
  164. const int last_column = clipped_rect.right() - rect.left();
  165. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  166. const size_t dst_skip = m_target->width();
  167. const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
  168. const size_t bitmap_skip = bitmap.width();
  169. for (int row = first_row; row <= last_row; ++row) {
  170. for (int j = 0; j <= (last_column - first_column); ++j) {
  171. char fc = bitmap_row[j];
  172. if (fc == '#')
  173. dst[j] = color.value();
  174. }
  175. bitmap_row += bitmap_skip;
  176. dst += dst_skip;
  177. }
  178. }
  179. FLATTEN void Painter::draw_glyph(const Point& point, char ch, Color color)
  180. {
  181. draw_bitmap(point, font().glyph_bitmap(ch), color);
  182. }
  183. void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alignment, Color color)
  184. {
  185. Point point;
  186. if (alignment == TextAlignment::TopLeft) {
  187. point = rect.location();
  188. } else if (alignment == TextAlignment::CenterLeft) {
  189. point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
  190. } else if (alignment == TextAlignment::CenterRight) {
  191. int text_width = text.length() * font().glyph_width();
  192. point = { rect.right() - text_width, rect.center().y() - (font().glyph_height() / 2) };
  193. } else if (alignment == TextAlignment::Center) {
  194. int text_width = text.length() * font().glyph_width();
  195. point = rect.center();
  196. point.move_by(-(text_width / 2), -(font().glyph_height() / 2));
  197. } else {
  198. ASSERT_NOT_REACHED();
  199. }
  200. for (unsigned i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) {
  201. byte ch = text[i];
  202. if (ch == ' ')
  203. continue;
  204. draw_glyph(point, ch, color);
  205. }
  206. }
  207. void Painter::set_pixel(const Point& p, Color color)
  208. {
  209. auto point = p;
  210. point.move_by(m_translation);
  211. if (!m_clip_rect.contains(point))
  212. return;
  213. m_target->scanline(point.y())[point.x()] = color.value();
  214. }
  215. ALWAYS_INLINE void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
  216. {
  217. if (m_draw_op == DrawOp::Copy)
  218. pixel = color.value();
  219. else if (m_draw_op == DrawOp::Xor)
  220. pixel ^= color.value();
  221. }
  222. void Painter::draw_line(const Point& p1, const Point& p2, Color color)
  223. {
  224. auto point1 = p1;
  225. point1.move_by(m_translation);
  226. auto point2 = p2;
  227. point2.move_by(m_translation);
  228. // Special case: vertical line.
  229. if (point1.x() == point2.x()) {
  230. const int x = point1.x();
  231. if (x < m_clip_rect.left() || x > m_clip_rect.right())
  232. return;
  233. if (point1.y() > point2.y())
  234. swap(point1, point2);
  235. if (point1.y() > m_clip_rect.bottom())
  236. return;
  237. if (point2.y() < m_clip_rect.top())
  238. return;
  239. int min_y = max(point1.y(), m_clip_rect.top());
  240. int max_y = min(point2.y(), m_clip_rect.bottom());
  241. for (int y = min_y; y <= max_y; ++y)
  242. set_pixel_with_draw_op(m_target->scanline(y)[x], color);
  243. return;
  244. }
  245. if (point1.x() > point2.x())
  246. swap(point1, point2);
  247. // Special case: horizontal line.
  248. if (point1.y() == point2.y()) {
  249. const int y = point1.y();
  250. if (y < m_clip_rect.top() || y > m_clip_rect.bottom())
  251. return;
  252. if (point1.x() > point2.x())
  253. swap(point1, point2);
  254. if (point1.x() > m_clip_rect.right())
  255. return;
  256. if (point2.x() < m_clip_rect.left())
  257. return;
  258. int min_x = max(point1.x(), m_clip_rect.left());
  259. int max_x = min(point2.x(), m_clip_rect.right());
  260. auto* pixels = m_target->scanline(point1.y());
  261. if (m_draw_op == DrawOp::Copy) {
  262. fast_dword_fill(pixels + min_x, color.value(), max_x - min_x + 1);
  263. } else {
  264. for (int x = min_x; x <= max_x; ++x)
  265. set_pixel_with_draw_op(pixels[x], color);
  266. }
  267. return;
  268. }
  269. // FIXME: Implement clipping below.
  270. ASSERT_NOT_REACHED();
  271. #if 0
  272. const double dx = point2.x() - point1.x();
  273. const double dy = point2.y() - point1.y();
  274. const double delta_error = fabs(dy / dx);
  275. double error = 0;
  276. const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  277. int y = point1.y();
  278. for (int x = point1.x(); x <= point2.x(); ++x) {
  279. m_target->scanline(y)[x] = color.value();
  280. error += delta_error;
  281. if (error >= 0.5) {
  282. y = (double)y + y_step;
  283. error -= 1.0;
  284. }
  285. }
  286. #endif
  287. }
  288. void Painter::draw_focus_rect(const Rect& rect)
  289. {
  290. Rect focus_rect = rect;
  291. focus_rect.move_by(1, 1);
  292. focus_rect.set_width(focus_rect.width() - 2);
  293. focus_rect.set_height(focus_rect.height() - 2);
  294. draw_rect(focus_rect, Color(96, 96, 192));
  295. }
  296. void Painter::blit(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  297. {
  298. Rect dst_rect(position, src_rect.size());
  299. dst_rect.intersect(m_clip_rect);
  300. RGBA32* dst = m_target->scanline(dst_rect.y()) + dst_rect.x();
  301. const RGBA32* src = source.scanline(src_rect.top()) + src_rect.left();
  302. const unsigned dst_skip = m_target->width();
  303. const unsigned src_skip = source.width();
  304. for (int i = dst_rect.height() - 1; i >= 0; --i) {
  305. fast_dword_copy(dst, src, dst_rect.width());
  306. dst += dst_skip;
  307. src += src_skip;
  308. }
  309. }
  310. void Painter::set_clip_rect(const Rect& rect)
  311. {
  312. m_clip_rect = Rect::intersection(rect, m_target->rect());
  313. }
  314. void Painter::clear_clip_rect()
  315. {
  316. m_clip_rect = m_target->rect();
  317. }