Imported Upstream version 1.23.0
[platform/upstream/groff.git] / doc / pic.ms
1 .\" Copyright (C) 2006-2020 Free Software Foundation, Inc.
2 .\"      Written by Eric S. Raymond <esr@thyrsus.com>
3 .\"
4 .\" This file is part of groff.
5 .\"
6 .\" groff is free software; you can redistribute it and/or modify it under
7 .\" the terms of the GNU General Public License as published by the Free
8 .\" Software Foundation, either version 3 of the License, or
9 .\" (at your option) any later version.
10 .\"
11 .\" groff is distributed in the hope that it will be useful, but WITHOUT ANY
12 .\" WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 .\" FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 .\" for more details.
15 .\"
16 .\" You should have received a copy of the GNU General Public License
17 .\" along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 .\"
19 .\" For tolerably obvious reason, this needs to be processed through PIC.
20 .\" It also needs to be processed through TBL and EQN.  Use "groff -p -e -t".
21 .\" There is no hope that this will ever look right under nroff.
22 .\"
23 .\" Comments beginning with %% are cut lines so portions of this
24 .\" document can be automatically extracted.  %%TUTORIAL%% begins the
25 .\" tutorial part; %%REFERENCE%% the reference part.  %%POSTLUDE%% the
26 .\" bibliography and end matter after the reference part.
27 .\"
28 .\" This document was written for free use and redistribution by
29 .\" Eric S. Raymond <esr@thyrsus.com> in August 1995.  It has been put
30 .\" under the GPL in March 2006.
31 .\"
32 .
33 .
34 .\" Set a proper TeX and LaTeX
35 .ie t \{\
36 .  ds tx T\h'-.1667m'\v'.224m'E\v'-.224m'\h'-.125m'X\"
37 .  ds lx L\h'-0.36m'\v'-0.22v'\s-2A\s0\h'-0.15m'\v'0.22v'\*(tx\"
38 .\}
39 .el \{\
40 .  ds tx TeX\"
41 .  ds lx LaTeX\"
42 .\}
43 .
44 .\" Centered caption for figure.  Assumes previous .KS
45 .de CE
46 .  ce 1
47 Figure \\n[H1]-\\$1
48 .  sp 1
49 .  KE
50 ..
51 .
52 .\" Definitions end here
53 .
54 .
55 .TL
56 Making Pictures With GNU PIC
57 .AU
58 Eric S. Raymond
59 .AI
60 \[la]\fIesr@snark.thyrsus.com\fP\[ra]
61 .AB
62 The \fBpic\fP language is a \fBtroff\fP extension that makes it easy
63 to create and alter box-and-arrow diagrams of the kind frequently used
64 in technical papers and textbooks.
65 This paper is both an introduction to and reference for \fIgpic\/\fP(1),
66 the implementation distributed by the Free Software Foundation for use
67 with \fIgroff\/\fP(1).
68 It also catalogs other implementations and explains the differences
69 among them.
70 .AE
71 .\"%%TUTORIAL%%
72 .
73 .
74 .NH 1
75 Introduction to PIC
76 .
77 .NH 2
78 Why PIC?
79 .PP
80 The \fBpic\fP language provides an easy way to write procedural
81 box-and-arrow diagrams to be included in \fBtroff\fP documents.
82 The language is sufficiently flexible to be quite useful for state
83 charts, Petri-net diagrams, flow charts, simple circuit schematics,
84 jumper layouts, and other kinds of illustration involving repetitive
85 uses of simple geometric forms and splines.
86 Because these descriptions are procedural and object-based, they are
87 both compact and easy to modify.
88 .PP
89 The phrase \[lq]GNU pic\[rq] may refer to either of two \fBpic\fP
90 implementations distributed by the Free Software Foundation and
91 intended to accept the same input language.
92 The \fIgpic\/\fP(1) implementation is for use with the \fIgroff\/\fP(1)
93 implementation of \fBtroff\fP.
94 The \fIpic2plot\/\fP(1) implementation runs standalone and is part of
95 the \fBplotutils\fR package.
96 Because both implementations are widely available in source form for
97 free, they are good bets for writing very portable documentation.
98 .
99 .NH 2
100 PIC Versions
101 .PP
102 The original 1984 pre-\fIditroff\/\fP(1) version of \fBpic\fP is long
103 obsolete.
104 The rewritten 1991 version is still available as part of the
105 Documenter's Work Bench module of System V.
106 .PP
107 Where differences between Documenter's Work Bench (1991) \fBpic\fP and GNU
108 \fBpic\fP need to be described, original \fBpic\fP is referred to as
109 \[lq]DWB pic\[rq].
110 Details on the history of the program are given at the end of this
111 document.
112 .PP
113 The \fBpic2plot\fR program does not require the rest of the
114 \fIgroff\/\fP(1) toolchain to render graphics.
115 It can display \fBpic\fR diagrams in an X\~window, or generate output
116 plots in a large number of other formats.
117 These formats include: PNG, PBM, PGM, PPM, GIF, SVG, Adobe Illustrator
118 format, idraw-editable Postscript, the WebCGM format for Web-based
119 vector graphics, the format used by the \fBxfig\fP drawing editor, the
120 Hewlett-Packard PCL\~5 printer language, the Hewlett-Packard Graphics
121 Language (by default, HP-GL/2), the ReGIS (remote graphics instruction
122 set) format developed by DEC, Tektronix format, and device-independent
123 GNU graphics metafile format.
124 .PP
125 In this document, \fIgpic\/\fP(1) and \fIpic2plot\/\fP(1) extensions are
126 marked as such.
127 .
128 .
129 .NH 1
130 Invoking PIC
131 .PP
132 Every \fBpic\fP description is a little program describing drawing
133 actions.
134 The \fB[gtn]roff\fP-dependent versions compile the program by
135 \fIpic\/\fP(1) into \fIgtroff\/\fP(1) macros; the \fIpic2plot\/\fP(1)
136 implementation uses a plotting library to draw the picture directly.
137 Programs that process or display
138 \fIgtroff\/\fP(1) output need not know or care that parts of the image
139 began life as \fBpic\fP descriptions.
140 .PP
141 The \fIpic\/\fP(1) program tries to translate anything between \fB.PS\fP
142 and \fB.PE\fP markers, and passes through everything else.
143 The normal definitions of \fB.PS\fP and \fB.PE\fP in the \fIms\fP macro
144 package and elsewhere have also the side-effect of centering the
145 \fBpic\fP output on the page.
146 .
147 .KS
148 .NH 2
149 PIC Error Messages
150 .PP
151 If you make a mistake in \fBpic\fP input such that the program cannot
152 interpret what you mean,
153 \fIgpic\/\fP(1) issues a diagnostic message in the format prescribed by
154 the GNU Coding Standards.
155 .
156 A typical error message looks like
157 .
158 .
159 .ds FAM C
160 .DS
161 pic:\fIfile\fP:\fInnn\fP: syntax error before \[aq]\fItoken\fP\[aq]
162 pic:\fIfile\fP:\fInnn\fP: giving up on this picture
163 .DE
164 .ds FAM T
165 .
166 .
167 .LP
168 where
169 .I file
170 is the name of the file being processed,
171 .I nnn
172 is a line number within that file,
173 and
174 .I token
175 is a symbol in the \fBpic\fP language
176 (often a keyword)
177 near
178 (usually just after)
179 the error location.
180 .KE
181 .
182 .
183 .NH 1
184 Basic PIC Concepts
185 .PP
186 Pictures are described procedurally, as collections of objects
187 connected by motions.
188 Normally, \fBpic\fP tries to string together objects left-to-right in
189 the sequence they are described, joining them at visually natural
190 points.
191 Here is an example illustrating the flow of data in \fBpic\fP
192 processing:
193 .KS
194 .PS
195 ellipse "document";
196 arrow;
197 box width 0.6 "\fIgpic\/\fP(1)"
198 arrow;
199 box width 1.1 "\fIgtbl\/\fP(1) or \fIgeqn\/\fP(1)" "(optional)" dashed;
200 arrow;
201 box width 0.6 "\fIgtroff\/\fP(1)";
202 arrow;
203 ellipse "PostScript"
204 .PE
205 .CE "1: Flow of \fBpic\fP data"
206 .PP
207 This was produced from the following \fBpic\fP program:
208 .KS
209 .DS
210 .ps -1
211 .vs -1
212 .CW
213 \&.PS
214 ellipse "document";
215 arrow;
216 box width 0.6 "\efIgpic\e/\efP(1)"
217 arrow;
218 box width 1.1 "\efIgtbl\e/\efP(1) or \efIgeqn\e/\efP(1)" "(optional)" dashed;
219 arrow;
220 box width 0.6 "\efIgtroff\e/\efP(1)";
221 arrow;
222 ellipse "PostScript"
223 \&.PE
224 .DE
225 .R
226 .KE
227 .LP
228 This little program illustrates several \fBpic\fP basics.
229 Firstly, we see how to invoke three object types; ellipses, arrows, and
230 boxes.
231 We see how to declare text lines to go within an object (and that text
232 can have font changes in it).
233 We see how to change the line style of an object from solid (the
234 default) to dashed.
235 And we see that a box can be made wider than its default size to
236 accommodate more text (we'll discuss this facility in detail in the next
237 section).
238 .PP
239 We also get to see \fBpic\fP's simple syntax.
240 Statements are ended by newlines or semicolons.
241 String quotes are required around all text arguments, whether or not
242 they contain spaces.
243 In general, the order of command arguments and modifiers like \[lq]width
244 1.2\[rq] or \[lq]dashed\[rq] doesn't matter, except that the order of
245 text arguments is significant.
246 .PP
247 Here are all but one of the basic \fBpic\fP objects at their default sizes:
248 .KS
249 .PS
250 box "box";
251 move;
252 line "line" "";
253 move;
254 arrow "arrow" "";
255 move;
256 circle "circle";
257 move;
258 ellipse "ellipse";
259 move;
260 arc; down; move; "arc"
261 .PE
262 .CE "2: Basic \fBpic\fP objects"
263 .PP
264 The missing simple object type is a \fIspline\fP.
265 There is also a way to collect objects into \fIblock composites\fP which
266 allows you to treat the whole group as a single object (resembling a
267 box) for many purposes.
268 We'll describe both of these later on.
269 .PP
270 The box, ellipse, circle, and block composite objects are \fIclosed\/\fR;
271 lines, arrows, arcs and splines are \fIopen\fP.
272 This distinction is often important in explaining command modifiers.
273 .PP
274 Figure \n[H1]-2 was produced by the following \fBpic\fP program,
275 which introduces some more basic concepts:
276 .KS
277 .DS
278 .CW
279 \&.PS
280 box "box";
281 move;
282 line "line" "";
283 move;
284 arrow "arrow" "";
285 move;
286 circle "circle";
287 move;
288 ellipse "ellipse";
289 move;
290 arc; down; move; "arc"
291 \&.PE
292 .DE
293 .ft R
294 .KE
295 .PP
296 The first thing to notice is the \fImove\fP command, which moves a
297 default distance (1/2 inch) in the current movement direction.
298 .PP
299 Secondly, see how we can also decorate lines and arrows with text.
300 The line and arrow commands each take two arguments here, specifying
301 text to go above and below the object.
302 If you wonder why one argument would not do, contemplate the output of
303 \fBarrow "ow!"\fP:
304 .KS
305 .PS
306 arrow "ow!"
307 .PE
308 .CE "3: Text centered on an arrow"
309 .PP
310 When a command takes one text string, \fBpic\fP tries to place it at
311 the object's geometric center.
312 As you add more strings, \fBpic\fP treats them as a vertical block to be
313 centered.
314 The program
315 .KS
316 .DS
317 .CW
318 line "1";
319 line "1" "2";
320 line "1" "2" "3";
321 line "1" "2" "3" "4";
322 line "1" "2" "3" "4" "5";
323 .DE
324 .ft R
325 .KE
326 .LP
327 for example, gives you this:
328 .KS
329 .sp 2
330 .PS
331 line "1";
332 line "1" "2";
333 line "1" "2" "3";
334 line "1" "2" "3" "4";
335 line "1" "2" "3" "4" "5";
336 .PE
337 .sp 2
338 .CE "4: Effects of multiple text arguments"
339 .PP
340 The last line of Figure 3-2's program, `\fBarc; down; move;
341 "arc"\fP', describing the captioned arc, introduces several new ideas.
342 Firstly, we see how to change the direction in which objects are
343 joined.
344 Had we written \fBarc; move; "arc"\fP, omitting \fBdown\fP the caption
345 would have been joined to the top of the arc, like this:
346 .KS
347 .PS
348 arc; move; "arc";
349 .PE
350 .CE "5: Result of \fBarc; move; ""arc""\fP"
351 .PP
352 This is because drawing an arc changes the default direction to the
353 one its exit end points at.
354 To reinforce this point, consider:
355 .KS
356 .PS
357 arc cw; move; "arc";
358 .PE
359 .CE "6: Result of \fBarc cw; move; ""arc""\fP"
360 .PP
361 All we've done differently here is specify \[lq]cw\[rq] for a clockwise arc
362 (\[lq]ccw\[rq] specifies counter-clockwise direction).
363 Observe how it changes the default direction to down, rather than up.
364 .PP
365 Another good way to see this via with the following program:
366 .KS
367 .DS
368 .CW
369 line; arc; arc cw; line
370 .DE
371 .ft R
372 .KE
373 .LP
374 which yields:
375 .KS
376 .PS
377 line; arc; arc cw; line;
378 .PE
379 .CE "7: Result of \fBline; arc; arc cw; line\fP"
380 .LP
381 Notice that we did not have to specify \[lq]up\[rq] for the second arc to be
382 joined to the end of the first.
383 .PP
384 Finally, observe that a string, alone, is treated as text to be
385 surrounded by an invisible box of a size either specified by width
386 and height attributes or by the defaults \fBtextwid\fR and
387 \fBtextht\fR.
388 Both are initially zero (because we don't know the default font size).
389 .
390 .
391 .NH 1
392 Sizes and Spacing
393 .PP
394 Sizes are specified in inches.
395 If you don't like inches, it's possible to set a global style variable
396 \fBscale\fP that changes the unit.
397 Setting \fBscale = 2.54\fP effectively changes the internal unit to
398 centimeters (all other size variable values are scaled correspondingly).
399 .
400 .NH 2
401 Default Sizes of Objects
402 .PP
403 Here are the default sizes for \fBpic\fP objects:
404 .TS H
405 center, tab(@), linesize(2);
406 lb | lb
407 l | l.
408 .sp 2p
409 Object@Default Size
410 .sp 2p
411 _
412 .sp 2p
413 .TH
414 box@0.75" wide by 0.5" high
415 circle@0.5" diameter
416 ellipse@0.75" wide by 0.5" high
417 arc@0.5" radius
418 line@0.5" long
419 arrow@0.5" long
420 .sp 5p
421 _
422 .TE
423 .PP
424 The simplest way to think about these defaults is that they make the
425 other basic objects fit snugly into a default-sized box.
426 .PP
427 \fIpic2plot\/\fP(1) does not necessarily emit a physical inch for
428 each virtual inch in its drawing coordinate system.
429 Instead, it draws on a canvas 8\~virtual inches by 8\~virtual inches
430 wide.
431 If its output page size is \[lq]letter\[rq], these virtual inches will
432 map to real ones.
433 Specifying a different page size (such as, say, \[lq]a4\[rq]) will scale
434 virtual inches so they are output as one eighth of the page width.
435 Also, \fIpic2plot\/\fP(1) centers all images by default, though the
436 \fB\-n\fP option can be used to prevent this.
437 .
438 .NH 2
439 Objects Do Not Stretch!
440 .PP
441 Text is rendered in the current font with normal troff line spacing.
442 Boxes, circles, and ellipses do \fInot\fP automatically resize to fit
443 enclosed text.
444 Thus, if you say \fBbox "this text far too long for a default box"\fP
445 you'll get this:
446 .KS
447 .PS
448 box "this text is far too long for a default box"
449 .PE
450 .CE "1: Boxes do not automatically resize"
451 .LP
452 which is probably not the effect you want.
453 .
454 .NH 2
455 Resizing Boxes
456 .PP
457 To change the box size, you can specify a box width with the \[lq]width\[rq]
458 modifier:
459 .KS
460 .PS
461 box width 3 "this text is far too long for a default box"
462 .PE
463 .CE "2: Result of \fBbox width 3 ""text far too long""\fP"
464 .PP
465 This modifier takes a dimension in inches.
466 There is also a \[lq]height\[rq] modifier that changes a box's height.
467 The \fBwidth\fP keyword may be abbreviated to \fBwid\fP; the
468 \fBheight\fP keyword to \fBht\fP.
469 .
470 .NH 2
471 Resizing Other Object Types
472 .PP
473 To change the size of a circle, give it a \fBrad[ius]\fP or
474 \fBdiam[eter]\fP modifier; this changes the radius or diameter of the
475 circle, according to the numeric argument that follows.
476 .KS
477 .PS
478 {circle rad 0.1; move down 0.2 from last circle .s; "0.1"};
479 move; circle rad 0.2 "0.2"; move; circle rad 0.3 "0.3";
480 .PE
481 .CE "3: Circles with increasing radii"
482 .PP
483 The \fBmove\fP command can also take a dimension, which just tells
484 it how many inches to move in the current direction.
485 .PP
486 Ellipses are sized to fit in the rectangular box defined by their
487 axes, and can be resized with \fBwidth\fP and \fBheight\fP like boxes.
488 .PP
489 You can also change the radius of curvature of an arc with \fBrad[ius]\fP
490 (which specifies the radius of the circle of which the arc is a segment).
491 Larger values yield flatter arcs.
492 .KS
493 .PS
494 {arc rad 0.1; move down 0.3 from last arc .center; "0.1"};
495 move;
496 {arc rad 0.2; move down 0.4 from last arc .center; "0.2"};
497 move;
498 {arc rad 0.3; move down 0.5 from last arc .center; "0.3"};
499 .PE
500 .CE "4: \fBarc rad\fP with increasing radii"
501 .PP
502 Observe that because an arc is defined as a quarter circle, increasing
503 the radius also increases the size of the arc's bounding box.
504 .
505 .NH 2
506 The `same' Keyword
507 .PP
508 In place of a dimension specification, you can use the keyword
509 \fBsame\fR.
510 This gives the object the same size as the previous one
511 of its type.
512 As an example, the program
513 .KS
514 .DS
515 .CW
516 \&.PS
517 box; box wid 1 ht 1; box same; box
518 \&.PE
519 .R
520 .DE
521 .KE
522 .LP
523 gives you
524 .KS
525 .PS
526 box; box wid 1 ht 1; box same; box
527 .PE
528 .CE "5: The \fBsame\fP keyword"
529 .
530 .
531 .NH 1
532 Generalized Lines and Splines
533 .
534 .NH 2
535 Diagonal Lines
536 .PP
537 It is possible to specify diagonal lines or arrows by adding multiple \fBup\fP,
538 \fBdown\fP, \fBleft\fP, and \fBright\fP modifiers to the line object.
539 Any of these can have a multiplier.
540 To understand the effects, think of the drawing area as being gridded
541 with standard-sized boxes.
542 .KS
543 .PS
544 # Draw a demonstration up left arrow with grid box overlay
545 define gridarrow
546 {
547         move right 0.5
548         [
549                 {arrow up left $1;}
550                 box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
551                 for i = 2 to ($1 / 0.5) do {
552                         box wid 0.5 ht 0.5 dotted with .sw at last box .se;
553                 }
554                 move down from last arrow .center;
555                 [
556                         sprintf("\fBarrow up left %g\fP", $1)
557                 ]
558         ]
559         move right 0.1 from last [] .e;
560 }
561 gridarrow(0.5);
562 gridarrow(1);
563 gridarrow(1.5);
564 gridarrow(2);
565 undef gridarrow
566 .PE
567 .CE "1: Diagonal arrows (dotted boxes show the implied 0.5-inch grid)"
568 .
569 .NH 2
570 Multi-Segment Line Objects
571 .PP
572 A \[lq]line\[rq] or \[lq]arrow\[rq] object may actually be a path
573 consisting of any number of segments of varying lengths and directions.
574 To describe a path, connect several line or arrow commands with the
575 keyword \fBthen\fP.
576 .KS
577 .PS
578 define zigzag { $1 right 1 then down .5 left 1 then right 1 }
579 zigzag(line);
580 .PE
581 .CE "2: \fBline right 1 then down .5 left 1 then right 1\fP"
582 .PP
583 If a path starts with \fBthen\fP, the first segment is assumed to be into
584 the current direction, using the default length.
585 .
586 .NH 2
587 Spline Objects
588 .PP
589 If you start a path with the \fBspline\fP keyword, the path vertices
590 are treated as control points for a spline curve fit.
591 .KS
592 .PS
593 [zigzag(spline);]
594 move down 0.2 from last [] .s;
595 "The spline curve..."
596 move right from last [] .e;
597 [
598         zigzag(line dashed);
599         spline from start of last line right 1 then down .5 left 1 then right 1;
600         "1" at last spline .start + (-0.1, 0);
601         "2" at last spline .start + (1.1, 0);
602         "3" at last spline .end + (-1.1, 0);
603         "4" at last spline .end + (0.1, 0);
604 ]
605 move down 0.2 from last [] .s;
606 "...with tangents displayed"
607 undef zigzag;
608 .PE
609 .CE "3: \fBspline right 1 then down .5 left 1 then right 1\fP"
610 .PP
611 You can describe many natural-looking but irregular curves this
612 way.
613 For example:
614 .KS
615 .PS
616 [spline right then up then left then down ->;]
617 move down 0.2 from last [] .s;
618 ["\fBspline right then up then left then down ->;\fP"]
619 move right 3 from last [] .se;
620 "\fBspline left then up right then down right ->;\fP"
621 move up 0.2;
622 [spline left then up right then down right ->;]
623 .PE
624 .CE "4: Two more spline examples"
625 .LP
626 Note the arrow decorations.
627 Arrowheads can be applied naturally to any path-based object, line or
628 spline.
629 We'll see how in the next section.
630 .
631 .
632 .NH 1
633 Decorating Objects
634 .
635 .NH 2
636 Text Special Effects
637 .PP
638 All \fBpic\fP implementations support the following font-styling
639 escapes within text objects:
640 .IP "\efR, \ef1" \w'\efR,\~\ef3'u+2n
641 Set Roman style (the default)
642 .IP "\efI, \ef2"
643 Set Italic style
644 .IP "\efB, \ef3"
645 Set Bold style
646 .IP \efP
647 Revert to previous style; only works one level deep, does not stack.
648 .PP
649 In the \fBpic\fP implementations that are preprocessors for a
650 toolchain that include \fB[gtn]roff\fP, text objects may also contain
651 \fB[gtn]roff\fP vertical- and horizontal-motion escapes such as \eh or \ev.
652 Troff special glyphs are also available.
653 All \e-escapes will be passed through to the postprocessing stage and
654 have their normal effects.
655 The base font family is set by the \fB[gtn]roff\fP environment at the
656 time the picture is rendered.
657 .PP
658 \fBpic2plot\fP replaces \fB[gtn]roff\fP horizontal- and vertical-motion
659 escapes with \e-escapes of its own.
660 Troff special glyphs are not available, but in most back ends Latin-1
661 special characters and a square-root radical will be.
662 See the \fBpic2plot\fP documentation for full details.
663 .
664 .NH 2
665 Dashed Objects
666 .PP
667 We've already seen that the modifier \fBdashed\fP can change the line
668 style of an object from solid to dashed.
669 GNU \fBgpic\fP permits you to dot or dash ellipses, circles, and arcs
670 (and splines in \*[tx] mode only); some versions of DWB may only permit
671 dashing of lines and boxes.
672 It's possible to change the dash interval by specifying a
673 number after the modifier.
674 .PP
675 .KS
676 .PS
677 box dashed "default";
678 move;
679 box dashed 0.05 "0.05";
680 move;
681 box dashed 0.1 "0.1";
682 move;
683 box dashed 0.15 "0.15";
684 move;
685 box dashed 0.2 "0.2";
686 .PE
687 .CE "1: Dashed objects"
688 .
689 .NH 2
690 Dotted Objects
691 .PP
692 Another available qualifier is \fBdotted\fP.
693 GNU \fBgpic\fP permits you to dot or dash ellipses, circles, and arcs
694 (and splines in \*[tx] mode only); some versions of DWB may only permit
695 dashing of lines and boxes.
696 It too can be suffixed with a number to specify the interval between
697 dots:
698 .KS
699 .PS
700 box dotted "default";
701 move;
702 box dotted 0.05 "0.05";
703 move;
704 box dotted 0.1 "0.1";
705 move;
706 box dotted 0.15 "0.15";
707 move;
708 box dotted 0.2 "0.2";
709 .PE
710 .CE "2: Dotted objects"
711 .
712 .NH 2
713 Rounding Box Corners
714 .PP
715 It is also possible, in GNU \fBgpic\fP only, to modify a box so it has
716 rounded corners:
717 .KS
718 .PS
719 box rad 0.05 "rad 0.05";
720 move;
721 box rad 0.1 "rad 0.1";
722 move;
723 box rad 0.15 "rad 0.15";
724 move;
725 box rad 0.2 "rad 0.2";
726 move;
727 box rad 0.25 "rad 0.25";
728 .PE
729 .CE "3: \fBbox rad\fP with increasing radius values"
730 .PP
731 Radius values higher than half the minimum box dimension are silently
732 truncated to that value.
733 .
734 .NH 2
735 Slanted Boxes
736 .PP
737 GNU \fBgpic\fP supports slanted boxes:
738 .KS
739 .PS
740 box wid 1.2 xslanted 0.1 "xslanted 0.1";
741 move;
742 box wid 1.2 yslanted -0.1 "yslanted -0.1";
743 move;
744 box wid 1.2 xslanted -0.2 yslanted 0.1 "xslanted -0.2" "yslanted 0.1";
745 .PE
746 .CE "4: Various slanted boxes."
747 .PP
748 The \fBxslanted\fP and \fByslanted\fP attributes specify the x and
749 y\~offset, respectively, of the box's upper right corner from its default
750 position.
751 .
752 .NH 2
753 Arrowheads
754 .PP
755 Lines and arcs can be decorated as well.
756 Any line or arc (and any spline as well) can be decorated with
757 arrowheads by adding one or more as modifiers:
758 .KS
759 .PS
760 line <- ->
761 .PE
762 .CE "5: Double-headed line made with  \fBline <- ->\fP"
763 .PP
764 In fact, the \fBarrow\fP command is just shorthand for \fBline ->\fP.
765 And there is a double-head modifier <->, so the figure above could have
766 been made with \fBline <->\fP.
767 .PP
768 Arrowheads have a \fBwidth\fP attribute, the distance across the rear;
769 and a \fBheight\fP attribute, the length of the arrowhead along the shaft.
770 .PP
771 Arrowhead style is controlled by the style variable \fBarrowhead\fP.
772 The DWB and GNU versions interpret it differently.
773 DWB defaults to open arrowheads and an \fBarrowhead\fP value of\~2; the
774 Kernighan paper says a value of\~7 makes solid arrowheads.
775 GNU \fBgpic\fP defaults to solid arrowheads and an \fBarrowhead\fP value
776 of\~1; a value of\~0 produces open arrowheads.
777 Note that solid arrowheads are always filled with the current outline
778 color.
779 .
780 .NH 2
781 Line Thickness
782 .PP
783 It's also possible to change the line thickness of an object (this is
784 a GNU extension, DWB \fBpic\fP doesn't support it).
785 The default thickness of the lines used to draw objects is controlled by the
786 .B linethick
787 variable.
788 This gives the thickness of lines in points.
789 A negative value means use the default thickness:
790 in \*[tx] output mode, this means use a thickness of 8 milliinches;
791 in \*[tx] output mode with the
792 .B -c
793 option, this means use the line thickness specified by
794 .B .ps
795 lines; in troff output mode, this means use a thickness proportional
796 to the pointsize.
797 A zero value means draw the thinnest possible line supported by the
798 output device.
799 Initially it has a value of -1.
800 There is also a \fBthickness\fP attribute (which can be abbreviated to
801 \fBthick\fP).
802 For example, \fBcircle thickness 1.5\fP would draw a circle using a line
803 with a thickness of 1.5 points.
804 The thickness of lines is not affected by the value of the
805 .B scale
806 variable, nor by any width or height given in the
807 .B .PS
808 line.
809 .
810 .NH 2
811 Invisible Objects
812 .PP
813 The modifier \fBinvis[ible]\fP makes an object entirely invisible.
814 This used to be useful for positioning text in an invisible object that
815 is properly joined to neighboring ones.
816 Newer DWB versions and GNU \fBpic\fP treat stand-alone text in exactly
817 this way.
818 .
819 .NH 2
820 Filled Objects
821 .PP
822 It is possible to fill boxes, circles, and ellipses.
823 The modifier \fBfill[ed]\fP accomplishes this.
824 You can suffix it with a fill value; the default is given by the style
825 variable \fBfillval\fP.
826 .PP
827 DWB \fBpic\fP and \fBgpic\fP have opposite conventions for fill values
828 and different defaults.
829 DWB \fBfillval\fP defaults to 0.3 and smaller values are darker; GNU
830 \fBfillval\fP uses 0 for white and 1 for black.
831 .KS
832 .PS
833 circle fill; move; circle fill 0.4; move; circle fill 0.9;
834 .PE
835 .CE "6: \fBcircle fill; move; circle fill 0.4; move; circle fill 0.9;\fR"
836 .PP
837 GNU \fBgpic\fP makes some additional guarantees.
838 A fill value greater than 1 can also be used: this means fill with the
839 shade of gray that is currently being used for text and lines.
840 Normally this is black, but output devices may provide a mechanism for
841 changing this.
842 The invisible attribute does not affect the filling of objects.
843 Any text associated with a filled object is added after the object has
844 been filled, so that the text is not obscured by the filling.
845 .
846 .NH 2
847 Colored Objects
848 .PP
849 As a GNU extension, three additional modifiers are available to specify
850 colored objects.
851 \fBoutline\fP sets the color of the outline, \fBshaded\fP the fill
852 color, and \fBcolor\fP sets both.
853 All three keywords expect a suffix specifying the color.
854 Example:
855 .KS
856 .PS
857 box color "yellow"; arrow color "cyan"; circle shaded "green" outline "black";
858 .PE
859 .CE "7: \fBbox color ""yellow""; arrow color ""cyan""; \
860 circle shaded ""green"" outline ""black"";\fR"
861 .PP
862 Alternative spellings are \fBcolour\fP, \fBcolored\fP, \fBcoloured\fP,
863 and \fBoutlined\fP.
864 .PP
865 Predefined color names for \fI[gtn]roff\/\fP-based \fBpic\fP
866 implementations are defined in the device macro files, for example
867 \f(CWps.tmac\fP; additional colors can be defined with the
868 \fB.defcolor\fP request (see the manual page of GNU \fItroff\/\fP(1)
869 for more details).
870 Currently, color support is not available at all in \*[tx] mode.
871 .PP
872 The \fIpic2plot\/\fP(1) carries with its own set of color names,
873 essentially those recognized by the X\~window system with \[lq]grey\[rq]
874 accepted as a variant of \[lq]gray\[rq].
875 .PP
876 \fBpic\fP assumes that at the beginning of a picture both glyph and fill
877 color are set to the default value.
878 .
879 .
880 .NH 1
881 More About Text Placement
882 .PP
883 By default, text is centered at the geometric center of the object it is
884 associated with.
885 The modifier \fBljust\fP causes the left end to be at the specified
886 point (which means that the text lies to the right of the specified
887 place!), the modifier \fBrjust\fP puts the right end at the place.
888 The modifiers \fBabove\fP and \fBbelow\fP center the text one half line
889 space in the given direction.
890 .PP
891 Text attributes can be combined:
892 .KS
893 .PS
894 [line up "ljust text" ljust;]
895 move 1.5;
896 [line up "rjust text" rjust;]
897 move;
898 [arrow 1 "ljust above" ljust above;]
899 move;
900 [arrow 1 "rjust below" rjust below;]
901 .PE
902 .CE "1: Text attributes"
903 .PP
904 What actually happens is that \fIn\fP text strings are centered in a box
905 that is \fBtextwid\fP wide by \fBtextht\fP high.
906 Both these variables are initially zero (that is \fBpic\fR's way of not
907 making assumptions about \fI[tg]roff\/\fP(1)'s default point size).
908 .PP
909 In GNU \fBgpic\fR, objects can have an
910 .B aligned
911 attribute.
912 This only works if the postprocessor is
913 \fBgrops\fP or \fBgropdf\fP.
914 Any text associated with an object having the
915 .B aligned
916 attribute is rotated about the center of the object
917 so that it is aligned in the direction from the start point
918 to the end point of the object.
919 Note that this attribute has no effect for objects whose start and
920 end points are coincident.
921 .
922 .
923 .NH 1
924 More About Direction Changes
925 .PP
926 We've already seen how to change the direction in which objects are
927 composed from rightwards to downwards.
928 Here are some more illustrative examples:
929 .KS
930 .PS
931 down;
932 [
933         "\fBright; box; arrow; circle; arrow; ellipse\fP";
934         move 0.2;
935         [right; box; arrow; circle; arrow; ellipse;]
936 ]
937 move down 0.3 from last [] .s;
938 [
939         "\fBleft; box; arrow; circle; arrow; ellipse\fP"
940         move 0.2;
941         [left; box; arrow; circle; arrow; ellipse;]
942 ]
943 # move down 0.3 from last [] .sw;
944 # To re-join this illustrations, delete everything from here down to
945 # the next #-comment, and uncomment the move line above
946 .PE
947 .CE "1: Effects of different motion directions (right and left)"
948 .KS
949 .PS
950 # To re-join this illustrations, delete everything down to here, then
951 # comment out the next `down' line.
952 # Don't forget to re-number the figures following!
953 down;
954 [
955         "\fBdown; box; arrow; circle; arrow; ellipse;\fP"
956         move 0.2;
957         box; arrow; circle; arrow; ellipse;
958 ]
959 move right 2 from last [] .e;
960 [
961         up; box; arrow; circle; arrow; ellipse;
962         move 0.2;
963         "\fBup; box; arrow; circle; arrow; ellipse;\fP"
964 ]
965 .PE
966 .CE "2: Effects of different motion directions (up and down)"
967 .PP
968 Something that may appear surprising happens if you change directions
969 in the obvious way:
970 .KS
971 .PS
972 box; arrow; circle; down; arrow; ellipse
973 .PE
974 .CE "3: \fBbox; arrow; circle; down; arrow; ellipse\fP"
975 .LP
976 You might have expected that program to yield this:
977 .KS
978 .PS
979 box; arrow; circle; move to last circle .s; down; arrow; ellipse
980 .PE
981 .CE "4: More intuitive?"
982 .LP
983 But, in fact, to get Figure \*[SN]3 you have to do this:
984 .KS
985 .DS
986 .CW
987 \&.PS
988 box;
989 arrow;
990 circle;
991 move to last circle .s;
992 down;
993 arrow;
994 ellipse
995 \&.PE
996 .R
997 .DE
998 .KE
999 .LP
1000 Why is this?
1001 Because the exit point for the current direction is already set when you
1002 draw the object.
1003 The second arrow in Figure \*[SN]2 dropped downwards from the circle's
1004 attachment point for an
1005 object to be joined to the right.
1006 .PP
1007 The meaning of the command \fBmove to last circle \&.s\fP should be
1008 obvious.
1009 In order to see how it generalizes, we'll need to go into detail on two
1010 important topics; locations and object names.
1011 .
1012 .
1013 .NH 1
1014 Naming Objects
1015 .PP
1016 The most natural way to name locations in \fBpic\fP is relative to
1017 objects.
1018 In order to do this, you have to be able to name
1019 objects.
1020 The \fBpic\fP language has rich facilities for this that try to emulate
1021 the syntax of English.
1022 .
1023 .NH 2
1024 Naming Objects By Order Of Drawing
1025 .PP
1026 The simplest (and generally the most useful) way to name an object is
1027 with a \fBlast\fP clause.
1028 It needs to be followed by an object type name; \fBbox\fP, \fBcircle\fP,
1029 \fBellipse\fP, \fBline\fP, \fBarrow\fP, \fBspline\fP, \fB""\fP, or
1030 \fB[]\fP (the last type refers to a \fIcomposite object\fP which we'll
1031 discuss later).
1032 So, for example, the \fBlast circle\fP clause in the program attached to
1033 Figure \*[SN]3 refers to the last circle drawn.
1034 .PP
1035 More generally, objects of a given type are implicitly numbered
1036 (starting from\~1).
1037 You can refer to (say) the third ellipse in the current picture with
1038 \fB3rd ellipse\fP, or to the first box as \fB1st box\fP, or to the fifth
1039 text string (which isn't an attribute to another object) as \fB5th
1040 ""\fP.
1041 .PP
1042 Objects are also numbered backwards by type from the last one.
1043 You can say \fB2nd last box\fP to get the second-to-last box, or
1044 \fB3rd last ellipse\fP to get the third-to-last ellipse.
1045 .PP
1046 In places where \fIn\/\fBth\fR is allowed, \fB`\fIexpr\/\fB'th\fR is
1047 also allowed.
1048 Note that
1049 .B 'th
1050 is a single token: no space is allowed between the
1051 .B '
1052 and the \fBth\fP.
1053 For example,
1054 .IP
1055 .KS
1056 .DS
1057 .CW
1058 for i = 1 to 4 do {
1059    line from `i'th box.nw to `i+1'th box.se
1060 }
1061 .DE
1062 .R
1063 .KE
1064 .
1065 .NH 2
1066 Naming Objects With Labels
1067 .PP
1068 You can also specify an object by referring to a label.
1069 A label is a word (which must begin with a capital letter) followed by a
1070 colon; you declare it by placing it immediately before the object
1071 drawing command.
1072 For example, the program
1073 .KS
1074 .DS
1075 .CW
1076 \&.PS
1077 A: box "first" "object"
1078 move;
1079 B: ellipse "second" "object"
1080 move;
1081 arrow right at A .r;
1082 \&.PE
1083 .R
1084 .DE
1085 .KE
1086 .LP
1087 declares labels \fBA\fP and \fBB\fP for its first and second objects.
1088 Here's what that looks like:
1089 .KS
1090 .PS
1091 A: box "first" "object"
1092 move;
1093 B: ellipse "second" "object"
1094 move;
1095 arrow right at A .r;
1096 .PE
1097 .CE "1: Example of label use"
1098 The \fBat\fP statement in the fourth line uses the label \fBA\fP (the
1099 behavior of \fBat\fP is explained in the next section).
1100 We'll see later on that labels are most useful for referring to block
1101 composite objects.
1102 .PP
1103 Labels are not constants but variables (you can view colon as a sort
1104 of assignment).
1105 You can say something like \fBA: A + (1,0);\fP and the effect is to
1106 reassign the label \fBA\fR to designate a position one inch to the right
1107 of its old value.
1108 .
1109 .
1110 .NH 1
1111 Describing locations
1112 .PP
1113 The location of points can be described in many different ways.
1114 All these forms are interchangeable as for as the \fBpic\fP language
1115 syntax is concerned; where you can use one, any of the others that would
1116 make semantic sense are allowed.
1117 .PP
1118 The special label \fBHere\fR always refers to the current position.
1119 .
1120 .NH 2
1121 Absolute Coordinates
1122 .PP
1123 The simplest is absolute coordinates in inches; \fBpic\fP uses a
1124 Cartesian system with (0,0) at the lower left corner of the virtual
1125 drawing surface for each picture (that is, X\~increases to the right
1126 and Y\~increases upwards).
1127 An absolute location may always be written in the conventional form as
1128 two comma-separated numbers surrounded by parentheses (and this is
1129 recommended for clarity).
1130 In contexts where it creates no ambiguity, the pair of X and
1131 Y\~coordinates suffices without parentheses.
1132 .PP
1133 It is a good idea to avoid absolute coordinates, however.
1134 They tend to make picture descriptions difficult to understand and
1135 modify.
1136 Instead, there are quite a number of ways to specify locations
1137 relative to \fBpic\fP objects and previous locations.
1138 .PP
1139 Another possibility of surprise is the fact that \fBpic\fP crops the
1140 picture to the smallest bounding box before writing it out.
1141 For example, if you have a picture consisting of a small box with its
1142 lower left corner at (2,2) and another small box with its upper right
1143 corner at (5,5), the width and height of the image are both 3\~units and
1144 not\~5.
1145 To get the origin at (0,0) included, simply add an invisible object to
1146 the picture, positioning the object's left corner at (0,0).
1147 .
1148 .NH 2
1149 Locations Relative to Objects
1150 .PP
1151 The symbol \fBHere\fP always refers to the position of the last object
1152 drawn or the destination of the last \fBmove\fP.
1153 .PP
1154 Alone and unqualified, a \fBlast circle\fP or any other way of
1155 specifying a closed-object or arc location refers as a position to the
1156 geometric center of the object.
1157 Unqualified, the name of a line or spline object refers to the position
1158 of the object start.
1159 .PP
1160 Also, \fBpic\fP objects have quite a few named locations
1161 associated with them.
1162 One of these is the object center, which can be indicated (redundantly)
1163 with the suffix \fB.center\fP (or just \fB.c\fP).
1164 Thus, \fBlast circle \&.center\fP is equivalent to \fBlast
1165 circle\fP.
1166 .NH 3
1167 Locations Relative to Closed Objects
1168 .PP
1169 Every closed object (box, circle, ellipse, or block composite) also
1170 has eight compass points associated with it;
1171 .KS
1172 .PS
1173 define dot {circle fill rad 0.02 at $1}
1174
1175 define compass { [
1176         ME: $1;
1177         dot(ME.c);  "\fB .c\fP"  at ME .c ljust;
1178         dot(ME.n);  "\fB.n\fP"   at ME .n above
1179         dot(ME.ne); "\fB .ne\fP" at ME .ne above
1180         dot(ME.e);  "\fB .e\fP"  at ME .e ljust
1181         dot(ME.se); "\fB .se\fP" at ME .se below
1182         dot(ME.s);  "\fB.s\fP"   at ME .s below
1183         dot(ME.sw); "\fB.sw \fP" at ME .sw below
1184         dot(ME.w);  "\fB.w \fP"  at ME .w rjust
1185         dot(ME.nw); "\fB.nw \fP" at ME .nw above
1186 ] }
1187 compass(box wid 1.5 ht 1);
1188 move right from last [] .e;
1189 compass(circle diam 1);
1190 move right from last [] .e;
1191 compass(ellipse wid 1.5 ht 1);
1192 .PE
1193 .CE "1: Compass points"
1194 .LP
1195 these are the locations where eight compass rays from the geometric center
1196 would intersect the figure.
1197 So when we say \fBlast circle \&.s\fP we are referring to the south
1198 compass point of the last circle drawn.
1199 The explanation of Figure 8-3's program is now complete.
1200 .PP
1201 (In case you dislike compass points, the names \fB.top\fP,
1202 \&\fB.bottom\fP, \fB.left\fP and \fB.right\fP are synonyms for \fB.n\fP,
1203 \&\fB.s\fP, \fB.e\fP, and \fB.w\fP respectively; they can even be
1204 abbreviated to \fB.t\fP, \fB.b\fP, \fB.l\fP and \fB.r\fP).
1205 .PP
1206 The names \fBcenter\fP, \fBtop\fP, \fBbottom\fP, \fBleft\fP, \fBright\fP,
1207 \fBnorth\fP, \fBsouth\fP, \fBeast\fP, and \fBwest\fP can also be used
1208 (without the leading dot) in a prefix form marked by \fBof\fP; thus,
1209 \fBcenter of last circle\fP and \fBtop of 2nd last ellipse\fP are both
1210 valid object references.
1211 Finally, the names \fBleft\fP and \fBright\fP can be prefixed with
1212 \fBupper\fP and \fBlower\fP which both have the obvious meaning.
1213 .PP
1214 Arc objects also have compass points; they are the compass points of
1215 the implied circle.
1216 .PP
1217 Non-closed objects (line, arrow, or spline) have compass points too, but
1218 the locations of them are completely arbitrary.
1219 In particular, different \fBpic\fP implementations return different
1220 locations.
1221 .NH 3
1222 Locations Relative to Open Objects
1223 .PP
1224 Every open object (line, arrow, arc, or spline) has three named
1225 points: \fB.start\fP, \fB.center\fP (or \fB.c\fP), and \fB.end\fP.
1226 They can also be used without leading dots in the \fBof\fP prefix form.
1227 The center of an arc is the center of its circle, but the center of
1228 a line, path, or spline is halfway between its endpoints.
1229 .KS
1230 .PS
1231 define critical {
1232         [ ME: $1;
1233                 dot(ME.c);     "\fB.center\fP" rjust at ME.center + (-0.1, 0.1)
1234                 dot(ME.start); "\fB.start\fP"  rjust at ME.start  + (-0.1, 0.1)
1235                 dot(ME.end);   "\fB.end\fP"    rjust at ME.end    + (-0.1, 0.1)
1236         ]
1237 }
1238 critical(line up right 1);
1239 move right 1 from last [] .e;
1240 critical(arc rad 0.5 cw);
1241 move down 0.5 from 2nd last [] .s;
1242 critical(line right 1 then down .5 left 1 then right 1);
1243 move right 1 from last [] .e;
1244 critical(spline right 1 then up right then left then left 1);
1245 .PE
1246 .CE "2: Special points on open objects"
1247 .PP
1248 .
1249 .NH 2
1250 Ways of Composing Positions
1251 .PP
1252 Once you have two positions to work with, there are several ways to
1253 combine them to specify new positions.
1254 .NH 3
1255 Vector Sums and Displacements
1256 .PP
1257 Positions may be added or subtracted to yield a new position (to be
1258 more precise, you can only add a position and an expression pair; the
1259 latter must be on the right side of the addition or subtraction sign).
1260 The result is the conventional vector sum or difference of coordinates.
1261 For example, \fBlast box \&.ne + (0.1, 0)\fP is a valid position.
1262 This example illustrates a common use, to define a position slightly
1263 offset from a named one (say, for captioning purposes).
1264 .NH 3
1265 Interpolation Between Positions
1266 .PP
1267 A position may be interpolated between any two positions.
1268 The syntax is `\fIfraction\fP \fBof the way between\fP \fIposition1\fP
1269 \fBand\fP \fIposition2\fP'.
1270 For example, you can say \fB1/3 of the way between Here and last ellipse
1271 \&.ne\fP.
1272 The fraction may be in numerator/denominator form or may be an ordinary
1273 number (values are \fInot\fP restricted to [0,1]).
1274 As an alternative to this verbose syntax, you can say `\fIfraction\fP
1275 \fB<\,\fP\fIposition1\fP \fB,\fP \fIposition2\/\fP\fB>\fP'; thus, the
1276 example could also be written as \fB1/3 <Here, last ellipse>\fP.
1277 .KS
1278 .PS
1279 arrow up right;
1280 P: 1/3 of the way between last arrow .start and last arrow .end;
1281 dot(P); move right 0.1; "P";
1282 .PE
1283 .CE "3: \fBP: 1/3 of the way between last arrow .start and last arrow .end\fP"
1284 .PP
1285 This facility can be used, for example, to draw double connections.
1286 .KS
1287 .PS
1288 A: box "yin"; move;
1289 B: box "yang";
1290 arrow right at 1/4 <A.e,A.ne>;
1291 arrow left  at 1/4 <B.w,B.sw>;
1292 .PE
1293 .CE "4: Doubled arrows"
1294 .LP
1295 You can get Figure \n[H1]-4 from the following program:
1296 .KS
1297 .DS
1298 .CW
1299 \&.PS
1300 A: box "yin"; move;
1301 B: box "yang";
1302 arrow right at 1/4 <A.e,A.ne>;
1303 arrow left  at 1/4 <B.w,B.sw>;
1304 \&.PE
1305 .R
1306 .DE
1307 .KE
1308 .LP
1309 Note the use of the short form for interpolating points.
1310 .NH 3
1311 Projections of Points
1312 .PP
1313 Given two positions \fIp\fP and \fIq\fP, the position
1314 \fB(\,\fP\fIp\fP\fB,\fP \fIq\fP\fB)\fP has the X\~coordinate of \fIp\fP
1315 and the Y coordinate of \fIq\fP.
1316 This can be helpful in placing an object at one of the corners of the
1317 virtual box defined by two other objects.
1318 .KS
1319 .PS
1320 box invis wid 2 height 1;
1321 dot(last box .ne); "\fB(B,A)\fP is here" ljust at last circle + (0.1, 0.1);
1322 dot(last box .se); "B" ljust at last circle + (0.1, -0.1)
1323 dot(last box .sw); "\fB(A,B)\fP is here" rjust at last circle + (-0.1, -0.1);
1324 dot(last box .nw); "A" ljust at last circle + (-0.1, 0.1)
1325 .PE
1326 .CE "5: Using (\fIx\fP, \fIy\fP) composition"
1327 .
1328 .NH 2
1329 Using Locations
1330 .PP
1331 There are four ways to use locations; \fBat\fP, \fBfrom\fP, \fBto\fP,
1332 and \fBwith\fP.
1333 All four are object modifiers; that is, you use them as suffixes to a
1334 drawing command.
1335 .PP
1336 The \fBat\fP modifier says to draw a closed object or arc with its
1337 center at the following location, or to draw a line/spline/arrow
1338 starting at the following location.
1339 .PP
1340 The \fBto\fP modifier can be used alone to specify a move destination.
1341 The \fBfrom\fP modifier can be used alone in the same way as \fBat\fP.
1342 .PP
1343 The \fBfrom\fP and \fBto\fP modifiers can be used with a \fBline\fR or
1344 \fBarc\fR command to specify start and end points of the object.
1345 In conjunction with named locations, this offers a very flexible
1346 mechanism for connecting objects.
1347 For example, the following program
1348 .KS
1349 .DS
1350 .CW
1351 \&.PS
1352 box "from"
1353 move 0.75;
1354 ellipse "to"
1355 arc cw from 1/3 of the way \e
1356     between last box .n and last box .ne to last ellipse .n;
1357 \&.PE
1358 .R
1359 .DE
1360 .KE
1361 .LP
1362 yields:
1363 .KS
1364 .PS
1365 box "from"
1366 move 0.75;
1367 ellipse "to"
1368 arc cw from 1/3 of the way \
1369     between last box .n and last box .ne to last ellipse .n;
1370 .PE
1371 .CE "6: A tricky connection specified with English-like syntax"
1372 .PP
1373 The \fBwith\fP modifier allows you to identify a named attachment
1374 point of an object (or a position within the object) with another point.
1375 This is very useful for connecting objects in a natural way.
1376 For an example, consider these two programs:
1377 .KS
1378 .PS
1379 [
1380         [
1381                 box wid 0.5 ht 0.5;
1382                 box wid 0.75 ht 0.75;
1383         ]
1384         move 0.1 down 0.3 from last [] .s;
1385         "\fBbox wid 0.5 ht 0.5; box wid 0.75 ht 0.75\fP"
1386 ]
1387 move from last [].e 1.5
1388 [
1389         [
1390                 box wid 0.5 ht 0.5;
1391                 box wid 0.75 ht 0.75 with .sw at last box .se;
1392         ]
1393         move 0.1 down 0.3 from last [] .s;
1394         box invisible "\fBbox wid 0.5 ht 0.5;\fP" \
1395           "\fBbox wid 0.75 ht 0.75 with .sw at last box .se;\fP"
1396 ]
1397 .PE
1398 .CE "7: Using the \fBwith\fP modifier for attachments"
1399 .
1400 .NH 2
1401 The `chop' Modifier
1402 .PP
1403 When drawing lines between circles that don't intersect them at a
1404 compass point, it is useful to be able to shorten a line by the radius
1405 of the circle at either or both ends.
1406 Consider the following program:
1407 .KS
1408 .DS
1409 .CW
1410 \&.PS
1411 circle "x"
1412 circle "y" at 1st circle - (0.4, 0.6)
1413 circle "z" at 1st circle + (0.4, -0.6)
1414 arrow from 1st circle to 2nd circle chop
1415 arrow from 2nd circle to 3rd circle chop
1416 arrow from 3rd circle to 1st circle chop
1417 \&.PE
1418 .DE
1419 .R
1420 .KE
1421 .LP
1422 It yields the following:
1423 .KS
1424 .PS
1425 circle "x"
1426 circle "y" at 1st circle - (0.4, 0.6)
1427 circle "z" at 1st circle + (0.4, -0.6)
1428 arrow from 1st circle to 2nd circle chop
1429 arrow from 2nd circle to 3rd circle chop
1430 arrow from 3rd circle to 1st circle chop
1431 .PE
1432 .CE "8: The \fBchop\fR modifier"
1433 .LP
1434 Notice that the \fBchop\fR attribute moves arrowheads rather than
1435 stepping on them.
1436 By default, the \fBchop\fR modifier shortens both ends of the line by
1437 \fBcirclerad\fR.
1438 By suffixing it with a number you can change the amount of chopping.
1439 .PP
1440 If you say \fBline \&.\|.\|.\& chop \fIr1\fP chop \fIr2\fP\fR with
1441 \fIr1\fP and \fIr2\fP both numbers, you can vary the amount of chopping
1442 at both ends.
1443 You can use this in combination with trigonometric functions to write
1444 code that deals with more complex intersections.
1445 .
1446 .
1447 .NH 1
1448 Object Groups
1449 .PP
1450 There are two different ways to group objects in \fBpic\fP; \fIbrace
1451 grouping\fP and \fIblock composites\fP.
1452 .
1453 .NH 2
1454 Brace Grouping
1455 .PP
1456 The simpler method is simply to group a set of objects within curly
1457 bracket or brace characters.
1458 On exit from this grouping, the current position and direction are
1459 restored to their value when the opening brace was encountered.
1460 .
1461 .NH 2
1462 Block Composites
1463 .PP
1464 A block composite object is created a series of commands enclosed by
1465 square brackets.
1466 The composite can be treated for most purposes like a single closed
1467 object, with the size and shape of its bounding box.
1468 Here is an example.
1469 The program fragment
1470 .KS
1471 .DS
1472 .CW
1473 A: [
1474     circle;
1475     line up 1 at last circle .n;
1476     line down 1 at last circle .s;
1477     line right 1 at last circle .e;
1478     line left 1 at last circle .w;
1479     box dashed with .nw at last circle .se + (0.2, -0.2);
1480     Caption: center of last box;
1481 ]
1482 .R
1483 .DE
1484 .KE
1485 .LP
1486 yields the block in figure \n[H1]-1, which we show both with and
1487 without its attachment points.
1488 The block's location becomes the value of \fBA\fP.
1489 .KS
1490 .PS
1491 define junction {
1492         circle;
1493         line up 1 at last circle .n;
1494         line down 1 at last circle .s;
1495         line right 1 at last circle .e;
1496         line left 1 at last circle .w;
1497         box dashed with .nw at last circle .se + (0.2, -0.2);
1498         Caption: center of last box;
1499 }
1500 [junction();]
1501 move;
1502 compass([junction()]);
1503 .PE
1504 .CE "1: A sample composite object"
1505 .LP
1506 To refer to one of the composite's attachment points, you can say
1507 (for example) \fBA \&.s\fP.
1508 For purposes of object naming, composites are a class.
1509 You could write \fBlast [] \&.s\fP as an equivalent reference, usable
1510 anywhere a location is needed.
1511 This construction is very important for putting together large,
1512 multi-part diagrams.
1513 .PP
1514 Blocks are also a variable-scoping mechanism, like a \fIgroff\/\fP(1)
1515 environment.
1516 All variable assignments done inside a block are undone at the end of it.
1517 To get at values within a block, write a name of the block followed by a
1518 dot, followed by the label you want.
1519 For example, we could refer the center of the box in the above composite as
1520 \fBlast [] \&.Caption\fP or \fBA.Caption\fP.
1521 .PP
1522 This kind of reference to a label can be used in any way any other
1523 location can be.
1524 For example, if we added \fB"Hi!" at A.Caption\fP
1525 the result would look like this:
1526 .KS
1527 .PS
1528 A: [junction();]
1529 "Hi!" at A.Caption;
1530 .PE
1531 .CE "2: Adding a caption using interior labeling"
1532 .PP
1533 You can also use interior labels in either part of a \fBwith\fR
1534 modifier.
1535 This means that the example composite could be placed relative to its
1536 caption box by a command containing \fBwith A.Caption at\fP.
1537 .PP
1538 Note that both width and height of the block composite object are always
1539 positive:
1540 .KS
1541 .PS
1542 [
1543         [
1544                 box wid -0.5 ht 0.5
1545                 box wid 0.75 ht 0.75
1546         ]
1547         move 0.1 down 0.3 from last [].s
1548         "\fBbox wid -0.5 ht 0.5; box wid 0.75 ht 0.75\fP"
1549 ]
1550 move from last [].e 2
1551 [
1552         [
1553                 [ box wid -0.5 ht 0.5 ]
1554                 box wid 0.75 ht 0.75
1555         ]
1556         move 0.1 down 0.3 from last [].s
1557         "\fB[box wid -0.5 ht 0.5]; box wid 0.75 ht 0.75\fP"
1558 ]
1559 .PE
1560 .CE "3: Composite block objects always have positive width and height"
1561 .PP
1562 Blocks may be nested.
1563 This means you can use block attachment points to build up complex
1564 diagrams hierarchically, from the inside out.
1565 Note that \fBlast\fP and the other sequential naming mechanisms
1566 don't look inside blocks, so if you have a program that looks
1567 like
1568 .KS
1569 .DS
1570 .CW
1571 \&.PS
1572 P: [box "foo"; ellipse "bar"];
1573 Q: [
1574         [box "baz"; ellipse "quxx"]
1575         "random text";
1576    ]
1577 arrow from 2nd last [];
1578 \&.PE
1579 .R
1580 .DE
1581 .KE
1582 .LP
1583 the arrow in the last line is attached to object \fBP\fP, not
1584 object \fBQ\fP.
1585 .PP
1586 In DWB \fBpic\fP, only references one level deep into enclosed blocks
1587 were permitted.
1588 GNU \fBgpic\fP removes this restriction.
1589 .PP
1590 The combination of block variable scoping, assignability of labels and
1591 the macro facility that we'll describe later on can be used to
1592 simulate functions with local variables (just wrap the macro body in
1593 block braces).
1594 .
1595 .
1596 .NH 1
1597 Style Variables
1598 .PP
1599 There are a number of global style variables in \fBpic\fR that can be used to
1600 change its overall behavior.
1601 We've mentioned several of them in previous sections.
1602 They're all described here.
1603 For each variable, the default is given.
1604 .TS H
1605 center, tab(@), linesize(2);
1606 lb | lb | lb
1607 l | n | l.
1608 .sp 2p
1609 Style Variable@Default@What It Does
1610 .sp 2p
1611 _
1612 .sp 2p
1613 .TH
1614 boxht@0.5@Default height of a box
1615 boxwid@0.75@Default width of a box
1616 lineht@0.5@Default length of vertical line
1617 linewid@0.75@Default length of horizontal line
1618 linethick@-1@Default line thickness
1619 arcrad @0.25@Default radius of an arc
1620 circlerad@0.25@Default radius of a circle
1621 ellipseht@0.5@Default height of an ellipse
1622 ellipsewid@0.75@Default width of an ellipse
1623 moveht@0.5@Default length of vertical move
1624 movewid@0.75@Default length of horizontal move
1625 textht@0@Default height of box enclosing a text object
1626 textwid@0@Default width of box enclosing a text object
1627 arrowht@0.1@Length of arrowhead along shaft
1628 arrowwid@0.05@Width of rear of arrowhead
1629 arrowhead@1@Enable/disable arrowhead filling
1630 dashwid@0.05@Interval for dashed lines
1631 maxpswid@8.5@Maximum width of picture
1632 maxpsht@11@Maximum height of picture
1633 scale@1@Unit scale factor
1634 fillval@0.5@Default fill value
1635 .sp 5p
1636 _
1637 .TE
1638 Any of these variables can be set with a simple assignment statement.
1639 For example:
1640 .KS
1641 .PS
1642 [boxht=1; boxwid=0.3; movewid=0.2; box; move; box; move; box; move; box;]
1643 .PE
1644 .CE "1: \fBboxht=1; boxwid=0.3; movewid=0.2; box; move; box; move; box; move; box;\fP"
1645 .PP
1646 In GNU \fBpic\fR, setting the \fBscale\fR variable re-scales all
1647 size-related state variables so that their values remain equivalent in
1648 the new units.
1649 .PP
1650 The command \fBreset\fP resets all style variables to their defaults.
1651 You can give it a list of variable names as arguments (optionally
1652 separated by commas), in which case it resets only those.
1653 .PP
1654 State variables retain their values across pictures until reset.
1655 .
1656 .
1657 .NH 1
1658 Expressions, Variables, and Assignment
1659 .PP
1660 A number is a valid expression, of course (all numbers are stored
1661 internally as floating-point).
1662 Decimal-point notation is acceptable;
1663 in GNU \fBgpic\fR, scientific notation in C's `e' format (like
1664 \f(CW5e-2\fP) is accepted.
1665 .PP
1666 Anywhere a number is expected, the language also accepts a
1667 variable.
1668 Variables may be the built-in style variable described in the last
1669 section, or new variables created by assignment.
1670 .PP
1671 DWB \fBpic\fP supports only the ordinary assignment via \fB=\fP, which
1672 defines the variable (on the left side of the equal sign) in the current
1673 block if it is not already defined there, and then changes the value (on
1674 the right side) in the current block.
1675 The variable is not visible outside of the block.
1676 This is similar to the C\~programming language where a variable within a
1677 block shadows a variable with the same name outside of the block.
1678 .PP
1679 GNU \fBgpic\fP supports an alternate form of assignment using \fB:=\fP.
1680 The variable must already be defined, and the value is assigned to
1681 that variable without creating a variable local to the current block.
1682 For example, this
1683 .KS
1684 .DS
1685 .CW
1686 x=5
1687 y=5
1688 [
1689   x:=3
1690   y=3
1691 ]
1692 print x " " y
1693 .DE
1694 .KE
1695 .LP
1696 prints \fB3 5\fP.
1697 .PP
1698 You can use the height, width, radius, and x and y coordinates of any
1699 object or corner in expressions.
1700 If \fBA\fP is an object label or name, all the following are valid:
1701 .KS
1702 .DS
1703 .CW
1704 A.x                  # x coordinate of the center of A
1705 A.ne.y               # y coordinate of the northeast corner of A
1706 A.wid                # the width of A
1707 A.ht                 # and its height
1708 2nd last circle.rad  # the radius of the 2nd last circle
1709 .R
1710 .DE
1711 .KE
1712 .LP
1713 Note the second expression, showing how to extract a corner coordinate.
1714 .PP
1715 Basic arithmetic resembling those of C operators are available; \fB+\fP,
1716 \fB*\fP, \fB-\fP, \fB/\fP, and \fB%\fP.
1717 So is \fB^\fP for exponentiation.
1718 Grouping is permitted in the usual way using parentheses.
1719 GNU \fBgpic\fP allows logical operators to appear in expressions;
1720 \fB!\&\fP (logical negation, not factorial), \fB&&\fP, \fB|\||\fP,
1721 \fB==\fP, \fB!=\fP, \fB>=\fP, \fB<=\fP, \fB<\fP, \fB>\fP.
1722 .PP
1723 Various built-in functions are supported: \fBsin(\fIx\fB)\fR,
1724 \fBcos(\fIx\fB)\fR, \fBlog(\fIx\fB)\fR, \fBexp(\fIx\fB)\fR,
1725 \fBsqrt(\fIx\fB)\fR, \fBmax(\fIx\fB,\fIy\fB)\fR,
1726 \fBatan2(\fIx\fB,\fIy\fB)\fR, \fBmin(\fIx\fB,\fIy\fB)\fR,
1727 \fBint(\fIx\fB)\fR, \fBrand()\fP, and \fBsrand()\fP.
1728 Both \fBexp\fP and \fBlog\fP are
1729 base\~10; \fBint\fP does integer truncation; \fBrand()\fP returns a
1730 random number in [0-1), and \fBsrand()\fP sets the seed for
1731 a new sequence of pseudo-random numbers to be returned by \fBrand()\fP
1732 (\fBsrand()\fP is a GNU extension).
1733 .PP
1734 GNU \fBgpic\fP also documents a one-argument form or rand,
1735 \fBrand(\fIx\fB)\fR, which returns a random number between 1 and
1736 \fIx\fP, but this is deprecated and may be removed in a future
1737 version.
1738 .PP
1739 The function \fBsprintf()\fP behaves like a C \fIsprintf\/\fP(3)
1740 function that only takes %%, %e, %E, %f, %g, and %G conversion
1741 specifications.
1742 .
1743 .
1744 .NH 1
1745 Macros
1746 .PP
1747 You can define macros in \fBpic\fP, with up to 32 arguments (up to 16
1748 on EBCDIC platforms).
1749 This is useful for diagrams with repetitive parts.
1750 In conjunction with the scope rules for block composites, it effectively
1751 gives you the ability to write functions.
1752 .PP
1753 The syntax is
1754 .DS
1755 .CW
1756 \fBdefine\fP \fIname\fP \fB{\fP \fIreplacement text \fB}\fP
1757 .R
1758 .DE
1759 .LP
1760 This defines \fIname\fR as a macro to be replaced by the replacement
1761 text (not including the braces).
1762 The macro may be called as
1763 .DS
1764 .CW
1765 \fIname\fB(\fIarg1, arg2, \|.\|.\|.\& argn\fB)\fR
1766 .R
1767 .DE
1768 .LP
1769 The arguments (if any) are substituted for tokens \fB$1\fP, \fB$2\fP
1770 \&.\|.\|.\& \fB$n\fP
1771 appearing in the replacement text.
1772 .PP
1773 As an example of macro use, consider this:
1774 .KS
1775 .DS
1776 .CW
1777 .ps -1
1778 .vs -1
1779 \&.PS
1780 # Plot a single jumper in a box, $1 is the on-off state.
1781 define jumper { [
1782     shrinkfactor = 0.8;
1783     Outer: box invis wid 0.45 ht 1;
1784
1785     # Count on end ] to reset these
1786     boxwid = Outer.wid * shrinkfactor / 2;
1787     boxht  = Outer.ht  * shrinkfactor / 2;
1788
1789     box fill (!$1) with .s at center of Outer;
1790     box fill ($1)  with .n at center of Outer;
1791 ] }
1792
1793 # Plot a block of six jumpers.
1794 define jumperblock {
1795     jumper($1);
1796     jumper($2);
1797     jumper($3);
1798     jumper($4);
1799     jumper($5);
1800     jumper($6);
1801
1802     jwidth  = last [].Outer.wid;
1803     jheight = last [].Outer.ht;
1804
1805     box with .nw at 6th last [].nw wid 6*jwidth ht jheight;
1806
1807     # Use {} to avoid changing position from last box draw.
1808     # This is necessary so move in any direction works as expected
1809     {"Jumpers in state $1$2$3$4$5$6" at last box .s + (0,-0.2);}
1810 }
1811
1812 # Sample macro invocations.
1813 jumperblock(1,1,0,0,1,0);
1814 move;
1815 jumperblock(1,0,1,0,1,1);
1816 \&.PE
1817 .ps
1818 .vs
1819 .R
1820 .DE
1821 .KE
1822 .LP
1823 It yields the following:
1824 .KS
1825 .PS
1826 # Plot a single jumper in a box, $1 is the on-off state.
1827 define jumper { [
1828     shrinkfactor = 0.8;
1829     Outer: box invis wid 0.45 ht 1;
1830
1831     # Count on end ] to reset these
1832     boxwid = Outer.wid * shrinkfactor / 2;
1833     boxht  = Outer.ht  * shrinkfactor / 2;
1834
1835     box fill (!$1) with .s at center of Outer;
1836     box fill ($1)  with .n at center of Outer;
1837 ] }
1838
1839 # Plot a block of six jumpers
1840 define jumperblock {
1841     jumper($1);
1842     jumper($2);
1843     jumper($3);
1844     jumper($4);
1845     jumper($5);
1846     jumper($6);
1847
1848     jwidth  = last [].Outer.wid;
1849     jheight = last [].Outer.ht;
1850
1851     box with .nw at 6th last [].nw wid 6*jwidth ht jheight;
1852
1853     # Use {} to avoid changing position from last box draw.
1854     # This is necessary so move in any direction works as expected
1855     {"Jumpers in state $1$2$3$4$5$6" at last box .s + (0,-0.2);}
1856 }
1857
1858 # Sample macro invocations
1859 jumperblock(1,1,0,0,1,0);
1860 move 0.25;
1861 jumperblock(1,0,1,0,1,1);
1862 .PE
1863 .CE "1: Sample use of a macro"
1864 .LP
1865 This macro example illustrates how you can combine [], brace grouping,
1866 and variable assignment to write true functions.
1867 .PP
1868 One detail the example above does not illustrate is the fact that
1869 macro argument parsing is not token-oriented.
1870 If you call \fBjumper(\~1\~)\fP, the value of $1 is \fB"\~1\~"\fP.
1871 You could even call \fBjumper(big\~string)\fP to give $1 the value
1872 \fB"big\~string"\fP.
1873 .PP
1874 If you want to pass in a coordinate pair, you can avoid getting
1875 tripped up by the comma by wrapping the pair in parentheses.
1876 .PP
1877 Macros persist through pictures.
1878 To undefine a macro, say \fBundef\fP \fIname\fR; for example,
1879 .DS
1880 \f(CWundef jumper\fP
1881 \f(CWundef jumperblock\fP
1882 .DE
1883 .LP
1884 would undefine the two macros in the jumper block example.
1885 .
1886 .
1887 .NH 1
1888 Import/Export Commands
1889 .PP
1890 Commands that import or export data between \fBpic\fR and its
1891 environment are described here.
1892 .
1893 .NH 2
1894 File and Table Insertion
1895 .PP
1896 The statement
1897 .DS
1898 \f(CWcopy\fP \fIfilename\fR
1899 .DE
1900 .LP
1901 inserts the contents of \fIfilename\fR in the \fBpic\fP input stream.
1902 Any \fB.PS\fP/\fB.PE\fP pair in the file is ignored.
1903 You can use this to include pre-generated images.
1904 .PP
1905 A variant of this statement replicates the \fBcopy thru\fP feature of
1906 \fIgrap\/\fP(1).
1907 The call
1908 .DS
1909 \f(CWcopy\fP \fIfilename\fR \f(CWthru\fP \fImacro\fP
1910 .DE
1911 .LP
1912 calls \fImacro\fP (which may be either a name or replacement text)
1913 on the arguments obtained by breaking each line of the file into
1914 blank-separated fields.
1915 The macro may have up to 9\~arguments.
1916 The replacement text may be delimited by braces or by a pair of
1917 instances of any character not appearing in the rest of the text.
1918 .PP
1919 If you write
1920 .DS
1921 \f(CWcopy\fP \f(CWthru\fP \fImacro\fP
1922 .DE
1923 .LP
1924 omitting the filename, lines to be parsed are taken from the input
1925 source up to the next \fB.PE\fP.
1926 .PP
1927 In either of the last two \fBcopy\fP commands, GNU \fBgpic\fP permits a
1928 trailing `\fBuntil\fP \fIword\/\fP' clause to be added which terminates
1929 the copy when the first word matches the argument (the default
1930 behavior is therefore equivalent to \fBuntil \&.PE\fP).
1931 .PP
1932 Accordingly, the command
1933 .RS
1934 .KS
1935 .IP
1936 .CW
1937 .nf
1938 \&.PS
1939 copy thru % circle at ($1,$2) % until "END"
1940 1 2
1941 3 4
1942 5 6
1943 END
1944 box
1945 \&.PE
1946 .R
1947 .fi
1948 .KE
1949 .RE
1950 .LP
1951 is equivalent to
1952 .RS
1953 .KS
1954 .IP
1955 .CW
1956 .nf
1957 \&.PS
1958 circle at (1,2)
1959 circle at (3,4)
1960 circle at (5,6)
1961 box
1962 \&.PE
1963 .R
1964 .fi
1965 .KE
1966 .RE
1967 .
1968 .NH 2
1969 Debug Messages
1970 .PP
1971 The command \fBprint\fR accepts any number of arguments, concatenates
1972 their output forms, and writes the result to standard error.
1973 Each argument must be an expression, a position, or a text string.
1974 .
1975 .NH 2
1976 Escape to Post-Processor
1977 .PP
1978 If you write
1979 .DS
1980 \fBcommand\fR \fIarg\fR\|.\|.\|.
1981 .DE
1982 .LP
1983 \fBpic\fP concatenates the arguments and pass them through as a line
1984 to troff or \*[tx].
1985 Each
1986 .I arg
1987 must be an expression, a position, or text.
1988 This has a similar effect to a line beginning with
1989 .B .
1990 or
1991 \fB\e\fR\|,
1992 but allows the values of variables to be passed through.
1993 .LP
1994 For example,
1995 .KS
1996 .DS
1997 .CW
1998 .nf
1999 \&.PS
2000 x = 14
2001 command ".ds string x is " x "."
2002 \&.PE
2003 \e*[string]
2004 .DE
2005 .R
2006 .KE
2007 .LP
2008 prints
2009 .DS
2010 .CW
2011 x is 14.
2012 .R
2013 .DE
2014 .
2015 .NH 2
2016 Executing Shell Commands
2017 .PP
2018 The command
2019 .DS
2020 \f(CWsh\fP \f(CW{\fP \fIanything.\|.\|.\fP \f(CW}\fP
2021 .DE
2022 .LP
2023 macro-expands the text in braces, then executes it as a shell command.
2024 This could be used to generate images or data tables for later
2025 inclusion.
2026 The delimiters shown as {} here may also be two copies of any one
2027 character not present in the shell command text.
2028 In either case, the body may contain balanced {} pairs.
2029 Strings in the body may contain balanced or unbalanced braces in any
2030 case.
2031 .
2032 .
2033 .NH 1
2034 Control-flow constructs
2035 .PP
2036 The \fBpic\fP language provides conditionals and looping.
2037 For example,
2038 .KS
2039 .DS
2040 .CW
2041 pi = atan2(0,-1);
2042 for i = 0 to 2 * pi by 0.1 do {
2043     "-" at (i/2, 0);
2044     "." at (i/2, sin(i)/2);
2045     ":" at (i/2, cos(i)/2);
2046 }
2047 .R
2048 .DE
2049 .KE
2050 .LP
2051 which yields this:
2052 .KS
2053 .PS
2054 pi = atan2(0,-1);
2055 for i = 0 to 2 * pi by 0.1 do {
2056     "-" at (i/2, 0);
2057     "." at (i/2, sin(i)/2);
2058     ":" at (i/2, cos(i)/2);
2059 }
2060 .PE
2061 .CE "1: Plotting with a \fBfor\fP loop"
2062 .LP
2063 The syntax of the \fBfor\fP statement is:
2064 .DS
2065 \fBfor\fR \fIvariable\fR \fB=\fR \fIexpr1\/\fR \fBto\fR \fIexpr2\/\fR \
2066 [\fBby\fR [\fB*\fR]\fIexpr3\/\fR] \fBdo\fR \fIX\fR \fIbody\fR \fIX\fR
2067 .DE
2068 The semantics are as follows:  Set
2069 .I variable
2070 to \fIexpr1\fR.
2071 While the value of
2072 .I variable
2073 is less than or equal to
2074 \fIexpr2\fR,
2075 do
2076 .I body
2077 and increment
2078 .I variable
2079 by
2080 \fIexpr3\fR;
2081 if
2082 .B by
2083 is not given, increment
2084 .I variable
2085 by\~1.
2086 If
2087 .I expr3
2088 is prefixed by\~\c
2089 .B *
2090 then
2091 .I variable
2092 is multiplied instead by
2093 \fIexpr3\fR.
2094 The value of
2095 .I expr3
2096 can be negative for the additive case;
2097 .I variable
2098 is then tested whether it is greater than or equal to
2099 \fIexpr2\fR.
2100 For the multiplicative case,
2101 .I expr3
2102 must be greater than zero.
2103 If the constraints aren't met, the loop isn't executed.
2104 .I X
2105 can be any character not occurring in
2106 \fIbody\fR; or the two \fIX\/\fPs may be paired braces (as in the
2107 \fBsh\fR command).
2108 .PP
2109 The syntax of the \fBif\fP statement is as follows:
2110 .DS
2111 \fBif\fR \fIexpr\fR \fBthen\fR \fIX\fR \fIif-true\fR \fIX\fR \
2112 [\fBelse\fR \fIY\fR \fIif-false\fR \fIY\/\fR]
2113 .DE
2114 Its semantics are as follows: Evaluate
2115 \fIexpr\fR;
2116 if it is non-zero then do
2117 \fIif-true\fR,
2118 otherwise do
2119 \fIif-false\fR.
2120 .I X
2121 can be any character not occurring in
2122 \fIif-true\fR.
2123 .I Y
2124 can be any character not occurring in
2125 \fIif-false\fR.
2126 .PP
2127 Eithe or both of the
2128 .I X
2129 or
2130 .I Y
2131 pairs may instead be balanced pairs of
2132 braces ({ and\~}) as in the \fBsh\fR command.
2133 In either case, the \fIif-true\fR may contain balanced pairs of braces.
2134 None of these delimiters are seen inside strings.
2135 .PP
2136 All the usual relational operators my be used in conditional expressions;
2137 \fB!\&\fP (logical negation, not factorial), \fB&&\fP, \fB|\||\fP, \fB==\fP,
2138 \fB!=\fP, \fB>=\fP, \fB<=\fP, \fB<\fP, \fB>\fP.
2139 .PP
2140 String comparison is also supported using \fB==\fP and \fB!=\fP.
2141 String comparisons may need to be parenthesized to avoid syntactic
2142 ambiguities.
2143 .
2144 .
2145 .NH 1
2146 Interface To [gt]roff
2147 .PP
2148 The output of \fBpic\fP is \fB[gt]roff\fP drawing commands.
2149 The GNU \fIgpic\/\fP(1) command warns that it relies on drawing
2150 extensions present in \fIgroff\/\fP(1) that are not present in
2151 \fItroff\/\fP(1).
2152 .
2153 .NH 2
2154 Scaling Arguments
2155 .PP
2156 The DWB \fIpic\/\fP(1) program accepts one or two arguments to
2157 \&\fB.PS\fP, which is interpreted as a width and height in inches to
2158 which the results of \fIpic\/\fP(1) should be scaled (width and height
2159 scale independently).
2160 If there is only one argument, it is interpreted as a width to scale the
2161 picture to, and height is scaled by the same proportion.
2162 .PP
2163 GNU \fBgpic\fP is less general; it accepts a single width to scale
2164 to, or a zero width and a maximum height to scale to.
2165 With two non-zero arguments, it scales to the maximum height.
2166 .
2167 .NH 2
2168 How Scaling is Handled
2169 .PP
2170 When \fBpic\fP processes a picture description on input, it passes
2171 \fB.PS\fP, \fB.PE\fP, and \fB.PF\fP through to the postprocessor.
2172 The \fB.PS\fP gets decorated with two numeric arguments which are the X
2173 and Y\~dimensions of the picture in inches.
2174 The post-processor can use these to reserve space for the picture and
2175 center it.
2176 .PP
2177 The GNU incarnation of the \fBms\fP macro package, for example, includes
2178 the following definitions:
2179 .KS
2180 .DS
2181 .ps -1
2182 .vs -1
2183 .CW
2184 \&.de PS
2185 \&.br
2186 \&.sp \e\en[DD]u
2187 \&.ie \e\en[.$]<2 .@error bad arguments to PS (not preprocessed with pic?)
2188 \&.el \e{\e
2189 \&.     ds@need (u;\e\e$1)+1v
2190 \&.     in +(u;\e\en[.l]-\e\en[.i]-\e\e$2/2>?0)
2191 \&.\e}
2192 \&..
2193 \&.de PE
2194 \&.par@reset
2195 \&.sp \e\en[DD]u+.5m
2196 \&..
2197 .R
2198 .DE
2199 .ps
2200 .vs
2201 .KE
2202 .LP
2203 Equivalent definitions of these and of \fB.PF\fP are supplied by GNU
2204 \fIpic\/\fP(1) if you use the \-mpic option; this should make it usable
2205 with macro pages other than \fIms\/\fR(1).
2206 .PP
2207 If \fB.PF\fP is used instead of \fB.PE\fP, the \fBtroff\fP position is
2208 restored to what it was at the picture start (Kernighan notes that
2209 the\~F stands for \[lq]flyback\[rq]).
2210 .PP
2211 The invocation
2212 .DS
2213 \&\fB.PS <\,\fP\fIfile\fP
2214 .DE
2215 .LP
2216 causes the contents of \fIfile\fP to replace the \fB.PS\fP line.
2217 This feature is deprecated; use `\fBcopy\fP \fIfile\fR' instead.
2218 .
2219 .NH 2
2220 PIC and [gt]roff commands
2221 .PP
2222 By default, input lines that begin with a period are passed to the
2223 postprocessor, embedded at the corresponding point in the output.
2224 Messing with horizontal or vertical spacing is an obvious recipe for
2225 bugs, but point size and font changes are usually safe.
2226 .PP
2227 Point sizes and font changes are also safe within text strings, as
2228 long as they are undone before the end of string.
2229 .PP
2230 The enablement of output line filling by \fB[gt]roff\fP is preserved
2231 across pictures.
2232 .
2233 .NH 2
2234 PIC and EQN
2235 .PP
2236 The Kernighan paper notes that there is a subtle problem with
2237 complicated equations inside \fBpic\fR pictures; they come out wrong if
2238 \fIeqn\/\fP(1) has to leave extra vertical space for the equation.
2239 If your equation involves more than subscripts and superscripts, you
2240 must add to the beginning of each equation the extra information
2241 \fBspace\~0\fP.
2242 He gives the following example:
2243 .KS
2244 .DS
2245 .CW
2246 arrow
2247 box "$space 0 {H( omega )} over {1 - H( omega )}$"
2248 arrow
2249 .R
2250 .DE
2251 .KE
2252 .EQ
2253 delim @@
2254 .EN
2255 .KS
2256 .PS
2257 arrow
2258 box "@space 0 {H( omega )} over {1 - H( omega )}@"
2259 arrow
2260 .PE
2261 .CE "1: Equations within pictures"
2262 .
2263 .NH 2
2264 Absolute Positioning of Pictures
2265 .PP
2266 A \fBpic\fP picture is positioned vertically by troff at the current
2267 position.
2268 The topmost position possible on a page is not the paper edge
2269 but a position which is one baseline lower so that the first row of glyphs
2270 is visible.
2271 To make a picture really start at the paper edge you have to make the
2272 baseline-to-baseline distance zero, this is, you must set the vertical
2273 spacing to\~0 (using \fB.vs\fP) before starting the picture.
2274 .
2275 .
2276 .NH 1
2277 Interface to TeX
2278 .PP
2279 .PP
2280 \*[tx] mode is enabled by the
2281 .B \-t
2282 option.
2283 In \*[tx] mode, pic defines a vbox called
2284 .B \egraph
2285 for each picture; the name can be changed with the pseudo-variable
2286 .B figname
2287 (which is actually a specially parsed command).
2288 You must yourself print that vbox using, for example, the command
2289 .RS
2290 .LP
2291 .CW
2292 \ecenterline{\ebox\egraph}
2293 .RE
2294 .LP
2295 Actually, since the vbox has a height of zero (it is defined with \evtop)
2296 this produces slightly more vertical space above the picture than
2297 below it;
2298 .RS
2299 .LP
2300 .CW
2301 \ecenterline{\eraise 1em\ebox\egraph}
2302 .RE
2303 .LP
2304 would avoid this.
2305 .PP
2306 To make the vbox having a positive height and a depth of zero (as used
2307 e.g.\& by \*(lx's \f(CW\%graphics.sty\fP), define the following macro in
2308 your document:
2309 .KS
2310 .DS
2311 .CW
2312 \edef\egpicbox#1{%
2313   \evbox{\eunvbox\ecsname #1\eendcsname\ekern 0pt}}
2314 .R
2315 .DE
2316 .KE
2317 .LP
2318 Now you can simply say
2319 .B \egpicbox{graph}
2320 instead of \ebox\egraph.
2321 .PP
2322 You must use a \*[tx] driver that supports the
2323 .B tpic
2324 specials, version\~2.
2325 .PP
2326 Lines beginning with
2327 .B \e
2328 are passed through transparently; a
2329 .B %
2330 is added to the end of the line to avoid unwanted spaces.
2331 You can safely use this feature to change fonts or to
2332 change the value of \fB\ebaselineskip\fP.
2333 Anything else may well produce undesirable results; use at your own risk.
2334 Lines beginning with a period are not given any special treatment.
2335 .PP
2336 The \*[tx] mode of \fIpic\/\fP(1) does \fInot\fP translate \fBtroff\fP
2337 font and size changes included in text strings!
2338 .PP
2339 Here an example how to use \fBfigname\fP.
2340 .KS
2341 .DS
2342 .CW
2343 \&.PS
2344 figname = foo;
2345 \&...
2346 \&.PE
2347
2348 \&.PS
2349 figname = bar;
2350 \&...
2351 \&.PE
2352
2353 \ecenterline{\ebox\efoo \ehss \ebox\ebar}
2354 .DE
2355 .R
2356 .KE
2357 .LP
2358 Use this feature sparsingly and only if really needed:
2359 A different name means a new box register in \*[tx], and the maximum number
2360 of box registers is only 256.
2361 Also be careful not to use a predefined \*[tx] or \*[lx] macro name as
2362 an argument to \fBfigname\fP since this inevitably causes an error.
2363 .
2364 .
2365 .NH 1
2366 Obsolete Commands
2367 .PP
2368 GNU \fIgpic\/\fP(1)  has a command
2369 .DS
2370 \fBplot\fR \fIexpr\fR [\fB"\fItext\fB"\fR]
2371 .DE
2372 This is a text object which is constructed by using
2373 .I text
2374 as a format string for sprintf
2375 with an argument of
2376 \fIexpr\fP.
2377 If
2378 .I text
2379 is omitted a format string of \fB"%g"\fP is used.
2380 Attributes can be specified in the same way as for a normal text
2381 object.
2382 Be very careful that you specify an appropriate format string;
2383 \fBpic\fP does only very limited checking of the string.
2384 This is deprecated in favour of
2385 \fBsprintf\fP.
2386 .
2387 .
2388 .NH 1
2389 Some Larger Examples
2390 .PP
2391 Here are a few larger examples, with complete source code.
2392 One of our earlier examples is generated in an instructive way using a
2393 for loop:
2394 .KS
2395 .DS
2396 .ps -1
2397 .vs -1
2398 .CW
2399 \&.PS
2400 # Draw a demonstration up left arrow with grid box overlay
2401 define gridarrow
2402 {
2403     move right 0.1
2404     [
2405         {arrow up left $1;}
2406         box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
2407         for i = 2 to ($1 / 0.5) do
2408         {
2409             box wid 0.5 ht 0.5 dotted with .sw at last box .se;
2410         }
2411         move down from last arrow .center;
2412         [
2413             sprintf("\efBarrow up left %g\efP", $1)
2414         ]
2415     ]
2416     move right 0.1 from last [] .e;
2417 }
2418 gridarrow(0.5);
2419 gridarrow(1);
2420 gridarrow(1.5);
2421 gridarrow(2);
2422 undef gridarrow
2423 \&.PE
2424 .R
2425 .DE
2426 .ps
2427 .vs
2428 .KE
2429 .KS
2430 .PS
2431 # Draw a demonstration up left arrow with grid box overlay
2432 define gridarrow
2433 {
2434         move right 0.5
2435         [
2436                 {arrow up left $1;}
2437                 box wid 0.5 ht 0.5 dotted with .nw at last arrow .end;
2438                 for i = 2 to ($1 / 0.5) do {
2439                         box wid 0.5 ht 0.5 dotted with .sw at last box .se;
2440                 }
2441                 move down from last arrow .center;
2442                 [
2443                         sprintf("\fBarrow up left %g\fP", $1)
2444                 ]
2445         ]
2446         move right 0.1 from last [] .e;
2447 }
2448 gridarrow(0.5);
2449 gridarrow(1);
2450 gridarrow(1.5);
2451 gridarrow(2);
2452 undef gridarrow
2453 .PE
2454 .CE "1: Diagonal arrows (dotted boxes show the implied 0.5-inch grid)"
2455 .PP
2456 Here's an example concocted to demonstrate layout of a large,
2457 multiple-part pattern:
2458 .KS
2459 .DS
2460 .ps -1
2461 .vs -1
2462 .CW
2463 \&.PS
2464 define filter {box ht 0.25 rad 0.125}
2465 lineht = 0.25;
2466 Top: [
2467     right;
2468     box "\efBms\efR" "sources";
2469     move;
2470     box "\efBHTML\efR" "sources";
2471     move;
2472     box "\efBlinuxdoc-sgml\efP" "sources" wid 1.5;
2473     move;
2474     box "\efBTexinfo\efP" "sources";
2475
2476     line down from 1st box .s lineht;
2477     A: line down;
2478     line down from 2nd box .s; filter "\efBhtml2ms\efP";
2479     B: line down;
2480     line down from 3rd box .s; filter "\efBformat\efP";
2481     C: line down;
2482     line down from 4th box .s; filter "\efBtexi2roff\efP";
2483     D: line down;
2484 ]
2485 move down 1 from last [] .s;
2486 Anchor: box wid 1 ht 0.75 "\efBms\efR" "intermediate" "form";
2487 arrow from Top.A.end to Anchor.nw;
2488 arrow from Top.B.end to 1/3 of the way between Anchor.nw and Anchor.ne;
2489 arrow from Top.C.end to 2/3 of the way between Anchor.nw and Anchor.ne;
2490 arrow from Top.D.end to Anchor.ne
2491 {
2492     # PostScript column
2493     move to Anchor .sw;
2494     line down left then down ->;
2495     filter "\efBpic\efP";
2496     arrow;
2497     filter "\efBeqn\efP";
2498     arrow;
2499     filter "\efBtbl\efP";
2500     arrow;
2501     filter "\efBgroff\efP";
2502     arrow;
2503     box "PostScript";
2504
2505     # HTML column
2506     move to Anchor .se;
2507     line down right then down ->;
2508     A: filter dotted "\efBpic2img\efP";
2509     arrow;
2510     B: filter dotted "\efBeqn2html\efP";
2511     arrow;
2512     C: filter dotted "\efBtbl2html\efP";
2513     arrow;
2514     filter "\efBms2html\efP";
2515     arrow;
2516     box "HTML";
2517
2518     # Nonexistence caption
2519     box dashed wid 1 at B + (2,0) "These tools" "don't yet exist";
2520     line chop 0 chop 0.1 dashed from last box .nw to A.e ->;
2521     line chop 0 chop 0.1 dashed from last box .w  to B.e ->;
2522     line chop 0 chop 0.1 dashed from last box .sw to C.e ->;
2523 }
2524 \&.PE
2525 .R
2526 .DE
2527 .ps
2528 .vs
2529 .KE
2530 .KS
2531 .PS
2532 define filter {box ht 0.25 rad 0.125}
2533 lineht = 0.25;
2534 Top: [
2535     right;
2536     box "\fBms\fR" "sources";
2537     move;
2538     box "\fBHTML\fR" "sources";
2539     move;
2540     box "\fBlinuxdoc-sgml\fP" "sources" wid 1.5;
2541     move;
2542     box "\fBTexinfo\fP" "sources";
2543
2544     line down from 1st box .s lineht;
2545     A: line down;
2546     line down from 2nd box .s; filter "\fBhtml2ms\fP";
2547     B: line down;
2548     line down from 3rd box .s; filter "\fBformat\fP";
2549     C: line down;
2550     line down from 4th box .s; filter "\fBtexi2roff\fP";
2551     D: line down;
2552 ]
2553 move down 1 from last [] .s;
2554 Anchor: box wid 1 ht 0.75 "\fBms\fR" "intermediate" "form";
2555 arrow from Top.A.end to Anchor.nw;
2556 arrow from Top.B.end to 1/3 of the way between Anchor.nw and Anchor.ne;
2557 arrow from Top.C.end to 2/3 of the way between Anchor.nw and Anchor.ne;
2558 arrow from Top.D.end to Anchor.ne
2559 {
2560     # PostScript column
2561     move to Anchor .sw;
2562     line down left then down ->;
2563     filter "\fBpic\fP";
2564     arrow;
2565     filter "\fBeqn\fP";
2566     arrow;
2567     filter "\fBtbl\fP";
2568     arrow;
2569     filter "\fBgroff\fP";
2570     arrow;
2571     box "PostScript";
2572
2573     # HTML column
2574     move to Anchor .se;
2575     line down right then down ->;
2576     A: filter dotted "\fBpic2img\fP";
2577     arrow;
2578     B: filter dotted "\fBeqn2html\fP";
2579     arrow;
2580     C: filter dotted "\fBtbl2html\fP";
2581     arrow;
2582     filter "\fBms2html\fP";
2583     arrow;
2584     box "HTML";
2585
2586     # Nonexistence caption
2587     box dashed wid 1 at B + (2,0) "These tools" "don't yet exist";
2588     line chop 0 chop 0.1 dashed from last box .nw to A.e ->;
2589     line chop 0 chop 0.1 dashed from last box .w  to B.e ->;
2590     line chop 0 chop 0.1 dashed from last box .sw to C.e ->;
2591 }
2592 .PE
2593 .CE "2: Hypothetical production flow for dual-mode publishing"
2594 .PP
2595 .KS
2596 .PS
2597 # a three-dimensional block
2598 #
2599 # tblock(<width>, <height>, <text>)
2600
2601 define tblock { [
2602   box ht $2 wid $1 \
2603       color "gold" outlined "black" \
2604       xslanted 0 yslanted 0 \
2605       $3;
2606   box ht .1 wid $1 \
2607       color "yellow" outlined "black" \
2608       xslanted .1 yslanted 0 \
2609       with .sw at last box .nw;
2610   box ht $2 wid .1 \
2611       color "goldenrod" outlined "black" \
2612       xslanted 0 yslanted .1 \
2613       with .nw at 2nd last box .ne;
2614 ] }
2615
2616 tblock(1, .5, "Master" "1");
2617 move -.1
2618 tblock(.5, 1, "Slave");
2619 .PE
2620 .CE "3: Three-dimensional Boxes"
2621 .PP
2622 Here the source code for figure \n[H1]-3:
2623 .KS
2624 .DS
2625 .CW
2626 \&.PS
2627 # a three-dimensional block
2628 #
2629 # tblock(<width>, <height>, <text>)
2630
2631 define tblock { [
2632   box ht $2 wid $1 \e
2633       color "gold" outlined "black" \e
2634       xslanted 0 yslanted 0 \e
2635       $3;
2636   box ht .1 wid $1 \e
2637       color "yellow" outlined "black" \e
2638       xslanted .1 yslanted 0 \e
2639       with .sw at last box .nw;
2640   box ht $2 wid .1 \e
2641       color "goldenrod" outlined "black" \e
2642       xslanted 0 yslanted .1 \e
2643       with .nw at 2nd last box .ne;
2644 ] }
2645
2646 tblock(1, .5, "Master" "1");
2647 move -.1
2648 tblock(.5, 1, "Slave");
2649 \&.PE
2650 .DE
2651 .ft R
2652 .KE
2653 .
2654 .
2655 .
2656 .NH 1
2657 PIC Reference
2658 .PP
2659 This is an annotated grammar of \fBpic\fP.
2660 .
2661 .NH 2
2662 Lexical Items
2663 .PP
2664 In general, \fBpic\fP is a free-format, token-oriented language that
2665 ignores whitespace outside strings.
2666 But certain lines and constructs are specially interpreted at the
2667 lexical level:
2668 .PP
2669 A comment begins with \fB#\fP and continues to \fB\en\fP (comments may
2670 also follow text in a line).
2671 A line beginning with a period or backslash may be interpreted as text
2672 to be passed through to the post-processor, depending on command-line
2673 options.
2674 An end-of-line backslash is interpreted as a request to continue the
2675 line; the backslash and following newline are ignored.
2676 .PP
2677 .RS
2678 Here are the grammar terminals:
2679 .IP \s[-1]INT\s[0]
2680 A positive integer.
2681 .IP \s[-1]NUMBER\s[0]
2682 A floating point numeric constant.
2683 May contain a decimal point or be expressed in scientific notation in
2684 the style of \fIprintf\/\fP(3)'s %e escape.
2685 A trailing `i' or `I' (indicating the unit `inch') is ignored.
2686 .IP \s[-1]TEXT\s[0]
2687 A string enclosed in double quotes.
2688 A double quote within \s[-1]TEXT\s[0] must be preceded by a backslash.
2689 Instead of \s[-1]TEXT\s[0] you can use
2690 .DS
2691 .CW
2692 sprintf ( TEXT [, <expr> ...] )
2693 .R
2694 .DE
2695 .IP
2696 except after the `until' and `last' keywords, and after all ordinal
2697 keywords (`th' and friends).
2698 .IP \s[-1]VARIABLE\s[0]
2699 A string starting with a character from the set [a-z], optionally
2700 followed by one or more characters of the set [a-zA-Z0-9_].
2701 (Values of variables are preserved across pictures.)
2702 .IP \s[-1]LABEL\s[0]
2703 A string starting with a character from the set [A-Z], optionally
2704 followed by one or more characters of the set [a-zA-Z0-9_].
2705 .IP \s[-1]COMMAND-LINE\s[0]
2706 A line starting with a command character (`.' in groff mode, `\e' in
2707 \*[tx] mode).
2708 .IP \s[-1]BALANCED-TEXT\s[0]
2709 A string either enclosed by `{' and `}' or with \fIX\fP and \fIX\fP,
2710 where \fIX\fP doesn't occur in the string.
2711 .IP \s[-1]BALANCED-BODY\s[0]
2712 Delimiters as in \s[-1]BALANCED-TEXT\s[0]; the body is interpreted as
2713 `\fB\[la]command\[ra].\|.\|.\fP'.
2714 .IP \s[-1]FILENAME\s[0]
2715 The name of a file.
2716 This has the same semantics as \s[-1]TEXT\s[0].
2717 .IP \s[-1]MACRONAME\s[0]
2718 Either \s[-1]VARIABLE\s[0] or \s[-1]LABEL\s[0].
2719 .RE
2720 .
2721 .NH 2
2722 Semi-Formal Grammar
2723 .PP
2724 Tokens not enclosed in \[la]\|\[ra] are literals, except:
2725 .IP 1.
2726 \fB\en\fP is a newline.
2727 .IP 2.
2728 Three dots is a suffix meaning `replace with 0 or more repetitions
2729 of the preceding element(s).
2730 .IP 3.
2731 An enclosure in square brackets has its usual meaning of `this clause is
2732 optional'.
2733 .IP 4.
2734 Square-bracket-enclosed portions within tokens are optional.
2735 Thus, `h\^[eigh]\^t' matches either `height' or `ht'.
2736 .LP
2737 If one of these special tokens has to be referred to literally, it is
2738 surrounded with single quotes.
2739 .PP
2740 The top-level \fBpic\fP object is a picture.
2741 .DS
2742 .CW
2743 <picture> ::=
2744   .PS [NUMBER [NUMBER]]\en
2745   <statement> ...
2746   .PE \en
2747 .R
2748 .DE
2749 .PP
2750 The arguments, if present, represent the width and height of the picture,
2751 causing \fBpic\fR to attempt to scale it to the given dimensions in
2752 inches.
2753 In no case, however, the X and Y\~dimensions of the picture exceed the
2754 values of the style variables \fBmaxpswid\fP and \fBmaxpsheight\fP
2755 (which default to the normal 8.5\^i by 11\^i page size).
2756 .PP
2757 If the ending `.PE' is replaced by `.PF', the page vertical position is
2758 restored to its value at the time `.PS' was encountered.
2759 Another alternate form of invocation is `.PS\~<\s[-1]FILENAME\s[0]',
2760 which replaces the `.PS' line with a file to be interpreted by \fBpic\fR
2761 (but this feature is deprecated).
2762 .PP
2763 The `.PS', `.PE', and `.PF' macros to perform centering and scaling are
2764 normally supplied by the post-processor.
2765 .PP
2766 In the following, either `|' or a new line starts an alternative.
2767 .DS
2768 .CW
2769 <statement> ::=
2770   <command> ;
2771   <command> \en
2772 .R
2773 .DE
2774 .DS
2775 .CW
2776 <command> ::=
2777   <primitive> [<attribute>]
2778   LABEL : [;] <command>
2779   LABEL : [;] <command> [<position>]
2780   { <command> ... }
2781   VARIABLE [:] = <any-expr>
2782   figname = MACRONAME
2783   up | down | left | right
2784   COMMAND-LINE
2785   command <print-arg> ...
2786   print <print-arg> ...
2787   sh BALANCED-TEXT
2788   copy FILENAME
2789   copy [FILENAME] thru MACRONAME [until TEXT]
2790   copy [FILENAME] thru BALANCED-BODY [until TEXT]
2791   for VARIABLE = <expr> to <expr> [by [*] <expr>] do BALANCED-BODY
2792   if <any-expr> then BALANCED-BODY [else BALANCED-BODY]
2793   reset [VARIABLE [[,] VARIABLE ...]]
2794 .R
2795 .DE
2796 .DS
2797 .CW
2798 <print-arg> ::=
2799   TEXT
2800   <expr>
2801   <position>
2802 .R
2803 .DE
2804 .PP
2805 The current position and direction are saved on entry to a
2806  `{\~.\|.\|.\~}' construction and restored on exit from it.
2807 .PP
2808 Note that in `if' constructions, newlines can only occur in
2809 \s[-1]BALANCED-BODY\s[0].
2810 This means that
2811 .DS
2812 .CW
2813 if
2814 { ... }
2815 else
2816 { ... }
2817 .R
2818 .DE
2819 .PP
2820 fails.
2821 You have to use the braces on the same line as the keywords:
2822 .DS
2823 .CW
2824 if {
2825 \&...
2826 } else {
2827 \&...
2828 }
2829 .R
2830 .DE
2831 .PP
2832 This restriction doesn't hold for the body after the `do' in a `for'
2833 construction.
2834 .PP
2835 At the beginning of each picture, `figname' is reset to the vbox name
2836 `graph'; this command has only a meaning in \*[tx] mode.
2837 While the grammar rules allow digits and the underscore in the value of
2838 `figname', \*[tx] normally accepts uppercase and lowercase letters only
2839 as box names (you have to use `\ecsname' if you really need to
2840 circumvent this limitation).
2841 .DS
2842 .CW
2843 <any-expr> ::=
2844   <expr>
2845   <text-expr>
2846   <any-expr> <logical-op> <any-expr>
2847   ! <any-expr>
2848 .R
2849 .DE
2850 .DS
2851 .CW
2852 <logical-op> ::=
2853   == | != | && | '||'
2854 .R
2855 .DE
2856 .DS
2857 .CW
2858 <text-expr> ::=
2859   TEXT == TEXT
2860   TEXT != TEXT
2861 .R
2862 .DE
2863 .PP
2864 Logical operators are handled specially by \fBpic\fP since they can
2865 deal with text strings also.
2866 \fBpic\fP uses \%\fIstrcmp\/\fP(3) to test for equality of strings; an
2867 empty string is considered as `false' for `&&' and `|\||'.
2868 .DS
2869 .CW
2870 <primitive> ::=
2871   box                   \fR# closed object \[em] rectangle\fP
2872   circle                \fR# closed object \[em] circle\fP
2873   ellipse               \fR# closed object \[em] ellipse\fP
2874   arc                   \fR# open object \[em] quarter-circle\fP
2875   line                  \fR# open object \[em] line\fP
2876   arrow                 \fR# open object \[em] line with arrowhead\fP
2877   spline                \fR# open object \[em] spline curve\fP
2878   move
2879   TEXT TEXT ...         \fR# text within invisible box\fP
2880   plot <expr> TEXT      \fR# formatted text\fP
2881   '[' <command> ... ']'
2882 .R
2883 .DE
2884 .PP
2885 Drawn objects within `[\~.\|.\|.\~]' are treated as a single composite
2886 object with a rectangular shape (that of the bounding box of all the
2887 elements).
2888 Variable and label assignments within a block are local to the block.
2889 Current direction of motion is restored to the value at start of block
2890 upon exit.
2891 Position is \fInot\fR restored (unlike `{\~}');
2892 instead, the current position becomes the exit position for the current
2893 direction on the block's bounding box.
2894 .DS
2895 .CW
2896 <attribute> ::=
2897   h[eigh]t <expr>       \fR# set height of closed figure\fP
2898   wid[th] <expr>        \fR# set width of closed figure\fP
2899   rad[ius] <expr>       \fR# set radius of circle/arc\fP
2900   diam[eter] <expr>     \fR# set diameter of circle/arc\fP
2901   up [<expr>]           \fR# move up\fP
2902   down [<expr>]         \fR# move down\fP
2903   left [<expr>]         \fR# move left\fP
2904   right [<expr>]        \fR# move right\fP
2905   from <position>       \fR# set from position of open figure\fP
2906   to <position>         \fR# set to position of open figure\fP
2907   at <position>         \fR# set center of open figure\fP
2908   with <path>           \fR# fix corner/named point at specified location\fP
2909   with <position>       \fR# fix position of object at specified location\fP
2910   by <expr-pair>        \fR# set object's attachment point\fP
2911   then                  \fR# sequential segment composition\fP
2912   dotted [<expr>]       \fR# set dotted line style\fP
2913   dashed [<expr>]       \fR# set dashed line style\fP
2914   thick[ness] <expr>    \fR# set thickness of lines\fP
2915   chop [<expr>]         \fR# chop end(s) of segment\fP
2916   '->' | '<-' | '<->'   \fR# decorate with arrows\fP
2917   invis[ible]           \fR# make primitive invisible\fP
2918   solid                 \fR# set solid line style\fP
2919   fill[ed] [<expr>]     \fR# fill closed figure with optional density\fP
2920   xscaled <expr>        \fR# slant box into x direction\fP
2921   yscaled <expr>        \fR# slant box into y direction\fP
2922   colo[u]r[ed] TEXT     \fR# set fill and outline color for figure\fP
2923   outline[d] TEXT       \fR# set outline color for figure\fP
2924   shaded TEXT           \fR# set fill color for figure\fP
2925   same                  \fR# copy size of previous object\fP
2926   cw | ccw              \fR# set orientation of curves\fP
2927   ljust | rjust         \fR# adjust text horizontally\fP
2928   above | below         \fR# adjust text vertically\fP
2929   aligned               \fR# align parallel to object\fP
2930   TEXT TEXT ...         \fR# text within object\fP
2931   <expr>                \fR# motion in the current direction\fR
2932 .R
2933 .DE
2934 .PP
2935 Missing attributes are supplied from defaults; inappropriate ones are
2936 silently ignored.
2937 For lines, splines, and arcs, height and width refer to arrowhead size.
2938 .PP
2939 The `at' primitive sets the center of the current object.
2940 The `with' attribute fixes the specified feature of the given object to
2941 a specified location.
2942 (Note that `with' is incorrectly described in the Kernighan paper.)
2943 .PP
2944 The `by' primitive is not documented in the tutorial portion of
2945 the Kernighan paper, and should probably be considered unreliable.
2946 .PP
2947 The primitive `arrow' is a synonym for `line\~->'.
2948 .PP
2949 Text is normally an attribute of some object, in which case successive
2950 strings are vertically stacked and centered on the object's center by
2951 default.
2952 Standalone text is treated as though placed in an invisible box.
2953 .PP
2954 A text item consists of a string or sprintf-expression, optionally
2955 followed by positioning information.
2956 Text (or strings specified with `sprintf') may contain font changes,
2957 size changes, and local motions, provided those changes are undone
2958 before the end of the current item.
2959 Text may also contain \e-escapes denoting special characters.
2960 The base font and specific set of escapes supported is implementation
2961 dependent, but supported escapes always include the following:
2962 .IP "\efR, \ef1" \w'\efR,\~\ef3'u+2n
2963 Set Roman style (the default)
2964 .IP "\efI, \ef2"
2965 Set Italic style
2966 .IP "\efB, \ef3"
2967 Set Bold style
2968 .IP \efP
2969 Revert to previous style; only works one level deep, does not stack.
2970 .PP
2971 Color names are dependent on the \gBpic\fR implementation, but in
2972 all modern versions color names recognized by the X\~window system are
2973 supported.
2974 .PP
2975 A position is an (x,y) coordinate pair.
2976 There are lots of different ways to specify positions:
2977 .DS
2978 .CW
2979 <position> ::=
2980   <position-not-place>
2981   <place>
2982   ( <position> )
2983 .R
2984 .DE
2985 .DS
2986 .CW
2987 <position-not-place> ::=
2988   <expr-pair>
2989   <position> + <expr-pair>
2990   <position> - <expr-pair>
2991   ( <position> , <position> )
2992   <expr> [of the way] between <position> and <position>
2993   <expr> '<' <position> , <position> '>'
2994 .R
2995 .DE
2996 .DS
2997 .CW
2998 <expr-pair> ::=
2999   <expr> , <expr>
3000   ( expr-pair )
3001 .R
3002 .DE
3003 .DS
3004 .CW
3005 <place> ::=
3006   <label>
3007   <label> <corner>
3008   <corner> [of] <label>
3009   Here
3010 .R
3011 .DE
3012 .DS
3013 .CW
3014 <label> ::=
3015   LABEL [. LABEL ...]
3016   <nth-primitive>
3017 .R
3018 .DE
3019 .DS
3020 .CW
3021 <corner> ::=
3022   .n | .e | .w | .s
3023   .ne | .se | .nw | .sw
3024   .c[enter] | .start | .end
3025   .t[op] | .b[ot[tom]] | .l[eft] | .r[ight]
3026   left | right | <top-of> | <bottom-of>
3027   <north-of> | <south-of> | <east-of> | <west-of>
3028   <center-of> | <start-of> | <end-of>
3029   upper left | lower left | upper right | lower right
3030 .R
3031 .DE
3032 .DS
3033 .CW
3034 <\,\f(CIxxx\/\fP-of> ::=
3035   \f(CIxxx\fP                   \fR# followed by `of'\fP
3036 .R
3037 .DE
3038 .DS
3039 .CW
3040 <nth-primitive> ::=
3041   <ordinal> <object-type>
3042   [<ordinal>] last <object-type>
3043 .R
3044 .DE
3045 .DS
3046 .CW
3047 <ordinal> ::=
3048   INT th
3049   INT st | INT nd | INT rd
3050   ` <any-expr> 'th
3051 .R
3052 .DE
3053 .DS
3054 .CW
3055 <object-type> ::=
3056   box
3057   circle
3058   ellipse
3059   arc
3060   line
3061   arrow
3062   spline
3063   '[]'
3064   TEXT
3065 .R
3066 .DE
3067 .PP
3068 As Kernighan notes, \[lq]since barbarisms like \fB1th\fP and \fB3th\fP are
3069 barbaric, synonyms like \fB1st\fP and \fB3rd\fP are accepted as well.\[rq]
3070 Objects of a given type are numbered from 1 upwards in order of
3071 declaration; the \fBlast\fP modifier counts backwards.
3072 .PP
3073 The \[lq]'th\[rq] form (which allows you to select a previous object with
3074 an expression, as opposed to a numeric literal) is not documented in DWB's
3075 \fIpic\/\fR(1).
3076 .PP
3077 The \[la]\,\fIxxx\/\fP-of\|\[ra] rule is special: The lexical parser checks whether
3078 \fIxxx\fP is followed by the token `of' without eliminating it so that
3079 the grammar parser can still see `of'.
3080 Valid examples of specifying a place with corner and label are thus
3081 .DS
3082 .CW
3083 A .n
3084 \&.n of A
3085 \&.n A
3086 north of A
3087 .R
3088 .DE
3089 .LP
3090 while
3091 .DS
3092 .CW
3093 north A
3094 A north
3095 .R
3096 .DE
3097 both cause a syntax error.
3098 (DWB \fBpic\fP also allows the weird form `A\~north\~of'.)
3099 .PP
3100 Here the special rules for the `with' keyword using a path:
3101 .DS
3102 .CW
3103 <path> ::=
3104   <relative-path>
3105   ( <relative-path> , <relative-path> )
3106 .R
3107 .DE
3108 .DS
3109 .CW
3110 <relative-path> ::=
3111   <corner>
3112   . LABEL [. LABEL ...] [<corner>]
3113 .R
3114 .DE
3115 .PP
3116 The following style variables control output:
3117 .TS H
3118 center tab(@), linesize(2);
3119 lb | lb | lb
3120 l | n | l.
3121 .sp 2p
3122 Style Variable@Default@What It Does
3123 .sp 2p
3124 _
3125 .sp 2p
3126 .TH
3127 boxht@0.5@Default height of a box
3128 boxwid@0.75@Default width of a box
3129 lineht@0.5@Default length of vertical line
3130 linewid@0.75@Default length of horizontal line
3131 arcrad @0.25@Default radius of an arc
3132 circlerad@0.25@Default radius of a circle
3133 ellipseht@0.5@Default height of an ellipse
3134 ellipsewid@0.75@Default width of an ellipse
3135 moveht@0.5@Default length of vertical move
3136 movewid@0.75@Default length of horizontal move
3137 textht@0@Default height of box enclosing a text object
3138 textwid@0@Default width of box enclosing a text object
3139 arrowht@0.1@Length of arrowhead along shaft
3140 arrowwid@0.05@Width of rear of arrowhead
3141 arrowhead@1@Enable/disable arrowhead filling
3142 dashwid@0.05@Interval for dashed lines
3143 maxpswid@8.5@Maximum width of picture
3144 maxpsht@11@Maximum height of picture
3145 scale@1@Unit scale factor
3146 fillval@0.5@Default fill value
3147 .sp 5p
3148 _
3149 .TE
3150 Any of these can be set by assignment, or reset using the \fBreset\fP
3151 statement.
3152 Style variables assigned within `[\~]' blocks are restored to their
3153 beginning-of-block value on exit; top-level assignments persist across
3154 pictures.
3155 Dimensions are divided by \fBscale\fR on output.
3156 .PP
3157 All \fBpic\fP expressions are evaluated in floating point; units
3158 are always inches (a trailing `i' or `I' is ignored).
3159 Expressions have the following simple grammar, with semantics very
3160 similar to C\~expressions:
3161 .DS
3162 .CW
3163 <expr> ::=
3164   VARIABLE
3165   NUMBER
3166   <place> <place-attribute>
3167   <expr> <op> <expr>
3168   - <expr>
3169   ( <any-expr> )
3170   ! <expr>
3171   <func1> ( <any-expr> )
3172   <func2> ( <any-expr> , <any-expr> )
3173   rand ( )
3174 .R
3175 .DE
3176 .DS
3177 .CW
3178 <place-attribute>
3179  .x | .y | .h[eigh]t | .wid[th] | .rad
3180 .R
3181 .DE
3182 .DS
3183 .CW
3184 <op> ::=
3185   + | - | * | / | % | ^ | '<' | '>' | '<=' | '>='
3186 .R
3187 .DE
3188 .DS
3189 .CW
3190 <func1> ::=
3191   sin | cos | log | exp | sqrt | int | rand | srand
3192 .R
3193 .DE
3194 .DS
3195 .CW
3196 <func2> ::=
3197   atan2 | max | min
3198 .R
3199 .DE
3200 .LP
3201 Both \fBexp\fP and \fBlog\fP are base 10; \fBint\fP does integer
3202 truncation; and \fBrand()\fP returns a random number in [0-1).
3203 .PP
3204 There are \fBdefine\fP and \fBundef\fR statements which are not part
3205 of the grammar (they behave as pre-processor macros to the language).
3206 These may be used to define pseudo-functions.
3207 .DS
3208 .CW
3209 \fBdefine\fP \fIname\fP \fB{\fP \fIreplacement-text\fP \fB}\fP
3210 .R
3211 .DE
3212 .LP
3213 This defines \fIname\fR as a macro to be replaced by the replacement
3214 text (not including the braces).
3215 The macro may be called as
3216 .DS
3217 .CW
3218 \fIname\/\fB(\,\fIarg1, arg2, .\|.\|., argn\fB\/)\fR
3219 .R
3220 .DE
3221 .LP
3222 The arguments (if any) are substituted for tokens $1, $2 \&.\|.\|.\& $n
3223 appearing in the replacement text.
3224 To undefine a macro, say \fBundef\fP \fIname\fR, specifying the name to
3225 be undefined.
3226 .\"%%POSTLUDE%%
3227 .
3228 .
3229 .NH 1
3230 History and Acknowledgements
3231 .PP
3232 Original \fBpic\fP was written to go with Joseph Ossanna's original
3233 \fItroff\/\fP(1) by Brian Kernighan, and later re-written by Kernighan
3234 with substantial enhancements (apparently as part of the evolution of
3235 \fItroff\/\fP(1) into \fIditroff\/\fP(1) to generate
3236 device-independent output).
3237 .PP
3238 The language had been inspired by some earlier graphics languages
3239 including \fBideal\fP and \fBgrap\fP.
3240 Kernighan credits Chris van Wyk (the designer of \fBideal\fP) with many
3241 of the ideas that went into
3242 \fBpic\fP.
3243 .PP
3244 .\" the original definitions of EQ and EN cause insertion of vertical
3245 .\" space which is not appropriate here
3246 .de EQ
3247 ..
3248 .de EN
3249 ..
3250 .EQ
3251 delim $$
3252 .EN
3253 The \fBpic\fP language was originally described by Brian Kernighan in
3254 Bell Labs Computing Science Technical Report #116 (you can obtain a
3255 PostScript copy of the revised version, [1], by sending a mail message to
3256 \fInetlib@research.att.com\fP with a body of `send 116 from
3257 research/cstr').
3258 There have been two revisions, in 1984 and 1991.
3259 .PP
3260 The document you are reading effectively subsumes Kernighan's
3261 description; it was written to fill in lacun\[ae] in the exposition and
3262 integrate in descriptions of the GNU \fIgpic\/\fP(1) and
3263 \fIpic2plot\/\fP(1) features.
3264 .PP
3265 The GNU \fBgpic\fR implementation was written by James Clark
3266 \[la]\,\fIjjc@jclark.com\/\fP\[ra].
3267 .PP
3268 The GNU \fBpic2plot\fR implementation is based on James Clark's parser
3269 code and maintained by Robert Maier, principal author of \fBplotutils\fP.
3270 .
3271 .
3272 .NH 1
3273 Bibliography
3274 .IP 1.
3275 Kernighan, B. W.
3276 \fBPIC \[em] A Graphics Language for Typesetting
3277 (Revised User Manual)\fP.
3278 Bell Labs Computing Science Technical Report #116, December 1991.
3279 .IP 2.
3280 Van Wyk, C. J.
3281 \fBA high-level language for specifying pictures\fP.
3282 \fIACM Transactions On Graphics\fP 1,2 (1982) 163-182.
3283 .
3284 .\" Local Variables:
3285 .\" mode: nroff
3286 .\" End:
3287 .\" vim: filetype=groff: