good-ctx.lua /size: 8215 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
good-ctx
'
]
=
{
2
version
=
1
.
000
,
3
comment
=
"
companion to font-lib.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
-- depends on ctx
10 11
local
type
,
next
,
tonumber
=
type
,
next
,
tonumber
12
local
find
,
splitup
=
string
.
find
,
string
.
splitup
13 14
local
fonts
=
fonts
15
local
nodes
=
nodes
16
local
attributes
=
attributes
17 18
----- trace_goodies = false trackers.register("fonts.goodies", function(v) trace_goodies = v end)
19
----- report_goodies = logs.reporter("fonts","goodies")
20 21
local
allocate
=
utilities
.
storage
.
allocate
22
local
setmetatableindex
=
table
.
setmetatableindex
23 24
local
implement
=
interfaces
.
implement
25 26
local
registerotffeature
=
fonts
.
handlers
.
otf
.
features
.
register
27
----- registerafmfeature = fonts.handlers.afm.features.register
28
----- registertfmfeature = fonts.handlers.tfm.features.register
29 30
local
fontgoodies
=
fonts
.
goodies
or
{
}
31 32
local
nuts
=
nodes
.
nuts
33
local
tonut
=
nuts
.
tonut
34
local
getattr
=
nuts
.
getattr
35
local
nextglyph
=
nuts
.
traversers
.
glyph
36 37
-- colorschemes
38 39
local
colorschemes
=
fontgoodies
.
colorschemes
or
allocate
{
}
40
fontgoodies
.
colorschemes
=
colorschemes
41
colorschemes
.
data
=
colorschemes
.
data
or
{
}
42 43
local
privatestoo
=
true
44 45
local
function
setcolorscheme
(
tfmdata
,
scheme
)
46
if
type
(
scheme
)
=
=
"
string
"
then
47
local
goodies
=
tfmdata
.
goodies
48
-- todo : check for already defined in shared
49
if
goodies
then
50
local
what
51
for
i
=
1
,
#
goodies
do
52
-- last one counts
53
local
g
=
goodies
[
i
]
54
what
=
g
.
colorschemes
and
g
.
colorschemes
[
scheme
]
or
what
55
end
56
if
type
(
what
)
=
=
"
table
"
then
57
-- this is font bound but we can share them if needed
58
-- just as we could hash the conversions (per font)
59
local
hash
=
tfmdata
.
resources
.
unicodes
60
local
reverse
=
{
}
61
local
characters
=
tfmdata
.
characters
62
for
i
=
1
,
#
what
do
63
local
w
=
what
[
i
]
64
for
j
=
1
,
#
w
do
65
local
name
=
w
[
j
]
66
local
kind
=
type
(
name
)
67
if
name
=
=
"
*
"
then
68
-- inefficient but only used for tracing anyway
69
for
_
,
unicode
in
next
,
hash
do
70
reverse
[
unicode
]
=
i
71
end
72
elseif
kind
=
=
"
number
"
then
73
reverse
[
name
]
=
i
74
elseif
kind
~
=
"
string
"
then
75
-- ignore invalid entries
76
elseif
find
(
name
,
"
:
"
,
1
,
true
)
then
77
local
start
,
stop
=
splitup
(
name
,
"
:
"
)
78
start
=
tonumber
(
start
)
79
stop
=
tonumber
(
stop
)
80
if
start
and
stop
then
81
-- limited usage: we only deal with non reassigned
82
-- maybe some day I'll also support the ones with a
83
-- tounicode in this range
84
for
unicode
=
start
,
stop
do
85
if
characters
[
unicode
]
then
86
reverse
[
unicode
]
=
i
87
end
88
end
89
end
90
else
91
local
unicode
=
hash
[
name
]
92
if
unicode
then
93
reverse
[
unicode
]
=
i
94
end
95
end
96
end
97
end
98
if
privatestoo
then
99
local
privateoffset
=
fonts
.
constructors
.
privateoffset
100
local
descriptions
=
tfmdata
.
descriptions
101
for
unicode
,
data
in
next
,
characters
do
102
if
unicode
>
=
privateoffset
then
103
if
not
reverse
[
unicode
]
then
104
local
d
=
descriptions
[
unicode
]
105
if
d
then
106
local
u
=
d
.
unicode
107
if
u
then
108
local
r
=
reverse
[
u
]
-- also catches tables
109
if
r
then
110
reverse
[
unicode
]
=
r
111
end
112
end
113
end
114
end
115
end
116
end
117
end
118
tfmdata
.
properties
.
colorscheme
=
reverse
119
return
120
end
121
end
122
end
123
tfmdata
.
properties
.
colorscheme
=
false
124
end
125 126
local
fontproperties
=
fonts
.
hashes
.
properties
127
local
a_colorscheme
=
attributes
.
private
(
'
colorscheme
'
)
128
local
setnodecolor
=
nodes
.
tracers
.
colors
.
set
129
local
cache
=
{
}
-- this could be a weak table
130 131
setmetatableindex
(
cache
,
function
(
t
,
a
)
132
local
v
=
{
}
133
setmetatableindex
(
v
,
function
(
t
,
c
)
134
local
v
=
"
colorscheme:
"
.
.
a
.
.
"
:
"
.
.
c
135
t
[
c
]
=
v
136
return
v
137
end
)
138
t
[
a
]
=
v
139
return
v
140
end
)
141 142
function
colorschemes
.
coloring
(
head
)
143
local
lastfont
=
nil
144
local
lastattr
=
nil
145
local
lastcache
=
nil
146
local
lastscheme
=
nil
147
for
n
,
char
,
f
in
nextglyph
,
head
do
148
local
a
=
getattr
(
n
,
a_colorscheme
)
149
if
a
then
150
if
f
~
=
lastfont
then
151
lastfont
=
f
152
lastscheme
=
fontproperties
[
f
]
.
colorscheme
153
end
154
if
a
~
=
lastattr
then
155
lastattr
=
a
156
lastcache
=
cache
[
a
]
157
end
158
if
lastscheme
then
159
local
sc
=
lastscheme
[
char
]
160
if
sc
then
161
setnodecolor
(
n
,
lastcache
[
sc
]
)
-- we could inline this one
162
end
163
end
164
end
165
end
166
return
head
167
end
168 169
function
colorschemes
.
enable
(
)
170
nodes
.
tasks
.
enableaction
(
"
processors
"
,
"
fonts.goodies.colorschemes.coloring
"
)
171
function
colorschemes
.
enable
(
)
end
172
end
173 174
registerotffeature
{
175
name
=
"
colorscheme
"
,
176
description
=
"
goodie color scheme
"
,
177
initializers
=
{
178
base
=
setcolorscheme
,
179
node
=
setcolorscheme
,
180
}
181
}
182 183
-- kern hackery:
184
--
185
-- yes : use goodies table
186
-- auto : assume features to be set (often ccmp only)
187 188
local
function
setkeepligatures
(
tfmdata
)
189
if
not
tfmdata
.
properties
.
keptligatures
then
190
local
goodies
=
tfmdata
.
goodies
191
if
goodies
then
192
for
i
=
1
,
#
goodies
do
193
local
g
=
goodies
[
i
]
194
local
letterspacing
=
g
.
letterspacing
195
if
letterspacing
then
196
local
keptligatures
=
letterspacing
.
keptligatures
197
if
keptligatures
then
198
local
unicodes
=
tfmdata
.
resources
.
unicodes
-- so we accept names
199
local
hash
=
{
}
200
for
k
,
v
in
next
,
keptligatures
do
201
local
u
=
unicodes
[
k
]
202
if
u
then
203
hash
[
u
]
=
true
204
else
205
-- error: unknown name
206
end
207
end
208
tfmdata
.
properties
.
keptligatures
=
hash
209
end
210
end
211
end
212
end
213
end
214
end
215 216
registerotffeature
{
217
name
=
"
keepligatures
"
,
218
description
=
"
keep ligatures in letterspacing
"
,
219
initializers
=
{
220
base
=
setkeepligatures
,
221
node
=
setkeepligatures
,
222
}
223
}
224 225
if
implement
then
226 227
implement
{
228
name
=
"
enablefontcolorschemes
"
,
229
onlyonce
=
true
,
230
actions
=
colorschemes
.
enable
,
231
overload
=
true
,
-- for now, permits new font loader
232
}
233 234
end
235