🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.postscript
Article #1991

Re: Absolutely sized fonts

#1991
From: Ciaran McHale
Date: Thu, 27 Aug 1992 15:17
94 lines
2995 bytes
In <9208271402.AA20109@enet-gw.pa.dec.com>
mek@hydrox.enet.dec.com (Mark Klamerus) writes:
>	Is there a way to absolutely specify the height and width of
>	a fixed font (like courier).  I need to create letters of a
>	sized which completely fits within a box and which fills the
>	box.  I need to know that given an x,y position I can draw
>	a box which is 300 x 500 (width by height) and draw a character
>	in it.  I essentially need the info from the FontBBox.

This may or may not be what you're looking for. It takes a string, the
name of the font you want it set in and the coordinates of a rectange in
which you want the text to be set. The string will be scaled to fit into
the rectangle, even if this means scaling it by different amounts
horizontally and vertically.


----cut here----
%!

/scaleTextToFitBox
%  input: string font-name x1 y1 x2 y2
% output:
% notes: prints str in the box (specified by the coordinates on the
%	 stack), but does appropriate scaling so that the text fits
%	 exactly.
{
	0 begin		% local dictionary
	/by2 exch def /bx2 exch def
	/by1 exch def /bx1 exch def
	/bh by2 by1 sub def % height of box
	/bw bx2 bx1 sub def % width of box
	/font exch def
	/str exch def

	gsave
		font findfont bh scalefont setfont % 1st guess at font size
		newpath
		0 0 moveto
		str true charpath flattenpath pathbbox
		/ty2 exch def /tx2 exch def
		/ty1 exch def /tx1 exch def
		/th ty2 ty1 sub def % height of text
		/tw tx2 tx1 sub def % width of text

		newpath
		bx1 tx1 sub by1 ty1 sub moveto
		bw tw div bh th div scale %scale the text to fit the box
		str show
	grestore
	end
}
dup 0 14 dict put
bind def

%
% Some code to show the above function in action
%

/box
%  input: x1 y1 x2 y2
% output:
% notes: Constructs the path of a box given by the coords on the stack
{
    0 begin	% local dictionary
	/y2 exch def /x2 exch def
	/y1 exch def /x1 exch def

	newpath
	x1 y1 moveto
	x2 y1 lineto
	x2 y2 lineto
	x1 y2 lineto
	closepath
    end
}
dup 0 4 dict put
bind def


(Hello World)		% string to be printed
/Times-Roman		% font used to print string
100 100 400 200		% coordinates of rectangle
4 copy box stroke	% this line is a debug aid (draws box around the text)
scaleTextToFitBox

showpage
----cut here----


Ciaran.
-- 
Ciaran McHale                                                             ----
Department of Computer Science, Trinity College, Dublin 2, Ireland.       \bi/
Telephone: +353-1-7021539 FAX: +353-1-772204 email: cjmchale@dsg.cs.tcd.ie \/
Technical Details
Message-ID: <1992Aug27.151713.4722@dsg.cs.tcd.ie>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!ieunet!tcdcs!maths.tcd.ie!longvalley!cjmchale
References: <9208271402.AA20109@enet-gw.pa.dec.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.postscript
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!ieunet!tcdcs!maths.tcd.ie!longvalley!cjmchale
From: cjmchale@dsg.cs.tcd.ie (Ciaran McHale)
Subject: Re: Absolutely sized fonts
Message-ID: <1992Aug27.151713.4722@dsg.cs.tcd.ie>
Organization: DSG, Dept. of Computer Science, Trinity College Dublin
References: <9208271402.AA20109@enet-gw.pa.dec.com>
Date: Thu, 27 Aug 1992 15:17:13 GMT
Lines: 94