Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

right trim a string? 1

Status
Not open for further replies.

smeaggie

Programmer
May 5, 2004
8
NL
Hello everybody,

I've got a program wich fills in variables inside a postscript file before I print it. To make sure the program doesn't overwrite any postscript source while inserting the values I have to add some spaces, like this:

Code:
(%33          ) show

the %33 part is overwritten by the program, if the text is too long, the "show" command is literraly overwritten so corrupts the source. so I add some spaces. No I have to print a line right after the string, so those spaces are annoying, because they are countered to when using the stringlength operator. Is there a way to eliminate the right most spaces? NOTE: the inserted text may also some spaces, so not all spaces should be eliminated...

Greetings,
Smeaggie
 
There is no "trim" operator in PostScript. You'll need to write a procedure to do this yourself using "search" or better yet "get".

Then you'll have to override "stringwidth" and "show" and any other string processing operators to first call your proc.

This is a little complex, but can be done.

Off the top of my head, I'd use "length" to get the count of the characters, then work back a character at a time (use the "get" operator). Compare the character/integer with the value for space (decimal 32). Put this in a loop that keeps working back up the string until you encounter a non-space. Record that position and create a new string using "getinterval". Pass that to your various string operators.

How to "overload" operators in PostScript: Say you wanted to add some code to the "show" operator. In our case, we want "show" to first run our procedure, leave a new modified string on the stack, and the do the real "show".

Code:
%!PS

/myTrimProc
{
  %code to trim a space-padded string
} bind def

/myShow /show load def  % load "show" into myShow

/show
{ myTrimProc  % first call trim procedure
  myShow      % then call "loaded" or "real" show
} bind def

(This is a test    ) show



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Here's some code to use, if you like.

Code:
%!PS

/rightTrim
{ dup
  length 1 sub -1 0
  { /i exch def dup i get 32 ne {exit} if
  } for
  0 i 1 add getinterval
  dup length string copy
} bind def

/myStringwidth /stringwidth load def
/myShow /show load def

/stringwidth
{ rightTrim
  myStringwidth
} bind def

/show
{ rightTrim
  myShow 
} bind def


/Courier 24 selectfont
10 600 moveto
(This is a test        ) show
( of right-trimming a string.           ) show

showpage



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
thanks this is really cool stuff :)

greetings,
smeaggie
 
Thanks. I have to be nosy, though, and wonder about your overall approach. Wouldn't it be better, to replace your "target strings" with PostScript names? And then let your calling program define those?

So instead of:

(%33 ) show

You would have:

myVar33 show

Then could your calling program write out the definitions, even to a separate file?

/myVar33 (This is the data) def

Then you could concatenate the two files and run them.

Another approach is to embody the PostScript program inside a procedure (call it /drawPage).

Then have a second procedure that 1) expects a bunch of data on the stack, gets it and plugs it into definitions and then 2) calls drawPage.

Structure:

Code:
%!PS

/drawPage
{
  % the big program. does all the shows, etc.
  % uses names like "myVar33 show"
} bind def

/getData
{ /myVar1 exch def
  /myVar2 exch def
  % and so on 
  /myVar33 exch def
  drawPage
} bind def

%next section written out by calling program
(This will end up in myVar33)
(Value for myVar2)
(Could be numbers, not just strings. This is myVar1)
getData



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
well that should work for sure, but I didn't write the calling program. it was written some time ago when not using postscript yet but just pure text, I build the postscript stuff around that later... I can't rewrite the calling program, but your solution works perfect so I'm happy anyway :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top