1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 15:15:07 +00:00

LibGfx+FontEditor+Base: Add "baseline" value to all fonts

This does nothing at the moment but will soon allow us to improve the
vertical positioning of text.
This commit is contained in:
Andreas Kling 2020-09-19 18:01:32 +02:00
parent d9863e0b6c
commit 95eeb321f9
18 changed files with 39 additions and 8 deletions

View file

@ -121,7 +121,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
font_metadata_group_box.set_layout<GUI::VerticalBoxLayout>();
font_metadata_group_box.layout()->set_margins({ 5, 15, 5, 5 });
font_metadata_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
font_metadata_group_box.set_preferred_size(0, 145);
font_metadata_group_box.set_preferred_size(0, 165);
font_metadata_group_box.set_title("Font metadata");
//// Name Row
@ -199,6 +199,25 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
glyph_header_width_spinbox.set_value(m_edited_font->glyph_fixed_width());
glyph_header_width_spinbox.set_enabled(false);
//// Baseline Row
auto& baseline_container = font_metadata_group_box.add<GUI::Widget>();
baseline_container.set_layout<GUI::HorizontalBoxLayout>();
baseline_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
baseline_container.set_preferred_size(0, 22);
auto& baseline_label = baseline_container.add<GUI::Label>();
baseline_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
baseline_label.set_preferred_size(100, 0);
baseline_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
baseline_label.set_text("Baseline:");
auto& baseline_spinbox = baseline_container.add<GUI::SpinBox>();
baseline_spinbox.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
baseline_spinbox.set_preferred_size(100, 0);
baseline_spinbox.set_min(0);
baseline_spinbox.set_max(m_edited_font->glyph_height() - 1);
baseline_spinbox.set_value(m_edited_font->baseline());
//// Fixed checkbox Row
auto& fixed_width_checkbox = font_metadata_group_box.add<GUI::CheckBox>();
fixed_width_checkbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
@ -240,7 +259,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
right_site_width = max(right_site_width, m_glyph_map_widget->preferred_width());
m_preferred_width = m_glyph_editor_widget->width() + right_site_width + 20;
m_preferred_height = m_glyph_map_widget->relative_rect().height() + 2 * m_edited_font->glyph_height() + 250;
m_preferred_height = m_glyph_map_widget->relative_rect().height() + 2 * m_edited_font->glyph_height() + 270;
};
m_glyph_editor_widget->on_glyph_altered = [this, update_demo](u8 glyph) {
@ -283,6 +302,11 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::Font>&& edite
update_demo();
};
baseline_spinbox.on_change = [this, update_demo](int value) {
m_edited_font->set_baseline(value);
update_demo();
};
// init widget
calculate_prefed_sizes();
m_glyph_map_widget->set_selected_glyph('A');

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -53,7 +53,8 @@ struct [[gnu::packed]] FontFileHeader
u8 type;
u8 is_variable_width;
u8 glyph_spacing;
u8 unused[5];
u8 baseline;
u8 unused[4];
char name[64];
};
@ -112,7 +113,7 @@ NonnullRefPtr<Font> Font::clone() const
memcpy(new_widths, m_glyph_widths, m_glyph_count);
else
memset(new_widths, m_glyph_width, m_glyph_count);
return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type));
return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type, m_baseline));
}
NonnullRefPtr<Font> Font::create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type)
@ -124,10 +125,10 @@ NonnullRefPtr<Font> Font::create(u8 glyph_height, u8 glyph_width, bool fixed, Fo
memset(new_rows, 0, bytes_per_glyph * count);
auto* new_widths = static_cast<u8*>(malloc(count));
memset(new_widths, glyph_width, count);
return adopt(*new Font("Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type));
return adopt(*new Font("Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type, 0));
}
Font::Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type)
Font::Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline)
: m_name(name)
, m_type(type)
, m_rows(rows)
@ -137,6 +138,7 @@ Font::Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_wid
, m_min_glyph_width(glyph_width)
, m_max_glyph_width(glyph_width)
, m_glyph_spacing(glyph_spacing)
, m_baseline(baseline)
, m_fixed_width(is_fixed_width)
{
// FIXME: This is just a dumb guess. It would be cool to know the actual x-height of the font!
@ -189,7 +191,7 @@ RefPtr<Font> Font::load_from_memory(const u8* data)
u8* widths = nullptr;
if (header.is_variable_width)
widths = (u8*)(rows) + count * bytes_per_glyph;
return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, type));
return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, type, header.baseline));
}
size_t Font::glyph_count_by_type(FontTypes type)
@ -240,6 +242,7 @@ bool Font::write_to_file(const StringView& path)
header.glyph_width = m_glyph_width;
header.glyph_height = m_glyph_height;
header.type = m_type;
header.baseline = m_baseline;
header.is_variable_width = !m_fixed_width;
header.glyph_spacing = m_glyph_spacing;
memcpy(header.name, m_name.characters(), min(m_name.length(), (size_t)63));

View file

@ -100,6 +100,9 @@ public:
u8 max_glyph_width() const { return m_max_glyph_width; }
u8 glyph_fixed_width() const { return m_glyph_width; }
u8 baseline() const { return m_baseline; }
void set_baseline(u8 baseline) { m_baseline = baseline; }
int width(const StringView&) const;
int width(const Utf8View&) const;
int width(const Utf32View&) const;
@ -129,7 +132,7 @@ public:
void set_type(FontTypes type);
private:
Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type);
Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline);
static RefPtr<Font> load_from_memory(const u8*);
static size_t glyph_count_by_type(FontTypes type);
@ -151,6 +154,7 @@ private:
u8 m_min_glyph_width { 0 };
u8 m_max_glyph_width { 0 };
u8 m_glyph_spacing { 0 };
u8 m_baseline { 0 };
bool m_fixed_width { false };
bool m_boldface { false };