-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyafoot.lua
175 lines (159 loc) · 4.67 KB
/
yafoot.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
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
pagenumber = 0
local file = io.open(tex.jobname..".fht", "r")
if file == nil
then
has_pages_ftn_ht = false
else
io.input(file)
pages_ftn_ht = {}
has_pages_ftn_ht = false
for line in file:lines() do
local line_arr = {}
for i in string.gmatch(line, "%S+") do
table.insert(line_arr, tonumber(i))
end
table.insert(pages_ftn_ht, line_arr)
has_pages_ftn_ht = true
end
io.close(file)
end
local file = io.open(tex.jobname..".fht", "w")
io.close(file)
if has_pages_ftn_ht
then
if pages_ftn_ht[1] and pages_ftn_ht[1][1] == 0
then
tex.setdimen("global", "my@tcb@ftn@height", pages_ftn_ht[1][2])
end
end
push_footnotes_below_lines = function (head, group)
for item in node.traverse_id(node.id("whatsit"), head) do
local is_footnote = node.has_attribute(item, 100)
if is_footnote and is_footnote > 0
then
local footnote = node.copy(tex.box[is_footnote])
head, new = node.insert_after(head, item, footnote)
node.set_attribute(new, 200, is_footnote)
item = item.next
new.width = 0
end
end
return head
end
-- crush vlist under hlist
crush_height_of_vlist = function (head, group, size)
for list in node.traverse_id(node.id("hlist"), head) do
for item in node.traverse(list) do
local f = node.has_attribute(item, 200)
if f
then
if not node.has_attribute(item, 300)
then
node.set_attribute(item, 300, item.height+item.depth)
item.height = 0
item.depth = 0
end
end
end
end
return head
end
move_footnote_bottom = function (page_head, group, s)
local yaftnins = node.new("vlist")
local n_head = node.copy_list(page_head)
recur = function (n)
for list in node.traverse(n) do
local footnotebox = node.has_attribute(list, 200)
if footnotebox
then
footnote = node.copy(tex.box[footnotebox])
for ftnitem in node.traverse(footnote.head) do
if node.has_attribute(ftnitem, 200)
then
footnote.head = node.remove(footnote.head, ftnitem)
end
end
if yaftnins
then
yaftnins.list, new = node.insert_after(
yaftnins.list, yaftnins.tail, footnote)
end
n_head = node.remove(n_head, list)
n_head = recur(list.head)
elseif list.head
then
n_head = recur(list.head)
end
end
return n_head
end
page_head = recur(n_head)
local split_top
if yaftnins.list
then
tex.box.footins = node.copy(node.vpack(yaftnins.list))
end
return page_head
end
-- called after the page was built (or tried to build)
function page_ftn_height(groupcode)
if groupcode == "after_output"
then
pagenumber = pagenumber+1
if has_pages_ftn_ht
then
for _,l in ipairs(pages_ftn_ht) do
if l[1] == pagenumber
then
tex.setdimen("global", "my@tcb@ftn@height", l[2])
end
end
end
elseif groupcode == "vmode_par" or groupcode == "hmode_par" or groupcode == "insert"
then
ftn_ht = get_ftnheight(tex.lists.contrib_head)
if ftn_ht > 0
then
local new_ftn_ht = ftn_ht
local file = io.open(tex.jobname..".fht", "r")
io.input(file)
curr_page_ftn_arr = {}
for line in file:lines() do
local curr_line_arr = {}
for i in string.gmatch(line, "%S+") do
table.insert(curr_line_arr, tonumber(i))
end
if curr_line_arr[1] == pagenumber
then
new_ftn_ht = new_ftn_ht + curr_line_arr[2]
else
table.insert(curr_page_ftn_arr, line)
end
end
io.close(file)
table.insert(curr_page_ftn_arr, pagenumber.." "..new_ftn_ht)
local table_string = table.concat(curr_page_ftn_arr, "\n")
local file = io.open(tex.jobname..".fht", "w")
io.output(file)
io.write(table_string)
io.close(file)
end
end
end
get_ftnheight = function (n)
local ftnheight = 0
for list in node.traverse(n) do
if node.has_attribute(list, 300)
then
ftnheight = ftnheight + node.get_attribute(list, 300)
ftnheight = ftnheight + get_ftnheight(list.head)
node.unset_attribute(list, 300)
elseif node.has_attribute(list, 200)
then
elseif list.head
then
ftnheight = ftnheight + get_ftnheight (list.head)
end
end
return ftnheight
end