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 --- bin/ruby2d | 1 + lib/ruby2d.rb | 1 + lib/ruby2d/font.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/ruby2d/text.rb | 2 +- test/testcard.rb | 3 +++ test/text_spec.rb | 7 ++++++- 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 lib/ruby2d/font.rb diff --git a/bin/ruby2d b/bin/ruby2d index 7437a92..3350898 100755 --- a/bin/ruby2d +++ b/bin/ruby2d @@ -27,6 +27,7 @@ end 'triangle', 'image', 'sprite', + 'font', 'text', 'sound', 'music' 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 diff --git a/test/testcard.rb b/test/testcard.rb index c433aa4..2b182cf 100644 --- a/test/testcard.rb +++ b/test/testcard.rb @@ -243,6 +243,9 @@ flash = 0 time_start = Time.now +# Default font for text +Text.new(x: 150, y: 470, text: "Default font", size: 20) + # Text size created_text = Text.new(x: 10, y: 270, text: "Created text", font: font) created_text_background = Rectangle.new( diff --git a/test/text_spec.rb b/test/text_spec.rb index 9a79532..67afe0e 100644 --- a/test/text_spec.rb +++ b/test/text_spec.rb @@ -6,10 +6,15 @@ RSpec.describe Ruby2D::Text do it "raises exception if font file doesn't exist" do expect { Text.new(font: "bad_font.ttf") }.to raise_error(Ruby2D::Error) end + + it "uses the system default font if one is not provided" do + t = Text.new + expect(t.font).to eq(Font.default) + end end describe "#text=" do - it 'maps Time to string' do + it "maps Time to string" do t = Text.new(font: "test/media/bitstream_vera/vera.ttf") t.text = Time.new(1, 1, 1, 1, 1, 1, 1) expect(t.text).to eq("0001-01-01 01:01:01 +0000") -- cgit v1.2.3