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

-- This is experimental code, so when I change it I need to check other modules
-- too.
--
-- Local par nodes are somewhat special. They start a paragraph and then register
-- the par direction. But they can also show op mid paragraph in which case they
-- register boxes and penalties. In that case the direction should not be affected.
--
-- We can assume that when hpack and prelinebreak filters are called, a local par
-- still sits at the head, but after a linebreak pass this node can be after the
-- leftskip (when present).

local nodes         = nodes
local nuts          = nodes.nuts

local nodecodes     = nodes.nodecodes
local localpar_code = nodecodes.localpar

local getid         = nuts.getid
local getsubtype    = nuts.getsubtype
local getdirection  = nuts.getdirection

local dirvalues     = nodes.dirvalues
local lefttoright   = dirvalues.lefttoright
local righttoleft   = dirvalues.righttoleft

local localparnewgraf_code = 0

local function newstack(head,direction)
    local stack = { }
    local top   = 0
    if head and getid(head) == localpar_code and getsubtype(head) == localparnewgraf_code then
        direction = getdirection(head)
    end
    if not direction then
        direction = lefttoright
    elseif direction == "TLT" then
        direction = lefttoright
    elseif direction == "TRT" then
        direction = righttoleft
    end
    local function update(node)
        local dir, pop = getdirection(node)
        if not pop then
            top = top + 1
            stack[top] = dir
            return dir
        elseif top == 0 then
            return direction
        elseif top == 1 then
            top = 0
            return direction
        else
            top = top - 1
            return stack[top]
        end
    end
    return direction, update
end