Debian Codegraph @

texmf-dist/doc/luametatex/base/luametatex-principles.tex

raw
1 % language=us runpath=texruns:manuals/luametatex
2
3 \environment luametatex-style
4
5 \startdocument[title=principles]
6
7 \startsection[title=Introduction]
8
9 This is a bit odd manual but needed anyway. In the process of adding features to
10 \LUAMETATEX\ and adapting \CONTEXT\ accordingly some decisions were made. On the
11 one hand generic flexibility is a criterion used when the extending engine, on
12 the other hand practical usability in \CONTEXT\ is used to decide where to draw
13 a line or make some choices. It makes no sense to complicate the already complex
14 engine even more, or cripple \CONTEXT\ when cleaner (low level) solutions are
15 possible.
16
17 Here I will collect some of the considerations and mention the choices made.
18 These are mostly mine but some result from discussions and experiments. This
19 overview is not complete, new primitives are discussed elsewhere and the
20 \CONTEXT\ low level manuals explain how to use these. Consider this to be a
21 teaser.
22
23 {\em This summary is work in progress.}
24
25 \stopsection
26
27 \startsection[title=Text fonts]
28
29 Plenty has been written about fonts in \TEX, so here I will only mention a few
30 aspects. Traditionally the \TEX\ engines works with copies of fonts at given
31 sizes. For large fonts that is kind of inefficient. This is why in \LUAMETATEX\
32 we can scale a font on|-|the|-|fly using \typ {\glyphscale}, \typ {\glyphxscale}
33 and \typ {\glyphyscale}. This feature is also used to implement a more efficient
34 (although not 100\percent\ metric compatible) compact font mode. It works okay in
35 text as well as math although it comes at a price: many more calculations are
36 needed at the engine end.
37
38 One way to get an expanded, squeezed, emboldened or slanted font in \CONTEXT\ is
39 to use the effects mechanism. It is quite flexible but again comes at a price
40 because the backend has to do more work which is measurable, especially because
41 effects can apply to the font or individual glyphs. However, the advantages
42 out|-|weight the disadvantages. At the cost of yet a bit more performance a more
43 native variant is also available using \typ {\glyphslant} and \typ
44 {\glyphweight}.
45
46 \definefontfeature[extendtest] [default][extend=1.25]
47 \definefontfeature[squeezetest][default][squeeze=1.25]
48 \definefontfeature[slanttest] [default][slant=0.5]
49 \definefontfeature[weighttesta][default][weight=0.2] % 0.4pt is bad for the g in lm
50 \definefontfeature[weighttestb][default][effect={weight=0.2,auto=yes}]
51
52 \starttexdefinition demo #1
53 \startcombination[nx=6,ny=1,distance=12.5mm]
54 {\scale[scale=8000]{\definedfont[Serif*default @ 10bp]\ruledhbox{\color[maincolor]{#1}}}} {}
55 {\scale[scale=8000]{\definedfont[Serif*extendtest @ 10bp]\ruledhbox{\color[maincolor]{#1}}}} {}
56 {\scale[scale=8000]{\definedfont[Serif*squeezetest @ 10bp]\ruledhbox{\color[maincolor]{#1}}}} {}
57 {\scale[scale=8000]{\definedfont[Serif*slanttest @ 10bp]\ruledhbox{\color[maincolor]{#1}}}} {}
58 {\scale[scale=8000]{\definedfont[Serif*weighttesta @ 10bp]\ruledhbox{\color[maincolor]{#1}}}} {}
59 {\scale[scale=8000]{\definedfont[Serif*weighttestb @ 10bp]\ruledhbox{\color[maincolor]{#1}}}} {}
60 \stopcombination
61 \stoptexdefinition
62
63 \startlinecorrection \demo{a} \stoplinecorrection
64 \startlinecorrection \demo{x} \stoplinecorrection
65 \startlinecorrection \demo{p} \stoplinecorrection
66 %startlinecorrection \demo{(} \stoplinecorrection
67 \startlinecorrection \demo{g} \stoplinecorrection
68 \startlinecorrection \demo{-} \stoplinecorrection
69
70 Extending, as seen in the second renderings, scales the shapes horizontally,
71 while squeezing, in the third renderings, does it in the vertical direction. In
72 both cases the dimensions have to be adapted. This is not the case when we slant.
73 The last two samples in a row have an increased weight, and these are the more
74 tricky cases because here one can argue how to scale and reposition a shape. When
75 a shape is above the baseline we increase the height, and when it goes below we
76 increase the depth. The engine is capable to increase the width, height and depth
77 and shift the shape a little. It only makes sense to adapt the height and depth
78 when they are non|-|zero. It will never be perfect, but this feature is not
79 perfect anyway.
80
81 The way fonts are set up in a \TEX\ macro package often originates in the past,
82 if only because it came with fonts. The Computer Modern fonts are among the few
83 that have multiple design sizes. However, the collection is pretty much based on
84 a ten point design. For math there are seven and five point variants for the
85 script sizes, for footnotes an eight point makes sense and section heads can use
86 the larger twelve point plus the few larger sizes. Setting up a twelve point
87 body font environment, as we have in \CONTEXT, is quite doable with the fonts but
88 for an eleven point body font more compromised have to be made.
89
90 One can wonder why in \CONTEXT\ the ten point math setup of 10/7/5 became 12/9/7
91 instead if 12/8.4/6 and the reason is just that when there were still bitmap
92 fonts one didn't want too many (intermediate) sizes. Anyway, we're sort of stuck
93 with this default setup now, but nothing prevents users to redefine a body font
94 environment.
95
96 Another speciality of \TEX\ (fonts) is that they have italic correction, something
97 that lacks in \OPENTYPE\ fonts (apart from math btu there it serves a different
98 purpose). We can however emulate it, and in \CONTEXT\ that is an option. Given that
99 we have to make choices it is clear that the engine can only be supportive here,
100 especially when we use the \typ {\glyphslant} method.
101
102 A curious case is the following: in Computer Modern we find italic correction in
103 the upright fonts, for instance between an \quote {f} and \quote {h}. Dealing with
104 this automatically is impossible because italic correction is not to be applied
105 between glyph runs of the same font.
106
107 % quite some italics with not angle and uprights with angles
108 % why not proper boundingbox (right side bearing) with (now also small) width
109 % instead of unpredictable either or not urx
110
111 \stopsection
112
113 \startsection[title=Math fonts]
114
115 Support for math in an \UNICODE\ aware engine is also driven by the repertoire of
116 characters and their organization in \UNICODE, as well as by \OPENTYPE\ math as
117 cooked up by \MICROSOFT\ with a bit of input from \TEX\ folk.
118
119 The engine is agnostic when it comes to \UNICODE: there are no character codes
120 interpreted in special ways. There are math alphabets but these are not special:
121 in a traditional eight bit engine we have families to deal with them, in a
122 \UNICODE\ aware engine there are several solutions. The most important character
123 property that has some consequence is the math class but for dealing with that
124 we're on our own anyway. Everything \UNICODE\ related is up to \CONTEXT\ to deal
125 with, and it is the macro package that drives the engine, using the constructs
126 that are available, like atoms with specific classes, fractions, accents,
127 delimiters, fences, radicals, operators etc.
128
129 When it comes to fonts it is more complex. The \OPENTYPE\ math standard is driven
130 by the fact that \MSWORD\ provides a math editor and therefore needs a font. That
131 font is Cambria and it is (at the time of writing this) the only font that comes
132 from the origin. It has not been extended, nor fixed so basically what is in
133 there kind of has become the standard. The other \OPENTYPE\ math fonts are a
134 curious mix of old and new technology and again not much has happened there.
135
136 Now, when it comes to choices here, a few can be made based on conclusions drawn
137 during decades of dealing with these fonts and the assumed technology.
138
139 \startitemize
140
141 \startitem
142 There has be no real developments so we can just assume that what we got is
143 what we will have forever. Cambria is and remains the standard, quite some
144 fonts shipped with \TEX\ have issues that will stay, and new fonts,
145 especially when developed outside \TEX's scope likely also have issues,
146 because, after all, what is used for testing them?
147 \stopitem
148
149 \startitem
150 Only a few renders support the new technology. It is unlikely that \MSWORD\
151 will \ change and basically \XETEX\ and \LUATEX\ are also frozen. On the web
152 old school fonts are used, at least till 2023. Plenty of time went by since
153 the beginning of the century and nothing improved.
154 \stopitem
155
156 \startitem
157 The most important font properties that play a role are parameters, italic
158 correction, variants and extensibles, anchors for accents, stylistic
159 alternates, script alternates and staircase kerns. There are some rules of
160 how to apply italic correction, but many fonts make them unapplicable. The
161 same is true for anchors and kerns. There are only top kerns.
162 \stopitem
163
164 \startitem
165 Italic correction is a flawed concept and we decided to just ignore them:
166 when specified we add it to the width and discard them afterwards. The value
167 is translated into a bottom right corner kern. For large operators we
168 translate them to top and bottom accents.
169 \stopitem
170
171 \startitem
172 Top accents can be flawed so in many cases we can just ignore them. They
173 only make sense for italic shapes anyway.
174 \stopitem
175
176 \startitem
177 Staircase kerns are a nice idea but make no sense. First of all they concern
178 two characters, nucleus and script, but we can also have accents, fraction,
179 fenced stuff and other constructs in scripts so instead we prefer a system of
180 corner kerns. Also, we noticed that staircase kerns are often implemented
181 partially and even then not that well, probably because there was no way to
182 test them. Even worse is that when they are inconsistent formulas can look
183 rather inconsistent. So, we translate staircase kerns into corner kerns and
184 add and|/| overload them by corner kerns. These kerns can then be applied for
185 any reasonable combination.
186 \stopitem
187
188 \startitem
189 Extensible are mixed breed. Rules should be extensibles but aren't. Some snippets
190 have \UNICODE\ points so they can be used to construct missing glyphs but the
191 repertoire is inconsistent. Because we don't expect \UNICODE\ to adapt we
192 therefore provide alternative solutions.
193 \stopitem
194
195 \startitem
196 The repertoire of math parameters is on the one hand incomplete and on the
197 other hand less dependent on the font and more on intended usage. So, apart
198 from a few, we end up with adapting to our needs. It is part of the more granular
199 control that we wish.
200 \stopitem
201
202 \startitem
203 Gaps in alphabet vectors are a pain but the engine is agnostic of them. For some
204 reason the \TEX\ community let itself down on this so it has to cope at the macro
205 level. It is by now an old problem.
206 \stopitem
207
208 \stopitemize
209
210 So, to summarize the font part, an alternative standard could discard the concept
211 of italic correction and go for proper widths, a simplified corner kern model,
212 provide top and bottom accents, prescribe a repertoire of extensibles and
213 snippets and at least fill the gaps in alphabets instead of relying on shared
214 glyphs. It won't happen any time soon, but still we do follow that approach and
215 have the engine ready for it. Because we adapt the fonts runtime to this, we can
216 eventually remove all the code related to italic correction and staircase kerns,
217 simply because it is not used.
218
219 \stopsection
220
221 \startsection[title=Rules]
222
223 The original \TEX\ engine actually has only two graphical elements: glyphs and
224 rules. These have a width, height and depth and when decisions are made, for
225 instance when deciding where to break a line, or when boxes are constructed these
226 dimensions have to be known. Actually, \TEX\ doesn't really care what these
227 elements are, it's the dimensions that matter most. Graphics for instance can be
228 abstract objects, traditionally injected via so called specials wrapped into a
229 box of given dimensions. The \PDFTEX\ and later engines added a native
230 representation but basically it acted like a box (or rule if you like). It's the
231 backend that turns glyphs, rules and these special boxes into something that one
232 can see and print.
233
234 Rules have the three dimensions we mentioned. There are horizontal and vertical rules,
235 but only at the primives level. Once you specified an \type {\hrule} or \type {\vrule}
236 it became a generic rule with the main difference being the default dimensions. A rule
237 initializes with so called running dimensions, think of signals that the final dimension
238 comes from the encapsulating box.
239
240 Here we have a vertical rule: {\darkblue \vrule width 3cm height 5mm depth 2mm}
241 with width 3cm, height 5mm and depth 2mm. If we don't specify a width we get the
242 default thickness of 0.4pt, as in {\darkblue \vrule height 5mm depth 2mm} and when we
243 prefix it with \type {\leaders} and let it follow by a \type {\hfill} we get this:
244 {\darkblue \leaders \vrule height 5mm depth 2mm\hfill}.
245
246 When we put on an \type {\hrule} on an empty line the running width kicks in:
247 \crlf {\darkblue \hrule height 5mm depth 2mm}\crlf which is a feature that one
248 can use in for instance tables. However the fact that we only talk rectangles
249 means that there is only a limited repertoire of applications. In order to frame
250 some text you need four (disconnected) rules, For a background fill you can use a
251 single rule. There is also an application for rules that have height and depth
252 but no width: these so called struts that can enforce vertical spacing and
253 dimensions.
254
255 So what does \LUAMETATEX\ bring to the rules? Because the engine itself is only
256 interested in dimensions it's more about passing information to the backend. For
257 this we have a few more fields in the rule nodes that can be set from \LUA. This
258 permits for instance to hook in \METAPOST\ graphics that adapt like rules. There
259 are a few more primitives, one for making struts: they can take their dimensions
260 from a character. In math mode they're invisible and don't influence inter-atom
261 spacing but still take their role in determining dimensions. Then there are the virtual
262 rules that have dimensions (to be used in the backend) but don't contribute in the
263 frontend. The \type {\novrule} and \type {\nohrule} do contribute but are ignores in the
264 backend so they are cheap alternatives for empty boxes with specific dimensions set.
265
266 Some rule subtypes are set by the engine, for instance the math engine marks
267 over, under, fraction and radical rules. In \LUA\ one can mark outline, user, box
268 and image rules so that node list processors can take their properties into
269 accounT when needed, the frontend is only interested in the dimensions and sees
270 them as normal rules.
271
272 \startbuffer
273 \hrule height \strutht depth \strutdp on 0.04tw off 0.01tw \relax
274 \stopbuffer
275
276 \bgroup \nowhitespace
277 \strut\darkblue \getbuffer
278 \egroup
279
280 Here we have the following call:
281
282 \typebuffer
283
284 The \type {on} and \type {off} are among the new keys and they do nothing at the
285 \TEX\ end. It is the backend that will create the dash pattern. You can achieve
286 the same effect with leaders but while here we have a single rule, for a leader
287 the engine will make as many rules as are needed for this dash pattern. This is a
288 good example of adding little to the fontend in order to make the backend do the
289 job. In a similar fashion outlines are delegated. Other tricks involve offsets
290 and there is room for some additional features but for now they are on the
291 \quotation {Only when I need it.} list, after all we need something to wish for.
292
293 \stopsection
294
295 \startsection[title=Paragraphs]
296
297 A lot can be said about paragraphs but we keep it short here. Much more can be
298 found in for instance the articles that we wrote on the subject. When you enter
299 (or generate) text it will be added to a list (of nodes). That list can become a
300 horizontal box, vertical box, or end up in the main vertical list. When we go
301 vertical the list will be split in lines and the process is called line breaking.
302 Between the lines we can get penalties that tell the machinery how a paragraph of
303 lines can be split over page boundaries.
304
305 When breaking the engine can use up to three passes: a first pass that uses \type
306 {\pretolerance} as criterion, a tolerant pass with hyphenation enabled using
307 \type {\tolerance} and an emergency pass that kicks in \type {\emergencystretch}
308 when set. In \LUAMETATEX\ we can have additional passes that come online
309 depending on criteria and|/|or thresholds; search for \type {\parpasses} to learn
310 more about this.
311
312 The par builder in \LUAMETATEX\ has more features that users can control and also
313 normalized the resulting lines so that later on from the \LUA\ end they can be
314 manipulated easier. There are also ways to let embedded inserts, marks and (v)adjusts
315 migrate to the outer level. All this takes more runtime than in original \TEX\ but
316 in practice one won't really notice this because we gain in other places.
317
318 Most or what is new is available as features in \CONTEXT, most noticeably in
319 extra keys to \type {\setupalign}. It is also good to know that we have ways to
320 hook specific features in what is called \quote {wrapping up paragraph}. Also,
321 contrary to traditional \TEX\ we configured \CONTEXT\ to use the mechanism that
322 freezes paragraph specific parameters with the current paragraph so that there is
323 no (or at least less) interference with grouping.
324
325 \stopsection
326
327 \startsection[title=Pages]
328
329 {\em todo}
330
331 \stopsection
332
333 \startsection[title=Alignments]
334
335 {\em todo}
336
337 \stopsection
338
339 \startsection[title=Adjusts]
340
341 You can put stuff before and after lines using \type {\vadjust} and at the edges
342 using \type {\localleftbox} and alike. Both are seen in the par builder, where the
343 boxes contribute to the dimensions and the adjusted material is inserted when the
344 paragraph is wrapped up and contributed to the current list. In \LUAMETATEX\ these
345 mechanism have been extended so that we can actually uses them in am meaningful
346 way.
347
348 \stopsection
349
350
351 \startsection[title=Marks]
352
353 These signals in the text are used for managing (for instance) running headers
354 and a few extra features have been added, like migration to an outer level and
355 resets. In \MKIV\ we handled marks in \LUA\ but with \LUAMETATEX\ it makes sense
356 to use the engine.
357
358 \stopsection
359
360 \startsection[title=Inserts]
361
362 Inserts are signals that end up in lines and migrate to the outer level, that is
363 the main vertical list. An example of usage is footnotes. In the main vertical
364 list they are bound to the line they relate to so that the page builder can make
365 sure that they end up on the same page. In \LUAMETATEX\ they can bubble up from
366 deeply nested boxes. Contrary to the traditional binding of an insert class to
367 various registers in \LUAMETATEX\ they can be managed independently which means
368 that they have more properties.
369
370 \stopsection
371
372 \startsection[title=Boxes]
373
374 {\em todo}
375
376 \stopsection
377
378 \startsection[title=Language]
379
380 {\em todo}
381
382 \stopsection
383
384 \startsection[title=Math]
385
386 Plenty has been written about the multi-year project of opening up and extending
387 the math engine. Opening up and providing full control is part of supporting and
388 experimenting with \OPENTYPE\ math fonts but we already discussed this in a
389 previous section. Another aspect of opening up is making hard coded properties
390 configureable, even if that feature will hardly be used, simply because the
391 built-in defaults make sense. Then there is all kind of control over rendering
392 that can be controlled by keywords to the math specific elements like atoms,
393 fractions, operators, accents, radicals and fences.
394
395 Because traditional fonts are phased out in favour of (often flawed) \OPENTYPE\
396 variants much of what is new is also controlled by fonts, be it that we have our
397 own extensions. In \CONTEXT\ mathfonts are tweaked to fit our model. Inter atom
398 spacing, penalties, discretionaries, continuation scripts (think multiscripts,
399 pre and post), additional classes, dictionaries, linebreaks, carrying properties
400 over math groups, are all features that make it possible to renderer more precise
401 math without the need for manual intervention. It often looks, for instance from
402 posts on support platforms, that the more or less standard math has to come with
403 tweaking your source; it has become an accepted practice. In \CONTEXT\ we always
404 had structure and we added some more of that and because the math engine carries
405 more information around we could eventually simplify some code otherwise done in
406 \LUA.
407
408 By looking at what \CONTEXT\ actually needs, we could decide to strip down the
409 math engine (old as well as new features). We can also decide to eventually just
410 assume wide fonts to be used and drop old font support. After all, because one
411 has to load the fonts with \LUA, it's not hard to map traditional fonts to
412 (extended) \OPENTYPE\ alternatives, which is actually what we do anyway with for
413 instance Antykwa.
414
415 \stopsection
416
417 \startsection[title=Programming]
418
419 {\em todo}
420
421 \stopsection
422
423 \startsection[title=Protection]
424
425 The idea behind \TEX\ is that users define macros. However, when they do so in
426 the perspective of a macro package there is the danger that core functionality
427 can be overwritten. Now, one can of course make all primitives less accessible,
428 for instance by some prefix. But that makes no real sense for features that
429 belong to the language. When users use CamelCase for their names they're unlikely
430 to run into issues, so while internal macros are actually prefixed, we don't
431 do that with the primitives, so you can write code that looks \TEX.
432
433 Over time \CONTEXT\ has been ridiculed by non users for prefixing with \type
434 {\do} or \type {\dodo} but that's by folk who love long (cryptic) names with many
435 underscores and other inaccessible characters. The way we protect users from
436 accidental overloading is by using the \LUAMETATEX\ overload protection system.
437 Macros (and primitives) can be tagged in way so that the engine can issue warning
438 or even error in case of an undesirable definition.
439
440 There is of course some overhead involved in for instance every \type {\def} or
441 \type {\let} but it is little and the engine is fast anyway.
442
443 \stopsection
444
445 \startsection[title=Optimization]
446
447 There are many places where the engine could be optimized without getting
448 obscure. One reason is that the memory layout is somewhat different because we
449 snap to 8, 16, 32 or 64 bits and the engine being a \UNICODE\ capable one already
450 has more memory available in some places than what was needed. Also, knowing
451 usage patterns, it was possible to identify possible bottlenecks and widen the
452 necks.
453
454 Furthermore, it was possible to improve input handling, logging, save stack
455 usage, keyword parsing, expressions, and much more. On the other hand nodes
456 became larger so there we loose some. The \LUAMETATEX\ engine is faster than
457 \LUATEX, although some of the gain is lost on the fact that one needs to use
458 \LUA\ backend.
459
460 \stopsection
461
462 \startsection[title=Input]
463
464 The input can come from files, token lists, macros and \LUA\ which means many
465 places. When it comes from \LUA\ it can be tokens, nodes, string, and each has
466 its special way of handling and the engine has to keep track of this when it
467 accumulated the input that pops up after a \LUA\ call. This is done as efficient
468 as possible without sacrificing performance. The fact that we have \UTF\ should
469 not have too much impact.
470
471 \stopsection
472
473 \startsection[title=Nesting]
474
475 When you enter a group a stack boundary is set and when some value changes the
476 original value is pushed on the stack. After leaving the group values are
477 restored. The engine tries to avoid redundant actions which improves memory usage
478 and runtime.
479
480 Every macro expansion, opened file, expanded token list, etc.\ pushes the input
481 stack and that comes with overhead. Again we have tried to minimize the impact
482 and thereby gain a bit over \LUATEX.
483
484 Other stacks like those used by math, alignment, conditionals, expressions etc.\
485 have also been improved some. On the other hand, by unweaving some shared code
486 there can be a price to pay, but as with everything usage patterns indicate no
487 penalty here.
488
489 \stopsection
490
491 \startsection[title=Conditions]
492
493 We already had more conditionals in \LUATEX\ but again the repertoire of
494 conditionals has been extended. This permits us to remove some middle-layer
495 helpers and stay closer to the core language. It also helps to improve
496 performance.
497
498 Another important addition has been \type {\orelse} than permits us to write test
499 in a way similar to what other language provide with for instance \type {elseif}
500 or \type {else if}.
501
502 \stopsection
503
504 \startsection[title=Macros]
505
506 Expanding macros happens a lot especially in a more complex macro package. This
507 means that adding features in that area can have a large impact on runtime.
508 Nevertheless the argument parser now provides a few handfuls of variants in
509 picking up arguments with out noticeable degradation, especially because these
510 new features can gain performance.
511
512 At the same time there have been some optimizations in storing macro related
513 states, checking and accessing parameters. There are additional (internal)
514 classes of macros that make for a more natural implementation; for instance
515 \type {\protected} macros are now first class commands.
516
517 \stopsection
518
519 \startsection[title=Keywords]
520
521 Some primitives accept one or more keywords and \LUAMETATEX\ adds some more. In
522 order to deal with this efficiently the keyword scanner has been optimized, where
523 even the context was taken into account. As a result the scanner was quite a bit
524 faster. This kind of optimization was a graduate process the eventually ended up
525 in what we have now. In traditional \TEX\ (and also \LUATEX) the order of
526 keywords is sometimes mixed and sometimes prescribed. In most cases only one
527 occurrence is permitted. So, for instance, this is valid in \LUATEX:
528
529 \starttyping
530 \hbox attr 123 456 attr 123 456 spread 10cm { }
531 \hrule width 10cm depth 3mm
532 \hskip 3pt plus 2pt minus 1pt
533 \stoptyping
534
535 The \type {attr} comes before the \type {spread}, rules can have multiple mixed
536 dimension specifiers, and in glue the optional \type {minus} part always comes
537 last. The last two commands are famous for look ahead side effects which is why
538 macro packages will end them with something not keyword, like \type {\relax},
539 when needed.
540
541 In \LUAMETATEX\ the following is okay. Watch the few more keywords in box and
542 rule specifications.
543
544 \starttyping
545 \hbox reverse to 10cm attr 123 456 orientation 4 xoffset 10pt spread 10cm { }
546 \hrule xoffset 10pt width 10cm depth 3mm
547 \hskip 3pt minus 1pt plus 2pt
548 \stoptyping
549
550 Here the order is not prescribed and, as demonstrated with the box specifier, for
551 instance dimensions (specified by \type {to} or \type {spread} can be overloaded
552 by later settings. In case you wonder if that breaks compatibility: in some way
553 it does but bad or sloppy keyword usage breaks a run anyway. For instance \type
554 {minuscule} results in \type {minus} with no dimension being seen. So, in the end
555 the user should not noticed it and when a user does, the macro package already
556 had an issue that had to be fixed.
557
558 \stopsection
559
560 \startsection[title=Directions]
561
562 The directional model in \LUAMETATEX\ is a simplified version the the model used
563 in \LUATEX. In fact, not much is happening at all: we only register a change in
564 direction. The approach is that we try to make node lists balanced but also try
565 to avoid some side effects. What happens is quite intuitive if we forget about
566 spaces (turned into glue) but even there what happens makes sense if you look at
567 it in detail. However that logic makes in|-|group switching kind of useless when
568 no properly nested grouping is used: switching from right to left several times
569 nested, results in spacing ending up after each other due to nested mirroring. Of
570 course a sane macro package will manage this for the user but here we are
571 discussing the low level injection of directional information.
572
573 This is what happens:
574
575 \starttyping
576 \textdirection 1 nur {\textdirection 0 run \textdirection 1 NUR} nur
577 \stoptyping
578
579 This becomes stepwise:
580
581 \startnarrower
582 \starttyping
583 injected: [push 1]nur {[push 0]run [push 1]NUR} nur
584 balanced: [push 1]nur {[push 0]run [pop 0][push 1]NUR[pop 1]} nur[pop 0]
585 result : run {RUNrun } run
586 \stoptyping
587 \stopnarrower
588
589 And this:
590
591 \starttyping
592 \textdirection 1 nur {nur \textdirection 0 run \textdirection 1 NUR} nur
593 \stoptyping
594
595 becomes:
596
597 \startnarrower
598 \starttyping
599 injected: [+TRT]nur {nur [+TLT]run [+TRT]NUR} nur
600 balanced: [+TRT]nur {nur [+TLT]run [-TLT][+TRT]NUR[-TRT]} nur[-TRT]
601 result : run {run RUNrun } run
602 \stoptyping
603 \stopnarrower
604
605 Now, in the following examples watch where we put the braces:
606
607 \startbuffer
608 \textdirection 1 nur {{\textdirection 0 run} {\textdirection 1 NUR}} nur
609 \stopbuffer
610
611 \typebuffer
612
613 This becomes:
614
615 \startnarrower
616 \getbuffer
617 \stopnarrower
618
619 Compare this to:
620
621 \startbuffer
622 \textdirection 1 nur {{\textdirection 0 run }{\textdirection 1 NUR}} nur
623 \stopbuffer
624
625 \typebuffer
626
627 Which renders as:
628
629 \startnarrower
630 \getbuffer
631 \stopnarrower
632
633 So how do we deal with the next?
634
635 \startbuffer
636 \def\ltr{\textdirection 0\relax}
637 \def\rtl{\textdirection 1\relax}
638
639 run {\rtl nur {\ltr run \rtl NUR \ltr run \rtl NUR} nur}
640 run {\ltr run {\rtl nur \ltr RUN \rtl nur \ltr RUN} run}
641 \stopbuffer
642
643 \typebuffer
644
645 It gets typeset as:
646
647 \startnarrower
648 \startlines
649 \getbuffer
650 \stoplines
651 \stopnarrower
652
653 We could define the two helpers to look back, pick up a skip, remove it and
654 inject it after the dir node. But that way we loose the subtype information that
655 for some applications can be handy to be kept as|-|is. This is why we now have a
656 variant of \prm {textdirection} which injects the balanced node before the skip.
657 Instead of the previous definition we can use:
658
659 \startbuffer[def]
660 \def\ltr{\linedirection 0\relax}
661 \def\rtl{\linedirection 1\relax}
662 \stopbuffer
663
664 \typebuffer[def]
665
666 and this time:
667
668 \startbuffer[txt]
669 run {\rtl nur {\ltr run \rtl NUR \ltr run \rtl NUR} nur}
670 run {\ltr run {\rtl nur \ltr RUN \rtl nur \ltr RUN} run}
671 \stopbuffer
672
673 \typebuffer[txt]
674
675 comes out as a properly spaced:
676
677 \startnarrower
678 \startlines
679 \getbuffer[def,txt]
680 \stoplines
681 \stopnarrower
682
683 Anything more complex that this, like combination of skips and penalties, or
684 kerns, should be handled in the input or macro package because there is no way we
685 can predict the expected behavior. In fact, the \prm {linedirection} is just a
686 convenience extra which could also have been implemented using node list parsing.
687
688 Directions are complicated by the fact that they often need to work over groups
689 so a separate grouping related stack is used. A side effect is that there can be
690 paragraphs with only a local par node followed by direction synchronization
691 nodes. Paragraphs like that are seen as empty paragraphs and therefore ignored.
692 Because \prm {noindent} doesn't inject anything but a \prm {indent} injects
693 an box, paragraphs with only an indent and directions are handles and paragraphs
694 with content. When indentation is normalized a paragraph with an indentation
695 skip is seen as content.
696
697 \stopsection
698
699 \startsection[title=Hooks]
700
701 {\em todo}
702
703 \stopsection
704
705 \startsection[title=Expressions]
706
707 {\em todo}
708
709 \stopsection
710
711 \startsection[title=Units]
712
713 The familiar \TEX\ units like \type {pt} and \type {cm} are supported but since
714 the 2021 \CONTEXT\ meeting we also support the Knuthian Potrzebie, cf.\ \typ
715 {en.wikipedia.org/wiki/Potrzebie}. The two character acronym is \type {dk}. One
716 \type {dk} is 6.43985pt. This unit is particularly suited for offsets in framed
717 examples.
718
719 In 2023 we added the Edith (\type {es}) and Tove (\type {ts}) as metric
720 replacements for the inch (\type {in}). As with the \type {dk} more background
721 information can be found in documents that come with \CONTEXT\ and user group
722 journals. The \type {eu} unit starts out as one \type {es} but can be scaled with
723 \prm {eufactor}.
724
725 \startbuffer
726 \localcontrolledloop -5 55 5 {
727 \eufactor=\currentloopiterator
728 \dontleavehmode\strut
729 \vrule height .1es depth .25ts width 1dk\relax\quad
730 \vrule height .1es depth .25ts width 1eu\relax\quad
731 \the\currentloopiterator
732 \par
733 }
734 \stopbuffer
735
736 \typebuffer
737
738 This example code shows all four new units. Watch how \prm {eufactor} is clipped
739 to a value in the range $1-50$. The default factor of $10$ makes the European
740 Unit equivalent to ten Toves or one Edith.
741
742 \startpacked
743 \startcolor[darkgray]
744 \getbuffer
745 \stopcolor
746 \stoppacked
747
748 In addition to these there can be user units but because these are macro package
749 dependent they are not discussed here.
750
751 \stopsection
752
753 \startsection[title=Local control]
754
755 There are a few new primitives that permit what we call local controlled
756 expansion. This permits for instance expanding non expandable macros and even
757 typesetting inside an expansion context like \type {\edef}. Regular \TEX has a
758 main loop to where it returns after every primitive action, but local control let
759 the engine go into a nested main loop.
760
761 \stopsection
762
763 \startsection[title=Overload protection]
764
765 Protection is achieved via prefixes. Depending on the value of the \prm
766 {overloadmode} variable warnings or errors will be triggered. Examples of usage
767 can be found in some documents that come with \CONTEXT, so here we just stick to
768 the basics.
769
770 \starttyping
771 \mutable \def\foo{...}
772 \immutable\def\foo{...}
773 \permanent\def\foo{...}
774 \frozen \def\foo{...}
775 \aliased \def\foo{...}
776 \stoptyping
777
778 A \prm {mutable} macro can always be changed contrary to an \prm {immutable} one.
779 For instance a macro that acts as a variable is normally \prm {mutable}, while a
780 constant can best be immutable. It makes sense to define a public core macro as
781 \prm {permanent}. Primitives start out a \prm {permanent} ones but with a primitive
782 property instead.
783
784 \startbuffer
785 \let\relaxone \relax 1: \meaningfull\relaxone
786 \aliased \let\relaxtwo \relax 2: \meaningfull\relaxtwo
787 \permanent\let\relaxthree\relax 3: \meaningfull\relaxthree
788 \stopbuffer
789
790 \typebuffer
791
792 The \prm {meaningfull} primitive is like \prm {meaning} but report the
793 properties too. The \prm {meaningless} companion reports the body of a macro.
794 Anyway, this typesets:
795
796 \startlines \tttf \getbuffer \stoplines
797
798 So, the \prm {aliased} prefix copies the properties. Keep in mind that a macro
799 package can redefine primitives, but \prm {relax} is an unlikely candidate.
800
801 There is an extra prefix \prm {noaligned} that flags a macro as being valid
802 for \prm {noalign} compatible usage (which means that the body must contain that
803 one. The idea is that we then can do this:
804
805 \starttyping
806 \permanent\protected\noaligned\def\foo{\noalign{...}} % \foo is unexpandable
807 \stoptyping
808
809 that is: we can have protected macros that don't trigger an error in the parser
810 where there is a look ahead for \prm {noalign} which is why normally protection
811 doesn't work well. So: we have macro flagged as permanent (overload protection),
812 being protected (that is, not expandable by default) and a valid equivalent of
813 the noalign primitive. Of course we can also apply the \prm {global} and \prm
814 {tolerant} prefixes here. The complete repertoire of extra prefixes is:
815
816 \starttabulate
817 \FL
818 \NC \type {frozen} \NC a macro that has to be redefined in a managed way \NC \NR
819 \NC \type {permanent} \NC a macro that had better not be redefined \NC \NR
820 \NC \type {primitive} \NC a primitive that normally will not be adapted \NC \NR
821 \NC \type {immutable} \NC a macro or quantity that cannot be changed, it is a constant \NC \NR
822 \NC \type {mutable} \NC a macro that can be changed no matter how well protected it is \NC \NR
823 \ML
824 \NC \type {instance} \NC a macro marked as (for instance) be generated by an interface \NC \NR
825 \ML
826 \NC \type {noaligned} \NC the macro becomes acceptable as \type {\noalign} alias \NC \NR
827 \ML
828 \NC \type {overloaded} \NC when permitted the flags will be adapted \NC \NR
829 \NC \type {enforced} \NC all is permitted (but only in zero mode or ini mode) \NC \NR
830 \NC \type {aliased} \NC the macro gets the same flags as the original \NC \NR
831 \ML
832 \NC \type {untraced} \NC the macro gets a different treatment in tracing \NC \NR
833 \LL
834 \stoptabulate
835
836 The not yet discussed \prm {instance} is just a flag with no special meaning
837 which can be used as classifier. The \prm {frozen} also protects against overload
838 which brings amount of blockers to four.
839
840 To what extent the engine will complain when a property is changed in a way that
841 violates the flags depends on the parameter \prm {overloadmode}. When this
842 parameter is set to zero no checking takes place. More interesting are values
843 larger than zero. If that is the case, when a control sequence is flagged as
844 mutable, it is always permitted to change. When it is set to immutable one can
845 never change it. The other flags determine the kind of checking done. Currently
846 the following overload values are used:
847
848 \starttabulate[|l|l|c|c|c|c|c|]
849 \NC \NC \BC immutable \BC permanent \BC primitive \BC frozen \BC instance \NC \NR
850 \NC 1 \NC warning \NC \star \NC \star \NC \star \NC \NC \NC \NR
851 \NC 2 \NC error \NC \star \NC \star \NC \star \NC \NC \NC \NR
852 \NC 3 \NC warning \NC \star \NC \star \NC \star \NC \star \NC \NC \NR
853 \NC 4 \NC error \NC \star \NC \star \NC \star \NC \star \NC \NC \NR
854 \NC 5 \NC warning \NC \star \NC \star \NC \star \NC \star \NC \star \NC \NR
855 \NC 6 \NC error \NC \star \NC \star \NC \star \NC \star \NC \star \NC \NR
856 \stoptabulate
857
858 The even values (except zero) will abort the run. A value of 255 will freeze this
859 parameter. At level five and above the \prm {instance} flag is also checked but
860 no drastic action takes place. We use this to signal to the user that a specific
861 instance is redefined (of course the definition macros can check for that too).
862
863 The \prm {overloaded} prefix can be used to overload a frozen macro. The \prm
864 {enforced} is more powerful and forces an overload but that prefix is only
865 effective in ini mode or when it's embedded in the body of a macro or token list
866 at ini time unless of course at runtime the mode is zero.
867
868 So far for a short explanation. More details can be found in the \CONTEXT\
869 documentation where we can discuss it in a more relevant perspective. It must be
870 noted that this feature only makes sense a controlled situation, that is: user
871 modules or macros of unpredictable origin will probably suffer from warnings and
872 errors when de mode is set to non zero. In \CONTEXT\ we're okay unless of course
873 users redefine instances but there a warning or error is kind of welcome.
874
875 There is an extra prefix \prm {untraced} that will suppress the meaning when
876 tracing so that the macro looks more like a primitive. It is still somewhat
877 experimental so what gets displayed might change.
878
879 The \prm {letfrozen}, \prm {unletfrozen}, \prm {letprotected} and \prm
880 {unletprotected} primitives do as their names advertise. Of course the \prm
881 {overloadmode} must be set so that it is permitted.
882
883 \stopsection
884
885 \startsection[title=Tracing]
886
887 There is are more tracing options, like in math, alignments and inserts, and
888 tracing can be more detailed. This is partly a aide effect of the need for
889 exploring new features. Tracing is not always compatible, if only because there
890 are more possibilities, for instance in the way macros are defined and can handle
891 arguments.
892
893 \stopsection
894
895 \stopdocument
896
897 % \setupbodyfont[pagella]
898 % \starttext
899 % \definefont[TestA][SerifBold*default,oldstyle]
900 % \def\TestB {{\infofont[\the\glyphdatafield]}}
901 % \def\TestC#1{{\infofont(#1)}\csname#1\endcsname}
902 % \bgroup
903 % \crlf abc 123\crlf
904 % \bgroup
905 % \TestA
906 % \TestB
907 % \crlf \strut \kern10pt abc 123 \crlf
908 % \egroup
909 % \TestB
910 % \TestC{smallcaps}%
911 % \TestB
912 % \crlf abc 123 \crlf
913 % \bgroup
914 % \TestA
915 % \TestB
916 % \crlf \strut \kern10pt abc 123 \crlf
917 % \egroup
918 % \TestB
919 % \bgroup
920 % \TestC{oldstyle}%
921 % \TestB
922 % \crlf \strut \kern10pt abc 123 \crlf
923 % \egroup
924 % \TestB
925 % \crlf abc 123 \crlf
926 % \egroup
927 % \stoptext