summaryrefslogtreecommitdiff
path: root/tex/context/base/meta-tex.tex
blob: e6143176ad799409eaf3b365680e3ac3761c78ff (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
%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).

\unprotect

\newwrite\TeXtextwrite

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

\def\currentTeXtext{\jobname-mpgraph}

\initializeboxstack\currentTeXtext

\newtoks\collectedmptexts

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

\def\dostartTeXtexts
  {\global\setfalse\TeXtextdone
   \startnointerference
   \the\everyMPTEXgraphic
   \openTeXtexts
   \ifrunMPgraphics
     \initializeboxstack\currentTeXtext
   \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\currentTeXtext{#1}{\box\nextbox}}
%     \hbox}

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

\long\def\doTeXtext[#1]#2#3%
  {\begingroup
   \setbox\nextbox\hbox
     {\setnormalcatcodes
      \newlinechar=`\^^M
      \everyeof\emptytoks
     %\def\ascii{#3}%
     %\scantokens\expandafter{\ascii}}%
      \scantokens{#3}}%
   \global\settrue\TeXtextdone
   \edef\currenttextxt{\number#2}%
   \executeifdefined{textext::#1}{\getvalue{textext::depth}}%
   \savebox\currentTeXtext\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[lm-texnansi.map]

\definefont[localMPtxtfont][MPtxtfont at 10bp]

\ifx\getTeXtext\undefined

    % this took a while to figure out

    % \def\getTeXtext
    %   {\localMPtxtfont
    %    \setbox\mptextbox\hbox{\foundbox\currentTeXtext{\number\nofTeXtexts}}%
    %    \setbox\scratchbox\hbox{\MPtextdata}%
    %    \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}}%

   \def\getTeXtext
     {\localMPtxtfont
      \setbox\mptextbox\hbox{\foundbox\currentTeXtext{\number\nofTeXtexts}}%
      \setbox\scratchbox\hbox{\MPtextdata}%
      \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

\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\filtersometxt#1\sometxt#2#3#4%
%   {\ifx#3\empty
%    \else
%      \increment\txtcounter
%      \TeXtext\txtcounter{#2}%
%      \expandafter\filtersometxt
%    \fi#3#4}

\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}

% \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