Class: Engine::Font
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
allowed_class?, #awake, get_class, included, register_class, #uuid
Instance Attribute Details
#font_file_path ⇒ Object
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
|
#source ⇒ Object
Returns the value of attribute source.
9
10
11
|
# File 'lib/engine/font.rb', line 9
def source
@source
end
|
Class Method Details
.bangers ⇒ Object
40
41
42
|
# File 'lib/engine/font.rb', line 40
def self.bangers
self.for("Bangers-Regular.ttf", source: :engine)
end
|
.caveat ⇒ Object
44
45
46
|
# File 'lib/engine/font.rb', line 44
def self.caveat
self.for("Caveat-Regular.ttf", source: :engine)
end
|
.font_cache ⇒ Object
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_mono ⇒ Object
32
33
34
|
# File 'lib/engine/font.rb', line 32
def self.jetbrains_mono
self.for("JetBrainsMono-Regular.ttf", source: :engine)
end
|
.noto_serif ⇒ Object
28
29
30
|
# File 'lib/engine/font.rb', line 28
def self.noto_serif
self.for("NotoSerif-Regular.ttf", source: :engine)
end
|
.open_sans ⇒ Object
24
25
26
|
# File 'lib/engine/font.rb', line 24
def self.open_sans
self.for("OpenSans-Regular.ttf", source: :engine)
end
|
.oswald ⇒ Object
48
49
50
|
# File 'lib/engine/font.rb', line 48
def self.oswald
self.for("Oswald-Regular.ttf", source: :engine)
end
|
.press_start_2p ⇒ Object
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_path ⇒ Object
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_data ⇒ Object
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
|
#texture ⇒ Object
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
|