Class: Ruby2D::Font
- Inherits:
-
Object
- Object
- Ruby2D::Font
- Defined in:
- lib/ruby2d/font.rb
Constant Summary collapse
- @@loaded_fonts =
{}
Instance Attribute Summary collapse
-
#ttf_font ⇒ Object
readonly
Returns the value of attribute ttf_font.
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.
- .load(path, size, style = nil) ⇒ Object
-
.path(font_name) ⇒ Object
Find a font file path from its name.
Instance Method Summary collapse
-
#initialize(path, size, style = nil) ⇒ Font
constructor
A new instance of Font.
Constructor Details
Instance Attribute Details
#ttf_font ⇒ Object (readonly)
Returns the value of attribute ttf_font.
7 8 9 |
# File 'lib/ruby2d/font.rb', line 7 def ttf_font @ttf_font end |
Class Method Details
.all ⇒ Object
List all fonts, names only
23 24 25 |
# File 'lib/ruby2d/font.rb', line 23 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ruby2d/font.rb', line 35 def all_paths if RUBY_ENGINE == 'mruby' fonts = Dir.entries(directory) # 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.select {|font| File.extname(font) == ".ttf" }.sort_by { |f| f.downcase.chomp '.ttf' } end |
.default ⇒ Object
Get the default font
55 56 57 58 59 60 61 |
# File 'lib/ruby2d/font.rb', line 55 def default if all.include? 'arial' path 'arial' else all_paths.first end end |
.directory ⇒ Object
Get the fonts directory for the current platform
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby2d/font.rb', line 64 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 # RPECK 23/12/2021 -- the uname command was not available on Windows [macos_font_path, linux_font_path, windows_font_path, openbsd_font_path].each do |folder| return folder if File.exists? folder end end end |
.load(path, size, style = nil) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/ruby2d/font.rb', line 14 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) end |
.path(font_name) ⇒ Object
Find a font file path from its name
28 29 30 31 32 |
# File 'lib/ruby2d/font.rb', line 28 def path(font_name) font = all_paths.find { |path| path.downcase.include?(font_name) } font = File.join(directory, font) if directory.include?('Windows') && RUBY_ENGINE == 'mruby' font end |