1. stroke

Paints a line following the current path using current line parameters.

1.1. Syntax

- stroke → -

1.2. Stack Effects

Table 1. Before
Level Object

(empty)

No operands required

Table 2. After
Level Object

(empty)

No results

1.3. Description

stroke paints a line following the current path using the current color. This line is centered on the path, has sides parallel to the path segments, and has a width (thickness) given by the current line width parameter in the graphics state.

stroke paints the joints between connected path segments with the current line join and the ends of open subpaths with the current line cap. The line is either solid or broken according to the dash pattern established by setdash.

The parameters in the graphics state controlling line rendition (line width, line join, line cap, dash pattern, etc.) are consulted at the time stroke is executed. Their values during path construction are irrelevant.

stroke implicitly performs a newpath after it has finished painting the current path.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Simple line stroke
newpath
100 100 moveto
200 200 lineto
stroke
Rectangle with thick stroke
5 setlinewidth        % 5-point line width
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
stroke
Dashed line
[5 3] 0 setdash      % 5 on, 3 off
newpath
50 150 moveto
250 150 lineto
stroke

1.6. Common Use Cases

1.6.1. Different Line Caps

1 setlinewidth

% Butt cap (0)
0 setlinecap
newpath
50 200 moveto
150 200 lineto
stroke

% Round cap (1)
1 setlinecap
newpath
50 150 moveto
150 150 lineto
stroke

% Projecting square cap (2)
2 setlinecap
newpath
50 100 moveto
150 100 lineto
stroke

1.6.2. Different Line Joins

10 setlinewidth

% Miter join (0)
0 setlinejoin
newpath
50 200 moveto
100 250 lineto
150 200 lineto
stroke

% Round join (1)
1 setlinejoin
newpath
50 150 moveto
100 200 lineto
150 150 lineto
stroke

% Bevel join (2)
2 setlinejoin
newpath
50 100 moveto
100 150 lineto
150 100 lineto
stroke

1.6.3. Complex Path Stroking

/drawStar {
  % x y radius drawStar
  /r exch def
  /y exch def
  /x exch def

  newpath
  x y r add moveto
  0 1 4 {
    144 mul rotate
    x y r add lineto
  } for
  closepath

  2 setlinewidth
  stroke
} def

150 150 75 drawStar

1.7. Common Pitfalls

Path Consumed After Stroke - stroke clears the current path. Use gsave/grestore to preserve it.
newpath
100 100 moveto
200 200 lineto
stroke
% Current path is now empty!

% To preserve path:
newpath
100 100 moveto
200 200 lineto
gsave
  stroke
grestore
% Path still exists
Degenerate Subpaths - Single points or coincident points produce different results based on line cap.
% Round line cap - produces filled circle
1 setlinecap
10 setlinewidth
newpath
150 150 moveto
closepath
stroke  % Draws a dot

% Butt cap - no output
0 setlinecap
newpath
150 150 moveto
closepath
stroke  % Nothing drawn
Line Width in User Space - Line width is affected by the CTM, which may cause non-uniform line widths.
1 setlinewidth
2 1 scale  % Scale x by 2, y by 1

newpath
100 100 moveto
200 100 lineto
stroke  % Line appears 2 points wide horizontally, 1 point vertically
Use Stroke Adjustment - Enable automatic stroke adjustment for uniform line appearance:
true setstrokeadjust  % Level 2
1 setlinewidth
newpath
100 100 moveto
200 200 lineto
stroke

1.8. Error Conditions

Error Condition

[limitcheck]

Path becomes too complex for implementation

1.9. Implementation Notes

  • Line width of 0 is interpreted as thinnest line possible (typically 1 device pixel)

  • Very thin lines may not be visible on high-resolution devices

  • Degenerate subpaths (single points, closed paths) are handled specially based on line cap

  • Curved segments are automatically flattened according to the flatness parameter

  • Automatic stroke adjustment can ensure uniform line widths

1.10. Graphics State Parameters

stroke is affected by:

  • Line width - Set by setlinewidth

  • Line cap - Set by setlinecap (0=butt, 1=round, 2=square)

  • Line join - Set by setlinejoin (0=miter, 1=round, 2=bevel)

  • Miter limit - Set by setmiterlimit

  • Dash pattern - Set by setdash

  • Stroke adjustment - Set by setstrokeadjust (Level 2)

  • Current color - Set by color operators

  • Current transformation matrix (CTM) - Affects line width and dash pattern

  • Clipping path - Clips the stroked output

  • Flatness - Affects curve rendering

1.11. Line Cap Styles

Butt cap (0)
  • Stroke squared off at endpoint

  • No projection beyond end of path

  • Default setting

Round cap (1)
  • Semicircular arc at endpoint

  • Diameter equals line width

  • Extends beyond endpoint by half line width

Projecting square cap (2)
  • Square projection at endpoint

  • Extends beyond endpoint by half line width

  • Projects perpendicular to path direction

1.12. Line Join Styles

Miter join (0)
  • Outer edges extended until they meet

  • Creates sharp corners

  • Subject to miter limit

  • Default setting

Round join (1)
  • Circular arc at corner

  • Diameter equals line width

  • Smooth corners

Bevel join (2)
  • Corner cut off at angle

  • Butt caps on segments, triangle fills notch

  • No sharp points

1.13. Best Practices

1.13.1. Set Line Parameters Before Stroking

% Set all line parameters first
2 setlinewidth
1 setlinecap
1 setlinejoin
10 setmiterlimit
[5 3] 0 setdash

% Then construct and stroke path
newpath
100 100 moveto
200 200 lineto
stroke

1.13.2. Preserve Path for Multiple Operations

newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath

% Fill then stroke
gsave
  0.8 setgray
  fill
grestore

0 setgray
2 setlinewidth
stroke

1.13.3. Use Appropriate Line Joins for Sharp Angles

% For sharp angles, use miter join with appropriate limit
0 setlinejoin        % Miter join
10 setmiterlimit     % Reasonable limit

% For any angle, round join works well
1 setlinejoin        % Round join

% For performance, bevel join is fastest
2 setlinejoin        % Bevel join

1.13.4. Reset Dash Pattern After Use

% Use dashed line
[5 3] 0 setdash
newpath
100 100 moveto
200 200 lineto
stroke

% Reset to solid
[] 0 setdash

1.14. Performance Considerations

  • Simple straight lines stroke faster than curves

  • Dashed lines are slower than solid lines

  • Round joins are slower than miter or bevel joins

  • Very wide lines are slower to stroke

  • Complex paths with many segments take longer

  • Stroke adjustment may impact performance slightly

1.15. Stroking vs Filling

Stroking characteristics
  • Paints along the path

  • Width determined by line width

  • Affected by line parameters

  • Can create open shapes

  • Path is consumed after operation

Filling characteristics
  • Paints inside the path

  • No concept of line width

  • Not affected by line parameters

  • Always creates closed shapes

  • Path is consumed after operation

1.16. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.