@@ -5,74 +5,60 @@ class Font
5
5
6
6
class << self
7
7
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
-
18
8
# Get all fonts with full file paths
19
9
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 }
53
13
54
14
# 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
56
16
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 } "
63
21
end
64
22
# If MRuby
65
23
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" ]
72
37
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"
73
46
end
74
47
end
75
48
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
+
76
62
end
77
63
78
64
end
0 commit comments