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

-- later we will consider an OO variant.

-- This module is just a playground. Occasionally we need to typeset
-- at the lua and and this is one method. In principle we can construct
-- pages this way too which sometimes makes sense in dumb cases. Actually,
-- if one only needs this, one does not really need tex, okay maybe the
-- parbuilder but that one can be simplified as well then.

-- set fonts, attributes
-- rest already done in packers etc
-- add local par whatsit (or wait till cleaned up)
-- collapse or new pars
-- interline spacing etc

-- DON'T USE THESE FUNCTIONS AS THEY WILL CHANGE!

local type = type

local utfvalues = string.utfvalues
local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns

local fontdata = fonts.identifiers

local new_glyph_node    = nodes.glyph
local new_glue_node     = nodes.glyph

local copy_node         = node.copy
local copy_node_list    = node.copy_list
local insert_node_after = node.insert_after
local flush_node_list   = node.flush_list
local hpack_node_list   = node.hpack
local vpack_node_list   = node.vpack
local write_node        = node.write

local current_font      = font.current

blobs = blobs or  { }

local newline = lpegpatterns.newline
local space   = lpegpatterns.spacer
local spacing = newline * space^0
local content = (space^1)/" " + (1-spacing)

local ctxtextcapture = lpeg.Ct ( (
    space^0 * (
        newline^2 * space^0 * lpeg.Cc("")
      + newline   * space^0 * lpeg.Cc(" ")
      + lpeg.Cs(content^1)
    )
)^0)

local function tonodes(str,fnt,attr) -- (str,template_glyph)
    if not str or str == "" then
        return
    end
    local head, tail, space, fnt, template = nil, nil, nil, nil, nil
    if not fnt then
        fnt = current_font()
    elseif type(fnt) ~= "number" and fnt.id == "glyph" then
        fnt, template = nil, fnt
 -- else
     -- already a number
    end
    for s in utfvalues(str) do
        local n
        if s == 32 then
            if not space then
                local parameters = fontdata[fnt].parameters
                space = new_glue_node(parameters.space,parameters.space_stretch,parameters.space_shrink)
                n = space
            else
                n = copy_node(space)
            end
        elseif template then
            n = copy_node(template)
            n.char = s
        else
            n = new_glyph_node(fnt,s)
        end
        if attr then -- normall false when template
            n.attr = copy_node_list(attr)
        end
        if head then
            insert_node_after(head,tail,n)
        else
            head = n
        end
        tail = n
    end
    return head, tail
end

blobs.tonodes = tonodes

function blobs.new()
    return {
        list = { },
    }
end

function blobs.append(t,str)
    local kind = type(str)
    local dummy = nil
    if kind == "string" then
        local pars = lpegmatch(ctxtextcapture,str)
        local list = t.list
        for p=1,#pars do
            local str = pars[p]
            if #str == 0 then
                list[#list+1 ] = { head = nil, tail = nil }
            else
                local l = list[#list]
                if not l then
                    l = { head = nil, tail = nil }
                    list[#list+1 ] = l
                end
                local head, tail = tonodes(str,nil,nil)
                if head then
                    if l.head then
                        l.tail.next = head
                        head.prev = l.tail
                        l.tail = tail
                    else
                        l.head, l.tail = head, tail
                    end
                end
            end
        end
    end
end

function blobs.pack(t,how)
    local list = t.list
    for i=1,#list do
        local pack = list[i].pack
        if pack then
            flush_node_list(node.pack)
        end
        if how == "vertical" then
            -- we need to prepend a local par node
            -- list[i].pack = node.vpack(list[i].head,"exactly")
            logs.report("blobs","vpack not yet supported")
        else
            list[i].pack = hpack_node_list(list[i].head,"exactly")
        end
    end
end

function blobs.write(t)
    local list = t.list
    for i=1,#list do
        local pack = list[i].pack
        if pack then
            write_node(pack)
        end
    end
end