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

-- can become typo-par or so

local trace_anchors  = false  trackers.register("paragraphs.anchors",  function(v) trace_anchors = v end)

local nuts           = nodes.nuts
local nodecodes      = nodes.nodecodes
local gluecodes      = nodes.gluecodes
local listcodes      = nodes.listcodes
local whatcodes      = nodes.whatsitcodes

local hlist_code     = nodecodes.hlist
local glue_code      = nodecodes.glue
local whatsit_code   = nodecodes.whatsit
local line_code      = listcodes.line
local leftskip_code  = gluecodes.leftskip
local rightskip_code = gluecodes.rightskip
local textdir_code   = whatcodes.textdir
local localpar_code  = whatcodes.localpar

local tonut          = nodes.tonut
local tonode         = nodes.tonode

local traverse_id    = nuts.traverse_id
local insert_before  = nuts.insert_before
local insert_after   = nuts.insert_after
local findtail       = nuts.tail
local remove_node    = nuts.remove
local hpack_nodes    = nuts.hpack

local getsubtype     = nuts.getsubtype
local getlist        = nuts.getlist
local getid          = nuts.getid
local getnext        = nuts.getnext
local getfield       = nuts.getfield
local setfield       = nuts.setfield

local setprop        = nuts.setprop
local getprop        = nuts.getprop

local nodepool       = nuts.pool
local new_glue       = nodepool.glue
local new_kern       = nodepool.kern
local new_leftskip   = nodepool.leftskip
local new_rightskip  = nodepool.rightskip
local new_hlist      = nodepool.hlist
local new_vlist      = nodepool.vlist
local new_rule       = nodepool.rule

local texgetcount    = tex.getcount

local paragraphs       = { }
typesetters.paragraphs = paragraphs

-- also strip disc

-- We only need to normalize the left side because when we mess around
-- we keep the page stream order (and adding content to the right of the
-- line is a no-go for tagged etc. For the same reason we don't use two
-- left anchors (each side fo leftskip) because there can be stretch. But,
-- maybe there are good reasons for having just that anchor (mostly for
-- educational purposes I guess.)

-- At this stage the localpar node is no longer of any use so we remove
-- it (each line has the direction attached). We might at some point also
-- strip the disc nodes as they no longer serve a purpose but that can
-- better be a helper. Anchoring left has advantage of keeping page stream.

-- indent     : hlist type 3
-- hangindent : shift and width

-- new_glue(0,65536,65536,2,2) -- hss (or skip -width etc)

-- -- rightskip checking
--
--  local tail      = findtail(head)
--  local rightskip = nil
--  local right     = new_hlist()
--  local id        = getid(tail)
--  if id == glue_code then
--      local subtype = getsubtype(tail)
--      if subtype == rightskip_code then
--          rightskip = tail
--      end
--  end
--  if not rightskip then
--      print("inserting rightskip")
--      rightskip = new_rightskip()
--      insert_after(head,tail,rightskip)
--      tail = rightskip
--  end
--  insert_after(head,tail,right)
--
--      tail    = tail,
--      right   = {
--          pack = right,
--          head = nil,
--          tail = nil,
--      }

-- todo: see if we can hook into box in buildpagefilter .. saves traverse

function paragraphs.normalize(head,...)
    if texgetcount("pagebodymode") > 0 then
        -- can be an option, maybe we need a proper state in lua itself
        return head, false
    end
    for line in traverse_id(hlist_code,tonut(head)) do
        if getsubtype(line) == line_code then
            local head     = getlist(line)
            local leftskip = nil
            local anchor   = new_hlist()
            local id       = getid(head)
            local shift    = getfield(line,"shift")
            local width    = getfield(line,"width")
            local hsize    = tex.hsize
            local reverse  = getfield(line,"dir") == "TRT" or false
            if id == glue_code then
                local subtype = getsubtype(head)
                if subtype == leftskip_code then
                    leftskip  = head
                end
                local next = getnext(head)
                if next and getsubtype(next) == localpar_code then
                    head = remove_node(head,next,true)
                end
            elseif id == whatsit_code then
                if getsubtype(head) == localpar_code then
                    head = remove_node(head,head,true)
                end
            end
            head  = insert_before(head,head,anchor)
            shift = shift + width - hsize
            if reverse then
                head  = insert_before(head,head,new_kern(shift))
                insert_after(head,anchor,new_kern(-shift))
            else
                head = insert_before(head,head,new_kern(-shift))
            end
            if not leftskip then
                head = insert_before(head,head,new_leftskip(0))
            end
setfield(anchor,"attr",getfield(line,"attr"))
-- print(nodes.idstostring(head))
-- print("NORMALIZE",line)
            setfield(line,"list",head)
            setprop(line,"line",{
                reverse = reverse,
                width   = width,
                hsize   = hsize,
                shift   = shift,
                head    = head,
                anchor  = {
                    pack = anchor,
                    head = nil,
                    tail = nil,
                },
            })
        end
    end
    return head, true
end

function paragraphs.addtoline(n,list)
    local line = getprop(n,"line")
    if line then
        if trace_anchors and not line.traced then
            line.traced = true
            local rule = new_rule(2*65536,2*65536,1*65536)
            local list = insert_before(rule,rule,new_kern(-1*65536))
            paragraphs.addtoline(n,list)
        end
        local list = tonut(list)
        local what = line.anchor
        local tail = what.tail
        local blob = new_hlist(list)
        if tail then
            insert_after(what.head,what.tail,blob)
        else
            setfield(what.pack,"list",blob)
            what.head = blob
        end
        what.tail = blob
    end
end