GlyphEditorWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "GlyphEditorWidget.h"
  27. #include <AK/StringBuilder.h>
  28. #include <LibGUI/Clipboard.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGfx/BitmapFont.h>
  31. #include <LibGfx/Palette.h>
  32. GlyphEditorWidget::~GlyphEditorWidget()
  33. {
  34. }
  35. void GlyphEditorWidget::initialize(Gfx::BitmapFont& mutable_font)
  36. {
  37. if (m_font == mutable_font)
  38. return;
  39. m_font = mutable_font;
  40. set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
  41. m_glyph = 0;
  42. }
  43. void GlyphEditorWidget::set_glyph(int glyph)
  44. {
  45. if (m_glyph == glyph)
  46. return;
  47. m_glyph = glyph;
  48. update();
  49. }
  50. void GlyphEditorWidget::delete_glyph()
  51. {
  52. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  53. for (int x = 0; x < bitmap.width(); x++)
  54. for (int y = 0; y < bitmap.height(); y++)
  55. bitmap.set_bit_at(x, y, false);
  56. if (on_glyph_altered)
  57. on_glyph_altered(m_glyph);
  58. update();
  59. }
  60. void GlyphEditorWidget::cut_glyph()
  61. {
  62. copy_glyph();
  63. delete_glyph();
  64. }
  65. void GlyphEditorWidget::copy_glyph()
  66. {
  67. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  68. u8 bits[bitmap.width()][bitmap.height()];
  69. for (int x = 0; x < bitmap.width(); x++) {
  70. for (int y = 0; y < bitmap.height(); y++) {
  71. bits[x][y] = bitmap.bit_at(x, y);
  72. }
  73. }
  74. StringBuilder glyph_builder;
  75. if (m_glyph < 128) {
  76. glyph_builder.append(m_glyph);
  77. } else {
  78. glyph_builder.append(128 | 64 | (m_glyph / 64));
  79. glyph_builder.append(128 | (m_glyph % 64));
  80. }
  81. HashMap<String, String> metadata;
  82. metadata.set("char", glyph_builder.to_string());
  83. metadata.set("width", String::number(bitmap.width()));
  84. metadata.set("height", String::number(bitmap.height()));
  85. auto data = ByteBuffer::copy(&bits[0], bitmap.width() * bitmap.height());
  86. GUI::Clipboard::the().set_data(data, "glyph/x-fonteditor", metadata);
  87. }
  88. void GlyphEditorWidget::paste_glyph()
  89. {
  90. auto mime_type = GUI::Clipboard::the().mime_type();
  91. if (!mime_type.starts_with("glyph/"))
  92. return;
  93. auto byte_buffer = GUI::Clipboard::the().data();
  94. auto buffer_height = GUI::Clipboard::the().data_and_type().metadata.get("height").value().to_int();
  95. auto buffer_width = GUI::Clipboard::the().data_and_type().metadata.get("width").value().to_int();
  96. u8 bits[buffer_width.value()][buffer_height.value()];
  97. int i = 0;
  98. for (int x = 0; x < buffer_width.value(); x++) {
  99. for (int y = 0; y < buffer_height.value(); y++) {
  100. bits[x][y] = byte_buffer[i];
  101. i++;
  102. }
  103. }
  104. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  105. for (int x = 0; x < min(bitmap.width(), buffer_width.value()); x++) {
  106. for (int y = 0; y < min(bitmap.height(), buffer_height.value()); y++) {
  107. if (bits[x][y])
  108. bitmap.set_bit_at(x, y, bits[x][y]);
  109. }
  110. }
  111. if (on_glyph_altered)
  112. on_glyph_altered(m_glyph);
  113. update();
  114. }
  115. void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
  116. {
  117. GUI::Frame::paint_event(event);
  118. GUI::Painter painter(*this);
  119. painter.add_clip_rect(frame_inner_rect());
  120. painter.add_clip_rect(event.rect());
  121. painter.fill_rect(frame_inner_rect(), palette().base());
  122. painter.translate(frame_thickness(), frame_thickness());
  123. painter.translate(-1, -1);
  124. for (int y = 1; y < font().glyph_height(); ++y) {
  125. int y_below = y - 1;
  126. bool bold_line = y_below == font().baseline() || y_below == font().mean_line();
  127. painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
  128. }
  129. for (int x = 1; x < font().max_glyph_width(); ++x)
  130. painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2());
  131. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  132. for (int y = 0; y < font().glyph_height(); ++y) {
  133. for (int x = 0; x < font().max_glyph_width(); ++x) {
  134. Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
  135. if (x >= font().glyph_width(m_glyph)) {
  136. painter.fill_rect(rect, palette().threed_shadow1());
  137. } else {
  138. if (bitmap.bit_at(x, y))
  139. painter.fill_rect(rect, palette().base_text());
  140. }
  141. }
  142. }
  143. }
  144. void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
  145. {
  146. draw_at_mouse(event);
  147. }
  148. void GlyphEditorWidget::mousemove_event(GUI::MouseEvent& event)
  149. {
  150. if (event.buttons() & (GUI::MouseButton::Left | GUI::MouseButton::Right))
  151. draw_at_mouse(event);
  152. }
  153. void GlyphEditorWidget::draw_at_mouse(const GUI::MouseEvent& event)
  154. {
  155. bool set = event.buttons() & GUI::MouseButton::Left;
  156. bool unset = event.buttons() & GUI::MouseButton::Right;
  157. if (!(set ^ unset))
  158. return;
  159. int x = (event.x() - 1) / m_scale;
  160. int y = (event.y() - 1) / m_scale;
  161. auto bitmap = font().glyph(m_glyph).glyph_bitmap();
  162. if (x < 0 || x >= bitmap.width())
  163. return;
  164. if (y < 0 || y >= bitmap.height())
  165. return;
  166. if (bitmap.bit_at(x, y) == set)
  167. return;
  168. bitmap.set_bit_at(x, y, set);
  169. if (on_glyph_altered)
  170. on_glyph_altered(m_glyph);
  171. update();
  172. }
  173. int GlyphEditorWidget::preferred_width() const
  174. {
  175. return frame_thickness() * 2 + font().max_glyph_width() * m_scale - 1;
  176. }
  177. int GlyphEditorWidget::preferred_height() const
  178. {
  179. return frame_thickness() * 2 + font().glyph_height() * m_scale - 1;
  180. }