Painter.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 <SharedGraphics/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_dword_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_dword_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_dword_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_with_opacity(const Point& position, const GraphicsBitmap& source, const Rect& src_rect, float opacity)
  180. {
  181. ASSERT(!m_target->has_alpha_channel());
  182. if (!opacity)
  183. return;
  184. if (opacity >= 1.0f)
  185. return blit(position, source, src_rect);
  186. byte alpha = 255 * opacity;
  187. Rect safe_src_rect = Rect::intersection(src_rect, source.rect());
  188. Rect dst_rect(position, safe_src_rect.size());
  189. dst_rect.move_by(state().translation);
  190. auto clipped_rect = Rect::intersection(dst_rect, clip_rect());
  191. if (clipped_rect.is_empty())
  192. return;
  193. const int first_row = clipped_rect.top() - dst_rect.top();
  194. const int last_row = clipped_rect.bottom() - dst_rect.top();
  195. const int first_column = clipped_rect.left() - dst_rect.left();
  196. const int last_column = clipped_rect.right() - dst_rect.left();
  197. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  198. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  199. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  200. const unsigned src_skip = source.pitch() / sizeof(RGBA32);
  201. for (int row = first_row; row <= last_row; ++row) {
  202. for (int x = 0; x <= (last_column - first_column); ++x) {
  203. Color src_color_with_alpha = Color::from_rgb(src[x]);
  204. src_color_with_alpha.set_alpha(alpha);
  205. Color dst_color = Color::from_rgb(dst[x]);
  206. dst[x] = dst_color.blend(src_color_with_alpha).value();
  207. }
  208. dst += dst_skip;
  209. src += src_skip;
  210. }
  211. }
  212. void Painter::blit_dimmed(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  213. {
  214. Rect safe_src_rect = src_rect.intersected(source.rect());
  215. auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation());
  216. auto clipped_rect = dst_rect.intersected(clip_rect());
  217. if (clipped_rect.is_empty())
  218. return;
  219. const int first_row = clipped_rect.top() - dst_rect.top();
  220. const int last_row = clipped_rect.bottom() - dst_rect.top();
  221. const int first_column = clipped_rect.left() - dst_rect.left();
  222. const int last_column = clipped_rect.right() - dst_rect.left();
  223. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  224. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  225. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  226. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  227. for (int row = first_row; row <= last_row; ++row) {
  228. for (int x = 0; x <= (last_column - first_column); ++x) {
  229. byte alpha = Color::from_rgba(src[x]).alpha();
  230. if (alpha == 0xff)
  231. dst[x] = Color::from_rgba(src[x]).to_grayscale().lightened().value();
  232. else if (!alpha)
  233. continue;
  234. else
  235. dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x]).to_grayscale().lightened()).value();
  236. }
  237. dst += dst_skip;
  238. src += src_skip;
  239. }
  240. }
  241. void Painter::blit_tiled(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  242. {
  243. auto dst_rect = Rect(position, 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. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  251. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  252. if (source.format() == GraphicsBitmap::Format::RGB32 || source.format() == GraphicsBitmap::Format::RGBA32) {
  253. int x_start = first_column + src_rect.left();
  254. for (int row = first_row; row <= last_row; ++row) {
  255. const RGBA32* sl = source.scanline((row + src_rect.top())
  256. % source.size().height());
  257. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  258. dst[x - x_start] = sl[x % source.size().width()];
  259. }
  260. dst += dst_skip;
  261. }
  262. return;
  263. }
  264. ASSERT_NOT_REACHED();
  265. }
  266. void Painter::blit_offset(const Point& position,
  267. const GraphicsBitmap& source,
  268. const Rect& src_rect,
  269. const Point& offset)
  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. int sr = row - offset.y() + src_rect.top();
  284. if (sr >= source.size().height() || sr < 0) {
  285. dst += dst_skip;
  286. continue;
  287. }
  288. const RGBA32* sl = source.scanline(sr);
  289. for (int x = x_start; x < clipped_rect.width() + x_start; ++x) {
  290. int sx = x - offset.x();
  291. if (sx < source.size().width() && sx >= 0)
  292. dst[x - x_start] = sl[sx];
  293. }
  294. dst += dst_skip;
  295. }
  296. return;
  297. }
  298. ASSERT_NOT_REACHED();
  299. }
  300. void Painter::blit_with_alpha(const Point& position, const GraphicsBitmap& source, const Rect& src_rect)
  301. {
  302. ASSERT(source.has_alpha_channel());
  303. Rect safe_src_rect = src_rect.intersected(source.rect());
  304. auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation());
  305. auto clipped_rect = dst_rect.intersected(clip_rect());
  306. if (clipped_rect.is_empty())
  307. return;
  308. const int first_row = clipped_rect.top() - dst_rect.top();
  309. const int last_row = clipped_rect.bottom() - dst_rect.top();
  310. const int first_column = clipped_rect.left() - dst_rect.left();
  311. const int last_column = clipped_rect.right() - dst_rect.left();
  312. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  313. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  314. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  315. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  316. for (int row = first_row; row <= last_row; ++row) {
  317. for (int x = 0; x <= (last_column - first_column); ++x) {
  318. byte alpha = Color::from_rgba(src[x]).alpha();
  319. if (alpha == 0xff)
  320. dst[x] = src[x];
  321. else if (!alpha)
  322. continue;
  323. else
  324. dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x])).value();
  325. }
  326. dst += dst_skip;
  327. src += src_skip;
  328. }
  329. }
  330. void Painter::blit(const Point& position, const GraphicsBitmap& source, const Rect& src_rect, float opacity)
  331. {
  332. if (opacity < 1.0f)
  333. return blit_with_opacity(position, source, src_rect, opacity);
  334. if (source.has_alpha_channel())
  335. return blit_with_alpha(position, source, src_rect);
  336. auto safe_src_rect = src_rect.intersected(source.rect());
  337. ASSERT(source.rect().contains(safe_src_rect));
  338. auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation());
  339. auto clipped_rect = dst_rect.intersected(clip_rect());
  340. if (clipped_rect.is_empty())
  341. return;
  342. const int first_row = clipped_rect.top() - dst_rect.top();
  343. const int last_row = clipped_rect.bottom() - dst_rect.top();
  344. const int first_column = clipped_rect.left() - dst_rect.left();
  345. RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x();
  346. const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
  347. if (source.format() == GraphicsBitmap::Format::RGB32 || source.format() == GraphicsBitmap::Format::RGBA32) {
  348. const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
  349. const size_t src_skip = source.pitch() / sizeof(RGBA32);
  350. for (int row = first_row; row <= last_row; ++row) {
  351. fast_dword_copy(dst, src, clipped_rect.width());
  352. dst += dst_skip;
  353. src += src_skip;
  354. }
  355. return;
  356. }
  357. if (source.format() == GraphicsBitmap::Format::Indexed8) {
  358. const byte* src = source.bits(src_rect.top() + first_row) + src_rect.left() + first_column;
  359. const size_t src_skip = source.pitch();
  360. for (int row = first_row; row <= last_row; ++row) {
  361. for (int i = 0; i < clipped_rect.width(); ++i)
  362. dst[i] = source.palette_color(src[i]).value();
  363. dst += dst_skip;
  364. src += src_skip;
  365. }
  366. return;
  367. }
  368. ASSERT_NOT_REACHED();
  369. }
  370. template<bool has_alpha_channel, typename GetPixel>
  371. 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)
  372. {
  373. for (int y = source.rect().top(); y <= source.rect().bottom(); ++y) {
  374. int dst_y = dst_rect.y() + y * vfactor;
  375. for (int x = source.rect().left(); x <= source.rect().right(); ++x) {
  376. auto src_pixel = get_pixel(source, x, y);
  377. for (int yo = 0; yo < vfactor; ++yo) {
  378. auto* scanline = (Color*)target.scanline(dst_y + yo);
  379. int dst_x = dst_rect.x() + x * hfactor;
  380. for (int xo = 0; xo < hfactor; ++xo) {
  381. if constexpr (has_alpha_channel)
  382. scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel);
  383. else
  384. scanline[dst_x + xo] = src_pixel;
  385. }
  386. }
  387. }
  388. }
  389. }
  390. template<bool has_alpha_channel, typename GetPixel>
  391. 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)
  392. {
  393. if (dst_rect == clipped_rect && !(dst_rect.width() % src_rect.width()) && !(dst_rect.height() % src_rect.height())) {
  394. int hfactor = dst_rect.width() / src_rect.width();
  395. int vfactor = dst_rect.height() / src_rect.height();
  396. if (hfactor == 2 && vfactor == 2)
  397. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 2, 2, get_pixel);
  398. if (hfactor == 3 && vfactor == 3)
  399. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 3, 3, get_pixel);
  400. if (hfactor == 4 && vfactor == 4)
  401. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 4, 4, get_pixel);
  402. return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, hfactor, vfactor, get_pixel);
  403. }
  404. for (int y = clipped_rect.top(); y <= clipped_rect.bottom(); ++y) {
  405. auto* scanline = (Color*)target.scanline(y);
  406. for (int x = clipped_rect.left(); x <= clipped_rect.right(); ++x) {
  407. auto scaled_x = ((x - dst_rect.x()) * hscale) >> 16;
  408. auto scaled_y = ((y - dst_rect.y()) * vscale) >> 16;
  409. auto src_pixel = get_pixel(source, scaled_x, scaled_y);
  410. if constexpr (has_alpha_channel) {
  411. scanline[x] = scanline[x].blend(src_pixel);
  412. } else
  413. scanline[x] = src_pixel;
  414. }
  415. }
  416. }
  417. void Painter::draw_scaled_bitmap(const Rect& a_dst_rect, const GraphicsBitmap& source, const Rect& src_rect)
  418. {
  419. auto dst_rect = a_dst_rect;
  420. if (dst_rect.size() == src_rect.size())
  421. return blit(dst_rect.location(), source, src_rect);
  422. auto safe_src_rect = src_rect.intersected(source.rect());
  423. ASSERT(source.rect().contains(safe_src_rect));
  424. dst_rect.move_by(state().translation);
  425. auto clipped_rect = dst_rect.intersected(clip_rect());
  426. if (clipped_rect.is_empty())
  427. return;
  428. int hscale = (src_rect.width() << 16) / dst_rect.width();
  429. int vscale = (src_rect.height() << 16) / dst_rect.height();
  430. if (source.has_alpha_channel()) {
  431. switch (source.format()) {
  432. case GraphicsBitmap::Format::RGB32:
  433. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::RGB32>);
  434. break;
  435. case GraphicsBitmap::Format::RGBA32:
  436. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::RGB32>);
  437. break;
  438. case GraphicsBitmap::Format::Indexed8:
  439. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::Indexed8>);
  440. break;
  441. default:
  442. do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::Invalid>);
  443. break;
  444. }
  445. } else {
  446. switch (source.format()) {
  447. case GraphicsBitmap::Format::RGB32:
  448. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::RGB32>);
  449. break;
  450. case GraphicsBitmap::Format::RGBA32:
  451. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::RGB32>);
  452. break;
  453. case GraphicsBitmap::Format::Indexed8:
  454. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::Indexed8>);
  455. break;
  456. default:
  457. do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<GraphicsBitmap::Format::Invalid>);
  458. break;
  459. }
  460. }
  461. }
  462. [[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, Color color)
  463. {
  464. draw_glyph(point, ch, font(), color);
  465. }
  466. [[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, const Font& font, Color color)
  467. {
  468. draw_bitmap(point, font.glyph_bitmap(ch), color);
  469. }
  470. void Painter::draw_text(const Rect& rect, const StringView& text, TextAlignment alignment, Color color, TextElision elision)
  471. {
  472. draw_text(rect, text, font(), alignment, color, elision);
  473. }
  474. void Painter::draw_text(const Rect& rect, const StringView& text, const Font& font, TextAlignment alignment, Color color, TextElision elision)
  475. {
  476. StringView final_text(text);
  477. String elided_text;
  478. if (elision == TextElision::Right) {
  479. int text_width = font.width(final_text);
  480. if (font.width(final_text) > rect.width()) {
  481. int glyph_spacing = font.glyph_spacing();
  482. int new_length = 0;
  483. int new_width = font.width("...");
  484. if (new_width < text_width) {
  485. for (int i = 0; i < final_text.length(); ++i) {
  486. int glyph_width = font.glyph_width(final_text.characters()[i]);
  487. // NOTE: Glyph spacing should not be added after the last glyph on the line,
  488. // but since we are here because the last glyph does not actually fit on the line,
  489. // we don't have to worry about spacing.
  490. int width_with_this_glyph_included = new_width + glyph_width + glyph_spacing;
  491. if (width_with_this_glyph_included > rect.width())
  492. break;
  493. ++new_length;
  494. new_width += glyph_width + glyph_spacing;
  495. }
  496. StringBuilder builder;
  497. builder.append(StringView(final_text.characters(), new_length));
  498. builder.append("...");
  499. elided_text = builder.to_string();
  500. final_text = elided_text;
  501. }
  502. }
  503. }
  504. Point point;
  505. if (alignment == TextAlignment::TopLeft) {
  506. point = rect.location();
  507. } else if (alignment == TextAlignment::CenterLeft) {
  508. point = { rect.x(), rect.center().y() - (font.glyph_height() / 2) };
  509. } else if (alignment == TextAlignment::CenterRight) {
  510. int text_width = font.width(final_text);
  511. point = { rect.right() - text_width, rect.center().y() - (font.glyph_height() / 2) };
  512. } else if (alignment == TextAlignment::Center) {
  513. int text_width = font.width(final_text);
  514. point = rect.center();
  515. point.move_by(-(text_width / 2), -(font.glyph_height() / 2));
  516. } else {
  517. ASSERT_NOT_REACHED();
  518. }
  519. int space_width = font.glyph_width(' ') + font.glyph_spacing();
  520. for (ssize_t i = 0; i < final_text.length(); ++i) {
  521. char ch = final_text.characters()[i];
  522. if (ch == ' ') {
  523. point.move_by(space_width, 0);
  524. continue;
  525. }
  526. draw_glyph(point, ch, font, color);
  527. point.move_by(font.glyph_width(ch) + font.glyph_spacing(), 0);
  528. }
  529. }
  530. void Painter::set_pixel(const Point& p, Color color)
  531. {
  532. auto point = p;
  533. point.move_by(state().translation);
  534. if (!clip_rect().contains(point))
  535. return;
  536. m_target->scanline(point.y())[point.x()] = color.value();
  537. }
  538. [[gnu::always_inline]] inline void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
  539. {
  540. if (draw_op() == DrawOp::Copy)
  541. pixel = color.value();
  542. else if (draw_op() == DrawOp::Xor)
  543. pixel ^= color.value();
  544. }
  545. void Painter::draw_line(const Point& p1, const Point& p2, Color color)
  546. {
  547. auto clip_rect = this->clip_rect();
  548. auto point1 = p1;
  549. point1.move_by(state().translation);
  550. auto point2 = p2;
  551. point2.move_by(state().translation);
  552. // Special case: vertical line.
  553. if (point1.x() == point2.x()) {
  554. const int x = point1.x();
  555. if (x < clip_rect.left() || x > clip_rect.right())
  556. return;
  557. if (point1.y() > point2.y())
  558. swap(point1, point2);
  559. if (point1.y() > clip_rect.bottom())
  560. return;
  561. if (point2.y() < clip_rect.top())
  562. return;
  563. int min_y = max(point1.y(), clip_rect.top());
  564. int max_y = min(point2.y(), clip_rect.bottom());
  565. for (int y = min_y; y <= max_y; ++y)
  566. set_pixel_with_draw_op(m_target->scanline(y)[x], color);
  567. return;
  568. }
  569. // Special case: horizontal line.
  570. if (point1.y() == point2.y()) {
  571. const int y = point1.y();
  572. if (y < clip_rect.top() || y > clip_rect.bottom())
  573. return;
  574. if (point1.x() > point2.x())
  575. swap(point1, point2);
  576. if (point1.x() > clip_rect.right())
  577. return;
  578. if (point2.x() < clip_rect.left())
  579. return;
  580. int min_x = max(point1.x(), clip_rect.left());
  581. int max_x = min(point2.x(), clip_rect.right());
  582. auto* pixels = m_target->scanline(point1.y());
  583. if (draw_op() == DrawOp::Copy) {
  584. fast_dword_fill(pixels + min_x, color.value(), max_x - min_x + 1);
  585. } else {
  586. for (int x = min_x; x <= max_x; ++x)
  587. set_pixel_with_draw_op(pixels[x], color);
  588. }
  589. return;
  590. }
  591. const double adx = abs(point2.x() - point1.x());
  592. const double ady = abs(point2.y() - point1.y());
  593. if (adx > ady) {
  594. if (point1.x() > point2.x())
  595. swap(point1, point2);
  596. } else {
  597. if (point1.y() > point2.y())
  598. swap(point1, point2);
  599. }
  600. // FIXME: Implement clipping below.
  601. const double dx = point2.x() - point1.x();
  602. const double dy = point2.y() - point1.y();
  603. double error = 0;
  604. if (dx > dy) {
  605. const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
  606. const double delta_error = fabs(dy / dx);
  607. int y = point1.y();
  608. for (int x = point1.x(); x <= point2.x(); ++x) {
  609. if (clip_rect.contains(x, y))
  610. m_target->scanline(y)[x] = color.value();
  611. error += delta_error;
  612. if (error >= 0.5) {
  613. y = (double)y + y_step;
  614. error -= 1.0;
  615. }
  616. }
  617. } else {
  618. const double x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1);
  619. const double delta_error = fabs(dx / dy);
  620. int x = point1.x();
  621. for (int y = point1.y(); y <= point2.y(); ++y) {
  622. if (clip_rect.contains(x, y))
  623. m_target->scanline(y)[x] = color.value();
  624. error += delta_error;
  625. if (error >= 0.5) {
  626. x = (double)x + x_step;
  627. error -= 1.0;
  628. }
  629. }
  630. }
  631. }
  632. void Painter::add_clip_rect(const Rect& rect)
  633. {
  634. state().clip_rect.intersect(rect.translated(m_clip_origin.location()));
  635. state().clip_rect.intersect(m_target->rect());
  636. }
  637. void Painter::clear_clip_rect()
  638. {
  639. state().clip_rect = m_clip_origin;
  640. }
  641. PainterStateSaver::PainterStateSaver(Painter& painter)
  642. : m_painter(painter)
  643. {
  644. m_painter.save();
  645. }
  646. PainterStateSaver::~PainterStateSaver()
  647. {
  648. m_painter.restore();
  649. }