- 1. clip
- 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 Interaction
- 1.11. Winding Number Rule
- 1.12. Best Practices
- 1.13. Performance Considerations
- 1.14. Clipping Path Management
- 1.15. See Also
1. clip
Intersects the current clipping path with the current path using the non-zero winding rule.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
No operands required |
| Level | Object |
|---|---|
(empty) |
No results |
1.3. Description
clip intersects the inside of the current clipping path with the inside of the current path to produce a new, smaller current clipping path. The inside of the current path is determined by the normal PostScript non-zero winding number rule, while the inside of the current clipping path is determined by whatever rule was used at the time that path was created.
In general, clip produces a new path whose inside (according to the non-zero winding number rule) consists of all areas that are inside both of the original paths. The way this new path is constructed (the order of its segments, whether it self-intersects, etc.) is not specified.
1.5. Examples
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
clip
newpath
% Graphics are now clipped to rectangle
50 50 moveto
250 250 lineto
stroke % Only portion inside clip is visible
newpath
150 150 75 0 360 arc
closepath
clip
newpath
% Draw something that gets clipped
0 0 moveto
300 300 lineto
stroke
% First clip
newpath
50 50 moveto
250 50 lineto
250 250 lineto
50 250 lineto
closepath
clip
newpath
% Second clip (intersection)
newpath
100 100 150 0 360 arc
closepath
clip
newpath
% Only intersection of both clips is visible
1.6. Common Use Cases
1.6.1. Creating Masked Regions
gsave
% Create mask
newpath
100 100 80 60 link:/commands/references/arc/[`arc`]
closepath
clip
newpath
% Draw content
% Only visible within mask
50 50 200 200 rectfill
grestore
1.7. Common Pitfalls
Clipping Can Only Shrink - There is no way to enlarge the current clipping path except by grestore or initclip.
|
% First clip
newpath
50 50 100 100 link:/commands/references/arc/[`arc`]
clip
% Can't expand beyond this
newpath
0 0 200 200 link:/commands/references/arc/[`arc`]
clip % Still limited to intersection
newpath
100 100 moveto
200 200 lineto
clip
% Path still exists!
% Must explicitly clear
newpath
| Open Paths Are Implicitly Closed - Open subpaths are treated as closed for clipping. |
newpath
100 100 moveto
200 100 lineto
200 200 lineto
% No closepath, but treated as closed
clip
newpath
| Always Use with gsave/grestore - Bracket clip operations to restore the original clipping path: |
gsave
newpath
100 100 50 0 360 arc
clip
newpath
% Clipped drawing operations
% ...
grestore
% Original clipping path restored
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Clipping path becomes too complex for implementation |
1.9. Implementation Notes
-
The clipping path is maintained separately from the current path
-
Clipping is applied during rendering of
fill,stroke,show, etc. -
Complex clipping paths may degrade rendering performance
-
The clipping path can become arbitrarily complex through repeated clips
-
Some implementations may have limits on clipping path complexity
1.10. Graphics State Interaction
The clipping path is part of the graphics state and is affected by:
The clipping path affects:
1.11. Winding Number Rule
clip uses the non-zero winding number rule:
-
Draw a ray from the point in any direction
-
Count crossings with path segments:
-
+1 for left-to-right crossings
-
-1 for right-to-left crossings
-
-
If the total is non-zero, point is inside
This means path direction matters and can create solid fills from complex paths.
1.12. Best Practices
1.12.1. Always Pair with gsave/grestore
% Good practice
gsave
newpath
100 100 80 60 link:/commands/references/arc/[`arc`]
closepath
clip
newpath
% Clipped operations
% ...
grestore
% Clip restored
1.12.2. Clear Path After clip
% Always clear path explicitly
newpath
100 100 moveto
200 200 lineto
closepath
clip
newpath % Important!
% Now draw clipped content
1.13. Performance Considerations
-
Simple rectangular clips are fastest
-
Circular and polygonal clips are moderately fast
-
Complex paths with many segments are slower
-
Each additional clip operation adds overhead
-
Very complex clipping paths may significantly impact rendering performance
-
Consider simplifying clips when possible
1.14. Clipping Path Management
% At document start
gsave
newpath
50 50 moveto
550 50 lineto
550 750 lineto
50 750 lineto
closepath
clip
newpath
% All document content
% ...
grestore
gsave
% Outer clip
newpath
50 50 200 200 link:/commands/references/arc/[`arc`]
clip
newpath
gsave
% Inner clip (intersection)
newpath
100 100 100 100 link:/commands/references/arc/[`arc`]
clip
newpath
% Drawing here is clipped to intersection
% ...
grestore
% Back to outer clip only
% ...
grestore
% Original clip restored
1.15. See Also
-
eoclip- Clip using even-odd rule -
clippath- Get current clipping path -
rectclip- Clip to rectangles (Level 2) -
initclip- Reset to device default -
fill- Fill path -
stroke- Stroke path -
gsave- Save graphics state -
grestore- Restore graphics state -
newpath- Clear current path -
closepath- Close current subpath