diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkiv/mlib-lua.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/mkiv/mlib-lua.lua | 281 |
1 files changed, 269 insertions, 12 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkiv/mlib-lua.lua b/Master/texmf-dist/tex/context/base/mkiv/mlib-lua.lua index baf9346c472..9831efc20d7 100644 --- a/Master/texmf-dist/tex/context/base/mkiv/mlib-lua.lua +++ b/Master/texmf-dist/tex/context/base/mkiv/mlib-lua.lua @@ -11,7 +11,7 @@ if not modules then modules = { } end modules ['mlib-lua'] = { -- maybe we need mplib.model, but how with instances local type, tostring, select, loadstring = type, tostring, select, loadstring -local find, gsub = string.find, string.gsub +local find, match, gsub, gmatch = string.find, string.match, string.gsub, string.gmatch local formatters = string.formatters local concat = table.concat @@ -62,11 +62,12 @@ end local f_code = formatters["%s return mp._f_()"] local f_numeric = formatters["%.16f"] +local f_integer = formatters["%i"] local f_pair = formatters["(%.16f,%.16f)"] local f_triplet = formatters["(%.16f,%.16f,%.16f)"] local f_quadruple = formatters["(%.16f,%.16f,%.16f,%.16f)"] -function mp.print(...) +local function mpprint(...) for i=1,select("#",...) do local value = select(i,...) if value ~= nil then @@ -85,14 +86,36 @@ function mp.print(...) end end -function mp.boolean(n) +mp.print = mpprint + +-- We had this: +-- +-- table.setmetatablecall(mp,function(t,k) mpprint(k) end) +-- +-- but the next one is more interesting because we cannot use calls like: +-- +-- lua.mp.somedefdname("foo") +-- +-- which is due to expansion of somedefdname during suffix creation. So: +-- +-- lua.mp("somedefdname","foo") + +table.setmetatablecall(mp,function(t,k,...) return t[k](...) end) + +function mp.boolean(b) + n = n + 1 + buffer[n] = b and "true" or "false" +end + +function mp.numeric(f) n = n + 1 - buffer[n] = n and "true" or "false" + buffer[n] = f and f_numeric(f) or "0" end -function mp.numeric(n) +function mp.integer(i) n = n + 1 - buffer[n] = n and f_numeric(n) or "0" + -- buffer[n] = i and f_integer(i) or "0" + buffer[n] = i or "0" end function mp.pair(x,y) @@ -152,6 +175,12 @@ function mp.size(t) buffer[n] = type(t) == "table" and f_numeric(#t) or "0" end +local mpnamedcolor = attributes.colors.mpnamedcolor + +mp.NamedColor = function(str) + mpprint(mpnamedcolor(str)) +end + -- experiment: names can change local datasets = { } @@ -173,7 +202,7 @@ end local replacer = lpeg.replacer("@","%%") -function mp.format(fmt,...) +function mp.fprint(fmt,...) n = n + 1 if not find(fmt,"%%") then fmt = lpegmatch(replacer,fmt) @@ -181,7 +210,7 @@ function mp.format(fmt,...) buffer[n] = formatters[fmt](...) end -function mp.quoted(fmt,s,...) +local function mpquoted(fmt,s,...) n = n + 1 if s then if not find(fmt,"%%") then @@ -195,6 +224,8 @@ function mp.quoted(fmt,s,...) end end +mp.quoted = mpquoted + function mp.n(t) return type(t) == "table" and #t or 0 end @@ -352,9 +383,10 @@ end -- texts: -local factor = 65536*(7227/7200) -local textexts = nil -local mptriplet = mp.triplet +local factor = 65536*(7227/7200) +local textexts = nil +local mptriplet = mp.triplet +local nbdimensions = nodes.boxes.dimensions function mp.tt_initialize(tt) textexts = tt @@ -374,7 +406,7 @@ end -- end function mp.tt_dimensions(n) - local box = textexts[n] + local box = textexts and textexts[n] if box then -- could be made faster with nuts but not critical mptriplet(box.width/factor,box.height/factor,box.depth/factor) @@ -383,6 +415,11 @@ function mp.tt_dimensions(n) end end +function mp.tb_dimensions(category,name) + local w, h, d = nbdimensions(category,name) + mptriplet(w/factor,h/factor,d/factor) +end + function mp.report(a,b) if b then report_message("%s : %s",a,b) @@ -390,3 +427,223 @@ function mp.report(a,b) report_message("%s : %s","message",a) end end + +-- + +local hashes = { } + +function mp.newhash() + for i=1,#hashes+1 do + if not hashes[i] then + hashes[i] = { } + mpprint(i) + return + end + end +end + +function mp.disposehash(n) + hashes[n] = nil +end + +function mp.inhash(n,key) + local h = hashes[n] + mpprint(h and h[key] and true or false) +end + +function mp.tohash(n,key) + local h = hashes[n] + if h then + h[key] = true + end +end + +local modes = tex.modes +local systemmodes = tex.systemmodes + +function mp.mode(s) + mpprint(modes[s] and true or false) +end + +function mp.systemmode(s) + mpprint(systemmodes[s] and true or false) +end + +-- for alan's nodes: + +function mp.isarray(str) + mpprint(find(str,"%d") and true or false) +end + +function mp.prefix(str) + mpquoted(match(str,"^(.-)[%d%[]") or str) +end + +function mp.dimensions(str) + local n = 0 + for s in gmatch(str,"%[?%-?%d+%]?") do --todo: lpeg + n = n + 1 + end + mpprint(n) +end + +-- faster and okay as we don't have many variables but probably only +-- basename makes sense and even then it's not called that often + +-- local hash = table.setmetatableindex(function(t,k) +-- local v = find(k,"%d") and true or false +-- t[k] = v +-- return v +-- end) +-- +-- function mp.isarray(str) +-- mpprint(hash[str]) +-- end +-- +-- local hash = table.setmetatableindex(function(t,k) +-- local v = '"' .. (match(k,"^(.-)%d") or k) .. '"' +-- t[k] = v +-- return v +-- end) +-- +-- function mp.prefix(str) +-- mpprint(hash[str]) +-- end + +local getdimen = tex.getdimen +local getcount = tex.getcount +local gettoks = tex.gettoks +local setdimen = tex.setdimen +local setcount = tex.setcount +local settoks = tex.settoks + +local mpprint = mp.print +local mpquoted = mp.quoted + +local factor = number.dimenfactors.bp + +-- more helpers + +function mp.getdimen(k) mpprint (getdimen(k)*factor) end +function mp.getcount(k) mpprint (getcount(k)) end +function mp.gettoks (k) mpquoted(gettoks (k)) end +function mp.setdimen(k,v) setdimen(k,v/factor) end +function mp.setcount(k,v) setcount(k,v) end +function mp.settoks (k,v) settoks (k,v) end + +-- def foo = lua.mp.foo ... enddef ; % loops due to foo in suffix + +mp._get_dimen_ = mp.getdimen +mp._get_count_ = mp.getcount +mp._get_toks_ = mp.gettoks +mp._set_dimen_ = mp.setdimen +mp._set_count_ = mp.setcount +mp._set_toks_ = mp.settoks + +-- position fun + +do + + local mprint = mp.print + local fprint = mp.fprint + local qprint = mp.quoted + local getwhd = job.positions.whd + local getxy = job.positions.xy + local getposition = job.positions.position + local getpage = job.positions.page + local getregion = job.positions.region + local getmacro = tokens.getters.macro + + function mp.positionpath(name) + local w, h, d = getwhd(name) + if w then + fprint("((%p,%p)--(%p,%p)--(%p,%p)--(%p,%p)--cycle)",0,-d,w,-d,w,h,0,h) + else + mprint("(origin--cycle)") + end + end + + function mp.positioncurve(name) + local w, h, d = getwhd(name) + if w then + fprint("((%p,%p)..(%p,%p)..(%p,%p)..(%p,%p)..cycle)",0,-d,w,-d,w,h,0,h) + else + mprint("(origin--cycle)") + end + end + + function mp.positionbox(name) + local p, x, y, w, h, d = getposition(name) + if p then + fprint("((%p,%p)--(%p,%p)--(%p,%p)--(%p,%p)--cycle)",x,y-d,x+w,y-d,x+w,y+h,x,y+h) + else + mprint("(%p,%p)",x,y) + end + end + + function mp.positionxy(name) + local x, y = getxy(name) + if x then + fprint("(%p,%p)",x,y) + else + mprint("origin") + end + end + + function mp.positionpage(name) + local p = getpage(name) + if p then + fprint("%p",p) + else + mprint("0") + end + end + + function mp.positionregion(name) + local r = getregion(name) + if r then + qprint(r) + else + qprint("unknown") + end + end + + function mp.positionwhd(name) + local w, h, d = getwhd(name) + if w then + fprint("(%p,%p,%p)",w,h,d) + else + mprint("(0,0,0)") + end + end + + function mp.positionpxy(name) + local p, x, y = getposition(name) + if p then + fprint("(%p,%p,%p)",p,x,y) + else + mprint("(0,0,0)") + end + end + + function mp.positionanchor() + qprint(getmacro("MPanchorid")) + end + +end + +do + + local mprint = mp.print + local qprint = mp.quoted + local getmacro = tokens.getters.macro + + function mp.texvar(name) + mprint(getmacro(metapost.namespace .. name)) + end + + function mp.texstr(name) + qprint(getmacro(metapost.namespace .. name)) + end + +end |