back-mps.lua /size: 5697 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
back-mps
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to lpdf-ini.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
fontproperties
=
fonts
.
hashes
.
properties
10
local
fontparameters
=
fonts
.
hashes
.
parameters
11 12
local
starttiming
=
statistics
.
starttiming
13
local
stoptiming
=
statistics
.
stoptiming
14 15
local
bpfactor
=
number
.
dimenfactors
.
bp
16
local
texgetbox
=
tex
.
getbox
17
local
formatters
=
string
.
formatters
18 19
local
rulecodes
=
nodes
.
rulecodes
20
local
normalrule_code
=
rulecodes
.
normal
21
----- boxrule_code = rulecodes.box
22
----- imagerule_code = rulecodes.image
23
----- emptyrule_code = rulecodes.empty
24
----- userrule_code = rulecodes.user
25
----- overrule_code = rulecodes.over
26
----- underrule_code = rulecodes.under
27
----- fractionrule_code = rulecodes.fraction
28
----- radicalrule_code = rulecodes.radical
29
local
outlinerule_code
=
rulecodes
.
outline
30 31
local
fonts
=
{
}
32
local
pages
=
{
}
33
local
buffer
=
{
}
34
local
b
=
0
35
local
converter
=
nil
36 37
local
function
reset
(
)
38
buffer
=
{
}
39
b
=
0
40
end
41 42
local
f_font
=
formatters
[
"
\\definefont[%s][file:%s*none @ %sbp]\n
"
]
43 44
local
f_glyph
=
formatters
[
[[
draw textext.drt("\%s\char%i\relax") shifted (%N,%N);
]]
]
45
local
f_rule
=
formatters
[
[[
fill unitsquare xscaled %N yscaled %N shifted (%N,%N);
]]
]
46
local
f_outline
=
formatters
[
[[
draw unitsquare xscaled %N yscaled %N shifted (%N,%N);
]]
]
47 48
-- actions
49 50
local
function
outputfilename
(
driver
)
51
return
tex
.
jobname
.
.
"
-output.tex
"
52
end
53 54
local
function
save
(
)
-- might become a driver function that already is plugged into stopactions
55
starttiming
(
drivers
)
56
if
#
pages
>
0
then
57
local
filename
=
outputfilename
(
)
58
drivers
.
report
(
"
saving result in %a
"
,
filename
)
59
reset
(
)
60
b
=
b
+
1
61
buffer
[
b
]
=
"
\\starttext\n
"
62
for
k
,
v
in
table
.
sortedhash
(
fonts
)
do
63
b
=
b
+
1
64
buffer
[
b
]
=
f_font
(
v
.
name
,
v
.
filename
,
v
.
size
)
65
end
66
for
i
=
1
,
#
pages
do
67
b
=
b
+
1
68
buffer
[
b
]
=
pages
[
i
]
69
end
70
b
=
b
+
1
71
buffer
[
b
]
=
"
\\stoptext\n
"
72
io
.
savedata
(
filename
,
table
.
concat
(
buffer
,
"
"
,
1
,
b
)
)
73
end
74
stoptiming
(
drivers
)
75
end
76 77
local
function
prepare
(
driver
)
78
converter
=
drivers
.
converters
.
lmtx
79
luatex
.
registerstopactions
(
1
,
function
(
)
80
save
(
)
81
end
)
82
end
83 84
local
function
initialize
(
driver
,
details
)
85
reset
(
)
86
b
=
b
+
1
87
buffer
[
b
]
=
"
\n\\startMPpage
"
88
end
89 90
local
function
finalize
(
driver
,
details
)
91
b
=
b
+
1
92
buffer
[
b
]
=
"
\\stopMPpage\n
"
93
pages
[
details
.
pagenumber
]
=
table
.
concat
(
buffer
,
"
\n
"
,
1
,
b
)
94
end
95 96
local
function
wrapup
(
driver
)
97
end
98 99
local
function
cleanup
(
driver
)
100
reset
(
)
101
end
102 103
local
function
convert
(
driver
,
boxnumber
,
pagenumber
)
104
converter
(
driver
,
texgetbox
(
boxnumber
)
,
"
page
"
,
pagenumber
)
105
end
106 107
-- flushers
108 109
local
last
110 111
local
function
updatefontstate
(
id
)
112
if
fonts
[
id
]
then
113
last
=
fonts
[
id
]
.
name
114
else
115
last
=
"
MPSfont
"
.
.
converters
.
Characters
(
id
)
116
fonts
[
id
]
=
{
117
filename
=
file
.
basename
(
fontproperties
[
id
]
.
filename
)
,
118
size
=
fontparameters
[
id
]
.
size
*
bpfactor
,
119
name
=
last
,
120
}
121
end
122
end
123 124
local
function
flushcharacter
(
current
,
pos_h
,
pos_v
,
pod_r
,
font
,
char
)
125
b
=
b
+
1
126
buffer
[
b
]
=
f_glyph
(
last
,
char
,
pos_h
*
bpfactor
,
pos_v
*
bpfactor
)
127
end
128 129
local
function
flushrule
(
current
,
pos_h
,
pos_v
,
pod_r
,
size_h
,
size_v
,
subtype
)
130
if
subtype
=
=
normalrule_code
then
131
b
=
b
+
1
132
buffer
[
b
]
=
f_rule
(
size_h
*
bpfactor
,
size_v
*
bpfactor
,
pos_h
*
bpfactor
,
pos_v
*
bpfactor
)
133
elseif
subtype
=
=
outlinerule_code
then
134
b
=
b
+
1
135
buffer
[
b
]
=
f_outline
(
size_h
*
bpfactor
,
size_v
*
bpfactor
,
pos_h
*
bpfactor
,
pos_v
*
bpfactor
)
136
end
137
end
138 139
local
function
flushsimplerule
(
current
,
pos_h
,
pos_v
,
pod_r
,
size_h
,
size_v
)
140
b
=
b
+
1
141
buffer
[
b
]
=
f_rule
(
size_h
*
bpfactor
,
size_v
*
bpfactor
,
pos_h
*
bpfactor
,
pos_v
*
bpfactor
)
142
end
143 144
-- installer
145 146
drivers
.
install
{
147
name
=
"
mps
"
,
148
actions
=
{
149
prepare
=
prepare
,
150
initialize
=
initialize
,
151
finalize
=
finalize
,
152
wrapup
=
wrapup
,
153
cleanup
=
cleanup
,
154
convert
=
convert
,
155
outputfilename
=
outputfilename
,
156
}
,
157
flushers
=
{
158
updatefontstate
=
updatefontstate
,
159
character
=
flushcharacter
,
160
rule
=
flushrule
,
161
simplerule
=
flushsimplerule
,
162
}
163
}
164 165
-- extras
166 167
-- if not mp then
168
-- return
169
-- end
170
--
171
-- local mpprint = mp.print
172
-- local formatters = string.formatters
173
--
174
-- local f_glyph = formatters[ [[draw textext.drt("\setfontid%i\relax\char%i\relax") shifted (%N,%N);]] ]
175
-- local f_rule = formatters[ [[fill unitsquare xscaled %N yscaled %N shifted (%N,%N);]] ]
176
--
177
-- local current = nil
178
-- local size = 0
179
--
180
-- function mp.place_buffermake(box)
181
-- drivers.convert("mps",box)
182
-- current = drivers.action("mps","fetch")
183
-- size = #current
184
-- end
185
--
186
-- function mp.place_buffersize()
187
-- mpprint(size)
188
-- end
189
--
190
-- function mp.place_bufferslot(i)
191
-- if i > 0 and i <= size then
192
-- local b = buffer[i]
193
-- local t = b[1]
194
-- if t == "glyph" then
195
-- mpprint(f_glyph(b[2],b[3],b[4]*bpfactor,b[5]*bpfactor))
196
-- elseif t == "rule" then
197
-- mpprint(f_rule(b[2]*bpfactor,b[3]*bpfactor,b[4]*bpfactor,b[5]*bpfactor))
198
-- end
199
-- end
200
-- end
201
--
202
-- function mp.place_bufferwipe()
203
-- current = nil
204
-- size = 0
205
-- end
206