summaryrefslogtreecommitdiff
path: root/tex/context/base/node-tst.lua
blob: d7ea96f26d9e5896d5ab4a4053070a864cacbddf (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
if not modules then modules = { } end modules ['node-tst'] = {
    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"
}

local glue    = node.id("glue")
local penalty = node.id("penalty")
local kern    = node.id("kern")
local glyph   = node.id("glyph")
local whatsit = node.id("whatsit")
local hlist   = node.id("hlist")

local find_node_tail = node.tail or node.slide

local chardata = characters.data

function nodes.the_left_margin(n) -- todo: three values
    while n do
        local id = n.id
        if id == glue then
            if n.subtype == 8 then -- 7 in c/web source
                return n.spec.width
            else
                return 0
            end
        elseif id == whatsit then
            n = n.next
        elseif id == hlist then
            return n.width
        else
            break
        end
    end
    return 0
end

function nodes.the_right_margin(n)
    if n then
        n = find_node_tail(n)
        while n do
            local id = n.id
            if id == glue then
                if n.subtype == 9 then -- 8 in the c/web source
                    return n.spec.width
                else
                    return 0
                end
            elseif id == whatsit then
                n = n.prev
            else
                break
            end
        end
    end
    return false
end

function nodes.somespace(n,all)
    if n then
        local id = n.id
        if id == glue then
            return (all or (n.spec.width ~= 0)) and glue
        elseif id == kern then
            return (all or (n.kern ~= 0)) and kern
        elseif id == glyph then
            local category = chardata[n.char].category
         -- maybe more category checks are needed
            return (category == "zs") and glyph
        end
    end
    return false
end

function nodes.somepenalty(n,value)
    if n then
        local id = n.id
        if id == penalty then
            if value then
                return n.penalty == value
            else
                return true
            end
        end
    end
    return false
end

function nodes.is_display_math(head)
    local n = head.prev
    while n do
        local id = n.id
        if id == penalty then
        elseif id == glue then
            if n.subtype == 6 then -- above_display_short_skip
                return true
            end
        else
            break
        end
        n = n.prev
    end
    n = head.next
    while n do
        local id = n.id
        if id == penalty then
        elseif id == glue then
            if n.subtype == 7 then -- below_display_short_skip
                return true
            end
        else
            break
        end
        n = n.next
    end
    return false
end