1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
if not modules then modules = { } end modules ['driv-shp'] = {
version = 1.001,
optimize = true,
comment = "companion to driv-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local nuts = nodes.nuts
local tonut = nodes.tonut
local getdirection = nuts.getdirection
local getlist = nuts.getlist
local getoffsets = nuts.getoffsets
local getorientation = nuts.getorientation
local getanchors = nuts.getanchors
local getgeometry = nuts.getgeometry
local getwhd = nuts.getwhd
local getkern = nuts.getkern
local getwidth = nuts.getwidth
local getheight = nuts.getheight
local getdepth = nuts.getdepth
local getnext = nuts.getnext
local getid = nuts.getid
local getshift = nuts.getshift
local getprop = nuts.getprop
local getreplace = nuts.getreplace
local setreplace = nuts.setreplace
----- getkerndimension = nuts.getkerndimension
----- getglyphdimensions = nuts.getglyphdimensions
local setdirection = nuts.setdirection
local setlink = nuts.setlink
local nextnode = nuts.traversers.node
local effectiveglue = nuts.effectiveglue
local dirdimensions = nuts.dirdimensions
local nodecodes = nodes.nodecodes
local whatsitcodes = nodes.whatsitcodes
local directioncodes = tex.directioncodes
local lefttoright_code = directioncodes.lefttoright
local righttoleft_code = directioncodes.righttoleft
local glyph_code = nodecodes.glyph
local kern_code = nodecodes.kern
local glue_code = nodecodes.glue
local hlist_code = nodecodes.hlist
local vlist_code = nodecodes.vlist
local dir_code = nodecodes.dir
local disc_code = nodecodes.disc
local math_code = nodecodes.math
local rule_code = nodecodes.rule
local whatsit_code = nodecodes.whatsit
local userdefined_code = nodes.whatsitcodes.userdefined
local drivers = drivers
local report = logs.reporter("drivers")
local magicconstants = tex.magicconstants
local maxdimen = magicconstants.maxdimen
local pos_h = 0
local pos_v = 0
local pos_r = lefttoright_code
local applyorientation = drivers.applyorientation
local applyanchor = drivers.applyanchor
local initialize
local finalize
local userdefined
local function reset_state()
pos_h = 0
pos_v = 0
pos_r = lefttoright_code
end
local dirstack = { }
local function reset_dir_stack()
dirstack = { }
end
local function handlewhatsit(current,pos_h,pos_v)
local action = userdefined[getprop(current,"id")]
if action then
action(current,pos_h,pos_v)
end
end
local hlist_out, vlist_out -- todo: some can be combined
hlist_out = function(this_box,current)
local ref_h = pos_h
local ref_v = pos_v
local ref_r = pos_r
pos_r = getdirection(this_box)
local cur_h = 0
local cur_b
for current, id, subtype in nextnode, current do
if id == glyph_code then -- or id == kern_code
local width, factor = getwidth(current,true)
if width ~= 0 then
if factor ~= 0 then
cur_h = cur_h + (1.0 + factor/1000000.0) * width
else
cur_h = cur_h + width
end
end
elseif id == glue_code then
-- cur_h = cur_h + effectiveglue(current,this_box)
cur_h = cur_h + effectiveglue(current,this_box,true)
elseif id == hlist_code or id == vlist_code then
local width, height, depth = getwhd(current)
local list = getlist(current)
if list then
local boxdir = getdirection(current) or lefttoright_code
local shift = getshift(current)
local geometry, hasoffset, hasorientation, hasanchor = getgeometry(current,true)
local anchor, source, target, targetdata, s_anchor, t_anchor
local anc_h, anc_v
local usedorientation = false
if hasanchor then
anchor, source, target, s_anchor, t_anchor = getanchors(current)
end
if hasorientation then
local orientation, xoffset, yoffset, woffset, hoffset, doffset = getorientation(current)
local orientation, basepoint_h, basepoint_v = applyorientation(orientation,0,shift,width,height,depth,woffset,hoffset,doffset,xoffset,yoffset)
if orientation == 1 then
basepoint_h = basepoint_h + doffset
if boxdir == pos_r then
basepoint_v = basepoint_v - height
end
usedorientation = orientation
elseif orientation == 2 then
if boxdir == pos_r then
basepoint_h = basepoint_h + width
end
usedorientation = orientation
elseif orientation == 3 then
basepoint_h = basepoint_h + hoffset
if boxdir ~= pos_r then
basepoint_v = basepoint_v - height
end
usedorientation = orientation
end
if target then
targetdata = anchors[target]
if targetdata then
anc_h = basepoint_h
anc_v = - basepoint_v
goto posdone
end
end
if pos_r == righttoleft_code then
pos_h = ref_h - (cur_h + basepoint_h)
else
pos_h = ref_h + (cur_h + basepoint_h)
end
-- pos_v = ref_v - (cur_v + basepoint_v)
pos_v = ref_v - basepoint_v
elseif hasoffset then
local orientation, xoffset, yoffset = getorientation(current)
local basepoint_h = boxdir ~= pos_r and width or 0
local basepoint_v = shift
if target then
targetdata = anchors[target]
if targetdata then
anc_h = xoffset + basepoint_h
anc_v = yoffset - basepoint_v
goto posdone
end
end
if pos_r == righttoleft_code then
pos_h = ref_h - (cur_h + basepoint_h + xoffset)
else
pos_h = ref_h + (cur_h + basepoint_h + xoffset)
end
pos_v = ref_v - (basepoint_v - yoffset)
elseif hasanchor then
local basepoint_h = boxdir ~= pos_r and width or 0
local basepoint_v = shift
if target then
targetdata = anchors[target]
if targetdata then
anc_h = basepoint_h
anc_v = - basepoint_v
goto posdone
end
end
if pos_r == righttoleft_code then
pos_h = ref_h - (cur_h + basepoint_h)
else
pos_h = ref_h + (cur_h + basepoint_h)
end
pos_v = ref_v - basepoint_v
else
local basepoint_h = boxdir ~= pos_r and width or 0
local basepoint_v = shift
if pos_r == righttoleft_code then
pos_h = ref_h - (cur_h + basepoint_h)
else
pos_h = ref_h + (cur_h + basepoint_h)
end
pos_v = ref_v - basepoint_v
end
goto process
::posdone::
if pos_r == righttoleft_code then
pos_h = targetdata[1] - anc_h
else
pos_h = targetdata[1] + anc_h
end
pos_v = targetdata[2] + anc_v
if anchor and anchor > 0 then
pos_h, pos_v = applyanchor(t_anchor,true, pos_h,pos_v,targetdata[3],targetdata[4],targetdata[5])
pos_h, pos_v = applyanchor(s_anchor,false,pos_h,pos_v,width,height,depth)
end
::process::
if source then
local anchor_h = pos_h
local anchor_v = pos_v
if usedorientation then
if usedorientation == 1 then
anchor_v = anchor_v - (width - height)
elseif usedorientation == 2 then
anchor_v = anchor_v - (depth - height)
elseif usedorientation == 3 then -- weird
anchor_v = anchor_v + (height - width)
end
end
anchors[source] = { anchor_h, anchor_v, width, height, depth }
end
-- if usedorientation then
-- pushorientation(usedorientation,pos_h,pos_v,pos_r)
-- end
if id == vlist_code then
vlist_out(current,list)
else
hlist_out(current,list)
end
-- if usedorientation then
-- poporientation(usedorientation,pos_h,pos_v,pos_r)
-- end
end
cur_h = cur_h + width
elseif id == kern_code then
local kern, factor = getkern(current,true)
if kern ~= 0 then
if factor ~= 0 then
cur_h = cur_h + (1.0 + factor/1000000.0) * kern
else
cur_h = cur_h + kern
end
end
elseif id == rule_code then
cur_h = cur_h + getwidth(current)
elseif id == math_code then
-- cur_h = cur_h + effectiveglue(current,this_box)
cur_h = cur_h + effectiveglue(current,this_box,true)
elseif id == dir_code then
local dir, cancel = getdirection(current)
if cancel then
local ds = dirstack[current]
if ds then
ref_h = ds.ref_h
ref_v = ds.ref_v
cur_h = ds.cur_h
end
pos_r = dir
else
local width, enddir = dirdimensions(this_box,current)
local new_h = cur_h + width
if dir ~= pos_r then
cur_h = new_h
end
if enddir ~= current then
dirstack[enddir] = {
cur_h = new_h,
ref_h = ref_h,
ref_v = ref_v,
}
setdirection(enddir,pos_r)
end
if pos_r == righttoleft_code then
pos_h = ref_h - cur_h
else
pos_h = ref_h + cur_h
end
pos_v = ref_v
ref_h = pos_h
ref_v = pos_v
cur_h = 0
pos_r = dir
goto synced
end
elseif id == whatsit_code then
if subtype == userdefined_code then
local action = userdefined[getprop(current,"id")]
if action then
if not cur_b then
local wd, ht, dp = getwhd(this_box)
cur_b = { wd, ht, dp, ref_h, ref_v, ref_r }
end
action(current, pos_h,pos_v, cur_b)
end
end
elseif id == disc_code then
local replace, tail = getreplace(current)
if replace then
setlink(tail,getnext(current))
setlink(current,replace)
setreplace(current)
end
else
goto synced
end
if pos_r == righttoleft_code then
pos_h = ref_h - cur_h
else
pos_h = ref_h + cur_h
end
pos_v = ref_v
::synced::
end
pos_h = ref_h
pos_v = ref_v
pos_r = ref_r
end
vlist_out = function(this_box,current)
local ref_h = pos_h
local ref_v = pos_v
local ref_r = pos_r
pos_r = getdirection(this_box)
local cur_v = - getheight(this_box)
local top_edge = cur_v
pos_h = ref_h
pos_v = ref_v - cur_v
local cur_b
for current, id, subtype in nextnode, current do
if id == glue_code then
-- cur_v = cur_v + effectiveglue(current,this_box)
cur_v = cur_v + effectiveglue(current,this_box,true)
elseif id == hlist_code or id == vlist_code then
local width, height, depth = getwhd(current)
local list = getlist(current)
if list then
local boxdir = getdirection(current) or lefttoright_code
local shift = getshift(current)
local geometry, hasoffset, hasorientation, hasanchor = getgeometry(current,true)
local anchor, source, target, targetdata, s_anchor, t_anchor
local usedorientation = false
if hasanchor then
anchor, source, target, s_anchor, t_anchor = getanchors(current)
end
if hasorientation then
local orientation, xoffset, yoffset, woffset, hoffset, doffset = getorientation(current)
local orientation, basepoint_h, basepoint_v = applyorientation(orientation,shift,height,width,height,depth,woffset,hoffset,doffset,xoffset,yoffset)
if orientation == 1 then
basepoint_h = basepoint_h + width - height -- hm
basepoint_v = basepoint_v - height
usedorientation = orientation
elseif orientation == 2 then
basepoint_h = basepoint_h + width
basepoint_v = basepoint_v + depth - height
usedorientation = orientation
elseif orientation == 3 then -- weird
basepoint_h = basepoint_h + height
usedorientation = orientation
end
if target then
targetdata = anchors[target]
if targetdata then
if pos_r == righttoleft_code then
pos_h = targetdata[1] - basepoint_h
else
pos_h = targetdata[1] + basepoint_h
end
pos_v = targetdata[2] - basepoint_v
goto posdone
end
end
if pos_r == righttoleft_code then
pos_h = ref_h - basepoint_h
else
pos_h = ref_h + basepoint_h
end
pos_v = ref_v - (cur_v + basepoint_v)
elseif hasoffset then
local orientation, xoffset, yoffset = getorientation(current)
-- local basepoint_h = shift
-- local basepoint_v = height
if boxdir ~= pos_r then
shift = shift + width
end
if target then
targetdata = anchors[target]
if targetdata then
if pos_r == righttoleft_code then
pos_h = targetdata[1] - (shift + xoffset)
else
pos_h = targetdata[1] + (shift + xoffset)
end
pos_v = targetdata[2] - (height - yoffset)
goto posdone
end
end
if pos_r == righttoleft_code then
pos_h = ref_h - (shift + xoffset)
else
pos_h = ref_h + (shift + xoffset)
end
pos_v = ref_v - (cur_v + height - yoffset)
elseif hasanchor then
-- local basepoint_h = shift
-- local basepoint_v = height
if boxdir ~= pos_r then
shift = shift + width
end
if target then
local a = anchors[target]
if a then
if pos_r == righttoleft_code then
pos_h = targetdata[1] - shift
else
pos_h = targetdata[1] + shift
end
pos_v = targetdata[2] - height
goto posdone
end
end
if pos_r == righttoleft_code then
pos_h = ref_h - shift
else
pos_h = ref_h + shift
end
pos_v = ref_v - (cur_v + height)
else
-- local basepoint_h = shift
-- local basepoint_v = height
if boxdir ~= pos_r then
shift = shift + width
end
if pos_r == righttoleft_code then
pos_h = ref_h - shift
else
pos_h = ref_h + shift
end
pos_v = ref_v - (cur_v + height)
end
goto process
::posdone::
if anchor and anchor > 0 then
pos_h, pos_v = applyanchor(t_anchor,true, pos_h,pos_v,targetdata[3],targetdata[4],targetdata[5])
pos_h, pos_v = applyanchor(s_anchor,false,pos_h,pos_v,width,height,depth)
end
::process::
if source then
-- move this into apply_anchor
local anchor_h = pos_h
local anchor_v = pos_v
if usedorientation then
if usedorientation == 1 then
anchor_v = anchor_v - (width - height)
elseif usedorientation == 2 then
anchor_v = anchor_v - (depth - height)
elseif usedorientation == 3 then -- weird
anchor_v = anchor_v + (height - width)
end
end
anchors[source] = { anchor_h, anchor_v, width, height, depth }
end
-- if usedorientation then
-- pushorientation(usedorientation,pos_h,pos_v,pos_r)
-- end
-- if source then
-- flushstored(current,source,true)
-- end
if id == vlist_code then
vlist_out(current,list)
else
hlist_out(current,list)
end
-- if source then
-- flushstored(current,source,false)
-- end
-- if usedorientation then
-- poporientation(usedorientation,pos_h,pos_v,pos_r)
-- end
end
cur_v = cur_v + height + depth
elseif id == kern_code then
cur_v = cur_v + getkern(current)
elseif id == rule_code then
local width, height, depth = getwhd(current)
cur_v = cur_v + height + depth
elseif id == whatsit_code then
if subtype == userdefined_code then
local action = userdefined[getprop(current,"id")]
if action then
if not cur_b then
local wd, ht, dp = getwhd(this_box)
cur_b = { wd, ht, dp, ref_h, ref_v, ref_r }
end
action(current,pos_h,pos_v)
end
end
else
goto synced
end
pos_h = ref_h
::synced::
end
pos_h = ref_h
pos_v = ref_v
pos_r = ref_r
end
function drivers.converters.analyze(driver,box)
if not driver then
report("error in converter, no driver")
return
elseif box then
box = tonut(box)
else
report("error in converter, no box")
return
end
local width, height, depth = getwhd(box)
local total = height + depth
if height > maxdimen or depth > maxdimen or width > maxdimen or total > maxdimen then
report("error in converter, overflow")
return
end
local actions = driver.actions
local flushers = driver.flushers
initialize = actions.initialize
finalize = actions.finalize
userdefined = flushers.userdefined
reset_dir_stack()
reset_state()
pos_r = getdirection(box)
pos_v = depth
pos_h = pos_r == righttoleft_code and width or 0
local details = {
boundingbox = { 0, 0, width, total },
}
if initialize then
initialize(driver,details)
end
if getid(box) == vlist_code then
vlist_out(box,getlist(box))
else
hlist_out(box,getlist(box))
end
if finalize then
finalize(driver,details)
end
end
|