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

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

	IMPORT	D_BeachBall
	IMPORT	D_GetStats
	IMPORT	D_GetWdth
	IMPORT	D_GrowLHandle
	IMPORT	D_MustHandle
	IMPORT	D_SelectFont
	IMPORT	T_CurColor
	IMPORT	T_CurFont
	IMPORT	T_CurIndent
	IMPORT	T_CurRulBits
	IMPORT	T_CurRulPtr
	IMPORT	T_CurStyle
	IMPORT	T_CurStyleSize
	IMPORT	T_GetParPtr
	IMPORT	T_GetSpacing
	IMPORT	T_LockRuler
	IMPORT	T_MaxAscent
	IMPORT	T_MaxDescent
	IMPORT	T_MulLine
	IMPORT	T_UnlockRuler



;------------------------------------------------------------------------------
;
; T_CountSpaces (TextPtr:l,Offset:w,MaxOffset:w) : TabStop:w,Spaces:w
;

T_CountSpaces	PROC		EXPORT
			;Using T_TextEqu

		INPUT  TextPtr:l,Offset:w,MaxOffset:w
		OUTPUT TabStop:w,Spaces:w

		BEGIN

		stz Spaces
		stz TabStop
		ldy Offset

CharLoop	cpy MaxOffset
		bge Exit
		lda [TextPtr],y
		iny

		and #$ff
		cmp #SPACE+1
		bge CharLoop

		cmp #5
		bge ChkTab
		asl a
		tax
		jmp (JumpTable,x)
ChkTab
		cmp #9
		beq DoTab
		cmp #13
		beq Exit
		cmp #32
		beq DoSpace
		bra Charloop

DoSpace	inc Spaces
		bra CharLoop

DoTab	bra CharLoop

Skip2	iny
Skip1	iny
		bra CharLoop

Exit	dey
		lda [TextPtr],y
		and #$ff
		cmp #SPACE
		bne No
		dec Spaces
No	RETURN


JumpTable	DC.W	Exit	; 0
	DC.W	Skip2	; 1
	DC.W	Skip1	; 2
	DC.W	Skip1	; 3
	DC.W	Skip1	; 4

		ENDP




;-------------------------------------------------------------------------------
;
; T_UseTab (TextPtr:l,Offset:w,CurH:w,MaxH:w,FontId:l,Color:w) : NextH
;
; TextPtr -- Ptr to Q_Text
; Offset  -- Offet into Q_Text after tab.
; CurH	-- Where Q_Text up to TextPtr ends horiz.
; MaxH	-- The right margin
;
; NextH   --	-1  -- Try tab on next Q_Line.
;			 >= 0  -- Where to start putting Q_Text
;

T_UseTab	PROC		EXPORT
			;Using P_Data
			;Using T_TextEqu
			;Using T_Data

		INPUT  TextPtr:l,Offset:w,CurH:w,MaxH:w,FontId:l,Color:w
		OUTPUT NextH:w

		LOCAL  RulerPtr:l,FontChanged:w,TabCount:w
		LOCAL  CurTab:l,TextWidth:w,Room:w,TheChar:w

		BEGIN


		moveword #-1,NextH

; D_Save these to be restored later

		pushword FontId
		pushword FontId+2
		pushword Color


		stz FontChanged

		lda T_CurRulBits
		and #T_Full+T_left
		jeq TreatSpace

		movelong T_CurRulPtr,RulerPtr

		cmpw CurH,[RulerPtr]:#T_Indent
		bge NotIndent
		moveword [RulerPtr]:y,CurTab
		stz CurTab+2
		bra DoTab
NotIndent



; If there are no tab stops, treat as a space

		moveword [RulerPtr]:#T_TabCount,TabCount
		jeq TreatSpace


; If the first tab stop is greater than MaxH, treat as a space

		addwl #T_TabArray,RulerPtr
		cmpw [RulerPtr],MaxH
		jge TreatSpace

		ldx #0
LookTab
		cmpw CurH,[RulerPtr]
		blt FoundTab
		addwl #4,RulerPtr
		inx
		cpx TabCount
		blt LookTab
		brl Exit				  ;Past all tabs, try next Q_Line.

FoundTab
		movelong [RulerPtr],CurTab


DoTab
		cmpw CurTab,MaxH
		bge Exit
		sta NextH
		lda CurTab+2
		beq Exit			   ;left tab just move over


		subword CurTab,CurH,Room
		stz TextWidth


CharLoop
		moveword [TextPtr]:Offset,a
		inc Offset
		and #$ff
		sta TheChar
		cmp #'.'
		bne noPer
		lda CurTab+2
		bpl NormalChar
		brl AdjustTab

NoPer
		cmp #sp
		bcs NormalChar


SpecChar
		asl a
		tax
		jmp (JumpTable,x)


NormalChar
		lda FontChanged
		beq NoChange
		rcall D_SelectFont,in=(FontId:a,FontId+2:x,#0:y)
		stz FontChanged
NoChange	rcall D_GetWdth,in=(TheChar:a),out=(a:a)
		addword a,TextWidth,TextWidth

GotTW
		cmp Room
		bge MakeCurH
		brl CharLoop


AdjustTab
		subword Room,TextWidth,a
		bpl Okay
		lda #0
Okay	addword a,CurH,NextH
		bra Exit



TreatSpace
		rcall D_GetWdth,in=(#SPACE:a),out=(a:a)
		addword a,CurH,CurH
		cmp MaxH
		bge Exit

MakeCurH	moveword CurH,NextH

Exit	ply
		plx
		pla
		jsl D_SelectFont

		RETURN


FChange
		moveword [TextPtr]:Offset,FontId
		inc Offset
		bra ChangeExit

SizeChange
		movebyte [TextPtr]:Offset,FontId+3
		bra ChangeExit

StyleChange
		movebyte [TextPtr]:Offset,FontId+2
		bra ChangeExit

ChangeExit
		inc Offset
		inc FontChanged
		brl CharLoop



JumpTable	DC.W	0,FChange			;0,1
	DC.W	StyleChange,SizeChange		;2,3
	DC.W	ChangeExit,CharLoop			;4,5
	DC.W	CharLoop,CharLoop			;6,7
	DC.W	0,AdjustTab					;8,9
	DC.W	0,0							;a,b
	DC.W	0,AdJustTab					;c,d

		ENDP





;------------------------------------------------------------------------------
;
; T_CutLine (TextPtr:l,LineNo:w,Offset:w,MaxWidth:w) :
;			 EndOffset:w,LineWidth:w,HitCR:w
;

T_CutLine	PROC		EXPORT
			;Using P_ObjData
			;Using T_Data
			;Using T_TextEqu

		INPUT  TextPtr:l,LineNo:w,Offset:w,MaxWidth:w
		OUTPUT EndOffset:w,LineWidth:w,HitCR:w

		LOCAL  CurWidth:w,Font:w,StyleSize:w
		LOCAL  MaxAscent:w,MaxDescent:w
		LOCAL  theChar:w,CharSize:w,FontChanged:w,ValidOffset:w

		BEGIN

		stz EndOffset
		stz CurWidth
		stz FontChanged
		stz ValidOffset
		stz HitCR

		lda LineNo
		bne NotFirst
		moveword T_CurIndent,CurWidth
NotFirst


		lda T_CurFont
		sta Font
		ldx T_CurStyleSize
		stx StyleSize
		ldy #0
		jsl D_SelectFont

		rcall D_GetStats,out=(MaxAscent:a,MaxDescent:x)



CharLoop
		moveword [TextPtr]:Offset,a
		inc Offset
		and #$ff
		sta theChar
		cmp #SPACE+1
		blt SpecialCase


		ldx FontChanged
		beq NoChange

		rcall D_SelectFont,in=(Font:a,StyleSize:x,#0:y)
		jsl D_GetStats
		cmp MaxAscent
		blt Smaller1
		sta MaxAscent
Smaller1	cpx MaxDescent
		blt Smaller2
		stx MaxDescent
Smaller2

		stz FontChanged
		lda theChar

NoChange
		cmp #$2D
		beq DoHyphen

CheckWidth	jsl D_GetWdth
		addword a,CurWidth,CurWidth
		cmp MaxWidth
		bge NoFit
		moveword Offset,ValidOffset
		bra CharLoop


NoFit	lda EndOffset
		bne WordBreaks

; went one too far
		jsr D_SetStuff
		dec EndOffset
		lda ValidOffset
		beq NoChanges
		moveword ValidOffset,EndOffset
NoChanges	rcall D_GetWdth,in=(theChar:a),out=(CharSize:a)
		subword LineWidth,CharSize,LineWidth
WordBreaks	brl EndLine



SpecialCase
		cmp #5
		bge ChkTab
		asl a
		tax
		jmp (JumpTable,x)

ChkTab
		cmp #9
		beq DoTab
		cmp #13
		beq X_DoCR
		cmp #32
		beq DoSpace
		brl Charloop
DoHyphen
		rcall D_GetWdth,in=(theChar:a),out=(a:a)
		addword a,CurWidth,a
		cmp MaxWidth
		bge NoFit
		sta CurWidth

		jsr D_SetStuff
		brl CharLoop


DoSpace
		jsr D_SetStuff

		lda theChar
		brl CheckWidth


DoTab
		jsr D_SetStuff

		in TextPtr:l,Offset:w,CurWidth:w,MaxWidth:w
		in StyleSize:w,Font:w,#0:w
		out a:w
		xcall T_UseTab

		bmi TabNext
		sta CurWidth
		jsr D_SetStuff
		brl CharLoop

TabNext	dec Offset
		jsr D_SetStuff
		brl EndLine


X_DoCR
		dec Offset
		jsr D_SetStuff
		inc HitCR
		brl EndLine


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

StyleChange	movebyte [TextPtr]:Offset,StyleSize
		bra ChangeExit

SizeChange	movebyte [TextPtr]:Offset,StyleSize+1

ChangeExit
		inc FontChanged
		inc Offset
		brl CharLoop


EndLine	RETURN


D_SetStuff
		moveword Offset,EndOffset
		moveword CurWidth,LineWidth
		moveword Font,T_CurFont
		moveword StyleSize,T_CurStyleSize
		moveword MaxAscent,T_MaxAscent
		moveword MaxDescent,T_MaxDescent
		rts



JumpTable	DC.W	CharLoop	; 0
	DC.W	FontChange	; 1
	DC.W	StyleChange	; 2
	DC.W	SizeChange	; 3
	DC.W	ChangeExit	; 4

		ENDP




;----------------------------------------------------------------------------
;
; T_CutParag (ObjHdl:l; ParagNo:w; VertPos:w) : EndObjHdl:l,EndVertPos:w
;
; Works on T_CurParHdl.

T_CutParag	PROC		EXPORT
			;Using P_ObjData
			;Using T_Data
			;Using T_TextEqu

		INPUT  ObjHdl:l,ParagNo:w,VertPos:w
		OUTPUT EndObjHdl:l,EndVertPos:w
		ERROR  Err


		LOCAL ObjPtr:l,ParagPtr:l,TextHdl:l,TextPtr:l
		LOCAL NextObj:l,LinesHdl:l,LinePtr:l,Room:w
		LOCAL LineCount:w,MaxWidth:w,MaxVert:w,ParHeight:w,LineHeight:w
		LOCAL Offset:w,EndOffset:w,LineWidth:w,LineFont:l,HitCR:w


		BEGIN

		stz Err
		stz ParHeight
		stz LineCount

		moveword #20,Room

		call D_MustHandle,in=(#20*T_LineInfoSize:l,#attrLocked+attrPurge1:w),out=LinesHdl:l
		movelong [LinesHdl],LinePtr

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

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

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		movelong ObjHdl,[ParagPtr]:#T_ObjHdl
		moveword VertPos,[ParagPtr]:#T_ParStart

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


		movelong [TextPtr],T_CurFont
		rcall D_SelectFont,in=(T_CurFont:a,T_CurStyle:x,#0:y)

		moveword #T_ParHeader,Offset



LineLoop
		movelong T_CurFont,LineFont

CutLine
		in TextPtr:l,LineCount:w,Offset:w,MaxWidth:w
		out EndOffset:w,LineWidth:w,HitCR:w
		xcall T_CutLine

		rcall T_GetSpacing,in=(T_CurRulBits:a,T_MaxAscent:x,T_MaxDescent:y)
		addword a,T_MaxAscent,a
		addword a,T_MaxDescent,LineHeight
		addword a,VertPos,a
		cmp MaxVert
		bge NextObject

		jsr SetLine
		lda HitCR
		beq LineLoop
ToDone	brl Done


NextObject
		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_NThread,NextObj
		cpzl NextObj
		bne NextOkay

		cmpw VertPos,MaxVert
		bge OverFlow

		moveword ParagNo,[ObjPtr]:#P_EndParag
		moveword LineCount,[ObjPtr]:#P_EndLine
		bra OverFlow


NextOkay
		moveword ParagNo,[ObjPtr]:#P_EndParag
		moveword LineCount,[ObjPtr]:#P_EndLine

		movelong NextObj,ObjHdl
		movelong [ObjHdl],ObjPtr
		moveword ParagNo,[ObjPtr]:#P_StartParag
		moveword LineCount,[ObjPtr]:#P_StartLine
		subword [ObjPtr]:#P_TRect+6,[ObjPtr]:#P_TRect+2,MaxWidth
		subword [ObjPtr]:#P_TRect+4,[ObjPtr]:#P_TRect,MaxVert
		stz VertPos

		movelong LineFont,T_CurFont
		brl CutLine


OverFlow
		jsr SetLine


Done
		movelong ObjHdl,EndObjHdl
		moveword VertPos,EndVertPos

		rcall T_MulLine,in=(LineCount:a),out=(a:a)
		tool _SetHandleSize,in=(#0:w,a:w,LinesHdl:l)
		call T_UnlockRuler
		tool _HUnlock,in=(TextHdl:l)

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		moveword ParHeight,[ParagPtr]:#T_ParHeight
		moveword LineCount,[ParagPtr]:#T_LineCount
		movelong LinesHdl,[ParagPtr]:#T_LinesHdl

Exit
		RETURN


SetLine
		cmpw LineCount,Room
		blt NoGrow

		addword LineCount,#20,a
		jsl T_MulLine
		call D_GrowLHandle,in=(#0:w,a:w,LinesHdl:l),out=LinePtr:l,err=Err
		jcs LocExit
		rcall T_MulLine,in=(LineCount:a)
		addwl a,LinePtr
NoGrow


		addword LineHeight,VertPos,VertPos
		addword LineHeight,ParHeight,ParHeight
		inc LineCount


		moveword Offset,[LinePtr]:#T_Offset
		subword EndOffset,Offset,[LinePtr]:#T_Length
		moveword LineWidth,[LinePtr]:#T_LineWidth
		shortm
		moveword T_MaxAscent,[LinePtr]:#T_Ascent
		moveword T_MaxDescent,[LinePtr]:#T_Descent
		longm

		moveword EndOffset,Offset
		addwl #T_LineInfoSize,LinePtr
LocExit	rts


		ENDP





;-------------------------------------------------------------------------------
;
; T_RecutParag (ObjHdl:l,ParagNo:w;StartVert:w) : EndObjHdl:l,EndVert:w
;

T_RecutParag	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu
			;Using P_ObjData

		INPUT  ObjHdl:l,ParagNo:w,StartVert:w
		OUTPUT EndObjHdl:l,EndVert:w

		LOCAL  ParagPtr:l,ParHeight:w,LinesHdl:l

		BEGIN

		jsl D_BeachBall

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		movelong [ParagPtr]:#T_LinesHdl,LinesHdl

		cpzl LinesHdl
		beq NoDispose
		tool _DisposeHandle,in=(LinesHdl:l)
NoDispose

		in ObjHdl:l,ParagNo:w,StartVert:w
		out EndObjHdl:l,EndVert:w
		xcall T_CutParag

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		tool _HUnlock,in=([ParagPtr]:#T_LinesHdl:l)

		RETURN
		ENDP





;------------------------------------------------------------------------------;
;
; T_TrackFont (TextPtr:l, StopOffset:w)
;
; Track a font from the start of the paragragh pointed to by TextPtr
;

T_TrackFont	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		INPUT  TextPtr:l,StopOffset:w


		BEGIN

		moveword [TextPtr]:#T_ParFont,T_CurFont
		moveword [TextPtr]:#T_ParStyle,T_CurStyleSize
		moveword [TextPtr]:#T_ParColor,a
		and #$ff
		sta T_CurColor


		ldy #T_ParHeader

CharLoop
		cpy StopOffset
		bge Exit
		lda [TextPtr],y
		iny
		and #$ff
		cmp #T_ColorChange+1
		bge CharLoop


		asl a
		tax
		jmp (JumpTable,x)


FontChange
		moveword [TextPtr]:y,T_CurFont
		iny
		iny
		bra CharLoop

StyleChange
		shortm
		moveword [TextPtr]:y,T_CurStyleSize
		longm
		iny
		bra CharLoop

SizeChange
		shortm
		moveword [TextPtr]:y,T_CurStyleSize+1
		longm
		iny
		bra CharLoop

ColorChange
		shortm
		moveword [TextPtr]:y,T_CurColor
		longm
		iny
		bra CharLoop



Exit	RETURN


JumpTable	DC.W	Exit
	DC.W	FontChange
	DC.W	StyleChange
	DC.W	SizeChange
	DC.W	ColorChange
		ENDP
		END

