tmac.fa: write the header only once for the last page
[neatroff_make.git] / demo / neatroff.ms
blobc4c8b6c3375e5cf058c72f38ed5c9080b743fcc6
1 .\" PSTITLE: Neatroff Introduction
2 .so neat__.ms
3 .ds en.cl "#237
4 .de MH
5 .       sp
6 .       LP
7 .       ne 1.5
8 \m[#237]\fI\\$1\fP\m[]
9 .       IP
11 .de NR
12 .       sp
13 .       LP
14 .       ne
15 .       ta 4.5i 6iR
16 .       nf
17 \f(CB\m[#237]\\$1\m0\fP \s-3\m[#237]\fB\\$2\fP  \fB\\$4\m0\fP
18 .       IP
20 .HD
21 .TL
22 \s+8Neatroff\m0\s-8
23 .AU
24 \fIAli Gholami Rudi\fP
25 .ce
26 \s-3\fIUpdated in June 2017\s+3\fP\&
27 .sp 3
28 Neatroff is a new implementation of Troff typesetting system in C
29 programming language, which tries to address, as neatly as possible,
30 some of the shortcomings of the original Troff based on the ideas and
31 features available in Plan 9 Troff, Heirloom Troff, and Groff.
32 Neatroff, its postscript postprocessor, neatpost, and its eqn
33 preprocessor, neateqn, are available at http:/\h'-.3n'/litcave.rudi.ir/.
34 This document enumerates Neatroff's features, its new requests, and
35 its differences compared to other Troff implementations.  On the other
36 hand, the document \(lqGetting Started with Neatroff,\(rq available at
37 http:/\h'-.3n'/litcave.rudi.ir/\:neatstart.pdf, explains how to set up and
38 use Neatroff.
40 .bp
41 .SH "Noteworthy Features"
42 The following list describes the main extensions of Neatroff compared
43 to the original Troff (many of these extensions are available in
44 Groff and Heirloom Troff as well).  The number register \&.neat,
45 which is always one in Neatroff, can be used to distinguish Neatroff
46 from other Troff implementations.
48 .MH "UTF-8 encoding
49 In Neatroff, input files and characters, glyph names, ligatures,
50 hyphenation patterns and dictionary, as well as quoted escape sequence
51 delimiters and arguments of commands like .tr, .hc, .tc, .lc, .mc, and
52 \&.fc are in UTF-8 encoding.
54 .MH "Long macro, register, and environment names
55 When not in compatibility mode (activated with -C command line option or the .cp
56 request, as in Groff), Neatroff supports long macro, register, and environment
57 names.  It also supports Groff-style escape sequences with long arguments
58 (for \\[], \\*[], \\$[], \\f[], \\g[], \\k[], \\m[], \\n[], and \\s[])
59 and interpolating string registers with arguments (\\*[xyz arg1 arg2 ...]).
60 Note that like Groff, Neatroff supports named environments and
61 is not limited to original Troff's three fixed environments.
63 .MH "Advanced font features, ligature, and pairwise kerning
64 Neatroff and neatmkfn (which generates Neatroff's font descriptions)
65 support many of the advanced font features available in modern fonts.
66 In a font, a set of substitution and positioning rules may be
67 specified, which are grouped into several features and scripts.  In
68 Neatroff, features can be enabled with \&.ff and the active script can
69 be selected with \&.ffsc.  Old-style ligatures (\&.lg) and pairwise
70 kerning (\&.kn) are still supported, as described in a later section.
72 .MH "Whole paragraph text formatting
73 Neatroff supports filling whole paragraphs at once, which results in
74 more uniform word spacing.  Like Heirloom Troff, the \&.ad request
75 accepts arguments p or pb, pl, and pr, which are equivalent to b, l
76 and r, except that the filling is done for whole paragraphs, i.e.,
77 words are collected until a line break is issued.  This inevitably
78 changes the behaviour of some requests, including traps: several
79 lines may be collected and ready to be output while executing them.
80 Also, when formatting whole paragraphs, \&'br fills the words
81 collected so far and outputs all resulting lines except the final
82 incomplete line.
84 .MH "Paragraph formatting algorithm
85 For deciding at what points to break a paragraph into lines, Neatroff
86 assigns a cost to each possible outcome: a cost of 100 is assigned to
87 each stretchable space that has to be stretched 100 percent.  The cost
88 grows quadratically and the cost of stretching a space 200 percent is 400.
89 There are requests that adjust the algorithm Neatroff uses for
90 performing paragraph formatting.
91 The \&.hycost request changes the cost of hyphenating words.  The
92 default value is zero.
93 The \\j escape sequence, as in Heirloom Troff, specifies the extra
94 cost of line break after a word; for instance, in \(lqHello\\j'10000'
95 world\(rq, the words are not split by the line breaking algorithm,
96 unless absolutely necessary (i.e., if other options are more costly).
97 The escape sequence \\\(ti introduces non-breakable stretchable space.
98 Also, to prevent paragraphs with
99 very short last lines, the \&.pmll (paragraph minimum line length)
100 sets the minimum length of a formatted line, specified as a
101 percentage of \\n(.l; \(lq.pmll 15\(rq, for instance, makes sure that
102 the length of last line of each paragraph is at least 15% of its other
103 lines; otherwise, a cost proportional to the value specified
104 as its second argument is added.
106 .MH "Controlling word spaces
107 The \&.ssh request sets the amount (in percentage) by which the
108 stretchable spaces in a line may be shrunk while formatting lines.
109 The default value is zero.
110 Also, the second
111 argument of \&.ss request specifies sentence space, as in Groff or
112 Heirloom Troff.
114 .MH "Text direction
115 The dir branch of Neatroff supports text direction to render
116 right-to-left languages.  The \&.<< and \&.>> requests specify text
117 direction and the \\< and \\> escape sequences change it temporarily
118 for including words in the reverse direction.  The value of number
119 registers \&.td and \&.cd indicate the current text and temporary
120 directions respectively; zero means left-to-right and one means
121 right-to-left.
123 .MH "Keshideh justification and cursive scripts
124 In the dir branch of Neatroff, a new adjustment type (.ad k) allows
125 inserting Keshideh characters before justifying text with hyphenation
126 and spaces.  The dir branch also supports cursive scripts, which
127 require connecting glyphs at their cursive attachment positions,
128 as defined in the fonts.
130 .MH "Font manipulation
131 In Neatroff, the mapping between Troff character names and glyphs in a
132 font can be modified with \&.fmap request: \(lq.fmap F C G\(rq
133 maps Troff character C to the glyph with device-specific name G for
134 font F.  When this glyph does not exist in F, Neatroff assumes that
135 the character C is not present in the font.  When G is missing, the
136 effect of \&.fmap for character C is cancelled.  Neatroff also implements
137 Groff's \&.fspecial and \&.fzoom requests: after \(lq.fspecial FN S1
138 S2 ...\(rq, when the current font is FN, the fonts S1, S2, ... are
139 assumed to be special.  Also, \(lq.fzoom FN zoom\(rq scales font FN by
140 the second argument after dividing it by 1000.
142 .MH "Colour support
143 Neatroff supports colours with .cl request and \\m[] escape sequence.
144 Unlike Groff, colours need not be defined beforehand and can be
145 specified directly.  The argument of \\m can be predefined colour
146 names (e.g. blue), predefined colour numbers (0 for black, 1 for red,
147 2 for green, 3 for yellow, 4 for blue, 5 for magenta, 6 for cyan, and
148 7 for white), #rgb and #rrggbb for specifying colours in hexadecimal
149 RGB format, #g and #gg for specifying grey with the given hexadecimal
150 level, and empty (\\m[]) for the previous colour.  The current colour
151 is available in \&.m number register.
153 .MH "Hyphenation language
154 The \&.hpf request loads hyphenation patterns, exceptions, and
155 character mappings from the addresses specified via its arguments.
156 The specified files should contain nothing but utf-8 patterns,
157 exceptions and mappings respectively (i.e. no TeX code), just like the
158 files whose names end with \&.pat.txt, \&.hyp.txt and \&.chr.txt in
159 ftp:/\h'-.3n'/ftp.ctan.org/\:tex-archive/\:language/\:hyph-utf8/\:tex/\:generic/\:hyph-utf8/\:patterns/\:txt/.
160 The \&.hpfa request is like \&.hpf, except that it does not clear the
161 previous hyphenation patterns and exceptions.  The second and third
162 arguments of these requests are optional.  With no arguments, these
163 requests load English hyphenation patterns and exceptions.  Also the
164 \&\(lq.hcode abcd...\(rq request, assigns the hyphenation code of b to
165 a and the hyphenation code of d to c; initially all upper-case ASCII
166 letters are mapped to their lower-case forms.
168 .MH "Filled drawing objects
169 Neatroff supports Groff-style polygons and filled drawing objects (p,
170 C, E and P commands for \\D escape sequence).  In Neatroff, however,
171 there is no specific background colour; objects are filled with the
172 current colour (.m number register).  Furthermore, in Neatroff
173 the edges of polygons can be lines, arcs, or splines; a letter among the
174 arguments of \\D'p ..' specifies the type of the subsequent
175 edges: \(oql\(cq, \(oqa\(cq, and \(oq\(ti\(cq for lines, arcs,
176 and splines respectively.
178 .MH "Conditional escape sequence
179 Neatroff supports a new escape sequence for conditional interpolation:
180 the escape sequence \\?'cond@expr1@expr2@', evaluates cond (exactly as
181 if it is a \&.if condition) and interpolates expr1, if the condition
182 is true, and expr2, otherwise.  The delimiter (@ in this example) can
183 be any character that cannot be part of the condition; for numerical
184 expressions, for instance, it cannot be a digit, an operator sign, or
185 a scale indicator, unless separated from the condition with \\&.  The
186 final delimiter, and even expr2, may be omitted, thus \\?'cond@expr'
187 is valid; Neatroff interpolates expr if cond is true.  Note that this
188 escape sequence is not interpolated in copy mode.
190 .MH "Neatpost-specific device commands
191 Neatroff supports \\X'rotate deg' for rotating the current page
192 around the current point.  Also \\X'eps file [width [height]]'
193 includes the given EPS file with its lower left corner at the current
194 point.  If width and height are given (in basic units), the file is
195 scaled appropriately.
198 .SH "Summary of New Requests"
199 This is the list of new request available in Neatroff compared
200 to those documented in \(lqTroff User's Manual\(rq by Ossanna
201 and Kernighan.
203 .NR "\&.cl C" "0" "previous" "E"
204 Change text colour.  The current colour is available in the number
205 register \\n(.m.  With no arguments, the previous colour is selected.
206 The format of the argument and the \\m escape sequence are described
207 in the previous section.
209 .NR "\&.ssh N" "0" "0" "E"
210 Set the amount stretchable spaces in formatted lines may be
211 shrunk in percentage (available through \\n[.ssh]).
213 .NR "\&.ad p*" "b" "adjust" "E"
214 With values pl, pr, pb, and p, this request instructs Neatroff to perform
215 whole-paragraph line formatting.  Also in dir branch of Neatroff, the
216 value k enables Keshideh justification (kp is the equivalent for
217 whole-paragraph formatting).
219 .NR "\&.eos S T" "S=\&.?!  T='"")]*" "none" "\-"
220 Specify sentence characters.  The first argument specifies
221 the characters that end a sentence and the second argument
222 specifies the characters ignored after them.
224 .NR "\&.ss M N" "M=12 N=12" "none" "E"
225 The second argument sets sentence space size (available in \\n[.sss]).
227 .NR "\&.fzoom F N" "1000" "none" "\-"
228 Magnify the given font by N/1000.
230 .NR "\&.ff F +F1 -F2" "\-" "none" "\-"
231 Enable or disable font features; the first argument specifies the
232 font and the rest of the arguments specify feature names,
233 prefixed with a plus to enable or a minus to disable.
234 When a feature is enabled, all substitution and positioning rules
235 of a font with that feature name are applied when laying out the
236 glyphs.
238 .NR "\&.ffsc F SC" "\-" "none" "\-"
239 Specify font's script.  A font description specifies a set of
240 rules for each script, grouped into several features.  With this
241 request, only the rules for the specified script are enabled.
242 By default, or when SC is missing, all scripts are selected.
244 .NR "\&.kn N" "1" "none" "E"
245 Enable or disable pairwise kerning (current value available through \\n[.kn]).
247 .NR "\&.fspecial F S1 S2" "\-" "none" "\-"
248 Set special fonts when the current font is F.
250 .NR "\&.fmap FN CH GID" "\-" "none" "\-"
251 Map Troff character CH to glyph with device dependent name GID for
252 font FN.  When gid is missing, the effect of mapping CH is cancelled.
253 Neatroff implicitly calls \&.fmap for all aliases in font descriptions
254 (character definitions whose second column is ").
256 .NR "\&.tkf FN S1 N1 S2 N2" "\-" "none" "\-"
257 Enable track kerning for font FN.  If the point size is at most
258 S1, the width of each character is increased by N1 points, if it
259 is at least S2, the width of each character is increased by N2
260 points, and if it is between S1 and S2, the width of each character
261 is increased by a value between N1 and N2, relative to the
262 difference between the current point size and S1.
264 .NR "\&.pmll N C" "0" "0" "E"
265 Set paragraph minimum line length in percentage.  To shorter lines,
266 Neatroff assigns a cost proportional to the value specified as the
267 second argument (or 100, if missing) when formatting paragraphs.
268 Number registers \\n[.pmll] and \\n[.pmllcost] store the
269 values passed to \&.pmll.
271 .NR "\&.hycost N N2 N3" "0" "none" "E"
272 Change the cost of hyphenating words when adjusting lines.
273 An argument of 100 assigns to each hyphenation the cost of stretching
274 a space one hundred percent while formatting.  The second and third
275 arguments specify additional costs for two and three consecutive
276 hyphenated lines (only when formatting whole paragraphs).
278 .NR "\&.hlm n" "0" "0" "E"
279 Set the maximum number of consecutive hyphenated lines (only when
280 formatting whole paragraphs).  The current value is available via
281 \\n[.hlm].  An argument of zero or a negative number implies no
282 limitation.
284 .NR "\&.hydash C" "\\\\:\\\\(hy\\\\(en\\\\(em-\\\\-\\\\(--" "none" "\-"
285 Specify the list of characters after which words may be broken (even
286 when hyphenation is disabled) without inserting hyphens.
288 .NR "\&.hystop C" "\\\\%" "none" "\-"
289 Specify hyphenation inhibiting characters.  Words containing any of
290 the given characters are not hyphenated, unless after dashes
291 (characters specified via \&.hydash) or hyphenation indicators (\\%).
293 .NR "\&.hpf H P C" "\-" "English" "\-"
294 Set hyphenation files for patterns, exceptions, and mappings.
295 With no arguments, loads English hyphenation patterns and exceptions.
297 .NR "\&.hpfa H P C" "\-" "English" "\-"
298 Like, \&.hpf, but do not clear the previous hyphenation patterns.
300 .NR "\&.hcode abcd..." "\-" "none" "\-"
301 Assign the hyphenation code of b to a and the hyphenation code of d to c.
303 .NR "\&.chop xx" "\-" "none" "\-"
304 Remove the last character of a string register.
306 .NR "\&.>>  \&.<<" "left-to-right" "\-" "E"
307 Render text in left-to-right or right-to-left direction (available
308 only in dir branch of Neatroff).  See the first section for
309 an explanation of escape sequences \\> and \\<.
311 .NR "\&.in2" "0" "previous" "E"
312 Right-side indentation (available only in dir branch of Neatroff).
313 The current right-side indentation is available in register \\n(.I.
315 .NR "\&.ti2" "0" "\-" "E"
316 Right-side temporary indentation (available only in dir branch of Neatroff).
318 .NR "\&.char C DEF" "\-" "\-" "\-"
319 Define Troff character C.
320 If DEF is missing, previous definitions for character C are removed.
322 .NR "\&.ochar FN C DEF" "\-" "\-" "\-"
323 Define Troff character C only for font FN.
324 If DEF is missing, previous definitions
325 for character C are removed.
327 .NR "\&.blm" "\-" "\-" "\-"
328 Specify the blank line macro.  If specified, each blank line is
329 treated as an invocation of this macro.
332 .SH "Notes"
333 .sp -1
334 .MH "Generating the output device
335 The neatmkfn program (available at http:/\h'-.3n'/litcave.rudi.ir/) generates
336 Neatroff font descriptions for AFM or TrueType fonts (OpenType fonts
337 are converted to TrueType first).  It includes a script to create
338 a complete output device for Neatroff.
340 .MH "Formatting equations with neateqn
341 Neateqn is an eqn preprocessor for Neatroff.  It implements many
342 of the extensions introduced in Groff's eqn preprocessor.  It
343 can use TeX's Computer Modern-style bracket-building symbols, if
344 available.
346 .MH "The standard macro packages
347 The standard Troff macro packages and a top-level build script to
348 obtain and install Neatroff are available in neatroff_make git
349 repository (available at http:/\h'-.3n'/litcave.rudi.ir/).
351 .MH "Missing requests
352 A few requests of the original Troff are not implemented:
353 \&.pi, .cf, .rd, .pm, .ul, .cu, .uf, \\H, and \\S.
355 .MH "Porting and distribution
356 Given that Neatroff, neatpost and neateqn can be compiled with Neatcc,
357 porting them to other Unix variants besides Linux should not be
358 difficult.  Note that Neatroff is released under the ISC licence.
360 .MH "List of OpenType font features
361 As mentioned in previous sections, font features can be enabled and
362 disabled with \&.ff request.  For the list of OpenType features in
363 general and their descriptions, see
364 https:/\h'-.3n'/en.wikipedia.org/\:wiki/\:List_of_typographic_features or
365 http:/\h'-.3n'/www.microsoft.com/\:typography/\:\s-1OTSPEC\s+1/\:featurelist.htm.
368 .SH "Font Description Files"
369 The format of font description files in Neatroff, although still mostly
370 backward compatible, has been slightly changed.  The value of special,
371 spacewidth, and ligatures parameters retain their old meanings; sizes
372 and name parameters are ignored, however.  The value of the fontname
373 parameter in Neatroff specifies the device name of the font
374 (e.g. Times-Roman); neatpost uses it to map Troff fonts to postscript fonts.
375 In the charset section, the forth field is always the
376 device-specific name of the glyph (accessible with \\N escape sequence)
377 and the optional fifth field specifies glyph's code (the fourth field
378 of the original Troff).
380 In addition to the old charset section of the original Troff, Neatroff
381 supports a new syntax for defining characters and kerning pairs.  Lines
382 starting with the word \(lqchar\(rq define characters (similar to lines in
383 the charset section) and lines starting with \(lqkern\(rq specify kerning pairs.
384 For the latter, \(lqkern\(rq is followed by three tokens: the name of the
385 first glyph, the name of the second glyph, and the amount of kerning
386 between them.  Specifying the name of glyphs (the fourth field after \(lqchar\(rq)
387 instead of character names allows specifying kerning pairs for glyphs
388 not mapped to any characters (may be later with \&.fmap request) or
389 specifying kerning pairs only once for all aliases of a character.
390 Here are a few lines of a font description file for Neatroff, created
391 with neatmkfn.
393 .cc.beg
394 .ta 5 10 15 20 25
395 name R
396 fontname Times-Roman
397 spacewidth 25
398 ligatures fi fl 0
399 # the list of characters
400 char    !       33      2       exclam  33
401 char    .       25      0       period  46
402 char    A       72      2       A       65
403 char    B       67      2       B       66
404 char    C       67      2       C       67
405 # the kerning pairs
406 kern    A       C       -5
407 kern    A       period  -1
408 .cc.end
410 The width column of the character definition lines can optionally
411 include four more numbers, separated with commas, that describe the
412 bounding boxes of the glyphs.  The bounding boxes are used in the \\w
413 escape sequence; after this escape sequence, the value of the bbllx,
414 bblly, bburx and bbury number registers are modified to represent the
415 bounding box of the argument of \\w.
417 To use the advanced features present in TrueType and OpenType fonts,
418 Neatroff supports lines that define substitution and positioning
419 rules, and cursive attachments (lines starting with \(lqgsub,\(rq
420 \(lqgpos,\(rq and \(lqgcur\(rq respectively).  Note that unlike
421 Heirloom Troff, which implements non-contextual single-character
422 substitutions, Neatroff implements many of the
423 more complex OpenType substitution and positioning features.
424 The following example shows how such features are defined
425 in Neatroff font descriptions:
427 .cc.beg
428 .ta 5 10 15 20 25
429 gsub liga:latn 4 -gl1 -gl2 -gl3 +gl123
430 gpos kern:latn 2 gl1:+0+0-5+0 gl2
431 .cc.end
433 In this example, the first line defines a 3-character ligature (with
434 feature name \(lqliga\(rq and script name \(lqlatn\(rq) and the second
435 defines pairwise kerning for the pair of glyphs gl1 and gl2
436 (decreasing the horizontal advance of gl1 by 5 basic units; with
437 feature name \(lqkern\(rq and script name \(lqlatn\(rq).  The patterns
438 can be longer and more detailed, defining context or glyph groups, to
439 support OpenType features that require them; for examples, see the
440 files generated by neatmkfn.
443 .SH "Source Code Organization
444 The following figure shows where Neatroff's major
445 layers and features are implemented in its source tree.
446 .sp -1
451 # part(title, description)
452 define part { [
453 M:      box ht 0.5 wid 2.3
454 .ps 15
455         $1 at 1/2 <M.w, M.nw> + (.2, 0) ljust
456 .ps 9
457         $2 at 1/2 <M.w, M.sw> + (.2, 0) ljust
458 .ps 20
459 ] }
460 # part2(title, description)
461 define part2 { [
462 M:      box ht 0.3 wid 2.6
463 .ps 13
464         $1 ljust at M.w + (.2, 0)
465 .ps 9
466         $2 ljust at M + (-.4, 0)
467 .ps 20
468 ] }
469 .ps 20
470 lineht = .3
471 HEAD:   "\s(17\fI\fP\s0"
472         down
473         move .3
474 IN:     part("in.c", "Input handling")
475         arrow
476 CP:     part("cp.c", "Copy-mode interpretation")
477         arrow
478 TR:     part("tr.c", "Troff request/macro execution")
479         arrow
480 REN:    part("ren.c", "Rendering, traps, and diversions")
481         arrow
482 OUT:    part("out.c", "Generating Troff output")
483         "\s(17\fI\fP\s0" at HEAD + (3, 0)
484         move .3
485 REG:    part2("reg.c", "Registers and environments")
486         move down 0.2
487 FMT:    part2("wb.c", "Word buffer")
488         move same
489         part2("eval.c", "Integer expression evaluation")
490         move same
491         part2("fmt.c", "Line formatting")
492         move same
493 DEV:    part2("dev.c", "Output device")
494         move same
495 FONT:   part2("font.c", "Fonts")
496         move same
497         part2("hyph.c", "Tex hyphenation")
498         move same
499         part2("dir.c", "Text direction")