Class: Engine::Font

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

Constant Summary collapse

TEXTURE_SIZE =
1024
GLYPH_COUNT =
16
CELL_SIZE =
TEXTURE_SIZE / GLYPH_COUNT

Instance Method Summary collapse

Constructor Details

#initialize(font_file_path) ⇒ Font

Returns a new instance of Font.



11
12
13
# File 'lib/engine/font.rb', line 11

def initialize(font_file_path)
  @font_file_path = font_file_path
end

Instance Method Details

#string_indices(string) ⇒ Object



29
30
31
# File 'lib/engine/font.rb', line 29

def string_indices(string)
  string.chars.reject{|c| c == "\n"}.map { |char| index_table[char] }
end

#string_offsets(string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/engine/font.rb', line 33

def string_offsets(string)
  offsets = []
  scale_factor = 1 / (1024.0 * 2)
  horizontal_offset = 0.0
  vertical_offset = 0.0
  font_path = File.expand_path(File.join(GAME_DIR, "_imported", @font_file_path.gsub(".ttf", ".json")))
  font_metrics = JSON.parse File.read(font_path)
  string.chars.each do |char|
    if char == "\n"
      vertical_offset -= 1.0
      horizontal_offset = 0.0
      next
    end
    offsets << [horizontal_offset, vertical_offset]
    horizontal_offset += 30 * scale_factor * font_metrics[index_table[char].to_s]["width"]
  end
  offsets
end

#textureObject



15
16
17
18
19
20
21
# File 'lib/engine/font.rb', line 15

def texture
  @texture ||=
    begin
      path = File.join("_imported", @font_file_path.gsub(".ttf", ".png"))
      Engine::Texture.for(path)
    end
end

#vertex_data(string) ⇒ Object



23
24
25
26
27
# File 'lib/engine/font.rb', line 23

def vertex_data(string)
  text_indices = string_indices(string)
  offsets = string_offsets(string)
  text_indices.zip(offsets).flatten
end