Debian Codegraph @

texmf-dist/tex/lualatex/luaoptions/luaoptions-lib.lua

raw
1 -- luacheck: ignore ly log self luatexbase internalversion font fonts tex token kpse status
2 local err, warn, info, log = luatexbase.provides_module({
3 name = "luaoptions-lib",
4 version = '0.9', --LUAOPTIONS_VERSION
5 date = "2026/05/20", --LUAOPTIONS_DATE
6 description = "Module luaoptions-lib.",
7 author = "The lualatex-tools Project",
8 copyright = "2015-2026 - the lualatex-tools Project",
9 license = "MIT",
10 })
11
12 local lib = {}
13 lib.TEX_UNITS = {'bp', 'cc', 'cm', 'dd', 'in', 'mm', 'pc', 'pt', 'sp', 'em',
14 'ex'}
15
16 -------------------------
17 -- General tool functions
18
19 function lib.basename(str)
20 --[[
21 Given the full path to a file, return only the file name (without path).
22 If there is no slash, return the full name.
23 --]]
24 return str:gsub(".*/(.*)", "%1") or str
25 end
26
27
28 function lib.contains(table_var, value)
29 --[[
30 Returns the key if the given table contains the given value, or nil.
31 A value of 'false' (string) is considered equal to false (Boolean).
32 --]]
33 for k, v in pairs(table_var) do
34 if v == value then return k
35 elseif v == 'false' and value == false then return k
36 end
37 end
38 end
39
40
41 function lib.contains_key(table_var, key)
42 -- Returs true if the given key is present in the table, nil otherwise.
43 for k in pairs(table_var) do
44 if k == key then return true end
45 end
46 end
47
48
49 function lib.convert_unit(value)
50 --[[
51 Convert a LaTeX unit, if possible.
52 TODO: Understand what this *really* does, what is accepted and returned.
53 --]]
54 if not value then return 0
55 elseif value == '' then return false
56 elseif value:match('\\') then
57 local n, u = value:match('^%d*%.?%d*'), value:match('%a+')
58 if n == '' then n = 1 end
59 return tonumber(n) * tex.dimen[u] / tex.sp("1pt")
60 else return ('%f'):format(tonumber(value) or tex.sp(value) / tex.sp("1pt"))
61 end
62 end
63
64
65 function lib.current_font_size()
66 --[[
67 Convenience function to return the font size of the current font
68 --]]
69 return lib.fontinfo(font.current()).size
70 end
71
72
73 function lib.dirname(str)
74 --[[
75 Given the full path to a file, return only the path (without file name),
76 including the last slash. If there is no slash, return an empty string.
77 --]]
78 return str:gsub("(.*/).*", "%1") or ''
79 end
80
81
82 local fontdata = fonts.hashes.identifiers
83 function lib.fontinfo(id)
84 --[[
85 Return a LuaTeX font object based on the given ID
86 --]]
87 return fontdata[id] or font.getfont(id)
88 end
89
90
91 function lib.max(a, b)
92 a, b = tonumber(a), tonumber(b)
93 if a > b then return a else return b end
94 end
95
96
97 function lib.min(a, b)
98 a, b = tonumber(a), tonumber(b)
99 if a < b then return a else return b end
100 end
101
102
103 function lib.mkdirs(str)
104 local path
105 if str:sub(1, 1) == '/' then path = '' else path = '.' end
106 for dir in str:gmatch('([^%/]+)') do
107 path = path .. '/' .. dir
108 lfs.mkdir(path)
109 end
110 end
111
112
113 function lib.orderedkeys(t)
114 local orderedIndex = {}
115 for k in pairs(t) do table.insert(orderedIndex, k) end
116 table.sort(orderedIndex)
117 return orderedIndex
118 end
119
120
121 function lib.orderedpairs(t)
122 local key
123 local i = 0
124 local orderedIndex = lib.orderedkeys(t)
125 return function ()
126 i = i + 1
127 key = orderedIndex[i]
128 if key then return key, t[key] end
129 end
130 end
131
132
133 function lib.print_table(t, indent)
134 -- Recursively print a table, avoiding recursion loops
135 indent = indent or ' '
136 local visited = {}
137 local skips = 0
138
139 local function _print_table(_t, _ind)
140 local keys = lib.orderedkeys(_t)
141 local display_keys = {}
142 local length = 0
143 for key, _ in pairs(_t) do
144 length = lib.max(length, #tostring(key))
145 end
146 for key, _ in pairs(_t) do
147 display_key = tostring(key)
148 display_keys[key] = display_key .. string.rep(
149 ' ', length - #display_key)
150 end
151 for k, v in lib.orderedpairs(_t) do
152 if type(v) == 'table' then
153 if visited[v] then
154 skips = skips + 1
155 else
156 visited[v] = true
157 print(_ind..display_keys[k], v)
158 if #_ind > 40 then err("k") end
159 _print_table(v, _ind..indent)
160 end
161 else
162 if v == '' then v = '(empty string)' end
163 print(_ind..display_keys[k], v)
164 end
165 end
166 end
167
168 print()
169 visited[t] = true
170 print("Print table:", t)
171 _print_table(t, indent)
172 print()
173 if skips > 0 then
174 print(string.format('%s recursive tables skipped', skips))
175 end
176 print("============")
177 print()
178 end
179
180 function lib.readlinematching(s, f)
181 if f then
182 local result = ''
183 while result and not result:find(s) do result = f:read() end
184 f:close()
185 return result
186 end
187 end
188
189
190 function lib.splitext(str, ext)
191 --[[
192 If 'ext' is supplied return str stripped of the given extension,
193 otherwise return the base and extension (if any)
194 --]]
195 return ext and (str:match('(.*)%.'..ext..'$') or str)
196 or (str:match('(.*)%.(%w*)$') or str)
197 end
198
199
200 ------------------------------------
201 -- Engine, version, TeX distribution
202
203 local function _loaded(ext)
204 --[[
205 Returns a function that checks whether a class or a package is loaded
206 https://tex.stackexchange.com/questions/501635/
207 --]]
208 local fmt = "ver@%s." .. ext
209 return function(name)
210 local macro = token.get_macro(fmt:format(name))
211 return macro ~= nil
212 end
213 end
214
215 -- Lua implementations of \@ifpackageloaded and \@ifclassloaded
216 lib.class_loaded = _loaded('cls')
217 lib.package_loaded = _loaded('sty')
218
219 local tex_engine = {}
220 setmetatable(tex_engine, tex_engine)
221
222 function tex_engine:__call()
223 --[[
224 Defines the properties extracted from the first line of jobname.log.
225 --]]
226 local f = io.open(kpse.find_file(tex.jobname..'.log'))
227 if not f then return {} end
228 self.engine, self.engine_version, self.dist, self.dist_version, self.format, self.format_version =
229 f:read():match(
230 'This is ([^,]*), Version ([^%(]*) %(([^%)]*) ([^%)]*)%)%s+%(format=([^%)]*) ([^)]*)%)'
231 )
232 f:close()
233 return self
234 end
235
236 function tex_engine:__index(k) return rawget(self(), k) end
237
238
239 lib.tex_engine = tex_engine
240 return lib