1. fill
Paints the area enclosed by the current path with the current color.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
No operands required |
| 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.5. Examples
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
fill
0.5 setgray % Set gray level to 50%
newpath
150 150 moveto
250 150 lineto
250 250 lineto
150 250 lineto
closepath
fill
% 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
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.9. Implementation Notes
-
filluses 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