From 3341f29e4e3e6f470534bf5843863138bd3a1778 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Tue, 19 Apr 2016 22:52:26 -0400 Subject: Fixing font file path and error checking --- lib/ruby2d/text.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 2dd36e9..93c2862 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -5,20 +5,17 @@ module Ruby2D attr_accessor :x, :y, :size, :text - def initialize(x=0, y=0, size=20, text="Hello World!", font="Arial", c="white") - if font.include? '.' - unless File.exists? font - raise Error, "Cannot find font file!" - else - @font = font - end + def initialize(x=0, y=0, size=20, msg="Hello World!", font="Arial", c="white") + + if File.exists? font + @font = font else @font = resolve_path(font) end @type_id = 4 @x, @y, @size = x, y, size - @text, @color = text, c + @text, @color = msg, c update_color(c) if defined? Ruby2D::DSL @@ -44,14 +41,18 @@ module Ruby2D private def resolve_path(font) - # TODO: Consider CSS names, like 'serif', 'san-serif', 'monospace' if RUBY_PLATFORM =~ /darwin/ font_path = "/Library/Fonts/#{font}.ttf" - unless File.exists? font_path - raise Error, "Cannot find system font!" - end + else + # Linux + font_path = "/usr/share/fonts/truetype/#{font}.ttf" + end + + unless File.exists? font_path + raise Error, "Cannot find system font" + else + font_path end - font_path end def update_color(c) -- cgit v1.2.3