if not modules then modules = { } end modules ['util-dim'] = { version = 1.001, comment = "support for dimensions", author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } --[[ldx--
Internally
A conversion function that takes a number, unit (string) and optional format (string) is implemented using this table.
--ldx]]-- local function numbertodimen(n,unit,fmt) if type(n) == 'string' then return n else unit = unit or 'pt' if not fmt then fmt = "%s%s" elseif fmt == true then fmt = "%0.5f%s" end return format(fmt,n*dimenfactors[unit],unit) -- if fmt then -- return format(fmt,n*dimenfactors[unit],unit) -- else -- return match(format("%.20f",n*dimenfactors[unit]),"(.-0?)0*$") .. unit -- end end end --[[ldx--We collect a bunch of converters in the
More interesting it to implement a (sort of) dimen datatype, one
that permits calculations too. First we define a function that
converts a string to scaledpoints. We use
We use a metatable to intercept errors. When no key is found in the table with factors, the metatable will be consulted for an alternative index function.
--ldx]]-- setmetatableindex(dimenfactors, function(t,s) -- error("wrong dimension: " .. (s or "?")) -- better a message return false end) --[[ldx--We redefine the following function later on, so we comment it here (which saves us bytecodes.
--ldx]]-- -- function string.todimen(str) -- if type(str) == "number" then -- return str -- else -- local value, unit = lpegmatch(dimenpair,str) -- return value/unit -- end -- end -- -- local stringtodimen = string.todimen local stringtodimen -- assigned later (commenting saves bytecode) local amount = S("+-")^0 * R("09")^0 * S(".,")^0 * R("09")^0 local unit = P("pt") + P("cm") + P("mm") + P("sp") + P("bp") + P("in") + P("pc") + P("dd") + P("cc") + P("nd") + P("nc") local validdimen = amount * unit lpeg.patterns.validdimen = validdimen --[[ldx--This converter accepts calls like:
With this in place, we can now implement a proper datatype for dimensions, one that permits us to do this:
We create a local metatable for this new type:
--ldx]]-- local dimensions = { } --[[ldx--The main (and globally) visible representation of a dimen is defined next: it is a one-element table. The unit that is returned from the match is normally a number (one of the previously defined factors) but we also accept functions. Later we will see why. This function is redefined later.
--ldx]]-- -- function dimen(a) -- if a then -- local ta= type(a) -- if ta == "string" then -- local value, unit = lpegmatch(pattern,a) -- if type(unit) == "function" then -- k = value/unit() -- else -- k = value/unit -- end -- a = k -- elseif ta == "table" then -- a = a[1] -- end -- return setmetatable({ a }, dimensions) -- else -- return setmetatable({ 0 }, dimensions) -- end -- end --[[ldx--This function return a small hash with a metatable attached. It is through this metatable that we can do the calculations. We could have shared some of the code but for reasons of speed we don't.
--ldx]]-- function dimensions.__add(a, b) local ta, tb = type(a), type(b) if ta == "string" then a = stringtodimen(a) elseif ta == "table" then a = a[1] end if tb == "string" then b = stringtodimen(b) elseif tb == "table" then b = b[1] end return setmetatable({ a + b }, dimensions) end function dimensions.__sub(a, b) local ta, tb = type(a), type(b) if ta == "string" then a = stringtodimen(a) elseif ta == "table" then a = a[1] end if tb == "string" then b = stringtodimen(b) elseif tb == "table" then b = b[1] end return setmetatable({ a - b }, dimensions) end function dimensions.__mul(a, b) local ta, tb = type(a), type(b) if ta == "string" then a = stringtodimen(a) elseif ta == "table" then a = a[1] end if tb == "string" then b = stringtodimen(b) elseif tb == "table" then b = b[1] end return setmetatable({ a * b }, dimensions) end function dimensions.__div(a, b) local ta, tb = type(a), type(b) if ta == "string" then a = stringtodimen(a) elseif ta == "table" then a = a[1] end if tb == "string" then b = stringtodimen(b) elseif tb == "table" then b = b[1] end return setmetatable({ a / b }, dimensions) end function dimensions.__unm(a) local ta = type(a) if ta == "string" then a = stringtodimen(a) elseif ta == "table" then a = a[1] end return setmetatable({ - a }, dimensions) end --[[ldx--It makes no sense to implement the power and modulo function but the next two do make sense because they permits is code like:
We also need to provide a function for conversion to string (so that
we can print dimensions). We print them as points, just like
Since it does not take much code, we also provide a way to access a few accessors
In the converter from string to dimension we support functions as
factors. This is because in
The previous code is rather efficient (also thanks to
When we cache converted strings this becomes 16.3 seconds. In order not to waste too much memory on it, we tag the values of the cache as being week which mean that the garbage collector will collect them in a next sweep. This means that in most cases the speed up is mostly affecting the current couple of calculations and as such the speed penalty is small.
We redefine two previous defined functions that can benefit from this:
--ldx]]-- local known = { } setmetatable(known, { __mode = "v" }) function dimen(a) if a then local ta= type(a) if ta == "string" then local k = known[a] if k then a = k else local value, unit = lpegmatch(dimenpair,a) if value and unit then k = value/unit -- to be considered: round else k = 0 end known[a] = k a = k end elseif ta == "table" then a = a[1] end return setmetatable({ a }, dimensions) else return setmetatable({ 0 }, dimensions) end end function string.todimen(str) -- maybe use tex.sp when available if type(str) == "number" then return str else local k = known[str] if not k then local value, unit = lpegmatch(dimenpair,str) if value and unit then k = value/unit -- to be considered: round else k = 0 end -- print(str,value,unit) known[str] = k end return k end end -- local known = { } -- -- function string.todimen(str) -- maybe use tex.sp -- local k = known[str] -- if not k then -- k = tex.sp(str) -- known[str] = k -- end -- return k -- end stringtodimen = string.todimen -- local variable defined earlier function number.toscaled(d) return format("%0.5f",d/2^16) end --[[ldx--In a similar fashion we can define a glue datatype. In that case we probably use a hash instead of a one-element table.
--ldx]]-- --[[ldx--Goodie:s
--ldx]]-- function number.percent(n,d) -- will be cleaned up once luatex 0.30 is out d = d or texget("hsize") if type(d) == "string" then d = stringtodimen(d) end return (n/100) * d end number["%"] = number.percent