1

I would like to place a horizontal line immediately below some text. Is it possible to determine the distance between the baseline and lowest descender of a given font in Ghostscript, or is the descender length only a property of certain characters and the offset can only be found by eye?

In my example below, the moveto command shifts the 20pt text to 20 points from the upper edge of the figure, and a vertical line of length 20 points (included for comparison) looks like it corresponds to the baseline of the font.

gs -q -o test.pdf -sDEVICE=pdfwrite -dDEVICEWIDTHPOINTS=200 -dDEVICEHEIGHTPOINTS=100 -c "newpath 27 80 moveto 27 100 lineto 0.5 setlinewidth stroke /Helvetica findfont 20 scalefont setfont 3 80 moveto (gijlpqy) show showpage"

3
  • In each font there is an array called font bounding box, which is the maximum rectangle into which every glyph fits. With postscript, this is (xlower, ylower, xupper, yupper), so the second value of this array is the distance from the baseline to the lowest point of any character.
    – ridgy
    Jan 1, 2018 at 13:24
  • To get this information for PS (type1 or type3) fonts, you can find the .pfb or .pfa and grep for FontBBox. Remember, PS fonts use 1000 units for one point. Or, if you are used to PS, you may extract this from the font in your program. It's more than 20 years I did this the last time, so please forgive me I can't help you more at the moment.
    – ridgy
    Jan 1, 2018 at 13:42
  • Look at stackoverflow.com/questions/3618194/… for a given string...
    – ridgy
    Jan 1, 2018 at 14:30

1 Answer 1

1

If you want to draw the line just under the descenders of a given string you can use pathbbox as in the link provided by ridgy in the comments to your question, but if you want a constant distance from the baseline you must use the font bounding box FontBBox which is a rectangle that encloses all characters of the font.

You need to scale this by the font's FontMatrix. You can find the code to do this in this stackoverflow answer by jcomeau_ictx, and copied below. Add this definition to the start of your PostScript code:

/descender { currentfont dup /FontBBox get           % [llx lly urx ury]
             1 get                                   % lower-left-y
             exch /FontMatrix get                    % [...]
             3 get                                   % yscale
             mul } def

and use it after your show operator. It will put the y-distance from the baseline to the lower edge of the font bounding box onto the stack. Simply add your line drawing code there, eg:

descender 0 exch  rmoveto  -100 0 rlineto  stroke

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .