1. setdash

Sets the dash pattern for stroking operations.

1.1. Syntax

array offset setdash → –

1.2. Stack Effects

Table 1. Before
Level Object

1

array (dash pattern array)

0

offset (dash offset value)

Table 2. After
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.4. PostScript Level

Level 1 and later

1.5. Examples

Solid line (no dash)
[] 0 setdash
0 0 moveto 100 0 lineto stroke  % Solid line
Simple dash pattern
[3] 0 setdash
0 0 moveto 100 0 lineto stroke  % 3 on, 3 off, repeating
Dash with gap
[5 3] 0 setdash
0 0 moveto 100 0 lineto stroke  % 5 on, 3 off, repeating
Dash-dot pattern
[10 3 2 3] 0 setdash
0 0 moveto 100 0 lineto stroke  % 10 on, 3 off, 2 on, 3 off
Using offset
[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.6.3. Border Patterns

/DashedBorder {
  gsave
    [5 5] 0 setdash
    2 setlinewidth
    0 0 moveto
    pageWidth 0 lineto
    pageWidth pageHeight lineto
    0 pageHeight lineto
    closepath stroke
  grestore
} def

1.6.4. Animated Dash Patterns

/AnimatedDash {
  % offset parameter
  [10 5] exch setdash
} def

% Create animation by varying offset
0 5 30 {
  AnimatedDash
  drawPath stroke
  0 10 translate
} for

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

[limitcheck]

Too many elements in array

[rangecheck]

Array contains negative values

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

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


Back to top

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