X-Received: by 10.66.52.17 with SMTP id p17mr673091pao.29.1361978010627; Wed, 27 Feb 2013 07:13:30 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!jn4no12886269pbb.1!news-out.google.com!jm3ni23360pbb.0!nntp.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 27 Feb 2013 09:13:30 -0600 From: John Deubert Organization: Acumen Training Newsgroups: comp.lang.postscript Date: Wed, 27 Feb 2013 07:13:30 -0800 Message-ID: <2013022707133067701-john@acumentrainingcom> References: <51295aca$0$2026$426a74cc@news.free.fr> MIME-Version: 1.0 Subject: Re: truncate strings / right aligment (alignement a droite et troncature de lignes) User-Agent: Unison/2.1.10 Lines: 181 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-z0sZiyO7o73I1aF6/2TvBOF35R5WggOXiSavWHBwNpha3qDqiUqke3TJz93ru6qnuNjnu55f1bOJeWe!+q4Xe8kAhl+x55gZzlBd7YwYPBOdvzpCcTlZYyGJUV0a/my1K9rdCh9a2LwPqIzGSDcQZt7u X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 6886 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Xref: csiph.com comp.lang.postscript:1279 On 2013-02-24 00:16:19 +0000, Thomas Harding said: > Hello / Bonjour > > Here is my first post here. It seems fr.comp.lang.postscript empty > Ceci est mon premier post dans *.comp.lang.postscript (t le fr.* est vide). > > There is no FU2, as I would for some traffic in fr.comp.lang.postscript. > > So, here is a tedious/beginner piece of code to shorten lines and also > right align. > > Voici un petit bout de code de débutant pour tronquer les lignes, et > éventuellement les justifier à droite. > > Goal is to fill forms. > > Le but est de remplir des formulaire "à cases"... > > > I think old-timers will correct all that :) > > =========================== > %!PS > %%Title: Basic PostScript String trimming and right justification > %%BoundingBox: 0 0 612 842 % for some viewers > %%Creator: tom@thomas-harding.name > %%Licence: GNU GPLv3 > %%For: comp.lang.postscript > %%Date: 2013.02.23 > %%Revised: 2013.02.23 > % %% would be cool to add it to tinydict :) > % > % Postscript procedures: right alignment, ashow-like right alignment, > % truncate strings (left or right) > % > % Copyright (2013) Thomas HARDING > % > % This program is free software: you can redistribute it and/or modify > % it under the terms of the GNU General Public License as published by > % the Free Software Foundation, either version 3 of the License, or > % (at your option) any later version. > % > % This program is distributed in the hope that it will be useful, > % but WITHOUT ANY WARRANTY; without even the implied warranty of > % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > % GNU General Public License for more details. > % > % You should have received a copy of the GNU General Public License > % along with this program. If not, see . > % > %%EndComments > %%BeginSetup > % temporary variables, useful here... > /Str { /str exch def } def > /Idx { /idx exch def } def 0 Idx > /Len { /len exch def } def 0 Len > /Chars { /chars exch def } def 0 Chars > /Maxlen { /maxlen exch def } def > /CurX { /curx exch def } def > /CurY { /cury exch def } def > /DestX { /destx exch def } def > /DestY { /desty exch def } def > /StrWidth { /strwidth exch def } def > % is len lower than maxlen? > /Maxlen? {len maxlen lt} bind def > % idx = len - maxlen ; len = maxlen > /rTruncate {len maxlen sub Idx maxlen Len} def > % Ooops: idx is negative cause string was too short > % so : len (to display ) is shortened by idx and then idx is 0 > /NegIdx? {0 idx lt {}{maxlen idx add Len 0 Idx} ifelse} def > % > % syntax (string) integer_lenght rightTrimString > % returns (string) interval limited to integer_lenght chars, right side > % > /rightTrimString {Maxlen dup length Chars 0 Idx chars Len Maxlen? {} if > rTruncate NegIdx? idx len getinterval} def > % > % syntax (string) integer_lenght leftTrimString > % returns (string) interval limited to integer_lenght chars, left side > % > /leftTrimString {Maxlen dup length Chars 0 Idx chars Len Maxlen? > {}{maxlen Len} ifelse idx len getinterval} def > % > % Obvious: go left from string with > % > /rightAlign { > currentpoint CurY CurX dup stringwidth DestY DestX > curx destx sub CurX > curx cury moveto > } def > % > % stringwidth will never work in case of ashow... > % so go left based from a "blind" one lenght then ashow > % > /rightAshow { > gsave > /Xsh { /xsh exch def } def > /Ysh { /ysh exch def } def > /Xstart currentpoint pop def > /Xstop { currentpoint pop } def > Str Ysh Xsh > gsave > nulldevice > % do a moveto to get a current path... > Xstart 0 moveto > xsh ysh str ashow > Xstop Xstart sub StrWidth > grestore > %gsave > %0 -10 rmoveto > %Xstart 20 string cvs show > %Xstop 20 string cvs show > %strwidth 20 string cvs show > %grestore > 0 strwidth sub 0 rmoveto > xsh 0 rmoveto > xsh ysh str ashow > grestore > } def > %%EndSetup > > %%BeginScript > > %(OK) show > /Courier 10 selectfont > 400 0 moveto 400 800 lineto stroke > %0 400 moveto 800 400 lineto stroke > > 400 410 moveto > (A simple right alignment without character limitation) rightAlign show > 400 400 moveto > %(X) show > ( of right-trimming a string e.g to 27 characters right trimmed) 27 > rightTrimString rightAlign show > %show % Ok : stack is empty > 400 390 moveto > 6 0 ( of right-trimming a rightAshow to 28 characters.) 28 > rightTrimString rightAshow > 400 380 moveto > (too short beside 80) 80 rightTrimString rightAlign show > 400 370 moveto > (too short beside 80) 80 leftTrimString show > 400 360 moveto > (of left-trimming a string e.g to 47 characters left trimmed) 47 > leftTrimString rightAlign show > %show % Ok : stack is empty > %(test) IdxAndLen > %(len) show len 20 string cvs show > %(idx) show idx 20 string cvs show > > 10 10 moveto > (This is a test) show > > showpage > %%EndScript > %%EOF > ================================================== You might find the July 2003 issue of the Acumen Journal interesting. The PostScript article that month is on justifying text (right, center, full) and presents somewhat more streamlined procedures for doing these tasks. The Acumen Journal is free for the downloading at www.acumentraining.com/acumenjournal.html. There are more than sixty of them available at this point, so there are a lot of topics covered. - John -- ======== John Deubert Acumen Training PostScript & PDF Engineering Classes & Consulting www.acumentraining.com Learn PostScript programming techniques Read the free Acumen Journal acumentraining.com/acumenjournal.html