1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
-- filename : l-tex.lua
-- comment : split off from luat-lib
-- author : Hans Hagen, PRAGMA-ADE, Hasselt NL
-- copyright: PRAGMA ADE / ConTeXt Development Team
-- license : see context related readme files
if not versions then versions = { } end versions['l-tex'] = 1.001
if not number then number = { } end
local dimenfactors = {
["pt"] = 1/65536,
["in"] = ( 100/ 7227)/65536,
["cm"] = ( 254/ 7227)/65536,
["mm"] = ( 254/72270)/65536,
["sp"] = 1,
["bp"] = ( 7200/ 7227)/65536,
["pc"] = ( 1/ 12)/65536,
["dd"] = ( 1157/ 1238)/65536,
["cc"] = ( 1157/14856)/65536,
["nd"] = (20320/21681)/65536,
["nc"] = ( 5080/65043)/65536
}
local function todimen(n,unit,fmt)
if type(n) == 'string' then
return n
else
unit = unit or 'pt'
return string.format(fmt or "%.5g%s", n*dimenfactors[unit], unit)
end
end
number.todimen = todimen
number.dimenfactors = dimenfactors
function number.topoints (n) return todimen(n,"pt") end
function number.toinches (n) return todimen(n,"in") end
function number.tocentimeters (n) return todimen(n,"cm") end
function number.tomillimeters (n) return todimen(n,"mm") end
function number.toscaledpoints(n) return todimen(n,"sp") end
function number.toscaledpoints(n) return n .. "sp" end
function number.tobasepoints (n) return todimen(n,"bp") end
function number.topicas (n) return todimen(n "pc") end
function number.todidots (n) return todimen(n,"dd") end
function number.tociceros (n) return todimen(n,"cc") end
function number.tonewdidots (n) return todimen(n,"nd") end
function number.tonewciceros (n) return todimen(n,"nc") end
--~ for k,v in pairs{nil, "%.5f%s", "%.8g%s", "%.8f%s"} do
--~ print(todimen(65536))
--~ print(todimen( 256))
--~ print(todimen(65536,'pt',v))
--~ print(todimen( 256,'pt',v))
--~ end
-- todo: use different scratchdimen
-- todo: use parser if no tex.dimen
function string.todimen(str)
if type(str) == "number" then
return str
elseif str:find("^[%d%-%+%.]+$") then
return tonumber(str)
--~ elseif tex then
--~ tex.dimen[0] = str
--~ return tex.dimen[0] or 0
else
local n, u = str:match("([%d%-%+%.]+)(%a%a)")
if n and u then
return n/dimenfactors[u]
else
return 0
end
end
end
--~ print(string.todimen("10000"))
--~ print(string.todimen("10pt"))
--~ See mk.pdf for an explanation of the following code:
--~
--~ function test(n)
--~ lua.delay(function(...)
--~ tex.sprint(string.format("pi: %s %s %s\\par",...))
--~ end)
--~ lua.delay(function(...)
--~ tex.sprint(string.format("more pi: %s %s %s\\par",...))
--~ end)
--~ tex.sprint(string.format("\\setbox0=\\hbox{%s}",math.pi*n))
--~ lua.flush(tex.wd[0],tex.ht[0],tex.dp[0])
--~ end
if lua then do
local delayed = { }
function lua.delay(f)
delayed[#delayed+1] = f
end
function lua.flush_delayed(...)
local t = delayed
delayed = { }
for i=1, #t do
t[i](...)
end
end
function lua.flush(...)
tex.sprint("\\directlua0{lua.flush_delayed(" .. table.concat({...},',') .. ")}")
end
end end
|