PathRasterizer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
  3. * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Font/PathRasterizer.h>
  8. namespace Gfx {
  9. PathRasterizer::PathRasterizer(Gfx::IntSize size)
  10. : m_size(size)
  11. {
  12. m_data.resize(m_size.area());
  13. for (int i = 0; i < m_size.area(); i++)
  14. m_data[i] = 0.f;
  15. }
  16. void PathRasterizer::draw_path(Gfx::Path& path)
  17. {
  18. for (auto& line : path.split_lines())
  19. draw_line(line.from, line.to);
  20. }
  21. RefPtr<Gfx::Bitmap> PathRasterizer::accumulate()
  22. {
  23. auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, m_size);
  24. if (bitmap_or_error.is_error())
  25. return {};
  26. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  27. Color base_color = Color::from_rgb(0xffffff);
  28. for (int y = 0; y < m_size.height(); y++) {
  29. float accumulator = 0.f;
  30. for (int x = 0; x < m_size.width(); x++) {
  31. accumulator += m_data[y * m_size.width() + x];
  32. float value = AK::min(AK::abs(accumulator), 1.f);
  33. u8 alpha = value * 255.f;
  34. bitmap->set_pixel(x, y, base_color.with_alpha(alpha));
  35. }
  36. }
  37. return bitmap;
  38. }
  39. void PathRasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
  40. {
  41. // FIXME: Shift x and y according to dy/dx
  42. if (p0.x() < 0.f)
  43. p0.set_x(roundf(p0.x()));
  44. if (p0.y() < 0.f)
  45. p0.set_y(roundf(p0.y()));
  46. if (p1.x() < 0.f)
  47. p1.set_x(roundf(p1.x()));
  48. if (p1.y() < 0.f)
  49. p1.set_y(roundf(p1.y()));
  50. if (p0.x() < 0.f || p0.y() < 0.f || p0.x() > m_size.width() || p0.y() > m_size.height()) {
  51. dbgln("!P0({},{})", p0.x(), p0.y());
  52. return;
  53. }
  54. if (p1.x() < 0.f || p1.y() < 0.f || p1.x() > m_size.width() || p1.y() > m_size.height()) {
  55. dbgln("!P1({},{})", p1.x(), p1.y());
  56. return;
  57. }
  58. // If we're on the same Y, there's no need to draw
  59. if (p0.y() == p1.y())
  60. return;
  61. float direction = -1.f;
  62. if (p1.y() < p0.y()) {
  63. direction = 1.f;
  64. AK::swap(p0, p1);
  65. }
  66. float const dxdy = (p1.x() - p0.x()) / (p1.y() - p0.y());
  67. float const dydx = AK::abs(1.f / dxdy);
  68. u32 y0 = floorf(p0.y());
  69. u32 y1 = ceilf(p1.y());
  70. float x_cur = p0.x();
  71. for (u32 y = y0; y < y1; y++) {
  72. u32 line_offset = m_size.width() * y;
  73. float dy = AK::min(y + 1.f, p1.y()) - AK::max(static_cast<float>(y), p0.y());
  74. float directed_dy = dy * direction;
  75. float x_next = x_cur + dy * dxdy;
  76. x_next = AK::max(x_next, 0.f);
  77. float x0 = x_cur;
  78. float x1 = x_next;
  79. if (x1 < x0) {
  80. x1 = x_cur;
  81. x0 = x_next;
  82. }
  83. float x0_floor = floorf(x0);
  84. float x1_ceil = ceilf(x1);
  85. u32 x0_floor_i = x0_floor;
  86. if (x1_ceil <= x0_floor + 1.f) {
  87. // If x0 and x1 are within the same pixel, then area to the right is (1 - (mid(x0, x1) - x0_floor)) * dy
  88. float area = .5f * (x0 + x1) - x0_floor;
  89. m_data[line_offset + x0_floor_i] += directed_dy * (1.f - area);
  90. if (x0_floor_i + 1 < static_cast<u32>(m_size.width()))
  91. m_data[line_offset + x0_floor_i + 1] += directed_dy * area;
  92. } else {
  93. float x0_right = 1.f - (x0 - x0_floor);
  94. u32 x1_floor_i = floorf(x1);
  95. float area_upto_here = .5f * x0_right * x0_right * dydx;
  96. m_data[line_offset + x0_floor_i] += direction * area_upto_here;
  97. for (u32 x = x0_floor_i + 1; x < x1_floor_i; x++) {
  98. m_data[line_offset + x] += direction * dydx;
  99. area_upto_here += dydx;
  100. }
  101. float remaining_area = dy - area_upto_here;
  102. m_data[line_offset + x1_floor_i] += direction * remaining_area;
  103. }
  104. x_cur = x_next;
  105. }
  106. }
  107. }