// C O O R D I N A T E   (PREPRESS)
// catalog number ???
//
//Coordinate() -- list info about coordinated fonts
//

get "Ix.dfs"
get "Streams.d"

// outgoing procedures
external
	[
	Coordinate
	]

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

// incoming procedures
external
	[
	PrePressWindowInit
	WindowClose
	WindowReadBlock
	WindowSetPosition
	ReadIXTempFile

	CheckParams
	TypeChar
	PrintIX

	MulDiv
	FLDI; FLDDP; FLD; FML; FAD; FDV; FTR; FCM

	TypeForm
	OpenFile
	Closes
	]

// incoming statics
external
	[
	@outstream
	@siz
	@resolutionx
	@params
	]

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

// File-wide structure and manifest declarations.

// Procedures

let Coordinate() be
[
	let fspl=vec 10
	let fchr=vec 10
	TypeForm("Spline file name: ",1,fspl,"Character file name: ",1,fchr,0)
	let sspl=PrePressWindowInit(fspl,false)
	let schr=PrePressWindowInit(fchr,false)
	outstream=OpenFile("PrePress.Lst", ksTypeWriteOnly, 1)
	TypeForm("Coordinate ",fspl," with ",fchr,0)

	let splx=vec IXLMax
	let chrx=vec IXLMax
	let spln=vec IXLName
	let chrn=vec IXLName

	ReadIXTempFile(sspl, spln, splx)
	ReadIXTempFile(schr, chrn, chrx)
	TypeHeader("Splines: ", spln, splx)
	TypeHeader("Characters: ", chrn, chrx)
// Now modify the actual parameters given switches in command
	if (params&gotsize) ne 0 then chrx>>IX.siz=siz
	if (params&gotresolution) ne 0 then chrx>>IX.resolutionx=resolutionx

	WindowSetPosition(sspl, lv splx>>IX.sa)
	WindowSetPosition(schr, lv chrx>>IX.sa)

	let bc=splx>>IX.bc
	let obc=chrx>>IX.bc
	let v=vec 100

	while bc gr obc do
		[
		WindowReadBlock(schr, v, CharWidthsize)
		obc=obc+1
		]
	while obc gr bc do
		[
		WindowReadBlock(sspl, v, SplineWidthsize)
		bc=bc+1
		]

	let maxtop=-1000
	let minbottom=1000
	while bc le splx>>IX.ec & bc le chrx>>IX.ec do
		[
		let chr=vec CharWidthsize
		WindowReadBlock(schr, chr, CharWidthsize)
		let spl=vec SplineWidthsize
		WindowReadBlock(sspl, spl, SplineWidthsize)

		bc=bc+1
		if (spl!0 eq 0 & spl!1 eq -1) %
			chr>>CharWidth.H eq HNonExCode then loop

//Now start printing:
		TypeChar(bc-1)
		let left=chr>>CharWidth.XL
		let right=left+chr>>CharWidth.W
		let bottom=chr>>CharWidth.YB
		let top=bottom+chr>>CharWidth.H
		if top gr maxtop then maxtop=top
		if bottom ls minbottom then minbottom=bottom
		TypeForm("  Width is ",3,lv chr>>CharWidth.WX)
		FLD(1, lv spl>>SplineWidth.WX)
		ConvRes(1, chrx)
		TypeForm(" s/b " ,2 ,1)
		FLDDP(2, lv chr>>CharWidth.WX)
		if FCM(2, 1) gr 0 then
			TypeForm("[width]")
		TypeForm("*N     ")
		TypeDim("L: ",left, lv spl>>SplineWidth.XL, chrx)
		TypeDim("R: ",right, lv spl>>SplineWidth.XR, chrx)
		TypeDim("B: ",bottom, lv spl>>SplineWidth.YB, chrx)
		TypeDim("T: ",top, lv spl>>SplineWidth.YT, chrx)
		TypeForm(0)
		]

// Now print summary stuff at end:
	TypeForm("*N*N*NSummary:*N")
	TypeForm("Maximum number of bits below baseline: ",10,-minbottom)
	TypeForm("*NMaximum number of bits above baseline: ",10,maxtop)
	let h=maxtop-minbottom
	TypeForm("*NFont size is ",10,chrx>>IX.siz," micas, or ")
	TypeForm(10, MulDiv(chrx>>IX.siz,18,635), " points.")
	FLDI(1, 1)
	ConvRes(1, chrx)
	FLDI(2, 1); FLDI(3, 2); FDV(2, 3); FAD(1, 2)
	let sbh=FTR(1)
	if sbh ls h then TypeForm("*NCharacters are too high for this point size -- total allowed is ",10,sbh)

	TypeForm(0)

	Closes(outstream)
	WindowClose(sspl)
	WindowClose(schr)
	]

and TypeDim(mes, ival, fpval, cv) be
[
	TypeForm(mes, 10, ival, $|)
	FLD(1, fpval)
	ConvRes(1, cv)
	TypeForm(2, 1, "  ")
]

// Convert fractions of char size to resolution units
and ConvRes(ac, ix) be
[
	FLDI(31, ix>>IX.siz)
	FML(1, 31)
	FLDI(31, ix>>IX.resolutionx)
	FML(ac, 31)
	FLDI(31, 25400)
	FDV(ac, 31)		// 1 = in res. units
]

and TypeHeader(herald, nam, ix) be
[
	TypeForm(herald, lv nam>>IXN.Name, "  ")
	PrintIX(ix)
]