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.
-
.from_stream(io) ⇒ Object
Load a TTF/OTF from any Ruby IO-like object — file, in-memory, network-backed reader.
- .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:
158 159 160 |
# File 'lib/sfml/graphics/font.rb', line 158 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.
70 71 72 |
# File 'lib/sfml/graphics/font.rb', line 70 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.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sfml/graphics/font.rb', line 77 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 46 47 48 |
# 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) # CSFML doesn't copy the font bytes — keep them pinned so the # GC doesn't free them while CSFML still references the buffer. font.instance_variable_set(:@_memory_pin, buf) font end |
.from_stream(io) ⇒ Object
Load a TTF/OTF from any Ruby IO-like object — file, in-memory, network-backed reader. Keep the IO open until the Font is no longer in use; CSFML reads glyphs lazily.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sfml/graphics/font.rb', line 53 def self.from_stream(io) stream = SFML::InputStream.new(io) ptr = C::Graphics.sfFont_createFromStream(stream.to_ptr) raise Error, "sfFont_createFromStream returned NULL" if ptr.null? font = allocate font.send(:_take_ownership, ptr) # CSFML reads the font lazily from this stream — keep it pinned. font.instance_variable_set(:@_stream_pin, stream) font.instance_variable_set(:@_io_pin, io) 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.
148 149 150 151 152 153 154 155 |
# File 'lib/sfml/graphics/font.rb', line 148 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.
98 99 100 101 |
# File 'lib/sfml/graphics/font.rb', line 98 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.
105 106 107 108 |
# File 'lib/sfml/graphics/font.rb', line 105 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).
113 114 115 116 117 118 |
# File 'lib/sfml/graphics/font.rb', line 113 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.
122 123 124 |
# File 'lib/sfml/graphics/font.rb', line 122 def line_spacing(character_size) C::Graphics.sfFont_getLineSpacing(@handle, Integer(character_size)) end |
#smooth=(value) ⇒ Object
91 92 93 |
# File 'lib/sfml/graphics/font.rb', line 91 def smooth=(value) C::Graphics.sfFont_setSmooth(@handle, !!value) end |
#smooth? ⇒ Boolean
89 |
# File 'lib/sfml/graphics/font.rb', line 89 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).
140 141 142 143 144 |
# File 'lib/sfml/graphics/font.rb', line 140 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.
128 129 130 |
# File 'lib/sfml/graphics/font.rb', line 128 def underline_position(character_size) C::Graphics.sfFont_getUnderlinePosition(@handle, Integer(character_size)) end |