1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:17:35 +00:00

FontEditor: Let GlyphEditorWidget initialize a null BitmapFont

And remove public reference getters as there is no guarantee they are
nonnull.
This commit is contained in:
thankyouverycool 2023-05-10 17:01:12 -04:00 committed by Andreas Kling
parent 0ad5e85163
commit 24046f9adf
3 changed files with 23 additions and 25 deletions

View file

@ -14,7 +14,7 @@ REGISTER_WIDGET(FontEditor, GlyphEditorWidget);
namespace FontEditor { namespace FontEditor {
void GlyphEditorWidget::set_font(Gfx::BitmapFont& mutable_font) void GlyphEditorWidget::initialize(Gfx::BitmapFont* mutable_font)
{ {
if (m_font == mutable_font) if (m_font == mutable_font)
return; return;
@ -43,21 +43,21 @@ void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
painter.translate(frame_thickness(), frame_thickness()); painter.translate(frame_thickness(), frame_thickness());
painter.translate(-1, -1); painter.translate(-1, -1);
for (int y = 1; y < font().glyph_height(); ++y) { for (int y = 1; y < m_font->glyph_height(); ++y) {
int y_below = y - 1; int y_below = y - 1;
bool bold_line = y_below == font().baseline() || y_below == font().mean_line(); bool bold_line = y_below == m_font->baseline() || y_below == m_font->mean_line();
painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1); painter.draw_line({ 0, y * m_scale }, { m_font->max_glyph_width() * m_scale, y * m_scale }, palette().threed_shadow2(), bold_line ? 2 : 1);
} }
for (int x = 1; x < font().max_glyph_width(); ++x) for (int x = 1; x < m_font->max_glyph_width(); ++x)
painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2()); painter.draw_line({ x * m_scale, 0 }, { x * m_scale, m_font->glyph_height() * m_scale }, palette().threed_shadow2());
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
for (int y = 0; y < font().glyph_height(); ++y) { for (int y = 0; y < m_font->glyph_height(); ++y) {
for (int x = 0; x < font().max_glyph_width(); ++x) { for (int x = 0; x < m_font->max_glyph_width(); ++x) {
Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale }; Gfx::IntRect rect { x * m_scale, y * m_scale, m_scale, m_scale };
if (x >= font().raw_glyph_width(m_glyph)) { if (x >= m_font->raw_glyph_width(m_glyph)) {
painter.fill_rect(rect, palette().threed_shadow1()); painter.fill_rect(rect, palette().threed_shadow1());
} else { } else {
if (bitmap.bit_at(x, y)) if (bitmap.bit_at(x, y))
@ -69,9 +69,9 @@ void GlyphEditorWidget::paint_event(GUI::PaintEvent& event)
bool GlyphEditorWidget::is_glyph_empty() bool GlyphEditorWidget::is_glyph_empty()
{ {
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
for (int x = 0; x < font().max_glyph_width(); x++) for (int x = 0; x < m_font->max_glyph_width(); x++)
for (int y = 0; y < font().glyph_height(); y++) for (int y = 0; y < m_font->glyph_height(); y++)
if (bitmap.bit_at(x, y)) if (bitmap.bit_at(x, y))
return false; return false;
return true; return true;
@ -79,7 +79,7 @@ bool GlyphEditorWidget::is_glyph_empty()
void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event) void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
{ {
if ((event.x() - 1) / m_scale + 1 > font().raw_glyph_width(m_glyph)) if ((event.x() - 1) / m_scale + 1 > m_font->raw_glyph_width(m_glyph))
return; return;
if (mode() == Move && is_glyph_empty()) if (mode() == Move && is_glyph_empty())
return; return;
@ -90,7 +90,7 @@ void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
draw_at_mouse(event); draw_at_mouse(event);
} else { } else {
memset(m_movable_bits, 0, sizeof(m_movable_bits)); memset(m_movable_bits, 0, sizeof(m_movable_bits));
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
for (int x = 0; x < bitmap.width(); x++) { for (int x = 0; x < bitmap.width(); x++) {
for (int y = 0; y < bitmap.height(); y++) { for (int y = 0; y < bitmap.height(); y++) {
int movable_x = Gfx::GlyphBitmap::max_width() + x; int movable_x = Gfx::GlyphBitmap::max_width() + x;
@ -139,7 +139,7 @@ void GlyphEditorWidget::draw_at_mouse(GUI::MouseEvent const& event)
return; return;
int x = (event.x() - 1) / m_scale; int x = (event.x() - 1) / m_scale;
int y = (event.y() - 1) / m_scale; int y = (event.y() - 1) / m_scale;
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
if (x < 0 || x >= bitmap.width()) if (x < 0 || x >= bitmap.width())
return; return;
if (y < 0 || y >= bitmap.height()) if (y < 0 || y >= bitmap.height())
@ -156,7 +156,7 @@ void GlyphEditorWidget::move_at_mouse(GUI::MouseEvent const& event)
{ {
int x_delta = ((event.x() - 1) / m_scale) - m_scaled_offset_x; int x_delta = ((event.x() - 1) / m_scale) - m_scaled_offset_x;
int y_delta = ((event.y() - 1) / m_scale) - m_scaled_offset_y; int y_delta = ((event.y() - 1) / m_scale) - m_scaled_offset_y;
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
if (abs(x_delta) > bitmap.width() || abs(y_delta) > bitmap.height()) if (abs(x_delta) > bitmap.width() || abs(y_delta) > bitmap.height())
return; return;
for (int x = 0; x < bitmap.width(); x++) { for (int x = 0; x < bitmap.width(); x++) {
@ -193,7 +193,7 @@ void GlyphEditorWidget::rotate_90(Gfx::RotationDirection direction)
if (on_undo_event) if (on_undo_event)
on_undo_event(); on_undo_event();
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
auto matrix = glyph_as_matrix(bitmap); auto matrix = glyph_as_matrix(bitmap);
auto clockwise = direction == Gfx::RotationDirection::Clockwise; auto clockwise = direction == Gfx::RotationDirection::Clockwise;
@ -219,7 +219,7 @@ void GlyphEditorWidget::flip(Gfx::Orientation orientation)
if (on_undo_event) if (on_undo_event)
on_undo_event(); on_undo_event();
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap(); auto bitmap = m_font->raw_glyph(m_glyph).glyph_bitmap();
auto matrix = glyph_as_matrix(bitmap); auto matrix = glyph_as_matrix(bitmap);
auto vertical = orientation == Gfx::Orientation::Vertical; auto vertical = orientation == Gfx::Orientation::Vertical;
@ -239,12 +239,12 @@ void GlyphEditorWidget::flip(Gfx::Orientation orientation)
int GlyphEditorWidget::preferred_width() const int GlyphEditorWidget::preferred_width() const
{ {
return frame_thickness() * 2 + font().max_glyph_width() * m_scale - 1; return frame_thickness() * 2 + m_font->max_glyph_width() * m_scale - 1;
} }
int GlyphEditorWidget::preferred_height() const int GlyphEditorWidget::preferred_height() const
{ {
return frame_thickness() * 2 + font().glyph_height() * m_scale - 1; return frame_thickness() * 2 + m_font->glyph_height() * m_scale - 1;
} }
void GlyphEditorWidget::set_scale(int scale) void GlyphEditorWidget::set_scale(int scale)

View file

@ -33,9 +33,7 @@ public:
int preferred_width() const; int preferred_width() const;
int preferred_height() const; int preferred_height() const;
Gfx::BitmapFont& font() { return *m_font; } void initialize(Gfx::BitmapFont*);
Gfx::BitmapFont const& font() const { return *m_font; }
void set_font(Gfx::BitmapFont&);
int scale() const { return m_scale; } int scale() const { return m_scale; }
void set_scale(int scale); void set_scale(int scale);

View file

@ -653,7 +653,7 @@ ErrorOr<void> MainWidget::initialize(StringView path, RefPtr<Gfx::BitmapFont>&&
if (m_preview_label) if (m_preview_label)
m_preview_label->set_font(*m_font); m_preview_label->set_font(*m_font);
m_glyph_editor_widget->set_font(*m_font); m_glyph_editor_widget->initialize(m_font);
m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height()); m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
m_glyph_editor_widget->set_glyph(active_glyph); m_glyph_editor_widget->set_glyph(active_glyph);