1+ --[[
2+ Custom Font Handler v1 - By Squishy6094
3+
4+ This file adds custom font functionality, and does not need to be edited
5+ Ensure this file is loaded before anything else (make the file name start with a or !)
6+ Use djui_hud_add_font() to add fonts as shown in main.lua
7+ ]]
8+
9+ FONT_HANDLER_VERSION_MAJOR = 1
10+ FONT_HANDLER_VERSION_MINOR = 0
11+ FONT_HANDLER_VERSION = " v" .. FONT_HANDLER_VERSION_MAJOR .. " ." .. FONT_HANDLER_VERSION_MINOR
12+
13+ local djui_classic_hud_set_font = djui_hud_set_font
14+ local djui_classic_hud_print_text = djui_hud_print_text
15+ local djui_classic_hud_print_text_interpolated = djui_hud_print_text_interpolated
16+ local djui_classic_hud_measure_text = djui_hud_measure_text
17+
18+ local customFont = false
19+
20+ local fontTable = {}
21+
22+ CUSTOM_FONT_COUNT = FONT_COUNT
23+ local customFontType = FONT_NORMAL
24+
25+ local latinChars = {
26+ [32 ] = " " , [33 ] = " !" , [34 ] = " \" " , [35 ] = " #" , [36 ] = " $" , [37 ] = " %" , [38 ] = " &" , [39 ] = " '" ,
27+ [40 ] = " (" , [41 ] = " )" , [42 ] = " *" , [43 ] = " +" , [44 ] = " ," , [45 ] = " -" , [46 ] = " ." , [47 ] = " /" ,
28+ [48 ] = " 0" , [49 ] = " 1" , [50 ] = " 2" , [51 ] = " 3" , [52 ] = " 4" , [53 ] = " 5" , [54 ] = " 6" , [55 ] = " 7" ,
29+ [56 ] = " 8" , [57 ] = " 9" , [58 ] = " :" , [59 ] = " ;" , [60 ] = " <" , [61 ] = " =" , [62 ] = " >" , [63 ] = " ?" ,
30+ [64 ] = " @" , [65 ] = " A" , [66 ] = " B" , [67 ] = " C" , [68 ] = " D" , [69 ] = " E" , [70 ] = " F" , [71 ] = " G" ,
31+ [72 ] = " H" , [73 ] = " I" , [74 ] = " J" , [75 ] = " K" , [76 ] = " L" , [77 ] = " M" , [78 ] = " N" , [79 ] = " O" ,
32+ [80 ] = " P" , [81 ] = " Q" , [82 ] = " R" , [83 ] = " S" , [84 ] = " T" , [85 ] = " U" , [86 ] = " V" , [87 ] = " W" ,
33+ [88 ] = " X" , [89 ] = " Y" , [90 ] = " Z" , [91 ] = " [" , [92 ] = " \\ " , [93 ] = " ]" , [94 ] = " ^" , [95 ] = " _" ,
34+ [96 ] = " `" , [97 ] = " a" , [98 ] = " b" , [99 ] = " c" , [100 ] = " d" , [101 ] = " e" , [102 ] = " f" , [103 ] = " g" ,
35+ [104 ] = " h" , [105 ] = " i" , [106 ] = " j" , [107 ] = " k" , [108 ] = " l" , [109 ] = " m" , [110 ] = " n" , [111 ] = " o" ,
36+ [112 ] = " p" , [113 ] = " q" , [114 ] = " r" , [115 ] = " s" , [116 ] = " t" , [117 ] = " u" , [118 ] = " v" , [119 ] = " w" ,
37+ [120 ] = " x" , [121 ] = " y" , [122 ] = " z" , [123 ] = " {" , [124 ] = " |" , [125 ] = " }" , [126 ] = " ~" ,
38+ -- Latin-1 Supplement
39+ [160 ] = " " , [161 ] = " ¡" , [162 ] = " ¢" , [163 ] = " £" , [164 ] = " ¤" , [165 ] = " ¥" , [166 ] = " ¦" , [167 ] = " §" ,
40+ [168 ] = " ¨" , [169 ] = " ©" , [170 ] = " ª" , [171 ] = " «" , [172 ] = " ¬" , [173 ] = " " , [174 ] = " ®" , [175 ] = " ¯" ,
41+ [176 ] = " °" , [177 ] = " ±" , [178 ] = " ²" , [179 ] = " ³" , [180 ] = " ´" , [181 ] = " µ" , [182 ] = " ¶" , [183 ] = " ·" ,
42+ [184 ] = " ¸" , [185 ] = " ¹" , [186 ] = " º" , [187 ] = " »" , [188 ] = " ¼" , [189 ] = " ½" , [190 ] = " ¾" , [191 ] = " ¿" ,
43+ [192 ] = " À" , [193 ] = " Á" , [194 ] = " Â" , [195 ] = " Ã" , [196 ] = " Ä" , [197 ] = " Å" , [198 ] = " Æ" , [199 ] = " Ç" ,
44+ [200 ] = " È" , [201 ] = " É" , [202 ] = " Ê" , [203 ] = " Ë" , [204 ] = " Ì" , [205 ] = " Í" , [206 ] = " Î" , [207 ] = " Ï" ,
45+ [208 ] = " Ð" , [209 ] = " Ñ" , [210 ] = " Ò" , [211 ] = " Ó" , [212 ] = " Ô" , [213 ] = " Õ" , [214 ] = " Ö" , [215 ] = " ×" ,
46+ [216 ] = " Ø" , [217 ] = " Ù" , [218 ] = " Ú" , [219 ] = " Û" , [220 ] = " Ü" , [221 ] = " Ý" , [222 ] = " Þ" , [223 ] = " ß" ,
47+ [224 ] = " à" , [225 ] = " á" , [226 ] = " â" , [227 ] = " ã" , [228 ] = " ä" , [229 ] = " å" , [230 ] = " æ" , [231 ] = " ç" ,
48+ [232 ] = " è" , [233 ] = " é" , [234 ] = " ê" , [235 ] = " ë" , [236 ] = " ì" , [237 ] = " í" , [238 ] = " î" , [239 ] = " ï" ,
49+ [240 ] = " ð" , [241 ] = " ñ" , [242 ] = " ò" , [243 ] = " ó" , [244 ] = " ô" , [245 ] = " õ" , [246 ] = " ö" , [247 ] = " ÷" ,
50+ [248 ] = " ø" , [249 ] = " ù" , [250 ] = " ú" , [251 ] = " û" , [252 ] = " ü" , [253 ] = " ý" , [254 ] = " þ" , [255 ] = " ÿ"
51+ }
52+
53+ local HudAnimTimer = 0
54+
55+ local function convert_unicode_table_to_string_table (input )
56+ local output = {}
57+ for i = 1 , # input do
58+ local letter = input [i ]
59+ if letter ~= nil and latinChars [letter .id ] ~= nil then
60+ output [latinChars [letter .id ]] = letter
61+ end
62+ end
63+ return output
64+ end
65+
66+ local function string_to_table (str )
67+ local charArray = {};
68+ local iStart = 0 ;
69+ local strLen = str :len ();
70+ local function bit (b )
71+ return 2 ^ (b - 1 );
72+ end
73+ local function hasbit (w , b )
74+ return w % (b + b ) >= b
75+ end
76+ local checkMultiByte = function (i )
77+ if (iStart ~= 0 ) then
78+ charArray [# charArray + 1 ] = str :sub (iStart , i - 1 )
79+ iStart = 0
80+ end
81+ end
82+ for i = 1 , strLen do
83+ local b = str :byte (i )
84+ local multiStart = hasbit (b , bit (7 )) and hasbit (b , bit (8 ))
85+ local multiTrail = not hasbit (b , bit (7 )) and hasbit (b , bit (8 ))
86+ if (multiStart ) then
87+ checkMultiByte (i )
88+ iStart = i
89+ elseif (not multiTrail ) then
90+ checkMultiByte (i )
91+ charArray [# charArray + 1 ] = str :sub (i , i )
92+ end
93+ end
94+ return charArray
95+ end
96+
97+ --- @param texture TextureInfo
98+ --- @param info table
99+ --- @param spacing integer
100+ --- @param offset integer
101+ --- @param backup string
102+ --- @param scale integer
103+ --- @return DjuiFontType
104+ function djui_hud_add_font (texture , info , spacing , offset , backup , scale )
105+ if texture == nil then return FONT_NORMAL end
106+ if info == nil then return FONT_NORMAL end
107+ if spacing == nil then spacing = 1 end
108+ if offset == nil then offset = 0 end
109+ if backup == nil then backup = " x" end
110+ if scale == nil then scale = 1 end
111+ if info [1 ] ~= nil and info [1 ].id ~= nil then
112+ info = convert_unicode_table_to_string_table (info )
113+ end
114+ CUSTOM_FONT_COUNT = CUSTOM_FONT_COUNT + 1
115+ fontTable [CUSTOM_FONT_COUNT ] = {
116+ spritesheet = texture ,
117+ spacing = spacing ,
118+ offset = offset ,
119+ info = info ,
120+ backup = backup ,
121+ scale = scale ,
122+ }
123+ return CUSTOM_FONT_COUNT
124+ end
125+
126+ --- @param fontType DjuiFontType
127+ --- @return nil
128+ function djui_hud_set_font (fontType )
129+ if fontType > FONT_COUNT then
130+ customFont = true
131+ customFontType = fontType
132+ else
133+ customFont = false
134+ djui_classic_hud_set_font (fontType )
135+ end
136+ end
137+
138+ local textShake = 0
139+ function djui_hud_effect_shake (intensity )
140+ textShake = math.ceil (intensity * 100 )* 0.01
141+ end
142+
143+ local textWaveX = 0
144+ local textWaveY = 0
145+ local textWaveSpeed = 0
146+ function djui_hud_effect_wave (x , y , speed )
147+ textWaveX = x
148+ textWaveY = y
149+ textWaveSpeed = speed
150+ end
151+
152+ --- @param message string
153+ --- @param x number
154+ --- @param y number
155+ --- @param scale number
156+ --- @return nil
157+ function djui_hud_print_text (message , x , y , scale )
158+ if customFont then
159+ if message == nil or message == " " then return end
160+ local message = string_to_table (message )
161+ local currFont = fontTable [customFontType ]
162+ y = y + currFont .offset
163+ scale = scale * currFont .scale
164+ for i = 1 , # message do
165+ local letter = message [i ]
166+ if letter and letter ~= " " then
167+ if currFont .info [letter ] == nil then
168+ letter = currFont .backup
169+ end
170+ local scaleWidth = scale * (currFont .info [letter ].height / currFont .info [letter ].width )
171+ djui_hud_render_texture_tile (currFont .spritesheet ,
172+ x + ((currFont .info [letter ].xoffset or 0 )* scale ) + math.random (- textShake * 100 , textShake * 100 )* 0.01 + math.sin ((HudAnimTimer + i * 2 )* textWaveSpeed * 0.1 )* textWaveX ,
173+ y + ((currFont .info [letter ].yoffset or 0 )* scale ) + math.random (- textShake * 100 , textShake * 100 )* 0.01 + math.cos ((HudAnimTimer + i * 2 )* textWaveSpeed * 0.1 )* textWaveY ,
174+ scaleWidth , scale ,
175+ currFont .info [letter ].x ,
176+ currFont .info [letter ].y ,
177+ currFont .info [letter ].width ,
178+ currFont .info [letter ].height )
179+ else
180+ letter = currFont .backup
181+ end
182+ x = x + (currFont .info [letter ].width + currFont .spacing )* scale
183+ end
184+ else
185+ djui_classic_hud_print_text (message , x , y , scale )
186+ end
187+ end
188+
189+ --- @param message string
190+ --- @param prevX number
191+ --- @param prevY number
192+ --- @param prevScale number
193+ --- @param x number
194+ --- @param y number
195+ --- @param scale number
196+ --- @return nil
197+ -- Custom Fonts do not currently support Interpolation due to lack of RESOLUTION_N64 support
198+ function djui_hud_print_text_interpolated (message , prevX , prevY , prevScale , x , y , scale )
199+ if customFont then
200+ if message == nil or message == " " then return end
201+ local message = string_to_table (message )
202+ local currFont = fontTable [customFontType ]
203+ prevY = prevY + currFont .offset
204+ y = y + currFont .offset
205+ scale = scale * currFont .scale
206+ for i = 1 , # message do
207+ local letter = message [i ]
208+ if letter and letter ~= " " then
209+ if currFont .info [letter ] == nil then
210+ letter = currFont .backup
211+ end
212+ local prevScaleWidth = prevScale * (currFont .info [letter ].height / currFont .info [letter ].width )
213+ local scaleWidth = scale * (currFont .info [letter ].height / currFont .info [letter ].width )
214+ local xOffset = ((currFont .info [letter ].xoffset or 0 )* scale ) + math.random (- textShake * 100 , textShake * 100 )* 0.01 + math.sin ((HudAnimTimer + i * 2 )* textWaveSpeed * 0.1 )* textWaveX
215+ local yOffset = ((currFont .info [letter ].yoffset or 0 )* scale ) + math.random (- textShake * 100 , textShake * 100 )* 0.01 + math.cos ((HudAnimTimer + i * 2 )* textWaveSpeed * 0.1 )* textWaveY
216+ djui_hud_render_texture_tile_interpolated (currFont .spritesheet ,
217+ prevX + xOffset ,
218+ prevY + yOffset ,
219+ prevScaleWidth , prevScale ,
220+ x + xOffset ,
221+ y + yOffset ,
222+ scaleWidth , scale ,
223+ currFont .info [letter ].x ,
224+ currFont .info [letter ].y ,
225+ currFont .info [letter ].width ,
226+ currFont .info [letter ].height )
227+ else
228+ letter = currFont .backup
229+ end
230+ x = x + (currFont .info [letter ].width + currFont .spacing )* scale
231+ prevX = prevX + (currFont .info [letter ].width + currFont .spacing )* prevScale
232+ end
233+ else
234+ djui_classic_hud_print_text_interpolated (message , prevX , prevY , prevScale , x , y , scale )
235+ end
236+ end
237+
238+ --- @param message string
239+ --- @return number
240+ function djui_hud_measure_text (message )
241+ if customFont then
242+ if message == nil or message == " " then return end
243+ local message = string_to_table (message )
244+ local currFont = fontTable [customFontType ]
245+ local scale = 1
246+ local x = 0
247+ for i = 1 , # message do
248+ local letter = message [i ]
249+ if letter and letter ~= " " then
250+ if currFont .info [letter ] == nil then
251+ letter = currFont .backup
252+ end
253+ else
254+ letter = currFont .backup
255+ end
256+ x = x + (currFont .info [letter ].width + currFont .spacing )* scale
257+ end
258+ return x
259+ else
260+ return djui_classic_hud_measure_text (message )
261+ end
262+ end
263+
264+ local function hud_update ()
265+ -- Reset Values Every Frame
266+ textShake = 0
267+ textWaveX = 0
268+ textWaveY = 0
269+ textWaveSpeed = 0
270+
271+ -- Update Basic Anim Timer
272+ HudAnimTimer = HudAnimTimer + 1
273+ end
274+
275+ hook_event (HOOK_ON_HUD_RENDER_BEHIND , hud_update )
276+
277+ -- Adding custom fonts here to prevent main clutter
278+ fontdataCharacteristic = {
279+ [" A" ] = {x = 0 , y = 0 , width = 26 , height = 32 },
280+ [" B" ] = {x = 32 , y = 0 , width = 25 , height = 32 },
281+ [" C" ] = {x = 32 * 2 , y = 0 , width = 25 , height = 32 },
282+ [" D" ] = {x = 32 * 3 , y = 0 , width = 23 , height = 32 },
283+ [" E" ] = {x = 32 * 4 , y = 0 , width = 24 , height = 32 },
284+ [" F" ] = {x = 32 * 5 , y = 0 , width = 24 , height = 32 },
285+ [" G" ] = {x = 32 * 6 , y = 0 , width = 26 , height = 32 },
286+ [" H" ] = {x = 32 * 7 , y = 0 , width = 25 , height = 32 },
287+
288+ [" I" ] = {x = 0 , y = 32 , width = 15 , height = 32 },
289+ [" J" ] = {x = 32 , y = 32 , width = 21 , height = 32 },
290+ [" K" ] = {x = 32 * 2 , y = 32 , width = 25 , height = 32 },
291+ [" L" ] = {x = 32 * 3 , y = 32 , width = 22 , height = 32 },
292+ [" M" ] = {x = 32 * 4 , y = 32 , width = 29 , height = 32 },
293+ [" N" ] = {x = 32 * 5 , y = 32 , width = 27 , height = 32 },
294+ [" Ñ" ] = {x = 32 * 6 , y = 32 , width = 27 , height = 32 },
295+ [" O" ] = {x = 32 * 7 , y = 32 , width = 26 , height = 32 },
296+
297+ [" P" ] = {x = 0 , y = 32 * 2 , width = 25 , height = 32 },
298+ [" Q" ] = {x = 32 , y = 32 * 2 , width = 27 , height = 32 },
299+ [" R" ] = {x = 32 * 2 , y = 32 * 2 , width = 25 , height = 32 },
300+ [" S" ] = {x = 32 * 3 , y = 32 * 2 , width = 24 , height = 32 },
301+ [" T" ] = {x = 32 * 4 , y = 32 * 2 , width = 28 , height = 32 },
302+ [" U" ] = {x = 32 * 5 , y = 32 * 2 , width = 26 , height = 32 },
303+ [" V" ] = {x = 32 * 6 , y = 32 * 2 , width = 27 , height = 32 },
304+ [" W" ] = {x = 32 * 7 , y = 32 * 2 , width = 30 , height = 32 },
305+
306+ [" X" ] = {x = 0 , y = 32 * 3 , width = 28 , height = 32 },
307+ [" Y" ] = {x = 32 , y = 32 * 3 , width = 27 , height = 32 },
308+ [" Z" ] = {x = 32 * 2 , y = 32 * 3 , width = 27 , height = 32 },
309+ [" !" ] = {x = 32 * 3 , y = 32 * 3 , width = 30 , height = 32 },
310+ [" ?" ] = {x = 32 * 4 , y = 32 * 3 , width = 27 , height = 32 },
311+ [" @" ] = {x = 32 * 5 , y = 32 * 3 , width = 29 , height = 32 },
312+ [" #" ] = {x = 32 * 6 , y = 32 * 3 , width = 29 , height = 32 },
313+ [" $" ] = {x = 32 * 7 , y = 32 * 3 , width = 23 , height = 32 },
314+
315+ [" %" ] = {x = 0 , y = 32 * 4 , width = 27 , height = 32 },
316+ [" ^" ] = {x = 32 , y = 32 * 4 , width = 24 , height = 32 },
317+ [" &" ] = {x = 32 * 2 , y = 32 * 4 , width = 29 , height = 32 },
318+ [" *" ] = {x = 32 * 3 , y = 32 * 4 , width = 18 , height = 32 },
319+ [" (" ] = {x = 32 * 4 , y = 32 * 4 , width = 17 , height = 32 },
320+ [" )" ] = {x = 32 * 5 , y = 32 * 4 , width = 17 , height = 32 },
321+ [" _" ] = {x = 32 * 6 , y = 32 * 4 , width = 31 , height = 32 },
322+ [" -" ] = {x = 32 * 7 , y = 32 * 4 , width = 23 , height = 32 },
323+
324+ [" +" ] = {x = 0 , y = 32 * 5 , width = 24 , height = 32 },
325+ [" =" ] = {x = 32 , y = 32 * 5 , width = 27 , height = 32 },
326+ [" <" ] = {x = 32 * 2 , y = 32 * 5 , width = 23 , height = 32 },
327+ [" >" ] = {x = 32 * 3 , y = 32 * 5 , width = 23 , height = 32 },
328+ [" ." ] = {x = 32 * 4 , y = 32 * 5 , width = 11 , height = 32 },
329+ [" ," ] = {x = 32 * 5 , y = 32 * 5 , width = 11 , height = 32 },
330+ [" :" ] = {x = 32 * 6 , y = 32 * 5 , width = 11 , height = 32 },
331+ [" ;" ] = {x = 32 * 7 , y = 32 * 5 , width = 11 , height = 32 },
332+
333+ [" /" ] = {x = 0 , y = 32 * 6 , width = 28 , height = 32 },
334+ [" \\ " ] = {x = 32 , y = 32 * 6 , width = 28 , height = 32 },
335+ [' "' ] = {x = 32 * 2 , y = 32 * 6 , width = 14 , height = 32 },
336+ [" '" ] = {x = 32 * 3 , y = 32 * 6 , width = 9 , height = 32 },
337+ [" |" ] = {x = 32 * 4 , y = 32 * 6 , width = 10 , height = 32 },
338+ [" ~" ] = {x = 32 * 5 , y = 32 * 6 , width = 23 , height = 32 },
339+ [" 1" ] = {x = 32 * 6 , y = 32 * 6 , width = 23 , height = 32 },
340+ [" 2" ] = {x = 32 * 7 , y = 32 * 6 , width = 26 , height = 32 },
341+
342+ [" 3" ] = {x = 0 , y = 32 * 7 , width = 24 , height = 32 },
343+ [" 4" ] = {x = 32 , y = 32 * 7 , width = 24 , height = 32 },
344+ [" 5" ] = {x = 32 * 2 , y = 32 * 7 , width = 26 , height = 32 },
345+ [" 6" ] = {x = 32 * 3 , y = 32 * 7 , width = 26 , height = 32 },
346+ [" 7" ] = {x = 32 * 4 , y = 32 * 7 , width = 30 , height = 32 },
347+ [" 8" ] = {x = 32 * 5 , y = 32 * 7 , width = 22 , height = 32 },
348+ [" 9" ] = {x = 32 * 6 , y = 32 * 7 , width = 24 , height = 32 },
349+ [" 0" ] = {x = 32 * 7 , y = 32 * 7 , width = 24 , height = 32 },
350+
351+ }
352+
353+ FONT_CHARACTERISTIC = djui_hud_add_font (get_texture_info (" char_select_font_characteristic" ), fontdataCharacteristic , - 5 , 0 , " X" , 1 )
0 commit comments