summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/meta-imp-kaktovik.mkxl
blob: 40e9f826c337079171d3452deecec8c38999ddc7 (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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
%D \module
%D   [       file=meta-imp-kaktovik,
%D        version=2023.04.18,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Kaktovik Numbers,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Just a quick hack for Hraban (after a post on the mailing list).

\startMPcalculation{simplefun}

    path kaktovik_glyphs[] ;

    def InitializeKaktovik =

        save p, d, u ;

        pair p[] ; numeric d ; d := .1 ; numeric u ; u := .15 ;

      % d := getparameterdefault "mpsfont" "d" .10 ;
      % u := getparameterdefault "mpsfont" "u" .15 ;

        p[0] := (0,1) ;
        p[1] := (1,0) ;
        p[2] := (2,1 -  d) ;
        p[3] := (3,0) ;
        p[4] := (4,1 - 2d) ;

        p[5] := (4,1 +  u) ;
        p[6] := (0,1 + 2u) ;
        p[7] := (4,1 + 4u) ;

        kaktovik_glyphs[0] := (1,1)  { right } .. { left } (2,1/2) { left } .. { right } (3,1);

        for i= 1 upto 4 :
            kaktovik_glyphs[i] := p[0] for j=1 upto i : -- p[j] endfor ;
        endfor ;

        kaktovik_glyphs[ 5] := p[5] -- p[0] ;                for i= 6 upto  9 : kaktovik_glyphs[i] := p[5] -- kaktovik_glyphs[i-5] ; endfor ;
        kaktovik_glyphs[10] := p[6] -- kaktovik_glyphs[ 5] ; for i=11 upto 14 : kaktovik_glyphs[i] := p[6] -- kaktovik_glyphs[i-5] ; endfor ;
        kaktovik_glyphs[15] := p[7] -- kaktovik_glyphs[10] ; for i=16 upto 19 : kaktovik_glyphs[i] := p[7] -- kaktovik_glyphs[i-5] ; endfor ;

    enddef ;

    vardef Kaktovik(expr i) =
        draw image (
            draw kaktovik_glyphs[i]
                xscaled (10/20 -1/20)
                yscaled (25/20 -2/20)
                withpen (pencircle xscaled 1/20 yscaled 5/20) rotated -(2*20)
              % withpen (pencircle xscaled 1/40 yscaled 5/40) rotated -(2*20)
              % withpen (pencircle xscaled 2.5/40 yscaled 5/40) rotated -(2*20)
            ;
        ) shifted (2/20,2/20)
    enddef ;

    lmt_registerglyphs [
        name     = "kaktovik",
        units    = 2,
        usecolor = true,
        width    = 2,
        height   = 2,
        depth    = 0,
        preamble = "InitializeKaktovik"
    ] ;

    for i=0 upto 19 :
        lmt_registerglyph [
            category = "kaktovik",
            unicode  = 119488 + i, % "0x1D2C0"
            code     = "Kaktovik(" & decimal i & ")"
        ] ;
    endfor ;

\stopMPcalculation

\startluacode
    local kaktovik      = moduledata.kaktovik or { }
    moduledata.kaktovik = kaktovik

    local tonumber  = tonumber
    local load      = load
    local reverse   = table.reverse
    local utfchar   = utf.char
    local gsub      = string.gsub
    local concat    = table.concat
    local utfvalues = string.utfvalues
    local lpegmatch = lpeg.match

    function kaktovik.tointeger(s)
        local n = 0
        for b in utfvalues(s) do
            local k = b - 0x1D2C0
            n = n * 20 + k
        end
        return n
    end

--    function kaktovik.tostring(n)
--        local digits = { }
--        local count  = 1
--        while true do
--            digits[count] = utfchar(0x1D2C0 + (n % 20))
--            n = n // 20
--            if n == 0 then
--                break;
--            end
--            count = count + 1
--        end
--        return concat(reverse(digits))
--    end

    local f = string.formatters["%N"]

    function kaktovik.tostring(n)
        if n >= 0 and n <= 19 then
            return utfchar(0x1D2C0 + n)
        else
            -- no need to optimize
            local result = gsub(f(n),"(%d+)",function(s)
                local digits = { }
                local count  = 1
                local n      = tonumber(s)
                while true do
                    digits[count] = utfchar(0x1D2C0 + (n % 20))
                    n = n // 20
                    if n == 0 then
                        break;
                    end
                    count = count + 1
                end
                return concat(reverse(digits))
            end)
            return result
        end
    end

    do

        local k = { }
        local n = 0
        for i=0x1D2C0,0x1D2C0+19 do
            k[utf.char(i)] = n
            n = n + 1
        end

        local p = lpeg.Cs (
            lpeg.Cc("return ")
          * (
                lpeg.utfchartabletopattern(k) / k
              + lpeg.P(1)
            )^0
        )

        local t = setmetatable({ },{ __index = math })

        function kaktovik.calculate(old)
            local new = lpegmatch(p,old)
            if new then
                new = load(new,nil,nil,t)
                if type(new) == "function" then
                    new = new()
                    if new then
                        return new
                    end
                end
            end
            return old
        end

    end

    interfaces.implement {
        name      = "kaktoviknumerals",
        arguments = "integer",
        actions   = { kaktovik.tostring, context }
    }
    interfaces.implement {
        name      = "kaktovikcalculate",
        public    = true,
        arguments = "string",
        actions   = { kaktovik.calculate, kaktovik.tostring, context }
    }
\stopluacode

\unprotect

\permanent\def\kaktoviknumerals#1{\clf_kaktoviknumerals\numexpr#1\relax}

\defineconversion [kaktoviknumerals] [\kaktoviknumerals]
\defineconversion [K]                [\kaktoviknumerals]

\definefontfeature
  [kaktovik]
  [metapost=kaktovik]

\protect

\continueifinputfile{meta-imp-kaktovik.mkxl}

\definefontfeature
  [default]
  [default]
  [metapost=kaktovik]
% [metapost={category=kaktovik,u=.25,d=.20,x=}]

\setupbodyfont[dejavu]

% \nopdfcompression

\startTEXpage[offset=1ts,width=3es]

    \start
    \showglyphs
        KAKTOVIK
        \dostepwiserecurse{0}{19}{1}{\kaktoviknumerals{#1}\space }
    \stop

    kaktovik \start
        \red\glyphxscale 700
        \dostepwiserecurse{0}{19}{1}{\kaktoviknumerals{#1}\space }
    \stop

    \startitemize[packed,K][color=orange,stopper=]
        \startitem first  \stopitem
        \startitem second \stopitem
        \startitem third  \stopitem
    \stopitemize

    KAKTOVIK \start
        \red  \kaktoviknumerals{2023} --
        \green\kaktoviknumerals{4}    --
        \blue \kaktoviknumerals{18}
    \stop

    KAKTOVIK

    𝋂 + 𝋂 = \kaktovikcalculate{𝋂 + 𝋂} = \kaktoviknumerals{4}

    \protected\def\ForWilli#1{#1 = \kaktovikcalculate{#1}}

    \ForWilli{(𝋂 + 𝋂) ^ (𝋂 + 𝋂)}

    \ForWilli{(sin ( 𝋂𝋓 ) )}
\stopTEXpage