🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.postscript
5 messages
5 total messages Started by Harald Martens Meyer Wed, 16 Dec 1992 12:11
Justifying strings, how??
#5953
Author: Harald Martens Meyer
Date: Wed, 16 Dec 1992 12:11
13 lines
931 bytes
I want to be able to justify strings; left, right and middle. How can
I do this when almost all PostScript fonts are non-fixed fonts? How do
I calculate where to put the string???

Anybody have a piece of code, or a general idea of how to do this?

  2
HM
--
Harald Martens Meyer    | Geomatic a.s       |      #_O_#      Powder
Email:meyer@geomatic.no | PO.Box 165 Skoyen  |    (/ _|_ \)     Snow
Tel: +47 2 50 43 30     | N-0212 OSLO 2      |     \/   \/       Is
Tfx: +47 2 50 05 55     | NORWAY             |      \   /  Simply the Best!
Re: Justifying strings, how??
#5955
Author: Jonathan Monsarrat
Date: Wed, 16 Dec 1992 14:54
81 lines
3519 bytes
> I want to be able to justify strings; left, right and middle.

Something like this should work. Use the "stringwidth" command to
get the length of the string you want to print. stringwidth returns
two values -- a X and a Y displacement. The Y is usually zero, so
"stringwidth pop" is a nice combination which will give you X displacement.

The below program is a bitty example of what you want to do.

If you want something a little more sophisticated and fancy, try
out the Rect.ps library for PostScript programmers. You can get it
and example files by ftp to wilma.cs.brown.edu:pub/postscript/Rect.

If you want something even fancier, try a drawing program that
produces Encapsulated PostScript.

-Jon
%! Jon Monsarrat   jgm@cs.brown.edu   Brown University %! Obfuscated PostScript
/f{dup 0 1 getinterval cvn exch cvx def}def<73657467726179>f<6D6F7665746F>f<63
6C6F736570617468>f(translate)f<6C696E65746F>f/p{45 rotate}def<726375727665746F
>f 144 36 m 360 36 l 360 396 l 144 396 l c 0.2 s fill 201.6 216 m 144 0 288 0
230 216 curveto c 1 s fill 216 dup t p 1 1 50{0 0 m 1 1 6{pop{rand 1073741824
div 2 div} cvx dup 1 exch exec sub s exec 288 mul -72 add } for r currentpoint
stroke gsave t .5 3 {setlinewidth s newpath 0 0 m 1 1 8{pop 3.6 0 18 13.68 0
7.2 r p}for}.8 10 2 index exec fill exec stroke grestore} for showpage


-----------------------  ex_justify.ps  ---------------------
%! PostScript quickie by Jon Monsarrat, jgm@cs.brown.edu
/SIZE 50 def                                   % What size will my font be?
/Times-Roman findfont SIZE scalefont setfont   % What will my font be?
/inch {72 mul} def                             % For convenience
/cm {28.34} def                                % For those who like metric
/rightmargin 7 inch def   % The right margin of the text
/leftmargin 1 inch def    % The left margin of the text
/bottommargin 1 inch def  % (do you really need a comment here?)
/topmargin 10 inch def    % (this comment intentionally left blank)

/drop      % Drop down one line from the current position.
{
  /height height SIZE 1.2 mul sub def    % Why 1.2? It usually works nicely.
  % if the height of the current text is below the bottommargin, new page!!
  height bottommargin lt { showpage /height topmargin def drop } if
} bind def

/rightj    % Takes a string off the stack and prints it right flushed
{
  dup stringwidth pop rightmargin exch sub height moveto show
  drop
} bind def

/leftj     % Takes a string off the stack and prints it left flushed
{
  leftmargin height moveto show
  drop
} bind def

/center    % Takes a string off the stack and prints it centered
{
  dup stringwidth pop rightmargin exch sub leftmargin sub 2 div
  leftmargin add height moveto show
  drop
} bind def

% The "height" variable is the current height of the text about to be
% printed.
/height topmargin def drop

(right)      rightj
(justified)  rightj
(text)       rightj

(left)       leftj
(justified)  leftj
(text)       leftj

(center)     center
(justified)  center
(text)       center

showpage
Re: Justifying strings, how??
#5964
Author: Henry McGilton
Date: Thu, 17 Dec 1992 01:11
71 lines
2847 bytes
In article <MEYER.92Dec16131125@ibsen.geomatic.no>  writes:
    *  I want to be able to justify strings; left, right and middle. How can
    *  I do this when almost all PostScript fonts are non-fixed fonts? How do
    *  I calculate where to put the string???
    *  Anybody have a piece of code, or a general idea of how to do this?
These kinds of calculations are trivially easy for the trivially
easy cases.  Following is some PostScript code taken from Chapter
3 of ``PostScript by Example'' by Mary Campione and Henry McGilton,
published by Addison-Wesley.  More fun starts when the strings to be
justfified are in multiple fonts -- we also discuss this case.

	........  Henry

Here cometh the code:
%!PS

%	This example appears in section "Justification Operations"
%	in Chapter 3 - "Text".
%

/inch { 72 mul } def		%  define inch procedure
/Palatino-Roman findfont	%  obtain font
1 inch scalefont		%  scale font to required size
setfont				%  set into graphics state
0.5 inch 5.5 inch moveto	%  set current point
(Left Text) show		%  show the string
showpage			%  display page

%!PS
%	This example appears in section "Justification Operations"
%	in Chapter 3 - "Text".
%

/inch { 72 mul } def		%  define inch procedure

/center_show {			%  stack =  x  y  (string)
	dup			%  make copy of string
	stringwidth pop 2 div	%  compute half string width
	4 -1 roll exch sub	%  decrement x position
	3 -1 roll moveto	%  set current point
	show			%  image string
} def

/Palatino-Roman findfont	%  obtain font
1 inch scalefont		%  scale font to required size
setfont				%  set into graphics state
4.25 inch 5.5 inch		%  current point
(Centered Text) center_show	%  show in middle of page
showpage			%  display page
%!PS

%	This example appears in section "Justification Operations"
%	in Chapter 3 - "Text".
%

/inch { 72 mul } def		%  define inch procedure

/right_show {			%  stack = x  y  (string)
	dup			%  make copy of string
	stringwidth pop		%  compute string width
	4 -1 roll exch sub	%  decrement x position
	3 -1 roll moveto	%  set current point
	show			%  display the string
} def

/Palatino-Roman findfont	%  obtain font
1 inch scalefont		%  scale font to required size
setfont				%  set into graphics state
8 inch 5.5 inch			%  current point
(Right Text) right_show		%  show string at right of page
showpage			%  display page
Re: Justifying strings, how??
#5983
Author: Stephen Zisk
Date: Fri, 18 Dec 1992 23:07
31 lines
2341 bytes
In article <1992Dec17.011125.8658@trilithon.mpk.ca.us>,
henry@trilithon.mpk.ca.us (Henry McGilton) wrote:
> 
> In article <MEYER.92Dec16131125@ibsen.geomatic.no>  writes:
>     *  I want to be able to justify strings; left, right and middle. How can
>     *  I do this when almost all PostScript fonts are non-fixed fonts? How do
>     *  I calculate where to put the string???
>     *  Anybody have a piece of code, or a general idea of how to do this?
> These kinds of calculations are trivially easy for the trivially
> easy cases.  Following is some PostScript code taken from Chapter
> 3 of ``PostScript by Example'' by Mary Campione and Henry McGilton,
> published by Addison-Wesley.  More fun starts when the strings to be
> justfified are in multiple fonts -- we also discuss this case.
> <PostScript program was here>

This works well, but puts the load onto the PostScript interpreter.
For tests, short jobs, and personal use, this is fine, but if you
are writing a driver with performance in mind, you should at least
consider doing the job on the host. This is often required anyway,
if you are doing line- or page-breaking, hyphenation, or other
sophisticated typography. If this is what you need, look at the
AFM files, which give character widths (and also provide other
goodies like bounding boxes, kerning info, etc). The format is defined
in technote #5004, and there are a bunch of other technotes on text
setting for a driver, all available from the Adobe File Server.

Stephen Zisk (zisk@adobe.com)  Disclaimer: These are my personal views
Adobe Systems Incorporated     and do not represent the position of
Eastern Regional Office        Adobe Systems Incorporated.
24 New England Exec. Park
Burlington, MA 01803           They pay me but they don't muzzle me!
Re: Justifying strings, how??
#5805
Author: peter da silva
Date: Mon, 21 Dec 1992 16:34
29 lines
1998 bytes
In article <zisk-181292180203@macne003.boston.us.adobe.com> zisk@adobe.com (Stephen Zisk) writes:
(justification on the printer...)
> This works well, but puts the load onto the PostScript interpreter.

<stringwidth> seems to be a cheap operator. 

> For tests, short jobs, and personal use, this is fine, but if you
> are writing a driver with performance in mind, you should at least
> consider doing the job on the host.

I don't know about that. I've used text-to-Postscript preprocessors, and
even on printers with a cheesy 68000 I've got better results in terms of
page speed letting the printer do this sort of thing. It seems that parsing
Postscript (breaking up numbers, words, etc) is relatively expensive
compared to executing the Postscript code... at least, that's what my
informal timings (printing a sizeable text file in MP, TEXTPS, PPS, and
other front ends) seem to imply.

As always, your milage may vary. Run tests both ways and see what happens.

Personally, I would prefer it if programs made decisions like this as late
as possible, so I can print a document with a different font (say, my printer
doesn't handle Palatino and the company is too cheap to buy a softfont)
without screwing things up too badly.
-- 
Peter da Silva                                            `-_-'
Ferranti International Controls Corporation                'U` 
Sugar Land, TX  77487-5012 USA
+1 713 274 5180                            "Zure otsoa besarkatu al duzu gaur?"
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads