Runtime: locate font files
This commit is contained in:
parent
4431cf4b29
commit
447e4279e7
@ -8,6 +8,14 @@ module Runtime
|
|||||||
@paths ||= default_paths
|
@paths ||= default_paths
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Possible extensions for fonts.
|
||||||
|
#
|
||||||
|
# @return [Array<String>]
|
||||||
|
# Possible extensions for font files.
|
||||||
|
def font_extensions
|
||||||
|
@font_extensions ||= ["otf", "ttf"]
|
||||||
|
end
|
||||||
|
|
||||||
# Find a runtime file.
|
# Find a runtime file.
|
||||||
#
|
#
|
||||||
# @param type [Symbol]
|
# @param type [Symbol]
|
||||||
@ -60,15 +68,53 @@ module Runtime
|
|||||||
case type
|
case type
|
||||||
when :shader
|
when :shader
|
||||||
File.join(runtime_path, "shaders", name)
|
File.join(runtime_path, "shaders", name)
|
||||||
|
when :font
|
||||||
|
fonts_dir = File.join(runtime_path, "fonts")
|
||||||
|
if File.directory?(fonts_dir)
|
||||||
|
if path = find_with_ext(fonts_dir, name, font_extensions)
|
||||||
|
path
|
||||||
|
else
|
||||||
|
path = nil
|
||||||
|
Dir.entries(fonts_dir).find do |dirent|
|
||||||
|
next if dirent.start_with?(".")
|
||||||
|
dir = File.join(fonts_dir, dirent)
|
||||||
|
next unless File.directory?(dir)
|
||||||
|
path = find_with_ext(dir, name, font_extensions)
|
||||||
|
end
|
||||||
|
path
|
||||||
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise "Unknown runtime type: #{type.inspect}"
|
raise "Unknown runtime type: #{type.inspect}"
|
||||||
end
|
end
|
||||||
if File.exists?(path)
|
if path and File.exists?(path)
|
||||||
path
|
path
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Look for a runtime file with multiple possible extensions.
|
||||||
|
#
|
||||||
|
# @param dir [String]
|
||||||
|
# The directory to look in.
|
||||||
|
# @param name [String]
|
||||||
|
# The basename of the file to look for.
|
||||||
|
# @extensions [Array<String>]
|
||||||
|
# The possible file extensions.
|
||||||
|
#
|
||||||
|
# @return [String, nil]
|
||||||
|
# Path to the file found, or +nil+ if a file was not found.
|
||||||
|
def find_with_ext(dir, name, extensions)
|
||||||
|
path = nil
|
||||||
|
([""] + extensions.map {|e| ".#{e}"}).find do |ext|
|
||||||
|
p = File.join(dir, name + ext)
|
||||||
|
if File.file?(p)
|
||||||
|
path = p
|
||||||
|
end
|
||||||
|
end
|
||||||
|
path
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user