bash - How to get display width of a string from Linux command line? -
i working on awk script processes text file line line, formats them , stuffs them svg file text field. svg takes care of text wrapping automatically, want predict each line wrap. (i need characters repeat , extend close end of line). know exact font, font size, , width of text field.
is there standard utility in linux or available in ubuntu give width in pixels or inches given string, font, , font size?
for example:
get-width 'nimbus sans l' 18 "test string" returns "x pixels"
you can ghostscript interpreter, assuming have , fonts set correctly.
here possibly mysterious incantation:
gs -dquiet -sdevice=nullpage 2>/dev/null - \ <<<'18 /nimbussanl-regu findfont exch scalefont setfont (test string) stringwidth pop =='
using -dquiet
suppresses warnings font substitution, not idea until have idea how name fonts you're looking for.
ghostscript
not layout engine, , may find measurement doesn't work complicated bidirectional text, combining characters, or east asian languages. (i tested little arabic, , ok, no guarantees.) not kern, produce measurements little larger layout engine, , possibly lot larger if font positions diacritics using kerning.
finally, if text includes unbalanced parentheses or backslashes, you'll need escape them. use following:
"$(sed 's/[()\\]/\\&/g' <<<"$text")"
that's because postscript strings enclosed in (...)
-- (test string)
-- , allowed include balanced parentheses. unbalanced parentheses generate syntax error, unless backslash-escaped.
Comments
Post a Comment