PathRasterizer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Font/PathRasterizer.h>
  7. namespace Gfx {
  8. PathRasterizer::PathRasterizer(Gfx::IntSize size)
  9. : m_size(size)
  10. {
  11. m_data.resize(m_size.width() * m_size.height());
  12. for (int i = 0; i < m_size.width() * m_size.height(); i++) {
  13. m_data[i] = 0.0f;
  14. }
  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::try_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.0;
  30. for (int x = 0; x < m_size.width(); x++) {
  31. accumulator += m_data[y * m_size.width() + x];
  32. float value = accumulator;
  33. if (value < 0.0f) {
  34. value = -value;
  35. }
  36. if (value > 1.0f) {
  37. value = 1.0;
  38. }
  39. u8 alpha = value * 255.0f;
  40. bitmap->set_pixel(x, y, base_color.with_alpha(alpha));
  41. }
  42. }
  43. return bitmap;
  44. }
  45. void PathRasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
  46. {
  47. // FIXME: Shift x and y according to dy/dx
  48. if (p0.x() < 0.0f) {
  49. p0.set_x(roundf(p0.x()));
  50. }
  51. if (p0.y() < 0.0f) {
  52. p0.set_y(roundf(p0.y()));
  53. }
  54. if (p1.x() < 0.0f) {
  55. p1.set_x(roundf(p1.x()));
  56. }
  57. if (p1.y() < 0.0f) {
  58. p1.set_y(roundf(p1.y()));
  59. }
  60. if (!(p0.x() >= 0.0f && p0.y() >= 0.0f && p0.x() <= m_size.width() && p0.y() <= m_size.height())) {
  61. dbgln("!P0({},{})", p0.x(), p0.y());
  62. return;
  63. }
  64. if (!(p1.x() >= 0.0f && p1.y() >= 0.0f && p1.x() <= m_size.width() && p1.y() <= m_size.height())) {
  65. dbgln("!P1({},{})", p1.x(), p1.y());
  66. return;
  67. }
  68. VERIFY(p0.x() >= 0.0f && p0.y() >= 0.0f && p0.x() <= m_size.width() && p0.y() <= m_size.height());
  69. VERIFY(p1.x() >= 0.0f && p1.y() >= 0.0f && p1.x() <= m_size.width() && p1.y() <= m_size.height());
  70. // If we're on the same Y, there's no need to draw
  71. if (p0.y() == p1.y()) {
  72. return;
  73. }
  74. float direction = -1.0;
  75. if (p1.y() < p0.y()) {
  76. direction = 1.0;
  77. auto tmp = p0;
  78. p0 = p1;
  79. p1 = tmp;
  80. }
  81. float dxdy = (p1.x() - p0.x()) / (p1.y() - p0.y());
  82. u32 y0 = floorf(p0.y());
  83. u32 y1 = ceilf(p1.y());
  84. float x_cur = p0.x();
  85. for (u32 y = y0; y < y1; y++) {
  86. u32 line_offset = m_size.width() * y;
  87. float dy = min(y + 1.0f, p1.y()) - max((float)y, p0.y());
  88. float directed_dy = dy * direction;
  89. float x_next = x_cur + dy * dxdy;
  90. if (x_next < 0.0f) {
  91. x_next = 0.0f;
  92. }
  93. float x0 = x_cur;
  94. float x1 = x_next;
  95. if (x1 < x0) {
  96. x1 = x_cur;
  97. x0 = x_next;
  98. }
  99. float x0_floor = floorf(x0);
  100. float x1_ceil = ceilf(x1);
  101. u32 x0i = x0_floor;
  102. if (x1_ceil <= x0_floor + 1.0f) {
  103. // If x0 and x1 are within the same pixel, then area to the right is (1 - (mid(x0, x1) - x0_floor)) * dy
  104. float area = ((x0 + x1) * 0.5f) - x0_floor;
  105. m_data[line_offset + x0i] += directed_dy * (1.0f - area);
  106. m_data[line_offset + x0i + 1] += directed_dy * area;
  107. } else {
  108. float dydx = 1.0f / dxdy;
  109. if (dydx < 0)
  110. dydx = -dydx;
  111. float x0_right = 1.0f - (x0 - x0_floor);
  112. u32 x1_floor_i = floorf(x1);
  113. float area_upto_here = 0.5f * x0_right * x0_right * dydx;
  114. m_data[line_offset + x0i] += direction * area_upto_here;
  115. for (u32 x = x0i + 1; x < x1_floor_i; x++) {
  116. m_data[line_offset + x] += direction * dydx;
  117. area_upto_here += dydx;
  118. }
  119. float remaining_area = (dy - area_upto_here);
  120. m_data[line_offset + x1_floor_i] += direction * remaining_area;
  121. }
  122. x_cur = x_next;
  123. }
  124. }
  125. }