GlyphEditorWidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <AK/UnicodeUtils.h>
  9. #include <LibGUI/Clipboard.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGfx/BitmapFont.h>
  12. #include <LibGfx/Palette.h>
  13. #include <string.h>
  14. static int x_offset;
  15. static int y_offset;
  16. GlyphEditorWidget::~GlyphEditorWidget()
  17. {
  18. }
  19. void GlyphEditorWidget::initialize(Gfx::BitmapFont& mutable_font)
  20. {
  21. if (m_font == mutable_font)
  22. return;
  23. m_font = mutable_font;
  24. set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
  25. }
  26. void GlyphEditorWidget::set_glyph(int glyph)
  27. {
  28. if (m_glyph == glyph)
  29. return;
  30. m_glyph = glyph;
  31. update();
  32. }
  33. void GlyphEditorWidget::delete_glyph()
  34. {
  35. if (on_undo_event)
  36. on_undo_event();
  37. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  38. for (int x = 0; x < font().max_glyph_width(); x++)
  39. for (int y = 0; y < font().glyph_height(); y++)
  40. bitmap.set_bit_at(x, y, false);
  41. font().set_glyph_width(m_glyph, 0);
  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().raw_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 (AK::UnicodeUtils::is_unicode_control_code_point(m_glyph))
  62. glyph_builder.append(AK::UnicodeUtils::get_unicode_control_code_point_alias(m_glyph).value());
  63. else if (Gfx::get_char_bidi_class(m_glyph) == Gfx::BidirectionalClass::STRONG_RTL)
  64. glyph_builder.append_code_point(0xFFFD);
  65. else
  66. glyph_builder.append_code_point(m_glyph);
  67. HashMap<String, String> metadata;
  68. metadata.set("char", glyph_builder.to_string());
  69. metadata.set("width", String::number(bitmap.width()));
  70. metadata.set("height", String::number(bitmap.height()));
  71. GUI::Clipboard::the().set_data(ReadonlyBytes(&bits[0], bitmap.width() * bitmap.height()), "glyph/x-fonteditor", metadata);
  72. }
  73. void GlyphEditorWidget::paste_glyph()
  74. {
  75. auto [data, mime_type, metadata] = GUI::Clipboard::the().data_and_type();
  76. if (!mime_type.starts_with("glyph/"))
  77. return;
  78. if (on_undo_event)
  79. on_undo_event();
  80. auto byte_buffer = data.data();
  81. auto buffer_height = metadata.get("height").value().to_int();
  82. auto buffer_width = metadata.get("width").value().to_int();
  83. u8 bits[buffer_width.value()][buffer_height.value()];
  84. int i = 0;
  85. for (int x = 0; x < buffer_width.value(); x++) {
  86. for (int y = 0; y < buffer_height.value(); y++) {
  87. bits[x][y] = byte_buffer[i];
  88. i++;
  89. }
  90. }
  91. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  92. for (int x = 0; x < min(bitmap.width(), buffer_width.value()); x++) {
  93. for (int y = 0; y < min(bitmap.height(), buffer_height.value()); y++) {
  94. bitmap.set_bit_at(x, y, bits[x][y]);
  95. }
  96. }
  97. if (on_glyph_altered)
  98. on_glyph_altered(m_glyph);
  99. update();
  100. }
  101. void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
  102. {
  103. GUI::Frame::paint_event(event);
  104. GUI::Painter painter(*this);
  105. painter.add_clip_rect(frame_inner_rect());
  106. painter.add_clip_rect(event.rect());
  107. painter.fill_rect(frame_inner_rect(), palette().base());
  108. painter.translate(frame_thickness(), frame_thickness());
  109. painter.translate(-1, -1);
  110. for (int y = 1; y < font().glyph_height(); ++y) {
  111. int y_below = y - 1;
  112. bool bold_line = y_below == font().baseline() || y_below == font().mean_line();
  113. painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
  114. }
  115. for (int x = 1; x < font().max_glyph_width(); ++x)
  116. painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2());
  117. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  118. for (int y = 0; y < font().glyph_height(); ++y) {
  119. for (int x = 0; x < font().max_glyph_width(); ++x) {
  120. Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
  121. if (x >= font().raw_glyph_width(m_glyph)) {
  122. painter.fill_rect(rect, palette().threed_shadow1());
  123. } else {
  124. if (bitmap.bit_at(x, y))
  125. painter.fill_rect(rect, palette().base_text());
  126. }
  127. }
  128. }
  129. }
  130. bool GlyphEditorWidget::is_glyph_empty()
  131. {
  132. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  133. for (int x = 0; x < font().max_glyph_width(); x++)
  134. for (int y = 0; y < font().glyph_height(); y++)
  135. if (bitmap.bit_at(x, y))
  136. return false;
  137. return true;
  138. }
  139. void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
  140. {
  141. if ((event.x() - 1) / m_scale + 1 > font().raw_glyph_width(m_glyph))
  142. return;
  143. if (mode() == Move && is_glyph_empty())
  144. return;
  145. m_is_clicking_valid_cell = true;
  146. if (on_undo_event)
  147. on_undo_event();
  148. if (mode() == Paint) {
  149. draw_at_mouse(event);
  150. } else {
  151. memset(m_movable_bits, 0, sizeof(m_movable_bits));
  152. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  153. for (int x = s_max_width; x < s_max_width + bitmap.width(); x++)
  154. for (int y = s_max_height; y < s_max_height + bitmap.height(); y++)
  155. m_movable_bits[x][y] = bitmap.bit_at(x - s_max_width, y - s_max_height);
  156. x_offset = (event.x() - 1) / m_scale;
  157. y_offset = (event.y() - 1) / m_scale;
  158. move_at_mouse(event);
  159. }
  160. }
  161. void GlyphEditorWidget::mouseup_event(GUI::MouseEvent&)
  162. {
  163. if (!m_is_clicking_valid_cell)
  164. return;
  165. m_is_clicking_valid_cell = false;
  166. }
  167. void GlyphEditorWidget::mousemove_event(GUI::MouseEvent& event)
  168. {
  169. if (!m_is_clicking_valid_cell)
  170. return;
  171. if (!(event.buttons() & (GUI::MouseButton::Primary | GUI::MouseButton::Secondary)))
  172. return;
  173. if (mode() == Paint)
  174. draw_at_mouse(event);
  175. else
  176. move_at_mouse(event);
  177. }
  178. void GlyphEditorWidget::enter_event(Core::Event&)
  179. {
  180. if (mode() == Move)
  181. set_override_cursor(Gfx::StandardCursor::Move);
  182. else
  183. set_override_cursor(Gfx::StandardCursor::None);
  184. }
  185. void GlyphEditorWidget::draw_at_mouse(const GUI::MouseEvent& event)
  186. {
  187. bool set = event.buttons() & GUI::MouseButton::Primary;
  188. bool unset = event.buttons() & GUI::MouseButton::Secondary;
  189. if (!(set ^ unset))
  190. return;
  191. int x = (event.x() - 1) / m_scale;
  192. int y = (event.y() - 1) / m_scale;
  193. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  194. if (x < 0 || x >= bitmap.width())
  195. return;
  196. if (y < 0 || y >= bitmap.height())
  197. return;
  198. if (bitmap.bit_at(x, y) == set)
  199. return;
  200. bitmap.set_bit_at(x, y, set);
  201. if (on_glyph_altered)
  202. on_glyph_altered(m_glyph);
  203. update();
  204. }
  205. void GlyphEditorWidget::move_at_mouse(const GUI::MouseEvent& event)
  206. {
  207. int x_delta = ((event.x() - 1) / m_scale) - x_offset;
  208. int y_delta = ((event.y() - 1) / m_scale) - y_offset;
  209. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  210. if (abs(x_delta) > bitmap.width() || abs(y_delta) > bitmap.height())
  211. return;
  212. for (int x = 0; x < bitmap.width(); x++) {
  213. for (int y = 0; y < bitmap.height(); y++) {
  214. bitmap.set_bit_at(x, y, m_movable_bits[s_max_width + x - x_delta][s_max_height + y - y_delta]);
  215. }
  216. }
  217. if (on_glyph_altered)
  218. on_glyph_altered(m_glyph);
  219. update();
  220. }
  221. static Vector<Vector<u8>> glyph_as_matrix(Gfx::GlyphBitmap const& bitmap)
  222. {
  223. Vector<Vector<u8>> result;
  224. result.ensure_capacity(bitmap.height());
  225. for (int y = 0; y < bitmap.height(); y++) {
  226. result.empend();
  227. auto& row = result.last();
  228. row.ensure_capacity(bitmap.width());
  229. for (int x = 0; x < bitmap.width(); x++) {
  230. row.append(bitmap.bit_at(x, y));
  231. }
  232. }
  233. return result;
  234. }
  235. void GlyphEditorWidget::rotate_90()
  236. {
  237. if (on_undo_event)
  238. on_undo_event();
  239. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  240. auto matrix = glyph_as_matrix(bitmap);
  241. for (int y = 0; y < bitmap.height(); y++) {
  242. for (int x = 0; x < bitmap.width(); x++) {
  243. int source_x = y;
  244. int source_y = bitmap.width() - 1 - x;
  245. bool value = false;
  246. if (source_x < bitmap.width() && source_y < bitmap.height()) {
  247. value = matrix[source_y][source_x];
  248. }
  249. bitmap.set_bit_at(x, y, value);
  250. }
  251. }
  252. if (on_glyph_altered)
  253. on_glyph_altered(m_glyph);
  254. update();
  255. }
  256. void GlyphEditorWidget::flip_vertically()
  257. {
  258. if (on_undo_event)
  259. on_undo_event();
  260. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  261. auto matrix = glyph_as_matrix(bitmap);
  262. for (int y = 0; y < bitmap.height(); y++) {
  263. for (int x = 0; x < bitmap.width(); x++) {
  264. int source_x = x;
  265. int source_y = bitmap.height() - 1 - y;
  266. bool value = matrix[source_y][source_x];
  267. bitmap.set_bit_at(x, y, value);
  268. }
  269. }
  270. if (on_glyph_altered)
  271. on_glyph_altered(m_glyph);
  272. update();
  273. }
  274. void GlyphEditorWidget::flip_horizontally()
  275. {
  276. if (on_undo_event)
  277. on_undo_event();
  278. auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
  279. auto matrix = glyph_as_matrix(bitmap);
  280. for (int y = 0; y < bitmap.height(); y++) {
  281. for (int x = 0; x < bitmap.width(); x++) {
  282. int source_x = bitmap.width() - 1 - x;
  283. int source_y = y;
  284. bool value = matrix[source_y][source_x];
  285. bitmap.set_bit_at(x, y, value);
  286. }
  287. }
  288. if (on_glyph_altered)
  289. on_glyph_altered(m_glyph);
  290. update();
  291. }
  292. int GlyphEditorWidget::preferred_width() const
  293. {
  294. return frame_thickness() * 2 + font().max_glyph_width() * m_scale - 1;
  295. }
  296. int GlyphEditorWidget::preferred_height() const
  297. {
  298. return frame_thickness() * 2 + font().glyph_height() * m_scale - 1;
  299. }
  300. void GlyphEditorWidget::set_scale(int scale)
  301. {
  302. if (m_scale == scale)
  303. return;
  304. m_scale = clamp(scale, 1, 15);
  305. update();
  306. }