// M A P C D T E M P  (PREPRESS)
// catalog number ???
//
// Used to do all CDtemp re-formatting (Grow, Rotate, Orbit, Sample)
// MapCDtemp(ixfn, charfn, arg)
//   Applies ixfn(ix, arg) to ix entry in CDtemp
//   Applies charfn(p, si, so, arg) to each character in CDtemp
//			p => CharWidth entry
//			si = input stream, positioned at character
//			so = output stream, positioned to receive character


get "ix.dfs"

// outgoing procedures
external
	[
	MapCDtemp
	]

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

// incoming procedures
external
	[
//WINDOW
	WindowRead
	WindowReadBlock
	WindowWrite
	WindowWriteBlock
	WindowGetPosition
	WindowSetPosition
	WindowCopy
	WindowClose

//PREPRESS
	ReadIXTempFile
	WriteIXTempFile
	SetPosRelative
	GetPosRelative
	PrePressWindowInit

//UTIL
	CheckCD
	TypeChar
	TypeForm
	FSGetX
	FSPut
	Zero
	]

// incoming statics
//external
//	[
//	]

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

// File-wide structure and manifest declarations.


// Procedures

let MapCDtemp(ixfn, charfn, arg) be
[
	let DPzero=table [ 0;0 ]
	let si=PrePressWindowInit(-1)		//Read CDtemp
	let so=PrePressWindowInit(0)		//Write scratch.
	let fn=vec IXLName
	let ix=vec IXLMax
	ReadIXTempFile(si, fn, ix)
	ixfn(ix, arg)
	WriteIXTempFile(so, fn, ix)

	let bc=ix>>IX.bc
	let ec=ix>>IX.ec
	let nc=ec-bc+1
	let CD=FSGetX(nc*2)
	let WD=FSGetX(nc*CharWidthsize)
	WindowReadBlock(si, WD, nc*CharWidthsize)
	WindowWriteBlock(so, WD, nc*CharWidthsize)
	let off=vec 2
	WindowGetPosition(si,off)
	WindowReadBlock(si, CD, nc*2)
	WindowWriteBlock(so, CD, nc*2)

	for c=0 to nc-1 do
		[
		let cdp=CD+c*2
		if cdp!0 ne -1 then
			[
			SetPosRelative(si, off, cdp)	//Get to char coding.
			GetPosRelative(so, off, cdp)
			let curpos=vec 2
			WindowGetPosition(so, curpos)
			let cp=c*CharWidthsize+WD	//Pointer to CharWidth entry
			TypeChar(c+bc)
			charfn(cp, si, so, arg)
			CheckCD(cp)
			if cp>>CharWidth.H eq HNonExCode then
				[
				cdp!0=-1; cdp!1=-1
				Zero(cp, CharWidthsize)		//Sanitize missing char
				cp>>CharWidth.H=HNonExCode
				]
			TypeForm(0)
			]
		]

	let tl=vec 1
	WindowGetPosition(so, tl)	//Total length
	GetPosRelative(so, lv ix>>IX.sa, lv ix>>IX.len) //get length
	WindowSetPosition(so, DPzero)
	WriteIXTempFile(so, fn, ix)
	WindowWriteBlock(so, WD, nc*CharWidthsize)
	WindowWriteBlock(so, CD, nc*2)
	FSPut(CD); FSPut(WD)
	WindowSetPosition(so, DPzero)
	WindowSetPosition(si, DPzero)
	WindowCopy(so, si, tl)		//Copy scratch file to CDtemp file.
	WindowClose(si, -1)
	WindowClose(so)
]