Skip to content

Commit 2539982

Browse files
Implement the ability to load font files
Users can now drag and drop font files to load them
1 parent 601fdea commit 2539982

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Autoload/Global.gd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ const HOME_SUBDIR_NAME := "pixelorama"
121121
const CONFIG_SUBDIR_NAME := "pixelorama_data"
122122
## The path of the directory where the UI layouts are being stored.
123123
const LAYOUT_DIR := "user://layouts"
124+
## The path of the ditectory where users can load custom fonts.
125+
const FONTS_DIR_PATH := "user://fonts"
124126

125127
## It is path to the executable's base drectory.
126128
var root_directory := "."
@@ -769,6 +771,13 @@ func _init() -> void:
769771

770772

771773
func _ready() -> void:
774+
if DirAccess.dir_exists_absolute(FONTS_DIR_PATH):
775+
var fonts_dir := DirAccess.open(FONTS_DIR_PATH)
776+
var files := fonts_dir.get_files()
777+
for file in files:
778+
var font_file := OpenSave.open_font_file(FONTS_DIR_PATH.path_join(file))
779+
if not font_file.data.is_empty():
780+
loaded_fonts.append(font_file)
772781
# Initialize Grid
773782
Grid.new() # gets auto added to grids array
774783
_initialize_keychain()

src/Autoload/OpenSave.gd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ signal shader_copied(file_path: String)
77

88
const BACKUPS_DIRECTORY := "user://backups"
99
const SHADERS_DIRECTORY := "user://shaders"
10+
const FONT_FILE_EXTENSIONS: PackedStringArray = [
11+
"ttf", "otf", "woff", "woff2", "pfb", "pfm", "fnt", "font"
12+
]
1013

1114
var current_session_backup := ""
1215
var preview_dialog_tscn := preload("res://src/UI/Dialogs/ImportPreviewDialog.tscn")
@@ -87,6 +90,15 @@ func handle_loading_file(file: String, force_import_dialog_on_images := false) -
8790
shader_copied.emit(new_path)
8891
elif file_ext == "mp3" or file_ext == "wav": # Audio file
8992
open_audio_file(file)
93+
elif file_ext in FONT_FILE_EXTENSIONS:
94+
var font_file := open_font_file(file)
95+
if font_file.data.is_empty():
96+
return
97+
if not DirAccess.dir_exists_absolute(Global.FONTS_DIR_PATH):
98+
DirAccess.make_dir_absolute(Global.FONTS_DIR_PATH)
99+
var new_path := Global.FONTS_DIR_PATH.path_join(file.get_file())
100+
DirAccess.copy_absolute(file, new_path)
101+
Global.loaded_fonts.append(font_file)
90102
elif file_ext == "ora":
91103
open_ora_file(file)
92104
elif file_ext == "kra":
@@ -1008,6 +1020,15 @@ func open_audio_file(path: String) -> void:
10081020
Global.animation_timeline.add_layer(new_layer, project)
10091021

10101022

1023+
func open_font_file(path: String) -> FontFile:
1024+
var font_file := FontFile.new()
1025+
if path.to_lower().get_extension() == "fnt" or path.to_lower().get_extension() == "font":
1026+
font_file.load_bitmap_font(path)
1027+
else:
1028+
font_file.load_dynamic_font(path)
1029+
return font_file
1030+
1031+
10111032
# Based on https://www.openraster.org/
10121033
func open_ora_file(path: String) -> void:
10131034
var zip_reader := ZIPReader.new()

0 commit comments

Comments
 (0)