summaryrefslogtreecommitdiff
path: root/tex/context/base/meta-tex.tex
blob: 75aeb54c7e5eb53febd67fd42e7c21c05fbda9c9 (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
%D \module
%D   [       file=meta-tex,
%D        version=2006.06.07,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=\METAPOST\ fast text insertion,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%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 Many thanks to Fabrice Popineau and Taco Hoekwater in helping me
%D figure out some aspects of the text inclusion method implemented
%D here. The following code is derived from a more advanced (and to
%D be used) mechanism where \TEX, \METAPOST\ and \LUA\ play together.

%D Much of this mechanism was written with running live DVD's of
%D the Dave Matthews band in the background (or the corner of my
%D screen).

% todo: testmacro for empty pic

\unprotect

\newwrite\TeXtextwrite

\def\openTeXtexts {\immediate\openout \TeXtextwrite\currentTeXtext.mpb\relax}
\def\closeTeXtexts{\immediate\closeout\TeXtextwrite}

\def\currentTeXtext {\jobname-mpgraph}
\def\currentTeXstack{mpgtxt}

\initializeboxstack\currentTeXstack

\newtoks\collectedmptexts
\newtoks\everyTeXtexts

\chardef\TeXtextsmode\zerocount % no inheritance

\long\def\startTeXtexts#1\stopTeXtexts
  {\global\collectedmptexts\expandafter{\the\collectedmptexts#1}}

\def\dostartTeXtexts
  {\global\setfalse\TeXtextdone
   \startnointerference
   \openTeXtexts
   \ifcase\TeXtextsmode
     % normally there is no need for this (faster anyway)
   \or
     \scantokens\expandafter{\the\everyMPTEXgraphic}% brr
   \or
     \the\everyTeXtexts
   \fi
   \ifrunMPgraphics
     \initializeboxstack\currentTeXstack
   \else
     \global\let\openTeXtexts\relax
     \global\let\finishTeXtexts\closeTeXtexts
   \fi}

\def\dostopTeXtexts
  {\ifrunMPgraphics
      \closeTeXtexts
   \fi
   \stopnointerference}

\let\finishTeXtexts\relax

\appendtoks
  \finishTeXtexts
\to \everystoptext

\newconditional\TeXtextdone

% \long\def\TeXtext#1%
%   {\dowithnextboxcontent
%      {\setnormalcatcodes}
%      {\global\settrue\TeXtextdone
%       \immediate\write\TeXtextwrite{savetxt(#1,\the\wd\nextbox,\the\ht\nextbox,\the\dp\nextbox);}%
%       \savebox\currentTeXstack{#1}{\box\nextbox}}
%     \hbox}

\long\def\TeXtext
  {\dosingleempty\doTeXtext}

% currently, colors in the converter don't use the color stack
%
% 0 = nothing, withcolor works ok, but nested colors fail
% 1 = local color stack ok
% 2 = obey color stack (not yet supported)

\chardef\TeXtextcolormode\plusone

\def\definetextext[#1]#2{\setvalue{textext@@#1}{#2}}

% \definetextext[framed]{\framed}
%
% \startMPcode
%     draw \sometxt[framed]{black} rotated 45 ;
% \stopMPcode

\long\def\doTeXtext[#1]#2#3%
  {\begingroup
   \setbox\nextbox\hbox
     {\setnormalcatcodes
      \endlinechar\minusone
      \everyeof\emptytoks
     %\def\ascii{#3}\scantokens\expandafter{\ascii}}%
      \ifcase\TeXtextcolormode
        \scantokens{\executeifdefined{textext@@#1}\firstofoneargument{#3}}%
      \else
        \localcolortrue
        \startcurrentcolor
        \scantokens{\executeifdefined{textext@@#1}\firstofoneargument{#3}}%
        \stopcurrentcolor
      \fi}%
   \global\settrue\TeXtextdone
   \edef\currenttextxt{\number#2}%
   \executeifdefined{textext::#1}{\getvalue{textext::depth}}%
   \savebox\currentTeXstack\currenttextxt{\box\nextbox}%
   \endgroup}

\setvalue{textext::depth}{\immediate\write\TeXtextwrite{savetxt(\currenttextxt,\the\nextboxwd,\the\nextboxht,\the\nextboxdp) shifted (0,-\the\nextboxdp);}}
\setvalue{textext::nodepth}{\immediate\write\TeXtextwrite{savetxt(\currenttextxt,\the\nextboxwd,\the\nextboxht,\the\nextboxdp);}}

\setvalue{textext::d}{\getvalue{textext::depth}}
\setvalue{textext::n}{\getvalue{textext::nodepth}}

\newbox\mptextbox

%definefontsynonym[MPtxtfont][texnansi-lmtt10] \loadmapfile[texnansi-public-lm.map]
\definefontsynonym[MPtxtfont][texnansi-lmtt10] \loadmapfile[lm-texnansi.map]

\definefont[localMPtxtfont][MPtxtfont at 10bp]

\ifx\getTeXtext\undefined

    % this took a while to figure out

   \let\MPtextdata\empty

   \def\getTeXtext
     {\ifx\MPtextdata\empty\else
        \localMPtxtfont
        \setbox\mptextbox\hbox{\foundbox\currentTeXstack{\number\nofTeXtexts}}%
        \setbox\scratchbox\hbox{\MPtextdata}% set in meta-pdf.mkii/mkiv
        \edef\mpwd{\the\dimexpr\MPtextsize\dimexpr\wd\scratchbox/10\relax\relax}%
        \edef\mpht{\the\dimexpr\MPtextsize\dimexpr\ht\scratchbox/10\relax\relax}%
        \setbox\mptextbox\hbox{\raise\dp\mptextbox\box\mptextbox}%
        \dp\mptextbox\zeropoint
        \scale[\c!width=\mpwd,\c!height=\mpht]{\box\mptextbox}%
      \fi}

\fi

\let\nofTeXtexts\!!zerocount

\setvalue{handleMPtext00001}% only height in tag (00001)
  {\setbox\scratchbox\hbox
     {\obeyMPspecials
      \edef\nofTeXtexts{\number\MPtextnumber}%
      \getTeXtext}%
   \setbox\scratchbox\hbox
     {\hskip\lastMPmoveX\onebasepoint\raise\lastMPmoveY\onebasepoint
      \box\scratchbox}%
   \ht\scratchbox\zeropoint
   \dp\scratchbox\zeropoint
   \wd\scratchbox\zeropoint
   \box\scratchbox}

\startMPextensions
    string txtfile ; txtfile := "\currentTeXtext.mpb" ;
    string txtfont ; txtfont := "\truefontname{MPtxtfont}" ;
    string txtpref ; txtpref := "00001::::" ;
\stopMPextensions

\long\def\dodofiltersometxt#1#2#3%
  {\ifx#2\empty
   \else
     \increment\txtcounter
     \TeXtext\txtcounter{#1}%
     \expandafter\filtersometxt
   \fi#2#3}

\long\def\redofiltersometxt[#1]#2%
  {\increment\txtcounter
   \TeXtext[#1]\txtcounter{#2}%
   \filtersometxt}

\long\def\filtersometxt#1\sometxt
  {\doifnextcharelse[\redofiltersometxt\dodofiltersometxt}

% cleaner in mkiv
%
% \filtersometxt abc\sometxt{def};hij\sometxt{klm};\sometxt{}\empty\relax

\long\def\flushTeXtexts#1%
  {\newcounter\txtcounter
   \dostartTeXtexts
     \the\collectedmptexts
     \filtersometxt#1\sometxt{}\empty\relax
   \dostopTeXtexts
   \ifconditional\TeXtextdone
     \immediate\write\MPwrite{loadtxts ; txtnext := 0 ;}%
     \global\collectedmptexts\emptytoks
   \fi
   \newcounter\txtcounter}

% \long\def\sometxt#1{sometxt(nexttxt)} % to be used in mp definitions, no ; here

\long\def\sometxt #1#{\dosometxt}       % grab optional [args]
\long\def\dosometxt#1{sometxt(nexttxt)} % to be used in mp definitions, no ; here

% we redefine the writer:

\long\def\writecheckedMPgraphic#1%
  {\ifforceMPTEXgraphic
     \global\MPTEXgraphictrue
   \else
     \global\MPTEXgraphicfalse
     \edef\ascii{#1}\convertcommand\ascii\to\MPascii
     \the\MPTEXgraphicchecks\relax % \relax is end condition!
   \fi
   \flushMPTEXgraphic% % verbatimtex etc
   \flushTeXtexts{#1}% added
   \writeMPgraphic{#1}} % potential optimization: pass \ascii

\protect \endinput

% torture test (will move)

\startMPpage
    numeric a_b_c ;
    picture p ; pickup pencircle scaled .1pt ;
    p := \sometxt{Just a \color[blue]{simple} example text.} ;
    p := image(draw p; draw boundingbox p withcolor red; ) ;
    p := p rotatedaround(center p,  360*(5/100)) ;
    draw p ; draw boundingbox p withcolor blue ;
    currentpicture := currentpicture scaled 20 ;
    draw boundingbox currentpicture  withcolor .5white ;
    setbounds currentpicture to boundingbox currentpicture enlarged 10pt ;
\stopMPpage

\startMPpage
    picture p ;
    p := \sometxt{\framed[width=fit,align=middle]{\input tufte\relax}} ;
    draw p rotatedaround(center p, 30) ;
\stopMPpage

\startMPpage
    picture p ;
    p := \sometxt{\framed[width=fit,align=middle]{\input tufte\relax}} ;
    draw p slanted .5 ;
\stopMPpage

\dorecurse{10} {
    \startTeXtexts
        \TeXtext{\recurselevel}{\ruledhbox{I must be {\green crazy} to implement this}}
    \stopTeXtexts
    \startMPpage
        picture p ; pickup pencircle scaled .1pt ;
        numeric i ; i := \recurselevel ;
        p := sometxt(i) ;
        p := p rotatedaround(center p,  360*(i*5/100)) ;
        draw p ; draw boundingbox p withcolor blue ;
        currentpicture := currentpicture scaled 20 ;
        draw boundingbox currentpicture  withcolor .5white ;
    \stopMPpage
}

\startTeXtexts
  \dorecurse{100}{\TeXtext{\recurselevel}{\ruledhbox{\strut interesting \recurselevel}}}
\stopTeXtexts

\startMPpage
    picture p ; pickup pencircle scaled .1pt ;
    for i = 1 upto 100:
        p := sometxt(i) ;
        p := p rotatedaround(center p,  360*(i*5/100)) ;
        draw p ; draw boundingbox p withcolor blue ;
    endfor ;
    currentpicture := currentpicture scaled 20 ;
    draw boundingbox currentpicture  withcolor .5white ;
\stopMPpage

\startTeXtexts
    \dorecurse{100}{\TeXtext{\recurselevel}{\ruledhbox{\strut interesting \recurselevel}}}
\stopTeXtexts

\startMPpage
    picture p ; pickup pencircle scaled .1pt ;
    for i = 1 step 5 until 100 :
        p := sometxt(i) ;
        p := p rotatedaround(center p,  360*(i/100)) ;
        draw p ; draw boundingbox p withcolor blue ;
    endfor ;
    currentpicture := currentpicture scaled 20 ;
    draw boundingbox currentpicture  withcolor .5white ;
\stopMPpage

\startTeXtexts
    \dorecurse{20}{\TeXtext{\recurselevel}{\externalfigure[t:/sources/cow.pdf][width=1cm]}}
\stopTeXtexts

\startMPpage
    picture p ; pickup pencircle scaled .1pt ;
    for i = 1 upto 20 :
        p := sometxt(i) ;
        p := p shifted (2.5cm,0) rotated (360*(i/20)) ;
        draw p ; draw boundingbox p withcolor blue  ;
    endfor ;
    currentpicture := currentpicture scaled 10 ;
    draw boundingbox currentpicture  withcolor .5white ;
\stopMPpage

\startTeXtexts
    \dorecurse{200}{\TeXtext{\recurselevel}{\ruledhbox{\strut I must be {\green crazy} \recurselevel}}}
\stopTeXtexts

\startMPpage
    picture p ; pickup pencircle scaled .1pt ;
    numeric i ; i := 100 ;
    p := sometxt(i) ;
    p := p rotatedaround(center p,  360*(i*36/100)) ;
    draw p ; draw boundingbox p withcolor blue  ;
    currentpicture := currentpicture scaled 20 ;
    draw boundingbox currentpicture  withcolor .5white ;
\stopMPpage

\dorecurse{10}{
    \startTeXtexts
        \dorecurse{200}{\TeXtext{\recurselevel}{\ruledhbox{\strut I must be {\green crazy} \recurselevel}}}
    \stopTeXtexts
    \startMPpage
        picture p ; pickup pencircle scaled .1pt ;
        j := 10*\recurselevel-9;
        k := 10*\recurselevel;
        for i = j upto k:
            p := sometxt(i) ;
            p := p rotatedaround(center p,  360*(i/100)) ;
            draw p ; draw boundingbox p withcolor blue ;
        endfor ;
        currentpicture := currentpicture scaled 20 ;
        draw boundingbox currentpicture  withcolor red ;
    \stopMPpage
}