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

-- this code dates from the beginning and is kind of experimental; it
-- will be optimized and improved soon

local next, type = next, type
local format = string.format

local has_attribute      = node.has_attribute
local unset_attribute    = node.unset_attribute
local set_attribute      = node.set_attribute
local copy_node          = node.copy
local insert_node_before = node.insert_before
local insert_node_after  = node.insert_after
local make_penalty_node  = nodes.penalty
local make_glue_node     = nodes.glue
local make_disc_node     = nodes.disc

local glyph   = node.id("glyph")
local kern    = node.id("kern")

breakpoints           = breakpoints         or { }
breakpoints.mapping   = breakpoints.mapping or { }
breakpoints.methods   = breakpoints.methods or { }
breakpoints.enabled   = false
breakpoints.attribute = attributes.private("breakpoint")

storage.register("breakpoints/mapping", breakpoints.mapping, "breakpoints.mapping")

local mapping = breakpoints.mapping

function breakpoints.setreplacement(id,char,kind,before,after,language)
    local map = mapping[id]
    if not map then
        map = { }
        mapping[id] = map
    end
    local cmap = map[char]
    if not cmap then
        cmap = { }
        map[char] = cmap
    end
    cmap[language or ""] = { kind or 1, before or 1, after or 1 }
end

breakpoints.methods[1] = function(head,start)
    if start.prev and start.next then
        insert_node_before(head,start,make_penalty_node(10000))
        insert_node_before(head,start,make_glue_node(0))
        insert_node_after(head,start,make_glue_node(0))
        insert_node_after(head,start,make_penalty_node(0))
    end
    return head, start
end
breakpoints.methods[2] = function(head,start) -- ( => (-
    if start.prev and start.next then
        local tmp = start
        start = make_disc_node()
        start.prev, start.next = tmp.prev, tmp.next
        tmp.prev.next, tmp.next.prev = start, start
        tmp.prev, tmp.next = nil, nil
        start.replace = tmp
        local tmp, hyphen = copy_node(tmp), copy_node(tmp)
        hyphen.char = languages.prehyphenchar(tmp.lang)
        tmp.next, hyphen.prev = hyphen, tmp
        start.post = tmp
        insert_node_before(head,start,make_penalty_node(10000))
        insert_node_before(head,start,make_glue_node(0))
        insert_node_after(head,start,make_glue_node(0))
        insert_node_after(head,start,make_penalty_node(10000))
    end
    return head, start
end
breakpoints.methods[3] = function(head,start) -- ) => -)
    if start.prev and start.next then
        local tmp = start
        start = make_disc_node()
        start.prev, start.next = tmp.prev, tmp.next
        tmp.prev.next, tmp.next.prev = start, start
        tmp.prev, tmp.next = nil, nil
        start.replace = tmp
        local tmp, hyphen = copy_node(tmp), copy_node(tmp)
        hyphen.char = languages.prehyphenchar(tmp.lang)
        tmp.prev, hyphen.next = hyphen, tmp
        start.pre = hyphen
        insert_node_before(head,start,make_penalty_node(10000))
        insert_node_before(head,start,make_glue_node(0))
        insert_node_after(head,start,make_glue_node(0))
        insert_node_after(head,start,make_penalty_node(10000))
    end
    return head, start
end
breakpoints.methods[4] = function(head,start) -- - => - - -
    if start.prev and start.next then
        local tmp = start
        start = make_disc_node()
        start.prev, start.next = tmp.prev, tmp.next
        tmp.prev.next, tmp.next.prev = start, start
        tmp.prev, tmp.next = nil, nil
        -- maybe prehyphenchar etc
        start.pre = copy_node(tmp)
        start.post = copy_node(tmp)
        start.replace = tmp
        insert_node_before(head,start,make_penalty_node(10000))
        insert_node_before(head,start,make_glue_node(0))
        insert_node_after(head,start,make_glue_node(0))
        insert_node_after(head,start,make_penalty_node(10000))
    end
    return head, start
end

function breakpoints.process(namespace,attribute,head)
    local done, numbers = false,  languages.numbers
    local start, n = head, 0
    while start do
        local id = start.id
        if id == glyph then
            local attr = has_attribute(start,attribute)
            if attr and attr > 0 then
                unset_attribute(start,attribute) -- maybe test for subtype > 256 (faster)
                -- look ahead and back n chars
                local map = mapping[attr]
                if map then
                    local cmap = map[start.char]
                    if cmap then
                        local smap = cmap[numbers[start.lang]] or cmap[""]
                        if smap then
                            if n >= smap[2] then
                                local m = smap[3]
                                local next = start.next
                                while next do -- gamble on same attribute
                                    local id = next.id
                                    if id == glyph then -- gamble on same attribute
                                        if map[next.char] then
                                            break
                                        elseif m == 1 then
                                            local method = breakpoints.methods[smap[1]]
                                            if method then
                                                head, start = method(head,start)
                                                done = true
                                            end
                                            break
                                        else
                                            m = m - 1
                                            next = next.next
                                        end
                                    elseif id == kern and next.subtype == 0 then
                                        next = next.next
                                        -- ignore intercharacter kerning, will go way
                                    else
                                        -- we can do clever and set n and jump ahead but ... not now
                                        break
                                    end
                                end
                            end
                            n = 0
                        else
                            n = n + 1
                        end
                    else
                         n = n + 1
                    end
                else
                    n = 0
                end
            end
        elseif id == kern and start.subtype == 0 then
            -- ignore intercharacter kerning, will go way
        else
            n = 0
        end
        start = start.next
    end
    return head, done
end

chars.handle_breakpoints = nodes.install_attribute_handler {
    name = "breakpoint",
    namespace = breakpoints,
    processor = breakpoints.process,
}