From c10ee230c83a5dfcda077652c2b16bc8f8a9d221 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Mon, 10 May 2010 21:19:21 +0300 Subject: Sync with ConTeXt stable (stable 2010.05.08) No functions removed, few new additions. --- lualibs-dimen.lua | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'lualibs-dimen.lua') diff --git a/lualibs-dimen.lua b/lualibs-dimen.lua index a8faa27..da5ab14 100644 --- a/lualibs-dimen.lua +++ b/lualibs-dimen.lua @@ -128,9 +128,9 @@ capture takes place.

local amount = (S("+-")^0 * R("09")^0 * P(".")^0 * R("09")^0) + Cc("0") local unit = R("az")^1 -local pattern = amount/tonumber * (unit^1/dimenfactors + Cc(1)) -- tonumber is new +local dimenpair = amount/tonumber * (unit^1/dimenfactors + Cc(1)) -- tonumber is new -lpeg.patterns.dimenpair = pattern +lpeg.patterns.dimenpair = dimenpair --[[ldx--

We use a metatable to intercept errors. When no key is found in @@ -149,7 +149,7 @@ function string:todimen() if type(self) == "number" then return self else - local value, unit = lpegmatch(pattern,self) + local value, unit = lpegmatch(dimenpair,self) return value/unit end end @@ -158,7 +158,7 @@ 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 pattern = amount * unit +local validdimen = amount * unit lpeg.patterns.validdimen = pattern @@ -166,12 +166,12 @@ lpeg.patterns.validdimen = pattern

This converter accepts calls like:

-string.todimen("10")) -string.todimen(".10")) -string.todimen("10.0")) -string.todimen("10.0pt")) -string.todimen("10pt")) -string.todimen("10.0pt")) +string.todimen("10") +string.todimen(".10") +string.todimen("10.0") +string.todimen("10.0pt") +string.todimen("10pt") +string.todimen("10.0pt")

And of course the often more efficient:

@@ -371,7 +371,7 @@ function dimen(a) if k then a = k else - local value, unit = lpegmatch(pattern,a) + local value, unit = lpegmatch(dimenpair,a) if type(unit) == "function" then k = value/unit() else @@ -395,7 +395,7 @@ function string:todimen() else local k = known[self] if not k then - local value, unit = lpegmatch(pattern,self) + local value, unit = lpegmatch(dimenpair,self) if value and unit then k = value/unit else @@ -408,6 +408,10 @@ function string:todimen() end end +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.

-- cgit v1.2.3