summaryrefslogtreecommitdiff
path: root/tex/context/base/x-mmp.mkiv
blob: 57061fda575c12c812b3b34735e014577881a0b6 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
%D \module
%D   [       file=x-mmp,
%D        version=2007.09.04,
%D          title=\CONTEXT\ XML Modules,
%D       subtitle=Presentation MathML Renderer,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% -- ignored: malignmark
% -- luacode will be moved to x-mmp.lua

\startluacode
    do

        lxml.mml = lxml.mml or { }

        local replacements = {
            [" "] = "&textspace;",
            ["."] = "&textperiod;",
            [","] = "&textcomma;",
        }

        local reppattern = "([ %.%,])"

        function lxml.mml.prepare_number(id,pattern)
            local str = xml.content(lxml.id(id),pattern) or ""
            str = str:gsub("^%s*(.-)%s*$","%1")
            str = str:gsub(reppattern,replacements)
            tex.sprint(str)
        end

        function lxml.mml.connect(id,pattern,separators)
            local n = xml.count(id,pattern)
            if n == 0 then
                -- skip
            elseif n == 1 then
                lxml.all(id,pattern)
            else
                local t = { }
                for s in unicode.utf8.gmatch(separators,"([^%s])") do
                    t[#t+1] = s
                end
                for i=1,n do
                    if i > 1 then
                        tex.sprint(tex.ctxcatcodes,"{")
                        tex.sprint(t[i] or t[#t] or "")
                        tex.sprint(tex.ctxcatcodes,"}")
                    end
                    lxml.idx(id,pattern,i) -- kind of slow, some day ...
                end
            end
        end

        local function flush(e,tag,toggle)
         -- tex.sprint(tex.ctxcatcodes,(toggle and "^{") or "_{")
            if toggle then
                tex.sprint(tex.ctxcatcodes,"^{")
            else
                tex.sprint(tex.ctxcatcodes,"_{")
            end
            if tag == "none" then
                tex.sprint(tex.ctxcatcodes,"{}")
            else
                xml.sprint(e.dt)
            end
            if not toggle then
                tex.sprint(tex.ctxcatcodes,"}")
            else
                tex.sprint(tex.ctxcatcodes,"}{}")
            end
            return not toggle
        end

        function lxml.mml.multiscripts(id)
            local done, toggle = false, false
            id = lxml.id(id)
            -- for i=1,#id.dt do local e = id.dt[i] if type(e) == table then ...
            for r, d, k in xml.elements(id,"/*") do
                local e = d[k]
                local tag = xml.tag(e)
                if tag == "mprescripts" then
                    tex.sprint(tex.ctxcatcodes,"{}")
                    done = true
                elseif done then
                    toggle = flush(e,tag,toggle)
                end
            end
            local done, toggle = false, false
            for r, d, k in xml.elements(id,"/*") do
                local e = d[k]
                local tag = xml.tag(e)
                if tag == "mprescripts" then
                    break
                elseif done then
                    toggle = flush(e,tag,toggle)
                else
                    xml.sprint(e.dt)
                    done = true
                end
            end
        end

    end
\stopluacode

\unprotect

% \xmlstrip  {\xmldocument} {mml:mrow|mml:mi|mml:mn|mml:mo|mml:mroot|mml:msqrt|mml:mfenced|mml:mfrac|mml:enclose}

% \startxmlsetups xml:mmp:process
%     \xmlgrab {\xmldocument} {mml:mrow|mml:mroot|mml:msqrt|mml:mi|mml:mo|mml:mn|mml:mfenced|mml:mfrac|mml:mspace|mml:enclose} {*}
%     \xmlgrab {\xmldocument} {mml:mover|mml:munder|mml:munderover|mml:msub|mml:msup|mml:msubsup|mml:mmultiscripts} {*}
%     \xmlgrab {\xmldocument} {mml:mstyle|mml:mtext|mml:merror|mml:mphantom|mml:mpadded|mml:ms|mp:mglyph} {*}
%     \xmlgrab {\xmldocument} {mml:mlabeledtr|mml:mtr|mml:mtd|mml:mtable} {*}
% \stopxmlsetups

% \startxmlsetups xml:mmp:process
%     \xmlgrab {\xmldocument} {mml:*} {*}
% \stopxmlsetups

% \xmlregistersetup{xml:mmp:process}

% helpers

% \let\myspecialnormalvert\myspecialstretchvert
% \setupMMLappearance[scripts][\c!alternative=\v!a] {} rond base

\mapXMLvalue{mmp}{normal}     {\tf} \mapXMLvalue{mmp}{double-stuck} {\bf}
\mapXMLvalue{mmp}{bolditalic} {\bi} \mapXMLvalue{mmp}{bold-italic}  {\bi}
\mapXMLvalue{mmp}{boldslanted}{\bs} \mapXMLvalue{mmp}{bold-slanted} {\bs}
\mapXMLvalue{mmp}{boldnormal} {\bf} \mapXMLvalue{mmp}{bold}         {\bf}
\mapXMLvalue{mmp}{slanted}    {\sl} \mapXMLvalue{mmp}{normalslanted}{\sl}
\mapXMLvalue{mmp}{italic}     {\it} \mapXMLvalue{mmp}{normalitalic} {\it}
\mapXMLvalue{mmp}{fraktur}    {\bf} \mapXMLvalue{mmp}{bold-fraktur} {\bf}
\mapXMLvalue{mmp}{script}     {\tf} \mapXMLvalue{mmp}{bold-script}  {\bf}

\def\setMMLptextstyle#1%
  {\XMLval{mmp}{\xmlatt{#1}{fontweight}\xmlatt{#1}{fontstyle}}{}}

\def\setMMLpmathstyle#1%
  {\XMLval{mmp}{\xmlatt{#1}{mathvariant}}{}}

\def\doMMPpcolor#1%
  {\doifelsenothing{\xmlatt{#1}{color}} \firstofoneargument {\color[\xmlatt{#1}{color}]}}

\def\doMMPpbackground#1%
  {\doifelsenothing{\xmlatt{#1}{background}}
     {\firstofoneargument}
     {\inframed
        [\c!frame=\v!off,
         \c!background=\v!color,
         \c!backgroundcolor=\xmlatt{#1}{background}]}}

\def\doMMPpsometext#1#2%
  {\doMMPpbackground{#1}{\doMMPpcolor{#1}{\setMMLptextstyle{#1}\ignorespaces#2\removeunwantedspaces}}}

\def\doMMLfiller#1% bugged
   {\pushmacro\doMMLfiller
    \let\doMMLfiller\gobbleoneargument
    \gdef\dodoMMLfiller{\disablefiller\mathematics{#1}}%
    \hbox
      {\def\normalorfiller##1##2%
         {\gdef\dodoMMLfiller{\enablefiller#1}%
          \let\normalorfiller\gobbletwoarguments}%
       $#1$}%
    \popmacro\doMMLfiller
    \dodoMMLfiller}

% setups

\defineXMLentity[textspace]  {\enspace}
\defineXMLentity[textcomma]  {{,}}
\defineXMLentity[textperiod] {{.}}

\defineXMLentity[_]          {{\_}}
\defineXMLentity[^]          {{\^}}

\startsetups mml:semantics % todo: width=ex/ex/pt
    \xmlflush{#1}
\stopsetups

\startxmlsetups mml:mi % todo: mathvariant mathsize mathcolor mathbackground
    \edef\MMPidentifier{\xmlstripped{#1}{*}}
    \doifXMLentityelse{\detokenize\expandafter{\MMPidentifier}} {
        \getXMLentity\MMPidentifier
    } {
        \MMPidentifier
    }
\stopxmlsetups

\startxmlsetups mml:mn % todo: mathvariant mathsize mathcolor mathbackground
    % function and style
    \begingroup
        \rm  \ctxlua{lxml.mml.prepare_number("#1","*")}
    \endgroup
\stopxmlsetups

\startxmlsetups mml:mo
    \edef\MMPoperator{\xmlstripped{#1}{*}}
    \doifXMLentityelse{\detokenize\expandafter{\MMPoperator}} {
        \getXMLentity\MMPoperator
    } {
        \MMPoperator
    }
\stopxmlsetups

\startxmlsetups mml:mrow
    \begingroup
        \ifcase\xmlcount{#1}{/mml:mo}\relax
            \xmlflush{#1}
        \else % no \let
            \def\MMLleft {\left }
            \def\MMLright{\right}
            \enabledelimiter
            \checkdelimiters{\xmlall{#1}{/mml:mo}}
            \fakeleftdelimiter
            \xmlflush{#1}
            \fakerightdelimiter
            \disabledelimiter
        \fi
    \endgroup
\stopxmlsetups

\startxmlsetups mml:mstyle
    \begingroup
        \setMMLpmathstyle{#1}
        \xmlflush{#1}
    \endgroup
\stopxmlsetups

\startxmlsetups mml:ms
    \xmlattdef{#1}{lquot}{"}
        \doMMPpsometext{#1}{\xmlflush{#1}}
    \xmlattdef{#1}{rquot}{"}
\stopxmlsetups

\startxmlsetups mml:mtext
    \hbox \bgroup \tf % else encoding problems, we can make mr an option
        \doMMPpsometext{#1}{\xmlflush{#1}}
    \egroup
\stopxmlsetups

\startxmlsetups mml:merror
    \hbox{$\displaystyle\xmlflush{#1}$}
\stopxmlsetups

\startxmlsetups mml:mphantom
    \phantom{{}\xmlflush{#1}{}}
\stopxmlsetups

\startxmlsetups mml:mpadded
    \xmlflush{#1}
\stopxmlsetups

\startxmlsetups mml:mfenced % {} around separator is needed for spacing
    \edef\MMPopen      {\xmlatt{#1}{open}}
    \edef\MMPclose     {\xmlatt{#1}{close}}
    \edef\MMPseparators{\xmlatt{#1}{separators}}
    \ifx\MMPseparators\empty \def\MMPseparators{,}\fi
    \doifelsenothing \MMPopen {
        \left.
    } {
        \left\MMPopen
    }
    \ctxlua{lxml.mml.connect("#1","/*","\MMPseparators")}%
    \doifelsenothing \MMPclose {
        \right.
    } {
        \right\MMPclose
    }
\stopxmlsetups

\startxmlsetups mml:menclose % notation=.....
    % we will implement this when we need it
    \xmlflush{#1}
\stopxmlsetups

\startxmlsetups mml:msqrt
    \sqrt{\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups mml:mroot
   \root{\xmlindex{#1}{*}{1}}\of{\xmlindex{#1}{*}{2}}
\stopxmlsetups

\startsetups mml:mfrac % dodo: handle linethickness in lua + unit
    \begingroup
    \edef\MMPlinethickness{\xmlatt{#1}{linethickness}}
    \ifx\MMPlinethickness\empty
        \doifelse{\xmlatt{#1}{bevelled}}{true} {
                \xmlindex{#1}{/*}{1}
                    \mathpunct{\kern-.2ex\left.\middle/\right.\kern-.25ex}
                \xmlindex{#1}{/*}{2}
        } {
            \frac{\xmlindex{#1}{/*}{1}}{\xmlindex{#1}{/*}{2}}
        }
    \else
        \processaction
           [\MMPlinethickness]
           [    thin=>\scratchdimen=.2pt,
              medium=>\scratchdimen=.4pt,
               thick=>\scratchdimen=.8pt,
             unknown=>\setdimensionwithunit\scratchdimen{\MMPlinethickness}{pt}]
        {
            {\xmlindex{#1}{/*}{1}}
                \above\scratchdimen
            {\xmlindex{#1}{/*}{2}}
        }
    \fi
    \endgroup
\stopsetups

\startsetups mml:mspace % todo: width=ex/ex/pt
    \hskip.5em\relax
\stopsetups

\startsetups mml:msub
    \xmlindex{#1}{/*}{1}_{\xmlindex{#1}{/*}{2}}
\stopsetups

\startsetups mml:msup
    \xmlindex{#1}{/*}{1}^{\xmlindex{#1}{/*}{2}}
\stopsetups

\startsetups mml:msubsup
    \xmlindex{#1}{/*}{1}_{\xmlindex{#1}{/*}{2}}^{\xmlindex{#1}{/*}{3}}
\stopsetups

\startsetups mml:mover
    \mathop {
        \vbox {
            \m@th\ialign {
                \hss##\hss\crcr
                \noalign{\kern3\p@}%
                \disabledelimiter\doMMLfiller{\xmlindex{#1}{/*}{2}}\crcr
                \noalign{\kern3\p@\nointerlineskip}%
                \disabledelimiter\doMMLfiller{\xmlindex{#1}{/*}{1}}\crcr
            }
        }
    }
    \limits
\stopsetups

\startsetups mml:munder
    \mathop {
        \vtop {
            \m@th\ialign{
                \hss##\hss\crcr
                \disabledelimiter\doMMLfiller{\xmlindex{#1}{/*}{1}}\crcr
                \noalign{\kern3\p@\nointerlineskip}%
                \disabledelimiter\doMMLfiller{\xmlindex{#1}{/*}{2}}\crcr
                \noalign{\kern3\p@}
            }
        }
    }
    \limits
\stopsetups

\startsetups mml:munderover
    \xmlindex{#1}{/*}{1}
      _{\disablefiller\disabledelimiter\xmlindex{#1}{/*}{2}}
      ^{\disablefiller\disabledelimiter\xmlindex{#1}{/*}{3}}
\stopsetups

\startxmlsetups mml:mlabeledtr
    \bTR \xmlall{#1}{/mml:mtd} \eTR
\stopxmlsetups

\startxmlsetups mml:mtr
    \bTR \xmlall{#1}{/mml:mtd} \eTR
\stopxmlsetups

\startxmlsetups mml:mtd
    \bTD$\MMLhack\xmlall{#1}{/*}\relax$\eTD
\stopxmlsetups

\startxmlsetups mml:mtable
    \vcenter \bgroup
        \bTABLE[\c!frame=\v!off]
            \xmlall{#1}{/mml:mtr|mml:mlabeledtr}
        \eTABLE
    \egroup
\stopxmlsetups

\startxmlsetups mml:mmultiscripts
    \ctxlua{lxml.mml.multiscripts("#1")}
\stopxmlsetups

\protect \endinput