Class: Ruby2D::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/font.rb

Class Method Summary collapse

Class Method Details

.allObject

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_pathsObject

Get all fonts with full file paths



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby2d/font.rb', line 19

def all_paths
  fonts = `find #{directory} -name *.ttf`.split("\n")
  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

.defaultObject

Get the default font



32
33
34
35
36
37
38
# File 'lib/ruby2d/font.rb', line 32

def default
  if all.include? 'arial'
    path 'arial'
  else
    all_paths.first
  end
end

.directoryObject

Get the fonts directory for the current platform



41
42
43
44
45
46
47
48
49
# File 'lib/ruby2d/font.rb', line 41

def directory
  if `uname`.include? 'Darwin'  # macOS
      "/Library/Fonts"
  elsif `uname`.include? 'Linux'
      "/usr/share/fonts/truetype"
  elsif `uname`.include? 'MINGW'
      "C:/Windows/Fonts"
  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