From b4bbf346ea7f124c8614467a0e47bd4062504d63 Mon Sep 17 00:00:00 2001 From: lstrzebinczyk Date: Sat, 29 Apr 2017 20:16:05 +0200 Subject: Text will use default font --- lib/ruby2d.rb | 1 + lib/ruby2d/font.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/ruby2d/text.rb | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 lib/ruby2d/font.rb (limited to 'lib') diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb index 1d48ab8..58a69da 100644 --- a/lib/ruby2d.rb +++ b/lib/ruby2d.rb @@ -13,6 +13,7 @@ require 'ruby2d/square' require 'ruby2d/triangle' require 'ruby2d/image' require 'ruby2d/sprite' +require 'ruby2d/font' require 'ruby2d/text' require 'ruby2d/sound' require 'ruby2d/music' diff --git a/lib/ruby2d/font.rb b/lib/ruby2d/font.rb new file mode 100644 index 0000000..0459c30 --- /dev/null +++ b/lib/ruby2d/font.rb @@ -0,0 +1,54 @@ +# Ruby2D::Font + +module Ruby2D + class Font + + class << self + + # List all fonts, names only + def all + all_paths.map { |path| path.split('/').last.chomp('.ttf').downcase }.uniq.sort + end + + # Find a font file path from its name + def path(font_name) + all_paths.find { |path| path.downcase.include?(font_name) } + end + + # Get all fonts with full file paths + 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 + + # Get the default font + def default + if all.include? 'arial' + path 'arial' + else + all_paths.first + end + end + + # Get the fonts directory for the current platform + 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 + + end + + end +end diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 1b2207d..f3f0580 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -14,7 +14,7 @@ module Ruby2D @text = (opts[:text] || "Hello Ruby!").to_s @size = opts[:size] || 20 @rotate = opts[:rotate] || 0 - @font = opts[:font] + @font = opts[:font] || Font.default unless RUBY_ENGINE == 'opal' unless File.exists? @font -- cgit v1.2.3