Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #1279

Re: truncate strings / right aligment (alignement a droite et troncature de lignes)

From John Deubert <john@acumentraining.com>
Organization Acumen Training
Newsgroups comp.lang.postscript
Date 2013-02-27 07:13 -0800
Message-ID <2013022707133067701-john@acumentrainingcom> (permalink)
References <51295aca$0$2026$426a74cc@news.free.fr>
Subject Re: truncate strings / right aligment (alignement a droite et troncature de lignes)

Show all headers | View raw


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 <tom@thomas-harding.name>
> %
> %    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 <http://www.gnu.org/licenses/>.
> %
> %%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

Back to comp.lang.postscript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

truncate strings / right aligment (alignement a droite et troncature de lignes) Thomas Harding <tom@harding.fr> - 2013-02-24 01:16 +0100
  Re: truncate strings / right aligment (alignement a droite et troncature de lignes) luser- -droog <mijoryx@yahoo.com> - 2013-02-23 21:32 -0800
    Re: truncate strings / right aligment (alignement a droite et troncature de lignes) Thomas Harding <tom@harding.fr> - 2013-02-25 20:05 +0100
      Re: truncate strings / right aligment (alignement a droite et troncature de lignes) luser- -droog <mijoryx@yahoo.com> - 2013-02-25 11:45 -0800
        Re: truncate strings / right aligment (alignement a droite et troncature de lignes) Thomas Harding <tom@harding.fr> - 2013-02-27 02:14 +0100
          Re: truncate strings / right aligment (alignement a droite et troncature de lignes) tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2013-02-27 20:44 -0500
  Re: truncate strings / right aligment (alignement a droite et troncature de lignes) John Deubert <john@acumentraining.com> - 2013-02-27 07:13 -0800
    Re: truncate strings / right aligment (alignement a droite et troncature de lignes) Thomas Harding <tom@harding.fr> - 2013-02-28 02:11 +0100
      Re: truncate strings / right aligment (alignement a droite et troncature de lignes) luser- -droog <mijoryx@yahoo.com> - 2013-02-27 19:20 -0800

csiph-web