summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/node-syn.lua
blob: 1b8e07382153d21428bf29d7cf6400e7d9f9f3b1 (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
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
if not modules then modules = { } end modules ['node-syn'] = {
    version   = 1.001,
    comment   = "companion to node-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- Because we have these fields in some node that are used by sunctex, I decided (because
-- some users seem to like that feature) to implement a variant that might work out better
-- for ConTeXt. This is experimental code. I don't use it myself so it will take a while
-- to mature. There will be some helpers that one can use in more complex situations like
-- included xml files.
--
-- It is unclear how the output gets interpreted. For instance, we only need to be able to
-- go back to a place where text is entered, but still we need all that redundant box
-- wrapping.
--
-- Possible optimizations: pack whole lines.

local type, rawset = type, rawset
local concat = table.concat
local formatters = string.formatters

local trace = false  trackers.register("system.syntex.visualize", function(v) trace = v end)

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

local getid              = nuts.getid
local getlist            = nuts.getlist
local setlist            = nuts.setlist
local getnext            = nuts.getnext
local getwhd             = nuts.getwhd
local getwidth           = nuts.getwidth
local getsubtype         = nuts.getsubtype
local getattr            = nuts.getattr

local nodecodes          = nodes.nodecodes
local kerncodes          = nodes.kerncodes

local glue_code          = nodecodes.glue
local kern_code          = nodecodes.kern
local kern_disc          = nodecodes.disc
local rule_code          = nodecodes.rule
----- math_code          = nodecodes.math
local hlist_code         = nodecodes.hlist
local vlist_code         = nodecodes.vlist
local glyph_code         = nodecodes.glyph
local fontkern_code      = kerncodes.fontkern

local insert_before      = nuts.insert_before
local insert_after       = nuts.insert_after

local nodepool           = nuts.pool
local new_latelua        = nodepool.latelua
local new_rule           = nodepool.rule
local new_hlist          = nodepool.hlist

local getdimensions      = nuts.dimensions
local getrangedimensions = nuts.rangedimensions

local a_fontkern         = attributes.private("fontkern")

local get_synctex_fields = nuts.get_synctex_fields
local set_synctex_fields = nuts.set_synctex_fields
local set_syntex_tag     = nodes.set_synctex_tag

local getcount           = tex.getcount
local setcount           = tex.setcount

local getpos             = function()
                               getpos = backends.codeinjections.getpos
                               return getpos()
                           end

local f_glue             = formatters["g%i,%i:%i,%i"]
local f_glyph            = formatters["x%i,%i:%i,%i"]
local f_kern             = formatters["k%i,%i:%i,%i:%i"]
local f_rule             = formatters["r%i,%i:%i,%i:%i,%i,%i"]
local f_hlist            = formatters["[%i,%i:%i,%i:%i,%i,%i"]
local f_vlist            = formatters["(%i,%i:%i,%i:%i,%i,%i"]
local s_hlist            = "]"
local s_vlist            = ")"
local f_hvoid            = formatters["h%i,%i:%i,%i:%i,%i,%i"]
local f_vvoid            = formatters["v%i,%i:%i,%i:%i,%i,%i"]

local characters         = fonts.hashes.characters

local synctex            = { }
luatex.synctex           = synctex

-- the file name stuff

local noftags            = 0
local stnums             = { }
local sttags             = table.setmetatableindex(function(t,name)
    noftags = noftags + 1
    t[name] = noftags
    stnums[noftags] = name
    return noftags
end)

function synctex.setfilename(name)
    if set_syntex_tag and name then
        set_syntex_tag(sttags[name])
    end
end

function synctex.resetfilename()
    if set_syntex_tag then
        local name = luatex.currentfile()
        if name then
            set_syntex_tag(name)
        end
    end
end

-- the node stuff

local result             = { }
local r                  = 0
local f                  = nil
local nofsheets          = 0
local nofobjects         = 0
local last               = 0
local filesdone          = 0
local enabled            = false
local compact            = true

local function writeanchor()
    local size = f:seek("end")
    f:write("!" .. (size-last) .. "\n")
    last = size
end

local function writefiles()
    local total = #stnums
    if filesdone < total then
        for i=filesdone+1,total do
            f:write("Input:"..i..":"..stnums[i].."\n")
        end
        filesdone = total
    end
end

local function flushpreamble()
    local jobname = tex.jobname
    stnums[0] = jobname
    f = io.open(file.replacesuffix(jobname,"syncctx"),"w")
    f:write("SyncTeX Version:1\n")
    f:write("Input:0:"..jobname.."\n")
    writefiles()
    f:write("Output:pdf\n")
    f:write("Magnification:1000\n")
    f:write("Unit:1\n")
    f:write("X Offset:0\n")
    f:write("Y Offset:0\n")
    f:write("Content:\n")
    flushpreamble = writefiles
end

local function flushpostamble()
    writeanchor()
    f:write("Postamble:\n")
    f:write("Count:"..nofobjects.."\n")
    writeanchor()
    f:write("Post scriptum:\n")
    f:close()
    enabled = false
end

local pageheight = 0 -- todo: set before we do this!

local function b_hlist(head,current,t,l,w,h,d)
    return insert_before(head,current,new_latelua(function()
        local x, y = getpos()
        r = r + 1
        result[r] = f_hlist(t,l,x,tex.pageheight-y,w,h,d)
        nofobjects = nofobjects + 1
    end))
end

local function b_vlist(head,current,t,l,w,h,d)
    return insert_before(head,current,new_latelua(function()
        local x, y = getpos()
        r = r + 1
        result[r] = f_vlist(t,l,x,tex.pageheight-y,w,h,d)
        nofobjects = nofobjects + 1
    end))
end

local function e_hlist(head,current)
    return insert_after(head,current,new_latelua(function()
        r = r + 1
        result[r] = s_hlist
        nofobjects = nofobjects + 1
    end))
end

local function e_vlist(head,current)
    return insert_after(head,current,new_latelua(function()
        r = r + 1
        result[r] = s_vlist
        nofobjects = nofobjects + 1
    end))
end

local function x_hlist(head,current,t,l,w,h,d)
    return insert_before(head,current,new_latelua(function()
        local x, y = getpos()
        r = r + 1
        result[r] = f_hvoid(t,l,x,tex.pageheight-y,w,h,d)
        nofobjects = nofobjects + 1
    end))
end

local function x_vlist(head,current,t,l,w,h,d)
    return insert_before(head,current,new_latelua(function()
        local x, y = getpos()
        r = r + 1
        result[r] = f_vvoid(t,l,x,tex.pageheight-y,w,h,d)
        nofobjects = nofobjects + 1
    end))
end

-- local function x_glyph(head,current,t,l)
--     return insert_before(head,current,new_latelua(function()
--         local x, y = getpos()
--         r = r + 1
--         result[r] = f_glyph(t,l,x,tex.pageheight-y)
--         nofobjects = nofobjects + 1
--     end))
-- end

-- local function x_glue(head,current,t,l)
--     return insert_before(head,current,new_latelua(function()
--         local x, y = getpos()
--         r = r + 1
--         result[r] = f_glue(t,l,x,tex.pageheight-y)
--         nofobjects = nofobjects + 1
--     end))
-- end

-- local function x_kern(head,current,t,l,k)
--     return insert_before(head,current,new_latelua(function()
--         local x, y = getpos()
--         r = r + 1
--         result[r] = f_kern(t,l,x,tex.pageheight-y,k)
--         nofobjects = nofobjects + 1
--     end))
-- end

-- local function x_rule(head,current,t,l,w,h,d)
--     return insert_before(head,current,new_latelua(function()
--         local x, y = getpos()
--         r = r + 1
--         result[r] = f_rule(t,l,x,tex.pageheight-y,w,h,d)
--         nofobjects = nofobjects + 1
--     end))
-- end

local function collect(head,t,l)
    local current = head
    while current do
        local id = getid(current)
        if id == glyph_code then
            local first = current
            local last  = current
            while true do
                id = getid(current)
                if id == glyph_code or id == disc_code then
                    last = current
                elseif id == kern_code and (getsubtype(current) == fontkern_code or getattr(current,a_fontkern)) then
                    last = current
                else
                    if id == glue_code then
                        -- we could go on when we're in the same t/l run
                        local tc, lc = get_synctex_fields(current)
                        if tc > 0 then
                            t, l = tc, lc
                        end
                        id = nil -- so no test later on
                    end
                    local w, h, d = getdimensions(first,getnext(last))
                 -- local w, h, d = getrangedimensions(head,first,getnext(last))
                    if trace then
                        -- color is already handled so no colors
                        head = insert_before(head,first,new_hlist(new_rule(w,32768,32768)))
                    end
if h < 655360 then
    h = 655360
end
if d < 327680 then
    d = 327680
end
                    head = x_hlist(head,first,t,l,w,h,d)
                    break
                end
                current = getnext(current)
                if not current then
                    local w, h, d = getdimensions(first,getnext(last))
                 -- local w, h, d = getrangedimensions(head,first,getnext(last))
                    if trace then
                        -- color is already handled so no colors
                        head = insert_before(head,first,new_hlist(new_rule(w,32768,32768)))
                    end
if h < 655360 then
    h = 655360
end
if d < 327680 then
    d = 327680
end
                    head = x_hlist(head,first,t,l,w,h,d)
                    return head
                end
            end
        end
        if id == hlist_code then
            local list = getlist(current)
            local tc, lc = get_synctex_fields(current)
            if tc > 0 then
                t, l = tc, lc
            end
            if compact then
                if list then
                    local l = collect(list,t,l)
                    if l ~= list then
                        setlist(current,l)
                    end
                end
            else
                local w, h, d = getwhd(current)
                if w == 0 or (h == 0 and d == 0) then
                    if list then
                        local l = collect(list,t,l)
                        if l ~= list then
                            setlist(current,l)
                        end
                    end
                elseif list then
                 -- head = b_hlist(head,current,t,l,w,h,d)
                    head = b_hlist(head,current,0,0,w,h,d)
                    local l = collect(list,t,l)
                    if l ~= list then
                        setlist(current,l)
                    end
                    head, current = e_hlist(head,current)
                else
                 -- head = x_hlist(head,current,t,l,w,h,d)
                    head = x_hlist(head,current,0,0,w,h,d)
                end
            end
        elseif id == vlist_code then
            local list = getlist(current)
            local tc, lc = get_synctex_fields(current)
            if tc > 0 then
                t, l = tc, lc
            end
            if compact then
                if list then
                    local l = collect(list,t,l)
                    if l ~= list then
                        setlist(current,l)
                    end
                end
            else
                local w, h, d = getwhd(current)
                if w == 0 or (h == 0 and d == 0) then
                    if list then
                        local l = collect(list,t,l)
                        if l ~= list then
                            setlist(current,l)
                        end
                    end
                elseif list then
                 -- head = b_vlist(head,current,t,l,w,h,d)
                    head = b_vlist(head,current,0,0,w,h,d)
                    local l = collect(list,t,l)
                    if l ~= list then
                        setlist(current,l)
                    end
                    head, current = e_vlist(head,current)
                else
                 -- head = x_vlist(head,current,t,l,w,h,d)
                    head = x_vlist(head,current,0,0,w,h,d)
                end
            end
        elseif id == glue_code then
            local tc, lc = get_synctex_fields(current)
            if tc > 0 then
                t, l = tc, lc
            end
         -- head = x_glue(head,current,t,l)
     -- elseif id == kern_code then
     --     local tc, lc = get_synctex_fields(current)
     --     if tc > 0 then
     --         t, l = tc, lc
     --     end
     --  -- local k = getwidth(current)
     --  -- if k ~= 0 then
     --  --     head = x_kern(head,current,t,l,k)
     --  -- end
     -- elseif id == rule_code then
     --     local tc, lc = get_synctex_fields(current)
     --     if tc > 0 then
     --         t, l = tc, lc
     --     end
     --  -- if t > 0 and l > 0 then
     --  -- local w, h, d = getwhd(current)
     --  --     head = x_rule(head,current,t,l,w,h,d)
     --  -- end
        end
        current = getnext(current)
    end
    return head
end

-- range of same numbers

function synctex.collect(head)
    if enabled then
        result, r = { }, 0
        head = collect(tonut(head),0,0)
        return tonode(head), true
    else
        return head, false
    end
end

-- also no solution for bad first file resolving in sumatra

function synctex.flush()
    if enabled then
        nofsheets = nofsheets + 1 -- could be realpageno
        flushpreamble()
        writeanchor()
        f:write("{"..nofsheets.."\n")
        if compact then
            f:write(f_vlist(0,0,0,0,tex.pagewidth,tex.pageheight,0))
            f:write("\n")
        end
        f:write(concat(result,"\n"))
        if compact then
            f:write("\n")
            f:write(s_vlist)
        end
        f:write("\n")
        writeanchor()
        f:write("}"..nofsheets.."\n")
        nofobjects = nofobjects + 2
        result, r = { }, 0
    end
end

function synctex.enable()
    if not enabled and node.set_synctex_mode then
        enabled = true
        node.set_synctex_mode(1)
        tex.normalsynctex = 0
        nodes.tasks.appendaction("shipouts", "after", "nodes.synctex.collect")
    end
end

function synctex.finish()
    if enabled then
        flushpostamble()
    end
end

-- not the best place

luatex.registerstopactions(synctex.finish)

nodes.tasks.appendaction("shipouts", "after", "luatex.synctex.collect")

-- moved here

local report_system = logs.reporter("system")
local synctex       = false

directives.register("system.synctex", function(v)
    if v == "context" then
        luatex.synctex.enable()
        setcount("normalsynctex",0)
        synctex = true
    else
        v = tonumber(v) or (toboolean(v,true) and 1) or (v == "zipped" and 1) or (v == "unzipped" and -1) or 0
        setcount("normalsynctex",v)
        synctex = v ~= 0
    end
    if synctex then
        report_system("synctex functionality is enabled (%s), expect runtime overhead!",tostring(v))
    else
        report_system("synctex functionality is disabled!")
    end
end)

statistics.register("synctex tracing",function()
    if synctex or getcount("normalsynctex") ~= 0 then
        return "synctex has been enabled (extra log file generated)"
    end
end)