forked from 26F-Studio/Zenitha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.lua
114 lines (96 loc) · 2.99 KB
/
font.lua
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
local set=love.graphics.setFont
---@type love.File[], love.Font[][]
local fontFiles,fontCache={},{}
---@type string, string
local defaultFont,defaultFallBack
---@type table<string, string>
local fallbackMap={}
---@type love.Font
local curFont=nil -- Current using font object
local FONT={}
---Set default font type
---@param name string
function FONT.setDefaultFont(name)
defaultFont=name
end
---Set default fallback font type
---@param name string
function FONT.setDefaultFallback(name)
defaultFallBack=name
end
---Set fallback font for an exist font
---@param font string
---@param fallback string
function FONT.setFallback(font,fallback)
fallbackMap[font]=fallback
end
---Get love's default font object
---@param size number
---@return love.Font
local function _rawget(size)
if not fontCache[size] then
assertf(type(size)=='number' and size>0 and size%1==0,"Need int >=1, got %s",size)
fontCache[size]=love.graphics.setNewFont(size,'normal',love.graphics.getDPIScale()*SCR.k*2)
end
return fontCache[size]
end
FONT.rawget=_rawget
---Set love's default font
---@param size number
local function _rawset(size)
set(fontCache[size] or _rawget(size))
end
FONT.rawset=_rawset
---Load font(s) from file(s)
---@param name string|string[]|any
---@param path string
---@overload fun(map:table<string, string>)
function FONT.load(name,path)
if type(name)=='table' then
for k,v in next,name do
FONT.load(k,v)
end
else
assertf(love.filesystem.getInfo(path),"Font file %s(%s) not exist!",name,path)
fontFiles[name]=love.filesystem.newFile(path)
fontCache[name]={}
end
end
---Get font object with font size, use default font name if not given
---
---Warning: any numbers not appeared before will cause a new font object to be created, so don't call this with too many different font sizes
---@param size number
---@param name? string
---@return love.Font
local function _get(size,name)
if not name then name=defaultFont end
local f=fontCache[name]
if not f then return _rawget(size) end
f=f[size]
if not f then
assertf(type(size)=='number' and size>0 and size%1==0,"Need int >=1, got %s",size)
f=love.graphics.newFont(fontFiles[name],size,'normal',love.graphics.getDPIScale()*SCR.k*2)
local fallbackName=fallbackMap[name] or defaultFallBack and name~=defaultFallBack and defaultFallBack
if fallbackName then
f:setFallbacks(_get(size,fallbackName))
end
fontCache[name][size]=f
end
return f
end
FONT.get=_get
---Set font with font size, use default font name if not given
---
---Warning: any numbers not appeared before will cause a new font object to be created, so don't call this with too many different font sizes
---@param size number
---@param name? string
local function _set(size,name)
if not name then name=defaultFont end
local f=_get(size,name)
if f~=curFont then
curFont=f
set(curFont)
end
end
FONT.set=_set
return FONT