Class: Engine::Font

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

Constant Summary collapse

TEXTURE_SIZE =
1024
GLYPH_COUNT =
16
CELL_SIZE =
TEXTURE_SIZE / GLYPH_COUNT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

allowed_class?, #awake, get_class, included, register_class, #uuid

Instance Attribute Details

#font_file_pathObject (readonly)

Returns the value of attribute font_file_path.



9
10
11
# File 'lib/engine/font.rb', line 9

def font_file_path
  @font_file_path
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/engine/font.rb', line 9

def source
  @source
end

Class Method Details

.bangersObject



40
41
42
# File 'lib/engine/font.rb', line 40

def self.bangers
  self.for("Bangers-Regular.ttf", source: :engine)
end

.caveatObject



44
45
46
# File 'lib/engine/font.rb', line 44

def self.caveat
  self.for("Caveat-Regular.ttf", source: :engine)
end

.font_cacheObject



20
21
22
# File 'lib/engine/font.rb', line 20

def self.font_cache
  @font_cache ||= {}
end

.for(font_file_path, source: :game) ⇒ Object



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

def self.for(font_file_path, source: :game)
  cache_key = [font_file_path, source]
  font_cache[cache_key] ||= create(font_file_path: font_file_path, source: source)
end

.from_serializable_data(data) ⇒ Object



52
53
54
# File 'lib/engine/font.rb', line 52

def self.from_serializable_data(data)
  self.for(data[:font_file_path], source: (data[:source] || :game).to_sym)
end

.jetbrains_monoObject



32
33
34
# File 'lib/engine/font.rb', line 32

def self.jetbrains_mono
  self.for("JetBrainsMono-Regular.ttf", source: :engine)
end

.noto_serifObject



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

def self.noto_serif
  self.for("NotoSerif-Regular.ttf", source: :engine)
end

.open_sansObject



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

def self.open_sans
  self.for("OpenSans-Regular.ttf", source: :engine)
end

.oswaldObject



48
49
50
# File 'lib/engine/font.rb', line 48

def self.oswald
  self.for("Oswald-Regular.ttf", source: :engine)
end

.press_start_2pObject



36
37
38
# File 'lib/engine/font.rb', line 36

def self.press_start_2p
  self.for("PressStart2P-Regular.ttf", source: :engine)
end

Instance Method Details

#resolve_font_json_pathObject



97
98
99
100
101
102
103
# File 'lib/engine/font.rb', line 97

def resolve_font_json_path
  if @source == :engine
    File.expand_path(File.join(ENGINE_DIR, "assets", "_imported", @font_file_path.gsub(".ttf", ".json")))
  else
    File.expand_path(File.join(GAME_DIR, "_imported", @font_file_path.gsub(".ttf", ".json")))
  end
end

#serializable_dataObject



56
57
58
# File 'lib/engine/font.rb', line 56

def serializable_data
  { font_file_path: @font_file_path, source: @source }
end

#string_indices(string) ⇒ Object



74
75
76
# File 'lib/engine/font.rb', line 74

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

#string_offsets(string) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/engine/font.rb', line 78

def string_offsets(string)
  offsets = []
  scale_factor = 1 / (1024.0 * 2)
  horizontal_offset = 0.0
  vertical_offset = 0.0
  font_path = resolve_font_json_path
  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



60
61
62
63
64
65
66
# File 'lib/engine/font.rb', line 60

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

#vertex_data(string) ⇒ Object



68
69
70
71
72
# File 'lib/engine/font.rb', line 68

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