Debian Codegraph @

texmf-dist/tex/lualatex/seatingchart/seatingchart.lua

raw
1 --[[
2 Package: seatingchart
3 Date:
4 2025-07-25
5 Version:
6 v0.5.0
7 --]]
8 Seat = {
9 id = nil,
10 row = 0,
11 col = 0,
12 x = 0,
13 y = 0,
14 rrow = 0,
15 rcol = 0,
16 rotate = 0,
17 kind = nil,
18 label="",
19 textlabel=""
20 }
21 function Seat:new(o)
22 o = o or {}
23 setmetatable(o, self)
24 self.__index = self
25 return o
26 end
27
28 AllSeats={}
29 SEATREMOVED = 1
30 SEATEMPTY = 2
31 SEATASSIGNED = 3
32 AISLE = 4
33 SPECIAL = 5
34
35 function initSeating(rows, cols, shape)
36 local cid = 0
37 AllSeats.size=0
38 AllSeats.rows=rows
39 AllSeats.cols=cols
40 --tex.sprint("\\typeout{*** initSeating CALLED, ndx: }")
41 if shape == "rect" then
42 for r = 1, rows do
43 for c = 1, cols do
44 cid = cid+1
45 AllSeats[cid] = Seat.new({id=cid, row=r, col=c, x=c-(cols+1)/2, y=r-1.0, rrow=0, rcol=0, rotate=0, kind=SEATEMPTY, label=tostring(cid), textlabel=""})
46 AllSeats.size=AllSeats.size+1
47 end
48 end
49 elseif shape == "arc" then
50 local arc=120
51 local d=8
52 local rot
53 local x
54 local y
55 local phi
56 for r = 1, rows do
57 for c = 1, cols do
58 cid = cid+1
59 phi=(c-(cols+1)/2)/(2*r+(cols/2))*arc*math.pi/180
60 x = (d+r)*math.sin(phi)
61 y= (d+r)*math.cos(phi)-d/2
62 rot=-phi/math.pi*180
63 AllSeats[cid] = Seat.new({id=cid, row=r, col=c,
64 rotate=rot,
65 x=x,
66 y=y,
67 rrow=0, rcol=0, kind=SEATEMPTY, label=tostring(cid)})
68 AllSeats.size=AllSeats.size+1
69 end
70 end
71 end
72
73 end
74
75 function initArc(rows, cols)
76 end
77
78 function seatDim(w,h)
79 AllSeats.seatwidth = w
80 AllSeats.seatheight = h
81 end
82
83 function findSeatAt(y,x)
84 if y<0 then
85 y = AllSeats.rows + y + 1
86 end
87 if x<0 then
88 x = AllSeats.cols + x + 1
89 end
90 if x>AllSeats.cols or y > AllSeats.rows then
91 tex.sprint("\\PackageError{\\packagename}{Index out of bound}{Check the used coordinates.}")
92 end
93 local ndx = (y-1)* AllSeats.cols + x
94 return ndx
95 end
96
97 function assignSeat(ndx,d)
98 local kind = d.kind or SEATASSIGNED
99 local runningrow = d.rr or 0
100 local runnigseat = d.rc or 0
101 -- tex.sprint("\\typeout{*** AllSeats[ndx] ",AllSeats,"[",ndx,"]}")
102 -- tex.sprint("\\typeout{ kind=",kind,", text='",d.label,"', rr=",runningrow,", rs=",runnigseat,"}")
103 if ndx ~= nil and AllSeats[ndx].kind == SEATEMPTY then
104 AllSeats[ndx].kind = kind
105 AllSeats[ndx].label=d.label or AllSeats[ndx].label
106 AllSeats[ndx].rrow=runningrow
107 AllSeats[ndx].rcol=runnigseat
108 return true
109 else
110 return false
111 end
112 end
113 function assignSeatAt(y,x,d)
114 assignSeat(findSeatAt(y,x),d)
115 end
116 function removeSeatAt(y,x)
117 assignSeat(findSeatAt(y,x),{kind=SEATREMOVED})
118 end
119 function removeAisle(c,from,to)
120 for r = from, to do
121 assignSeatAt(r,c,{kind=AISLE})
122 end
123 end
124
125 function seatingSchemeInRows(pat,policy)
126 local numseats = AllSeats.cols
127 local numrows = AllSeats.rows
128 local frow = policy["first row"]
129 local lrow = policy["last row"]
130 local rstep = policy["row sep"]
131 local rres = policy["row restart"]
132 local ltext = policy["label text"]
133 local runningrow = 1
134 rs={}
135 for r = frow, lrow do
136 if (r-frow) % rstep == 0 then
137 table.move({{[1]=r,[2]=runningrow}}, 1, 1, #rs + 1, rs)
138 runningrow=runningrow+1
139 end
140 if (r-frow) == rres-1 then
141 frow = frow + rres
142 end
143 end
144 -- tex.sprint("\\typeout{**** pattern=",pat,", numseats=",numseats,"}")
145 if string.len(pat) < numseats then
146 pat = string.rep(pat,math.ceil(numseats/string.len(pat)))
147 end
148 for _,row in pairs(rs) do
149 local r=row[1]
150 local rr=row[2]
151 -- tex.sprint("\\typeout{**** seatingSchemeInRow called r=",r,"}")
152 local step = 1
153 local pndx = 1
154 local seat = 1
155 if policy["rtol"] then
156 step = -1
157 pndx = -1
158 seat = numseats
159 end
160 local runningseat=1
161 -- tex.sprint("\\typeout{ Seatscheme is ",pat,"}")
162 while ((seat >= 1) and (seat<= numseats)) do
163 local curkind = AllSeats[findSeatAt(r,seat)].kind
164 -- tex.sprint("\\typeout{ Try seat ",seat,"(kind=",curkind,") with 'pat[",pndx,"]=",string.upper(string.sub(pat,pndx,pndx)),"'}")
165 if string.upper(string.sub(pat,pndx,pndx)) == 'X' and ((curkind == SEATEMPTY) or (curkind == SEATASSIGNED))then
166 -- tex.sprint("\\typeout{*** Ask for assignment, rr=",rr,", rc=",runningseat,", label =",ltext,"}")
167 assignSeatAt(r,seat,{ kind=SEATASSIGNED, rr=rr, rc=runningseat, label=ltext } )
168 runningseat=runningseat+1
169 pndx = pndx + step
170 elseif curkind == SEATEMPTY then
171 pndx = pndx + step
172 elseif string.upper(string.sub(pat,pndx,pndx)) == 'X' and curkind == SEATREMOVED and policy["ignore removed seats"]==false then
173 pndx = pndx + step
174 runningseat=runningseat+1
175 elseif curkind == SEATREMOVED and policy["ignore removed seats"]==false then
176 pndx = pndx + step
177 elseif curkind == AISLE then
178 if policy["aisle restarts"] then
179 pndx = step
180 else
181 pndx = pndx + policy["aisle counts"]*step
182 end
183 end
184 seat = seat + step
185 end
186 end
187 end
188
189 -- Drawing
190 function seatempty(s)
191 local width = AllSeats.seatwidth
192 local height = AllSeats.seatheight
193 tex.sprint("\\node[rectangle,empty seat, minimum width=",width,", minimum height=",height,",rotate=",s.rotate,"] at (",s.x,",",s.y,") {};")
194 tex.sprint("\\node[empty label, minimum width=",width,",rotate=",s.rotate,"] at (",s.x,",",s.y,")"..
195 "{\\tucemptylabelformat{",s.row,"}{",s.col,"}{",s.rrow,"}{",s.rcol,"}{",s.label,"}};")
196 end
197
198 function seatassigned(s)
199 local width = AllSeats.seatwidth
200 local height = AllSeats.seatheight
201 tex.sprint("\\node[rectangle,assigned seat, minimum width=",width,", minimum height=",height,",rotate=",s.rotate,"] at (",s.x,",",s.y,") {Xy};")
202 tex.sprint("\\node[assigned label,minimum width=",width,",rotate=",s.rotate,"] at (",s.x,",",s.y,")"..
203 "{\\scassignedlabelformat{",s.row,"}{",s.col,"}{",s.rrow,"}{",s.rcol,"}{",s.label,"}};")
204 end
205
206 function seatremoved(s)
207 end
208
209 function drawSeats()
210 local case_tbl =
211 {
212 [SEATREMOVED] = seatremoved,
213 [AISLE] = seatremoved,
214 [SEATEMPTY] = seatempty,
215 [SEATASSIGNED] = seatassigned,
216 }
217 for _ ,s in ipairs(AllSeats) do
218 if s.kind ~= nil then
219 -- tex.sprint("\\typeout{ *** kind=",s.kind,", x=",s.col,", y=",s.row,"}")
220 func = case_tbl[s.kind]
221 if func ~= nil then
222 func(s)
223 end
224 end
225 end
226 end
227
228 function getNumberOfSeats()
229 tex.count["scnumofseats"]=AllSeats.size
230 end
231
232 function setupNextlabel(ndx)
233 -- print("ndx=",ndx, "type:",type(ndx))
234 local s = AllSeats[ndx]
235 if s and (s.kind ~=nil) and (s.kind == SEATASSIGNED) then
236 -- print("S={",s.row,",",s.col,",",s.rrow,",",s.rcol,", '",s.label,"' }")
237 tex.sprint("\\toggletrue{scfoundassigned}")
238 tex.sprint("\\def\\scnextlabel{\\scassignedlabelformat{"..tostring(s.row).."}{"..tostring(s.col).."}{"..tostring(s.rrow).."}{"..tostring(s.rcol).."}{"..tostring(s.label).."}}")
239 end
240 end
241
242 function saveTextLabel(ndx)
243 -- print("*** Try to assign textlabel to "..tostring(ndx), tex.box[0],AllSeats[ndx],AllSeats[ndx].textlabel)
244 if tex.box[0] and AllSeats[ndx] then
245 -- print("*** Do assign textlabel to "..tostring(ndx))
246 AllSeats[ndx].textlabel=""
247 -- print("\n")
248 for n in node.traverse(tex.box[0].head) do
249 -- 12: glue, 13: kern
250 if n.id==12 or n.id==13 then AllSeats[ndx].textlabel=AllSeats[ndx].textlabel.." " end
251 -- 29: Glyph
252 if n.id==29 then AllSeats[ndx].textlabel=AllSeats[ndx].textlabel..string.char(n.char) end
253 -- 7/2: disc, automatic
254 if n.id==7 and n.subtype==2 then
255 local rep = n.replace
256 AllSeats[ndx].textlabel=AllSeats[ndx].textlabel..string.char(rep.char)
257 end
258 end
259 end
260 end
261
262 function generateSeatList(outstream,instream, coor)
263 local names={}
264 local emtpydefault
265 if instream ~= nil then
266 f = io.open(instream,"r")
267 if not f then
268 tex.print("\\PackageError{\\packagename}{Can't read file '",instream,"'}{}")
269 return
270 else
271 for line in io.lines(instream) do
272 -- print("*** TAKE "..line)
273 table.insert(names, line)
274 end
275 -- print("names[1]="..names[1])
276 emptydefault=string.gsub(names[1],"[^,]","")
277 -- print("empty='"..emptydefault.."'")
278 f:close()
279 end
280 end
281
282 seatfile = io.open(outstream, "w")
283 if not seatfile then
284 tex.print("\\PackageError{\\packagename}{Can't read file '",instream,"'}{}")
285 return
286 else
287 local index=1
288 for j ,s in ipairs(AllSeats) do
289 -- print("j="..tostring(j))
290 if s.kind == SEATASSIGNED then
291 if names[index] ~= nil then
292 seatfile:write(names[index]..",")
293 elseif instream ~= nil then
294 -- print("** empty='"..emptydefault.."'")
295 seatfile:write(emptydefault..",")
296 end
297 if coor then
298 seatfile:write(tostring(s.row)..","..tostring(s.col)..",")
299 end
300 -- print("Label is",s.textlabel)
301 seatfile:write(s.textlabel)
302 seatfile:write(string.char(10))
303 index=index+1
304 end
305 end
306 seatfile:close()
307 if index <= #names then
308 luatexbase.add_to_callback("stop_run", screportmissing(names,index), "Report" )
309 end
310 end
311 end
312
313 function screportmissing(names, index)
314 local report=string.char(10).."\27[34mPackage 'seatingchart':"..string.char(10)..[[*******************************************
315 The following person(s) couldn't be seated:]]..string.char(10)
316
317 for i=index, #names do
318 report=report.." - \27[1m"..names[i].."\27[22m"..string.char(10)
319 end
320 report=report.."*******************************************"..string.char(10)
321 return function()
322 texio.setescape(false)
323 texio.write_nl(report)
324 end
325 end
326