Runtime: locate font files

This commit is contained in:
Josh Holtrop 2014-07-15 16:54:12 -04:00
parent 4431cf4b29
commit 447e4279e7

View File

@ -8,6 +8,14 @@ module Runtime
@paths ||= default_paths
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.
#
# @param type [Symbol]
@ -60,15 +68,53 @@ module Runtime
case type
when :shader
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
raise "Unknown runtime type: #{type.inspect}"
end
if File.exists?(path)
if path and File.exists?(path)
path
else
nil
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