// C O N V E R T C O M  (PREPRESS)
// catalog number ???
//
//Convert(thicken)
//	Processes the convert command
//

get "ix.dfs"

// outgoing procedures
external
	[
	Convert
	]

// outgoing statics
//external
//	[
//	]
//static
//	[
//	]

// incoming procedures
external
	[

//PREPRESS
	PrePressWindowInit
	CheckParams
	ReadIXTempFile
	WriteIXTempFile
	SetPosRelative
	GetPosRelative
	TypeChar
	IllCommand

//UTIL
	CheckAC
	Zero
	MoveBlock
	FSGetX
	FSPut

//WINDOW
	WindowReadBlock
	WindowGetPosition
	WindowSetPosition
	WindowWriteBlock
	WindowClose

//CONVERT
	SetSCVTransform
	ConvertAWidth
	ConvertAChar

//SCAN
	TypeForm
	]

// incoming statics
external
	[
	@siz			//Point size of font
	@rotation			//Rotation of font
	@incline			//Incline of font
	@resolutionx		//Resolutions
	@resolutiony
	@convertThicken
	@convertOrbitized
	]

// internal statics
//static
//	[
//	]

// File-wide structure and manifest declarations.
manifest [ ACtemp=-1;SDtemp=-2
			]
// Procedures


//CONVERT command processor.  Included here so that the file
// CONVERT.C can be used by the fonts pass without requiring all
// this stuff.

let Convert(thicken,inFile,outFile;numargs na) be [
	convertThicken=thicken
	unless CheckParams(gotsize+gotresolution) then IllCommand()

	if na ls 3 then [ inFile=SDtemp;outFile=ACtemp]
	let ws=PrePressWindowInit(inFile,false)	//SDtemp
	let wc=PrePressWindowInit(outFile)	//ACtemp

	let fn=vec IXLName
	let ixo=vec IXLMax
	ReadIXTempFile(ws,fn,ixo)
	let ix=vec IXLMax
	MoveBlock(ix,ixo,IXLMax)		//IX is the new one, ixo the old
	ix>>IX.Type=convertOrbitized?IXTypeOrbitChars,IXTypeChars
	ix>>IX.siz=siz
	ix>>IX.rotation=rotation
	ix>>IX.resolutionx=resolutionx
	ix>>IX.resolutiony=resolutiony	//Remember for matches.
	let bc=ix>>IX.bc
	let ec=ix>>IX.ec
	let nc=ec-bc+1
	let WT=FSGetX(nc*SplineWidthsize)
	WindowReadBlock(ws,WT,nc*SplineWidthsize)
	let offo=vec 1
	WindowGetPosition(ws,offo)		//AC offset
	let AC=FSGetX(nc*2)
	WindowReadBlock(ws,AC,nc*2)
	let CWT=FSGetX(nc*CharWidthsize)
	Zero(CWT, nc*CharWidthsize)	//All widths 0, even of undef chars
//Write new file.
	WriteIXTempFile(wc,fn,ix)		//Write directories.
	WindowGetPosition(wc,lv ix>>IX.sa)	//Remember base address
	WindowWriteBlock(wc,CWT,nc*CharWidthsize)
	let offn=vec 1
	WindowGetPosition(wc,offn)		//AC offset for new ones
	WindowWriteBlock(wc,AC,nc*2)

	SetSCVTransform(siz,rotation,incline,resolutionx,resolutiony)

	let bbgood=((rotation rem (60*90)) eq 0) & (incline eq 0)
	let p=vec size Convert/16		//Prepare parameters
	p>>Convert.SplineOk=false		//Characters only!
	p>>Convert.PressFontPart=false
	p>>Convert.BBGood=bbgood
	p>>Convert.Monotone=bbgood	//If no rotation, splines monotone

	let ps=WT; let pc=CWT
for c=1 to nc do
	[
	ConvertAWidth(ps,pc,p)
	ps=ps+SplineWidthsize
	pc=pc+CharWidthsize
	]

	pc=CWT
for c=0 to nc-1 do
	[
	if AC!(c*2) ne -1 then
	[				//Convert character.
	SetPosRelative(ws,offo,AC+c*2)
	GetPosRelative(wc,offn,AC+c*2)	//Save new position.
	TypeChar(c+bc)			//Signal we are converting
	let pnc=vec CharWidthsize
	let res=ConvertAChar(ws,wc,pnc,p,FSGetX,FSPut)	//Do the work.
	if res ne 0 then TypeForm("Error, ConvertAChar returns ",10,res,0)

//If bbgood, check to see if bounding box was computed correctly.
// (see comments in ConvertAWidth)
	if bbgood then
	if pc>>CharWidth.XL ne pnc>>CharWidth.XL %
	  pc>>CharWidth.YB ne pnc>>CharWidth.YB %
	  pc>>CharWidth.W ne pnc>>CharWidth.W %
	  pc>>CharWidth.H ne pnc>>CharWidth.H then
		TypeForm(" [Bounding box incorrect.]")

//Store bounding box information into WT table.
	pc>>CharWidth.XL=pnc>>CharWidth.XL
	pc>>CharWidth.YB=pnc>>CharWidth.YB
	pc>>CharWidth.W=pnc>>CharWidth.W
	pc>>CharWidth.H=pnc>>CharWidth.H

	CheckAC(pc)
	TypeForm(0)
	]
	pc=pc+CharWidthsize
	]				//Convert loop

	let tl=vec 1; WindowGetPosition(wc,tl)
	GetPosRelative(wc,lv ix>>IX.sa,lv ix>>IX.len) //Save length

	WindowSetPosition(wc,table [ 0;0 ])	//At beginning
	WriteIXTempFile(wc,fn,ix)		//ReWrite directory
	WindowWriteBlock(wc,CWT,nc*CharWidthsize) // Write WT
	WindowWriteBlock(wc,AC,nc*2)		// and AC

	WindowClose(ws)
	WindowClose(wc,tl)		//Close & truncate

	FSPut(AC); FSPut(WT); FSPut(CWT)
]