summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorlstrzebinczyk <[email protected]>2017-04-29 20:16:05 +0200
committerTom Black <[email protected]>2018-10-04 23:22:16 -0700
commitb4bbf346ea7f124c8614467a0e47bd4062504d63 (patch)
tree70b593e756530e409a3aa524c3f0c1c9a8a5c868 /lib
parent691ae68db759b6cbab5a4c1a9cee4dc472d4435f (diff)
downloadruby2d-b4bbf346ea7f124c8614467a0e47bd4062504d63.tar.gz
ruby2d-b4bbf346ea7f124c8614467a0e47bd4062504d63.zip
Text will use default font
Diffstat (limited to 'lib')
-rw-r--r--lib/ruby2d.rb1
-rw-r--r--lib/ruby2d/font.rb54
-rw-r--r--lib/ruby2d/text.rb2
3 files changed, 56 insertions, 1 deletions
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