Class: Ruby2D::Font

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

Constant Summary collapse

FONT_CACHE_LIMIT =
100
@@loaded_fonts =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, size, style = nil) ⇒ Font

Returns a new instance of Font.



11
12
13
# File 'lib/ruby2d/font.rb', line 11

def initialize(path, size, style=nil)
  @ttf_font = Font.ext_load(path, size, style.to_s)
end

Instance Attribute Details

#ttf_fontObject (readonly)

Returns the value of attribute ttf_font.



9
10
11
# File 'lib/ruby2d/font.rb', line 9

def ttf_font
  @ttf_font
end

Class Method Details

.allObject

List all fonts, names only



29
30
31
# File 'lib/ruby2d/font.rb', line 29

def all
  all_paths.map { |path| path.split('/').last.chomp('.ttf').downcase }.uniq.sort
end

.all_pathsObject

Get all fonts with full file paths



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby2d/font.rb', line 39

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

.defaultObject

Get the default font



60
61
62
63
64
65
66
# File 'lib/ruby2d/font.rb', line 60

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

.directoryObject

Get the fonts directory for the current platform



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby2d/font.rb', line 69

def directory
  macos_font_path   = '/Library/Fonts'
  linux_font_path   = '/usr/share/fonts'
  windows_font_path = 'C:/Windows/Fonts'
  openbsd_font_path = '/usr/X11R6/lib/X11/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
    when /openbsd/
      openbsd_font_path
    end
  # If MRuby
  else
    uname = `uname`
    if uname.include? 'Darwin'  # macOS
      macos_font_path
    elsif uname.include? 'Linux'
      linux_font_path
    elsif uname.include? 'MINGW'
      windows_font_path
    elsif uname.include? 'OpenBSD'
      openbsd_font_path
    end
  end
end

.load(path, size, style = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby2d/font.rb', line 16

def load(path, size, style=nil)
  unless File.exist? path
    raise Error, "Cannot find font file `#{path}`"
  end

  (@@loaded_fonts[[path, size, style]] ||= Font.new(path, size, style)).tap do |font|
    if @@loaded_fonts.size > FONT_CACHE_LIMIT
      @@loaded_fonts.shift
    end
  end
end

.path(font_name) ⇒ Object

Find a font file path from its name



34
35
36
# File 'lib/ruby2d/font.rb', line 34

def path(font_name)
  all_paths.find { |path| path.downcase.include?(font_name) }
end