summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkxl/typo-shp.lmt
blob: 30719c40e2879fe4f9d2b95cb796dbab9e4154f1 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
if not modules then modules = { } end modules ['meta-imp-txt'] = {
    version   = 1.001,
    comment   = "companion to meta-imp-txt.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

local setmetatableindex = table.setmetatableindex
local settings_to_array = utilities.parsers.settings_to_array

local texget       = tex.get
local texset       = tex.set
local texgetcount  = tex.getcount
local texgetglue   = tex.getglue

local expandmacro  = token.expand_macro

local implement    = interfaces.implement

local scan         = mp.scan
local scannumeric  = scan.numeric
local scaninteger  = scan.integer
local scanboolean  = scan.boolean
local scanstring   = scan.string

local bpfactor     = number.dimenfactors.bp

local context      = context

local parshapes    = { }
local properties   = { }

metapost.registerscript("setparshapeproperty", function()
    local k = scanstring()
    if k == "line" then
        local entry  = properties.shape[scannumeric()]
        local indent = scannumeric() / bpfactor
        local width  = scannumeric() / bpfactor
        entry[1] = indent
        entry[2] = width
    elseif k == "lines" then
        properties.lines = scaninteger()
        properties.shape = setmetatableindex(function(t,k)
            local v = { 0, properties.width or 0 }
            t[k] = v
            return v
        end)
    elseif k == "first" then
        properties[k] = scanboolean()
    elseif k == "inspect" then
        inspect(properties)
    else
        properties[k] = scannumeric() / bpfactor
    end
end)

implement {
    name      = "setparagraphmetashape",
    public    = true,
    protected = true,
    arguments = { "optional", "optional" },      -- array
    actions   = function(list,options)
        if list and list ~= "" then
            list    = settings_to_array(list)    -- array
            options = settings_to_array(options) -- array
            if #list > 0 then
                parshapes  = { }
                properties = { }
                for i=1,#list do
                    properties = { }
                    parshapes[i] = properties
                    expandmacro("spac_shapes_calculate","{"..list[i].."}")
                end
                local t, n = { }, 0
                for i=1,#parshapes do
                    local p = parshapes[i]
                    local s = p.shape
                    if s then
                        for i=1,(p.lines or #s) do
                            n = n + 1
                            t[n] = s[i]
                        end
                    end
                end
                if n > 0 then
                    for i=1,#options do
                        t[options[i]] = true
                    end
                    texset("parshape",t)
                end
            end
        end
    end
}

-- implement {
--     name      = "resetparagraphmetashape",
--     public    = true,
--     protected = true,
--     actions   = function(list)
--         parshapes  = { }
--         properties = { }
--     end
-- }

implement {
    name      = "getshapeparameter",
    public    = true,
    arguments = "string",
    actions   = function(name)
        local index = texgetcount("shapetextindex")
        local value = parshapes[index][name]
        if type(value) == "boolean" then
            value = value and 1 or 0
        end
        context(value or 0) -- so the first check, for "lines" is always ok
    end
}

-- Another experiment: continuing parshapes with alternative definitions:
--
-- left d | right d | left d right d | both d | left d hsize d |
-- copy n | reset | repeat | done

do

    local scanners     = tokens.scanners
    local scanword     = scanners.word
    local scandimen    = scanners.dimen
    local scanstring   = scanners.string
    local scancardinal = scanners.cardinal

    implement {
        name      = "setparagraphshape",
        protected = true,
        actions   = function()
            local t = { }
            local n = 0
            local h = texget("hsize")
            local a = 0
            while true do
                local key = scanword()
              ::AGAIN::
                if key == "left" then
                    local l = scandimen()
                    key = scanword()
                    if key == "right" then
                        n = n + 1 ; t[n] = { l, a + h - l - scandimen() }
                    elseif key == "hsize" then
                        n = n + 1 ; t[n] = { l, a + scandimen() }
                    else
                        n = n + 1 ; t[n] = { l, a + h }
                        goto AGAIN
                    end
                elseif key == "right" then
                    n = n + 1 ; t[n] = { 0, a + h - scandimen() }
                elseif key == "both" then
                    local b = scandimen()
                    n = n + 1 ; t[n] = { b, a + h - b - b }
                elseif key == "copy" then
                    local c = scancardinal()
                    for i=1,c do
                        local m = n + 1
                        t[m] = t[n]
                        n = m
                    end
                elseif key == "done" then
                    -- in case the user ended with "done"
                    scanword()
                    break
                elseif key == "metapost" then
                    local list = settings_to_array(scanstring())    -- array
                    properties = { }
                    parshapes  = { }
                    for i=1,#list do
                        properties = { }
                        parshapes[i] = properties
                        expandmacro("spac_shapes_calculate","{"..list[i].."}")
                    end
                    for i=1,#parshapes do
                        local p = parshapes[i]
                        local s = p.shape
                        if s then
                            for i=1,(p.lines or #s) do
                                local si = s[i]
                                n = n + 1 ; t[n] = { si[1], a + si[2] }
                            end
                        end
                    end
                elseif key == "repeat" then
                    t["repeat"] = true
                elseif key == "delete" then
                    local c = scancardinal()
                    for i=1,c do
                        if n > 0 then
                            t[n] = nil
                            n = n - 1
                        else
                            break
                        end
                    end
                elseif key == "reset" then
                    n = n + 1 ; t[n] = { 0, a + h }
                    break
                elseif key == "absolute" then
                    local s = scanword()
                    local l = texgetglue("leftskip")
                    local r = texgetglue("rightskip")
                    if s == "left" then
                        a = l
                    elseif s == "right" then
                        a = r
                    elseif s == "both" then
                        a = l + r
                    else
                        a = l + r
                        goto AGAIN
                    end
                else
                    logs.report("system","bad key %a in paragraphshape",key)
                    break
                end
            end
            texset("parshape",t)
        end,
    }

    local NC = context.NC
    local NR = context.NR
    local VL = context.VL

    implement {
        name      = "showparagraphshape",
        protected = true,
        public    = true,
        actions   = function()
            local p = texget("parshape")
            if p then
                -- only english interface (for now)
                context.inleftmargin(
                    {
                        align   = "flushright",
                        strut   = "no",
                        width   = "0pt",
                     -- voffset = "-\\lineheight"
                    }, function()
                        context.starttabulate {
                            before = "",
                            after  = "",
                            unit   = "2pt",
                            rulethickness = ".1pt",
                            format = "|rb{\\smallinfofont}|lb{\\smallinfofont}|"
                        }
                            for i=1,#p do
                                NC() context("%P",p[i][1])
                                VL() context("%P",p[i][2])
                                NC() NR()
                            end
                        context.stoptabulate()
                    end
                )
            end
        end
    }

end