if not modules then modules = { } end modules ['l-dimen'] = { 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]]-- -- was: local function todimen(n,unit,fmt) if type(n) == 'string' then return n else unit = unit or 'pt' return format(fmt or "%s%s",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]]-- local mt = { } setmetatable(dimenfactors,mt) mt.__index = function(t,s) -- error("wrong dimension: " .. (s or "?")) -- better a message return false end function string:todimen() if type(self) == "number" then return self else local value, unit = lpegmatch(pattern,self) print(value,unit) return value/unit end end --[[ldx--This converter accepts calls like:
And of course the often more efficient:
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.
--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 = a:todimen() elseif ta == "table" then a = a[1] end if tb == "string" then b = b:todimen() 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 = a:todimen() elseif ta == "table" then a = a[1] end if tb == "string" then b = b:todimen() 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 = a:todimen() elseif ta == "table" then a = a[1] end if tb == "string" then b = b:todimen() 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 = a:todimen() elseif ta == "table" then a = a[1] end if tb == "string" then b = b:todimen() 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 = a:todimen() 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
In order to set the defaults we call this function now. At some point the macro package needs to make sure the function is called again.
--ldx]]-- dimensions.texify() --[[ldx--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(pattern,a) if type(unit) == "function" then k = value/unit() else k = value/unit 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() if type(self) == "number" then return self else local k = known[self] if not k then local value, unit = lpegmatch(pattern,self) if value and unit then k = value/unit else k = 0 end -- print(self,value,unit) known[self] = k end return k end 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) -- will be cleaned up once luatex 0.30 is out local hsize = tex.hsize if type(hsize) == "string" then hsize = hsize:todimen() end return (n/100) * hsize end number["%"] = number.percent