diff options
Diffstat (limited to 'tex/context/base/pack-rul.lua')
-rw-r--r-- | tex/context/base/pack-rul.lua | 131 |
1 files changed, 97 insertions, 34 deletions
diff --git a/tex/context/base/pack-rul.lua b/tex/context/base/pack-rul.lua index 329ea63b8..151642c3a 100644 --- a/tex/context/base/pack-rul.lua +++ b/tex/context/base/pack-rul.lua @@ -14,6 +14,14 @@ if not modules then modules = { } end modules ['pack-rul'] = { -- challenge: adapt glue_set -- setfield(h,"glue_set", getfield(h,"glue_set") * getfield(h,"width")/maxwidth -- interesting ... doesn't matter much +-- \framed[align={lohi,middle}]{$x$} +-- \framed[align={lohi,middle}]{$ $} +-- \framed[align={lohi,middle}]{\hbox{ }} +-- \framed[align={lohi,middle}]{\hbox{}} +-- \framed[align={lohi,middle}]{$\hskip2pt$} + +local type = type + local hlist_code = nodes.nodecodes.hlist local vlist_code = nodes.nodecodes.vlist local box_code = nodes.listcodes.box @@ -21,15 +29,26 @@ local line_code = nodes.listcodes.line local texsetdimen = tex.setdimen local texsetcount = tex.setcount -local texgetbox = tex.getbox -local hpack = nodes.hpack -local free = nodes.free -local copy = nodes.copy_list -local traverse_id = nodes.traverse_id -local node_dimensions = nodes.dimensions -function commands.doreshapeframedbox(n) - local box = texgetbox(n) +local implement = interfaces.implement + +local nuts = nodes.nuts + +local getfield = nuts.getfield +local setfield = nuts.setfield +local getnext = nuts.getnext +local getprev = nuts.getprev +local getlist = nuts.getlist +local getid = nuts.getid +local getsubtype = nuts.getsubtype +local getbox = nuts.getbox + +local hpack = nuts.hpack +local traverse_id = nuts.traverse_id +local node_dimensions = nuts.dimensions + +local function doreshapeframedbox(n) + local box = getbox(n) local noflines = 0 local firstheight = nil local lastdepth = nil @@ -38,27 +57,27 @@ function commands.doreshapeframedbox(n) local maxwidth = 0 local totalwidth = 0 local averagewidth = 0 - local boxwidth = box.width + local boxwidth = getfield(box,"width") if boxwidth ~= 0 then -- and h.subtype == vlist_code - local list = box.list + local list = getlist(box) if list then local function check(n,repack) if not firstheight then - firstheight = n.height + firstheight = getfield(n,"height") end - lastdepth = n.depth + lastdepth = getfield(n,"depth") noflines = noflines + 1 - local l = n.list + local l = getlist(n) if l then if repack then - local subtype = n.subtype + local subtype = getsubtype(n) if subtype == box_code or subtype == line_code then - lastlinelength = node_dimensions(l,n.dir) -- used to be: hpack(copy(l)).width + lastlinelength = node_dimensions(l,getfield(n,"dir")) -- used to be: hpack(copy(l)).width else - lastlinelength = n.width + lastlinelength = getfield(n,"width") end else - lastlinelength = n.width + lastlinelength = getfield(n,"width") end if lastlinelength > maxwidth then maxwidth = lastlinelength @@ -80,33 +99,34 @@ function commands.doreshapeframedbox(n) -- vdone = true end if not firstheight then - -- done + -- done) elseif maxwidth ~= 0 then if hdone then for h in traverse_id(hlist_code,list) do - local l = h.list + local l = getlist(h) if l then - local subtype = h.subtype + local subtype = getsubtype(h) if subtype == box_code or subtype == line_code then - h.list = hpack(l,maxwidth,'exactly',h.dir) - h.shift = 0 -- needed for display math + l = hpack(l,maxwidth,'exactly',getfield(h,"dir")) -- multiple return values + setfield(h,"list",l) + setfield(h,"shift",0) -- needed for display math, so no width check possible end - h.width = maxwidth + setfield(h,"width",maxwidth) end end - box.width = maxwidth -- moved - averagewidth = noflines > 0 and totalwidth/noflines or 0 end -- if vdone then -- for v in traverse_id(vlist_code,list) do - -- local width = n.width + -- local width = getfield(n,"width") -- if width > maxwidth then - -- v.width = maxwidth + -- setfield(v,"width",maxwidth) -- end -- end -- end - box.width = maxwidth + setfield(box,"width",maxwidth) averagewidth = noflines > 0 and totalwidth/noflines or 0 + else -- e.g. empty math {$ $} or \hbox{} or ... +setfield(box,"width",0) end end end @@ -118,19 +138,19 @@ function commands.doreshapeframedbox(n) texsetdimen("global","framedaveragewidth",averagewidth) end -function commands.doanalyzeframedbox(n) - local box = texgetbox(n) +local function doanalyzeframedbox(n) + local box = getbox(n) local noflines = 0 local firstheight = nil local lastdepth = nil - if box.width ~= 0 then - local list = box.list + if getfield(box,"width") ~= 0 then + local list = getlist(box) if list then local function check(n) if not firstheight then - firstheight = n.height + firstheight = getfield(n,"height") end - lastdepth = n.depth + lastdepth = getfield(n,"depth") noflines = noflines + 1 end for h in traverse_id(hlist_code,list) do @@ -145,3 +165,46 @@ function commands.doanalyzeframedbox(n) texsetdimen("global","framedfirstheight",firstheight or 0) texsetdimen("global","framedlastdepth",lastdepth or 0) end + +implement { name = "doreshapeframedbox", actions = doreshapeframedbox, arguments = "integer" } +implement { name = "doanalyzeframedbox", actions = doanalyzeframedbox, arguments = "integer" } + +function nodes.maxboxwidth(box) + local boxwidth = getfield(box,"width") + if boxwidth == 0 then + return 0 + end + local list = getlist(box) + if not list then + return 0 + end + if getid(box) == hlist_code then + return boxwidth + end + local lastlinelength = 0 + local maxwidth = 0 + local function check(n,repack) + local l = getlist(n) + if l then + if repack then + local subtype = getsubtype(n) + if subtype == box_code or subtype == line_code then + lastlinelength = node_dimensions(l,getfield(n,"dir")) + else + lastlinelength = getfield(n,"width") + end + else + lastlinelength = getfield(n,"width") + end + if lastlinelength > maxwidth then + maxwidth = lastlinelength + end + end + end + for h in traverse_id(hlist_code,list) do -- no dir etc needed + check(h,true) + end + for v in traverse_id(vlist_code,list) do -- no dir etc needed + check(v,false) + end +end |