;
; utils
;
; Various useful routines
;
;  1995-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's Sculptrix.
;
; Sculptrix is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Sculptrix is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Sculptrix.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

;----- Main code ------------------------------------------------------------

		AREA	|Module$$Code|,CODE,READONLY

; --- utils_strWidth ---
;
; On entry:	R0 == pointer to a string
;
; On exit:	R0 == width of the string in OS units
;
; Use:		Returns the width of a string, as it would be displayed in
;		an icon (i.e. taking into account things like the current
;		desktop font etc.)  The width is exact, so if you want to
;		e.g. draw a box round it, you'll have to add on a little
;		clearance at each end.  8 OS units seems to be a good size
;		for the clearance (so the total width you'd use is given by
;		utils_strWidth(string)+16, because it has two ends).
;
;		[Stolen and bodged from sapphire/wimp.s by mdw]

		EXPORT	utils_strWidth
utils_strWidth	ROUT

		; --- We need to find the string length anyway ---
		;
		; This length calculation is done inline to avoid a
		; dependency on string.  It's not very long, anyway, and
		; it *does* save a MOV R5,R0 :-)

		STMFD	R13!,{R1-R6,R14}	;Save a bunch of registers
		MOV	R5,#0			;Length counter starts at 0
		MOV	R6,R0			;Keep string pointer

00		LDRB	R14,[R0,R5]		;Get a byte from the string
		CMP	R14,#' '		;Is it a control char?
		ADDCS	R5,R5,#1		;Bump on *anyway* (see above)
		BCS	%b00			;If more to go, loop back

		; --- Now try to find the current font handle ---

		MOV	R0,#8			;Read Wimp font handle
		SWI	XWimp_ReadSysInfo	;Find out from WindowMangler
		MOVVS	R0,#0			;If nothing, assume no font
		CMP	R0,#0			;Is there there a real font?
		BEQ	%10utils_strWidth	;No -- skip ahead too

		; --- Now suss out the width of the string ---

		SWI	Font_SetFont		;Set up the desktop font
		MOV	R1,#1000		;A nice big round number
		MOV	R2,R1			;And another nice big number
		SWI	Font_Converttopoints	;Font_StringWidth is weird
		MOV	R3,R2			;Move coords to right regs
		MOV	R2,R1			;That means both of them
		MOV	R1,R6			;Retreive string pointer
		MOV	R4,#-1			;Don't split the string
		ADD	R5,R5,#1		;Move this counter on one
		SWI	Font_StringWidth	;Find the width of the string
		MOV	R1,R2			;Move coordinates back again
		MOV	R2,R3			;Again, that means both
		SWI	Font_ConverttoOS	;Convert to OS units now
		MOV	R0,R1			;Put the width in R0
		LDMFD	R13!,{R1-R6,PC}^	;Return to caller, then

		; --- Just normal system font ---

10		MOV	R0,R5,LSL #4		;Multiply by character width
		LDMFD	R13!,{R1-R6,PC}^	;Return to caller

		LTORG

;----- That's all, folks ----------------------------------------------------

		END
