Rem
The
monospaced font class by Oxid
http://blitzmax.3dn.ru
EndRem
Local text$ = "A regular width font"
Graphics 800 , 600 , , 60
'load font
Local font:TImageFont_FixedWidth = LoadFixedWidthFont( "Courier_10px_regular.bmp", 32,126,8,12 )
'activate font
SetImageFont font
'calc text width in pixels
Local mx = TextWidth(text)
Flip
DrawText( "nvnsdvuisdvuis", 100, 100 )
While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
Cls
DrawText( text, (GraphicsWidth()-mx)/2, 100 )
Flip
Wend
End
Type TImageFont_FixedWidth Extends TImageFont
'Field _glyphs:TImageGlyph[]
Field _height#
Field _char_start%
Field _char_end%
Field _char_width#
Method Style%()
Return 0
End Method
Method Height%()
Return _height
End Method
Method CountGlyphs%()
Return _glyphs.length
End Method
Method CharToGlyph%( char% )
If char >= _char_start And char <= _char_end Return char-_char_start
Return -1
End Method
Method LoadGlyph:TImageGlyph( index% )
Assert index >= 0 And index <
_glyphs.length
Return _glyphs[index]
End Method
Method Draw( text$,x#,y#,ix#,iy#,jx#,jy# )
For Local i%=0 Until text.length
Local n%=CharToGlyph( text[i] )
If n% < 0 Continue
Local glyph:TImageGlyph=LoadGlyph(n)
Local image:TImage=glyph._image
If image
Local frame:TImageFrame=image.Frame(0)
If frame
Local tx#=glyph._x*ix+glyph._y*iy
Local ty#=glyph._x*jx+glyph._y*jy
frame.Draw
0,0,image.width,image.height,x+tx,y+ty
EndIf
EndIf
x:+glyph._advance*ix
y:+glyph._advance*jx
Next
End Method
End Type
Function LoadFixedWidthFont:TImageFont_FixedWidth(
url:Object, char_start%=32, char_end%=126, char_width%=8, char_height%=12, flags%=-1 )
Local font:TImageFont_FixedWidth = New TImageFont_FixedWidth
font._char_start
= char_start
font._char_end
= char_end
Local length% = font._char_end -
font._char_start + 1
Assert length > 0
font._char_width
= char_width
font._height
= char_height
Local img:TImage = LoadAnimImage(url, char_width, char_height, 0, length, flags)
Assert img
font._glyphs
= New TImageGlyph[length]
For Local i%=0 Until length
Local gliph:TImageGlyph = New TImageGlyph
gliph._image
= LoadImage( img.pixmaps[i], flags )
gliph._advance
= char_width
gliph._x
= 0
gliph._y
= 0
gliph._w
= char_width
gliph._h
= char_height
font._glyphs[i]=gliph
Next
Return font
End Function |