meta-imp-txt.lmt /size: 3024 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['meta-imp-txt'] = {
2    version   = 1.001,
3    comment   = "companion to meta-imp-txt.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9local setmetatableindex = table.setmetatableindex
10
11local texset       = tex.set
12
13local scan         = mp.scan
14local scannumeric  = scan.numeric
15local scaninteger  = scan.integer
16local scanboolean  = scan.boolean
17local scanstring   = scan.string
18
19local expandmacro  = token.expandmacro -- todo
20
21local bpfactor     = number.dimenfactors.bp
22
23local metapost     = metapost
24metapost.parshapes = { }
25
26local parshapes    = { }
27local properties   = { }
28
29-- initialize shapes to 0 hsize
30
31function metapost.parshapes.reset()
32    parshapes  = { }
33    properties = { }
34end
35
36function metapost.parshapes.next()
37    properties = { }
38    parshapes[#parshapes+1] = properties
39end
40
41function metapost.parshapes.inspect()
42    inspect(parshapes)
43end
44
45function metapost.parshapes.getshape(n)
46    return (parshapes and parshapes[n]) or parshapes or nil
47end
48
49function metapost.parshapes.get(index,name)
50    local v = parshapes[index][name]
51    if type(v) == "boolean" then
52        context(v and 1 or 0)
53    else
54        context(v)
55    end
56end
57
58function metapost.parshapes.wholeshape() -- maybe just collect them earlier
59    local t, n = { }, 0
60    for i=1,#parshapes do
61        local s = parshapes[i].shape
62        if s then
63            for i=1,#s do
64                n = n + 1
65                t[n] = s[i]
66            end
67        end
68    end
69    if n > 0 then
70        texset("parshape",t)
71    end
72end
73
74metapost.registerscript("setparshapeproperty", function()
75    local k = scanstring()
76    if k == "line" then
77        local entry  = properties.shape[scannumeric()]
78        local indent = scannumeric() / bpfactor
79        local width  = scannumeric() / bpfactor
80        entry[1] = indent
81        entry[2] = width
82    elseif k == "lines" then
83        properties.lines = scaninteger()
84        properties.shape = setmetatableindex(function(t,k)
85            local v = { 0, properties.width or 0 }
86            t[k] = v
87            return v
88        end)
89    elseif k == "first" then
90        properties[k] = scanboolean()
91    elseif k == "inspect" then
92        inspect(properties)
93    else
94        properties[k] = scannumeric() / bpfactor
95    end
96end)
97
98interfaces.implement {
99    name      = "setparagraphmetashape",
100    public    = true,
101    protected = true,
102    arguments = "optional",
103    actions   = function(list)
104        if list and list ~= "" then
105            list = utilities.parsers.settings_to_array(list)
106            if #list > 0 then
107                metapost.parshapes.reset()
108                for i=1,#list do
109                    metapost.parshapes.next()
110                    expandmacro("spac_shapes_calculate","{"..list[i].."}")
111                end
112                metapost.parshapes.wholeshape()
113            end
114        end
115    end
116}
117