Skip to content

Commit

Permalink
looking for fonts in two more folders on macOS; also code style and g…
Browse files Browse the repository at this point in the history
…ot rid of one method
  • Loading branch information
Nakilon committed May 11, 2019
1 parent 43ce980 commit a80fa4b
Showing 1 changed file with 42 additions and 56 deletions.
98 changes: 42 additions & 56 deletions lib/ruby2d/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,60 @@ class Font

class << self

# List all fonts, names only
def all
all_paths.map { |path| path.split('/').last.chomp('.ttf').downcase }.uniq.sort
end

# Find a font file path from its name
def path(font_name)
all_paths.find { |path| path.downcase.include?(font_name) }
end

# Get all fonts with full file paths
def all_paths
# MRuby does not have `Dir` defined
if RUBY_ENGINE == 'mruby'
fonts = `find #{directory} -name *.ttf`.split("\n")
# If MRI and/or non-Bash shell (like cmd.exe)
else
fonts = Dir["#{directory}/**/*.ttf"]
end

fonts = fonts.reject do |f|
f.downcase.include?('bold') ||
f.downcase.include?('italic') ||
f.downcase.include?('oblique') ||
f.downcase.include?('narrow') ||
f.downcase.include?('black')
end

fonts.sort_by { |f| f.downcase.chomp '.ttf' }
end

# Get the default font
def default
if all.include? 'arial'
path 'arial'
else
all_paths.first
end
end

# Get the fonts directory for the current platform
def directory
macos_font_path = '/Library/Fonts'
linux_font_path = '/usr/share/fonts'
windows_font_path = 'C:/Windows/Fonts'
macos_font_paths = ["/Library/Fonts", "#{Dir.home}/Library/Fonts"]
linux_font_path = %w{ /usr/share/fonts }
windows_font_path = %w{ C:/Windows/Fonts }

# If MRI and/or non-Bash shell (like cmd.exe)
if Object.const_defined? :RUBY_PLATFORM
[Dir.pwd, *if Object.const_defined? :RUBY_PLATFORM
case RUBY_PLATFORM
when /darwin/ # macOS
macos_font_path
when /linux/
linux_font_path
when /mingw/
windows_font_path
when /darwin/ ; macos_font_paths
when /linux/ ; linux_font_path
when /mingw/ ; windows_font_path
else ; raise Ruby2D::Error, "unknown Ruby platform #{RUBY_PLATFORM}"
end
# If MRuby
else
if `uname`.include? 'Darwin' # macOS
macos_font_path
elsif `uname`.include? 'Linux'
linux_font_path
elsif `uname`.include? 'MINGW'
windows_font_path
case `uname`
when /Darwin/ ; macos_font_paths
when /Linux/ ; linux_font_path
when /MINGW/ ; windows_font_path
else ; raise Ruby2D::Error, "unknown Ruby platform #{`uname`}"
end
end].flat_map do |dir|
# MRuby does not have `Dir` defined
if RUBY_ENGINE == "mruby"
`find #{dir} -name *.ttf`.split "\n"
# If MRI and/or non-Bash shell (like cmd.exe)
else
Dir["#{dir}/**/*.ttf"]
end
end.reject do |path|
path.downcase.include?("bold") ||
path.downcase.include?("italic") ||
path.downcase.include?("oblique") ||
path.downcase.include?("narrow") ||
path.downcase.include?("black")
end.sort_by do |path|
File.basename path.downcase, ".ttf"
end
end

# Find a font file path from its name
def path font_name
all_paths.find{ |path| path.downcase.include? font_name.downcase }
end

# Get the default font
def default
paths = all_paths
paths.find do |path|
"arial" == File.basename(path.downcase, ".ttf")
end or paths.first
end

end

end
Expand Down

0 comments on commit a80fa4b

Please sign in to comment.