spac-ali.lua /size: 5680 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
spac-ali
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to spac-ali.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
div
=
math
.
div
10
local
format
=
string
.
format
11 12
local
tasks
=
nodes
.
tasks
13
local
enableaction
=
tasks
.
enableaction
14 15
local
nuts
=
nodes
.
nuts
16
local
nodepool
=
nuts
.
pool
17 18
local
tonode
=
nuts
.
tonode
19
local
tonut
=
nuts
.
tonut
20 21
local
getnext
=
nuts
.
getnext
22
local
getprev
=
nuts
.
getprev
23
local
getid
=
nuts
.
getid
24
local
getlist
=
nuts
.
getlist
25
local
setlist
=
nuts
.
setlist
26
local
setlink
=
nuts
.
setlink
27
local
getdirection
=
nuts
.
getdirection
28
local
takeattr
=
nuts
.
takeattr
29
local
getsubtype
=
nuts
.
getsubtype
30
local
getwidth
=
nuts
.
getwidth
31
local
findtail
=
nuts
.
tail
32 33
local
righttoleft_code
=
nodes
.
dirvalues
.
righttoleft
34 35
local
hpack_nodes
=
nuts
.
hpack
36 37
local
unsetvalue
=
attributes
.
unsetvalue
38 39
local
nodecodes
=
nodes
.
nodecodes
40
local
listcodes
=
nodes
.
listcodes
41 42
local
hlist_code
=
nodecodes
.
hlist
43
local
vlist_code
=
nodecodes
.
vlist
44 45
local
linelist_code
=
listcodes
.
line
46 47
local
new_stretch
=
nodepool
.
stretch
48 49
local
a_realign
=
attributes
.
private
(
"
realign
"
)
50 51
local
texsetattribute
=
tex
.
setattribute
52
local
texgetcount
=
tex
.
getcount
53 54
local
isleftpage
=
layouts
.
status
.
isleftpage
55 56
typesetters
=
typesetters
or
{
}
57
local
alignments
=
{
}
58
typesetters
.
alignments
=
alignments
59 60
local
report_realign
=
logs
.
reporter
(
"
typesetters
"
,
"
margindata
"
)
61
local
trace_realign
=
trackers
.
register
(
"
typesetters.margindata
"
,
function
(
v
)
trace_margindata
=
v
end
)
62 63
local
nofrealigned
=
0
64 65
-- leftskip rightskip parfillskip
66
-- raggedleft 0 + 0 -
67
-- raggedright 0 0 fil
68
-- raggedcenter 0 + 0 + -
69 70
local
function
handler
(
head
,
leftpage
,
realpageno
)
-- traverse_list
71
local
current
=
head
72
while
current
do
73
local
id
=
getid
(
current
)
74
if
id
=
=
hlist_code
then
75
if
getsubtype
(
current
)
=
=
linelist_code
then
76
local
a
=
takeattr
(
current
,
a_realign
)
77
if
not
a
or
a
=
=
0
then
78
-- skip
79
else
80
local
align
=
a
%
10
81
local
pageno
=
div
(
a
,
10
)
82
if
pageno
=
=
realpageno
then
83
-- already ok
84
else
85
local
action
=
0
86
if
align
=
=
1
then
-- flushright
87
action
=
leftpage
and
1
or
2
88
elseif
align
=
=
2
then
-- flushleft
89
action
=
leftpage
and
2
or
1
90
end
91
-- WS: watch this
92
local
direction
=
getdirection
(
current
)
93
-- or should this happen at the tex end:
94
if
direction
=
=
righttoleft_code
then
95
if
action
=
=
1
then
96
action
=
2
97
elseif
action
=
=
2
then
98
action
=
1
99
end
100
end
101
--
102
if
action
=
=
1
then
103
local
head
=
getlist
(
current
)
104
setlink
(
findtail
(
head
)
,
new_stretch
(
3
)
)
-- append
105
setlist
(
current
,
hpack_nodes
(
head
,
getwidth
(
current
)
,
"
exactly
"
,
direction
)
)
106
if
trace_realign
then
107
report_realign
(
"
flushing left, align %a, page %a, realpage %a
"
,
align
,
pageno
,
realpageno
)
108
end
109
elseif
action
=
=
2
then
110
local
list
=
getlist
(
current
)
111
local
head
=
setlink
(
new_stretch
(
3
)
,
list
)
-- prepend
112
setlist
(
current
,
hpack_nodes
(
head
,
getwidth
(
current
)
,
"
exactly
"
,
direction
)
)
113
if
trace_realign
then
114
report_realign
(
"
flushing right. align %a, page %a, realpage %a
"
,
align
,
pageno
,
realpageno
)
115
end
116
elseif
trace_realign
then
117
report_realign
(
"
invalid flushing, align %a, page %a, realpage %a
"
,
align
,
pageno
,
realpageno
)
118
end
119
nofrealigned
=
nofrealigned
+
1
120
end
121
end
122
end
123
handler
(
getlist
(
current
)
,
leftpage
,
realpageno
)
124
elseif
id
=
=
vlist_code
then
125
handler
(
getlist
(
current
)
,
leftpage
,
realpageno
)
126
end
127
current
=
getnext
(
current
)
128
end
129
return
head
130
end
131 132
function
alignments
.
handler
(
head
)
133
return
handler
(
head
,
isleftpage
(
)
,
texgetcount
(
"
realpageno
"
)
)
134
end
135 136
local
enabled
=
false
137 138
function
alignments
.
set
(
n
)
139
if
not
enabled
then
140
enableaction
(
"
shipouts
"
,
"
typesetters.alignments.handler
"
)
141
enabled
=
true
142
if
trace_realign
then
143
report_realign
(
"
enabled
"
)
144
end
145
end
146
texsetattribute
(
a_realign
,
texgetcount
(
"
realpageno
"
)
*
10
+
n
)
147
end
148 149
interfaces
.
implement
{
150
name
=
"
setrealign
"
,
151
actions
=
alignments
.
set
,
152
arguments
=
"
integer
"
,
153
}
154 155
statistics
.
register
(
"
realigning
"
,
function
(
)
156
if
nofrealigned
>
0
then
157
return
format
(
"
%s processed
"
,
nofrealigned
)
158
else
159
return
nil
160
end
161
end
)
162