Sha256: 9d8e2ceb58df3de8bf6fe2867ea05bd79dc5528b0cd607bbc4fb1ea3c5ee69e6

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# 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
        # MRuby does not have `Dir` defined
        if RUBY_ENGINE == 'mruby'
          fonts = `find #{directory} -name *.ttf`.split("\n")
        # 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.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
        macos_font_path   = '/Library/Fonts'
        linux_font_path   = '/usr/share/fonts'
        windows_font_path = 'C:/Windows/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
          end
        # If MRuby
        else
          uname = `uname`
          if uname.include? 'Darwin' # macOS
            macos_font_path
          elsif uname.include? 'Linux'
            linux_font_path
          elsif uname.include? 'MINGW'
            windows_font_path
          end
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby2d-0.10.0 lib/ruby2d/font.rb