summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/syst-con.lua
blob: f0ea8546a2e0c0ec790841bb428b8f684fdcdaeb (plain)
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
if not modules then modules = { } end modules ['syst-con'] = {
    version   = 1.001,
    comment   = "companion to syst-con.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tonumber = tonumber
local math = math
local utfchar = utf.char
local gsub = string.gsub

converters       = converters or { }
local converters = converters

local context    = context
local commands   = commands
local implement  = interfaces.implement

local formatters = string.formatters

-- For raw 8 bit characters, the offset is 0x110000 (bottom of plane 18) at the top
-- of LuaTeX's char range but outside the unicode range. This is no longer the case
-- in LuaMetaTeX.

function converters.hexstringtonumber(n) tonumber(n,16) end
function converters.octstringtonumber(n) tonumber(n, 8) end

function converters.rawcharacter     (n) utfchar(0x110000+n) end

local f_lchexnumber  = formatters["%x"]
local f_uchexnumber  = formatters["%X"]
local f_lchexnumbers = formatters["%02x"]
local f_uchexnumbers = formatters["%02X"]
local f_octnumber    = formatters["%03o"]
local   nicenumber   = formatters["%0.6F"] -- or N

local lchexnumber  = function(n) if n < 0 then n = 0x100000000 + n end return f_lchexnumber (n) end
local uchexnumber  = function(n) if n < 0 then n = 0x100000000 + n end return f_uchexnumber (n) end
local lchexnumbers = function(n) if n < 0 then n = 0x100000000 + n end return f_lchexnumbers(n) end
local uchexnumbers = function(n) if n < 0 then n = 0x100000000 + n end return f_uchexnumbers(n) end
local octnumber    = function(n) if n < 0 then n = 0x100000000 + n end return f_octnumber   (n) end

converters.lchexnumber  = lchexnumber
converters.uchexnumber  = uchexnumber
converters.lchexnumbers = lchexnumbers
converters.uchexnumbers = uchexnumbers
converters.octnumber    = octnumber
converters.nicenumber   = nicenumber

implement { name = "hexstringtonumber", actions = { tonumber, context }, arguments = { "integer", 16 } }
implement { name = "octstringtonumber", actions = { tonumber, context }, arguments = { "integer",  8 } }

implement { name = "rawcharacter", actions = function(n) context(utfchar(0x110000+n)) end, arguments = "integer" }

implement { name = "lchexnumber",  actions = { lchexnumber,  context }, arguments = "integer" }
implement { name = "uchexnumber",  actions = { uchexnumber,  context }, arguments = "integer" }
implement { name = "lchexnumbers", actions = { lchexnumbers, context }, arguments = "integer" }
implement { name = "uchexnumbers", actions = { uchexnumbers, context }, arguments = "integer" }
implement { name = "octnumber",    actions = { octnumber,    context }, arguments = "integer" }

implement { name = "sin",  actions = { math.sin,  nicenumber, context }, arguments = "number" }
implement { name = "cos",  actions = { math.cos,  nicenumber, context }, arguments = "number" }
implement { name = "tan",  actions = { math.tan,  nicenumber, context }, arguments = "number" }

implement { name = "sind", actions = { math.sind, nicenumber, context }, arguments = "number" }
implement { name = "cosd", actions = { math.cosd, nicenumber, context }, arguments = "number" }
implement { name = "tand", actions = { math.tand, nicenumber, context }, arguments = "number" }

-- only as commands

function commands.format(fmt,...) context((gsub(fmt,"@","%%")),...) end

implement {
    name      = "formatone", -- used as such so no name change here
    public    = true,
    protected = true,
    arguments = "2 strings",
    actions   = function(f,s) context((gsub(f,"@","%%")),s) end,
}