Article View: adobe.postscript.programming
Article #1463Re: Please Help Regarding Scaling
From: Eric Rose
Date: Thu, 30 May 2002 17:13
Date: Thu, 30 May 2002 17:13
31 lines
8594 bytes
8594 bytes
Thank for your help Aandi. I dug into ps code more than I wanted to. Here's the result:
%!PS-Adobe-3.0
% ----------------------------------------------------------------------------- % Version: 1.0 % Author : Eric Rose (erose@cskauto.com) % History: 05/30/02: inception % Purpose: To print name badges on Avery labels, scaled to fit and centered % Theory : % The spec called for the labels to be scaled to fit horizonally or % vertically. To accomplish this, I first obtain a charpath of the string % with the scale set to 100. 100 was chosen because it gave me a high degree % of accuracy of the charpath including the padding before the first % character of the string (see glyphs). % % Once the lower left and upper right x/y cordinates are obtained, we can use % these data to determine if the vertical or horizonal components of the text % will hit the bounding box parameters first. Ratio of width to height is used % to determine the formula for calculating the scale, compared to the bounding % box. % % After the scale is determined, positioing around a center point horizonally % and vertically of the Avery label was trivial. % % Usage: % modify parameters for x/ySpacing, boxHeight/Width, cpx/y depending on the % Avery label your using. Put a string on the stack and call doJob. % % -----------------------------------------------------------------------------
% variables -------------------------------------------------------------------
/ySpacing 63 def % horiz movement to next cp of avery label (static) /xSpacing 151 def % vert movement to next cp of avery label (static) /boxWidth 135 def % avery label width: 1 7/8" (static) /boxHeight 54 def % avery label height: 3/4" (static) /cpx 306 def % center point of avery label, x (can move for multi) /cpy 396 def % center point of avery label, y /llx 0 def % llx from charpath (populateCharPathBounds) /lly 0 def % lly from charpath /urx 0 def % urx from charpath /ury 0 def % ury from charpath /cpWidth 0 def % charpath width (populateCharPathBounds) /cpHeight 0 def % charpath height /myScale 0 def % calculated scale (calculateScale) /ox 0 def % origin x (movement left to center) (calculateOrigin) /oy 0 def % origin y (movement down to center)
% functions -------------------------------------------------------------------
% ----------------------------------------------------------------------------- % For debug only, draws the bounding box around around cpx and cpy % Stack in : nothing % Stack out: nothing % Modifies : nothing % ----------------------------------------------------------------------------- /drawBox {% boxWidth 2 div cpx exch sub truncate cvi % boxHeight 2 div cpy exch sub truncate cvi boxWidth 2 div cpx exch sub boxHeight 2 div cpy exch sub moveto 0 boxHeight rlineto % up boxWidth 0 rlineto % right 0 boxHeight -1 mul rlineto % down boxWidth -1 mul 0 rlineto % left stroke } def
% ----------------------------------------------------------------------------- % Populates charpath variables % This is called twice, once a scale of 100 and once when scale is recalculated % Stack in : string to center % Stack out: same string % Modifies : llx, lly, urx, ury, cpWidth, cpHeight % ----------------------------------------------------------------------------- /populateCharPathBounds {dup % preserve string gsave % save graphics state newpath % start a new path 0 0 moveto % reset origin because of newpath true charpath % obtain path of glyph outlines flattenpath % for better dimensions of box pathbbox % bounding box of string grestore % restore graphics /ury exch def % save ury (reals) /urx exch def % save urx /lly exch def % save lly /llx exch def % save llx urx llx sub /cpWidth exch def % calc and save cpWidth ury lly sub /cpHeight exch def % calc and save cpHeight } def
% ----------------------------------------------------------------------------- % Calculates scale to fit test within the box defined by boxWidth and boxHeight % Previous calls: populateCharPathBounds with scale set to 100 % Stack in : nothing % Stack out: nothing % Modifies : myScale % ----------------------------------------------------------------------------- /calculateScale {cpWidth cpHeight div % charpath box ratio boxWidth boxHeight div % bounding box ratio ge % cpRatio >= boxRatio ? {boxWidth cpWidth div % yes, scale based on width } {boxHeight cpHeight div % no, scale based on height } ifelse 100 mul % apply to 100 (our begining scale) /myScale exch def % save it } def
% ----------------------------------------------------------------------------- % Calculates movement based on charpath results to center around cpx and cpy % Previous calls: populateCharPathBounds, calculateScale % left movement: (((urx+llx)/2)+llx) % down movement: ((ury+lly)/2) % Stack in : nothing % Stack out: nothing % Modifies : ox and oy % ----------------------------------------------------------------------------- /calculateOrigin {urx llx sub 2 div % center point of charpath llx add % account for char pading cpx exch sub % apply to center point /ox exch def % save it (left movement) ury lly add % if lly below line, will be neg 2 div % mid point of movement cpy exch sub % apply to center point /oy exch def % save it (down movement) } def
% ----------------------------------------------------------------------------- % Main subroutine, in case we have multiple lables % Stack in : string to center % Stack out: nothing (consumes incomming string) % Modifies : calculated variables % ----------------------------------------------------------------------------- /doJob {drawBox % debug only !!! take out on final /Helvetica findfont 100 scalefont setfont % start with 100 pt font populateCharPathBounds % populate ll[xy], ur[xy] at 100 scale calculateScale % calculate scale from width or height /Helvetica findfont myScale scalefont setfont % apply it populateCharPathBounds % for center, the ll[xy], ur[xy] with new scale calculateOrigin % find new origin ox oy moveto show % move there, show text (stack clear now) } def
% main ------------------------------------------------------------------------
% for testing purposes only, to determine behavior
/cpy 740 def /cpx 100 def (Bugs Bunny) doJob /cpx cpx 140 add def (Bugs) doJob /cpx cpx 140 add def (Daffy Duck) doJob /cpx cpx 140 add def (Daffy) doJob
/cpy cpy 60 sub def /cpx 100 def (Foghorn Leghorn)doJob /cpx cpx 140 add def (Foghorn) doJob /cpx cpx 140 add def (Leghorn) doJob /cpx cpx 140 add def (Sylvester) doJob
/cpy cpy 60 sub def /cpx 100 def (Tweety) doJob % vertical scale /cpx cpx 140 add def (Roadrunner) doJob /cpx cpx 140 add def (Tweetie) doJob /cpx
Technical Details
Message-ID:
<ef88c70.1@WebX.aaQKaf7vad4>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!news-out.visi.com!hermes.visi.com!newsfeed.media.kyoto-u.ac.jp!newsfeed01.sul.t-online.de!t-online.de!WebXReferences:
<ef88c70.-1@WebX.aaQKaf7vad4>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10c61a,830b42484cb2121b X-Google-Attributes: gid10c61a,public X-Google-ArrivalTime: 2002-05-30 10:38:43 PST From: "Eric Rose" <erose@cskauto.com> Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!news-out.visi.com!hermes.visi.com!newsfeed.media.kyoto-u.ac.jp!newsfeed01.sul.t-online.de!t-online.de!WebX Newsgroups: adobe.postscript.programming Subject: Re: Please Help Regarding Scaling Message-ID: <ef88c70.1@WebX.aaQKaf7vad4> Date: Thu, 30 May 2002 10:13:29 -0700 References: <ef88c70.-1@WebX.aaQKaf7vad4> Lines: 209 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit