1. fill

Paints the area enclosed by the current path with the current color.

1.1. Syntax

- fill → -

1.2. Stack Effects

Table 1. Before
Level Object

(empty)

No operands required

Table 2. After
Level Object

(empty)

No results

1.3. Description

fill paints the area enclosed by the current path with the current color. Any previous contents of that area on the current page are obscured, so areas may be erased by filling with color set to white.

Before painting, fill implicitly closes any open subpaths of the current path. The inside of the current path is determined by the normal non-zero winding number rule.

fill implicitly performs a newpath after it has finished filling the current path. To preserve the current path across a fill operation, use the sequence:

gsave fill grestore

1.4. PostScript Level

Level 1 and later

1.5. Examples

Filling a simple rectangle
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
fill
Filling with a specific color
0.5 setgray          % Set gray level to 50%
newpath
150 150 moveto
250 150 lineto
250 250 lineto
150 250 lineto
closepath
fill
Filling multiple overlapping paths
% First shape
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
fill

% Second overlapping shape
newpath
150 150 moveto
250 150 lineto
250 250 lineto
150 250 lineto
closepath
fill

1.6. Common Use Cases

1.6.1. Filling Complex Shapes

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

  newpath
  x y r add moveto
  0 1 4 {
    72 mul rotate
    x y r add lineto
    36 rotate
    x y r 0.382 mul add lineto
    36 neg rotate
  } for
  closepath
  fill
} def

200 200 50 drawStar

1.6.2. Erasing Areas

% Draw something
0 setgray
newpath
100 100 moveto
200 200 lineto
stroke

% Erase part of it with white fill
1 setgray              % White
newpath
120 120 40 0 360 arc
closepath
fill

1.6.3. Using Non-Zero Winding Rule

% Outer rectangle (counterclockwise)
newpath
50 50 moveto
250 50 lineto
250 250 lineto
50 250 lineto
closepath

% Inner rectangle (counterclockwise - same direction)
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath

fill  % Both rectangles filled (non-zero winding rule)

1.7. Common Pitfalls

Path Consumed After Fill - fill clears the current path after execution. Use gsave and grestore to preserve it.
newpath
100 100 moveto
200 200 lineto
fill
% Current path is now empty!
Open Subpaths - Open subpaths are implicitly closed before filling. This may produce unexpected results if you intended the path to remain open.
newpath
100 100 moveto
200 100 lineto
200 200 lineto
% Path auto-closes to (100,100) before fill
fill
Preserve Path for Multiple Operations - To both fill and stroke the same path, use save/restore:
newpath
100 100 moveto
200 100 lineto
200 200 lineto
closepath

gsave
  fill
grestore
stroke

1.8. Error Conditions

Error Condition

[limitcheck]

Path becomes too complex for implementation

1.9. Implementation Notes

  • fill uses the non-zero winding number rule to determine which areas are inside the path

  • Overlapping subpaths may create holes or fills depending on their direction

  • The current color and color space determine the fill color

  • Clipping is applied after the fill area is determined

  • Very complex paths may exceed implementation limits

1.10. Interaction with Graphics State

fill is affected by:

  • Current color and color space

  • Current clipping path

  • Current transformation matrix (CTM)

  • Flatness parameter (for curved paths)

fill does not affect:

  • Line width or line parameters

  • Current point (path is cleared)

  • Graphics state stack

1.11. Best Practices

1.11.1. Separate Path Construction from Painting

/myShape {
  newpath
  100 100 moveto
  200 100 lineto
  200 200 lineto
  100 200 lineto
  closepath
} def

% Use the shape multiple times
0.3 setgray
myShape fill

0.8 setgray
myShape fill

1.11.2. Use Appropriate Winding Rule

% For simple fills, fill is fine
newpath
100 100 50 0 360 arc
closepath
fill

% For complex shapes with holes, consider eofill
% (see eofill documentation)

1.11.3. Always Start with newpath

% Good practice
newpath
100 100 moveto
200 200 lineto
fill

% Bad practice - may have leftover path segments
100 100 moveto
200 200 lineto
fill

1.12. Performance Considerations

  • Simple convex paths fill faster than complex concave paths

  • Paths with many segments take longer to fill

  • Very large fill areas may be slower on some devices

  • Avoid unnecessarily complex paths when simple ones will do

1.13. See Also


Back to top

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