Class: Hokusai::Backends::RaylibBackend::Font

Inherits:
Font
  • Object
show all
Defined in:
ui/src/hokusai/backends/raylib/font.rb

Constant Summary collapse

DEFAULT =
"–—‘’“”…\r\n\t0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%%^&*(),.?/\"\\[]-_=+|~`{}<>;:'\0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, spacing = 1.0) ⇒ Font

Returns a new instance of Font.



62
63
64
65
# File 'ui/src/hokusai/backends/raylib/font.rb', line 62

def initialize(raw, spacing = 1.0)
  @raw = raw
  @spacing = spacing
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



27
28
29
# File 'ui/src/hokusai/backends/raylib/font.rb', line 27

def raw
  @raw
end

Class Method Details

.defaultObject



31
32
33
34
35
36
37
# File 'ui/src/hokusai/backends/raylib/font.rb', line 31

def self.default
  font = Raylib.GetFontDefault
  Raylib.SetTextureFilter(font.texture, Raylib::TEXTURE_FILTER_BILINEAR)
  Raylib.GenTextureMipmaps(font.texture)

  new(font)
end

.from(file) ⇒ Object



39
40
41
42
43
44
45
# File 'ui/src/hokusai/backends/raylib/font.rb', line 39

def self.from(file)
  font = Raylib.LoadFont(file)
  Raylib.SetTextureFilter(font.texture, Raylib::TEXTURE_FILTER_BILINEAR)
  Raylib.GenTextureMipmaps(font.texture)

  new(font)
end

.from_ext(file, font_size, codepoint_string = DEFAULT) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'ui/src/hokusai/backends/raylib/font.rb', line 47

def self.from_ext(file, font_size, codepoint_string = DEFAULT)
  ptr = FFI::MemoryPointer.new :int
  codepoints = Raylib.LoadCodepoints(codepoint_string, ptr)
  count = ptr.read_int

  raylib_font = Raylib.LoadFontEx(file, font_size, codepoints, count)
  Raylib.GenTextureMipmaps(raylib_font.texture)
  Raylib.SetTextureFilter(raylib_font.texture, Raylib::TEXTURE_FILTER_BILINEAR)

  font = new(raylib_font)
  Raylib.UnloadCodepoints(codepoints)

  font
end

Instance Method Details

#clamp(text, size, width, initial_offset = 0.0) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'ui/src/hokusai/backends/raylib/font.rb', line 98

def clamp(text, size, width, initial_offset = 0.0)
  clamping_pointer = FFI::MemoryPointer.new :pointer
  RaylibBackend::DataForCb.create(size, spacing, raw) do |ptr|
    ret = LibHokusai.hoku_text_clamp(clamping_pointer, text, width, initial_offset, ptr, RaylibBackend::OnWidthCb)
    raise Hokusai::Error.new("Clamping failed") unless ret.zero?
  end

  clamping = LibHokusai::HokuClamping.new(FFI::AutoPointer.new(clamping_pointer.get_pointer(0), LibHokusai.method(:hoku_text_clamping_free)))
  
  Hokusai::Clamping.new(clamping)
end

#clamp_markdown(text, size, width, initial_offset = 0.0) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'ui/src/hokusai/backends/raylib/font.rb', line 86

def clamp_markdown(text, size, width, initial_offset = 0.0)
  clamping_pointer = FFI::MemoryPointer.new :pointer
  RaylibBackend::DataForCb.create(size, spacing, raw) do |ptr|
    ret = LibHokusai.hoku_text_md_clamp(clamping_pointer, text, width, initial_offset, ptr, RaylibBackend::OnWidthCb)
    raise Hokusai::Error.new("Clamping failed") unless ret.zero?
  end

  clamping = LibHokusai::HokuClamping.new(FFI::AutoPointer.new(clamping_pointer.get_pointer(0), LibHokusai.method(:hoku_text_clamping_free)))
  
  Hokusai::Clamping.new(clamping, markdown: true)
end

#heightObject



82
83
84
# File 'ui/src/hokusai/backends/raylib/font.rb', line 82

def height
  raw.baseSize
end

#measure(string, size) ⇒ Object



76
77
78
79
80
# File 'ui/src/hokusai/backends/raylib/font.rb', line 76

def measure(string, size)
  vec = Raylib.MeasureTextEx(raw, string, size, spacing)

  [vec.x, vec.y]
end

#spacingObject

returns the spacing for this font based on the text size



69
70
71
72
73
74
# File 'ui/src/hokusai/backends/raylib/font.rb', line 69

def spacing
  ssize = raw.baseSize || 1
  ssize = 10 if ssize < 10

  (ssize / (raw.baseSize.zero? ? 1 : raw.baseSize)).to_f
end