• No results found

Lewis Carroll

10.2 LINE-DRAWING DETAILS

The POSTSCRIPT language gives complete control over how the

stroke operator converts a path into a painted line or curve. The setlinewidth operator determines the width of the stroked line.

There are several operators that allow us to precisely determine other characteristics of a stroked path. Among these are:

setlinecap Determines the appearance of line segment ends.

setlinejoin Determines the method by which different line segments are joined.

setdash Determines the pattern for dashed lines. We shall examine each of these operators in turn.

setlinecap

The setlinecap operator takes a number from the stack and uses

it as a code determining how POSTSCRIPT will end stroked line

segments. For example, the program line

1 setlinecap

would cause POSTSCRIPT to paint all line segments with round

ends.

There are three values for the line cap code:

0 Butt caps. The line segment has square ends perpen-

Linecap = 0; Butt caps

Linecap = 1; Round caps

Linecap = 2; Projecting caps

dicular to the path. This is the POSTSCRIPT default line

cap.

1 Round caps. The line segment ends with semicircular

caps with diameters equal to the width of the line.

2 Projecting square caps. These are similar to butt caps,

but extend one-half of a line width beyond the line segment’s endpoint.

setlinejoin

When two connected line segments are stroked, POSTSCRIPT

needs to make a decision about what type of joint to use between

them. The setlinejoin operator tells POSTSCRIPT how to join

connecting line segments. This operator is similar to setlinecap, in that it takes a code from the top of the stack. This code can have values from zero to two, corresponding to the following types of line joins:

Linejoin = 0; Miter joins

Linejoin = 1; Round joins

Linejoin = 2; Bevel joins

0 Mitered join. The edges of the stroke are extended until

they meet. This is the default join. This join is affected by the current miter limit (see below).

1 Rounded join. The segments are connected by a circular

join with a diameter equal to the line width.

2 Bevel join. The segments are finished with butt end caps

and the notch at the larger angle between the segments is filled with a triangle.

Miter Limit

Mitered joins can present a problem. If two line segments meet at an extremely small angle, the mitered join can produce a spike that extends a considerable distance beyond the intersection of the path segments. To prevent this, the join switches from mitered to beveled when the angle between line segments be- comes too acute.

a

b

That is, if the current line join is 0, line segments will normally be connected with a mitered joint (see a, at left). However, if the angle between the two segments is too small, the connection is beveled (as in b).

The angle at which this changeover is made is determined by the

l w

The miter limit is the maximum ratio of l/w.

current miter limit. The miter limit is the maximum ratio of the diagonal line through a join to the width of the lines producing the join (see at left). This ratio can be set by the setmiterlimit operator, which takes a number from the stack and makes it the new miter limit. The smaller this number is, the less tolerant POSTSCRIPT becomes of small mitered angles and the sooner it

will switch to beveled joins. The default POSTSCRIPTmiter limit

is ten, specifying a miter limit angle of about eleven degrees.

10 setmiterlimit

3 setmiterlimit

The illustration at left shows two line segments intersecting at an angle of thirty degrees. In the upper figure, the miter limit is the default 10; in the lower, the limit has been changed to 3. The angle is the same, but the lower miter limit causes the second pair to be beveled, rather than mitered.

setdash

The current path is normally stroked with a solid line. Other methods of stroking a path are possible, however. The POSTSCRIPT graphics state includes a dash array and a dash offset that together describe what pattern of alternating black and white dashes should be used to stroke paths.

This pattern is set by the setdash operator, which takes an array and a number from the stack and makes them the current dash array and offset. The array contains a set of numbers, such as

[3 5 1 5]

which represent the lengths of alternating black and white seg- ments should make up a stroked line. The array above would cause all paths to be stroked with a repeating sequence consisting of three units of black, five units of no ink, one unit black, five

[3 5 1 5] 0 setdash units no ink. This pattern will repeat along the entire stroked path

(see illustration at left).

The second argument passed to setdash is the offset within the dash pattern where the stroke operator is to start when it prints a line. That is, if we were to set the dash pattern with the line

[6 3] 3 setdash

stroked lines would begin three units into the pattern, or halfway through the first long dash.

The following program illustrates the effects of the setdash argu- ments on the appearance of stroked lines. It draws two thick ver- tical lines and then draws a series of horizontal lines between them, each with a different dash pattern or offset. The horizontal lines are numbered with their vertical positions above the origin.

% --- Variables & Procedures ---

130 115 100 85 70 55 40 25 10 /ypos 130 def

/Times-Roman findfont 6 scalefont setfont /prt-n { ( ) cvs show } def /borders {−2.5 0 moveto 0 135 rlineto 102.5 0 moveto 0 135 rlineto stroke } def /newline

{ /ypos ypos 15 sub def } def /doLine

{ 0 ypos moveto 100 0 rlineto stroke 5 ypos 2 add moveto ypos prt-n newline } def % --- Begin Program --- 250 350 translate 5 setlinewidth borders 10.2 LINE-DRAWING DETAILS 107

.5 setlinewidth

[ ] 0 setdash doLine %empty array for solid line [4 2] 0 setdash doLine [2 4] 0 setdash doLine [6 4 2 4] 0 setdash doLine [4 4] 0 setdash doLine [4 4] 1 setdash doLine [4 4] 2 setdash doLine [4 4] 3 setdash doLine [4 4] 4 setdash doLine showpage

Much of this program is familiar to us already. The newline pro- cedure decrements the variable ypos, which holds the current vertical position. Prt-n converts a number to a string and prints it on the current page. Borders draws two vertical lines one hundred units apart.

The doLine procedure draws a line, prints the value of ypos above the line, and then decrements ypos.

/doLine

{ 0 ypos moveto 100 0 rlineto stroke 5 ypos 2 add moveto ypos prt-n newline } def

The program moves the origin to the middle of the page and prints the vertical borders in 5-unit-wide lines.

5 setlinewidth borders

The line width is reset to .5 and nine horizontal lines are drawn, each with a different dash pattern or offset.

The first dash pattern,

[ ] 0 setdash doLine

has an empty dash array, signifying a solid line. The offset is unimportant in this case. The next three lines,

[4 2] 0 setdash doLine [2 4] 0 setdash doLine [6 4 2 4] 0 setdash doLine

draw lines of various dash patterns. The last five lines have the same pattern, but different offsets.

[4 4] 0 setdash doLine [4 4] 1 setdash doLine [4 4] 2 setdash doLine [4 4] 3 setdash doLine [4 4] 4 setdash doLine

For more information on the setdash operator, refer to the

POSTSCRIPT Language Reference Manual and the POSTSCRIPT

Language Cookbook.

10.3 OPERATOR SUMMARY

Graphics State Operators clip —⇒—

Set clipping boundary to current path

setdash ary n⇒— Set dash array

setlinecap 0/1/2⇒—

Set shape of stroked line ends

setlinejoin 0/1/2⇒—

Set shape of stroked line joins

setmiterlimit num⇒—

Set maximum miter ratio

CHAPTER

11

IMAGES