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


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

	IMPORT	D_FastMult
	IMPORT	P_DrawTabs
	IMPORT	P_Fix2Int
	IMPORT	P_LShift
	IMPORT	D_SelectFont
	IMPORT	D_Set4Pat

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

	ENTRY	ruler_marks


;------------------------------------------------------------------------------
;
; P_Do_Ruler ()
;

P_Do_Ruler	PROC		EXPORT
			;Using P_RulerData

		input ppi:l,start:w,vis_start:w,vis_end:w,vert:w
		; pass in necess. globals
		input hrulrect:l,vrulrect:l,portrect:l,contentrect:l,pagemode:w
		output mindist:w

		local max:w,min:w,gran:w,i:w,length:w,space:w
		local tmp:w,j:w,k:w,v_line:l,h_line:l,start_offset:w
		local dist:l,tmp_dist:l,min_pix:l,h_pos:l,v_pos:l,pos:l
		local v_end:l,ruleraddr:l,Q_Line:w,inch_min:w
		local tmprect:r,foorect:r


		BEGIN

		_PenNormal

		cmpw vis_start,vis_end
		jeq EXIT

		stz Q_Line
		stz length
		stzl dist
		movelong #ruler_marks,ruleraddr
		call D_Set4Pat,in=(#LightYellow:w)


;		 ; if (vert)
		lda vert
		jeq ELSE1

;		 ; {
		moverect [vrulrect],tmprect
		moveword vis_start,tmprect
		moveword vis_end,tmprect+4
		tool _ClipRect,in=(!tmprect:l)
		tool _PaintRect,in=(!tmprect:l)

		tool _SetPenSize,in=(#2:w,#1:w)
		call D_Set4Pat,in=(#Black:w)

		moverect [portrect],foorect
		moveword [contentrect]:#4,foorect+4
		tool _ClipRect,in=(!foorect:l)
		subword [vrulrect]:#6,#2,s
		pushword #0
		_MoveTo
		tool _Line,in=(#0:w,#P_VRulLength:w)

		tool _ClipRect,in=(!tmprect:l)

		movelong !tmp_dist,v_pos
		movelong !pos,h_pos
		movelong !Q_Line,v_line
		movelong !length,h_line
		moveword #4,start_offset

		subword [vrulrect]:#6,#2,a
		movefixword a,pos
		movefixword #P_MIN_VPIX,min_pix

		brl ENDIF1
;		 ; } else {
ELSE1
		moverect [hrulrect],tmprect
		moveword vis_start,tmprect+2
		moveword vis_end,tmprect+6
		tool _ClipRect,in=(!tmprect:l)
		tool _PaintRect,in=(!tmprect:l)

		call D_Set4Pat,in=(#Black:w)
		moverect [portrect],foorect
		moveword [contentrect]:#6,foorect+6
		tool _ClipRect,in=(!foorect:l)

		tool _MoveTo,in=(tmprect+2:w,#P_HRulTop+P_HRulWdth-7:w)
		tool _Line,in=(#640:w,#0:w)

		tool _MoveTo,in=([vrulrect]:#2:w,#P_HRulTop+P_HRulWdth-1:w)
		tool _Line,in=(#640:w,#0:w)
		tool _ClipRect,in=(!tmprect:l)

		movelong !pos,v_pos
		movelong !tmp_dist,h_pos
		movelong !length,v_line
		movelong !Q_Line,h_line
		moveword #2,start_offset

		subword [hrulrect]:#4,#7,a
		movefixword a,pos

		movefixword #P_MIN_HPIX,min_pix

;		 ; }
ENDIF1	_PenNormal

;		   More inits.
;		 ; max = (short) Fix2Long(FixDiv(Int2Fix(vis_end-start),ppi))+1;

		spacelong
		subword vis_end,start,a
		pushfixword a
		pushlong ppi
		_FixDiv
		pulllong ax
		rcall P_Fix2Int,in=(ax:ax)
		inc a
		sta max



;		 ; min = (short) Fix2Long(FixDiv(Int2Fix(vis_start-start),ppi))-1;

		spacelong
		subword vis_start,start,a
		pushfixword a
		pushlong ppi
		_FixDiv
		pulllong ax
		rcall P_Fix2Int,in=(ax:ax)
		dec a
		sta min

;		 ; ----
		movefixword vis_end,v_end


;		 ; for (gran=7;(dist<ppi) && (gran>=0);gran--)
		moveword #7,gran
		brl FOR1

;		 ; {
LOOP1


;		   dist = ppi / (1<<gran);

		movelong ppi,dist
		bmi NegDist
		ldx gran
		bra DivLoop1
Div1	clc
		ror dist+2
		ror dist
		dex
DivLoop1	bne Div1
		bra doneDiv

NegDist	ldx gran
		bra DivLoop2
Div2	sec	 ; sign extension.
		ror dist+2
		ror dist
		dex
DivLoop2	bne Div2


DoneDiv

		scmpw dist+2,min_pix+2
		jlt CONT1
		bne NoCONT
		cmpw min_pix,dist
		jge CONT1

NoCONT
		pushfixword start

		tool _FixMul,in=(min:w,#0:w,ppi:l),out=(:l)
		addlong s,dist,tmp_dist
		addlong s,tmp_dist,tmp_dist

		clc
		rol dist
		rol dist+2

		lda gran
		asl a
		pha
		rcall P_LShift,in=(vert:a,#4:x)
		addword a,s,y
		moveword [ruleraddr]:y,length


;		 ;	 for (;tmp_dist<=v_end;)
		bra FOR0
;		 ;	 {
LOOP0

		rcall P_Fix2Int,in=([h_pos]:ax),out=(a:a)
		pha
		rcall P_Fix2Int,in=([v_pos]:ax),out=(a:a)
		pha

		_MoveTo

		tool _Line,in=([h_line]:w,[v_line]:w)

		addlong tmp_dist,dist,tmp_dist
FOR0	scmpw v_end+2,tmp_dist+2
		blt ENDFOR0
		jne LOOP0
		cmpw v_end,tmp_dist
		jge LOOP0

ENDFOR0
;		 ;	 }
CONT1	dec gran
FOR1	scmpw dist+2,ppi+2
		blt CHECK2
		bne ENDFOR1
		cmpw dist,ppi
		bge ENDFOR1

CHECK2	lda gran
		jpl LOOP1

;		 ; }
ENDFOR1



;		 ; if (vert)
		lda vert
		beq ELSE2

		lda #-11
		bra ENDIF2
ELSE2

		lda #-4
ENDIF2
		sta length


		movelong ppi,dist
		moveword #1,space

;		 ; while (dist < min_pix)
		bra WHILE2

;		 ; {
LOOP2
		clc
		rol dist
		rol dist+2

		asl space

;		 ; }
WHILE2	scmpw dist+2,min_pix+2
		blt LOOP2
		bne ENDWHILE2
		cmpw dist,min_pix
		blt LOOP2


ENDWHILE2
		moveword min,inch_min
		cmpw space,#1
		beq GotMin

;		   if (tmp = min/space, min != tmp*space) min = (tmp+1)*space
		tool _SDivide,in=(min:w,space:w),out=(a:w,tmp:w)
		beq GotMin

		lda tmp
		bmi IsNeg
		inc a
		bra GotTmp
IsNeg	dec a
GotTmp

		rcall D_FastMult,in=(a:x,space:y),out=(inch_min:a)

GotMin
;		 ; tmp_dist = FixMul(Int2Fix(inch_min),ppi) + Int2Fix(start)

		pushfixword start
		tool _FixMul,in=(inch_min:w,#0:w,ppi:l),out=(:l)
		addlong 1:s,5:s,tmp_dist
		ply
		ply
		ply
		ply

;		 ; for (i=inch_min;i<=max;i+=space,tmp_dist+=dist)
		moveword inch_min,i
		brl FOR3

;		 ; {
LOOP3
		rcall P_Fix2Int,in=([h_pos]:ax),out=(a:a)
		pha
		rcall P_Fix2Int,in=([v_pos]:ax),out=(a:a)
		pha

		_MoveTo

		tool _Line,in=([h_line]:w,[v_line]:w)

		addword i,space,i
		addlong tmp_dist,dist,tmp_dist
;		 ; }
FOR3	scmpw max,i
		jge LOOP3

ENDFOR3

		tool _SetForeColor,in=(#0:w)
		tool _SetBackColor,in=(#4:w)
		rcall D_SelectFont,in=(#0:a,#$0800:x,#0:y)

;		 ; if (vert)
		lda vert
		beq ELSE4
;		 ; {
		subword [vrulrect]:#6,#36,a
		movefixword a,pos
		movefixword #MIN_V_NPIX,min_pix

		bra ENDIF4
;		 ; } else {
ELSE4
		movefixword #H_NUM,pos
		movefixword #MIN_H_NPIX,min_pix

;		 ; }
ENDIF4

		movelong ppi,dist
		moveword #1,space


;		 ; while (dist < min_pix)
		bra WHILE5
LOOP5

		clc
		rol dist
		rol dist+2

		asl space

WHILE5	scmpw dist+2,min_pix+2
		blt LOOP5
		bne ENDWHILE5
		cmpw dist,min_pix
		blt LOOP5

ENDWHILE5



;		 ; for (i=0;i>=min;i-=space)
		lda #0
		sta i
		bra FOR6
LOOP6
		subword i,space,i
FOR6	scmpw a,min
		bge LOOP6


;		 ; tmp_dist = FixMul(ppi,Int2Fix(i)) + Int2Fix(start+start_offset);

		addword start,start_offset,s
		pushword #0
		tool _FixMul,in=(ppi:l,i:w,#0:w),out=(:l)
		addlong 1:s,5:s,tmp_dist
		ply
		ply
		ply
		ply

		lda pagemode
		bne ENDIF7

		addword space,space,a
		addword a,i,i
		addlong tmp_dist,dist,tmp_dist
		addlong tmp_dist,dist,tmp_dist


ENDIF7

;		 ; for (;i<=max;i+=space,tmp_dist+=dist)
		brl FOR8
LOOP8
		rcall P_Fix2Int,in=([h_pos]:ax),out=(a:a)
		pha
		rcall P_Fix2Int,in=([v_pos]:ax),out=(a:a)
		pha

		_MoveTo

		subword #0,i,a
		bpl GOTJ

		lda i
GOTJ	tool _UDivide,in=(a:w,#10:w),out=(j:w,k:w)


		lda k
		beq NO_K
		addword a,#'0',a
		pha
		_DrawChar
		bra DRAW_J

NO_K	lda vert
		beq DRAW_J
		tool _DrawChar,in=(#' ':w)
		tool _DrawChar,in=(#' ':w)

DRAW_J	addword j,#'0',a
		pha
		_DrawChar


		lda vert
		beq HorizScritch

		pushword #3
		pushword #0


		pushword #62
		rcall P_Fix2Int,in=([v_pos]:ax)
		subword a,#4,a
		pha

		bra CONT8
HorizScritch
		pushword #0
		pushword #1

		rcall P_Fix2Int,in=([h_pos]:ax)
		dec a
		dec a
		pha
		pushword #3

CONT8	_MoveTo
		_Line

		addword i,space,i
		addlong tmp_dist,dist,tmp_dist

FOR8	scmpw max,i
		jge LOOP8


ENDFOR8
EXIT

		lda vert
		bne gone

		call P_DrawTabs

gone	return
		ENDP



;----------------------------------------------------------
P_RulerData	PROC		EXPORT
		EXPORT	ruler_marks







;					  2	 8	32	128   th of an inch
;				   1  ;  4  ; 16  ; 64  ;
ruler_marks	DC.W	 -5,-4,-3,-2,-1,-1,-1,-1
	DC.W	-11,-8,-6,-4,-2,-2,-2,-2





;P_VisLMarg	 ds 2				; what are these?
;P_VisIndent	ds 2
;P_VisRMarg	 ds 2


		ENDP
		END

