Class: SFML::Font
- Inherits:
-
Object
- Object
- SFML::Font
- Defined in:
- lib/sfml/graphics/font.rb
Overview
Constant Summary collapse
- SEARCH_PATHS =
[ "/usr/share/fonts", "/usr/local/share/fonts", "/Library/Fonts", "/System/Library/Fonts", File.("~/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.("../assets/fonts/DejaVuSans.ttf", __dir__).freeze
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.default ⇒ Object
The default font bundled with ruby-sfml.
-
.find(name) ⇒ Object
Look up a font on disk by basename (with or without extension).
-
.from_memory(bytes) ⇒ Object
Load a font from a Ruby String of bytes — useful when the font lives inside a ‘data:` URL, an embedded asset, or a network response.
- .load(path) ⇒ Object
Instance Method Summary collapse
-
#dup ⇒ Object
(also: #clone)
Deep copy.
-
#family ⇒ Object
Human-readable family name (e.g. “DejaVu Sans”).
-
#has_glyph?(codepoint) ⇒ Boolean
‘true` if the font has a glyph for the given Unicode codepoint (Integer) or single-character String.
-
#kerning(first, second, character_size:, bold: false) ⇒ Object
Horizontal kerning offset between two adjacent glyphs at the given character size.
-
#line_spacing(character_size) ⇒ Object
Distance between two consecutive baselines for the given character size.
- #smooth=(value) ⇒ Object
- #smooth? ⇒ Boolean
-
#texture(character_size) ⇒ Object
The internal glyph atlas as a ‘SFML::Texture` (read-only — we don’t own the pointer; CSFML keeps it alive as long as the font does).
-
#underline_position(character_size) ⇒ Object
Vertical offset of the underline from the baseline (positive values point downward).
-
#underline_thickness(character_size) ⇒ Object
Thickness of the underline stroke.
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
139 140 141 |
# File 'lib/sfml/graphics/font.rb', line 139 def handle @handle end |
Class Method Details
.default ⇒ Object
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.
51 52 53 |
# File 'lib/sfml/graphics/font.rb', line 51 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.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sfml/graphics/font.rb', line 58 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 |
.from_memory(bytes) ⇒ Object
Load a font from a Ruby String of bytes — useful when the font lives inside a ‘data:` URL, an embedded asset, or a network response. The bytes are copied by SFML before this call returns; the caller’s String can be GC’d safely.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sfml/graphics/font.rb', line 34 def self.from_memory(bytes) raise ArgumentError, "expected a String, got #{bytes.class}" unless bytes.is_a?(String) buf = FFI::MemoryPointer.new(:uint8, bytes.bytesize) buf.write_bytes(bytes) ptr = C::Graphics.sfFont_createFromMemory(buf, bytes.bytesize) raise Error, "sfFont_createFromMemory returned NULL" if ptr.null? font = allocate font.send(:_take_ownership, ptr) font end |
Instance Method Details
#dup ⇒ Object Also known as: clone
Deep copy. The returned font has its own atlas state; mutate one without affecting the other.
129 130 131 132 133 134 135 136 |
# File 'lib/sfml/graphics/font.rb', line 129 def dup ptr = C::Graphics.sfFont_copy(@handle) raise Error, "sfFont_copy returned NULL" if ptr.null? font = self.class.allocate font.send(:_take_ownership, ptr) font end |
#family ⇒ Object
Human-readable family name (e.g. “DejaVu Sans”). Read once via ‘sfFont_getInfo` — CSFML returns a static C string from FreeType so we copy out into a Ruby String.
79 80 81 82 |
# File 'lib/sfml/graphics/font.rb', line 79 def family info = C::Graphics.sfFont_getInfo(@handle) info[:family].null? ? nil : info[:family].read_string end |
#has_glyph?(codepoint) ⇒ Boolean
‘true` if the font has a glyph for the given Unicode codepoint (Integer) or single-character String.
86 87 88 89 |
# File 'lib/sfml/graphics/font.rb', line 86 def has_glyph?(codepoint) cp = codepoint.is_a?(String) ? codepoint.codepoints.first : Integer(codepoint) C::Graphics.sfFont_hasGlyph(@handle, cp || 0) end |
#kerning(first, second, character_size:, bold: false) ⇒ Object
Horizontal kerning offset between two adjacent glyphs at the given character size. Float, in pixels (often negative — the kern pulls the second glyph leftward).
94 95 96 97 98 99 |
# File 'lib/sfml/graphics/font.rb', line 94 def kerning(first, second, character_size:, bold: false) a = first.is_a?(String) ? first.codepoints.first : Integer(first) b = second.is_a?(String) ? second.codepoints.first : Integer(second) fn = bold ? :sfFont_getBoldKerning : :sfFont_getKerning C::Graphics.send(fn, @handle, a || 0, b || 0, Integer(character_size)) end |
#line_spacing(character_size) ⇒ Object
Distance between two consecutive baselines for the given character size. Float, in pixels.
103 104 105 |
# File 'lib/sfml/graphics/font.rb', line 103 def line_spacing(character_size) C::Graphics.sfFont_getLineSpacing(@handle, Integer(character_size)) end |
#smooth=(value) ⇒ Object
72 73 74 |
# File 'lib/sfml/graphics/font.rb', line 72 def smooth=(value) C::Graphics.sfFont_setSmooth(@handle, !!value) end |
#smooth? ⇒ Boolean
70 |
# File 'lib/sfml/graphics/font.rb', line 70 def smooth? = C::Graphics.sfFont_isSmooth(@handle) |
#texture(character_size) ⇒ Object
The internal glyph atlas as a ‘SFML::Texture` (read-only — we don’t own the pointer; CSFML keeps it alive as long as the font does).
121 122 123 124 125 |
# File 'lib/sfml/graphics/font.rb', line 121 def texture(character_size) ptr = C::Graphics.sfFont_getTexture(@handle, Integer(character_size)) return nil if ptr.null? Texture.send(:_borrow, ptr) end |
#underline_position(character_size) ⇒ Object
Vertical offset of the underline from the baseline (positive values point downward). Float, in pixels.
109 110 111 |
# File 'lib/sfml/graphics/font.rb', line 109 def underline_position(character_size) C::Graphics.sfFont_getUnderlinePosition(@handle, Integer(character_size)) end |