Class: Ruby2D::Font

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

Overview

System-font discovery: list the available fonts, resolve a name to a file path, and locate the bundled default. Font files are opened and cached natively (see R2D_FontCacheGet in the C extension), keyed by path, size, and style — there are no Ruby-side Font instances.

Class Method Summary collapse

Class Method Details

.allObject

List all fonts, names only



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

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

.defaultObject

Get full path to the default font



24
25
26
27
28
29
30
31
# File 'lib/ruby2d/font.rb', line 24

def default
  if RUBY_ENGINE == 'mruby'
    # Native and WASM builds bundle fonts at ruby2d/fonts/ relative to the binary
    'ruby2d/fonts/outfit/outfit.ttf'
  else
    File.expand_path('../../assets/resources/fonts/outfit/outfit.ttf', __dir__)
  end
end

.path(font_name) ⇒ Object

Find a font file path from its name (case-insensitive, matching all)



16
17
18
19
20
21
# File 'lib/ruby2d/font.rb', line 16

def path(font_name)
  font_name = font_name.to_s.downcase
  # Match against the file's basename (as `all` computes names), not the
  # whole path — otherwise a directory component like 'fonts' matches.
  all_paths.find { |path| path.split('/').last.chomp('.ttf').downcase.include?(font_name) }
end