summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/typo-syn.lmt
blob: dbc5b27ff81c99d6a4dd8b6ef123c61ebeb9ccd4 (plain)
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
if not modules then modules = { } end modules ['typo-syn'] = {
    version   = 1.000,
    optimize  = true,
    comment   = "companion to typo-syn.mkxl",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local nodes             = nodes

local tasks             = nodes.tasks
local enableaction      = tasks.enableaction
----- disableaction     = tasks.disableaction

local nuts              = nodes.nuts
local tonut             = nodes.tonut

local getnext           = nuts.getnext
local getprev           = nuts.getprev
local getid             = nuts.getid
local getsubtype        = nuts.getsubtype
local getattr           = nuts.getattr
local setattrlist       = nuts.setattrlist
local getattrlist       = nuts.getattrlist

local getprop           = nuts.getprop
local setprop           = nuts.setprop

local setlink           = nuts.setlink
local setprev           = nuts.setprev
local setnext           = nuts.setnext
local setoffsets        = nuts.setoffsets

local getwhd            = nuts.getwhd

local getwidth          = nuts.getwidth
local setwidth          = nuts.setwidth
local getdepth          = nuts.getdepth
local setdepth          = nuts.setdepth
local getheight         = nuts.getheight
local setheight         = nuts.setheight
local gettotal          = nuts.gettotal
local getlist           = nuts.getlist
local setlist           = nuts.setlist
local getdisc           = nuts.getdisc
local setdisc           = nuts.setdisc

local hpack             = nuts.hpack
local rangedimensions   = nuts.rangedimensions
local insertbefore      = nuts.insertbefore
local removenode        = nuts.remove
local flushnode         = nuts.flush

local traverselist      = nuts.traverselist

local nodecodes         = nodes.nodecodes
local glyph_code        = nodecodes.glyph
local rule_code         = nodecodes.rule
local disc_code         = nodecodes.disc
local hlist_code        = nodecodes.hlist
local vlist_code        = nodecodes.vlist
local glue_code         = nodecodes.glue
local kern_code         = nodecodes.kern
local penalty_code      = nodecodes.penalty

local line_code             = nodes.listcodes.line
local fontkern_code         = nodes.kerncodes.fontkern
local parfillrightskip_code = nodes.gluecodes.parfillrightskip
local baselineskip_code     = nodes.gluecodes.baselineskip

-----------------

local a_synchronize   = attributes.private("synchronize")

local parallels       = typesetters.parallels or { }
typesetters.parallels = parallels or { }

local registervalue   = attributes.registervalue
local getvalue        = attributes.getvalue
local hasvalues       = attributes.hasvalues

local trace  = false
-- local trace  = true
local report = logs.reporter("parallel")

local pushsavelevel = tex.pushsavelevel -- token.expandmacro("bgroup")
local popsavelevel  = tex.popsavelevel  -- token.expandmacro("egroup")
local dontcomplain  = tex.dontcomplain

local slack    = 6553.6
local index    = 0
local lastattr = nil
local lastline = nil

interfaces.implement {
    name      = "registersynchronize",
    arguments = { "dimen", "dimen", "box" },
    actions   = function(ht,dp,box)
        index = index + 1
        box   = tonut(box)
        local t = {
            index      = index,
            lineheight = ht,
            linedepth  = dp,
            height     = getheight(height),
            depth      = getheight(depth),
            box        = box,
        }
        local v = registervalue(a_synchronize,t)
        tex.setattribute(a_synchronize,v)
        if index > 0 then
            enableaction("vboxbuilders", "typesetters.parallels.handler")
            enableaction("mvlbuilders",  "typesetters.parallels.handler")
        end
    end,

}

local function hsplit(box,width)
    local first    = getlist(box)
    local last     = first
    local current  = first
    local previous = current
    local sofar    = 0
    local lastdisc = nil
    -- todo: option
    width = width - slack
    --
    while true do
        previous = current
        local id = getid(current)
        if id == glyph_code then
            local wd = getwidth(current)
            if sofar + wd > width then
                break
            else
                sofar = sofar + wd
            end
        elseif id == kern_code then
            if getsubtype(current) == fontkern_code then
                -- assume sane kerns
                local wd = getwidth(current)
                sofar = sofar + wd
            else
                last = previous
                local wd = getwidth(current)
                if sofar + wd > width then
                    break
                else
                    sofar = sofar + wd
                end
                lastdisc = nil
            end
        elseif id == disc_code then
            -- move on / inject post if needed
            local pre, post, replace = getdisc(current)
            local wdr = replace and rangedimensions(box,replace) or 0
            if sofar + wdr > width then
                -- handle post and pre here
                last     = previous
                break
            end
            local wdp = pre and rangedimensions(box,pre) or 0
--            if sofar + wdp > width then
--                last     = previous
--lastdisc = current
--                break
--          end
            sofar = sofar + wdr
            lastdisc = current
        elseif id == glue_code then
            last = previous
            local wd = getwidth(current)
            if sofar + wd > width then
                break
            else
                sofar = sofar + wd
            end
            lastdisc = nil
        elseif id == hlist_code or id == vlist_code then
            last = previous
            local wd = getwidth(current)
            if sofar + wd > width then
                break
            else
                sofar = sofar + wd
            end
            lastdisc = nil
        elseif id == rule_code then
            last = previous
            local wd = getwidth(current)
            if sofar + wd > width then
                break
            else
                sofar = sofar + wd
            end
            lastdisc = nil
        else
            lastdisc = nil
        end
        local next = getnext(current)
        if next then
            current = next
        else
            last = previous
            break
        end
    end
    local next
    if lastdisc then
        local pre, post, replace = getdisc(lastdisc)
        last = getprev(lastdisc)
        --
        next = getnext(lastdisc)
        if next then
            setprev(next)
        end
        --
        setlink(last,pre)
        if post then
            setlink(post,next)
            next = post
        end
        setdisc(lastdisc,nil,nil,replace)
        flushnode(lastdisc)
    else
        next = getnext(last)
        if next then
            setprev(next)
        end
        setnext(last)
    end
    while last do
        local id = getid(last)
        if id == glue_code or id == penalty_code then
            first, last = removenode(first,last,true)
        else
            break
        end
    end
    if first then
        pushsavelevel()
        dontcomplain()
        local result, badness = hpack(first,width,"exactly")
        if badness > 200 then
            result = hpack(first)
        end
        popsavelevel()
        setattrlist(result,getattrlist(box)) -- useattrlist(result,box)
        setheight(result,getheight(box))
        setdepth(result,getdepth(box))
        setlist(box,next)
        setwidth(box,rangedimensions(box,next))
        return result
    end
end

local function getproperties(parent)
    local props = getprop(parent,"parallel")
    if not props then
        local w, h, d = getwhd(parent)
        props = {
            width  = w,
            height = h,
            depth  = d,
        }
        setprop(parent,"parallel",props)
    end
    return props
end

local function setproperties(parent,data,result,level,ctotal)
    local props  = getproperties(parent)
    local depth  = props.depth
    local height = props.height
    local delta  = data.linedepth - depth
    if delta > 0 then
        depth = data.linedepth
        setdepth(parent,depth)
        props.depth = depth
        local n = getnext(parent)
        if n and getid(n) == glue_code and getsubtype(n) == baselineskip_code then
            setwidth(n,getwidth(n) - delta)
        end
    end
--     if height < data.lineheight then
--         height = data.lineheight
--         setheight(parent,height)
--         props.height = height
--     end
    local offset = level * ctotal
    if props.depth + offset > depth then
        setdepth(parent,props.depth+offset)
    end
    setoffsets(result,0,-offset)
    setwidth(result,0)
end

local function flush(head,first,last,a,parent,nesting)
    if first and nesting == 0 then
        local data = getvalue(a_synchronize,a)
        local upto = getnext(last)
        if upto and getid(upto) == penalty_code then
            upto = getnext(upto)
        end
        if upto and getid(upto) == glue_code and getsubtype(upto) == parfillrightskip_code then
            upto = getnext(upto)
        end
        local props = getproperties(parent)
        local width = rangedimensions(parent,first,upto)
        if width > props.width then
            width = props.width
        end
        local content = data.box
        local index   = data.index
        if not content then
            if trace then
                report("index %i, verdict %a",index,"done")
            end
        else
            local result = nil
            local cwidth = getwidth(content)
            local ctotal = gettotal(content)
            if cwidth <= width then -- slack
                if trace then
                    report("index %i, available %p, content %p, verdict %a",index,width,cwidth,"fit")
                end
                result   = content
                data.box = nil
            elseif cwidth > width then
                if trace then
                    report("index %i, available %p, content %p, verdict %a",index,width,cwidth,"overflow")
                end
                result   = hsplit(content,width)
                lastattr = a
                lastline = parent
            else
                report("index %i, verdict %a",index,"weird")
            end
            if result then
                setproperties(parent,data,result,1,ctotal)
                head = insertbefore(head,first,result)
            end
        end
    end
    return head
end

local function lastflush(lastline,lastattr)
    local data = getvalue(a_synchronize,lastattr)
    if not data then
        return
    end
    local content = data.box
    if not content or getwidth(content) == 0 then
        return
    end
    local head = getlist(lastline)
    if not head then
        return
    end
    local first  = head
    local last   = nil
    local props  = getproperties(lastline)
    local width  = props.width
    local height = props.height
    local depth  = props.depth
    local level  = 1
    if depth < data.linedepth then
        depth = data.linedepth
        setdepth(lastline,depth)
    end
    if height < data.lineheight then
        height = data.lineheight
        setheight(lastline,height)
    end
    while true do
        local content = data.box
        local index   = data.index
        if content then
            local result = nil
            local total  = 0
            local cwidth = getwidth(content)
            local ctotal = gettotal(content)
            if cwidth <= width then -- slack
                if trace then
                    report("index %i, available %p, content %p, verdict %a",index,width,cwidth,"fit")
                end
                result = content
                data.box  = nil
            elseif cwidth > width then
                if trace then
                    report("index %i, available %p, content %p, verdict %a",index,width,cwidth,"overflow")
                end
                result = hsplit(content,width)
            else
                report("index %i, verdict %a",index,"weird")
            end
            if result then
                level = level + 1
                setproperties(lastline,data,result,level,ctotal)
                head = insertbefore(head,first,result)
                setlist(lastline,head)
            else
                break
            end
        else
            break
        end
    end
end

local processranges = nuts.processranges

function parallels.handler(head,where)
    if where == "hmodepar" and hasvalues(a_synchronize) then
        lastattr = nil
        lastline = nil
        for n, id, subtype in traverselist(head) do
            if subtype == line_code then
                lastattr = nil
                local list = getlist(n)
                local head = processranges(a_synchronize,flush,list,n)
                if head ~= list then
                    setlist(n,head)
                end
            end
        end
        if lastattr and lastline then
            lastflush(lastline,lastattr)
        end
    end
    return head
end