blob-ini.lua /size: 4908 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['blob-ini'] = {
2    version   = 1.001,
3    comment   = "companion to blob-ini.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
9-- This module is just a playground. Occasionally we need to typeset at the lua and and
10-- this is one method. In principle we can construct pages this way too which sometimes
11-- makes sense in dumb cases. Actually, if one only needs this, one does not really need
12-- tex, okay maybe the parbuilder but that one can be simplified as well then.
13
14-- set fonts, attributes
15-- rest already done in packers etc
16-- add local par whatsit (or wait till cleaned up)
17-- collapse or new pars
18-- interline spacing etc
19
20-- blob.char
21-- blob.line
22-- blob.paragraph
23-- blob.page
24
25local type, tostring = type, tostring
26local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
27
28local report_blobs  = logs.reporter("blobs")
29
30local flushnodelist = nodes.flushlist
31local hpacknodelist = nodes.hpack
32
33local typesetters   = nodes.typesetters
34local tonodes       = typesetters.tonodes
35local tohpack       = typesetters.tohpack
36local tovpack       = typesetters.tovpack
37
38local context       = context
39
40local implement     = interfaces.implement
41
42blobs               = blobs or  { }
43local blobs         = blobs
44
45blobs.tonodes       = tonodes
46blobs.tohpack       = tohpack
47blobs.tovpack       = tovpack
48
49-- end of helpers
50
51local newline = lpeg.patterns.newline
52local space   = lpeg.patterns.spacer
53local newpar  = (space^0*newline*space^0)^2
54
55local ctxtextcapture = lpeg.Ct ( ( space^0 * ( newpar + lpeg.Cs(((space^1/" " + 1)-newpar)^1) ) )^0)
56
57function blobs.new()
58    return {
59        list = { },
60    }
61end
62
63function blobs.dispose(t)
64    local list = t.list
65    for i=1,#list do
66        local li = list[i]
67        local pack = li.pack
68        if pack then
69            flushnodelist(pack)
70            li.pack = nil
71        end
72    end
73end
74
75function blobs.append(t,str) -- compare concat and link
76    local typ = type(str)
77    local dummy = nil
78    if typ == "number" then
79        str = tostring(str)
80        typ = "string"
81    end
82    if typ == "string" then
83        local pars = lpegmatch(ctxtextcapture,str)
84        local list = t.list
85        for p=1,#pars do
86            local head, tail = tonodes(pars[p],nil,nil)
87            list[#list+1] = { head = head, tail = tail }
88        end
89    end
90end
91
92function blobs.pack(t,how)
93    local list = t.list
94    for i=1,#list do
95        local pack = list[i].pack
96        if pack then
97            flushnodelist(pack)
98        end
99        if how == "vertical" then
100            -- we need to prepend a local par node
101            -- list[i].pack = vpack_node_list(list[i].head,"exactly")
102            report_blobs("vpack not yet supported")
103        else
104            list[i].pack = hpacknodelist(list[i].head,"exactly")
105        end
106    end
107end
108
109function blobs.write(t)
110    local list = t.list
111    for i=1,#list do
112        local li = list[i]
113        local pack = li.pack
114        if pack then
115            context(pack)
116            flushnodelist(pack)
117            li.pack = nil
118        end
119    end
120end
121
122function blobs.dimensions(t)
123    local list = t.list
124    local first = list and list[1]
125    if first then
126        local pack = first.pack
127        return pack.width, pack.height, pack.depth
128    else
129        return 0, 0, 0
130    end
131end
132
133-- blob.char
134-- blob.line: head, tail
135-- blob.paragraph
136-- blob.page
137
138-- local lineblob = {
139--     type = "line",
140--     head = false,
141--     tail = false,
142--     pack = false,
143--     properties = { },
144-- end
145
146-- local parblob = {
147--     type = "line",
148--     head = false,
149--     tail = false,
150--     pack = false,
151--     properties = { },
152-- end
153
154-- for the moment here:
155
156local function strwd(str)
157    local l = tohpack(str)
158    local w = l.width
159    flushnodelist(l)
160    return w
161end
162
163local function strht(str)
164    local l = tohpack(str)
165    local h = l.height
166    flushnodelist(l)
167    return h
168end
169
170local function strdp(str)
171    local l = tohpack(str)
172    local d = l.depth
173    flushnodelist(l)
174    return d
175end
176
177local function strhd(str)
178    local l = tohpack(str)
179    local s = l.height + l.depth
180    flushnodelist(l)
181    return s
182end
183
184blobs.strwd = strwd
185blobs.strht = strht
186blobs.strdp = strdp
187blobs.strhd = strhd
188
189-- upgraded
190
191local scan_hbox = tokens.scanners.hbox
192
193implement { name = "strwd", actions = function() local l = scan_hbox() context(l.width)            flushnodelist(l) end }
194implement { name = "strht", actions = function() local l = scan_hbox() context(l.height)           flushnodelist(l) end }
195implement { name = "strdp", actions = function() local l = scan_hbox() context(l.depth)            flushnodelist(l) end }
196implement { name = "strhd", actions = function() local l = scan_hbox() context(l.height + l.depth) flushnodelist(l) end }
197