浏览代码

[Brl.Font/Brl.FreeTypeFont] Allow fractional front sizes (#362)

Co-authored-by: Ronny Otto <[email protected]>
Ronny Otto 4 月之前
父节点
当前提交
201d9d2263
共有 2 个文件被更改,包括 17 次插入8 次删除
  1. 2 2
      font.mod/font.bmx
  2. 15 6
      freetypefont.mod/freetypefont.bmx

+ 2 - 2
font.mod/font.bmx

@@ -63,7 +63,7 @@ End Type
 Type TFontLoader
 	Field _succ:TFontLoader
 
-	Method LoadFont:TFont( url:Object,size:Int,style:Int ) Abstract
+	Method LoadFont:TFont( url:Object,size:Float,style:Int ) Abstract
 
 End Type
 
@@ -79,7 +79,7 @@ Function AddFontLoader( loader:TFontLoader )
 	_loaders=loader
 End Function
 
-Function LoadFont:TFont( url:Object,size:Int,style:Int=SMOOTHFONT )
+Function LoadFont:TFont( url:Object,size:Float,style:Int=SMOOTHFONT )
 
 	Local loader:TFontLoader=_loaders
 	

+ 15 - 6
freetypefont.mod/freetypefont.bmx

@@ -173,7 +173,7 @@ Type TFreeTypeFont Extends BRL.Font.TFont
 		Throw "Not supported"
 	End Method
 	
-	Function Load:TFreeTypeFont( src:Object,size:Int,style:Int )
+	Function Load:TFreeTypeFont( src:Object,size:Float,style:Int )
 
 		Local buf:Byte[]
 				
@@ -198,7 +198,7 @@ Type TFreeTypeFont Extends BRL.Font.TFont
 	
 	End Function
 
-	Function LoadFace:Byte Ptr( src:Object,size:Int,style:Int, buf:Byte[] Var )
+	Function LoadFace:Byte Ptr( src:Object,size:Float,style:Int, buf:Byte[] Var )
 
 		Global ft_lib:Byte Ptr
 		
@@ -265,10 +265,19 @@ Type TFreeTypeFont Extends BRL.Font.TFont
 		Else
 			Return Null
 		End If
-		
+
+
+		' Freetype's char height is "FreeType 26.6 Fixed-Point"
+		' -> 26 bit for the integer part, 6 bit for the decimal places
+		' -> "64" equals to 1.0 in floating point numbers
+		' So the module only support fractional sizes with a detail of 1/64
 		While size
-			If Not FT_Set_Pixel_Sizes( ft_face,0,size ) Exit
-			size:-1
+			'default DPI is 72 (setting 0,0 will use that)
+			If Not FT_Set_Char_Size( ft_face, 0, LongInt(size * 64), 0,0 ) Then Exit
+			' If it failed, ensure to use only integer sizes now
+			' (eg. a bitmap font was tried to get loaded) 
+			' First try will be the next integer (10.5 -> 10)
+			size = Ceil(size) - 1
 		Wend
 		If Not size 
 			FT_Done_Face ft_face
@@ -282,7 +291,7 @@ End Type
 
 Type TFreeTypeFontLoader Extends TFontLoader
 
-	Method LoadFont:TFreeTypeFont( url:Object,size:Int,style:Int ) Override
+	Method LoadFont:TFreeTypeFont( url:Object,size:Float,style:Int ) Override
 	
 		Return TFreeTypeFont.Load( url,size,style )