//Written by Lyle Ramshaw September 8, 1980  4:08 PM:
// Text is treated differently in version 5.0 than in earlier versions;
//  the procedure adjustText in this module will change the "left"
//  and "top" numbers from the old format to the new format.  Doing
//  a ↑P after the change will put all characters at the same places
//  that they formerly were, except that arrowheads will have shifted
//  by some small amount (less than an Alto dot).  
 
// outgoing procedures:
external [ adjustText ]

// incoming procedures (from zpArrows, zpText respectively):
external [ ArrowsMicaAdjust;  fontAddress ]

// incoming statics:
external [ @fontDefTable;  @fontFile ]

//local definitions (.AL font format):
structure WX [
	wx	bit 15
	noExt	bit 1
	]

structure WXplus1 [
	skip	byte
	bits	byte
	]


get "zpDefs.bcpl"
get "zpPressDf.bcpl"


let adjustText(textPointer) be [adjustText
let str=textPointer+TEXTblockSize
let left=textPointer>>TEXT.left
let top=textPointer>>TEXT.top
let font=textPointer>>TEXT.font
let len=str>>STRING.length
let arrowsFlag=false
if fontDefTable!font>>FONTDEF.isArrows eq 1 then arrowsFlag=true
if (len ne 1) then
	[   //multicharacter string
	return
	]
test arrowsFlag
  ifnot
	[   //single-character string, but NOT an arrowhead
	//first, we compute the height by the old, exotic rules
	let fontPointer=fontAddress(font)
	let oldH=nil
	oldTextSize(str,fontPointer,lv oldH)
	let oldBottom=top-oldH+1
	textPointer>>TEXT.top=oldBottom+fontFile>>FONTFILE.baseline↑font
	]
  ifso
	[  //an arrowhead
	let fontPointer=fontAddress(font)
	let oldH=nil
	oldTextSize(str,fontPointer,lv oldH)
	let oldBottom=top-oldH+1
	let oldYMicas=scaleFactor*(oldBottom)
	let oldXMicas=scaleFactor*(left)
	let newYMicas=scaleFactor*(top-fontFile>>FONTFILE.baseline↑font)
	let newXMicas=scaleFactor*(left)
	ArrowsMicaAdjust(str>>STRING.char↑1,lv newXMicas,lv newYMicas)
	//now, we take the difference between new and old, round to
	//the nearest Alto dot, and adjust top and left by those amounts
	manifest [ BigNum=100; Magic=BigNum*scaleFactor+(scaleFactor/2) ]
	let xDel=(newXMicas-oldXMicas+Magic)/scaleFactor - BigNum
	let yDel=(newYMicas-oldYMicas+Magic)/scaleFactor - BigNum
	textPointer>>TEXT.left=left-xDel
	textPointer>>TEXT.top=top-yDel
	]
]adjustText

//this is what the old Draw's used to do for all one-character strings
and oldTextSize(textPtr, fontPtr, heightAd) be
[
let hb,ht=0, @(fontPtr-2)
let c=textPtr>>STRING.char↑1
	[ let wxpt=fontPtr+c+fontPtr!c
	  let newht=(wxpt+1)>>WXplus1.skip
	  let newhb=newht+(wxpt+1)>>WXplus1.bits
	  if newhb gr hb then hb=newhb
	  if newht ls ht then ht=newht
	  c=wxpt>>WX.wx
	  if wxpt>>WX.noExt then break
	] repeat
@heightAd=hb-ht
]