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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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)
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
101
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
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 |