LibGfx+FontEditor: Consolidate BitmapFont width and height limits

And make them more self-documenting. Previously these constraints
were duplicated across multiple files.
This commit is contained in:
thankyouverycool 2021-11-29 10:33:34 -05:00 committed by Andreas Kling
parent 541f1de3a8
commit e29abc5395
Notes: sideshowbarker 2024-07-17 23:20:37 +09:00
5 changed files with 20 additions and 16 deletions

View file

@ -184,9 +184,13 @@ void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
} else {
memset(m_movable_bits, 0, sizeof(m_movable_bits));
auto bitmap = font().raw_glyph(m_glyph).glyph_bitmap();
for (int x = s_max_width; x < s_max_width + bitmap.width(); x++)
for (int y = s_max_height; y < s_max_height + bitmap.height(); y++)
m_movable_bits[x][y] = bitmap.bit_at(x - s_max_width, y - s_max_height);
for (int x = 0; x < bitmap.width(); x++) {
for (int y = 0; y < bitmap.height(); y++) {
int movable_x = Gfx::GlyphBitmap::max_width() + x;
int movable_y = Gfx::GlyphBitmap::max_height() + y;
m_movable_bits[movable_x][movable_y] = bitmap.bit_at(x, y);
}
}
m_scaled_offset_x = (event.x() - 1) / m_scale;
m_scaled_offset_y = (event.y() - 1) / m_scale;
move_at_mouse(event);
@ -250,7 +254,9 @@ void GlyphEditorWidget::move_at_mouse(const GUI::MouseEvent& event)
return;
for (int x = 0; x < bitmap.width(); x++) {
for (int y = 0; y < bitmap.height(); y++) {
bitmap.set_bit_at(x, y, m_movable_bits[s_max_width + x - x_delta][s_max_height + y - y_delta]);
int movable_x = Gfx::GlyphBitmap::max_width() + x - x_delta;
int movable_y = Gfx::GlyphBitmap::max_height() + y - y_delta;
bitmap.set_bit_at(x, y, m_movable_bits[movable_x][movable_y]);
}
}
if (on_glyph_altered)

View file

@ -10,9 +10,6 @@
#include <LibGUI/Frame.h>
#include <LibGfx/BitmapFont.h>
static constexpr int s_max_width = 32;
static constexpr int s_max_height = 36;
class GlyphEditorWidget final : public GUI::Frame {
C_OBJECT(GlyphEditorWidget)
public:
@ -74,7 +71,7 @@ private:
int m_scale { 10 };
int m_scaled_offset_x { 0 };
int m_scaled_offset_y { 0 };
u8 m_movable_bits[s_max_width* 3][s_max_height * 3] = {};
u8 m_movable_bits[Gfx::GlyphBitmap::max_width() * 3][Gfx::GlyphBitmap::max_height() * 3] {};
Mode m_mode { Paint };
bool m_is_clicking_valid_cell { false };
};

View file

@ -23,9 +23,6 @@
#include <LibGfx/FontStyleMapping.h>
#include <LibGfx/Palette.h>
static constexpr int s_max_width = 32;
static constexpr int s_max_height = 36;
namespace GUI {
class GlyphPreviewWidget final : public Frame {
@ -120,7 +117,7 @@ private:
int m_glyph_width { 20 };
int m_mean_line { 2 };
int m_baseline { 16 };
u8 m_bits[s_max_width][s_max_height] {};
u8 m_bits[Gfx::GlyphBitmap::max_width()][Gfx::GlyphBitmap::max_height()] {};
};
}
@ -172,8 +169,8 @@ NewFontDialog::NewFontDialog(GUI::Window* parent_window)
m_glyph_height_spinbox->set_value(20);
m_glyph_width_spinbox->set_value(20);
m_glyph_height_spinbox->set_max(s_max_height);
m_glyph_width_spinbox->set_max(s_max_width);
m_glyph_height_spinbox->set_max(Gfx::GlyphBitmap::max_height());
m_glyph_width_spinbox->set_max(Gfx::GlyphBitmap::max_width());
m_mean_line_spinbox->set_value(2);
m_baseline_spinbox->set_value(16);
m_mean_line_spinbox->set_max(max(m_glyph_height_spinbox->value() - 2, 0));

View file

@ -45,7 +45,7 @@ public:
private:
size_t m_code_point;
RefPtr<Gfx::BitmapFont> m_font;
u8 m_bits[32][36] = {};
u8 m_bits[Gfx::GlyphBitmap::max_width()][Gfx::GlyphBitmap::max_height()] {};
u8 m_width { 0 };
mutable u8 m_restored_width { 0 };
mutable u32 m_restored_code_point { 0 };

View file

@ -38,10 +38,14 @@ public:
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
static constexpr size_t bytes_per_row() { return sizeof(u32); }
static constexpr int max_width() { return bytes_per_row() * 8; }
static constexpr int max_height() { return max_width() + bytes_per_row(); }
private:
AK::Bitmap bitmap(size_t y) const
{
return { const_cast<u8*>(m_rows) + sizeof(u32) * (m_start_index + y), sizeof(u32) * 8 };
return { const_cast<u8*>(m_rows) + bytes_per_row() * (m_start_index + y), bytes_per_row() * 8 };
}
const u8* m_rows { nullptr };