1if 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
9local fontproperties = fonts . hashes . properties
10local fontparameters = fonts . hashes . parameters
11
12local starttiming = statistics . starttiming
13local stoptiming = statistics . stoptiming
14
15local bpfactor = number . dimenfactors . bp
16local texgetbox = tex . getbox
17local formatters = string . formatters
18
19local rulecodes = nodes . rulecodes
20local normalrule_code = rulecodes . normal
21
22
23
24
25
26
27
28
29local outlinerule_code = rulecodes . outline
30
31local fonts = { }
32local pages = { }
33local buffer = { }
34local b = 0
35local converter = nil
36
37local function reset ( )
38 buffer = { }
39 b = 0
40end
41
42local f_font = formatters [ " \\definefont[%s][file:%s*none @ %sbp]\n " ]
43
44local f_glyph = formatters [ [[ draw textext.drt("\%s\char%i\relax") shifted (%N,%N); ]] ]
45local f_rule = formatters [ [[ fill unitsquare xscaled %N yscaled %N shifted (%N,%N); ]] ]
46local f_outline = formatters [ [[ draw unitsquare xscaled %N yscaled %N shifted (%N,%N); ]] ]
47
48
49
50local function outputfilename ( driver )
51 return tex . jobname . . " -output.tex "
52end
53
54local function save ( )
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 )
75end
76
77local function prepare ( driver )
78 converter = drivers . converters . lmtx
79 luatex . registerstopactions ( 1 , function ( )
80 save ( )
81 end )
82end
83
84local function initialize ( driver , details )
85 reset ( )
86 b = b + 1
87 buffer [ b ] = " \n\\startMPpage "
88end
89
90local function finalize ( driver , details )
91 b = b + 1
92 buffer [ b ] = " \\stopMPpage\n "
93 pages [ details . pagenumber ] = table . concat ( buffer , " \n " , 1 , b )
94end
95
96local function wrapup ( driver )
97end
98
99local function cleanup ( driver )
100 reset ( )
101end
102
103local function convert ( driver , boxnumber , pagenumber )
104 converter ( driver , texgetbox ( boxnumber ) , " page " , pagenumber )
105end
106
107
108
109local last
110
111local 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
122end
123
124local 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 )
127end
128
129local 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
137end
138
139local 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 )
142end
143
144
145
146drivers . 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 |