typo-dig.lua /size: 5180 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
typo-dig
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to typo-dig.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
-- we might consider doing this after the otf pass because now osf do not work
10
-- out well in node mode.
11 12
local
next
,
type
,
tonumber
=
next
,
type
,
tonumber
13
local
format
,
insert
=
string
.
format
,
table
.
insert
14
local
round
,
div
=
math
.
round
,
math
.
div
15 16
local
trace_digits
=
false
trackers
.
register
(
"
typesetters.digits
"
,
function
(
v
)
trace_digits
=
v
end
)
17 18
local
report_digits
=
logs
.
reporter
(
"
typesetting
"
,
"
digits
"
)
19 20
local
nodes
,
node
=
nodes
,
node
21 22
local
nuts
=
nodes
.
nuts
23 24
local
getnext
=
nuts
.
getnext
25
local
getprev
=
nuts
.
getprev
26
local
getid
=
nuts
.
getid
27
local
getwidth
=
nuts
.
getwidth
28
local
isglyph
=
nuts
.
isglyph
29
local
takeattr
=
nuts
.
takeattr
30 31
local
setlink
=
nuts
.
setlink
32
local
setnext
=
nuts
.
setnext
33
local
setprev
=
nuts
.
setprev
34 35
local
hpack_node
=
nuts
.
hpack
36
local
traverse_id
=
nuts
.
traverse_id
37
local
insert_node_before
=
nuts
.
insert_before
38
local
insert_node_after
=
nuts
.
insert_after
39 40
local
texsetattribute
=
tex
.
setattribute
41
local
unsetvalue
=
attributes
.
unsetvalue
42 43
local
nodecodes
=
nodes
.
nodecodes
44
local
glyph_code
=
nodecodes
.
glyph
45 46
local
nodepool
=
nuts
.
pool
47
local
enableaction
=
nodes
.
tasks
.
enableaction
48 49
local
new_glue
=
nodepool
.
glue
50 51
local
fonthashes
=
fonts
.
hashes
52
local
chardata
=
fonthashes
.
characters
53 54
local
v_reset
=
interfaces
.
variables
.
reset
55 56
local
charbase
=
characters
.
data
57
local
getdigitwidth
=
fonts
.
helpers
.
getdigitwidth
58 59
typesetters
=
typesetters
or
{
}
60
local
typesetters
=
typesetters
61 62
typesetters
.
digits
=
typesetters
.
digits
or
{
}
63
local
digits
=
typesetters
.
digits
64 65
digits
.
actions
=
{
}
66
local
actions
=
digits
.
actions
67 68
local
a_digits
=
attributes
.
private
(
"
digits
"
)
69 70
-- at some point we can manipulate the glyph node so then i need
71
-- to rewrite this then
72 73
function
nodes
.
aligned
(
head
,
start
,
stop
,
width
,
how
)
74
if
how
=
=
"
flushright
"
or
how
=
=
"
middle
"
then
75
head
,
start
=
insert_node_before
(
head
,
start
,
new_glue
(
0
,
65536
,
65536
)
)
76
end
77
if
how
=
=
"
flushleft
"
or
how
=
=
"
middle
"
then
78
head
,
stop
=
insert_node_after
(
head
,
stop
,
new_glue
(
0
,
65536
,
65536
)
)
79
end
80
local
prv
=
getprev
(
start
)
81
local
nxt
=
getnext
(
stop
)
82
setprev
(
start
)
83
setnext
(
stop
)
84
local
packed
=
hpack_node
(
start
,
width
,
"
exactly
"
)
-- no directional mess here, just lr
85
if
prv
then
86
setlink
(
prv
,
packed
)
87
end
88
if
nxt
then
89
setlink
(
packed
,
nxt
)
90
end
91
if
getprev
(
packed
)
then
92
return
head
,
packed
93
else
94
return
packed
,
packed
95
end
96
end
97 98
actions
[
1
]
=
function
(
head
,
start
,
attr
)
99
local
char
,
font
=
isglyph
(
start
)
100
local
unic
=
chardata
[
font
]
[
char
]
.
unicode
or
char
101
if
charbase
[
unic
]
.
category
=
=
"
nd
"
then
-- ignore unic tables
102
local
oldwidth
=
getwidth
(
start
)
103
local
newwidth
=
getdigitwidth
(
font
)
104
if
newwidth
~
=
oldwidth
then
105
if
trace_digits
then
106
report_digits
(
"
digit trigger %a, instance %a, char %C, unicode %U, delta %s
"
,
107
attr
%
100
,
div
(
attr
,
100
)
,
char
,
unic
,
newwidth
-
oldwidth
)
108
end
109
head
,
start
=
nodes
.
aligned
(
head
,
start
,
start
,
newwidth
,
"
middle
"
)
110
return
head
,
start
111
end
112
end
113
return
head
,
start
114
end
115 116
function
digits
.
handler
(
head
)
117
local
current
=
head
118
while
current
do
119
if
getid
(
current
)
=
=
glyph_code
then
120
local
attr
=
takeattr
(
current
,
a_digits
)
121
if
attr
and
attr
>
0
then
122
local
action
=
actions
[
attr
%
100
]
-- map back to low number
123
if
action
then
124
head
,
current
=
action
(
head
,
current
,
attr
)
125
elseif
trace_digits
then
126
report_digits
(
"
unknown digit trigger %a
"
,
attr
)
127
end
128
end
129
end
130
if
current
then
131
current
=
getnext
(
current
)
132
end
133
end
134
return
head
135
end
136 137
local
m
,
enabled
=
0
,
false
-- a trick to make neighbouring ranges work
138 139
function
digits
.
set
(
n
)
-- number or 'reset'
140
if
n
=
=
v_reset
then
141
n
=
unsetvalue
142
else
143
n
=
tonumber
(
n
)
144
if
n
then
145
if
not
enabled
then
146
enableaction
(
"
processors
"
,
"
typesetters.digits.handler
"
)
147
if
trace_digits
then
148
report_digits
(
"
enabling digit handler
"
)
149
end
150
enabled
=
true
151
end
152
if
m
=
=
100
then
153
m
=
1
154
else
155
m
=
m
+
1
156
end
157
n
=
m
*
100
+
n
158
else
159
n
=
unsetvalue
160
end
161
end
162
texsetattribute
(
a_digits
,
n
)
163
end
164 165
-- interface
166 167
interfaces
.
implement
{
168
name
=
"
setdigitsmanipulation
"
,
169
actions
=
digits
.
set
,
170
arguments
=
"
string
"
171
}
172