Class: Ruby2D::Font
- Inherits:
-
Object
- Object
- Ruby2D::Font
- Defined in:
- lib/ruby2d/font.rb
Class Method Summary collapse
-
.all ⇒ Object
List all fonts, names only.
-
.all_paths ⇒ Object
Get all fonts with full file paths.
-
.default ⇒ Object
Get the default font.
-
.directory ⇒ Object
Get the fonts directory for the current platform.
-
.path(font_name) ⇒ Object
Find a font file path from its name.
Class Method Details
.all ⇒ Object
List all fonts, names only
9 10 11 |
# File 'lib/ruby2d/font.rb', line 9 def all all_paths.map { |path| path.split('/').last.chomp('.ttf').downcase }.uniq.sort end |
.all_paths ⇒ Object
Get all fonts with full file paths
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby2d/font.rb', line 19 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 |
.default ⇒ Object
Get the default font
40 41 42 43 44 45 46 |
# File 'lib/ruby2d/font.rb', line 40 def default if all.include? 'arial' path 'arial' else all_paths.first end end |
.directory ⇒ Object
Get the fonts directory for the current platform
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruby2d/font.rb', line 49 def directory macos_font_path = '/Library/Fonts' linux_font_path = '/usr/share/fonts' windows_font_path = 'C:/Windows/Fonts' # If MRI and/or non-Bash shell (like cmd.exe) 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 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 end end end |
.path(font_name) ⇒ Object
Find a font file path from its name
14 15 16 |
# File 'lib/ruby2d/font.rb', line 14 def path(font_name) all_paths.find { |path| path.downcase.include?(font_name) } end |