summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/plain/pitex/pitex.tex
blob: 5b62b8b5529ab5b4cd05e8735566dc5457ec414a (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
395
396
397
% This is piTeX, a set of macros I (Paul Isambert) use to
% typeset documentations for my packages (that's why it is
% archived on CTAN).
%
% Perhaps in the future, when this achieves some kind of
% format-like completude, there'll be a documentation and
% it'll be publicly announced.
%
% You can of course use those macros, but you are on your
% own, and the files will probably be modified without announcement.
% The file is supposed to be \input on plain TeX with LuaTeX, at least v.0.6.
%
%
% The files needed are:
%
% texapi.tex (an independent package \input in the following file)
% yax.tex (an independent package)
% fonts.ptx (piTeX font "management")
% luaotfload.sty (an independent package (not by me))
% sections.ptx (piTeX sectionning commands)
%
% Date: July 2010.
%
%
% User interface
\input yax % which itself \input's texapi

\setcatcodes{\@\_=11}
\def\ptx@error{\senderror{PiTeX}}

% This to write in latin1.
\directlua{%
  lualog = function (message)
    texio.write_nl("") texio.write_nl("")
    texio.write_nl(message)
    texio.write_nl("") texio.write_nl("")
  end
 function convert(buf)
   return string.gsub(buf,"(.)", function (ch)
       return unicode.utf8.char(string.byte(ch))
     end)
 end
 callback.register('process_input_buffer',convert)
 }

\newcount\ptx@attribute_count
\def\newattribute#1{%
  \advance\ptx@attribute_count1
  \edefcs{ptx@attribute_#1}{10\the\ptx@attribute_count}%
  }
\def\ptx@attribute_number#1{\usecs{ptx@attribute_#1}}
\def\ptx@attribute_set#1{\passcs\attribute{ptx@attribute_#1}=1\relax}
\def\ptx@attribute_unset#1{\passcs\attribute{ptx@attribute_#1}=-"7FFFFFFF\relax}

\newdimen\urvsize
\urvsize\vsize

\def\inputpitexfile#1 {\input #1.ptx }

\inputpitexfile fonts
\inputpitexfile sections
% No footnotes in documentations.
%\input footnotes.tex
% I redefine the output routine on every job.
%\input output.tex

\defactiveparameter document{%
  \pdfinfo{%
    /Author (\usevalueor #1 : author {Unknown author})
    /Title  (\usevalueor #1 : pdftitle  {\usevalueor #1 : title {No title}})
    \ifattribute #1 : subject {/Subject (\usevalue #1 : subject )}{}
    \ifattribute #1 : keywords {/Keywords (\usevalue #1 : keywords )}{}
    }%
  \pdfcatalog{%
    /PageMode
      \ifcasevalue #1 : display
        \val outlines   /UseOutlines
        \val signets    /UseOutlines % French!
        \val thumbs     /UseThumbs
        \val thumbnails /UseThumbs
        \val full       /FullScreen
        \val fullscreen /FullScreen
        \elseval        /UseNone
      \endval
    }%
  }

\restrictparameter document : author title pdftitle date subject keywords display version\par
\restrictattribute document:display outlines signets thumbs thumbnails full fullscreen\par


\defactiveparameter page{%
  \settovalue\pdfpagewidth  #1 : pagewidth
  \settovalue\pdfpageheight #1 : pageheight
  \settovalue\baselineskip  #1 : baselineskip
  \settovalueor\topskip     #1 : topskip {\topskip=\baselineskip}%
  \settovalue\pdfhorigin    #1 : left
  \ifattribute #1 : right
              {\hsize = \dimexpr(\pdfpagewidth-\pdfhorigin-\usevalue #1 : right )\relax}
              {\settovalue\hsize #1 : hsize }%
  \settovalue\pdfvorigin    #1 : top
  \settovalue\parindent     #1 : parindent
  \settovalue\parskip       #1 : parskip
  \ifattribute #1 : lines {\vsize=\usevalue #1 : lines \baselineskip}{}%
  }
\restrictparameter page : pagewidth
                          pageheight
                          hsize
                          baselineskip
                          topskip
                          left
                          right
                          top
                          lines
                          parindent
                          parskip
                          \par

% Defaults... they don't produce anything beautiful.
% I redefine them on every job.
\setparameter page:
  pageheight   = 28cm
  pagewidth    = 21cm
  hsize        = 15cm
  baselineskip = 12pt
  lines        = 42
  left         = 1in
  top          = 1in
  parskip      = 0pt


% Should be used with YaX to set parameters.
\long\pdef\newblock{%
  \ifnext*{\gobbleoneand\ptx@newblock_group}
          {\ptx@newblock_nogroup}%
  }
\def\ptx@newblock_nogroup#1#2#3{%
  \def#1{\ifnext/{\gobbleoneand{#3}}{#2}}%
  }
\def\ptx@newblock_group#1#2#3{%
  \def#1{\ifnext/{\gobbleoneand{#3\egroup}}{\bgroup#2}}%
  }
\def\antigobblespace{%
  \ifcatnext a{ }{\iffnext({ }}%
  }


% PDF links
\def\dest#1#2{%
  \ifcs{ptx@link_name=#1}
       {\ptx@error{Link `#1' already exists}#2}
       {\letcs{ptx@link_name=#1}\relax
        \bgroup
          \ifvmode
            \pdfdest name {#1} xyz #2%
          \else
            \setbox0=\hbox{#2}%
            \raise\ht0\hbox{\pdfdest name {#1} xyz}#2%
          \fi
        \egroup
        }%
  }
\def\link#1#2{%
  \pdfstartlink attr {/Border [0 0 0]} goto name {#1}#2\pdfendlink
  }

% Margin notes à la Bringhurst: aligned with the line they refer
% to, unless they bleed into the bottom margin, in which case they
% are shifted up so that their bottom line is aligned with the
% page's bottom line. Totally experimental. It'd be nice if I could
% do that with sections too (so that a note doesn't bleed into the
% next section).
\newattribute{note}
\def\note{\ifnext[{\ptx@note}{\ptx@note[0pt]}}
\def\ptx@note[#1]#2{%
  \ptx@attribute_set{note}%
  \setbox0=\hbox{\vtop{% We need the box's height minus its first line, hence the vtop.
    \notefont \baselineskip=9pt
    \hsize\dimexpr\pdfpagewidth-(\pdfhorigin+\hsize+4em)\relax
    \rightskip=0pt plus 1fil
    \it\noindent #2}}%
  \ptx@attribute_unset{note}%
  \ht0=0pt \dp0=0pt
  \vadjust{%
    \kern-\dimexpr(2pt+#1)\relax
    \moveright\dimexpr\hsize+1em
    \box0
    \kern\dimexpr2pt+#1}%
  \vrule height 0pt width 0pt depth 2pt
% Problem: if the line where the note appears is last of its
% box, the box has no depth and vfilling doesn't work properly.
  }%
\directlua{%
  %
  % Box255 is traversed from the last node upward.
  % Each node with dimension adds to the "remaining height",
  % which is used when a note is encountered: if the note's depth
  % (minus its last box's depth) is larger than the remaining height,
  % then it is shifted upward, so that it doesn't bleed in the bottom
  % margin. When computing the remaining height, kerns and glues aren't
  % taken into account until a box has been seen (and the depth of
  % that first box isn't added either), so the note doesn't even
  % bleed into available page space that isn't filled with text.
  %
  process_marginalia = function (head)
    local remainingheight, first, item = 0, true, node.slide(head)
    while item do
      if node.has_attribute(item,\ptx@attribute_number{note}) then
        local depth = item.list.depth - node.tail(item.list.list).depth
        if depth > remainingheight then
          lualog("A note has been shifted on page " .. tex.count[0] ..
                 " by an amount of " .. (depth-remainingheight)/65536 .. "pt.")
          item.list.shift = remainingheight-depth
        end
      elseif node.has_field(item, "height") then
        remainingheight = remainingheight+item.height
        if first then
          first = false
        else
          remainingheight = remainingheight + item.depth
        end
      elseif node.has_field(item,"kern") then
        if not first then
          remainingheight = remainingheight+item.kern
        end
      elseif node.has_field(item, "spec") then
        if not first then
          remainingheight = remainingheight+item.spec.width
        end
      end
      item = item.prev
    end
  end
  }
\def\processmarginalia{%
  \directlua{process_marginalia(tex.box[255].list)}%
  }

\def\stoppage{\penalty-10000\relax}
\def\clearpage{\vfil\stoppage\relax}
\def\needspace#1{%
  \penalty0
  \ifdim\dimexpr\pagegoal-\pagetotal<\dimexpr#1\relax
    \clearpage
  \fi
  }

% Verbatim facilities.
\newbox\ptx@box_temp

\def\tcode#1{{\codefont#1}}
\long\def\com#1{%
  \bgroup
    \codefont
    \string#1%
  \egroup
  \antigobblespace
  }
\def\arg#1{{\codefont\it<#1>}\iffnext\spacechar{\kern.2ex }}
\def\barg#1{{\codefont\char"007B\relax{\it<#1>}\char"007D\relax}}
\def\oarg#1{{\codefont[{\it<#1>}]}}


\def\exampleskip{\vskip\baselineskip}
\def\example{%
  \exampleskip
  \bgroup
  \parindent0pt
  \setcatcodes{\\\{\}\$\&\#\^\_\ \~\%=12,\^^M=13}%
  \codefont
  \readexample
  }%
\let\ptx@example_original\example
\bgroup
\setcatcodes{\\=12,\|=0,\^^M=13}%
|gdef|readexample#1^^M#2\example/{#2|egroup|exampleskip}%
|restorecatcodes%
\setcatcodes{\^^M=13}%
\gdef^^M{\quitvmode\endgraf}%
\egroup%
\def\verb#1{%
  \def\ptx@verb##1#1{##1\egroup}%
  \bgroup
  \setcatcodes{\\\{\}\$\&\#\^\_\ \~\%=12}%
  \codefont
  \ptx@verb
  }

%
% Read and process examples.
%
\newwrite\ptx@examplewrite

\bgroup
\setcatcodes{\%=12,\/=14}
\gdef\Example{/
  \immediate\openout\ptx@examplewrite=\jobname.pex
  \immediate\write\ptx@examplewrite{% This is a scratch file used to typeset examples in \jobname.tex.}/
  \immediate\write\ptx@examplewrite{% You can delete it.}/
  \immediate\write\ptx@examplewrite{%}/
  \bgroup
  \setcatcodes{\\\#\%\^^M\ =12}/
  \writeexample
  }/
\egroup

\bgroup
\setcatcodes{\^^M=12}%
\gdef\writeexample#1^^M{%
  \immediate\write\ptx@examplewrite{\noexpand\example}%  
  \ptx@writeexample}%
\gdef\ptx@writeexample#1^^M{%
  \passexpanded{\ifstring{#1}}{\examplestring}%
               {\immediate\write\ptx@examplewrite{\string\example/}%
                \immediate\closeout\ptx@examplewrite\egroup%
                \processexample}%
               {\immediate\write\ptx@examplewrite{\primunexpanded{#1}}%
                \ptx@writeexample}%
  }%
{\setcatcodes{\\=12,\|=0}|gdef|examplestring{\Example/}}%
\egroup

\def\typesetexample{%
  {\input\jobname.pex }%
  }
\def\doexamplenogroup{\let\example\ptx@Example\input\jobname.pex \let\example\ptx@example_original}
\def\doexample{{\doexamplenogroup}}
\def\ptx@Example{%
  \iffnext/{\gobbleoneand\endinput}%
  }
% To be redefined for different layout.
\def\processexample{\typesetexample\doexample}

\def\verb#1{%
  \def\ptx@verb##1#1{##1\egroup}%
  \bgroup
  \setcatcodes{\\\{\}\$\&\#\^\_\ \~\%=12}%
  \codefont
  \ptx@verb
  }


\newdimen\extraboxspace
\newdimen\ptx@extraboxspace_top
\newdimen\ptx@extraboxspace_right
\newdimen\ptx@extraboxspace_bottom
\newdimen\ptx@extraboxspace_left

\newfornoempty\ptx@colorbox_loop{1}#2,{%
  \ifcase#1
    \ptx@extraboxspace_top    =#2
    \ptx@extraboxspace_right  =#2
    \ptx@extraboxspace_bottom =#2
    \ptx@extraboxspace_left   =#2
  \or
    \ptx@extraboxspace_right  =#2
    \ptx@extraboxspace_left   =#2
  \or
    \ptx@extraboxspace_bottom =#2
  \or
    \ptx@extraboxspace_left   =#2
  \fi
  \passarguments{\numexpr(#1+1)}%
  }
\def\colorbox{%
  \ifnextnospace[\ptx@colorbox_setborders
          {\ptx@extraboxspace_top    =\extraboxspace
           \ptx@extraboxspace_right  =\extraboxspace
           \ptx@extraboxspace_bottom =\extraboxspace
           \ptx@extraboxspace_left   =\extraboxspace
           \ptx@colorbox_do}%
  }
\def\ptx@colorbox_setborders[#1]{%
  \ptx@colorbox_loop{0}{#1,}%
  \ptx@colorbox_do
  }
{\setcatcodes{pt=12}
\gdef\noPT#1pt{#1 }}
\def\ptx@colorbox_do#1#2{%
  \bgroup
  \setbox\ptx@box_temp=\hbox{#2}%
  \hbox{%
    \pdfliteral{
      q #1 rg #1 RG
      -\expandafter\noPT\the\ptx@extraboxspace_left
       \expandafter\noPT\the\dimexpr(\ht\ptx@box_temp+\ptx@extraboxspace_top)\relax
       \expandafter\noPT\the\dimexpr(\wd\ptx@box_temp+\ptx@extraboxspace_left+\ptx@extraboxspace_right)\relax
      -\expandafter\noPT\the\dimexpr(\ht\ptx@box_temp+\ptx@extraboxspace_top+\dp0+\ptx@extraboxspace_bottom)\relax
      re f Q}%
    #2}%
  \egroup
  }

\def\trace{\tracingcommands4 \tracingmacros4 }
\def\untrace{\tracingcommands0 \tracingmacros0 }

\restorecatcodes