GlyphEditorWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "GlyphEditorWidget.h"
  8. #include <LibGUI/Painter.h>
  9. #include <LibGfx/Font/BitmapFont.h>
  10. #include <LibGfx/Palette.h>
  11. REGISTER_WIDGET(FontEditor, GlyphEditorWidget);
  12. namespace FontEditor {
  13. void GlyphEditorWidget::initialize(Gfx::BitmapFont* mutable_font)
  14. {
  15. if (m_font == mutable_font)
  16. return;
  17. m_font = mutable_font;
  18. update();
  19. }
  20. void GlyphEditorWidget::set_glyph(int glyph)
  21. {
  22. if (m_glyph == glyph)
  23. return;
  24. m_glyph = glyph;
  25. update();
  26. }
  27. void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
  28. {
  29. if (!m_font)
  30. return;
  31. GUI::Frame::paint_event(event);
  32. GUI::Painter painter(*this);
  33. painter.add_clip_rect(frame_inner_rect());
  34. painter.add_clip_rect(event.rect());
  35. painter.fill_rect(frame_inner_rect(), palette().base());
  36. painter.translate(frame_thickness(), frame_thickness());
  37. painter.translate(-1, -1);
  38. for (int y = 1; y < m_font->glyph_height(); ++y) {
  39. int y_below = y - 1;
  40. bool bold_line = y_below == m_font->baseline() || y_below == m_font->mean_line();
  41. painter.draw_line({ 0, y * m_scale }, { m_font->max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
  42. }
  43. for (int x = 1; x < m_font->max_glyph_width(); ++x)
  44. painter.draw_line({ x * m_scale, 0 }, { x * m_scale, m_font->glyph_height() * m_scale }, palette().threed_shadow2());
  45. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  46. for (int y = 0; y < m_font->glyph_height(); ++y) {
  47. for (int x = 0; x < m_font->max_glyph_width(); ++x) {
  48. Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
  49. if (x >= m_font->raw_glyph_width(m_glyph)) {
  50. painter.fill_rect(rect, palette().threed_shadow1());
  51. } else {
  52. if (bitmap.bit_at(x, y))
  53. painter.fill_rect(rect, palette().base_text());
  54. }
  55. }
  56. }
  57. }
  58. bool GlyphEditorWidget::is_glyph_empty()
  59. {
  60. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  61. for (int x = 0; x < m_font->max_glyph_width(); x++)
  62. for (int y = 0; y < m_font->glyph_height(); y++)
  63. if (bitmap.bit_at(x, y))
  64. return false;
  65. return true;
  66. }
  67. void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
  68. {
  69. if ((event.x() - 1) / m_scale + 1 > m_font->raw_glyph_width(m_glyph))
  70. return;
  71. if (mode() == Move && is_glyph_empty())
  72. return;
  73. m_is_clicking_valid_cell = true;
  74. if (mode() == Paint) {
  75. draw_at_mouse(event);
  76. } else {
  77. memset(m_movable_bits, 0, sizeof(m_movable_bits));
  78. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  79. for (int x = 0; x < bitmap.width(); x++) {
  80. for (int y = 0; y < bitmap.height(); y++) {
  81. int movable_x = Gfx::GlyphBitmap::max_width() + x;
  82. int movable_y = Gfx::GlyphBitmap::max_height() + y;
  83. m_movable_bits[movable_x][movable_y] = bitmap.bit_at(x, y);
  84. }
  85. }
  86. m_scaled_offset_x = (event.x() - 1) / m_scale;
  87. m_scaled_offset_y = (event.y() - 1) / m_scale;
  88. move_at_mouse(event);
  89. }
  90. }
  91. void GlyphEditorWidget::mouseup_event(GUI::MouseEvent&)
  92. {
  93. if (!m_is_clicking_valid_cell)
  94. return;
  95. m_is_clicking_valid_cell = false;
  96. }
  97. void GlyphEditorWidget::mousemove_event(GUI::MouseEvent& event)
  98. {
  99. if (!m_is_clicking_valid_cell)
  100. return;
  101. if (!(event.buttons() & (GUI::MouseButton::Primary | GUI::MouseButton::Secondary)))
  102. return;
  103. if (mode() == Paint)
  104. draw_at_mouse(event);
  105. else
  106. move_at_mouse(event);
  107. }
  108. void GlyphEditorWidget::enter_event(Core::Event&)
  109. {
  110. if (mode() == Move)
  111. set_override_cursor(Gfx::StandardCursor::Move);
  112. else
  113. set_override_cursor(Gfx::StandardCursor::None);
  114. }
  115. void GlyphEditorWidget::draw_at_mouse(GUI::MouseEvent const& event)
  116. {
  117. bool set = event.buttons() & GUI::MouseButton::Primary;
  118. bool unset = event.buttons() & GUI::MouseButton::Secondary;
  119. if (!(set ^ unset))
  120. return;
  121. int x = (event.x() - 1) / m_scale;
  122. int y = (event.y() - 1) / m_scale;
  123. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  124. if (x < 0 || x >= bitmap.width())
  125. return;
  126. if (y < 0 || y >= bitmap.height())
  127. return;
  128. if (bitmap.bit_at(x, y) == set)
  129. return;
  130. if (on_undo_event && event.type() == GUI::MouseEvent::MouseDown)
  131. on_undo_event("Paint Glyph"sv);
  132. bitmap.set_bit_at(x, y, set);
  133. if (on_glyph_altered)
  134. on_glyph_altered(m_glyph);
  135. update();
  136. }
  137. void GlyphEditorWidget::move_at_mouse(GUI::MouseEvent const& event)
  138. {
  139. if (on_undo_event && event.type() == GUI::MouseEvent::MouseDown)
  140. on_undo_event("Move Glyph"sv);
  141. int x_delta = ((event.x() - 1) / m_scale) - m_scaled_offset_x;
  142. int y_delta = ((event.y() - 1) / m_scale) - m_scaled_offset_y;
  143. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  144. if (abs(x_delta) > bitmap.width() || abs(y_delta) > bitmap.height())
  145. return;
  146. for (int x = 0; x < bitmap.width(); x++) {
  147. for (int y = 0; y < bitmap.height(); y++) {
  148. int movable_x = Gfx::GlyphBitmap::max_width() + x - x_delta;
  149. int movable_y = Gfx::GlyphBitmap::max_height() + y - y_delta;
  150. bitmap.set_bit_at(x, y, m_movable_bits[movable_x][movable_y]);
  151. }
  152. }
  153. if (on_glyph_altered)
  154. on_glyph_altered(m_glyph);
  155. update();
  156. }
  157. static Vector<Vector<u8>> glyph_as_matrix(Gfx::GlyphBitmap const& bitmap)
  158. {
  159. Vector<Vector<u8>> result;
  160. result.ensure_capacity(bitmap.height());
  161. for (int y = 0; y < bitmap.height(); y++) {
  162. result.empend();
  163. auto& row = result.last();
  164. row.ensure_capacity(bitmap.width());
  165. for (int x = 0; x < bitmap.width(); x++) {
  166. row.append(bitmap.bit_at(x, y));
  167. }
  168. }
  169. return result;
  170. }
  171. void GlyphEditorWidget::rotate_90(Gfx::RotationDirection direction)
  172. {
  173. auto clockwise = direction == Gfx::RotationDirection::Clockwise;
  174. auto action_text = clockwise ? "Rotate Glyph Clockwise"sv : "Rotate Glyph Counterclockwise"sv;
  175. if (on_undo_event)
  176. on_undo_event(action_text);
  177. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  178. auto matrix = glyph_as_matrix(bitmap);
  179. for (int y = 0; y < bitmap.height(); y++) {
  180. for (int x = 0; x < bitmap.width(); x++) {
  181. int source_x = clockwise ? y : max(bitmap.width() - 1 - y, 0);
  182. int source_y = clockwise ? bitmap.width() - 1 - x : x;
  183. bool value = false;
  184. if (source_x < bitmap.width() && source_y < bitmap.height()) {
  185. value = (!clockwise && y >= bitmap.width()) ? false : matrix[source_y][source_x];
  186. }
  187. bitmap.set_bit_at(x, y, value);
  188. }
  189. }
  190. if (on_glyph_altered)
  191. on_glyph_altered(m_glyph);
  192. update();
  193. }
  194. void GlyphEditorWidget::flip(Gfx::Orientation orientation)
  195. {
  196. auto vertical = orientation == Gfx::Orientation::Vertical;
  197. auto action_text = vertical ? "Flip Glyph Vertically"sv : "Flip Glyph Horizontally"sv;
  198. if (on_undo_event)
  199. on_undo_event(action_text);
  200. auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
  201. auto matrix = glyph_as_matrix(bitmap);
  202. for (int y = 0; y < bitmap.height(); y++) {
  203. for (int x = 0; x < bitmap.width(); x++) {
  204. int source_x = vertical ? x : bitmap.width() - 1 - x;
  205. int source_y = vertical ? bitmap.height() - 1 - y : y;
  206. bool value = matrix[source_y][source_x];
  207. bitmap.set_bit_at(x, y, value);
  208. }
  209. }
  210. if (on_glyph_altered)
  211. on_glyph_altered(m_glyph);
  212. update();
  213. }
  214. int GlyphEditorWidget::preferred_width() const
  215. {
  216. return frame_thickness() * 2 + m_font->max_glyph_width() * m_scale - 1;
  217. }
  218. int GlyphEditorWidget::preferred_height() const
  219. {
  220. return frame_thickness() * 2 + m_font->glyph_height() * m_scale - 1;
  221. }
  222. void GlyphEditorWidget::set_scale(int scale)
  223. {
  224. if (m_scale == scale)
  225. return;
  226. m_scale = clamp(scale, 1, 15);
  227. update();
  228. }
  229. }