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

-- When I ran into the following experimental code again, I figured that it dated
-- from the early days of mkiv, so I updates it a bit to fit into todays context.
-- In the process I might have messed up things. For instance we had a diffent
-- wrapper then using head and tail.

-- todo: only letters (no punctuation)
-- todo: nuts

local trace_firstlines   = false  trackers.register("typesetters.firstlines", function(v) trace_firstlines = v end)
local report_firstlines  = logs.reporter("nodes","firstlines")

typesetters.firstlines   = typesetters.firstlines or { }
local firstlines         = typesetters.firstlines

local nodes              = nodes
local tasks              = nodes.tasks

local nodecodes          = nodes.nodecodes
local glyph              = nodecodes.glyph
local rule               = nodecodes.rule
local disc               = nodecodes.disc
local kern               = nodecodes.kern

local traverse_id        = nodes.traverse_id
local free_node_list     = nodes.flush_list
local free_node          = nodes.flush_node
local copy_node_list     = nodes.copy_list
local insert_node_after  = nodes.insert_after
local insert_node_before = nodes.insert_before
local hpack_node_list    = nodes.hpack

local hpack_filter       = nodes.processors.hpack_filter

local newpenalty         = nodes.pool.penalty
local newkern            = nodes.pool.kern
local tracerrule         = nodes.tracers.pool.nodes.rule

local actions            = { }
firstlines.actions       = actions

local a_firstline        = attributes.private('firstline')
local a_color            = attributes.private('color')
local a_transparency     = attributes.private('transparency')
local a_colorspace       = attributes.private('colormodel')

local texsetattribute    = tex.setattribute
local unsetvalue         = attributes.unsetvalue

local variables          = interfaces.variables
local v_default          = variables.default
local v_line             = variables.line
local v_word             = variables.word

----- is_letter          = characters.is_letter
----- categories         = characters.categories

local settings           = nil

function firstlines.set(specification)
    settings = specification or { }
    tasks.enableaction("processors","typesetters.firstlines.handler")
    if trace_firstlines then
        report_firstlines("enabling firstlines")
    end
    texsetattribute(a_firstline,1)
end

commands.setfirstline = firstlines.set

actions[v_line] = function(head,setting)
 -- local attribute = fonts.specifiers.contextnumber(setting.feature) -- was experimental
    local dynamic    = setting.dynamic
    local font       = setting.font
    local noflines   = setting.n or 1
    local ma         = setting.ma or 0
    local ca         = setting.ca
    local ta         = setting.ta
    local hangafter  = tex.hangafter
    local hangindent = tex.hangindent
    local parindent  = tex.parindent
    local nofchars   = 0
    local n          = 0
    local temp       = copy_node_list(head)
    local linebreaks = { }
    for g in traverse_id(glyph,temp) do
        if dynamic > 0 then
            g[0] = dynamic
        end
        g.font = font
    end
    local start = temp
    local list  = temp
    local prev  = temp
    for i=1,noflines do
        local hsize = tex.hsize - tex.leftskip.width - tex.rightskip.width
        if i == 1 then
            hsize = hsize - parindent
        end
        if i <= - hangafter then
            hsize = hsize - hangindent
        end
        while start do
            local id = start.id
            if id == glyph then
                n = n + 1
            elseif id == disc then
                -- this could be an option
            elseif id == kern then -- todo: fontkern
                -- this could be an option
            elseif n > 0 then
                local pack = hpack_node_list(copy_node_list(list,start))
                if pack.width > hsize then
                    free_node_list(pack)
                    list = prev
                    break
                else
                    linebreaks[i] = n
                    prev = start
                    free_node_list(pack)
                    nofchars = n
                end
            end
            start = start.next
        end
        if not linebreaks[i] then
            linebreaks[i] = n
        end
    end
    local start = head
    local n     = 0
    for i=1,noflines do
        local linebreak = linebreaks[i]
        while start and n < nofchars do
            local id = start.id
            if id == glyph then -- or id == disc then
                if dynamic > 0 then
                    start[0] = dynamic
                end
                start.font = font
                if ca and ca > 0 then
                    start[a_colorspace] = ma == 0 and 1 or ma
                    start[a_color]      = ca
                end
                if ta and ta > 0 then
                    start[a_transparency] = ta
                end
                n = n + 1
            end
            if linebreak == n then
                if trace_firstlines then
                    head, start = insert_node_after(head,start,newpenalty(10000)) -- nobreak
                    head, start = insert_node_after(head,start,newkern(-65536))
                    head, start = insert_node_after(head,start,tracerrule(65536,4*65536,2*65536,"darkblue"))
                end
                head, start = insert_node_after(head,start,newpenalty(-10000)) -- break
                break
            end
            start = start.next
        end
    end
    free_node_list(temp)
    return head, true
end

actions[v_word] = function(head,setting)
 -- local attribute = fonts.specifiers.contextnumber(setting.feature) -- was experimental
    local dynamic  = setting.dynamic
    local font     = setting.font
    local words    = 0
    local nofwords = setting.n or 1
    local start    = head
    local ok       = false
    local ma       = setting.ma or 0
    local ca       = setting.ca
    local ta       = setting.ta
    while start do
        local id = start.id
        -- todo: delete disc nodes
        if id == glyph then
            if not ok then
                words = words + 1
                ok = true
            end
            if ca and ca > 0 then
                start[a_colorspace] = ma == 0 and 1 or ma
                start[a_color]      = ca
            end
            if ta and ta > 0 then
                start[a_transparency] = ta
            end
            if dynamic > 0 then
                start[0] = dynamic
            end
            start.font = font
        elseif id == disc then
            -- continue
        elseif id == kern then -- todo: fontkern
            -- continue
        else
            ok = false
            if words == nofwords then
                break
            end
        end
        start = start.next
    end
    return head, true
end

actions[v_default] = actions[v_line]

local function process(namespace,attribute,head)
    local start = head
    local attr  = nil
    while start do
        attr = start[attribute]
        if attr then
            break
        elseif start.id == glyph then
            break
        else
            start = start.next
        end
    end
    if attr then
        -- here as we can process nested boxes first so we need to keep state
        tasks.disableaction("processors","typesetters.firstlines.handler")
     -- texsetattribute(attribute,unsetvalue)
        local alternative = settings.alternative or v_default
        local action = actions[alternative] or actions[v_default]
        if action then
            if trace_firstlines then
                report_firstlines("processing firstlines, alternative %a",alternative)
            end
            return action(head,settings)
        end
    end
    return head, false
end