Class: Hokusai::Backends::SDLBackend::Font

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Font

Returns a new instance of Font.



34
35
36
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 34

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



32
33
34
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 32

def raw
  @raw
end

Class Method Details

.from(file = "#{__dir__}/Monaco.ttf", size = 50) ⇒ Object

Raises:



24
25
26
27
28
29
30
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 24

def self.from(file = "#{__dir__}/Monaco.ttf", size=50)
  raw = SDL.TTF_OpenFont(file, size)

  raise Hokusai::Error.new("Cannot open font from #{file}: #{ttf_error}") if raw.null?

  new(raw)
end

.ttf_errorObject



38
39
40
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 38

def self.ttf_error
  SDL.GetError.read_string
end

Instance Method Details

#ascentObject



57
58
59
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 57

def ascent
  SDL.TTF_FontAscent(raw)
end

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

Raises:



69
70
71
72
73
74
75
76
77
78
79
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 69

def clamp(text, size, width, initial_offset = 0.0)
  self.size = size

  clamping_pointer = FFI::MemoryPointer.new :pointer

  ret = LibHokusai.hoku_text_clamp(clamping_pointer, text, width, initial_offset, raw, SDLBackend::OnWidthCb)
  raise Hokusai::Error.new("Clamping failed") unless ret.zero?

  clamping = LibHokusai::HokuClamping.new(clamping_pointer.get_pointer(0))
  Hokusai::Clamping.new(clamping)
end

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

Raises:



81
82
83
84
85
86
87
88
89
90
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 81

def clamp_markdown(text, size, width, initial_offset = 0.0)
  self.size = size
  clamping_pointer = FFI::MemoryPointer.new :pointer

  ret = LibHokusai.hoku_text_md_clamp(clamping_pointer, text, width, initial_offset, raw, SDLBackend::OnWidthCb)
  raise Hokusai::Error.new("Clamping failed") unless ret.zero?

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

#descentObject



61
62
63
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 61

def descent
  SDL.TTF_FontDescent(raw)
end

#fits(text, width) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 92

def fits(text, width)
  extent = FFI::MemoryPointer.new :int
  count = FFI::MemoryPointer.new :int
  SDL.TTF_Measure(raw, text, width.to_i, extent, count)

  e = extent.read_int
  c = count.read_int

  extent.free
  count.free

  [e, c]
end

#heightObject



65
66
67
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 65

def height
  SDL.TTF_FontHeight(raw)
end

#measure(text, size = 15) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 106

def measure(text, size = 15)
  begin
    self.size = size
    w = FFI::MemoryPointer.new :int
    h = FFI::MemoryPointer.new :int

    SDL.TTF_SizeText(raw, text, w, h)

    width = w.read_int
    height = h.read_int

    w.free
    h.free

    [width, height]
  ensure
    self.size = height
  end
end

#render(text, color = Color.new(0, 0, 0, 255), **args) ⇒ Object



50
51
52
53
54
55
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 50

def render(text, color = Color.new(0, 0, 0, 255), **args)
  set_style(**args)

  render_color = SDLBackend.color(color.r, color.g, color.b, color.a)
  SDL.TTF_RenderUTF8_Blended(raw, text, render_color)
end

#set_style(bold: false, italic: false) ⇒ Object



42
43
44
45
46
47
48
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 42

def set_style(bold: false, italic: false)
  style = SDL::TTF_STYLE_NORMAL
  style = style | SDL::TTF_STYLE_BOLD if bold
  style = style | SDL::TTF_STYLE_ITALIC if italic

  SDL.TTF_SetFontStyle(raw, style)
end

#size=(val) ⇒ Object



126
127
128
# File 'ui/src/hokusai/backends/sdl2/font.rb', line 126

def size=(val)
  SDL.TTF_SetFontSize(raw, val.to_i)
end