/*
 * $RCSfile: ImportWraps.psw,v $
 *
 * (c) Copyright 1992-1994 Adobe Systems Incorporated.
 * All rights reserved.
 * 
 * Permission to use, copy, modify, distribute, and sublicense this software
 * and its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notices appear in all copies and that
 * both those copyright notices and this permission notice appear in
 * supporting documentation and that the name of Adobe Systems Incorporated
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  No trademark license
 * to use the Adobe trademarks is hereby granted.  If the Adobe trademark
 * "Display PostScript"(tm) is used to describe this software, its
 * functionality or for any other purpose, such use shall be limited to a
 * statement that this software works in conjunction with the Display
 * PostScript system.  Proper trademark attribution to reflect Adobe's
 * ownership of the trademark shall be given whenever any such reference to
 * the Display PostScript system is made.
 * 
 * ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
 * ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 * ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON- INFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO EVENT SHALL ADOBE BE LIABLE
 * TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  ADOBE WILL NOT
 * PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
 * 
 * Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
 * Incorporated which may be registered in certain jurisdictions
 * 
 * Author:  Adobe Systems Incorporated
 */

/***************************************************************
**
** FUNCTION:    PSWDrawControlPoints
**
** DESCRIPTION: Draws control points from the current font with xyshow
**
** PARAMETERS:  X, Y		initial position
**		Pts		list of displacements
**		NumPts		size of Pts array
**		String		characters to show
**
** RETURN:      None
**
***************************************************************/

defineps PSWDrawControlPoints (float X, Y; float  Pts[NumPts];
			       int NumPts; char String[NumChars]; int NumChars)
	X Y moveto (String) Pts xyshow
endps

/***************************************************************
**
** FUNCTION:    PSWSetRectViewClip
**
** DESCRIPTION: Sets the view clip to the rectangles
**
** PARAMETERS:  Rect		List of rectangles
**		NumRect		Number of points in Rect
**
** RETURN:      None
**
***************************************************************/

defineps PSWSetRectViewClip(float numstring Rect[NumRect]; int NumRect)
    Rect rectviewclip
endps

/***************************************************************
**
** FUNCTION:    PSWClearWindow
**
** DESCRIPTION: Fills window with white
**
** PARAMETERS:  Width, Height	window dimensions
**
** RETURN:      None
**
***************************************************************/

defineps PSWClearWindow(float Width, Height)
    currentgray 1 setgray 0 0 Width Height rectfill setgray
endps

/***************************************************************
**
** FUNCTION:    PSWDefineFont
**
** DESCRIPTION: Define the control point font 
**
** PARAMETERS:  fontname    the name of the font
**
** RETURN:      None
**
***************************************************************/

defineps PSWDefineFont(char *fontname)
  9 dict dup begin
    /A  { 			% x y  A  x' y'
	transform
	floor 0.5 add exch
	floor 0.5 add exch
	itransform
    } bind def
    /FontName /fontname def
    /FontType 3 def
    /FontMatrix [.001 0 0 .001 0 0] def
    /FontBBox [-500 -500 500 500] def 

    /Encoding 256 array def
        0 1 255 {Encoding exch /.notdef put} for
    
    Encoding
        (a) 0 get /Rectfill put
    
    /CharProcs 2 dict def
    CharProcs begin
        /.notdef { } def
        /Rectfill
        {
            0 0 A moveto -500 -500 rmoveto 0 1000 rlineto
            1000 0 rlineto 0 -1000 rlineto closepath
            fill
        } def
    end
    
    /BuildChar
    {
        1000 0 -500 -500 500 500 setcachedevice
        exch begin
            true setstrokeadjust
            Encoding exch get
            CharProcs exch get
            exec
        end
    } def
  end
  
  /fontname exch definefont pop
endps

/***************************************************************
**
** FUNCTION:    PSWGetTransform
**
** DESCRIPTION: Gets the current transform matrices and offset
**              coordinates.
**
** PARAMETERS:  None
**
** RETURN:      Ctm         array containing current transform matrix
**              Invctm      array containing inverted matrix
**              XOffset     X offset value
**              YOffset     Y offset value
**
***************************************************************/

defineps PSWGetTransform (| float ctm[6], invctm[6]; 
			  int *xOffset, *yOffset)
	matrix currentmatrix dup ctm
	matrix invertmatrix invctm
	currentXoffset yOffset xOffset
endps

/***************************************************************
**
** FUNCTION:	PSWSetMatrixAndGetTransform
**
** DESCRIPTION:	Sets the current matrix to have the specified
**		translation and halftone phase, and returns
**		the matrices
**
** PARAMETERS:	tx, ty		translation
**		hx, hy		halftonephase
**
** RETURN:	Ctm		array of current transform matrix
**		Invctm		array of inverted matrix
**		XOffset		X offset value
**		YOffset		Y offset value
**
***************************************************************/
defineps PSWSetMatrixAndGetTransform (float tx, ty; int hx, hy
		| float Ctm[6], Invctm[6]; int *XOffset, *YOffset)
        initmatrix
	tx ty translate
	hx hy sethalftonephase
	matrix currentmatrix dup Ctm
	matrix invertmatrix Invctm
	currentXoffset YOffset XOffset
endps

/***************************************************************
**
** FUNCTION:    PSWDeskTop
**
** DESCRIPTION: Draw gray background, page, and drop shadow
**
** PARAMETERS:  X, Y, W, H	Dimensions of page
**
** RETURN:      None
**
***************************************************************/

defineps PSWDesktop (float X, Y, W, H)
	%
	% draw a background 80% gray "desktop" behind the page
	%
	gsave
		/desk 1024 16 mul def
		/edge 10 def
		% draw the outside, huge, desktop rectangle 
		0.8 setgray
		desk neg desk neg desk 2 mul desk 2 mul rectfill
		% draw a hole for the display page at X, Y 
	        1 setgray
		X Y W H rectfill
		% draw a black border around the display page edge 
		4 setlinewidth
		0 setgray
		X Y W H rectstroke
		% add a black drop shadow under the display page
		newpath
		edge edge neg translate
		X Y moveto
		W 0 rlineto
		0 H rlineto
		edge neg 0 rlineto
		0 H neg edge add rlineto
		W neg edge add 0 rlineto
		closepath
		0 setgray fill
	grestore
endps

/***************************************************************
**
** FUNCTION:	PSWSetMaskTransfer
**
** DESCRIPTION:	Sets the transfer function so that
**		any color comes out as black
**
** PARAMETERS:	ctxt	Context to use
**
** RETURN:	None
**
***************************************************************/
defineps PSWSetMaskTransfer(DPSContext ctxt)
        {pop 0} settransfer
endps

/***************************************************************
**
** FUNCTION:	PSWDefineExecFunction
**
** DESCRIPTION:	Define a function that, when executed, will make
**		the interpreter read and execute the following
**		data
**
** PARAMETERS:	ctxt	Context to use
**
** RETURN:	None
**
***************************************************************/
defineps PSWDefineExecFunction(DPSContext ctxt)
	/ExecPS {
		/execSuccess false def
		(%stdin) (r) file
		cvx stopped
		pop	% always true
		% Flush until you get the magic line
		{
			{currentfile 256 string readline} stopped
			pop % don't care stopped result
			pop % don't care readline bool result
(Magic end of data line \)\)\)\)\)\)\)\)\)\) 99#2 2#99 <xyz> // 7gsad,32h4ghNmndFgj2)
			eq
			{exit} if
		} loop
	} def
endps

/***************************************************************
**
** FUNCTION:	PSWBeginEPSF
**
** DESCRIPTION:	Prepares for EPSF execution by doing a save, counting
**		stacks, and setting graphics state to default
**
** PARAMETERS:	ctxt	Context to use
**
** RETURN:	None
**
***************************************************************/
defineps PSWBeginEPSF(DPSContext ctxt)
	/EPSFsave save def
	count /OpStackSize exch def
	/DictStackSize countdictstack def
	/showpage {} def
	userdict begin
	0 setgray 0 setlinecap
	1 setlinewidth 0 setlinejoin
	10 setmiterlimit [] 0 setdash newpath
	/languagelevel where {
	    pop languagelevel
	    1 ne {
	        false setstrokeadjust false setoverprint
	    } if
	} if
	/ExecPS load
endps

/***************************************************************
**
** FUNCTION:	PSWEndEPSF
**
** DESCRIPTION:	Restores after EPSF execution and returns execution
**		status
**
** PARAMETERS:	ctxt	Context to use
**
** RETURN:	err	Execution status (true == execution error)
**
***************************************************************/
defineps PSWEndEPSF(DPSContext ctxt | boolean *err)
	execSuccess not err
	count OpStackSize sub 
	    dup 0 lt {neg {pop} repeat} {pop} ifelse
	countdictstack DictStackSize sub
	    dup 0 lt {neg {end} repeat} {pop} ifelse
endps

/***************************************************************
**
** FUNCTION:	PSWCheckFontResident
**
** DESCRIPTION:	Checks if a font is resident, either in SharedFontDirectory,
**		FontDirectory, or (%font%)
**
** PARAMETERS:	ctxt		Context to use
**		name		Font name
**
** RETURN:	resident	Whether font is resident
**
***************************************************************/
defineps PSWCheckFontResident(DPSContext ctxt; char *name; | boolean *resident)
    SharedFontDirectory /name known {
	true resident
    } {
	FontDirectory /name known {
	    true resident
	} {
	    /buf (name) length 6 add string def
	    buf 0 (%font%) putinterval
	    buf 6 (name) putinterval
	    false buf {pop pop true} buf filenameforall resident
	} ifelse
    } ifelse
endps

/***************************************************************
**
** FUNCTION:	PSWTransformBeforeEPSF
**
** DESCRIPTION:	Transform the coordinate system before EPSF execution
**
** PARAMETERS:	ctxt		Context to use
**		tx, ty		Translation
**		sx, sy		Scale
**		r		Rotation
**		ox, oy		Original x and y	
**
** RETURN:	None
**
***************************************************************/
defineps PSWTransformBeforeEPSF(DPSContext ctxt; float tx, ty, sx, sy, r,
				ox, oy)
    initgraphics
    tx ty translate
    r rotate
    sx sy scale
    ox oy translate
endps

/***************************************************************
**
** FUNCTION:	PSWDrawBox
**
** DESCRIPTION:	Draw a gray box with an X through it
**
** PARAMETERS:	llx, lly, urx, ury	Box corners
**		tx, ty			Translation
**		sx, sy			Scale
**		r			Rotation
**
** RETURN:	None
**
***************************************************************/
defineps PSWDrawBox(float llx, lly, urx, ury, tx, ty, sx, sy, r)
    gsave
    tx ty translate
    r rotate
    sx sy scale
    llx neg lly neg translate
    .4 setgray
    llx lly urx llx sub ury lly sub 4 copy 4 copy rectclip rectfill
    8 setlinewidth
    .8 setgray
    rectstroke
    newpath
    llx lly moveto
    urx ury lineto
    llx ury moveto
    urx lly lineto
    4 setlinewidth
    stroke
    grestore
endps
