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