summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/typo-mar.mkiv
blob: 2b89f57774de8c725c22f071e868138817cd5826 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
% macros=mkvi

%D \module
%D   [       file=typo-mar,
%D        version=2010.02.15, % was experimental code
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Margindata,
%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.

% todo: tags

\writestatus{loading}{ConTeXt Typesetting Macros / Margindata}

\unprotect

%D This module has been on the agenda for a while. Actually, it is
%D one of the things that I really need myself, for instance when
%D rendering rather unpredictable (educational) tests encoded in
%D XML. This module permits anchoring for instance item numbers and
%D also overload them when they have subnumbers. In the future it
%D might replace the current maginal note mechanism (that then
%D will be just an instance).
%D
%D In spite of what might be expected, the more advanced \LUA\ based
%D variant is upto twice as fast on simple entries. Also, we no longer
%D need an extra pass to get inner and outer alignments in sync with
%D the pagebuilder.

\registerctxluafile{typo-mar}{1.001}

%definesystemattribute[margindata] % only at the lua end

%D In \MKII\ we have three categories and their historically meaning
%D is as follows:
%D
%D marginlines: These are flushed relative to the start of a line and
%D need to be invoked there.
%D
%D marginwords: These can be issued in the text flow and will migrate
%D sidewards; in spite of the name, it can be a paragraph of text as
%D well, but normally it's words.
%D
%D margintexts: These can be set beforehand and are flushed at the
%D next paragraph of text (of header).
%D
%D In \MKIV\ we have further integrated the mechanism and we now have:
%D
%D margindata: This can be anything that needs to go into the margin.
%D It can be anchored in the text or given beforehand in which case
%D it gets flushed at the first occasion.
%D
%D margintext: This runs on top of margindata and the only difference
%D is that it uses the framed mechanism for packaging.
%D
%D Stacking is done differently as is inner and outer alignment (in
%D most cases more efficient). The functionality is mostly the same
%D as in \MKII, but there are a few additions, like names entries,
%D where later ones overload preceding not yet flushed ones. Data can
%D get catagorized and is then treated as a group (e.g. when stacking
%D is needed).
%D
%D The amount of \TEX\ code is less than in \MKII\ because we do all
%D trickery in at the \LUA\ end. At the end of this file we define
%D several commands, like \type {\inleftmargin} and \type {\inleft}.
%D You can configure them individually or as a group. There is an
%D inheritance model in place.
%D
%D The following notes will be stacked:
%D
%D \starttyping
%D \ininner[line=2]{IM A}
%D \ininner[stack=yes]{IM B}
%D \ininner[stack=yes]{IM C}
%D \stoptyping
%D
%D The distance between them is determined by \type {dy}:
%D
%D \starttyping
%D \ininner[stack=yes,dy=2ex][frame=on] {IM A}
%D \ininner[stack=yes,dy=2ex][frame=on] {IM B}
%D \stoptyping
%D
%D There are several methods of vertical alignment.
%D
%D \starttyping
%D \inmargin [method=first]             [frame=on]                              {first\\second} \input ward \par
%D \inmargin [method=first]             [frame=on,offset=3pt]                   {first\\second} \input ward \par
%D \inmargin [method=first,voffset=-3pt][frame=on,offset=3pt,rulethickness=3pt] {first\\second} \input ward \par
%D \inmargin [method=first,voffset=-6pt][frame=on,offset=3pt,rulethickness=3pt] {first\\second} \input ward \par
%D \stoptyping
%D
%D You sometimes need to combine \type {voffset} with \type {offset}. The first
%D argument concerns the data, the second the framed. Not sharing the setup is
%D on purpose: location, offset, alignment and other parameters might clash.

\installcorenamespace{margindata}
\installcorenamespace{marginframed}

\installcommandhandler       \??margindata   {margindata}   \??margindata
\installframedcommandhandler \??marginframed {marginframed} \??marginframed

\setupmargindata
  [\c!location=\v!left,
 % \c!align=,
 % \c!method=,
   \c!style=\v!bold,
   \c!color=, % maybe \maintextcolor
 % \c!name=,
 % \c!category=,
   \c!threshold=.25ex,
   \c!margin=\v!normal,
   \c!scope=\v!global,
   \c!width=,
 % \c!stack=,
   \c!line=0,
   \c!anchor=\v!text,
   \c!bottomspace=\strutdepth, % slack used for keeptogether
   \c!dy=\zeropoint,
   \c!distance=\zeropoint,
   \c!hoffset=\zeropoint,
   \c!voffset=\zeropoint]

\setupmarginframed % so, align should be set with the data command
  [\c!strut=\v!yes,
   \c!offset=\v!overlay,
   \c!fr!analyze=\v!yes,
   \c!frame=\v!off,
   \c!width=\margindataparameter\c!width,
   \c!align=\margindataparameter\c!align]

\appendtoks
    \setuevalue\currentmargindata{\margindata[\currentmargindata]}%
\to \everydefinemargindata

\newconditional\inhibitmargindata      % This one is used at the Lua end!
\newtoks       \everymargindatacontent % Later on we will set this one.

\appendtoks
    \settrue\inhibitmargindata
\to \everyforgetall

\appendtoks
    \forgetall
    \tf
    \resetallattributes % \deactivatecolor % needed, but maybe we should switch to maintextcolor: \onlyinheritmaintextcolor
\to \everymargindatacontent

% trialtypesetting: no need for margin stuff while trialing as
% is has no dimensions

\definesystemconstant{margintext}

\newcount\nofmargintexts

\definepagestate[\s!margintext]

\unexpanded\def\typo_margins_data_synchronize
  {\doforcedtrackpagestate\s!margintext\nofmargintexts % includes increment
   \docheckpagestate\s!margintext\nofmargintexts
  %\doifrightpagestateelse\s!margintext\nofmargintexts\relax\relax
   \realpageno\realpagestateno
   \swapmargins}

\unexpanded\def\margindata
  {\iftrialtypesetting
     \expandafter\typo_margins_data_nop
   \else
     \expandafter\typo_margins_data_yes
   \fi}

\def\typo_margins_data_nop[#name]%
  {\dodoubleempty\typo_margins_data_nop_indeed}

\def\typo_margins_data_yes[#name]%
  {\setfalse\inhibitmargindata % flushing afterwards
   \begingroup
  %\settrue\inhibitmargindata  % no flushing in here
   \def\currentmargindata{#name}%
   \let\currentmarginframed\currentmargindata
   \dodoubleempty\typo_margins_data_yes_indeed}

\unexpanded\def\typo_margins_data_nop_indeed[#dataparameters][#textparameters]#content%
  {}

\unexpanded\def\typo_margins_data_yes_indeed[#dataparameters][#textparameters]#content%
  {\iffirstargument
     \setupcurrentmargindata[#dataparameters]%
   \fi
   \doifelsenothing{#content}\donefalse\donetrue
   \ifdone
     \edef\currentmarginreference{\margindataparameter\c!reference}%
     \ifx\currentmarginreference\empty \else
       \strc_references_set_page_only_destination_box_attribute\currentmarginreference\currentmarginreference
     \fi
     \edef\currentmargindatastrut{\margindataparameter\c!strut}%
     \dostarttagged\t!margintext\currentmargindata
     \ifcsname\currentmarginframedhash\s!parent\endcsname
       \setbox\nextbox\hbox \currentmarginreference \bgroup
         \the\everymargindatacontent
         \usemargindatastyleandcolor\c!style\c!color
         \setupcurrentmarginframed[\c!location=\v!normal,#textparameters]%
\typo_margins_data_synchronize
         \inheritedmarginframedframed\bgroup
           \ifx\currentmargindatastrut\empty \else
             \synchronizestrut\currentmargindatastrut
           \fi
           \begstrut
           \strc_references_flush_destination_nodes
           \margindataparameter\c!command{#content}%
           \endstrut
         \egroup
       \egroup
       \edef\currentmarginfirstheight{\number\dimexpr\framedfirstheight}%
     \else
       \edef\currentmargindatawidth{\margindataparameter\c!width}%
       \ifx\currentmargindatawidth\empty
         \setbox\nextbox\hbox \currentmarginreference \bgroup
\typo_margins_data_synchronize
           \the\everymargindatacontent
           \usemargindatastyleandcolor\c!style\c!color
           \ifx\currentmargindatastrut\empty \else
             \synchronizestrut\currentmargindatastrut
           \fi
           \begstrut
           \strc_references_flush_destination_nodes
           \margindataparameter\c!command{#content}%
           \endstrut
         \egroup
         \let\currentmarginfirstheight\empty
       \else
         \setbox\nextbox\hbox \currentmarginreference \bgroup
\typo_margins_data_synchronize
           \dosetraggedcommand{\margindataparameter\c!align}%
           \vtop \bgroup
             \the\everymargindatacontent
             \usemargindatastyleandcolor\c!style\c!color
             \hsize\currentmargindatawidth
             \raggedcommand
             \ifx\currentmargindatastrut\empty \else
               \synchronizestrut\currentmargindatastrut
             \fi
             \begstrut
             \strc_references_flush_destination_nodes
             \margindataparameter\c!command{#content}%
             \endstrut
           \egroup
         \egroup
         \edef\currentmarginfirstheight{true}%
       \fi
     \fi
     \dostoptagged
   \fi
   \ifdone
     \anch_positions_initialize % we use positions at the lua end
     \ctxcommand{savemargindata{
       location    = "\margindataparameter\c!location",
       method      = "\margindataparameter\c!method",
       category    = "\margindataparameter\c!category",
       name        = "\margindataparameter\c!name",
       scope       = "\margindataparameter\c!scope",
       number      = \number\nextbox,
       margin      = "\margindataparameter\c!margin", % local normal margin edge
       distance    = \number\dimexpr\margindataparameter\c!distance,
       hoffset     = \number\dimexpr\margindataparameter\c!hoffset,
       voffset     = \number\dimexpr\margindataparameter\c!voffset,
       dy          = \number\dimexpr\margindataparameter\c!dy,
       bottomspace = \number\dimexpr\margindataparameter\c!bottomspace,
     \ifx\currentmarginfirstheight\empty \else
       baseline    = \currentmarginfirstheight,
     \fi
       threshold   = \number\dimexpr\margindataparameter\c!threshold, % overlap related, will change
     \ifhmode
       inline      = true,
     \fi
       anchor      = "\margindataparameter\c!anchor",
   %
   % we're not in forgetall
   %
   % \ifzeropt\leftskip \else
   %   leftskip = \number\leftskip,
   % \fi
   % \ifzeropt\leftskip \else
   %   rightskip = \number\rightskip,
   % \fi
       align       = "\margindataparameter\c!align",
       line        = \number\margindataparameter\c!line,
       stack       = "\margindataparameter\c!stack",
     }}%
   \else
     \ctxcommand{savemargindata{
       location    = "\margindataparameter\c!location",
       method      = "\margindataparameter\c!method",
       category    = "\margindataparameter\c!category",
       name        = "\margindataparameter\c!name",
       scope       = "\margindataparameter\c!scope",
       number      = \number\nextbox,
     }}%
   \fi
   \endgroup}

%D Downward compatible hack:

\unexpanded\def\spaceorpar
  {\endgraf\ifhmode\space\fi}

\appendtoks
    \let\\\spaceorpar
\to \everymargindatacontent

%D Another one:

% \installcorenamespace{oppositemargin}
%
% \letvalue{\??oppositemargin\v!left  }\v!right
% \letvalue{\??oppositemargin\v!right }\v!left
% \letvalue{\??oppositemargin\v!inner }\v!outer
% \letvalue{\??oppositemargin\v!outer }\v!inner
% \letvalue{\??oppositemargin\v!normal}\v!normal
%
% \def\oppositemargin#1%
%   {\csname\??oppositemargin\ifcsname\??oppositemargin#1\endcsname#1\else\v!normal\fi\endcsname}

%D Definitions:

% common to lines and text

\setupmargindata [\v!left ] [\c!method=\v!first,\c!location=\v!left ,\c!margin=\v!margin,\c!align=\v!flushright,\s!parent=\??margindata] % we could autoparent when no define yet
\setupmargindata [\v!right] [\c!method=\v!first,\c!location=\v!right,\c!margin=\v!margin,\c!align=\v!flushleft, \s!parent=\??margindata]
\setupmargindata [\v!outer] [\c!method=\v!first,\c!location=\v!outer,\c!margin=\v!margin,\c!align=\v!inner,     \s!parent=\??margindata]
\setupmargindata [\v!inner] [\c!method=\v!first,\c!location=\v!inner,\c!margin=\v!margin,\c!align=\v!outer,     \s!parent=\??margindata]

% lines

\definemargindata [inleftmargin]  [\v!left ] [\c!margin=\v!margin,\c!width=\leftmarginwidth ,\c!style=,\c!color=]
\definemargindata [inrightmargin] [\v!right] [\c!margin=\v!margin,\c!width=\rightmarginwidth,\c!style=,\c!color=]
\definemargindata [inoutermargin] [\v!outer] [\c!margin=\v!margin,\c!width=\outermarginwidth,\c!style=,\c!color=]
\definemargindata [ininnermargin] [\v!inner] [\c!margin=\v!margin,\c!width=\innermarginwidth,\c!style=,\c!color=]

\definemargindata [inleftedge]    [\v!left ] [\c!margin=\v!edge  ,\c!width=\leftedgewidth   ,\c!style=,\c!color=,\c!category=\v!edge]
\definemargindata [inrightedge]   [\v!right] [\c!margin=\v!edge  ,\c!width=\rightedgewidth  ,\c!style=,\c!color=,\c!category=\v!edge]
\definemargindata [inouteredge]   [\v!outer] [\c!margin=\v!edge  ,\c!width=\outeredgewidth  ,\c!style=,\c!color=,\c!category=\v!edge]
\definemargindata [ininneredge]   [\v!inner] [\c!margin=\v!edge  ,\c!width=\inneredgewidth  ,\c!style=,\c!color=,\c!category=\v!edge]

\definemargindata [atleftmargin]  [\v!left ] [\c!margin=\v!normal,\c!width=\leftmarginwidth ,\c!style=,\c!color=]
\definemargindata [atrightmargin] [\v!right] [\c!margin=\v!normal,\c!width=\rightmarginwidth,\c!style=,\c!color=]

% text

\definemargindata [inleft]     [\v!left ]    [\c!margin=\v!margin,\c!width=\leftmarginwidth ,\c!align=\v!flushright]
\definemargindata [inright]    [\v!right]    [\c!margin=\v!margin,\c!width=\rightmarginwidth,\c!align=\v!flushleft]
\definemargindata [inouter]    [\v!outer]    [\c!margin=\v!margin,\c!width=\outermarginwidth,\c!align=\v!inner]
\definemargindata [ininner]    [\v!inner]    [\c!margin=\v!margin,\c!width=\innermarginwidth,\c!align=\v!outer]

% no longer auto auto-other

\definemargindata [inmargin]   [\v!left]     [\c!margin=\v!margin,\c!width=\leftmarginwidth, \c!align=\v!flushright]
\definemargindata [inother]    [\v!right]    [\c!margin=\v!margin,\c!width=\rightmarginwidth,\c!align=\v!flushleft]

\definemargindata [margintext] [\v!left]     [\c!margin=\v!margin,\c!width=\leftmarginwidth, \c!align=\v!flushright,\c!stack=\v!yes]

\setupmarginframed [\v!left ] [\c!method=\v!first,\c!align=\v!flushright,\s!parent=\??marginframed] % we could autoparent when no define yet
\setupmarginframed [\v!right] [\c!method=\v!first,\c!align=\v!flushleft, \s!parent=\??marginframed]
\setupmarginframed [\v!outer] [\c!method=\v!first,\c!align=\v!inner,     \s!parent=\??marginframed]
\setupmarginframed [\v!inner] [\c!method=\v!first,\c!align=\v!outer,     \s!parent=\??marginframed]

\definemarginframed [inleft]   [\v!left ]
\definemarginframed [inright]  [\v!right]
\definemarginframed [inouter]  [\v!outer]
\definemarginframed [ininner]  [\v!inner]
\definemarginframed [inmargin] [\v!inleft]
\definemarginframed [inother]  [\v!inright]

\let\marginword   \margintext
\let\margintitle  \margintext
\let\inothermargin\inother    % for old times sake

%definemargindata [inouterextra] [\v!outer] [\c!margin=\v!edge,\c!location=\v!outer,\c!width=\outeredgewidth,\c!align=\v!outer,\c!category=\v!edge]
%definemargindata [ininnerextra] [\v!inner] [\c!margin=\v!edge,\c!location=\v!inner,\c!width=\inneredgewidth,\c!align=\v!inner,\c!category=\v!edge]
%
%definemarginframed [inouterextra] [\v!outer]
%definemarginframed [ininnerextra] [\v!inner]

%D As we have more control we are not backward compatible although in
%D practice it won't hurt that much. So, from now on use:
%D
%D \starttyping
%D \definemargindata
%D \setupmargindata
%D \definemarginframed
%D \setupmarginframed
%D \stoptyping

% The following sort of works okay but is to be avoided:

\let\definemarginline\definemargindata

\unexpanded\def\defineinmargin
  {\doquadrupleempty\typo_inmargin_define}

\def\typo_inmargin_define[#name][#location][#align][#settings]% not completely compatible
  {\definemargindata[#name][\c!location=#location,\c!align=#align,#settings]%
   \definemarginframed[#name][#location][\c!align=#align,#settings]}

\let\setupinmargin\setupmargindata % only partial (no framed)

% begin of experimental code (will move)
%
% \dosetanchor{x}test \dostarthanchoring{x}\llap{crap}\dostophanchoring{x}test test test
% test \dostarthanchoring{text}\llap{crap}\dostophanchoring{text}test test test

% \def\dosetanchor      #1{\dontleavehmode\latelua{anchors.set("#1")}}
% \def\doresetanchor    #1{\dontleavehmode\latelua{anchors.reset("#1")}}
% \def\doresetanchornow #1{\directlua{anchors.reset("#1")}}
% \def\dostartanchoring #1{\dontleavehmode\latelua{anchors.startmove("#1")}}
% \def\dostopanchoring  #1{\dontleavehmode\latelua{anchors.stopmove("#1")}}
% \def\dostarthanchoring#1{\dontleavehmode\latelua{anchors.startmove("#1","h")}}
% \def\dostartvanchoring#1{\dontleavehmode\latelua{anchors.startmove("#1","v")}}
% \let\dostophanchoring    \dostopanchoring
% \let\dostopvanchoring    \dostopanchoring

\protect \endinput