GlyphEditorWidget.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GlyphEditorWidget.h"
  7. #include <AK/StringBuilder.h>
  8. #include <LibGUI/Clipboard.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/BitmapFont.h>
  11. #include <LibGfx/Palette.h>
  12. static int x_offset;
  13. static int y_offset;
  14. GlyphEditorWidget::~GlyphEditorWidget()
  15. {
  16. }
  17. void GlyphEditorWidget::initialize(Gfx::BitmapFont& mutable_font)
  18. {
  19. if (m_font == mutable_font)
  20. return;
  21. m_font = mutable_font;
  22. set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
  23. m_glyph = 0;
  24. }
  25. void GlyphEditorWidget::set_glyph(int glyph)
  26. {
  27. if (m_glyph == glyph)
  28. return;
  29. m_glyph = glyph;
  30. update();
  31. }
  32. void GlyphEditorWidget::delete_glyph()
  33. {
  34. if (on_undo_event)
  35. on_undo_event();
  36. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  37. for (int x = 0; x < bitmap.width(); x++)
  38. for (int y = 0; y < bitmap.height(); y++)
  39. bitmap.set_bit_at(x, y, false);
  40. if (on_glyph_altered)
  41. on_glyph_altered(m_glyph);
  42. update();
  43. }
  44. void GlyphEditorWidget::cut_glyph()
  45. {
  46. copy_glyph();
  47. delete_glyph();
  48. }
  49. void GlyphEditorWidget::copy_glyph()
  50. {
  51. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  52. u8 bits[bitmap.width()][bitmap.height()];
  53. for (int x = 0; x < bitmap.width(); x++) {
  54. for (int y = 0; y < bitmap.height(); y++) {
  55. bits[x][y] = bitmap.bit_at(x, y);
  56. }
  57. }
  58. StringBuilder glyph_builder;
  59. if (m_glyph < 128) {
  60. if (m_glyph == 10)
  61. glyph_builder.append("LF");
  62. else
  63. glyph_builder.append(m_glyph);
  64. } else {
  65. glyph_builder.append(128 | 64 | (m_glyph / 64));
  66. glyph_builder.append(128 | (m_glyph % 64));
  67. }
  68. HashMap<String, String> metadata;
  69. metadata.set("char", glyph_builder.to_string());
  70. metadata.set("width", String::number(bitmap.width()));
  71. metadata.set("height", String::number(bitmap.height()));
  72. GUI::Clipboard::the().set_data(ReadonlyBytes(&bits[0], bitmap.width() * bitmap.height()), "glyph/x-fonteditor", metadata);
  73. }
  74. void GlyphEditorWidget::paste_glyph()
  75. {
  76. auto mime_type = GUI::Clipboard::the().mime_type();
  77. if (!mime_type.starts_with("glyph/"))
  78. return;
  79. if (on_undo_event)
  80. on_undo_event();
  81. auto byte_buffer = GUI::Clipboard::the().data();
  82. auto buffer_height = GUI::Clipboard::the().data_and_type().metadata.get("height").value().to_int();
  83. auto buffer_width = GUI::Clipboard::the().data_and_type().metadata.get("width").value().to_int();
  84. u8 bits[buffer_width.value()][buffer_height.value()];
  85. int i = 0;
  86. for (int x = 0; x < buffer_width.value(); x++) {
  87. for (int y = 0; y < buffer_height.value(); y++) {
  88. bits[x][y] = byte_buffer[i];
  89. i++;
  90. }
  91. }
  92. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  93. for (int x = 0; x < min(bitmap.width(), buffer_width.value()); x++) {
  94. for (int y = 0; y < min(bitmap.height(), buffer_height.value()); y++) {
  95. if (bits[x][y])
  96. bitmap.set_bit_at(x, y, bits[x][y]);
  97. }
  98. }
  99. if (on_glyph_altered)
  100. on_glyph_altered(m_glyph);
  101. update();
  102. }
  103. void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
  104. {
  105. GUI::Frame::paint_event(event);
  106. GUI::Painter painter(*this);
  107. painter.add_clip_rect(frame_inner_rect());
  108. painter.add_clip_rect(event.rect());
  109. painter.fill_rect(frame_inner_rect(), palette().base());
  110. painter.translate(frame_thickness(), frame_thickness());
  111. painter.translate(-1, -1);
  112. for (int y = 1; y < font().glyph_height(); ++y) {
  113. int y_below = y - 1;
  114. bool bold_line = y_below == font().baseline() || y_below == font().mean_line();
  115. painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
  116. }
  117. for (int x = 1; x < font().max_glyph_width(); ++x)
  118. painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2());
  119. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  120. for (int y = 0; y < font().glyph_height(); ++y) {
  121. for (int x = 0; x < font().max_glyph_width(); ++x) {
  122. Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
  123. if (x >= font().raw_glyph_width(m_glyph)) {
  124. painter.fill_rect(rect, palette().threed_shadow1());
  125. } else {
  126. if (bitmap.bit_at(x, y))
  127. painter.fill_rect(rect, palette().base_text());
  128. }
  129. }
  130. }
  131. }
  132. bool GlyphEditorWidget::is_glyph_empty()
  133. {
  134. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  135. for (int x = 0; x < bitmap.width(); x++)
  136. for (int y = 0; y < bitmap.height(); y++)
  137. if (bitmap.bit_at(x, y))
  138. return false;
  139. return true;
  140. }
  141. void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
  142. {
  143. if ((event.x() - 1) / m_scale + 1 > font().raw_glyph_width(m_glyph))
  144. return;
  145. if (mode() == Move && is_glyph_empty())
  146. return;
  147. m_is_clicking_valid_cell = true;
  148. if (on_undo_event)
  149. on_undo_event();
  150. if (mode() == Paint) {
  151. draw_at_mouse(event);
  152. } else {
  153. memset(m_movable_bits, 0, sizeof(m_movable_bits));
  154. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  155. for (int x = s_max_width; x < s_max_width + bitmap.width(); x++)
  156. for (int y = s_max_height; y < s_max_height + bitmap.height(); y++)
  157. m_movable_bits[x][y] = bitmap.bit_at(x - s_max_width, y - s_max_height);
  158. x_offset = (event.x() - 1) / m_scale;
  159. y_offset = (event.y() - 1) / m_scale;
  160. move_at_mouse(event);
  161. }
  162. }
  163. void GlyphEditorWidget::mouseup_event(GUI::MouseEvent&)
  164. {
  165. if (!m_is_clicking_valid_cell)
  166. return;
  167. m_is_clicking_valid_cell = false;
  168. }
  169. void GlyphEditorWidget::mousemove_event(GUI::MouseEvent& event)
  170. {
  171. if (!m_is_clicking_valid_cell)
  172. return;
  173. if (!(event.buttons() & (GUI::MouseButton::Left | GUI::MouseButton::Right)))
  174. return;
  175. if (mode() == Paint)
  176. draw_at_mouse(event);
  177. else
  178. move_at_mouse(event);
  179. }
  180. void GlyphEditorWidget::enter_event(Core::Event&)
  181. {
  182. if (mode() == Move)
  183. set_override_cursor(Gfx::StandardCursor::Move);
  184. else
  185. set_override_cursor(Gfx::StandardCursor::None);
  186. }
  187. void GlyphEditorWidget::draw_at_mouse(const GUI::MouseEvent& event)
  188. {
  189. bool set = event.buttons() & GUI::MouseButton::Left;
  190. bool unset = event.buttons() & GUI::MouseButton::Right;
  191. if (!(set ^ unset))
  192. return;
  193. int x = (event.x() - 1) / m_scale;
  194. int y = (event.y() - 1) / m_scale;
  195. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  196. if (x < 0 || x >= bitmap.width())
  197. return;
  198. if (y < 0 || y >= bitmap.height())
  199. return;
  200. if (bitmap.bit_at(x, y) == set)
  201. return;
  202. bitmap.set_bit_at(x, y, set);
  203. if (on_glyph_altered)
  204. on_glyph_altered(m_glyph);
  205. update();
  206. }
  207. void GlyphEditorWidget::move_at_mouse(const GUI::MouseEvent& event)
  208. {
  209. int x_delta = ((event.x() - 1) / m_scale) - x_offset;
  210. int y_delta = ((event.y() - 1) / m_scale) - y_offset;
  211. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  212. if (abs(x_delta) > bitmap.width() || abs(y_delta) > bitmap.height())
  213. return;
  214. for (int x = 0; x < bitmap.width(); x++) {
  215. for (int y = 0; y < bitmap.height(); y++) {
  216. bitmap.set_bit_at(x, y, m_movable_bits[s_max_width + x - x_delta][s_max_height + y - y_delta]);
  217. }
  218. }
  219. if (on_glyph_altered)
  220. on_glyph_altered(m_glyph);
  221. update();
  222. }
  223. int GlyphEditorWidget::preferred_width() const
  224. {
  225. return frame_thickness() * 2 + font().max_glyph_width() * m_scale - 1;
  226. }
  227. int GlyphEditorWidget::preferred_height() const
  228. {
  229. return frame_thickness() * 2 + font().glyph_height() * m_scale - 1;
  230. }
  231. void GlyphEditorWidget::set_scale(int scale)
  232. {
  233. if (m_scale == scale)
  234. return;
  235. m_scale = clamp(scale, 1, 15);
  236. update();
  237. }