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


Groups > comp.lang.postscript > #1270 > unrolled thread

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

Started byThomas Harding <tom@harding.fr>
First post2013-02-24 01:16 +0100
Last post2013-02-27 19:20 -0800
Articles 9 — 4 participants

Back to article view | Back to comp.lang.postscript


Contents

  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

#1270 — truncate strings / right aligment (alignement a droite et troncature de lignes)

FromThomas Harding <tom@harding.fr>
Date2013-02-24 01:16 +0100
Subjecttruncate strings / right aligment (alignement a droite et troncature de lignes)
Message-ID<51295aca$0$2026$426a74cc@news.free.fr>
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
==================================================

-- 
Thomas Harding

[toc] | [next] | [standalone]


#1272

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-02-23 21:32 -0800
Message-ID<257d8e39-d4be-4b36-b553-bf296439f0dc@googlegroups.com>
In reply to#1270
On Saturday, February 23, 2013 6:16:19 PM UTC-6, Thomas Harding wrote:
> 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 :)
> 
> 

No offense, but it's very hard to read. I'd recommend using indentation and blank lines after procedure definitions.

And you have lots of 'setter' procedures. Take a look at this syntax for local variables: {/dic/proc/src}{exch def}forall

Longer example.
%!

%string proc  map  --
%array proc  map  --
% replace each element with proc(element)
/map{
    4 dict dup begin
    {/dic/proc/src}{exch def}forall

    0 1 src length 1 sub
    (
     {
        //dic/i 2 index put
        //src exch get
            //proc exec
        //src exch //dic/i get exch put
     }
    )token pop exch pop
    bind end for
}def

[1 1 1]dup{1 sub}map ==
(---)== flush
(this_is_a_lowercase_string___ish)dup
{16#20 sub}map
==



[toc] | [prev] | [next] | [standalone]


#1273

FromThomas Harding <tom@harding.fr>
Date2013-02-25 20:05 +0100
Message-ID<512bb504$0$2093$426a74cc@news.free.fr>
In reply to#1272
Le 24/02/2013 06:32, luser- -droog a écrit :
> On Saturday, February 23, 2013 6:16:19 PM UTC-6, Thomas Harding wrote:
>>
>> Goal is to fill forms.
>>
>>
>
> No offense, but it's very hard to read. I'd recommend using indentation and blank lines after procedure definitions.
>
/agree. The only usable samples I found are in Bluebook
(getting now a look at PLRM, which would be a betters start)
  and "tinydict".

For last it is as unreadable as what I given here :)

> And you have lots of 'setter' procedures. Take a look at this syntax for local variables: {/dic/proc/src}{exch def}forall

Yes, this also annoying me.

What I need is to understand how to handle dictionaries and how to 
*constantly* figure out the stack.

I'd better to run ghostscript manually (it's about 30 hours I work on a 
form with some chunks from "tinidict" -- and notably added a toolow? 
procedure which prints \(...\) then discards remaining text), instead of 
/writing the flow on a sheet of paper/ (obviously with errors).

What I needed for (manually run gs) in order to try your procedure.

eg: I didn't find radix expression in Bluebook, as so as many 
constructions used in your sample which are defined in PLRM.

Maybe this is your goal, while I'm still unable to get any FAQ here 
(comp.lang.postscript directory does not exists on rtfm,
and Google is no longer the better way to find useful resources...).

:)

> Longer example.
> %!
>
> %string proc  map  --
> %array proc  map  --
> % replace each element with proc(element)

It seems that procedure appears time to time here, but it currently 
totally puzzles me. As the code I given, one comment a line would be 
useful to newbies ^^

-- 
Thomas Harding

[toc] | [prev] | [next] | [standalone]


#1274

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-02-25 11:45 -0800
Message-ID<e80f308f-da69-4134-893d-27d1a7a7d400@googlegroups.com>
In reply to#1273
On Monday, February 25, 2013 1:05:50 PM UTC-6, Thomas Harding wrote:
> Le 24/02/2013 06:32, luser- -droog a �crit :
> 
> > On Saturday, February 23, 2013 6:16:19 PM UTC-6, Thomas Harding wrote:
> 
> >>
> 
> >> Goal is to fill forms.
> 
> >>
> 
> >>
> 
> >
> 
> > No offense, but it's very hard to read. I'd recommend using indentation and blank lines after procedure definitions.
> 
> >
> 
> /agree. The only usable samples I found are in Bluebook
> 
> (getting now a look at PLRM, which would be a betters start)
> 
>   and "tinydict".
> 

Yes. The PLRM is excellent. For learning, I'd recommend the first edition because it shows you just the basics with fewer distractions.
And get the Green Book, too. And "Thinking In Postscript", 
all free online (and super-cheap used).

> 
> For last it is as unreadable as what I given here :)
> 
> 
> 
> > And you have lots of 'setter' procedures. Take a look at this syntax for local variables: {/dic/proc/src}{exch def}forall
> 
> 
> 
> Yes, this also annoying me.
> 
> 
> 
> What I need is to understand how to handle dictionaries and how to 
> 
> *constantly* figure out the stack.

There are at least 2 different style of writing postscript. 
One is to write it more like C code where each line calculates a simple 
expression and stores the result in a variable. 

  /var a b add c div d exp def
  /res var e exp def

This way, there's no state left on the stack at the end of each line, therefore nothing to track.

I tend to do this style in my first drafts, and then eliminate variables for speed or space.

The opposite style is no variables. For this you need a disciplined use of comments, because that where the variable names are.

  a b c d e
  5 2 roll  % d e a b c
  3 1 roll  % d e c a b
  add       % d e c a+b
  exch div  % d e (a+b)/c
  3 -1 roll % e (a+b)/c d
  exp       % e ((a+b)/c)^d
  exch exp  % ((a+b)/c)^d^e
  /res exch def

Note the last line has no comment because the stack is now clear.

> I'd better to run ghostscript manually (it's about 30 hours I work on a 
> 
> form with some chunks from "tinidict" -- and notably added a toolow? 
> 
> procedure which prints \(...\) then discards remaining text), instead of 
> 
> /writing the flow on a sheet of paper/ (obviously with errors).
> 
> 
> 
> What I needed for (manually run gs) in order to try your procedure.
> 
> 
> 
> eg: I didn't find radix expression in Bluebook, as so as many 
> 
> constructions used in your sample which are defined in PLRM.
> 
> 
> 
> Maybe this is your goal, while I'm still unable to get any FAQ here 
> 
> (comp.lang.postscript directory does not exists on rtfm,
> 
> and Google is no longer the better way to find useful resources...).
> 

I found this. But all the FAQs are pretty old.
http://www.inkguides.com/postscript-faq.asp

There's stackoverflow.com if you've got a specific programming issue. 
And codereview.stackexchange.com for working code. 
Not a lot of postscript there at the moment, but the more the merrier!

> 
> 
> :)
> 
> 
> 
> > Longer example.
> 
> > %!
> 
> >
> 
> > %string proc  map  --
> 
> > %array proc  map  --
> 
> > % replace each element with proc(element)
> 
> 
> 
> It seems that procedure appears time to time here, but it currently 
> 
> totally puzzles me. As the code I given, one comment a line would be 
> 
> useful to newbies ^^
> 

I really just wanted to show you the local variable syntax.

But here goes. This procedure uses almost all of my tricksy tricks.


%string proc  map  -- 
%array proc  map  -- 
/map{
    4 dict dup begin  % src proc dic
    % This line creates and begins a dictionary, 
    % leaving a copy on the stack

    {/dic/proc/src}{exch def}forall
    % This is equivalent to
    % /dic exch def /proc exch def /src exch def
    % note /dic /proc and /src are in the reverse order in the stack

    0 1 src length 1 sub 
    % set up the parameters for a 'for' loop
    % which will index into /src
    % so it goes from 0 to length(src)-1 by 1
    ( % begin a string template
     { % begin loop body
        //dic/i 2 index put  % store the index as /i in the dict
        //src exch get       % get element i from src
            //proc exec      % execute procedure
        //src exch //dic/i get exch put  % store result back in src
     } % end loop body
    )token pop exch pop % extract loop body from string, replacing //names
    bind            % replace system names with operators
    end             % discard dictionary (names are already used)
    for             % run the 'for' loop
}def

[toc] | [prev] | [next] | [standalone]


#1276

FromThomas Harding <tom@harding.fr>
Date2013-02-27 02:14 +0100
Message-ID<512d5cdc$0$2222$426a34cc@news.free.fr>
In reply to#1274
Le 25/02/2013 20:45, luser- -droog a écrit :
> On Monday, February 25, 2013 1:05:50 PM UTC-6, Thomas Harding wrote:
>
> Yes. The PLRM is excellent. For learning, I'd recommend the first edition because it shows you just the basics with fewer distractions.
> And get the Green Book, too. And "Thinking In Postscript",
> all free online (and super-cheap used).

Thanks for all that. I pinned the FAQ pointers page too.

What I done first was really monkey code, as shown in /procBAD, listing 
2.1 of GreenBook you just pointed me :)... I'd better to ask for here 
good pointers instead of loosing time to /try to figure out/ an almost 
unreadable code (lack of comments) then adapt it.

"Thinking in Postscript" seems a good start of point. Bluebook is too 
synthetic compared too, and PLRM (version 3) discouraged me -- more than 
9 hundred pages, and a reference for advanced users more than a 
/learning book/.

Having also "petite introduction au language Postscript" (in French), 
from Jean-Paul Davalan, but mainly on mathematics (computing) and graphics.

"Thinking is postscript" seems like the "K&R": a schoolbook, what I 
needed for :)

==

Today I rewritten all the "shorten string" procedure (see below), using 
an array.

It is simple, while I "put/get" array elements instead of "exch def", so 
I'm unsure it is not also a /ProcBad sample ;)

At least there are no extra setters outside (even "ar" is not reachable 
outside).

> There are at least 2 different style of writing postscript.
> One is to write it more like C code where each line calculates a simple
> expression and stores the result in a variable.
>
>    /var a b add c div d exp def
>    /res var e exp def
[snip]

I still need a mix :)

The explained procedure is clear. I used here it to fill a new string 
with spaces (16#20).


%!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
%
% "map" procdure
%
% replace each element with proc(element)
% by: mijoryx@yahoo.com aka "luser- -droog", from comp.lang.postscript
%string proc  map  --
%array proc  map  --
/map{
         4 dict dup begin  % src proc dic
         % This line creates and begins a dictionary,
         % leaving a copy on the stack

         {/dic/proc/src}{exch def}forall
         % This is equivalent to
         % /dic exch def /proc exch def /src exch def
         % note /dic /proc and /src are in the reverse order in the stack

         0 1 src length 1 sub
         % set up the parameters for a 'for' loop
         % which will index into /src
         % so it goes from 0 to length(src)-1 by 1
         ( % begin a string template
           { % begin loop body
            //dic/i 2 index put  % store the index as /i in the dict
            //src exch get       % get element i from src
            //proc exec      % execute procedure
            //src exch //dic/i get exch put  % store result back in src
           } % end loop body
         )token pop exch pop % extract loop body from string, replacing 
//names
          bind            % replace system names with operators
          end             % discard dictionary (names are already used)
          for             % run the 'for' loop
}def
%
%
% shorten a (string) to N characters Left or Right
%
/ShortenString {

%(string) len dir fill ShortenString

% dir : O for Left (Lef) / 1 for Right (ight)
% fill: 0/1 : padding (1) too short lines
% (Left) 3 O 0 ShortenString =>  (Lef)
% (Right) 4 1 0 ShortenString => (ight)
% (if string too short) 80 1 1 => (    xN    if string too short)
% (if string too short) 80 1 0 => (if string too short)

     /ar [
         () % string to display
         0 % max lenght
         0 % 0 for left, 1 for right trim
         0 % will be strlen
         0 % will be index
         0 % will be "length" of string to display
	0 % will be 1 for "pad", 0: no pad
     ] def
     % pops from stack, fill "ar" : Fill, L/R, maxlen, (str)
     ar exch 6 exch put
     ar exch 2 exch put
     ar exch 1 exch put
     ar exch 0 exch put
     % put in ar at index 3 "How many chars in index0 of ar?"
     ar 3 ar 0 get length put
     % is (string length) > to max length
     ar 3 get ar 1 get gt {
         % case true, we shorten
         %
         % so,
         % do we shorten left or right ? (left: 0)
         ar 2 get 0 eq {
             % "left" trimming
             % ar 4 0 set %(already done)
         }{
             % "index" is  string lenght - max lenght
             ar 4
             ar 3 get ar 1 get sub
             put
         }ifelse
             % simply set "lenght to display" to "maxlen"
             ar 5 ar 1 get put
     }{
      % (string) is too short compared to "maxlen" parameter
      ar 6 get 1 eq {
      % create string, len = maxlen, then fill with spaces
      % this is already \0 (nul) in a new string,
      % anyway 0 mul => 0 to ensure; then  adding 20 hexa = ascii space
      % Q: will it work with any encoding ?
      ar 1 get string dup {0 mul 16#20 add} map dup
      % padding left (0) or right (1) ?
      ar 2 get 0 eq {
      %left: padding from end of string to maxlen
      0
      }{
      %right: so get maxlen - strlen as index, in order to put string at end
      ar 1 get ar 3 get sub
      } ifelse
      % the string
      ar 0 get
      % putted in new string...
      putinterval
      ar exch 0 exch put
      ar 5 ar 1 get put
      }{
      % no padding, so replacing "maxlen" by strlen
      %ar 5 ar 3 get put
      ar 5 ar 3 get put
      } ifelse
     }ifelse

     % ar: 0, the string, 4 string index, 5 length to display
     ar 0  get ar 4 get ar 5 get getinterval
} def
%%
/Left 0 def
/Right 1 def
/Pad 1 def
/NoPad 0 def
%%EndSetup
%%BeginScript

/Courier 10 selectfont

10 410 moveto
( of right-trimming  a string e.g to 27 characters right trimmed)
  27 Right NoPad ShortenString show

10 380 moveto
(too short beside 80 right pad)
  80 Right Pad ShortenString show

10 370 moveto
(too short beside 80i right nopad)
  80 Right NoPad ShortenString show

10 360 moveto
(too short beside 80 left pad)
  80 Left Pad ShortenString show
     (xxx where we are)show

10 350 moveto
(too short beside 80 left no pad)
  80 Left NoPad ShortenString show

10 340 moveto
(left-shorten string, e.g. to 42 characterslong )
42 Left NoPad ShortenString show

showpage
%%EndScript
%%EOF

Best Regards,
-- 
Thomas Harding

[toc] | [prev] | [next] | [standalone]


#1284

Fromtlvp <mPiOsUcB.EtLlLvEp@att.net>
Date2013-02-27 20:44 -0500
Message-ID<exlu2q54wvju.1wvknyzwrdmo$.dlg@40tude.net>
In reply to#1276
On Wed, 27 Feb 2013 02:14:16 +0100, Thomas Harding wrote:

> Having also "petite introduction au language Postscript" (in French), 
> from Jean-Paul Davalan, but mainly on mathematics (computing) and graphics.

Thanks for mentioning that. Looks nice, once I got over the initial shock
from "Ojets" -- nothing new, just "Objets" misspelled :-) . Cheers, -- tlvp
-- 
Avant de repondre, jeter la poubelle, SVP.

[toc] | [prev] | [next] | [standalone]


#1279

FromJohn Deubert <john@acumentraining.com>
Date2013-02-27 07:13 -0800
Message-ID<2013022707133067701-john@acumentrainingcom>
In reply to#1270
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

[toc] | [prev] | [next] | [standalone]


#1282

FromThomas Harding <tom@harding.fr>
Date2013-02-28 02:11 +0100
Message-ID<512eadc1$0$1838$426a74cc@news.free.fr>
In reply to#1279
Le 27/02/2013 16:13, John Deubert a écrit :

>> So, here is a tedious/beginner piece of code to shorten lines and also
>> right align.

>
> 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.

Thanks.

That article is very interesting, especially for code comprehension of 
procedures I found in "minidict.ps" and related !

There are "free fields" with ________ lines, I modified for a procedure 
from minidict -- as a blind recipe -- which your fully commented code 
will help me to rewrite in a sane maneer :)

Unfortunately, the pre-printed forms I have to fill are designed for 
hand-writing, with frames like that :

   |_|_|_|_|_||

              ^ "bold" frame limit => right justification
   ||_|_|_|_|_|
    ^ bold frame limit => left justification

Using /Courrier with an ashow and a trimmed/padded string is good enough.

I rewritten the trim/pad strings procedure and it's OK now (at least for 
me). message-id : <512d5cdc$0$2222$426a34cc@news.free.fr>

Obviously, frame-spacing varies field to field, what I didnt expected 
before to actually print ^^

I just have now to issue a procedure to compute 1 char frame for current 
font (Courrier is fixed), give field whole lenght and field frames 
count, to compute an "xpad".

then "(string) xpad 0 ashow"

No matter I redo the wheel : other persons has enough clue of office 
wysiswyg programs to do a batch, so I have now all time I want to learn :p

-- 
Thomas Harding

[toc] | [prev] | [next] | [standalone]


#1285

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-02-27 19:20 -0800
Message-ID<4e83ffde-0a1b-4342-a748-835b09aa9a78@googlegroups.com>
In reply to#1282
On Wednesday, February 27, 2013 7:11:42 PM UTC-6, Thomas Harding wrote:
> Le 27/02/2013 16:13, John Deubert a écrit :
> 
> 
> 
> >> So, here is a tedious/beginner piece of code to shorten lines and also
> 
> >> right align.
> 
> 
> 
> >
> 
> > 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.
> 
> 
> 
> Thanks.
> 
> 
> 
> That article is very interesting, especially for code comprehension of 
> 
> procedures I found in "minidict.ps" and related !
> 
> 
> 
> There are "free fields" with ________ lines, I modified for a procedure 
> 
> from minidict -- as a blind recipe -- which your fully commented code 
> 
> will help me to rewrite in a sane maneer :)
> 
> 
> 
> Unfortunately, the pre-printed forms I have to fill are designed for 
> 
> hand-writing, with frames like that :
> 
> 
> 
>    |_|_|_|_|_||
> 
> 
> 
>               ^ "bold" frame limit => right justification
> 
>    ||_|_|_|_|_|
> 
>     ^ bold frame limit => left justification
> 
> 
> 
> Using /Courrier with an ashow and a trimmed/padded string is good enough.
> 
> 
> 
> I rewritten the trim/pad strings procedure and it's OK now (at least for 
> 
> me). message-id : <512d5cdc$0$2222$426a34cc@news.free.fr>
> 
> 
> 
> Obviously, frame-spacing varies field to field, what I didnt expected 
> 
> before to actually print ^^
> 
> 
> 
> I just have now to issue a procedure to compute 1 char frame for current 
> 
> font (Courrier is fixed), give field whole lenght and field frames 
> 
> count, to compute an "xpad".
> 
> 
> 
> then "(string) xpad 0 ashow"
> 
> 
> 
> No matter I redo the wheel : other persons has enough clue of office 
> 
> wysiswyg programs to do a batch, so I have now all time I want to learn :p
> 
> 
> 
> -- 
> 
> Thomas Harding

Here's an example of a form-filling program. I had the luxury of drawing the form as well, so I knew all the dimensions already. But it makes no attempt to trim or pad the strings, instead they are organized in an array with the dimensions specified separately.

https://groups.google.com/d/msg/comp.lang.postscript/18ZkuyjfbPA/FuKQ6dsMdr0J

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.postscript


csiph-web