// pressDirs.sr
// Bcpl/f pressDirs.sr

// last modified
// RML March 27, 1980  5:20 PM underlined offset fiddle
// RML April 3, 1978  10:56 AM single page color Press file
// RML November 22, 1977  5:12 PM - bug fix, add lv to emitPressOp!
// RML November 21, 1977  4:06 PM - pressSetOp
// RML September 28, 1977  3:31 PM - pressHighlight
// RML September 23, 1977  6:12 PM - color additives

// Last modified November 3, 1979  1:07 PM by Taft

get "BRAVO1.DF"
get "Q.DF"
get "ALTOFILESYS.D"
get "PRESS.DF"
get "PCOLOR.DF"

// Incoming Procedures

external	[
	DlPosition
	doublesub
	EvenByte
	move
	movec
	MyFrame
	PressInitTranslate
	PutChar
	PutWds
	QLength
	LengthQ
	RoundRatio
	];


// Incoming Statics
external	[
	El
	ElTrailer
	mpcuncatt
	vpep
	vPress
	vsgh
	]

// Outgoing Procedures

external	[
	emitPressOp
	pressHighlight
	pressSetColor
	pressSetOp
	pressShowChars
	pressShowRectangle
	pressUnderline
	pressXpdBorder
	updateEtFrame
	];



//**********    For Pressery, see    **************//
// Sproull, Bob,and Newman, William; January 26, 1977
// "Press File Format", on <GR-DOCS>Press.Ears
//******************************************//

// U P D A T E E T F R A M E
//
let updateEtFrame(xLeft, xRight, yBase) be
[
if xLeft < ElTrailer>>ELTRAILER.left  then
	ElTrailer>>ELTRAILER.left = xLeft

let t = (yBase-RoundRatio(vsgh>>SG.blmax, pttomicamlt, pttomicadiv))
if t < ElTrailer>>ELTRAILER.bottom  then
	ElTrailer>>ELTRAILER.bottom = t

t = xRight-xLeft 
if t > ElTrailer>>ELTRAILER.width  then
	ElTrailer>>ELTRAILER.width = t

t = (yBase+RoundRatio(vsgh>>SG.topmax, pttomicamlt, pttomicadiv))
if t > ElTrailer>>ELTRAILER.height  then
	ElTrailer>>ELTRAILER.height = t
]


// S E T O P
//
and pressSetOp(index, var) be
[	// index is the offset of an item in the vPress array
	// var is the value to set it to.
let ptr = lv vPress ! index
unless var eq ptr >> vPRESS.value do
	[
	ptr >> vPRESS.value  = var
	ptr >> vPRESS.State  = true
	]
]

// E M I T P R E S S O P
//
and emitPressOp(index) be
[
let ptr = lv vPress ! index
if ptr >> vPRESS.State then
	[
	let val = ptr >> vPRESS.value
	switchon index into
		[
	case vPressX: pressSetX(val);  endcase
	case vPressY: pressSetY(val); endcase
	case vPressFont: pressSetFont(val); endcase
	case vPressSpace: pressSetSpaceX(val); endcase
	case vPressHue: pressSetHue(val); endcase
	case vPressValue : pressSetBrightness(val); endcase
	case vPressChroma : pressSetSaturation(val); endcase
		]
	]
ptr >> vPRESS.State = false
]


// S E T X	
//
and pressSetX(x) = valof
[
PutChar(pressSetXm,El)
PutChar(x<<lh,El)
PutChar(x,El)
resultis x
]

// S E T Y
//
and pressSetY(y) = valof
[
PutChar(pressSetYm,El)
PutChar(y<<lh,El)
PutChar(y,El)
resultis y
]

// S H O W C H A R A C T E R S
//
and pressShowChars(n) be
[
if n le 0 then return

emitPressOp(vPressX)
(lv vPress ! vPressX) >> vPRESS.value = -1	// x is advanced by Show Char

emitPressOp(vPressY)
emitPressOp(vPressFont)
emitPressOp(vPressSpace)
emitPressOp(vPressHue)
emitPressOp(vPressValue)
emitPressOp(vPressChroma)

test n le 32 ifso
	[	// generate short form
	PutChar(pressShowCharsShortm+(n-1),El)
	]

ifnot	[
	PutChar(pressShowCharsm,El)
	PutChar(n,El)
	]
]

// S E T F O N T
//
and pressSetFont(font) be
[
PutChar(pressSetFontm+(font&17b),El)
]

// S E T C O L O R
//
and pressSetColor(colorx) be
[
let ptr = lv mpcuncatt! (colorx lshift 1)
pressSetOp(vPressHue, ptr>>vColor.hue)
pressSetOp(vPressValue, ptr>>vColor.value)
pressSetOp(vPressChroma, ptr>>vColor.chroma)
]

// S E T B R I G H T N E S S 
//
and pressSetBrightness(value) be
[
PutChar(pressSetBrightnessm,El)
PutChar(value,El)
]

// S E T H U E 
//
and pressSetHue(hugh) be
[
PutChar(pressSetHuem,El)
PutChar(hugh,El)
]

// S E T S A T U R A T I O N 
//
and pressSetSaturation(value) be
[
PutChar(pressSetSaturationm,El)
PutChar(value,El)
]

// S E T S P A C E X
//
and pressSetSpaceX(spc) be
[
PutChar(pressSetSpaceXm,El)
PutChar(spc<<lh,El)
PutChar(spc,El)
]

// U N D E R L I N E
//
and pressUnderline(xStart,xCurrent,yCurrent, dummy) be
[	// dummy is here to make underline and highlight look alike.
if xStart eq xCurrent then return
pressSetOp(vPressX, xStart)	// Somehow, should depend on font - see updateEtFrame

//	in case offset is pending
let oldY = pressRetrieveValue(vPressY)

pressSetOp(vPressY, yCurrent-pressUlDym)

pressShowRectangle(xCurrent-xStart,pressUlHeightm)
pressSetOp(vPressY, oldY eq 0 ? yCurrent, oldY)
	// if underlining, highlighting is done before show chars,
//pressSetOp(vPressX, xCurrent)
]

// S H O W R E C T A N G L E
//
and pressShowRectangle(width,height) be
[
emitPressOp(vPressX)
emitPressOp(vPressY)
emitPressOp(vPressHue)
emitPressOp(vPressValue)
emitPressOp(vPressChroma)

PutChar(pressShowRectanglem,El)

PutChar(width<<lh,El)
PutChar(width,El)

PutChar(height<<lh,El)
PutChar(height,El)
]

// X P D B O R D E R
//
and pressXpdBorder(x,y) be
[
pressSetOp(vPressX, x+150)		// side 1
pressSetOp(vPressY, y+150)
pressShowRectangle(2980,40)

pressSetOp(vPressX, x+150)		// side 2
pressSetOp(vPressY, y+150)
pressShowRectangle(40,1470)

pressSetOp(vPressX, x+150)		// side 3
pressSetOp(vPressY, y+1580)
pressShowRectangle(2980,40)

pressSetOp(vPressX, x+3090)		// side 4
pressSetOp(vPressY, y+150)
pressShowRectangle(40,1470)

pressSetOp(vPressX, x)			// side 5
pressSetOp(vPressY, y)
pressShowRectangle(3280,40)

pressSetOp(vPressX, x)			// side 6
pressSetOp(vPressY, y)
pressShowRectangle(40,1770)

pressSetOp(vPressX, x)			// side 7
pressSetOp(vPressY, y+1730)
pressShowRectangle(3280,40)

pressSetOp(vPressX, x+3240)			// side 8
pressSetOp(vPressY, y)
pressShowRectangle(40,1770)
]

// P R E S S H I G H L I G H T
//
and pressHighlight(xStart, xCurrent, yStart, lastColor) be
[
if xStart eq xCurrent then return
let t = vsgh>>SG.blmax
pressSetColor(lastColor+cWhitex+1)
pressSetOp(vPressX, xStart)	// may not be needed

//	in case offset is pending
let oldY = pressRetrieveValue(vPressY)

pressSetOp(vPressY, yStart-RoundRatio(t, pttomicamlt, pttomicadiv))
pressShowRectangle(xCurrent-xStart, RoundRatio((vsgh>>SG.ldTop gr 0 ? 1,0)+vsgh>>SG.topmax+t , pttomicamlt, pttomicadiv))
pressSetOp(vPressY, oldY eq 0 ? yStart, oldY)
	// if underlining, highlighting is done before show chars,
pressSetColor(lastColor)
]

// P R E S S R E T R I E V E V A L U E
//
and pressRetrieveValue(index) = valof
[	// 0 if State = 0, else
	// value
let ptr = lv vPress!index
resultis ptr>>vPRESS.State ? ptr>>vPRESS.value, 0
]

// The end of Press Directives