- 1. stroke
- 1.1. Syntax
- 1.2. Stack Effects
- 1.3. Description
- 1.4. PostScript Level
- 1.5. Examples
- 1.6. Common Use Cases
- 1.7. Common Pitfalls
- 1.8. Error Conditions
- 1.9. Implementation Notes
- 1.10. Graphics State Parameters
- 1.11. Line Cap Styles
- 1.12. Line Join Styles
- 1.13. Best Practices
- 1.14. Performance Considerations
- 1.15. Stroking vs Filling
- 1.16. See Also
1. stroke
Paints a line following the current path using current line parameters.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
No operands required |
| 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.
1.5. Examples
newpath
100 100 moveto
200 200 lineto
stroke
5 setlinewidth % 5-point line width
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
stroke
[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.7. Common Pitfalls
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.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
-
Stroke squared off at endpoint
-
No projection beyond end of path
-
Default setting
-
Semicircular arc at endpoint
-
Diameter equals line width
-
Extends beyond endpoint by half line width
-
Square projection at endpoint
-
Extends beyond endpoint by half line width
-
Projects perpendicular to path direction
1.12. Line Join Styles
-
Outer edges extended until they meet
-
Creates sharp corners
-
Subject to miter limit
-
Default setting
-
Circular arc at corner
-
Diameter equals line width
-
Smooth corners
-
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.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
-
Paints along the path
-
Width determined by line width
-
Affected by line parameters
-
Can create open shapes
-
Path is consumed after operation
-
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
-
fill- Fill path interior -
strokepath- Convert stroke to outline path -
ustroke- Stroke user path (Level 2) -
setlinewidth- Set line width -
setlinecap- Set line cap style -
setlinejoin- Set line join style -
setmiterlimit- Set miter limit -
setdash- Set dash pattern -
setstrokeadjust- Enable stroke adjustment (Level 2) -
newpath- Clear current path -
gsave- Save graphics state -
grestore- Restore graphics state