This repository was archived by the owner on May 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualcharacterheight.lua
More file actions
278 lines (191 loc) · 9.37 KB
/
Copy pathvisualcharacterheight.lua
File metadata and controls
278 lines (191 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
--
-- Advanced settings
--
local VCH_OVERRIDE_DEFAULTS = true -- Should the script override default surface.CreateFont & surface.SetFont?
local g_pfnGetFont
--[[ Note
If you set VCH_OVERRIDE_DEFAULTS to false,
then assumingly you have your own surface.GetFont
and in that case set g_pfnGetFont to it.
Or.
Just provide the font in use to surface.GetVisualCharacterHeight.
]]
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Prepare
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
--
-- Libraries, functions
--
local surface = surface
local render = render
local string = string
local ReadPixel = render.ReadPixel
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Purpose: Store the former functions.
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
local CreateFontEx
local SetFontEx
if ( VCH_OVERRIDE_DEFAULTS ) then
surface.CreateFontEx = surface.CreateFontEx or surface.CreateFont
surface.SetFontEx = surface.SetFontEx or surface.SetFont
CreateFontEx = surface.CreateFontEx
SetFontEx = surface.SetFontEx
end
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(Override) surface.CreateFont
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
if ( VCH_OVERRIDE_DEFAULTS ) then
function surface.CreateFont( name, data )
VisualCharacterHeight_Uncache( name )
-- Clearing the font's cache that it gets remeasured later,
-- just in case the size or weight got changed.
return CreateFontEx( name, data )
end
end
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(Override) surface.SetFont
Purpose: Store the current font for later access.
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
local g_surface_FontUsed
if ( VCH_OVERRIDE_DEFAULTS ) then
g_surface_FontUsed = 'DermaDefault'
function surface.SetFont( font )
g_surface_FontUsed = font
return SetFontEx( font )
end
end
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Cache
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
local VCHCache = {}
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
VisualCharacterHeight_Uncache
Purpose: Later fresh recache.
Note: If VCH_OVERRIDE_DEFAULTS = false and your font's size &/ weight is «dynamic»,
you might want to call it before that change. (This assumes that
surface.GetVisualCharacterHeight is used.)
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
function VisualCharacterHeight_Uncache( specificfont )
if ( specificfont ) then
VCHCache[specificfont] = nil
else
for font in pairs( VCHCache ) do VCHCache[font] = nil end
end
end
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
The common render target
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
local g_texVCH = GetRenderTargetEx(
'_rt_VisualCharacterHeight',
ScrW(), ScrH(),
RT_SIZE_FULL_FRAME_BUFFER, MATERIAL_RT_DEPTH_NONE,
bit.bor( 2, 256 ), 0,
8 -- IMAGE_FORMAT_A8
)
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(Internal) CalculateVisualCharacterHeight
Purpose: Works out the visual height of the provided characters.
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
local function CalculateVisualCharacterHeight( chars )
-- Work out the operating area
local w, h = surface.GetTextSize( chars )
local yTop = 0 -- The ordinate of the highest visible pixel
local yBottom = h - 1 -- The ordinate of the lowest visible pixel
render.PushRenderTarget( g_texVCH )
render.SetScissorRect( 0, 0, w, h, true )
render.Clear( 255, 255, 255, 0 )
surface.SetAlphaMultiplier( 1 )
-- Just in case the overall alpha right now is zero.
-- E.g. <Panel>:Paint(); <Panel>:GetAlpha() => 0.
cam.Start2D()
surface.SetTextColor( 255, 255, 255, 255 )
if ( string.find( chars, '\n' ) ) then
-- Multi-line text
local offsetY = 0
local lineHeight = select( 2, surface.GetTextSize( '\n' ) )
for line in string.gmatch( chars, '[^\n]*' ) do
if ( line == '' ) then offsetY = offsetY + ( lineHeight * 0.5 ) continue end
-- line = '' means that it's a '\n' itself.
surface.SetTextPos( 0, offsetY )
surface.DrawText( line )
end
else
-- Simple text
surface.SetTextPos( 0, 0 )
surface.DrawText( chars )
end
cam.End2D()
render.CapturePixels()
-- yTop
for y = 0, h - 1 do
for x = 0, w - 1 do
local _, _, _, a = ReadPixel( x, y )
if ( a ~= 0 ) then yTop = y goto exit_1 end
end
end
::exit_1::
-- yBottom
for y = h - 1, 0, -1 do
for x = w - 1, 0, -1 do
local _, _, _, a = ReadPixel( x, y )
if ( a ~= 0 ) then yBottom = y goto exit_2 end
end
end
::exit_2::
render.SetScissorRect( 0, 0, 0, 0, false )
render.PopRenderTarget()
return yTop, yBottom
end
--[[–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
surface.GetVisualCharacterHeight
Purpose: Gets the visual height of the provided characters.
Returns:
1 number visualheight
2 number roofheight
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––]]
function surface.GetVisualCharacterHeight( chars, font )
if ( not isstring( chars ) ) then
assert( false, Format( 'bad argument #1 to \'GetVisualCharacterHeight\' (string expected, got %s)', type( chars ) ) )
end
--
-- Manage the font
--
if ( not VCH_OVERRIDE_DEFAULTS ) then
if ( g_pfnGetFont ) then
g_surface_FontUsed = g_pfnGetFont()
end
if ( not font and not g_surface_FontUsed ) then
assert( false, 'font to \'GetVisualCharacterHeight\' is not provided or cannot be obtained' )
end
if ( font and ( g_surface_FontUsed and g_surface_FontUsed ~= font or true ) ) then
surface.SetFont( font )
elseif ( g_surface_FontUsed ) then
font = g_surface_FontUsed
end
else
if ( font and g_surface_FontUsed ~= font ) then
surface.SetFont( font )
else
font = g_surface_FontUsed
end
end
local vchcache_font = VCHCache[font]
if ( not vchcache_font ) then
-- Prepare a place in the cache
vchcache_font = {}
VCHCache[font] = vchcache_font
else
-- Return the stored
local charsmeasurement = vchcache_font[chars]
if ( charsmeasurement ) then
return charsmeasurement.visualheight, charsmeasurement.roofheight
end
end
local yTop, yBottom = CalculateVisualCharacterHeight( chars )
local visualheight = ( ( yBottom - yTop ) + 1 )
local roofheight = yTop
if ( not vchcache_font[chars] ) then
vchcache_font[chars] = { visualheight = visualheight; roofheight = roofheight }
end
return visualheight, roofheight
end