GlyphEditorWidget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <LibGUI/Painter.h>
  28. #include <LibGfx/BitmapFont.h>
  29. #include <LibGfx/Palette.h>
  30. GlyphEditorWidget::GlyphEditorWidget(Gfx::BitmapFont& mutable_font)
  31. : m_font(mutable_font)
  32. {
  33. set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
  34. }
  35. GlyphEditorWidget::~GlyphEditorWidget()
  36. {
  37. }
  38. void GlyphEditorWidget::set_glyph(int glyph)
  39. {
  40. if (m_glyph == glyph)
  41. return;
  42. m_glyph = glyph;
  43. update();
  44. }
  45. void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
  46. {
  47. GUI::Frame::paint_event(event);
  48. GUI::Painter painter(*this);
  49. painter.add_clip_rect(frame_inner_rect());
  50. painter.add_clip_rect(event.rect());
  51. painter.fill_rect(frame_inner_rect(), palette().base());
  52. painter.translate(frame_thickness(), frame_thickness());
  53. painter.translate(-1, -1);
  54. for (int y = 1; y < font().glyph_height(); ++y) {
  55. int y_below = y - 1;
  56. bool bold_line = y_below == font().baseline() || y_below == font().mean_line();
  57. painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
  58. }
  59. for (int x = 1; x < font().max_glyph_width(); ++x)
  60. painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2());
  61. auto bitmap = font().glyph_bitmap(m_glyph);
  62. for (int y = 0; y < font().glyph_height(); ++y) {
  63. for (int x = 0; x < font().max_glyph_width(); ++x) {
  64. Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
  65. if (x >= font().glyph_width(m_glyph)) {
  66. painter.fill_rect(rect, palette().threed_shadow1());
  67. } else {
  68. if (bitmap.bit_at(x, y))
  69. painter.fill_rect(rect, palette().base_text());
  70. }
  71. }
  72. }
  73. }
  74. void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
  75. {
  76. draw_at_mouse(event);
  77. }
  78. void GlyphEditorWidget::mousemove_event(GUI::MouseEvent& event)
  79. {
  80. if (event.buttons() & (GUI::MouseButton::Left | GUI::MouseButton::Right))
  81. draw_at_mouse(event);
  82. }
  83. void GlyphEditorWidget::draw_at_mouse(const GUI::MouseEvent& event)
  84. {
  85. bool set = event.buttons() & GUI::MouseButton::Left;
  86. bool unset = event.buttons() & GUI::MouseButton::Right;
  87. if (!(set ^ unset))
  88. return;
  89. int x = (event.x() - 1) / m_scale;
  90. int y = (event.y() - 1) / m_scale;
  91. auto bitmap = font().glyph_bitmap(m_glyph);
  92. if (x < 0 || x >= bitmap.width())
  93. return;
  94. if (y < 0 || y >= bitmap.height())
  95. return;
  96. if (bitmap.bit_at(x, y) == set)
  97. return;
  98. bitmap.set_bit_at(x, y, set);
  99. if (on_glyph_altered)
  100. on_glyph_altered(m_glyph);
  101. update();
  102. }
  103. int GlyphEditorWidget::preferred_width() const
  104. {
  105. return frame_thickness() * 2 + font().max_glyph_width() * m_scale - 1;
  106. }
  107. int GlyphEditorWidget::preferred_height() const
  108. {
  109. return frame_thickness() * 2 + font().glyph_height() * m_scale - 1;
  110. }