- 1. charpath
1. charpath
Get character outlines as path.
1.3. Description
charpath obtains the character path outlines that would result if string were shown at the current point using show. Instead of painting the path, however, charpath appends the path to the current path. This yields a result suitable for general filling, stroking, or clipping.
The bool operand determines what happens if the character path is designed to be stroked rather than filled or outlined:
charpath does not produce results for portions of a character defined as images or masks rather than as paths.
1.4. Protected Fonts
The outlines of some fonts are protected. (In Level 1 implementations, this applies to all fonts; in Level 2, only to certain special fonts and not to ordinary Type 1 or Type 3 fonts.)
If the current font is protected, using charpath to obtain its outlines causes the pathforall and upath operators to be disabled for as long as those outlines remain in the current path.
1.5. Parameters
string (string)
: The text whose character outlines are to be obtained
bool (boolean)
: - true - Convert stroked paths to filled outlines (via strokepath)
- false - Use paths as-is
1.6. Examples
1.6.1. Outlined Text
/Helvetica-Bold findfont 48 scalefont setfont
newpath
100 700 moveto
(OUTLINED) true charpath
% Stroke the outline
2 setlinewidth
0 setgray stroke
1.6.2. Filled and Stroked Text
/Helvetica-Bold findfont 48 scalefont setfont
newpath
100 700 moveto
(TEXT) true charpath
% Fill with color
0.9 0.9 0.9 setrgbcolor fill
% Stroke outline
newpath
100 700 moveto
(TEXT) true charpath
0 setgray
1 setlinewidth stroke
1.7. Errors
limitcheck : Path becomes too complex
nocurrentpoint : Current point is not defined
stackunderflow : Fewer than two operands on stack
typecheck
: string is not a string or bool is not a boolean
1.8. Character Path Construction
For each character in the string:
-
Font selects appropriate glyph
-
Glyph outline is obtained
-
Outline is transformed by
FontMatrixand CTM -
Outline is appended to current path
-
Current point updated (but path continues)
1.9. True vs. False Parameter
| bool | Effect | Use For |
|---|---|---|
|
Character paths appended as-is |
Stroking character outlines |
|
Paths converted via |
Filling or clipping with stroked characters |
Example:
% For stroked appearance, use false
newpath
100 100 moveto
(ABC) false charpath
0.5 setlinewidth stroke
% For filled appearance of stroked chars, use true
newpath
100 200 moveto
(ABC) true charpath
fill
1.10. Use Cases
1.10.1. Drop Shadow Effect
/dropShadow { % string dropShadow -
gsave
% Shadow
0.7 setgray
currentpoint
2 add exch 2 sub exch moveto
dup true charpath fill
% Main text
0 setgray
moveto
true charpath fill
grestore
} def
100 700 moveto
(SHADOW) dropShadow
1.10.2. Gradient Fill Text
% Fill text with gradient
newpath
200 400 moveto
(GRADIENT) true charpath
gsave
clip
% Create gradient within text
0 1 100 {
dup 100 div setgray
0 exch 400 1 rectfill
} for
grestore
1.11. Advanced Techniques
1.11.1. Per-Character Path Manipulation
% Get individual character paths
/charPaths { % string charPaths -
{
% For each character
1 string dup 0 4 -1 roll put
gsave
newpath
currentpoint moveto
dup true charpath
% Process path here
gsave 0.8 setgray fill grestore
0 setgray 0.5 setlinewidth stroke
stringwidth rmoveto
grestore
} forall
} def
100 700 moveto
(PATHS) charPaths
1.11.2. Text Measurement Without Side Effects
/measureText { % string measureText width height
gsave
newpath
0 0 moveto
true charpath
pathbbox
% Returns: llx lly urx ury
3 -1 roll sub % height = ury - lly
3 1 roll exch sub % width = urx - llx
grestore
} def
(Sample) measureText % Returns actual visual dimensions
1.12. Path Reuse
% Create text path once, use multiple times
/textPath {
gsave
newpath
0 0 moveto
(REUSE) true charpath
currentpoint /endY exch def /endX exch def
grestore
} def
% Use the path multiple times
100 100 translate
textPath gsave 0.9 setgray fill grestore
textPath 0 setgray 1 setlinewidth stroke
1.13. Limitations
Image-based characters:
- charpath does not produce paths for bitmap characters
- Some fonts mix paths and bitmaps
- Only path-based portions are included
Protected fonts:
- Some fonts prevent path extraction
- Affects pathforall and upath operations
- Level 2 Type 1 fonts typically not protected
Font caching: - May cause characters to be added to font cache - Cache side effects persist
1.14. Comparison with show
| Operator | Output |
|---|---|
Paints characters (rasterized) |
|
Constructs path outlines |
|
Returns width vector only |
1.15. See Also
-
show- Paint text string -
stringwidth- Calculate text width -
pathbbox- Get path bounding box -
strokepath- Convert stroke to filled path -
clip- Set clipping path -
fill- Fill path -
stroke- Stroke path -
setfont- Establish current font