summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/meta-imp-txt.lmt
blob: e4b88bd3698fe148ceab48358edb15c2460e774f (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
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 texset       = tex.set

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

local expandmacro  = token.expandmacro -- todo

local bpfactor     = number.dimenfactors.bp

local metapost     = metapost
metapost.parshapes = { }

local parshapes    = { }
local properties   = { }

-- initialize shapes to 0 hsize

function metapost.parshapes.reset()
    parshapes  = { }
    properties = { }
end

function metapost.parshapes.next()
    properties = { }
    parshapes[#parshapes+1] = properties
end

function metapost.parshapes.inspect()
    inspect(parshapes)
end

function metapost.parshapes.getshape(n)
    return (parshapes and parshapes[n]) or parshapes or nil
end

function metapost.parshapes.get(index,name)
    local v = parshapes[index][name]
    if type(v) == "boolean" then
        context(v and 1 or 0)
    else
        context(v)
    end
end

function metapost.parshapes.wholeshape() -- maybe just collect them earlier
    local t, n = { }, 0
    for i=1,#parshapes do
        local s = parshapes[i].shape
        if s then
            for i=1,#s do
                n = n + 1
                t[n] = s[i]
            end
        end
    end
    if n > 0 then
        texset("parshape",t)
    end
end

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)

interfaces.implement {
    name      = "setparagraphmetashape",
    public    = true,
    protected = true,
    arguments = "optional",
    actions   = function(list)
        if list and list ~= "" then
            list = utilities.parsers.settings_to_array(list)
            if #list > 0 then
                metapost.parshapes.reset()
                for i=1,#list do
                    metapost.parshapes.next()
                    expandmacro("spac_shapes_calculate","{"..list[i].."}")
                end
                metapost.parshapes.wholeshape()
            end
        end
    end
}