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