1. charpath

Get character outlines as path.

1.1. Syntax

string bool charpath → -

1.2. Stack Effects

Before:

string bool

After:

(empty)

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:

  • If bool is false, charpath simply appends the character path to the current path; the result is suitable only for stroking.

  • If bool is true, charpath applies the strokepath operator to the character path; the result is suitable for filling or clipping, but not for stroking.

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.6.3. Clipping with Text

% Use text as clipping path
gsave
  newpath
  200 400 moveto
  (CLIP) true charpath
  clip

  % Draw something clipped by text
  0 1 1 setrgbcolor
  0 0 moveto
  400 800 lineto stroke
grestore

1.6.4. Text with Pattern Fill

/Helvetica-Bold findfont 72 scalefont setfont

newpath
100 650 moveto
(PATTERN) true charpath

% Fill with pattern (simplified)
gsave
  clip
  % Draw pattern within text outline
  0 5 400 {
    0 moveto 400 0 rlineto stroke
  } for
grestore

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:

  1. Font selects appropriate glyph

  2. Glyph outline is obtained

  3. Outline is transformed by FontMatrix and CTM

  4. Outline is appended to current path

  5. Current point updated (but path continues)

1.9. True vs. False Parameter

bool Effect Use For

false

Character paths appended as-is

Stroking character outlines

true

Paths converted via strokepath

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.10.3. Text Outline with Effects

/fancyText {  % string fancyText -
  % White fill
  dup true charpath
  1 setgray fill

  % Black outline
  true charpath
  0 setgray
  3 setlinewidth stroke
} def

100 700 moveto
(FANCY) fancyText

1.10.4. Getting Character Bounds

/stringBBox {  % string stringBBox llx lly urx ury
  gsave
    newpath
    0 0 moveto
    true charpath
    pathbbox
  grestore
} def

/Helvetica findfont 12 scalefont setfont
(Hello) stringBBox
% Returns: llx lly urx ury

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

show

Paints characters (rasterized)

charpath

Constructs path outlines

stringwidth

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


Back to top

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