2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-02 08:05:14 +01:00
|
|
|
#include "FontEditor.h"
|
2020-05-09 01:06:56 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/AboutDialog.h>
|
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Application.h>
|
2020-07-10 20:59:25 -04:00
|
|
|
#include <LibGUI/FilePicker.h>
|
2020-05-09 01:06:56 +03:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-15 01:56:30 +01:00
|
|
|
#include <LibGUI/Menu.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/MenuBar.h>
|
2020-05-09 01:06:56 +03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Window.h>
|
2020-02-14 23:02:47 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-14 23:53:11 +01:00
|
|
|
#include <LibGfx/Font.h>
|
2020-07-10 20:59:25 -04:00
|
|
|
#include <LibGfx/Point.h>
|
2019-02-02 08:05:14 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-07-10 20:59:25 -04:00
|
|
|
if (pledge("stdio shared_buffer thread rpath accept unix cpath wpath fattr", nullptr) < 0) {
|
2020-01-13 12:22:49 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2019-02-11 14:56:23 +01:00
|
|
|
|
2020-07-10 20:59:25 -04:00
|
|
|
if (pledge("stdio shared_buffer thread rpath accept cpath wpath", nullptr) < 0) {
|
2020-01-13 12:22:49 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-09 01:06:56 +03:00
|
|
|
const char* path = nullptr;
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(path, "The font file for editing.", "file", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
2019-02-05 07:23:01 +01:00
|
|
|
|
2020-05-09 01:06:56 +03:00
|
|
|
RefPtr<Gfx::Font> edited_font;
|
|
|
|
if (path == nullptr) {
|
|
|
|
path = "/tmp/saved.font";
|
|
|
|
edited_font = Gfx::Font::default_font().clone();
|
|
|
|
} else {
|
|
|
|
edited_font = Gfx::Font::load_from_file(path)->clone();
|
2019-02-05 07:23:01 +01:00
|
|
|
if (!edited_font) {
|
2020-10-05 18:02:53 +02:00
|
|
|
String message = String::formatted("Couldn't load font: {}\n", path);
|
2020-07-15 20:45:11 -06:00
|
|
|
GUI::MessageBox::show(nullptr, message, "Font Editor", GUI::MessageBox::Type::Error);
|
2019-02-05 07:23:01 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 01:06:56 +03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-font-editor");
|
2019-02-02 08:05:14 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
auto window = GUI::Window::construct();
|
2020-05-09 01:06:56 +03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-12-31 00:12:52 +02:00
|
|
|
|
2020-07-10 20:59:25 -04:00
|
|
|
auto set_edited_font = [&](const String& path, RefPtr<Gfx::Font>&& font, Gfx::IntPoint point) {
|
|
|
|
// Convert 256 char font to 384 char font.
|
|
|
|
if (font->type() == Gfx::FontTypes::Default)
|
|
|
|
font->set_type(Gfx::FontTypes::LatinExtendedA);
|
|
|
|
|
2020-10-05 18:02:53 +02:00
|
|
|
window->set_title(String::formatted("{} - Font Editor", path));
|
2020-07-10 20:59:25 -04:00
|
|
|
auto& font_editor_widget = window->set_main_widget<FontEditorWidget>(path, move(font));
|
|
|
|
window->set_rect({ point, { font_editor_widget.preferred_width(), font_editor_widget.preferred_height() } });
|
|
|
|
};
|
2020-07-31 16:12:11 -06:00
|
|
|
set_edited_font(path, move(edited_font), window->position());
|
2019-12-31 00:12:52 +02:00
|
|
|
|
2020-04-21 16:01:00 +02:00
|
|
|
auto menubar = GUI::MenuBar::construct();
|
2019-12-31 00:12:52 +02:00
|
|
|
|
2020-04-04 12:18:40 +02:00
|
|
|
auto& app_menu = menubar->add_menu("Font Editor");
|
2020-07-10 20:59:25 -04:00
|
|
|
app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
2020-07-15 19:52:02 -06:00
|
|
|
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
|
2020-07-10 20:59:25 -04:00
|
|
|
if (!open_path.has_value())
|
|
|
|
return;
|
|
|
|
|
|
|
|
RefPtr<Gfx::Font> new_font = Gfx::Font::load_from_file(open_path.value())->clone();
|
|
|
|
if (!new_font) {
|
2020-10-05 18:02:53 +02:00
|
|
|
String message = String::formatted("Couldn't load font: {}\n", open_path.value());
|
2020-07-15 20:45:11 -06:00
|
|
|
GUI::MessageBox::show(window, message, "Font Editor", GUI::MessageBox::Type::Error);
|
2020-07-10 20:59:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_edited_font(open_path.value(), move(new_font), window->position());
|
|
|
|
}));
|
2020-11-01 21:32:27 +00:00
|
|
|
app_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
|
2020-07-10 21:23:44 -04:00
|
|
|
FontEditorWidget* editor = static_cast<FontEditorWidget*>(window->main_widget());
|
|
|
|
editor->save_as(editor->path());
|
|
|
|
}));
|
2020-11-01 21:32:27 +00:00
|
|
|
app_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
|
2020-07-10 21:20:39 -04:00
|
|
|
FontEditorWidget* editor = static_cast<FontEditorWidget*>(window->main_widget());
|
2020-07-12 13:39:34 -04:00
|
|
|
LexicalPath lexical_path(editor->path());
|
2020-07-15 19:52:02 -06:00
|
|
|
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, lexical_path.title(), lexical_path.extension());
|
2020-07-10 21:20:39 -04:00
|
|
|
if (!save_path.has_value())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (editor->save_as(save_path.value()))
|
2020-10-05 18:02:53 +02:00
|
|
|
window->set_title(String::formatted("{} - Font Editor", save_path.value()));
|
2020-07-10 21:20:39 -04:00
|
|
|
}));
|
2020-07-10 20:59:25 -04:00
|
|
|
app_menu.add_separator();
|
2020-07-04 16:52:01 +02:00
|
|
|
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
|
|
|
|
app->quit();
|
2019-12-31 00:12:52 +02:00
|
|
|
return;
|
|
|
|
}));
|
|
|
|
|
2020-04-04 12:18:40 +02:00
|
|
|
auto& help_menu = menubar->add_menu("Help");
|
|
|
|
help_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
|
2020-05-09 01:06:56 +03:00
|
|
|
GUI::AboutDialog::show("Font Editor", app_icon.bitmap_for_size(32), window);
|
2019-12-31 00:12:52 +02:00
|
|
|
}));
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
app->set_menubar(move(menubar));
|
2019-12-31 00:12:52 +02:00
|
|
|
|
2020-05-09 01:06:56 +03:00
|
|
|
window->show();
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
return app->exec();
|
2019-02-02 08:05:14 +01:00
|
|
|
}
|