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