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

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

	IMPORT	D_GrowHandle
	IMPORT	T_GetParPtr
	IMPORT	T_OffsetPars

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

	ENTRY	T_LessRoom
	ENTRY	T_MakeRoom







; ------------------------------------------------------------------------------
; T_MakeRoom and T_LessRoom will grow or shrink the Q_Text blocks the
; correct amount and make sure everything in the parag array data
; structure gets updated, with the exception of the LinesHdl.
;


;------------------------------------------------------------------------------
;
; T_DoRoom (Par:w,Offset:w,Bytes:w)
;

T_DoRoom	PROC		EXPORT

		lda 4,s				  ;look at bytes
		jpl T_MakeRoom
		eor #$ffff
		inc a
		sta 4,s
		brl T_LessRoom

		ENDP



;------------------------------------------------------------------------------
;
; T_MakeRoom (ParagNo:w,Offset:w,Bytes:w)
;

T_MakeRoom	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		INPUT ParagNo:w,Offset:w,Bytes:w

		LOCAL ParagPtr:l,TextHdl:l,TextPtr:l
		LOCAL X_Blocksize:w,TextSize:w,NewTSize:w
		ERROR Err

		BEGIN

		stz Err

		lda Bytes
		jeq Exit

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		movelong [ParagPtr]:#T_TextBlock,TextHdl
		addword [ParagPtr]:#T_ParOffset,Offset,Offset


		movelong [TextHdl],TextPtr
		moveword [TextPtr],X_Blocksize
		moveword [TextPtr]:#T_TextSize,TextSize
		addword a,Bytes,NewTSize
		cmp X_Blocksize
		blt Fits




;   1. Grow Q_Text Block

		call D_GrowHandle,in=(#0:w,NewTSize:w,TextHdl:l),err=(Err)
		bcs Exit


		movelong [TextHdl],TextPtr
		moveword NewTSize,[TextPtr]


; BlockMove rest of Q_Text down

Fits
		moveword NewTSize,[TextPtr]:#T_TextSize
		addwl Offset,TextPtr

		pushlong TextPtr
		lda Bytes
		addlong a,TextPtr,s
		pushword #0
		subword TextSize,Offset,s
		_BlockMove


		call T_OffsetPars,in=(ParagNo:w,Bytes:w)


Exit	RETURN
		ENDP





;------------------------------------------------------------------------------
;
; T_LessRoom (ParagNo:w,Offset:w,Bytes:w)
;
; This routine will remove the Bytes starting at ParagNo,Offset.

T_LessRoom	PROC		EXPORT
			;Using T_TextEqu

		INPUT ParagNo:w,Offset:w,Bytes:w
		LOCAL ParagPtr:l,TextBlock:l,TextPtr:l
		LOCAL TextSize:w,SrcOffset:w,DestOffset:w

		BEGIN


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

		movelong [TextBlock],TextPtr
		moveword [TextPtr]:#T_TextSize,TextSize

		addword [ParagPtr]:#T_ParOffset,Offset,DestOffset
		addword a,Bytes,SrcOffset
		addlong a,TextPtr,s	   ;Src
		lda DestOffset
		addlong a,TextPtr,s	   ;Dest
		subword TextSize,SrcOffset,a
		pushword #0
		pushword a				;Amount
		_BlockMove

		subword TextSize,Bytes,TextSize
		cmp #4
		beq KillIt
		moveword a,[TextPtr]
		moveword a,[TextPtr]:#T_TextSize

		pushword #0
		pushword TextSize
		pushlong TextBlock
		_SetHandleSize

		pushword ParagNo
		subword #0,Bytes,s
		jsl T_OffsetPars
		bra Exit

KillIt
		tool _DisposeHandle,in=(TextBlock:l)

Exit	RETURN
		ENDP





;-------------------------------------------------------------------------------
;
; T_CalcFBytes (FontId1:l,Color1:w,FontId2:l,Color2:w) : Bytes:w
;

T_CalcFBytes	PROC		EXPORT

		INPUT  FontID1:l,Color1:w,FontID2:l,Color2:w
		OUTPUT Bytes:w

		BEGIN

		stz Bytes

		cmpw FontId1,FontID2
		beq SameFont

		moveword #3,bytes

SameFont
		cmpb FontId1+2,FontId2+2
		beq SameStyle

		addword #2,bytes,bytes

SameStyle
		cmpb FontId1+3,FontId2+3
		beq SameSize

		addword #2,bytes,bytes

SameSize
		cmpb color1,color2
		beq SameColor

		addword #2,bytes,bytes

SameColor

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; T_WriteFbytes (Ptr:l,FontId1:l,Color1:w,FontId2:l,Color2:w)
;

T_WriteFBytes	PROC		EXPORT

		INPUT Ptr:l,FontID1:l,Color1:w,FontID2:l,Color2:w

		BEGIN


		cmpw FontId1,FontID2
		beq SameFont
		moveword #1,[Ptr]
		moveword FontId2,[Ptr]:#1
		addwl #3,Ptr
SameFont

		cmpb FontId1+2,FontId2+2
		beq SameStyle
		lda FontId2+1			;2 is in the high
		shortm
		lda #2				   ;flag is in the low
		longm
		sta [Ptr]

		addwl #2,Ptr
SameStyle

		cmpb FontId1+3,FontId2+3
		beq SameSize

		lda FontId2+2			;3 is in the high
		shortm
		lda #3				   ;flag is in the low
		longm
		sta [Ptr]

		addwl #2,Ptr
SameSize

		cmpb color1,color2
		beq SameColor

		lda color2-1			;2 is in the high
		shortm
		lda #4				   ;flag is in the low
		longm
		sta [Ptr]

SameColor

		RETURN
		ENDP
		END

