Code archives/Graphics/fixedmap font

This code has been declared by its author to be Public Domain code.

Download source code

fixedmap font by Jesse2010
this is for those who keep on complaining that BlitzMax font module is slow.
this program copies the loaded glyph images and makes copies of equal size images and display them directly or rotated into the display. there are two ways to display them.
if you are planning to use rotation and/or scaling use the draw method "fast"
else if you are not planning to use "SetRotation" to display them use the DrawFixed method "faster".
Type TBitMap
	Field image		:TImage
	Field gx			:Int
	Field gy			:Int
	Field advance		:Int
	Function Create:TBitMap(image:TImage,x:Float,y:Float,adv:Int,width:Int,height:Int)
		
		If width  = 0 Return Null 
		If height = 0  Return Null
		Local b:TbitMap = New TBitMap
		If image = Null
			b.gx = x
			b.gy = y
			b.advance = adv
			Return b	
		EndIf
		
		
		Local from:TPixmap = image.lock(0,True,False)
		
		b.image = CreateImage(width,height)
		b.gx = x
		b.gy = y
		b.advance = adv
		
		Local inTo:TPixmap = b.image.lock(0,True,True)
		into.ClearPixels(0)
		Local w:Int
		Local h:Int
		If into.width  >= from.width+x Then  w = from.width  Else w = inTo.width - x
		If into.height >= from.height+x Then h = from.height Else h = inTo.height- y

		For Local d:Int = 0 Until h
			For Local a:Int = 0  Until w
					Local n:Int = from.ReadPixel(a,d)
					inTo.WritePixel(a+x+2,d+y+2,n)
			Next
		Next

		Return b
		
	End Function
	
	
	Method Draw(x:Float,y:Float)
		If image DrawImage image,x,y
	End Method
	
End Type		

Type TBitMapFont
	
	Field bitMap		:Tbitmap[96]
	Field gfx			:TMax2dGraphics
	
	Function Create:TBitmapFont(url:String,size:Int)
		Local oldFont:TImageFont = GetImageFont()
		Local imgFont:TImageFont=LoadImageFont(url,size)
		Local b:TbitMapFont = New TbitMapFont
		If imgfont=Null Return Null
		SetImageFont(imgfont)
		
		b.gfx = tmax2dgraphics.Current()
		
		For Local i:Int = 0 Until 96
			Local n:Int = imgFont.CharToGlyph(i+32)
			If n < 0 Continue
			Local glyph:TImageGlyph = imgFont.LoadGlyph(n)
			If glyph
				b.bitMap[i] = TBitMap.Create(glyph._image,glyph._x,glyph._y,glyph._advance,size*2,size*2)			
			Else
			 	b.bitmap[i] = New TBitMap
			EndIf
		Next
		SetImageFont(oldFont)
		Return b
	End Function
	
	Method draw(text:String,x:Float,y:Float)
		For Local i:Int = 0 Until text.length
			Local a:Int = text[i]-32
			Local bm:TBitMap = bitmap[a]
			bm.Draw(x,y)
			x :+ bm.advance * gfx.tform_ix
			y :+ bm.advance * gfx.tform_jx
		Next
	End Method
	
	Method drawFixed(text:String,x:Float,y:Float)
		For Local i:Int = 0 Until text.length
			Local a:Int = text[i]-32
			bitmap[a].draw(x,y)
			x:+bitmap[a].advance
		Next
			
	End Method
End Type

Comments

Jesse2010
or this is if you want to display the blitzmax font images directly. again fast and faster
SuperStrict
Graphics 800,600
SetBlend Alphablend
Local myfont:TBitmapFont = TBitmapFont.Create(GetEnv_("systemroot")+"\fonts\COURBD.TTF",32)
myfont.drawfixed("this is a drawfixed test no rotation",100,100)
myfont.draw("this is a draw  test no rotation",100,150)
SetRotation 30
myfont.drawfixed("this is a drawfixed test rotated",100,200)
myfont.draw("this is a draw test rotated",100,250)
Flip()
WaitKey()

Type TBitMap
	Field gx			:Int
	Field gy			:Int
	Field gImg			:TImage
	Field advance		:Int
	Function Create:TBitMap(image:TImage,x:Float,y:Float,adv:Int)
		
		Local b:TbitMap = New TBitMap
		b.gImg = image
		b.gx = x
		b.gy = y
		b.advance = adv
		Return b
	End Function
	
	Method Draw(x:Float,y:Float)
			If gImg DrawImage gImg,x,y
	End Method	
End Type		

Type TBitMapFont
	
	Field bitMap		:Tbitmap[96]
	Field gfx			:TMax2dGraphics
	
	Function Create:TBitmapFont(url:String,size:Int)
		Local oldFont:TImageFont = GetImageFont()
		Local imgFont:TImageFont=LoadImageFont(url,size)
		Local b:TbitMapFont = New TbitMapFont
		If imgfont=Null Return Null
		SetImageFont(imgfont)
		
		b.gfx = tmax2dgraphics.Current()
		
		For Local i:Int = 0 Until 96
			Local n:Int = imgFont.CharToGlyph(i+32)
			If n < 0 Continue
			Local glyph:TImageGlyph = imgFont.LoadGlyph(n)
			If glyph
				b.bitMap[i] = TBitMap.Create(glyph._image,glyph._x,glyph._y,glyph._advance)			
			Else
			 	b.bitmap[i] = New TBitMap
			EndIf
		Next
		SetImageFont(oldFont)
		Return b
	End Function
	
	Method draw(text:String,x:Float,y:Float)
		For Local i:Int = 0 Until text.length
			Local bm:TBitMap = bitmap[text[i]-32]
			Local tx:Float = bm.gx * gfx.tform_ix + bm.gy * gfx.tform_iy
			Local ty:Float = bm.gx * gfx.tform_jx + bm.gy * gfx.tform_jy 
			bm.Draw(x+tx,y+ty)
			x :+ bm.advance * gfx.tform_ix
			y :+ bm.advance * gfx.tform_jx
		Next
	End Method
	
	Method drawfixed(text:String,x:Float,y:Float)
		For Local i:Int = 0 Until text.length
			Local bm:TBitMap = bitmap[text[i]-32]
			bm.Draw(x+bm.gx,y+bm.gy)
			x :+ bm.advance
		Next
	End Method

End Type



Hezkore2011
Why isn't Field bitMap:Tbitmap[96] extended to 256?
With a length of only 96, our Swedish ÅÄÖ aren't supported.
Extended Ascii is quite useful you know. ;)
http://www.asciitable.com/


Jesse2011
I know. I am horrible. I just picked the most common english standard codes. :)

you can even extend it to uni-code if the font support it. Everything is there. ;).
simple enough.


Code Archives Forum