REM ***********************************************
REM Title: text trim
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
REM Description: 
REM
REM When creating a scrollable window which displays text, it becomes 
REM necessary to clip the text as itis scrolled horizontally. An easy 
REM and common solution is to determine how much of the text is scrolled 
REM beyond the window and divided that number of pixels by 16 (DB's default 
REM text size per character).
REM That is ok if you're using the default text setup in DB which uses a fixed
REM width for characters, but the moment you change to another font, that method
REM will no longer work because not all characters have the same width. The
REM method demonstrated below corrects this issue to work with any type of font.
 
 
rem some text to display
dim stuff$(5)
stuff$(1) = "Compilers are percieved to be magical artifacts, carefully crafted by the"
stuff$(2) = "wizards, and unfathomable by the mere mortals. This paper attempts to"
stuff$(3) = "dispel this myth. We build a simple compiler for a simple language in a"
stuff$(4) = "step-by-step fashion. The input language accepted by the compiler starts"
stuff$(5) = "minimal, and grows as our knowledge of how to build compilers grows."
 
 
rem coordinates to display text
tx = 20
ty = 80
 
set text font "Arial"
 
sync on
 
DO
	cls
 
	mx = mousex()
 
	for i = 1 to 5
		rem how many pixels is the text displayed beyond our limit
		trimInPixels = mx-tx
		rem how many characters does that translate into?
		c = calcTextTrimAmount(stuff$(i), trimInPixels)
		rem trim that number of characters from the beginning of the string
		s$ = right$(stuff$(i), len(stuff$(i))-c)
		rem get the trimmed characters no longer to be displayed
		sb$ = left$(stuff$(i), c)
		rem to keep text at the same position despite having characters
		rem trimmed from the left(start of the string), determine offset amount
		offsetX = text width(sb$)
		rem display newly trimmed text
		text tx+offsetX, ty+(i*16), s$
	next i
 
	rem draw a boundary line
	rem text will not be displayed before this line 
	line mx, 1, mx, screen height()
 
	sync
LOOP
 
REM **************************************************
REM Calculates how many characters need to be trimmed
REM from the start of the string equally 'width'
REM **************************************************
function calcTextTrimAmount(s$,width)
	L = len(s$)
	for i = 1 to L
		r = text width(left$(s$,i))
		if r > width then exitfunction i-1
	next i
endfunction L