
; Util to convert sources generated by FontMaster from Seka to Devpac format.

; Source © M.Meany Jan 91

		incdir		"sys:include/"
		include		"exec/exec_lib.i"
		include		"exec/exec.i"
		include		"libraries/dos.i"
		include		"libraries/dosextens.i"
		include		"source9:include/arpbase.i"

;*****************************************

CALLSYS    MACRO		;added CALLSYS macro - using CALLARP
	IFGT	NARG-1       	;CALLINT etc can slow code down and  
	FAIL	!!!         	;waste a lot of memory  S.M. 
	ENDC                 
	JSR	_LVO\1(A6)
	ENDM
		
*****************************************************************************

;** OPENARP moved to front as it will print a message on the CLI then **
;**   return to easystart if it can't find the ARP library ,we don't  **
;**                need to do any error checking of our own           **

start		OPENARP				;use arp's own open macro
		movem.l		(sp)+,d0/a0	;restore d0 and a0 as the
						;the macro leaves these on
						;the stack causing corrupt stack
		move.l		a6,_ArpBase	;store arpbase
		
;--------------	the ARP library opens and uses the graphics and intuition 
;		libs and it is quite legal for us to get these bases for 
;		our own use

		move.l		IntuiBase(a6),_IntuitionBase
		move.l		GfxBase(a6),_GfxBase

;--------------	Store pointer to and length of supplied filename

		move.l		a0,filename		store ptr to filename
		move.l		d0,d4			store name length

;--------------	Zero terminate filename supplied

		move.b		#0,-1(a0,d0)

;--------------	Get CLI window handle

		CALLARP		Output
		move.l		d0,CLI_out
		
;--------------	Display opening message

		move.l		d0,d1
		move.l		#opener,d2
		move.l		#opener_len,d3
		CALLSYS		Write
		
;--------------	Check for usage or no filename

		move.l		filename,a0
		cmpi.b		#'?',(a0)
		beq		usage
		
		tst.b		(a0)
		beq		usage
				
;-------------- Reserve some memory for Devpac font file name

		move.l		d4,d0
		addq.l		#5,d0
		move.l		#MEMF_PUBLIC,d1
		CALLSYS		ArpAllocMem
		move.l		d0,newfilename
		beq		error1

;--------------	Create name of devpac font file from filename supplied

; Copy exsisting filename

		move.l		filename,a0
		move.l		d0,a1
		subq.l		#2,d4
makenewname	move.b		(a0)+,(a1)+
		dbra		d4,makenewname

; Add _DEV extension to Devpac filename

		lea		extension,a0
		moveq.l		#4,d4
addextension	move.b		(a0)+,(a1)+
		dbra		d4,addextension

;--------------	Reserve some memory for file buffer

		move.l		#20000,d0
		move.l		#MEMF_PUBLIC,d1
		CALLSYS		ArpAllocMem
		move.l		d0,buffer
		beq		error1

;--------------	Reserve some memory for copy buffer

		move.l		#20000,d0
		move.l		#MEMF_PUBLIC,d1
		CALLSYS		ArpAllocMem
		move.l		d0,copy_buffer
		beq		error1

;--------------	Open the font file

		move.l		filename,d1
		move.l		#MODE_OLDFILE,d2
		CALLSYS		Open
		move.l		d0,In_hd
		beq		error_INFILE

;--------------	Read in all data

		move.l		d0,d1
		move.l		buffer,d2
		move.l		#20000,d3
		CALLSYS		Read
		move.l		d0,d5

;--------------	Close the font file

		move.l		In_hd,d1
		CALLSYS		Close

;--------------	Open the new font file for devpac

		move.l		newfilename,d1
		move.l		#MODE_NEWFILE,d2
		CALLSYS		Open
		move.l		d0,Out_hd
		beq		error_OUTFILE

;--------------	Write a short message to this file

		move.l		Out_hd,d1
		move.l		#message,d2
		moveq.l		#m_len,d3
		CALLSYS		Write

;--------------	Init buffer pointers, counter and some constants

		move.l		buffer,a0
		move.l		copy_buffer,a1
		subq.l		#2,d5
		moveq.l		#$0a,d1
		moveq.l		#$09,d2

;--------------	Create Devpac font

; Branch if not a zero byte

loop		tst.b		(a0)+
		bne.s		.ok

; If we get to here then current byte is a 0. This being the case we must
;insert a line feed and tab character

		move.b		d1,(a1)+
		move.b		d2,(a1)+
		bra		.ignore
	
; Copy current char, decrease counter and loop back if not finished

.ok		move.b		-1(a0),(a1)+
.ignore		dbra		d5,loop

;--------------	Source converted so write it to new font file
		
		move.l		Out_hd,d1
		move.l		copy_buffer,d2
		move.l		a1,d3
		sub.l		d2,d3
		CALLSYS		Write

;--------------	Close new font file

		move.l		Out_hd,d1
		CALLSYS		Close

;-------------- Close ARP library

error1		move.l		_ArpBase,a1
		CALLEXEC	CloseLibrary

;-------------- All done so quit

		rts

********************************************
; USAGE ROUTINE
********************************************

usage		move.l		CLI_out,d1
		move.l		#usage_msg,d2
		move.l		#usage_len,d3
		CALLSYS		Write
		
		bra		error1
		
********************************************
; ERROR ROUTINES
********************************************

;--------------	Could not open specified file so display error message

error_INFILE	move.l		CLI_out,d1
		move.l		#error_msg1,d2
		move.l		#msg1_len,d3
		CALLSYS		Write
		
		bra		error1

;--------------	Could not open new font file so display error message

error_OUTFILE	move.l		CLI_out,d1
		move.l		#error_msg2,d2
		move.l		#msg2_len,d3
		CALLSYS		Write
		
		bra		error1

********************************************
; VARIABLES AND STRINGS
********************************************

_ArpBase	dc.l		0
_GfxBase	dc.l		0
_IntuitionBase	dc.l		0

In_hd		dc.l		0
Out_hd		dc.l		0
CLI_out		dc.l		0

filename	dc.l		0

newfilename	dc.l		0

buffer		dc.l		0
copy_buffer	dc.l		0

extension	dc.b		'_DEV',0
		even
		
newline		dc.b		$0a,$09
		even
		
message		dc.b		'; Converted font, brought to you by :'
		dc.b		' M.Meany',$0a,'; Devpac Rules OK !'
		dc.b		$0a,$0a,$09
m_len		equ		*-message
		even

usage_msg	dc.b		'Converts the source generated by FontMaster'
		dc.b		' from Seka to Devpac format.',$0a,$0a
		dc.b		'Call from the CLI as follows: '
		dc.b		$09,'FontConvert <filename>',$0a,$0a
		dc.b		'Where <filename> is the name of source to '
		dc.b		'convert.',$0a,$0a,$09
		dc.b		'EG. 1>fontconvert df1:myfont.s',$0a,$0a
		dc.b		'A file will be created on df1: called '
		dc.b		'myfont.s_DEV that will contain',$0a
		dc.b		'the Devpac compatable source. The _DEV '
		dc.b		'extension is for your convenience !'
		dc.b		$0a,$0a
usage_len	equ		*-usage_msg
		even

opener		dc.b		$9b,'3;33;40m'
		dc.b		' FontConvert © M.Meany 1991.',$0a,$0a
		dc.b		$9b,'0;31;40m'
opener_len	equ		*-opener
		even

error_msg1	dc.b		'**** Aborted: Could Not Open Specified File'
		dc.b		' !',$0a
msg1_len	equ		*-error_msg1
		even

error_msg2	dc.b		'**** Aborted: Could Not Create New File'
		dc.b		' !',$0a
msg2_len	equ		*-error_msg2
		even


