Skip to content

Commit a80fa4b

Browse files
committed
looking for fonts in two more folders on macOS; also code style and got rid of one method
1 parent 43ce980 commit a80fa4b

File tree

1 file changed

+42
-56
lines changed

1 file changed

+42
-56
lines changed

lib/ruby2d/font.rb

+42-56
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,60 @@ class Font
55

66
class << self
77

8-
# List all fonts, names only
9-
def all
10-
all_paths.map { |path| path.split('/').last.chomp('.ttf').downcase }.uniq.sort
11-
end
12-
13-
# Find a font file path from its name
14-
def path(font_name)
15-
all_paths.find { |path| path.downcase.include?(font_name) }
16-
end
17-
188
# Get all fonts with full file paths
199
def all_paths
20-
# MRuby does not have `Dir` defined
21-
if RUBY_ENGINE == 'mruby'
22-
fonts = `find #{directory} -name *.ttf`.split("\n")
23-
# If MRI and/or non-Bash shell (like cmd.exe)
24-
else
25-
fonts = Dir["#{directory}/**/*.ttf"]
26-
end
27-
28-
fonts = fonts.reject do |f|
29-
f.downcase.include?('bold') ||
30-
f.downcase.include?('italic') ||
31-
f.downcase.include?('oblique') ||
32-
f.downcase.include?('narrow') ||
33-
f.downcase.include?('black')
34-
end
35-
36-
fonts.sort_by { |f| f.downcase.chomp '.ttf' }
37-
end
38-
39-
# Get the default font
40-
def default
41-
if all.include? 'arial'
42-
path 'arial'
43-
else
44-
all_paths.first
45-
end
46-
end
47-
48-
# Get the fonts directory for the current platform
49-
def directory
50-
macos_font_path = '/Library/Fonts'
51-
linux_font_path = '/usr/share/fonts'
52-
windows_font_path = 'C:/Windows/Fonts'
10+
macos_font_paths = ["/Library/Fonts", "#{Dir.home}/Library/Fonts"]
11+
linux_font_path = %w{ /usr/share/fonts }
12+
windows_font_path = %w{ C:/Windows/Fonts }
5313

5414
# If MRI and/or non-Bash shell (like cmd.exe)
55-
if Object.const_defined? :RUBY_PLATFORM
15+
[Dir.pwd, *if Object.const_defined? :RUBY_PLATFORM
5616
case RUBY_PLATFORM
57-
when /darwin/ # macOS
58-
macos_font_path
59-
when /linux/
60-
linux_font_path
61-
when /mingw/
62-
windows_font_path
17+
when /darwin/ ; macos_font_paths
18+
when /linux/ ; linux_font_path
19+
when /mingw/ ; windows_font_path
20+
else ; raise Ruby2D::Error, "unknown Ruby platform #{RUBY_PLATFORM}"
6321
end
6422
# If MRuby
6523
else
66-
if `uname`.include? 'Darwin' # macOS
67-
macos_font_path
68-
elsif `uname`.include? 'Linux'
69-
linux_font_path
70-
elsif `uname`.include? 'MINGW'
71-
windows_font_path
24+
case `uname`
25+
when /Darwin/ ; macos_font_paths
26+
when /Linux/ ; linux_font_path
27+
when /MINGW/ ; windows_font_path
28+
else ; raise Ruby2D::Error, "unknown Ruby platform #{`uname`}"
29+
end
30+
end].flat_map do |dir|
31+
# MRuby does not have `Dir` defined
32+
if RUBY_ENGINE == "mruby"
33+
`find #{dir} -name *.ttf`.split "\n"
34+
# If MRI and/or non-Bash shell (like cmd.exe)
35+
else
36+
Dir["#{dir}/**/*.ttf"]
7237
end
38+
end.reject do |path|
39+
path.downcase.include?("bold") ||
40+
path.downcase.include?("italic") ||
41+
path.downcase.include?("oblique") ||
42+
path.downcase.include?("narrow") ||
43+
path.downcase.include?("black")
44+
end.sort_by do |path|
45+
File.basename path.downcase, ".ttf"
7346
end
7447
end
7548

49+
# Find a font file path from its name
50+
def path font_name
51+
all_paths.find{ |path| path.downcase.include? font_name.downcase }
52+
end
53+
54+
# Get the default font
55+
def default
56+
paths = all_paths
57+
paths.find do |path|
58+
"arial" == File.basename(path.downcase, ".ttf")
59+
end or paths.first
60+
end
61+
7662
end
7763

7864
end

0 commit comments

Comments
 (0)