meta-imp-kaktovik.mkxl /size: 6742 b    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=meta-imp-kaktovik,
3%D        version=2023.04.18,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=Kaktovik Numbers,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D Just a quick hack for Hraban (after a post on the mailing list).
15
16\startMPcalculation{simplefun}
17
18    path kaktovik_glyphs[] ;
19
20    def InitializeKaktovik =
21
22        save p, d, u ;
23
24        pair p[] ; numeric d ; d := .1 ; numeric u ; u := .15 ;
25
26      % d := getparameterdefault "mpsfont" "d" .10 ;
27      % u := getparameterdefault "mpsfont" "u" .15 ;
28
29        p[0] := (0,1) ;
30        p[1] := (1,0) ;
31        p[2] := (2,1 -  d) ;
32        p[3] := (3,0) ;
33        p[4] := (4,1 - 2d) ;
34
35        p[5] := (4,1 +  u) ;
36        p[6] := (0,1 + 2u) ;
37        p[7] := (4,1 + 4u) ;
38
39        kaktovik_glyphs[0] := (1,1)  { right } .. { left } (2,1/2) { left } .. { right } (3,1);
40
41        for i= 1 upto 4 :
42            kaktovik_glyphs[i] := p[0] for j=1 upto i : -- p[j] endfor ;
43        endfor ;
44
45        kaktovik_glyphs[ 5] := p[5] -- p[0] ;                for i= 6 upto  9 : kaktovik_glyphs[i] := p[5] -- kaktovik_glyphs[i-5] ; endfor ;
46        kaktovik_glyphs[10] := p[6] -- kaktovik_glyphs[ 5] ; for i=11 upto 14 : kaktovik_glyphs[i] := p[6] -- kaktovik_glyphs[i-5] ; endfor ;
47        kaktovik_glyphs[15] := p[7] -- kaktovik_glyphs[10] ; for i=16 upto 19 : kaktovik_glyphs[i] := p[7] -- kaktovik_glyphs[i-5] ; endfor ;
48
49    enddef ;
50
51    vardef Kaktovik(expr i) =
52        draw image (
53            draw kaktovik_glyphs[i]
54                xscaled (10/20 -1/20)
55                yscaled (25/20 -2/20)
56                withpen (pencircle xscaled 1/20 yscaled 5/20) rotated -(2*20)
57              % withpen (pencircle xscaled 1/40 yscaled 5/40) rotated -(2*20)
58              % withpen (pencircle xscaled 2.5/40 yscaled 5/40) rotated -(2*20)
59            ;
60        ) shifted (2/20,2/20)
61    enddef ;
62
63    lmt_registerglyphs [
64        name     = "kaktovik",
65        units    = 2,
66        usecolor = true,
67        width    = 2,
68        height   = 2,
69        depth    = 0,
70        preamble = "InitializeKaktovik"
71    ] ;
72
73    for i=0 upto 19 :
74        lmt_registerglyph [
75            category = "kaktovik",
76            unicode  = 119488 + i, % "0x1D2C0"
77            code     = "Kaktovik(" & decimal i & ")"
78        ] ;
79    endfor ;
80
81\stopMPcalculation
82
83\startluacode
84    local kaktovik      = moduledata.kaktovik or { }
85    moduledata.kaktovik = kaktovik
86
87    local tonumber  = tonumber
88    local load      = load
89    local reverse   = table.reverse
90    local utfchar   = utf.char
91    local gsub      = string.gsub
92    local concat    = table.concat
93    local utfvalues = string.utfvalues
94    local lpegmatch = lpeg.match
95
96    function kaktovik.tointeger(s)
97        local n = 0
98        for b in utfvalues(s) do
99            local k = b - 0x1D2C0
100            n = n * 20 + k
101        end
102        return n
103    end
104
105--    function kaktovik.tostring(n)
106--        local digits = { }
107--        local count  = 1
108--        while true do
109--            digits[count] = utfchar(0x1D2C0 + (n % 20))
110--            n = n // 20
111--            if n == 0 then
112--                break;
113--            end
114--            count = count + 1
115--        end
116--        return concat(reverse(digits))
117--    end
118
119    local f = string.formatters["%N"]
120
121    function kaktovik.tostring(n)
122        if n >= 0 and n <= 19 then
123            return utfchar(0x1D2C0 + n)
124        else
125            -- no need to optimize
126            local result = gsub(f(n),"(%d+)",function(s)
127                local digits = { }
128                local count  = 1
129                local n      = tonumber(s)
130                while true do
131                    digits[count] = utfchar(0x1D2C0 + (n % 20))
132                    n = n // 20
133                    if n == 0 then
134                        break;
135                    end
136                    count = count + 1
137                end
138                return concat(reverse(digits))
139            end)
140            return result
141        end
142    end
143
144    do
145
146        local k = { }
147        local n = 0
148        for i=0x1D2C0,0x1D2C0+19 do
149            k[utf.char(i)] = n
150            n = n + 1
151        end
152
153        local p = lpeg.Cs (
154            lpeg.Cc("return ")
155          * (
156                lpeg.utfchartabletopattern(k) / k
157              + lpeg.P(1)
158            )^0
159        )
160
161        local t = setmetatable({ },{ __index = math })
162
163        function kaktovik.calculate(old)
164            local new = lpegmatch(p,old)
165            if new then
166                new = load(new,nil,nil,t)
167                if type(new) == "function" then
168                    new = new()
169                    if new then
170                        return new
171                    end
172                end
173            end
174            return old
175        end
176
177    end
178
179    interfaces.implement {
180        name      = "kaktoviknumerals",
181        arguments = "integer",
182        actions   = { kaktovik.tostring, context }
183    }
184    interfaces.implement {
185        name      = "kaktovikcalculate",
186        public    = true,
187        arguments = "string",
188        actions   = { kaktovik.calculate, kaktovik.tostring, context }
189    }
190\stopluacode
191
192\unprotect
193
194\permanent\def\kaktoviknumerals#1{\clf_kaktoviknumerals\numexpr#1\relax}
195
196\defineconversion [kaktoviknumerals] [\kaktoviknumerals]
197\defineconversion [K]                [\kaktoviknumerals]
198
199\definefontfeature
200  [kaktovik]
201  [metapost=kaktovik]
202
203\protect
204
205\continueifinputfile{meta-imp-kaktovik.mkxl}
206
207\startbuffer
208
209\definefontfeature
210  [default]
211  [default]
212  [metapost=kaktovik]
213% [metapost={category=kaktovik,u=.25,d=.20,x=}]
214
215\setupbodyfont[dejavu]
216
217% \nopdfcompression
218
219\start
220    \showglyphs
221    KAKTOVIK
222    \dostepwiserecurse{0}{19}{1}{\kaktoviknumerals{#1}\space }
223\stop
224
225kaktovik \start
226    \red\glyphxscale 700
227    \dostepwiserecurse{0}{19}{1}{\kaktoviknumerals{#1}\space }
228\stop
229
230\startitemize[packed,K][color=orange,stopper=]
231    \startitem first  \stopitem
232    \startitem second \stopitem
233    \startitem third  \stopitem
234\stopitemize
235
236KAKTOVIK \start
237    \red  \kaktoviknumerals{2023} --
238    \green\kaktoviknumerals{4}    --
239    \blue \kaktoviknumerals{18}
240\stop
241
242KAKTOVIK
243
244𝋂 + 𝋂 = \kaktovikcalculate{𝋂 + 𝋂} = \kaktoviknumerals{4}
245
246\protected\def\ForWilli#1{#1 = \kaktovikcalculate{#1}}
247
248\ForWilli{(𝋂 + 𝋂) ^ (𝋂 + 𝋂)}
249
250\ForWilli{(sin ( 𝋂𝋓 ) )}
251
252\stopbuffer
253
254\starttext
255
256\startTEXpage[offset=1ts,width=3es]
257    \getbuffer
258\stopTEXpage
259
260\setuplayout[tight]
261
262\usemodule[scite]
263
264\typebuffer[option=TEX]
265
266\stoptext
267