GlyphEditorWidget.cpp 7.7 KB

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