Class: SFML::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/graphics/font.rb

Overview

A typeface loaded from a TTF/OTF file.

font = SFML::Font.default                                                # bundled DejaVu Sans
font = SFML::Font.load("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf")
font = SFML::Font.find("DejaVuSans")                                     # search common locations

Constant Summary collapse

SEARCH_PATHS =
[
  "/usr/share/fonts",
  "/usr/local/share/fonts",
  "/Library/Fonts",
  "/System/Library/Fonts",
  File.expand_path("~/Library/Fonts"),
  "C:/Windows/Fonts",
].freeze
DEFAULT_PATH =

Path to the font ruby-sfml ships with: DejaVu Sans (Bitstream Vera license, redistributable). See lib/sfml/assets/fonts/DejaVuSans.LICENSE.txt.

File.expand_path("../assets/fonts/DejaVuSans.ttf", __dir__).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handleObject (readonly)

:nodoc:



59
60
61
# File 'lib/sfml/graphics/font.rb', line 59

def handle
  @handle
end

Class Method Details

.defaultObject

The default font bundled with ruby-sfml. Use this when you don’t care which typeface as long as you can render text — examples, debug HUDs, prototypes. Memoized so subsequent calls return the same Font instance.



34
35
36
# File 'lib/sfml/graphics/font.rb', line 34

def self.default
  @default ||= load(DEFAULT_PATH)
end

.find(name) ⇒ Object

Look up a font on disk by basename (with or without extension). Useful for examples that should “just run” — production code should ship its own font files. Returns nil if nothing is found.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sfml/graphics/font.rb', line 41

def self.find(name)
  target = name.to_s.downcase.sub(/\.(ttf|otf)\z/, "")
  SEARCH_PATHS.each do |dir|
    next unless File.directory?(dir)
    match = Dir.glob(File.join(dir, "**", "*.{ttf,otf}")).find do |path|
      File.basename(path).downcase.sub(/\.(ttf|otf)\z/, "") == target
    end
    return load(match) if match
  end
  nil
end

.load(path) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
# File 'lib/sfml/graphics/font.rb', line 21

def self.load(path)
  ptr = C::Graphics.sfFont_createFromFile(path.to_s)
  raise Error, "Could not load font from #{path.inspect}" if ptr.null?

  font = allocate
  font.send(:_take_ownership, ptr)
  font
end

Instance Method Details

#smooth=(value) ⇒ Object



55
56
57
# File 'lib/sfml/graphics/font.rb', line 55

def smooth=(value)
  C::Graphics.sfFont_setSmooth(@handle, !!value)
end

#smooth?Boolean

Returns:

  • (Boolean)


53
# File 'lib/sfml/graphics/font.rb', line 53

def smooth?       = C::Graphics.sfFont_isSmooth(@handle)