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

-- this is preliminary code, use insert_before etc

local report_hyphenation = logs.reporter("languages","hyphenation")

local tex          = tex
local context      = context
local commands     = commands
local nodes        = nodes

local splitstring  = string.split

local nodecodes    = nodes.nodecodes

local disc_code    = nodecodes.disc
local hlist_code   = nodecodes.hlist
local vlist_code   = nodecodes.vlist
local glue_code    = nodecodes.glue
local glyph_code   = nodecodes.glyph

local new_penalty  = nodes.pool.penalty
local new_hlist    = nodes.pool.hlist
local new_glue     = nodes.pool.glue

local free_node    = nodes.free
local copy_list    = nodes.copy_list
local copy_node    = nodes.copy
local find_tail    = nodes.tail

local texsetbox    = tex.setbox
local texgetbox    = tex.getbox
local texget       = tex.get

local function hyphenatedlist(list)
    while list do
        local id, next, prev = list.id, list.next, list.prev
        if id == disc_code then
            local hyphen = list.pre
            if hyphen then
                local penalty = new_penalty(-500)
                hyphen.next, penalty.prev = penalty, hyphen
                prev.next, next.prev = hyphen, penalty
                penalty.next, hyphen.prev = next, prev
                list.pre = nil
                free_node(list)
            end
        elseif id == vlist_code or id == hlist_code then
            hyphenatedlist(list.list)
        end
        list = next
    end
end

commands.hyphenatedlist = hyphenatedlist

function commands.showhyphenatedinlist(list)
    report_hyphenation("show: %s",nodes.listtoutf(list,false,true))
end

local function checkedlist(list)
    if type(list) == "number" then
        return texgetbox(list).list
    else
        return list
    end
end

local function applytochars(list,what,nested)
    local doaction = context[what or "ruledhbox"]
    local noaction = context
    local current  = checkedlist(list)
    while current do
        local id = current.id
        if nested and (id == hlist_code or id == vlist_code) then
            context.beginhbox()
            applytochars(current.list,what,nested)
            context.endhbox()
        elseif id ~= glyph_code then
            noaction(copy_node(current))
        else
            doaction(copy_node(current))
        end
        current = current.next
    end
end

local function applytowords(list,what,nested)
    local doaction = context[what or "ruledhbox"]
    local noaction = context
    local current  = checkedlist(list)
    local start
    while current do
        local id = current.id
        if id == glue_code then
            if start then
                doaction(copy_list(start,current))
                start = nil
            end
            noaction(copy_node(current))
        elseif nested and (id == hlist_code or id == vlist_code) then
            context.beginhbox()
            applytowords(current.list,what,nested)
            context.egroup()
        elseif not start then
            start = current
        end
        current = current.next
    end
    if start then
        doaction(copy_list(start))
    end
end

commands.applytochars = applytochars
commands.applytowords = applytowords

local split_char = lpeg.Ct(lpeg.C(1)^0)
local split_word = lpeg.tsplitat(lpeg.patterns.space)
local split_line = lpeg.tsplitat(lpeg.patterns.eol)

function commands.processsplit(str,command,how,spaced)
    how = how or "word"
    if how == "char" then
        local words = lpeg.match(split_char,str)
        for i=1,#words do
            local word = words[i]
            if word == " " then
                if spaced then
                    context.space()
                end
            elseif command then
                context[command](word)
            else
                context(word)
            end
        end
    elseif how == "word" then
        local words = lpeg.match(split_word,str)
        for i=1,#words do
            local word = words[i]
            if spaced and i > 1 then
                context.space()
            end
            if command then
                context[command](word)
            else
                context(word)
            end
        end
    elseif how == "line" then
        local words = lpeg.match(split_line,str)
        for i=1,#words do
            local word = words[i]
            if spaced and i > 1 then
                context.par()
            end
            if command then
                context[command](word)
            else
                context(word)
            end
        end
    else
        context(str)
    end
end

function commands.vboxlisttohbox(original,target,inbetween)
    local current = texgetbox(original).list
    local head = nil
    local tail = nil
    while current do
        if current.id == hlist_code then
            local list = current.list
            if head then
                if inbetween > 0 then
                    local n = new_glue(0,0,inbetween)
                    tail.next = n
                    n.prev = tail
                    tail = n
                end
                tail.next = list
                list.prev = tail
            else
                head = list
            end
            tail = find_tail(list)
            tail.next = nil
            current.list = nil
        end
        current = current.next
    end
    local result = new_hlist()
    result.list = head
    texsetbox(target,result)
end

function commands.hboxtovbox(original)
    local b = texgetbox(original)
    local factor = texget("baselineskip").width / texget("hsize")
    b.depth = 0
    b.height = b.width * factor
end

function commands.boxtostring(n)
    context.puretext(nodes.toutf(tex.box[n].list)) -- helper is defined later
end