Implement support for loading system fonts on Linux, macOS / iOS and Windows.

This commit is contained in:
bruvzg 2022-07-08 15:38:30 +03:00
parent 3e0e84a54c
commit 36ef8f29dc
No known key found for this signature in database
GPG key ID: 7960FCF39844EC38
27 changed files with 1089 additions and 54 deletions

View file

@ -81,10 +81,7 @@ class EditorPropertyArray : public EditorProperty {
GDCLASS(EditorPropertyArray, EditorProperty);
PopupMenu *change_type = nullptr;
bool updating = false;
bool dropping = false;
Ref<EditorPropertyArrayObject> object;
int page_length = 20;
int page_index = 0;
int changing_type_index;
@ -106,29 +103,35 @@ class EditorPropertyArray : public EditorProperty {
Button *reorder_selected_button = nullptr;
void _page_changed(int p_page);
void _length_changed(double p_page);
void _add_element();
void _edit_pressed();
void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
void _change_type(Object *p_button, int p_index);
void _change_type_menu(int p_index);
void _object_id_selected(const StringName &p_property, ObjectID p_id);
void _remove_pressed(int p_index);
void _button_draw();
bool _is_drop_valid(const Dictionary &p_drag_data) const;
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
void _reorder_button_gui_input(const Ref<InputEvent> &p_event);
void _reorder_button_down(int p_index);
void _reorder_button_up();
protected:
Ref<EditorPropertyArrayObject> object;
bool updating = false;
bool dropping = false;
static void _bind_methods();
void _notification(int p_what);
virtual void _add_element();
virtual void _length_changed(double p_page);
virtual void _edit_pressed();
virtual void _property_changed(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
virtual void _change_type(Object *p_button, int p_index);
virtual void _change_type_menu(int p_index);
virtual void _object_id_selected(const StringName &p_property, ObjectID p_id);
virtual void _remove_pressed(int p_index);
virtual void _button_draw();
virtual bool _is_drop_valid(const Dictionary &p_drag_data) const;
virtual bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
virtual void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
public:
void setup(Variant::Type p_array_type, const String &p_hint_string = "");
virtual void update_property() override;

View file

@ -1 +1 @@
<svg height="16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M1.5 1v3h1a1 1 0 0 1 1-1h2v6a1 1 0 0 1-1 1v1h4v-1a1 1 0 0 1-1-1V3h2a1 1 0 0 1 1 1h1V1h-6Z" fill="#e0e0e0"/><path d="M4.5 5v3h1a1 1 0 0 1 1-1h2v6a1 1 0 0 1-1 1v1h4v-1a1 1 0 0 1-1-1V7h2a1 1 0 0 1 1 1h1V5h-6Z" fill="#ff5f5f"/></svg>
<svg height="16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M7.5 7.5h-1a.519.519 0 0 0-.281.084A.491.491 0 0 0 6 8v.5h-.5V9a1 1 0 0 1-1 1v1H8V9.854A1 1 0 0 1 7.5 9V7.5zM1.5 1v3h1a1 1 0 0 1 1-1h2v1.5h2V3h2a1 1 0 0 1 1 1h1V1h-10z" style="fill:#e0e0e0;fill-opacity:1"/><path d="M4.5 5v3h1a1 1 0 0 1 1-1h2v6a1 1 0 0 1-1 1v1h4v-1a1 1 0 0 1-1-1V7h2a1 1 0 0 1 1 1h1V5h-6z" fill="#ff5f5f"/></svg>

Before

Width:  |  Height:  |  Size: 302 B

After

Width:  |  Height:  |  Size: 401 B

Before After
Before After

View file

@ -1 +1 @@
<svg height="16" width="16" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path d="M2.437 1 1.379 4h1A.84 1.192 50 0 1 3.73 3h2L3.615 9a.84 1.192 50 0 1-1.352 1l-.353 1h4l.353-1a.84 1.192 50 0 1-.648-1l2.116-6h2a.84 1.192 50 0 1 .648 1h1l1.058-3h-6Z" fill="#e0e0e0"/><path d="m4.621 5-.705 2-.353 1h1a.84 1.192 49.998 0 1 1.353-1h2L5.8 13a.84 1.192 49.998 0 1-1.353 1l-.353 1h4l.353-1a.84 1.192 49.998 0 1-.647-1l2.116-6h2a.84 1.192 49.998 0 1 .647 1h1l.353-1 .705-2h-6Z" fill="#ff5f5f"/></svg>
<svg height="16" width="16" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path d="M5.975 11 7.21 7.5H5.916a.478.478 0 0 0-.113.016.837.837 0 0 0-.127.043c-.044.018-.089.04-.133.066l-.043.027V9a1 1 0 0 1-1 1v1h1.475zM1.5 1v3h1a1 1 0 0 1 1-1h2v1.5h2V3h2a1 1 0 0 1 1 1h1V1h-10z" style="fill:#e0e0e0;fill-opacity:1"/><path d="m4.621 5-.705 2-.353 1h1a.84 1.192 49.998 0 1 1.353-1h2L5.8 13a.84 1.192 49.998 0 1-1.353 1l-.353 1h4l.353-1a.84 1.192 49.998 0 1-.647-1l2.116-6h2a.84 1.192 49.998 0 1 .647 1h1l.353-1 .705-2h-6z" fill="#ff5f5f"/></svg>

Before

Width:  |  Height:  |  Size: 505 B

After

Width:  |  Height:  |  Size: 552 B

Before After
Before After

View file

@ -0,0 +1 @@
<svg height="16" width="16" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path style="fill:#e0e0e0;fill-opacity:1;stroke-width:.714755" d="m5.787 1-.402 1.613c-.352.265-.71.122-1.012-.111l-.904-.541L2.46 2.973l.853 1.425c-.058.438-.412.586-.79.635-.343.065-.674.216-1.024.213V6.72c.367 0 .715.157 1.074.224.371.032.716.243.727.65l-.84 1.4 1.008 1.01c.443-.266.895-.53 1.33-.802.349-.044.675.139.674.506l.314 1.258c.459-.059 1.099.115 1.45-.082.117-.475.242-.954.35-1.428A.67.67 0 0 1 8 9.195V7.5H6.5a.519.519 0 0 0-.281.084A.491.491 0 0 0 6 8v.5H4v-4h5.75c-.005-.22.107-.434.254-.625l.543-.902L9.535 1.96l-1.426.853c-.437-.058-.588-.412-.636-.79L7.217 1h-1.43z"/><path d="M4.5 5v3h1a1 1 0 0 1 1-1h2v6a1 1 0 0 1-1 1v1h4v-1a1 1 0 0 1-1-1V7h2a1 1 0 0 1 1 1h1V5h-6z" fill="#ff5f5f"/></svg>

After

Width:  |  Height:  |  Size: 797 B

View file

@ -895,17 +895,45 @@ void FontPreview::_notification(int p_what) {
Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
Color text_color = get_theme_color(SNAME("font_color"), SNAME("Label"));
font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + 2 * EDSCALE), name, HORIZONTAL_ALIGNMENT_CENTER, get_size().x, font_size, text_color);
// Draw font preview.
Vector2 pos = Vector2(0, font->get_height(font_size)) + (get_size() - Vector2(0, font->get_height(font_size)) - line->get_size()) / 2;
line->draw(get_canvas_item(), pos, text_color);
bool prev_ok = true;
if (prev_font.is_valid()) {
if (prev_font->get_font_name().is_empty()) {
prev_ok = false;
} else {
String name;
if (prev_font->get_font_style_name().is_empty()) {
name = prev_font->get_font_name();
} else {
name = vformat("%s (%s)", prev_font->get_font_name(), prev_font->get_font_style_name());
}
if (prev_font->is_class("FontVariation")) {
name += " " + TTR(" - Variation");
}
font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + 2 * EDSCALE), name, HORIZONTAL_ALIGNMENT_CENTER, get_size().x, font_size, text_color);
// Draw font baseline.
Color line_color = text_color;
line_color.a *= 0.6;
draw_line(Vector2(0, pos.y + line->get_line_ascent()), Vector2(pos.x - 5, pos.y + line->get_line_ascent()), line_color);
draw_line(Vector2(pos.x + line->get_size().x + 5, pos.y + line->get_line_ascent()), Vector2(get_size().x, pos.y + line->get_line_ascent()), line_color);
String sample;
static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";
for (int i = 0; i < sample_base.length(); i++) {
if (prev_font->has_char(sample_base[i])) {
sample += sample_base[i];
}
}
if (sample.is_empty()) {
sample = prev_font->get_supported_chars().substr(0, 6);
}
if (sample.is_empty()) {
prev_ok = false;
} else {
prev_font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + prev_font->get_height(50)), sample, HORIZONTAL_ALIGNMENT_CENTER, get_size().x, 50, text_color);
}
}
}
if (!prev_ok) {
text_color.a *= 0.5;
font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + 2 * EDSCALE), TTR("Unable to preview font"), HORIZONTAL_ALIGNMENT_CENTER, get_size().x, font_size, text_color);
}
} break;
}
}
@ -917,30 +945,11 @@ Size2 FontPreview::get_minimum_size() const {
}
void FontPreview::set_data(const Ref<Font> &p_f) {
line->clear();
if (p_f.is_valid()) {
name = vformat("%s (%s)", p_f->get_font_name(), p_f->get_font_style_name());
if (p_f->is_class("FontVariation")) {
name += " " + TTR(" - Variation");
}
String sample;
static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";
for (int i = 0; i < sample_base.length(); i++) {
if (p_f->has_char(sample_base[i])) {
sample += sample_base[i];
}
}
if (sample.is_empty()) {
sample = p_f->get_supported_chars().substr(0, 6);
}
line->add_string(sample, p_f, 50);
}
prev_font = p_f;
update();
}
FontPreview::FontPreview() {
line.instantiate();
}
/*************************************************************************/
@ -964,6 +973,71 @@ bool EditorInspectorPluginFontPreview::parse_property(Object *p_object, const Va
return false;
}
/*************************************************************************/
/* EditorPropertyFontNamesArray */
/*************************************************************************/
void EditorPropertyFontNamesArray::_add_element() {
Size2 size = get_size();
menu->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
menu->reset_size();
menu->popup();
}
void EditorPropertyFontNamesArray::_add_font(int p_option) {
if (updating) {
return;
}
Variant array = object->get_array();
int previous_size = array.call("size");
array.call("resize", previous_size + 1);
array.set(previous_size, menu->get_item_text(p_option));
emit_changed(get_edited_property(), array, "", false);
object->set_array(array);
update_property();
}
EditorPropertyFontNamesArray::EditorPropertyFontNamesArray() {
menu = memnew(PopupMenu);
menu->add_item("Sans-Serif", 0);
menu->add_item("Serif", 1);
menu->add_item("Monospace", 2);
menu->add_item("Fantasy", 3);
menu->add_item("Cursive", 4);
menu->add_separator();
if (OS::get_singleton()) {
Vector<String> fonts = OS::get_singleton()->get_system_fonts();
for (int i = 0; i < fonts.size(); i++) {
menu->add_item(fonts[i], i + 6);
}
}
add_child(menu);
menu->connect("id_pressed", callable_mp(this, &EditorPropertyFontNamesArray::_add_font));
}
/*************************************************************************/
/* EditorInspectorPluginSystemFont */
/*************************************************************************/
bool EditorInspectorPluginSystemFont::can_handle(Object *p_object) {
return Object::cast_to<SystemFont>(p_object) != nullptr;
}
bool EditorInspectorPluginSystemFont::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
if (p_path == "font_names") {
EditorPropertyFontNamesArray *editor = memnew(EditorPropertyFontNamesArray);
editor->setup(p_type, p_hint_text);
add_property_editor(p_path, editor);
return true;
}
return false;
}
/*************************************************************************/
/* FontEditorPlugin */
/*************************************************************************/
@ -973,6 +1047,10 @@ FontEditorPlugin::FontEditorPlugin() {
fc_plugin.instantiate();
EditorInspector::add_inspector_plugin(fc_plugin);
Ref<EditorInspectorPluginSystemFont> fs_plugin;
fs_plugin.instantiate();
EditorInspector::add_inspector_plugin(fs_plugin);
Ref<EditorInspectorPluginFontPreview> fp_plugin;
fp_plugin.instantiate();
EditorInspector::add_inspector_plugin(fp_plugin);

View file

@ -34,6 +34,7 @@
#include "core/io/marshalls.h"
#include "editor/editor_plugin.h"
#include "editor/editor_properties.h"
#include "editor/editor_properties_array_dict.h"
/*************************************************************************/
@ -225,8 +226,7 @@ protected:
void _notification(int p_what);
static void _bind_methods();
String name;
Ref<TextLine> line;
Ref<Font> prev_font;
public:
virtual Size2 get_minimum_size() const override;
@ -249,6 +249,33 @@ public:
/*************************************************************************/
class EditorPropertyFontNamesArray : public EditorPropertyArray {
GDCLASS(EditorPropertyFontNamesArray, EditorPropertyArray);
PopupMenu *menu = nullptr;
protected:
virtual void _add_element() override;
void _add_font(int p_option);
static void _bind_methods(){};
public:
EditorPropertyFontNamesArray();
};
/*************************************************************************/
class EditorInspectorPluginSystemFont : public EditorInspectorPlugin {
GDCLASS(EditorInspectorPluginSystemFont, EditorInspectorPlugin);
public:
virtual bool can_handle(Object *p_object) override;
virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide = false) override;
};
/*************************************************************************/
class FontEditorPlugin : public EditorPlugin {
GDCLASS(FontEditorPlugin, EditorPlugin);