1. setdash
Sets the dash pattern for stroking operations.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
(empty) |
Dash pattern parameter set |
1.3. Description
setdash sets the dash pattern parameter in the graphics state. This controls the pattern of dashes and gaps used by subsequent [stroke] operations.
The array specifies the pattern:
-
Empty
[]: Solid line (no dashing) -
One element
[a]: Alternating a-unit dashes and a-unit gaps -
Two elements
[a b]: a-unit dashes, b-unit gaps (repeating) -
Multiple elements: Sequence repeats: dash, gap, dash, gap…
The offset specifies how far into the pattern to start (in user space units).
The dash pattern is interpreted in user space and is unaffected by transformations applied after setdash is called.
1.5. Examples
[] 0 setdash
0 0 moveto 100 0 lineto stroke % Solid line
[3] 0 setdash
0 0 moveto 100 0 lineto stroke % 3 on, 3 off, repeating
[5 3] 0 setdash
0 0 moveto 100 0 lineto stroke % 5 on, 3 off, repeating
[10 3 2 3] 0 setdash
0 0 moveto 100 0 lineto stroke % 10 on, 3 off, 2 on, 3 off
[6 3] 0 setdash
0 10 moveto 100 10 lineto stroke
[6 3] 3 setdash % Start 3 units into pattern
0 20 moveto 100 20 lineto stroke
1.6. Common Use Cases
1.6.1. Standard Dash Patterns
% Dashed line
[6 3] 0 setdash
% Dotted line
[2 2] 0 setdash
% Dash-dot
[10 3 2 3] 0 setdash
% Dash-dot-dot
[10 3 2 3 2 3] 0 setdash
% Long dash, short dash
[12 3 3 3] 0 setdash
1.6.2. Technical Drawing Styles
/HiddenLine {
[3 2] 0 setdash
0.5 setlinewidth
} def
/CenterLine {
[20 5 5 5] 0 setdash
0.5 setlinewidth
} def
/PhantomLine {
[20 5 5 5 5 5] 0 setdash
0.5 setlinewidth
} def
1.7. Common Pitfalls
| Line Caps Affect Dashes - Each dash is treated with the current line cap style. |
1 setlinecap % Round caps
[5 3] 0 setdash
% Dashes will have round ends
| Pattern in User Space - Scaling affects dash appearance. |
[3 3] 0 setdash
2 2 scale
% Dashes now appear 6 units (in device space)
Empty Array Means Solid - Use [] 0 to turn off dashing.
|
[3 3] 0 setdash % Dashed
[] 0 setdash % Back to solid
| Offset Wraps Around - Offset values wrap through the pattern. |
[6 3] 0 setdash % Start at beginning
[6 3] 9 setdash % Same as offset 0 (9 = 6+3)
[6 3] 15 setdash % Same again (15 = pattern length × 1 + 6)
| Use Round Caps for Dots - Combine round caps with short dashes for perfect dots. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Too many elements in array |
[ |
Array contains negative values |
[ |
Fewer than 2 operands on stack |
[ |
First operand not an array, or second not a number |
1.9. Implementation Notes
-
Pattern repeats cyclically
-
Dash lengths in user space units
-
Applied at stroke time (CTM matters then, not at setdash)
-
Zero-length dash/gap elements allowed
-
Odd number of array elements: pattern doubles
-
Very fast parameter setting
1.10. Dash Pattern Interpretation
The dash pattern is applied along the path:
Pattern: [10 5]
Offset: 0
Path: ────────── ────────── ──────────
└─10 units┘ 5 └─10 units┘ 5 └─10 units┘
Pattern: [10 5]
Offset: 5
Path: ────────── ────────── ──────
└5┘ └─10 units┘ 5 └─10 units┘ 5 └─10...
1.11. Odd Number of Elements
If array has odd number of elements, pattern is used twice:
[3 5 2] 0 setdash
% Equivalent to:
[3 5 2 3 5 2] 0 setdash
% Pattern: 3 on, 5 off, 2 on, 3 off, 5 on, 2 off
1.12. Corner Handling
% Dashes don't coordinate with corners
[10 5] 0 setdash
0 0 moveto
100 0 lineto
100 100 lineto % Corner may fall in dash or gap
stroke
1.13. See Also
-
currentdash- Get current dash pattern -
setlinecap- Set line end style -
setlinewidth- Set line width -
setlinejoin- Set corner style