From 447e4279e72022b819bb69e6488f0fe143908a85 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 15 Jul 2014 16:54:12 -0400 Subject: [PATCH] Runtime: locate font files --- runtime/lib/runtime.rb | 48 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/runtime/lib/runtime.rb b/runtime/lib/runtime.rb index ee96c2c..7524aa1 100644 --- a/runtime/lib/runtime.rb +++ b/runtime/lib/runtime.rb @@ -8,6 +8,14 @@ module Runtime @paths ||= default_paths end + # Possible extensions for fonts. + # + # @return [Array] + # 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] + # 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