How can I have the text size shrink automatically depending on character length in Postscipt? Or to have the text resize in order to fit a fixed area?
Asked
Active
Viewed 122 times
1
-
1You want to use `stringwidth`. See http://stackoverflow.com/questions/3618194/how-to-determine-string-height-in-postscript for a related discussion. – lhf Jul 16 '15 at 01:36
2 Answers
2
I think it's simpler to make a 1-pt font, measure,and then scale up. Something like (untested)
/showwid { % string width /font-name
gsave
1 selectfont
1 index stringwidth pop div % width/stringwidth
currentfont scalefont setfont
show
grestore
} def
This code doesn't account for height at all.

luser droog
- 18,988
- 3
- 53
- 105
1
The way i do it is:
- get string width
- divide area width by string width
- multiply current font size by that
- then use this as new font size and show
So, the code is something like:
%some bogus vars to make it understandable
/myfont /Helvetica def
/mysz 10 def
/mywidth 100 def
/mystr (Hello PS world!) def
%supposedly you'd be in the middle of something
%therefore we would have the font already selected
myfont mysz selectfont
50 50 moveto
myfont mywidth mystr stringwidth pop div mysz mul selectfont
mystr show
% and for good measure, a line showing the size
50 49 moveto
mywidth 0 rlineto stroke

Marcio Baraco
- 11
- 1