Browse Source

More font tweaks.

Mark Sibly 7 years ago
parent
commit
8c02fdc2a5

+ 7 - 0
VERSIONS.TXT

@@ -1,4 +1,11 @@
 
+***** Monkey2 v1.1.12 *****
+
+Added AngelFont and ImageFont support. Font.Load tries to guess correct font type via file extension but you can use ImageFont.Load and AngelFont.Load directly if you want. Changed monochrome font texture format to I8.
+
+Fixed PATH growing when updating modules in ted2go.
+
+
 ***** Monkey2 v1.1.11 *****
 
 Major change to name munging for tmp files to make them shorter.

+ 4 - 0
modules/freetype/freetype.monkey2

@@ -217,11 +217,15 @@ Function FT_Get_Char_Index:FT_UInt( face:FT_Face,charcode:FT_ULong )
 
 Function FT_Get_First_Char:FT_ULong( face:FT_Face,agindex:FT_UInt Ptr )
 Function FT_Get_Next_Char:FT_ULong( face:FT_Face,char_code:FT_ULong,agindex:FT_UInt Ptr )
+	
+Function FT_Get_Kerning( face:FT_Face,left_glyph:FT_UInt,right_glyph:FT_UInt,kern_mode:FT_UInt,akerning:FT_Vector Ptr )
 
 Function FT_Load_Char:FT_Error( face:FT_Face,char_code:FT_ULong,load_flags:Int )
 Function FT_Load_Glyph:FT_Error( face:FT_Face,glyph_index:FT_UInt,load_flags:Int )
 Function FT_Render_Glyph:FT_Error( slot:FT_GlyphSlot,render_mode:Int )
 
+Function FT_HAS_KERNING:Int( face:FT_Face )
+	
 #rem
 '**** old *****
 

+ 4 - 4
modules/mojo/graphics/angelfont.monkey2

@@ -67,8 +67,6 @@ Class AngelFont Extends Font
 	
 	Function Load:AngelFont( path:String,shader:Shader=Null,textureFlags:TextureFlags=TextureFlags.FilterMipmap )
 		
-		If Not shader shader=Shader.Open( "font" )
-		
 		Local fnt:=LoadString( path,True )
 		If Not fnt
 			If Not ExtractRootDir( path ) fnt=LoadString( "font::"+path,True )
@@ -108,9 +106,11 @@ Class AngelFont Extends Font
 				
 				Local fpath:=dir+file.Slice( 1,-1 )
 				
-				Local pixmap:=Pixmap.Load( fpath,PixelFormat.A8,True )
+				Local pixmap:=Pixmap.Load( fpath,Null,True )
+				
+				Local pshader:=shader ?Else (pixmap.Format=PixelFormat.I8 ? Shader.Open( "font" ) Else Shader.Open( "sprite" ))
 				
-				Local image:=New Image( pixmap,textureFlags,shader )
+				Local image:=New Image( pixmap,textureFlags,pshader )
 				
 				If id>=pages.Length pages.Resize( id+1 )
 					

+ 3 - 3
modules/mojo/graphics/canvas.monkey2

@@ -1107,6 +1107,8 @@ Class Canvas
 			For Local i:=i0 Until i1
 				
 				Local char:=text[i]
+
+				tx+=_font.GetKerning( lastChar,char )	'add kerning before render
 			
 				Local g:=_font.GetGlyph( char )
 			
@@ -1125,10 +1127,8 @@ Class Canvas
 				AddVertex( x1,y1,s1,t1 )
 				AddVertex( x0,y1,s0,t1 )
 				
-				tx+=g.advance
+				tx+=g.advance							'add advance after render
 
-				tx+=_font.GetKerning( lastChar,char )
-				
 				lastChar=char
 			Next
 			

+ 18 - 2
modules/mojo/graphics/font.monkey2

@@ -62,6 +62,7 @@ Class Font Extends Resource
 	Method GetKerning:Float( firstChar:Int,secondChar:Int ) Virtual
 		Return 0
 	End
+	
 	#rem monkeydoc Measures the width of some text when rendered by the font.
 	#end
 	Method TextWidth:Float( text:String )
@@ -69,7 +70,7 @@ Class Font Extends Resource
 		Local w:=0.0,lastChar:=0
 
 		For Local char:=Eachin text
-			w+=GetGlyph( char ).advance+GetKerning( lastChar,char )
+			w+=GetKerning( lastChar,char )+GetGlyph( char ).advance
 			lastChar=char
 		Next
 		
@@ -80,7 +81,22 @@ Class Font Extends Resource
 	#end
 	Function Load:Font( path:String,size:Float,shader:Shader=Null,textureFlags:TextureFlags=TextureFlags.FilterMipmap )
 		
-		Return FreeTypeFont.Load( path,size,shader,textureFlags )
+		Local font:Font
+		
+		Select ExtractExt( path )
+		Case ".ttf",".otf",".fon"
+			font=FreeTypeFont.Load( path,size,shader,textureFlags )
+		Case ".fnt"
+			font=AngelFont.Load( path,shader,textureFlags )
+		Case ""
+			font=FreeTypeFont.Load( path+".ttf",size,shader,textureFlags )
+			If Not font 
+				font=FreeTypeFont.Load( path+".otf",size,shader,textureFlags )
+				If Not font font=FreeTypeFont.Load( path+".fon",size,shader,textureFlags )
+			Endif
+		End
+		
+		Return font
 	End
 	
 	Protected

+ 22 - 6
modules/mojo/graphics/freetypefont.monkey2

@@ -51,10 +51,22 @@ Class FreeTypeFont Extends Font
 		Return gpage.image
 	End
 	
-	Function Load:FreeTypeFont( path:String,height:Float,shader:Shader,textureFlags:TextureFlags )
-	
-		If Not shader shader=Shader.Open( "font" )
+	Method GetKerning:Float( firstChar:Int,secondChar:Int ) Override
+		
+		If Not _hasKerning Return 0
+
+		Local firstGlyph:=FT_Get_Char_Index( _face,firstChar )
+		Local secondGlyph:=FT_Get_Char_Index( _face,secondChar )
 		
+		Local delta:FT_Vector
+		
+		FT_Get_Kerning( _face,firstGlyph,secondGlyph,0,Varptr delta )
+		
+		Return delta.x Shr 6
+	End
+	
+	Function Load:FreeTypeFont( path:String,height:Float,shader:Shader=Null,textureFlags:TextureFlags=TextureFlags.FilterMipmap )
+	
 		If Not FreeType And FT_Init_FreeType( Varptr FreeType ) Return Null
 		
 		Local data:=DataBuffer.Load( path )
@@ -69,6 +81,8 @@ Class FreeTypeFont Extends Font
 			Return Null
 		Endif
 		
+		If Not shader shader=Shader.Open( "font" )
+		
 		Local font:=New FreeTypeFont( data,face,height,shader,textureFlags )
 		
 		Return font
@@ -97,6 +111,7 @@ Class FreeTypeFont Extends Font
 	Field _face:FT_Face
 	Field _shader:Shader
 	Field _textureFlags:TextureFlags
+	Field _hasKerning:Bool
 	
 	Field _height:Int
 	Field _ascent:Int
@@ -149,7 +164,7 @@ Class FreeTypeFont Extends Font
 		texw=1 Shl Int( Ceil( Log2( texw ) ) )
 		texh=1 Shl Int( Ceil( Log2( texh ) ) )
 		
-		Local pixmap:=New Pixmap( texw,texh,PixelFormat.A8 )
+		Local pixmap:=New Pixmap( texw,texh,PixelFormat.I8 )
 		pixmap.Clear( Color.None )
 	
 		Local glyphs:=New Glyph[numChars],glyph:Glyph,nullGlyph:Glyph
@@ -170,7 +185,7 @@ Class FreeTypeFont Extends Font
 			Local gw:=Int( slot->bitmap.width )
 			Local gh:=Int( slot->bitmap.rows )
 	
-			Local tmp:=New Pixmap( gw,gh,PixelFormat.A8,slot->bitmap.buffer,slot->bitmap.pitch )
+			Local tmp:=New Pixmap( gw,gh,PixelFormat.I8,slot->bitmap.buffer,slot->bitmap.pitch )
 			
 			If tx+gw+1>pixmap.Width
 				ty+=maxh
@@ -204,7 +219,8 @@ Class FreeTypeFont Extends Font
 		_face=face
 		_shader=shader
 		_textureFlags=textureFlags
-
+		_hasKerning=FT_HAS_KERNING( _face )
+		
 		Local size_req:FT_Size_RequestRec
 		
 		size_req.type=FT_SIZE_REQUEST_TYPE_REAL_DIM

+ 3 - 1
modules/mojo/graphics/imagefont.monkey2

@@ -73,8 +73,10 @@ Class ImageFont Extends Font
 			If Not ExtractRootDir( path ) pixmap=Pixmap.Load( "font::"+path,Null,True )
 			If Not pixmap Return Null
 		Endif
+		
+		Local pshader:=shader ?Else (pixmap.Format=PixelFormat.I8 ? Shader.Open( "font" ) Else Shader.Open( "sprite" ))
 
-		Local font:=New ImageFont( pixmap,charWidth,charHeight,firstChar,numChars,padding,shader,textureFlags )
+		Local font:=New ImageFont( pixmap,charWidth,charHeight,firstChar,numChars,padding,pshader,textureFlags )
 		
 		Return font
 	End

+ 1 - 1
modules/mojo/graphics/shaders/font.glsl

@@ -29,7 +29,7 @@ uniform sampler2D m_ImageTexture0;
 
 void main(){
 
-	float alpha=texture2D( m_ImageTexture0,v_TexCoord0 ).a;
+	float alpha=texture2D( m_ImageTexture0,v_TexCoord0 ).r;
 
 #if MX2_RENDERPASS==0