
	load 'macros.dump'
	include 'driver.equ'
	include 'pl.equ'

;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	D_GetWdth
	IMPORT	P_ContentRect
	IMPORT	P_CurrentWin
	IMPORT	P_Fix2Int
	IMPORT	P_H
	IMPORT	P_Page
	IMPORT	P_V
	IMPORT	D_SelectFont
	IMPORT	T_CaretDrawn
	IMPORT	T_CountSpaces
	IMPORT	T_CurFont
	IMPORT	T_CurIndent
	IMPORT	T_CurRulBits
	IMPORT	T_CurSize
	IMPORT	T_CurStyle
	IMPORT	T_DotLine
	IMPORT	T_DotObj
	IMPORT	T_DotOffset
	IMPORT	T_DotParag
	IMPORT	T_DotPixs
	IMPORT	T_GetLinePtr
	IMPORT	T_GetObject
	IMPORT	T_GetParPtr
	IMPORT	T_GetRulBits
	IMPORT	T_GetSpacing
	IMPORT	T_LockRuler
	IMPORT	T_TopLine
	IMPORT	T_TrackFont
	IMPORT	T_UnlockRuler
	IMPORT	T_UseTab

;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	T_Pix2Offset





;------------------------------------------------------------------------------
;
; T_GetStart (LineWidth:w,MaxWidth:w,NoIndent:w) : StartWidth:w
;
; Returns the starting horiz offset for a Q_Line of Q_Text, using the current
; ruler information.

T_GetStart	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		INPUT  LineWidth:w,MaxWidth:w,NoIndent:w
		OUTPUT StartWidth:w

		BEGIN

		stz StartWidth

		lda T_CurRulBits
		and #T_Center
		bne DoCenter
		lda T_CurRulBits
		and #T_Right
		beq DoIndent

DoRight	subword MaxWidth,LineWidth,StartWidth
		bra DoIndent

DoCenter	subword MaxWidth,LineWidth,a
		lsr a
		sta StartWidth

DoIndent
		lda NoIndent
		bne Exit
		addword StartWidth,T_CurIndent,StartWidth

Exit	RETURN
		ENDP




;-------------------------------------------------------------------------
;
; T_Pt2Offset (ObjHdl:l,Pt:l) : ParagNo:w,Offset:w,LineNo:w,Q_Pixels:w
;

T_Pt2Offset	PROC		EXPORT
			;Using P_ObjData
			;Using T_TextEqu
			;Using T_Data

		INPUT  ObjHdl:l,Pt:l
		OUTPUT ParagNo:w,Offset:w,LineNo:w,Q_Pixels:w

		LOCAL  ObjPtr:l,ParagPtr:l,LinePtr:l
		LOCAL  EndParag:w,EndLine:w,ParagCount:w,LineCount:w,Q_Rect:r
		LOCAL  RulBits:w,TotHeight:w,Leading:w,Ascent:w,Descent:w
		LOCAL  MaxWidth:w


		BEGIN

		movelong [ObjHdl],ObjPtr
		moveword [ObjPtr]:#P_StartParag,ParagNo
		moveword [ObjPtr]:#P_StartLine,LineNo
		moveword [ObjPtr]:#P_EndParag,EndParag
		moveword [ObjPtr]:#P_EndLine,EndLine

		moverect [ObjPtr]:#P_TRect,Q_Rect

		subword Pt,Q_Rect,Pt
		subword Pt+2,Q_Rect+2,Pt+2
		subword Q_Rect+6,Q_Rect+2,MaxWidth

		rcall T_GetParPtr,in=(#0:a),out=(ParagPtr:ax)
		moveword [ParagPtr],ParagCount


		stz TotHeight


; Find the Q_Line and the parag

ParagLoop
		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		moveword [ParagPtr]:#T_LineCount,LineCount
		rcall T_GetRulBits,in=(ParagNo:a),out=(RulBits:a)
		rcall T_GetLinePtr,in=(ParagNo:a,LineNo:x),out=(LinePtr:ax)


LineLoop
		cmpw ParagNo,EndParag
		blt CheckCount
		cmpw LineNo,EndLine
		blt CheckCount
		brl PastEnd

CheckCount
		cmpw LineNo,LineCount
		bge NextParag

		moveword [LinePtr]:#T_Ascent,a
		and #$ff
		sta Ascent
		moveword [LinePtr]:#T_Descent,a
		and #$ff
		sta Descent

		rcall T_GetSpacing,in=(RulBits:a,Ascent:x,Descent:y),out=(a:a)
		addword a,Ascent,a
		addword a,Descent,a
		addword a,TotHeight,TotHeight
		scmpw a,Pt
		bge FindPixels

		addwl #T_LineInfoSize,LinePtr
		inc LineNo
		bra LineLoop



NextParag
		stz LineNo
		inc ParagNo
		cmpw ParagNo,ParagCount
		bge PastEnd

		addwl #T_ParInfoSize,ParagPtr
		brl ParagLoop


PastEnd
		moveword MaxWidth,Pt+2

		lda LineNo
		bne Skip
		lda ParagNo
		beq FindPixels
		dec ParagNo
		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		moveword [ParagPtr]:#T_LineCount,LineNo
Skip	dec LineNo



FindPixels
		in ParagNo:w,LineNo:w,Pt+2:w,MaxWidth:w
		out Offset:w,Q_Pixels:w
		xcall T_Pix2Offset

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; T_Pix2Offset (ParagNo:w,LineNo:w,Pix:w,MaxWidth:w) : Offset:w,Q_Pixels:w
;
;

T_Pix2Offset	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		INPUT  ParagNo:w,LineNo:w,Pix:w,MaxWidth:w
		OUTPUT Offset:w,Q_Pixels:w

		LOCAL  ParagPtr:l,TextHdl:l,TextPtr:l,LinePtr:l
		LOCAL  LineCount:w,MaxOffset:w,LineWidth:w
		LOCAL  Width:l,NextWidth:l,SpaceExtra:l
		LOCAL  FontChanged:w,Spaces:w,TabStop:w

		BEGIN


		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		rcall T_LockRuler,in=(ax:ax)

		moveword [ParagPtr]:#T_LineCount,LineCount
		movelong [ParagPtr]:#T_TextBlock,TextHdl
		tool _HLock,in=(TextHdl:l)
		movelong [TextHdl],TextPtr
		moveword [ParagPtr]:#T_ParOffset,a
		addwl a,TextPtr

		rcall T_GetLinePtr,in=(ParagNo:a,LineNo:x),out=(LinePtr:ax)
		moveword [LinePtr]:#T_Offset,Offset
		addword a,[LinePtr]:#T_Length,MaxOffset
		moveword [LinePtr]:#T_LineWidth,LineWidth


		stz FontChanged
		stzl SpaceExtra
		stzl Width

		call T_GetStart,in=(LineWidth:w,MaxWidth:w,LineNo:w),out=(a:w)
		sta Width+2

		lda Pix
		jmi SetPixs


		lda T_CurRulBits
		and #T_Full
		beq NotFull

		dec LineCount
		cmpw LineNo,LineCount
		bge NotFull

		in TextPtr:l,Offset:w,MaxOffset:w
		out TabStop:w,Spaces:w
		xcall T_CountSpaces

		lda Spaces
		beq NotFull

		spacelong
		subword MaxWidth,LineWidth,s
		pushword Spaces
		_FixRatio
		pulllong SpaceExtra
NotFull



		call T_TrackFont,in=(TextPtr:l,Offset:w)
		rcall D_SelectFont,in=(T_CurFont:a,T_CurStyle:x,#0:y)


CharLoop
		cmpw Offset,MaxOffset
		jge SetPixs

		moveword [TextPtr]:Offset,a
		inc Offset
		and #$ff
		cmp #T_MaxChange+1
		bge NormalChar

		inc FontChanged
		asl a
		tax
		jmp (JumpTable,x)


NormalChar	ldx FontChanged
		beq NoChange

		pha
		rcall D_SelectFont,in=(T_CurFont:a,T_CurStyle:x,#0:y)
		pla
		stz FontChanged

NoChange
		cmp #SPACE
		beq DoSpace
		cmp #TAB
		beq DoTab
		cmp #CR
		beq FoundIt

		jsl D_GetWdth
		sta NextWidth+2
		stz NextWidth
		brl CheckIt


DoSpace
		moveword SpaceExtra,NextWidth
		rcall D_GetWdth,in=(#SPACE:a),out=(a:a)
		addword a,SpaceExtra+2,NextWidth+2
		brl CheckIt


DoTab
		in TextPtr:l,Offset:w,Width+2:w,MaxWidth:w
		in T_CurFont:l,#0:w
		out a:w
		xcall T_UseTab
		bmi FoundIt
		subword a,Width+2,NextWidth+2
		stz NextWidth
		bra CheckIt


CheckIt
		rcall P_Fix2Int,in=(NextWidth:ax),out=(a:a)
		lsr a
		addword a,Width+2,a
		cmp Pix
		bge FoundIt

		addlong Width,NextWidth,Width
		brl CharLoop



FoundIt	dec Offset
SetPixs	rcall P_Fix2Int,in=(Width:ax),out=(a:a)
		cmp MaxWidth
		blt StorePixs
		lda MaxWidth
StorePixs	sta Q_Pixels

		call T_UnlockRuler
		tool _HUnlock,in=(TextHdl:l)

Exit
		RETURN




JumpTable	DC.W	CharLoop
	DC.W	FontChange
	DC.W	StyleChange
	DC.W	SizeChange
	DC.W	ChangeExit
	DC.W	CharLoop
	DC.W	CharLoop
	DC.W	CharLoop


FontChange
		moveword [TextPtr]:Offset,T_CurFont
		inc Offset
		bra ChangeExit

StyleChange
		movebyte [TextPtr]:Offset,T_CurStyle
		bra ChangeExit

SizeChange
		movebyte [TextPtr]:Offset,T_CurSize
		bra ChangeExit

ChangeExit
		inc Offset
		brl CharLoop



		ENDP





;------------------------------------------------------------------------------;
;
; T_Offset2Pix (ObjHdl:l,ParagNo:w,StopOffset:w,LineNo:w) : Pixs:w
;

T_Offset2Pix	PROC		EXPORT
			;Using P_ObjData
			;Using T_TextEqu
			;Using T_Data

		INPUT  ObjHdl:l,ParagNo:w,StopOffset:w,LineNo:w
		OUTPUT Pixs:w

		LOCAL  ParagPtr:l,LinePtr:l,TextHdl:l,TextPtr:l,ObjPtr:l
		LOCAL  LineCount:w,MaxWidth:w
		LOCAL  CurOffset:w,MaxOffset:w,LineWidth:w
		LOCAL  Width:l,SpaceExtra:l
		LOCAL  Spaces:w,TabStop:w,FontChanged:w


		BEGIN

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		rcall T_LockRuler,in=(ax:ax)
		rcall T_GetLinePtr,in=(ParagNo:a,LineNo:x),out=(LinePtr:ax)

		movelong [ParagPtr]:#T_TextBlock,TextHdl
		moveword [ParagPtr]:#T_LineCount,LineCount
		moveword [LinePtr]:#T_Offset,CurOffset
		addword a,[LinePtr]:#T_Length,MaxOffset
		moveword [LinePtr]:#T_LineWidth,LineWidth

		tool _HLock,in=(TextHdl:l)
		movelong [TextHdl],TextPtr
		moveword [ParagPtr]:#T_ParOffset,a
		addwl a,TextPtr

		stzl Width
		stzl SpaceExtra

		movelong [ObjHdl],ObjPtr
		subword [ObjPtr]:#P_TRect+6,[ObjPtr]:#P_TRect+2,MaxWidth


		in LineWidth:w,MaxWidth:w,LineNo:w
		out Width+2:w
		xcall T_GetStart


		lda T_CurRulBits
		and #T_Full
		beq NotFull

		dec LineCount
		cmpw LineNo,LineCount
		bge NotFull


		in TextPtr:l,CurOffset:w,MaxOffset:w
		out TabStop:w,Spaces:w
		xcall T_CountSpaces


		lda Spaces
		beq NotFull


		spacelong
		subword MaxWidth,LineWidth,s
		pushword Spaces
		_FixRatio
		pulllong SpaceExtra
NotFull




		call T_TrackFont,in=(TextPtr:l,CurOffset:w)
		rcall D_SelectFont,in=(T_CurFont:a,T_CurStyle:x,#0:y)


CharLoop
		cmpw CurOffset,StopOffset
		jge Done
		moveword [TextPtr]:CurOffset,a
		inc CurOffset
		and #$ff
		cmp #T_MaxChange+1
		blt SpecialCase

		ldx FontChanged
		beq NoChange

		pha
		rcall D_SelectFont,in=(T_CurFont:a,T_CurStyle:x,#0:y)
		pla
		stz FontChanged
NoChange


		cmp #SPACE
		beq DoSpace
		cmp #TAB
		beq DoTab
		cmp #CR
		beq Done


		jsl D_GetWdth
		addword a,Width+2,Width+2
		bra CharLoop

DoSpace
		rcall D_GetWdth,in=(#SPACE:a),out=(a:a)
		addword a,Width+2,Width+2
		addlong Width,SpaceExtra,Width
		bra CharLoop


DoTab
		in TextPtr:l,CurOffset:w,Width+2:w,MaxWidth:w
		in T_CurFont:l,#0:w
		out a:w
		xcall T_UseTab
		bmi Done
		sta Width+2
		brl CharLoop



SpecialCase
		inc FontChanged
		asl a
		tax
		jmp (JumpTable,x)



Done	rcall P_Fix2Int,in=(Width:ax),out=(Width+2:a)

SetPixs
		cmpw Width+2,MaxWidth
		blt StorePixs
		lda MaxWidth
StorePixs	sta Pixs

		call T_UnlockRuler
		tool _HUnlock,in=(TextHdl:l)

		RETURN


JumpTable	DC.W	Done
	DC.W	FontChange
	DC.W	StyleChange
	DC.W	SizeChange
	DC.W	ChangeExit
	DC.W	CharLoop
	DC.W	CharLoop
	DC.W	CharLoop

FontChange
		moveword [TextPtr]:CurOffset,T_CurFont
		inc CurOffset
		bra ChangeExit

StyleChange
		movebyte [TextPtr]:CurOffset,T_CurStyle
		bra ChangeExit

SizeChange
		movebyte [TextPtr]:CurOffset,T_CurSize
		bra ChangeExit

ChangeExit
		inc CurOffset
		brl CharLoop


		ENDP





;------------------------------------------------------------------------------
;
; T_Offset2Line (ParagNo:w,Offset:w) : ObjHdl:l,LineNo:w
;

T_Offset2Line	PROC		EXPORT
			;Using P_ObjData
			;Using T_Data
			;Using T_TextEqu

		INPUT  ParagNo:w,Offset:w
		OUTPUT ObjHdl:l,LineNo:w
		LOCAL  LinePtr:l


		BEGIN

		rcall T_GetLinePtr,in=(ParagNo:a,#0:x),out=(LinePtr:ax)

		stz LineNo
LineLoop	addword [LinePtr],[LinePtr]:#T_Length,a
		cmpw a,Offset
		bge EndLoop

		addwl #T_LineInfoSize,LinePtr
		inc LineNo
		bra LineLoop
EndLoop


		call T_GetObject,in=(ParagNo:w,LineNo:w),out=(ObjHdl:l)

Exit	RETURN
		ENDP




;------------------------------------------------------------------------------;
;
; T_BlinkCaret ()
;

T_BlinkCaret	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data
			;Using T_TextEqu

		LOCAL ParagPtr:l,LinePtr:l,ObjHdl:l,ObjPtr:l
		LOCAL RTop:w,RLeft:w,Top:w,Height:w,Ascent:w,Descent:w

		BEGIN

		tool _SetPort,in=(P_CurrentWin:l)

		movelong T_DotObj,ObjHdl
		movelong [ObjHdl],ObjPtr
		cmpw [ObjPtr]:#P_PageNo,P_Page
		jne Exit

		moveword [ObjPtr]:#P_TRect,RTop
		moveword [ObjPtr]:#P_TRect+2,RLeft

		call T_TopLine,in=(T_DotParag:w,T_DotLine:w),out=(a:l,Top:w)
		rcall T_GetLinePtr,in=(T_DotParag:a,T_DotLine:x),out=(LinePtr:ax)

		moveword [LinePtr]:#T_Ascent,a
		and #$ff
		sta Ascent
		moveword [LinePtr]:#T_Descent,a
		and #$ff
		sta Descent

		addword a,Ascent,Height

		rcall T_GetParPtr,in=(T_DotParag:a),out=(ParagPtr:ax)
		rcall T_GetRulBits,in=(T_DotParag:a),out=(a:a)
		rcall T_GetSpacing,in=(a:a,Ascent:x,Descent:y),out=(a:a)
		addword a,Top,Top

		in T_DotObj:l,T_DotParag:w,T_DotOffset:w,T_DotLine:w
		out T_DotPixs:w
		xcall T_Offset2Pix


		addword P_ContentRect+2,RLeft,a
		subword a,P_H,a
		addword a,T_DotPixs,s

		addword P_ContentRect,RTop,a
		subword a,P_V,a
		addword a,Top,s
		_MoveTo

		tool _SetPenMode,in=(#notXOR:w)
		tool _Line,in=(#0:w,Height:w)
		tool _SetPenMode,in=(#0:w)

Exit	RETURN
		ENDP





;------------------------------------------------------------------------------
;
; T_EraseCaret ()
;
; Erases the caret if necessary.

T_EraseCaret	PROC		EXPORT
			;Using T_Data

		lda T_CaretDrawn
		beq Exit
		jsl T_BlinkCaret
		stz T_CaretDrawn
Exit	rtl
		ENDP
		END

