[comp.lang.postscript] printf

clp@altos86.Altos.COM (Chuck L. Peterson) (10/25/90)

I use this to print out a number:
	num 4 string cvs show

What do I have to change to do the equivilent of
printf("%8d",num) here?

Chuck L. Peterson
clp@altos.com

glenn@heaven.woodside.ca.us (Glenn Reid) (10/25/90)

In article <4296@altos86.Altos.COM> clp@altos86.Altos.COM (Chuck L. Peterson) writes:
>I use this to print out a number:
>	num 4 string cvs show
>
>What do I have to change to do the equivilent of
>printf("%8d",num) here?

Here's what I do.  You can adapt this approach for variable notions
of 8 or whatever.  The basic idea is to have a string that is the
right field length, fill it with padding (spaces or 0's), then use
"putinterval" to put your string (from cvs) into the buffer, either
flush-left or flush-right.  The program below emulates "%8d" exactly
(I think).

%!
/scratch 8 string def
/buff 8 string def
/padding (\040\040\040\040\040\040\040\040) def
% (\040) is a space, but is easier to see and count :-)
% note: padding could be (00000000) if you wanted leading 0's.
% note: also, replace "dup length 8 exch sub" with "0" to make flush-left.
% note: the space is not the same width as a '0' in a non-proportional font.

/printf8 {
	buff 0 padding putinterval	% put 8 spaces into buff (each time)
	scratch cvs			% convert the number to a string
	0%dup length 8 exch sub		% compute its length, subtract from 8
	exch buff 3 1 roll putinterval	% put numstring into rightmost cols of buff
	buff show			% buff is now (00000123)
} bind def

100 100 moveto
/Courier findfont 24 scalefont setfont
123 printf8
30 0 rmoveto
3465 printf8

showpage

%%EOF

(Glenn) cvn

-- 
 Glenn Reid				RightBrain Software
 glenn@heaven.woodside.ca.us		PostScript/NeXT developers
 ..{adobe,next}!heaven!glenn		415-851-1785

clp@altos86.Altos.COM (Chuck L. Peterson) (10/26/90)

Only after getting the %8d thing working did I realize that
this is completely not what I want since my goal is
to right-justify numbers.  It turns out that %8d will only
work with a constant-width font.

I fixed my problem by doing right-justification at (x,y) with this:

	number 8 string cvs dup stringwidth pop
	x exch sub y moveto
	show

Chuck L. Peterson
clp@altos.com

glenn@heaven.woodside.ca.us (Glenn Reid) (10/27/90)

In article <4311@altos86.Altos.COM> clp@altos86.Altos.COM (Chuck L. Peterson) writes:
>Only after getting the %8d thing working did I realize that
>this is completely not what I want since my goal is
>to right-justify numbers.  It turns out that %8d will only
>work with a constant-width font.

Well, since I took the time to answer your original question, I feel
entitled to point out that you probably should have asked the higher-order
question about how to right-justify text (or at least included some notion
of what it was you were trying to accomplish), rather than just asking how
to emulate the printf("%8d").  :-)

Glad you got it working, though.  I didn't much like the %8d stuff anyway,
and I'm glad you didn't need it.

/Glenn

-- 
 Glenn Reid				RightBrain Software
 glenn@heaven.woodside.ca.us		PostScript/NeXT developers
 ..{adobe,next}!heaven!glenn		415-851-1785