summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorthankyouverycool <66646555+thankyouverycool@users.noreply.github.com>2022-07-30 07:34:04 -0400
committerAndreas Kling <kling@serenityos.org>2022-08-04 02:54:00 +0200
commit407231f11c2817776c5b50480f48e7ea857258a4 (patch)
treea1ced58abe8dc73dcebd526c80714ae87c774d7d /Userland/Applications
parent807bd6da6ca062307e90fecd560a9414c6ae7768 (diff)
downloadserenity-407231f11c2817776c5b50480f48e7ea857258a4.zip
FontEditor: Move new font creation to NewFontDialog and handle errors
Fixes potential OOM crashes when creating a new font and an oversight in which glyph spacing was not being set.
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/FontEditor/MainWidget.cpp21
-rw-r--r--Userland/Applications/FontEditor/NewFontDialog.cpp17
-rw-r--r--Userland/Applications/FontEditor/NewFontDialog.h7
3 files changed, 27 insertions, 18 deletions
diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp
index e1f5f87d3c..19fecb4f8a 100644
--- a/Userland/Applications/FontEditor/MainWidget.cpp
+++ b/Userland/Applications/FontEditor/MainWidget.cpp
@@ -96,19 +96,14 @@ ErrorOr<void> MainWidget::create_actions()
if (!request_close())
return;
auto new_font_wizard = NewFontDialog::construct(window());
- if (new_font_wizard->exec() == GUI::Dialog::ExecResult::OK) {
- auto metadata = new_font_wizard->new_font_metadata();
- auto new_font = Gfx::BitmapFont::create(metadata.glyph_height, metadata.glyph_width, metadata.is_fixed_width, 0x110000);
- new_font->set_name(metadata.name);
- new_font->set_family(metadata.family);
- new_font->set_presentation_size(metadata.presentation_size);
- new_font->set_weight(metadata.weight);
- new_font->set_slope(metadata.slope);
- new_font->set_baseline(metadata.baseline);
- new_font->set_mean_line(metadata.mean_line);
- window()->set_modified(true);
- MUST(initialize({}, move(new_font)));
- }
+ if (new_font_wizard->exec() != GUI::Dialog::ExecResult::OK)
+ return;
+ new_font_wizard->hide();
+ auto maybe_font = new_font_wizard->create_font();
+ if (maybe_font.is_error())
+ return show_error("Failed to create new font"sv, maybe_font.error());
+ if (auto result = initialize({}, move(maybe_font.value())); result.is_error())
+ show_error("Failed to initialize font"sv, result.error());
});
m_new_action->set_status_tip("Create a new font");
diff --git a/Userland/Applications/FontEditor/NewFontDialog.cpp b/Userland/Applications/FontEditor/NewFontDialog.cpp
index 30e2ea2c5d..9adff87764 100644
--- a/Userland/Applications/FontEditor/NewFontDialog.cpp
+++ b/Userland/Applications/FontEditor/NewFontDialog.cpp
@@ -214,3 +214,20 @@ void NewFontDialog::save_metadata()
m_new_font_metadata.glyph_spacing = m_spacing_spinbox->value();
m_new_font_metadata.is_fixed_width = m_fixed_width_checkbox->is_checked();
}
+
+ErrorOr<NonnullRefPtr<Gfx::BitmapFont>> NewFontDialog::create_font()
+{
+ save_metadata();
+
+ auto font = TRY(Gfx::BitmapFont::try_create(m_new_font_metadata.glyph_height, m_new_font_metadata.glyph_width, m_new_font_metadata.is_fixed_width, 0x110000));
+ font->set_name(m_new_font_metadata.name);
+ font->set_family(m_new_font_metadata.family);
+ font->set_presentation_size(m_new_font_metadata.presentation_size);
+ font->set_weight(m_new_font_metadata.weight);
+ font->set_slope(m_new_font_metadata.slope);
+ font->set_baseline(m_new_font_metadata.baseline);
+ font->set_mean_line(m_new_font_metadata.mean_line);
+ font->set_glyph_spacing(m_new_font_metadata.glyph_spacing);
+
+ return font;
+}
diff --git a/Userland/Applications/FontEditor/NewFontDialog.h b/Userland/Applications/FontEditor/NewFontDialog.h
index 86cff47286..eb7f2f0733 100644
--- a/Userland/Applications/FontEditor/NewFontDialog.h
+++ b/Userland/Applications/FontEditor/NewFontDialog.h
@@ -9,16 +9,13 @@
#include <LibGUI/Window.h>
#include <LibGUI/Wizards/WizardDialog.h>
#include <LibGUI/Wizards/WizardPage.h>
+#include <LibGfx/Font/BitmapFont.h>
class NewFontDialog final : public GUI::WizardDialog {
C_OBJECT(NewFontDialog);
public:
- auto new_font_metadata()
- {
- save_metadata();
- return m_new_font_metadata;
- }
+ ErrorOr<NonnullRefPtr<Gfx::BitmapFont>> create_font();
private:
NewFontDialog(GUI::Window* parent_window);