Question

You can obtain the width of a string in the current font with stringwidth and although this actually pushes offset coordinates on the stack, the y-value always seems to be useless. Is there a way to determine the exact height of a string, that may or may not include descenders?

Was it helpful?

Solution

stringwidth, as it says, doesn't return string's height. (In all cases I looked at, the second integer on the stack after executing stringwidth was 0 -- for strings that run in horizontal direction.) stringwidth gives the relative coordinates of the currentpoint after executing a (string) show.

The PLRM has this to say about stringwidth:

Note that the width returned by stringwidth is defined as movement of the current point. It has nothing to do with the dimensions of the glyph outlines.

So what would work to take into account the string's height? The magic words to read up about in PRLM are charpath and pathbbox. Try this:

%!
/Helvetica findfont 60 scalefont setfont
200 700 4 0 360 arc fill 
200 700 moveto (test test) dup 
true charpath pathbbox 
3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 
1 0 0 setrgbcolor
200 700 moveto rmoveto show showpage

It calculates the string's (printed in red) height and uses that info to try and center a small filled circle (printed in black) into the center of its bounding box:

Sample PostScript visualized

OTHER TIPS

I have already answered this in How to determine string height in PostScript?, but it is useful here also.

Just adding to pipitas answer:

/textheight { 
    gsave                                  % save graphic context
    {                            
        100 100 moveto                     % move to some point 
        (HÍpg) true charpath pathbbox      % gets text path bounding box (LLx LLy URx URy)
        exch pop 3 -1 roll pop             % keeps LLy and URy
        exch sub                           % URy - LLy
    }
    stopped                                % did the last block fail?
    {
        pop pop                            % get rid of "stopped" junk
        currentfont /FontMatrix get 3 get  % gets alternative text height
    }
    if
    grestore                               % restore graphic context
} bind def

/jumpTextLine { 
    textheight 1.25 mul                    % gets textheight and adds 1/4
    0 exch neg rmoveto                     % move down only in Y axis
} bind def

The method expects that some font is already set. It works over the selected font (setfont) and its size (scalefont).

I use (HÍpg) to get the biggest bounding box possible, using accentuated uppercase characters and "below line" characters. The result is good enough.

The alternative approach steals from dreamlax's answer -- some fonts do not support charpath operator.

Saving and restoring the graphic context keeps the current point in place, so it has no impact over the "flow" of your document.

Hope I've helped.

This seems to work most of the time:

/fontheight { currentfont /FontMatrix get 3 get } bind def
/lineheight { fontheight 1.2 mul } bind def

It won't work for all /FontTypes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top