typo-drp.lua /size: 11 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
typo-drp
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to typo-drp.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
-- This one is sensitive for order (e.g. when combined with first line
10
-- processing.
11 12
-- todo: use isglyph
13 14
local
tonumber
,
type
,
next
=
tonumber
,
type
,
next
15
local
ceil
=
math
.
ceil
16
local
settings_to_hash
=
utilities
.
parsers
.
settings_to_hash
17 18
local
trace_initials
=
false
trackers
.
register
(
"
typesetters.initials
"
,
function
(
v
)
trace_initials
=
v
end
)
19
local
report_initials
=
logs
.
reporter
(
"
nodes
"
,
"
initials
"
)
20 21
local
initials
=
typesetters
.
paragraphs
or
{
}
22
typesetters
.
initials
=
initials
or
{
}
23 24
local
nodes
=
nodes
25 26
local
tasks
=
nodes
.
tasks
27
local
enableaction
=
tasks
.
enableaction
28
local
disableaction
=
tasks
.
disableaction
29 30
local
nuts
=
nodes
.
nuts
31
local
tonut
=
nodes
.
tonut
32 33
local
getnext
=
nuts
.
getnext
34
local
getprev
=
nuts
.
getprev
35
local
getchar
=
nuts
.
getchar
36
local
getid
=
nuts
.
getid
37
local
getattr
=
nuts
.
getattr
38
local
getwhd
=
nuts
.
getwhd
39 40
local
getprop
=
nuts
.
getprop
41
local
setprop
=
nuts
.
setprop
42 43
local
setattr
=
nuts
.
setattr
44
local
setlink
=
nuts
.
setlink
45
local
setprev
=
nuts
.
setprev
46
local
setnext
=
nuts
.
setnext
47
local
setfont
=
nuts
.
setfont
48
local
setchar
=
nuts
.
setchar
49
local
setwhd
=
nuts
.
setwhd
50
local
setkern
=
nuts
.
setkern
51
local
setoffsets
=
nuts
.
setoffsets
52
local
setglyphdata
=
nuts
.
setglyphdata
53
local
setattr
=
nuts
.
setattr
54 55
local
hpack_nodes
=
nuts
.
hpack
56 57
local
nodecodes
=
nodes
.
nodecodes
58 59
local
nodepool
=
nuts
.
pool
60
local
new_kern
=
nodepool
.
kern
61 62
local
insert_before
=
nuts
.
insert_before
63
local
insert_after
=
nuts
.
insert_after
64
local
remove_node
=
nuts
.
remove
65
local
start_of_par
=
nuts
.
start_of_par
66 67
local
nextnode
=
nuts
.
traversers
.
node
68
local
nextglyph
=
nuts
.
traversers
.
glyph
69 70
local
variables
=
interfaces
.
variables
71
local
v_default
=
variables
.
default
72
local
v_margin
=
variables
.
margin
73
local
v_auto
=
variables
.
auto
74
local
v_first
=
variables
.
first
75
local
v_last
=
variables
.
last
76 77
local
texget
=
tex
.
get
78
local
texset
=
tex
.
set
79
local
unsetvalue
=
attributes
.
unsetvalue
80 81
local
glyph_code
=
nodecodes
.
glyph
82
local
hlist_code
=
nodecodes
.
hlist
83
local
glue_code
=
nodecodes
.
glue
84
local
kern_code
=
nodecodes
.
kern
85
local
localpar_code
=
nodecodes
.
localpar
86 87
local
actions
=
{
}
88
initials
.
actions
=
actions
89 90
local
a_initial
=
attributes
.
private
(
"
initial
"
)
91
local
a_color
=
attributes
.
private
(
'
color
'
)
92
local
a_transparency
=
attributes
.
private
(
'
transparency
'
)
93
local
a_colormodel
=
attributes
.
private
(
'
colormodel
'
)
94 95
local
category
=
characters
.
category
96 97
local
function
set
(
par
,
specification
)
98
enableaction
(
"
processors
"
,
"
typesetters.initials.handler
"
)
99
if
trace_initials
then
100
report_initials
(
"
enabling initials
"
)
101
end
102
setprop
(
par
,
a_initial
,
specification
)
103
end
104 105
function
initials
.
set
(
specification
)
106
nuts
.
setparproperty
(
set
,
specification
)
107
end
108 109
interfaces
.
implement
{
110
name
=
"
setinitial
"
,
111
actions
=
initials
.
set
,
112
arguments
=
{
113
{
114
{
"
location
"
}
,
115
{
"
enabled
"
,
"
boolean
"
}
,
116
{
"
method
"
}
,
117
{
"
distance
"
,
"
dimen
"
}
,
118
{
"
hoffset
"
,
"
dimen
"
}
,
119
{
"
voffset
"
,
"
dimen
"
}
,
120
{
"
font
"
,
"
integer
"
}
,
121
{
"
dynamic
"
,
"
integer
"
}
,
122
{
"
ca
"
,
"
integer
"
}
,
123
{
"
ma
"
,
"
integer
"
}
,
124
{
"
ta
"
,
"
integer
"
}
,
125
{
"
n
"
,
"
integer
"
}
,
126
{
"
m
"
,
"
integer
"
}
,
127
}
128
}
129
}
130 131
-- todo: prevent linebreak .. but normally a initial ends up at the top of
132
-- a page so this has a low priority
133 134
actions
[
v_default
]
=
function
(
head
,
setting
)
135
-- begin of par
136
local
first
=
getnext
(
head
)
137
local
indent
=
false
138
-- parbox .. needs to be set at 0
139
if
first
and
getid
(
first
)
=
=
hlist_code
then
140
first
=
getnext
(
first
)
141
indent
=
true
142
end
143
-- we need to skip over kerns and glues (signals)
144
while
first
and
getid
(
first
)
~
=
glyph_code
do
145
first
=
getnext
(
first
)
146
end
147
if
first
and
getid
(
first
)
=
=
glyph_code
then
148
local
ma
=
setting
.
ma
or
0
149
local
ca
=
setting
.
ca
150
local
ta
=
setting
.
ta
151
local
last
=
first
152
local
distance
=
setting
.
distance
or
0
153
local
voffset
=
setting
.
voffset
or
0
154
local
hoffset
=
setting
.
hoffset
or
0
155
local
parindent
=
texget
(
"
parindent
"
)
156
local
baseline
=
texget
(
"
baselineskip
"
,
false
)
157
local
lines
=
tonumber
(
setting
.
n
)
or
0
158
local
dynamic
=
setting
.
dynamic
159
local
font
=
setting
.
font
160
local
method
=
settings_to_hash
(
setting
.
method
)
161
local
length
=
tonumber
(
setting
.
m
)
or
1
162
--
163
-- 1 char | n chars | skip first quote | ignore punct | keep punct
164
--
165
if
getattr
(
first
,
a_initial
)
then
166
for
current
in
nextnode
,
getnext
(
first
)
do
167
if
getattr
(
current
,
a_initial
)
then
168
last
=
current
169
else
170
break
171
end
172
end
173
elseif
method
[
v_auto
]
then
174
local
char
=
getchar
(
first
)
175
local
kind
=
category
(
char
)
176
if
kind
=
=
"
po
"
or
kind
=
=
"
pi
"
then
177
if
method
[
v_first
]
then
178
-- remove quote etc before initial
179
local
next
=
getnext
(
first
)
180
if
not
next
then
181
-- don't start with a quote or so
182
return
head
183
end
184
last
=
nil
185
for
current
in
nextglyph
,
next
do
186
head
,
first
=
remove_node
(
head
,
first
,
true
)
187
first
=
current
188
last
=
first
189
break
190
end
191
if
not
last
then
192
-- no following glyph or so
193
return
head
194
end
195
else
196
-- keep quote etc with initial
197
local
next
=
getnext
(
first
)
198
if
not
next
then
199
-- don't start with a quote or so
200
return
head
201
end
202
for
current
in
nextglyph
,
next
do
203
last
=
current
204
break
205
end
206
if
last
=
=
first
then
207
return
head
208
end
209
end
210
elseif
kind
=
=
"
pf
"
then
211
-- error: final quote
212
else
213
-- okay
214
end
215
-- maybe also: get all A. B. etc
216
local
next
=
getnext
(
first
)
217
if
next
then
218
for
current
,
char
in
nextglyph
,
next
do
219
local
kind
=
category
(
char
)
220
if
kind
=
=
"
po
"
then
221
if
method
[
v_last
]
then
222
-- remove period etc after initial
223
remove_node
(
head
,
current
,
true
)
224
else
225
-- keep period etc with initial
226
last
=
current
227
end
228
end
229
break
230
end
231
end
232
else
233
for
current
in
nextglyph
,
first
do
234
last
=
current
235
if
length
<
=
1
then
236
break
237
else
238
length
=
length
-
1
239
end
240
end
241
end
242
local
current
=
first
243
while
true
do
244
local
id
=
getid
(
current
)
245
if
id
=
=
kern_code
then
246
setkern
(
current
,
0
)
247
elseif
id
=
=
glyph_code
then
248
local
next
=
getnext
(
current
)
249
if
font
then
250
setfont
(
current
,
font
)
251
end
252
if
dynamic
>
0
then
253
setglyphdata
(
current
,
dynamic
)
254
end
255
-- can be a helper
256
if
ca
and
ca
>
0
then
257
setattr
(
current
,
a_colormodel
,
ma
=
=
0
and
1
or
ma
)
258
setattr
(
current
,
a_color
,
ca
)
259
end
260
if
ta
and
ta
>
0
then
261
setattr
(
current
,
a_transparency
,
ta
)
262
end
263
--
264
end
265
if
current
=
=
last
then
266
break
267
else
268
current
=
getnext
(
current
)
269
end
270
end
271
-- We pack so that successive handling cannot touch the dropped cap. Packaging
272
-- in a hlist is also needed because we cannot locally adapt e.g. parindent (not
273
-- yet stored in with localpar).
274
local
prev
=
getprev
(
first
)
275
local
next
=
getnext
(
last
)
276
--
277
setprev
(
first
)
278
setnext
(
last
)
279
local
dropper
=
hpack_nodes
(
first
)
280
local
width
,
height
,
depth
=
getwhd
(
dropper
)
281
setwhd
(
dropper
,
0
,
0
,
0
)
282
--
283
setlink
(
prev
,
dropper
)
284
setlink
(
dropper
,
next
)
285
--
286
if
next
then
287
local
current
=
next
288
while
current
do
289
local
id
=
getid
(
current
)
290
if
id
=
=
glue_code
or
id
=
=
kern_code
then
291
local
next
=
getnext
(
current
)
292
-- remove_node(current,current,true) -- created an invalid next link and dangling remains
293
remove_node
(
head
,
current
,
true
)
294
current
=
next
295
else
296
break
297
end
298
end
299
end
300
--
301
local
hoffset
=
width
+
hoffset
+
distance
+
(
indent
and
parindent
or
0
)
302
for
current
in
nextglyph
,
first
do
303
setoffsets
(
current
,
-
hoffset
,
-
voffset
)
-- no longer - height here
304
if
current
=
=
last
then
305
break
306
end
307
end
308
--
309
first
=
dropper
310
--
311
if
setting
.
location
=
=
v_margin
then
312
-- okay
313
else
314
if
lines
=
=
0
then
-- safeguard, not too precise
315
lines
=
ceil
(
(
height
+
voffset
)
/
baseline
)
316
end
317
-- We cannot set parshape yet ... when we can I'll add a slope
318
-- option (positive and negative, in emwidth).
319
local
hangafter
=
-
lines
320
local
hangindent
=
width
+
distance
321
if
trace_initials
then
322
report_initials
(
"
setting hangafter to %i and hangindent to %p
"
,
hangafter
,
hangindent
)
323
end
324
texset
(
"
hangafter
"
,
hangafter
)
325
texset
(
"
hangindent
"
,
hangindent
)
326
end
327
if
indent
then
328
insert_after
(
first
,
first
,
new_kern
(
-
parindent
)
)
329
end
330
end
331
return
head
332
end
333 334
-- we can count ... when all done, we can disable ...
335 336
function
initials
.
handler
(
head
)
337
if
getid
(
head
)
=
=
localpar_code
and
start_of_par
(
head
)
then
338
local
settings
=
getprop
(
head
,
a_initial
)
339
if
settings
then
340
disableaction
(
"
processors
"
,
"
typesetters.initials.handler
"
)
341
local
alternative
=
settings
.
alternative
or
v_default
342
local
action
=
actions
[
alternative
]
or
actions
[
v_default
]
343
if
action
then
344
if
trace_initials
then
345
report_initials
(
"
processing initials, alternative %a
"
,
alternative
)
346
end
347
return
action
(
head
,
settings
)
348
end
349
end
350
end
351
return
head
352
end
353