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

	IMPORT	D_Deref
	IMPORT	D_DrawText
	IMPORT	D_GetStats
	IMPORT	D_GetWdth
	IMPORT	D_KillFont
	IMPORT	D_SectClipRect
	IMPORT	D_SelectFont


;-------------------------------------------------------------------------------
;
; X_DrawPLText
;


X_DrawPLText	PROC	EXPORT

	INPUT TextHdl:l,RectPtr:l

	LOCAL OldFont:l,OldColor:w
	LOCAL ClipRgn:l,Just:w,Spacing:w,Remain:w
	LOCAL TextPtr:l,EndPtr:l,Pt:l

	LOCAL Offset:w,StartOffset:w,EndOffset:w
	LOCAL LineTop:w,MaxWidth:w,CurWidth:w,EndWidth:w
	LOCAL Font:l,Color:w,FontChanged:w
	LOCAL StartFont:l,StartColor:w
	LOCAL Ascent:w,Descent:w,Leading:w
	LOCAL EndAscent:w,EndDescent:w,EndLeading:w
	LOCAL MaxAscent:w,MaxDescent:w,MaxLeading:w

	LOCAL StartPar:w,theChar:w,SpaceCount:w
	LOCAL StartSeg:w,EndSeg:w,LeftOffset:w


	BEGIN

	tool _NewRgn,out=(ClipRgn:l)
	tool _GetClip,in=(ClipRgn:l)
	call D_SectClipRect,in=(RectPtr:l)

	tool _GetFontId,out=(OldFont:l)
	tool _GetForeColor,out=(OldColor:w)
	jsl D_KillFont

	rcall D_Deref,in=(TextHdl:ax),out=(TextPtr:ax)
	tool _GetHandleSize,in=(TextHdl:l),out=(:l)
	addlong s,TextPtr,EndPtr

	moveword #1,StartPar
	stz EndOffset
	moveword [RectPtr],LineTop
	subword [RectPtr]:#6,[RectPtr]:#2,MaxWidth

LineLoop
	lda StartPar
	beq NotStart
	addwl EndOffset,TextPtr
	cmpl TextPtr,EndPtr
	bge Done
	jsr DoParag
	stz StartPar
NotStart

	jsr CutLine
	jsr DrawLine

	jsr GetBaseOffset
	addword a,LineTop,a
	addword a,EndDescent,LineTop
	scmpw LineTop,[RectPtr]:#4
	blt LineLoop

Done
	tool _HUnlock,in=(TextHdl:l)
	tool _SetClip,in=(ClipRgn:l)
	tool _DisposeHandle,in=(ClipRgn:l)
	rcall D_SelectFont,in=(OldFont:ax,OldColor:y)


	RETURN


DoParag
	movelong [TextPtr],Font
	moveword [TextPtr]:#4,a
	and #$ff
	sta Color
	moveword [TextPtr]:#5,a
	and #$ff
	sta Just
	moveword [TextPtr]:#6,a
	and #$ff
	sta Spacing
	moveword #7,Offset
	rts


D_SetStuff
	moveword Offset,EndOffset
	moveword CurWidth,EndWidth
	moveword MaxAscent,EndAscent
	moveword MaxDescent,EndDescent
	moveword MaxLeading,EndLeading
	rts



CutLine
	movelong Font,StartFont
	moveword Color,StartColor
	moveword Offset,StartOffset

	stz CurWidth
	stz SpaceCount
	stz EndOffset

	rcall D_SelectFont,in=(Font:ax,Color:y)
	rcall D_GetStats,out=(MaxAscent:a,MaxDescent:x,MaxLeading:y)
	stz FontChanged


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


	lda FontChanged
	beq NoChange

	rcall D_SelectFont,in=(Font:a,Font+2:x,#0:y)
	rcall D_GetStats,out=(Ascent:a,Descent:x,Leading:y)
	cmpw Ascent,MaxAscent
	blt AscOk
	sta MaxAscent
AscOk

	cmpw Descent,MaxDescent
	blt DscOk
	sta MaxDescent
DscOk

	cmpw Leading,MaxLeading
	blt LdgOk
	sta MaxLeading
LdgOk

	stz FontChanged
NoChange



CheckWidth
	rcall D_GetWdth,in=(theChar:a)
	addword a,CurWidth,CurWidth
	cmp MaxWidth
	blt CharLoop


NoFit
	lda EndOffset
	bne Set
	jsr D_SetStuff
Set	brl CutExit


SpecialCase
	asl a
	tax
	jmp (CutTable,x)


DoSpace
	jsr D_SetStuff
	inc SpaceCount
	bra CheckWidth

DoTab
	tool _UDivide,in=(CurWidth:w,#80:w),out=(Remain:w,a:w)
	subword #80,Remain,a
	addword a,CurWidth,a
	cmp MaxWidth
	bge TabNext

	sta CurWidth
	jsr D_SetStuff
	brl CharLoop

TabNext	dec Offset
	jsr D_SetStuff
	brl CutExit


X_DoCR
	jsr D_SetStuff
	inc StartPar
	brl CutExit


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

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

SizeChange	movebyte [TextPtr]:Offset,Font+3

ChangeExit
	inc Offset
	inc FontChanged
	brl CharLoop


CutExit
	rts






DrawLine
	jsr DoJust

	addword [RectPtr]:#2,LeftOffset,s
	jsr GetBaseOffset
	addword a,LineTop,s
	_MoveTo

	stz FontChanged
	movelong StartFont,Font
	moveword StartColor,Color
	rcall D_SelectFont,in=(Font:ax,Color:y)
	moveword StartOffset,Offset
	moveword Offset,StartSeg


CharLoop2
	cmpw Offset,EndOffset
	jge DrawExit

	moveword [TextPtr]:Offset,a
	inc Offset
	and #$ff
	sta TheChar
	cmp #5
	blt ChangeChar


; real characters

	lda FontChanged
	beq NoChange2

	rcall D_SelectFont,in=(Font:ax,Color:y)
	stz FontChanged

	moveword Offset,StartSeg
	dec StartSeg
NoChange2

	cmpw TheChar,#TAB
	bne CharLoop2



DrawTab
	moveword Offset,EndSeg
	dec EndSeg
	jsr DrawSegment
	tool _GetPen,in=(!Pt:l)
	subword Pt+2,[RectPtr]:#2,a
	tool _UDivide,in=(a:w,#80:w),out=(Remain:w,a:w)
	subword #80,Remain,a
	tool _Move,in=(a:w,#0:w)
	moveword Offset,StartSeg
	bra CharLoop2



ChangeChar
	lda FontChanged
	bne DoJump
	moveword Offset,EndSeg
	dec EndSeg
	jsr DrawSegment
	inc FontChanged

DoJump
	lda theChar
	asl a
	tax
	jmp (DrawTable,x)


FontChange2	moveword [TextPtr]:Offset,Font
	inc Offset
	bra ChangeExit2

StyleChange2	movebyte [TextPtr]:Offset,Font+2
	bra ChangeExit2

SizeChange2	movebyte [TextPtr]:Offset,Font+3
	bra ChangeExit2

ColorChange2	moveword [TextPtr]:Offset,a
	and #$ff
	sta Color

ChangeExit2
	inc Offset
	brl CharLoop2

DrawExit
	moveword Offset,EndSeg
	jsr DrawSegment

	tool _SetSpaceExtra,in=(#0:l)
	rts



DrawSegment
	lda StartSeg
	addlong a,TextPtr,s
	subword EndSeg,StartSeg,s
	jsl D_DrawText
	rts



;------------------------------------------------------------------------------

GetBaseOffset
	cmpw Spacing,#2
	beq Triple
	cmp #1
	beq Double

Single	lda EndLeading
	bra GetBaseExit

Double	lda EndAscent
	lsr a
	addword a,EndLeading,a
	bra GetBaseExit

Triple	addword EndAscent,EndLeading,a

GetBaseExit
	addword a,EndAscent,a
	rts




;--------------------------------------------------------------------------------


DoJust

	stz LeftOffset

	lda Just
	beq JustExit
	cmp #1
	beq DoCenter
	cmp #2
	beq DoRight


DoFull
	lda StartPar
	bne JustExit
	lda SpaceCount
	beq JustExit
	spacelong
	subword MaxWidth,EndWidth,s
	pushword SpaceCount
	_FixRatio
	_SetSpaceExtra
	bra JustExit


DoRight	subword MaxWidth,EndWidth,a
	bra SetLeft

DoCenter	subword MaxWidth,EndWidth,a
	lsr a

SetLeft	sta LeftOffset
JustExit
	rts






CutTable	DC.W	CharLoop	; 0
	DC.W	FontChange	; 1
	DC.W	StyleChange	; 2
	DC.W	SizeChange	; 3
	DC.W	ChangeExit	; 4
	DC.W	CharLoop	; 5
	DC.W	CharLoop	; 6
	DC.W	CharLoop	; 7
	DC.W	CharLoop	; 8
	DC.W	DoTab	; 9
	DC.W	CharLoop	;10
	DC.W	CharLoop	;11
	DC.W	CharLoop	;12
	DC.W	X_DoCR	;13
	DC.W	CharLoop	;14
	DC.W	CharLoop	;15
	DC.W	CharLoop	;16
	DC.W	CharLoop	;17
	DC.W	CharLoop	;18
	DC.W	CharLoop	;19
	DC.W	CharLoop	;20
	DC.W	CharLoop	;21
	DC.W	CharLoop	;22
	DC.W	CharLoop	;23
	DC.W	CharLoop	;24
	DC.W	CharLoop	;25
	DC.W	CharLoop	;26
	DC.W	CharLoop	;27
	DC.W	CharLoop	;28
	DC.W	CharLoop	;29
	DC.W	CharLoop	;30
	DC.W	CharLoop	;31
	DC.W	DoSpace	;32


DrawTable	DC.W	DrawExit	; 0
	DC.W	FontChange2	; 1
	DC.W	StyleChange2	; 2
	DC.W	SizeChange2	; 3
	DC.W	ColorChange2	; 4


	ENDP
	END

