|
@@ -18,36 +18,94 @@ Public
|
|
#rem monkeydoc @hidden
|
|
#rem monkeydoc @hidden
|
|
#end
|
|
#end
|
|
Class FreeTypeFont Extends Font
|
|
Class FreeTypeFont Extends Font
|
|
|
|
+
|
|
|
|
+ Method GetGlyph:Glyph( char:Int ) Override
|
|
|
|
+
|
|
|
|
+ Local page:=char Shr 8
|
|
|
|
+ If page<0 Or page>=_pages.Length Return _nullGlyph
|
|
|
|
|
|
- Function Load:FreeTypeFont( path:String,fheight:Float,shader:Shader,textureFlags:TextureFlags )
|
|
|
|
-
|
|
|
|
- Local ext:=ExtractExt( path )
|
|
|
|
- If Not ext
|
|
|
|
- Local font:=Load( path+".otf",fheight,shader,textureFlags )
|
|
|
|
- If Not font font=Load( path+".ttf",fheight,shader,textureFlags )
|
|
|
|
- If Not font font=Load( path+".fon",fheight,shader,textureFlags )
|
|
|
|
- Return font
|
|
|
|
- Endif
|
|
|
|
|
|
+ Local gpage:=_pages[page]
|
|
|
|
+ If Not gpage Return _nullGlyph
|
|
|
|
+
|
|
|
|
+ If Not gpage.image LoadGlyphPage( page,gpage )
|
|
|
|
+
|
|
|
|
+ Local index:=char & 255
|
|
|
|
+ If index>=gpage.glyphs.Length Return _nullGlyph
|
|
|
|
+
|
|
|
|
+ Return gpage.glyphs[index]
|
|
|
|
+ End
|
|
|
|
+
|
|
|
|
+ Method GetGlyphPage:Image( char:Int ) Override
|
|
|
|
+
|
|
|
|
+ Local page:=char Shr 8
|
|
|
|
+ If page<0 Or page>=_pages.Length Return Null
|
|
|
|
+
|
|
|
|
+ Local gpage:=_pages[page]
|
|
|
|
+ If Not gpage Return Null
|
|
|
|
+
|
|
|
|
+ If Not gpage.image LoadGlyphPage( page,gpage )
|
|
|
|
+
|
|
|
|
+ Local index:=char & 255
|
|
|
|
+ If index>=gpage.glyphs.Length Return Null
|
|
|
|
+
|
|
|
|
+ Return gpage.image
|
|
|
|
+ End
|
|
|
|
|
|
|
|
+ Function Load:FreeTypeFont( path:String,height:Float,shader:Shader,textureFlags:TextureFlags )
|
|
|
|
+
|
|
|
|
+ If Not shader shader=Shader.Open( "font" )
|
|
|
|
+
|
|
If Not FreeType And FT_Init_FreeType( Varptr FreeType ) Return Null
|
|
If Not FreeType And FT_Init_FreeType( Varptr FreeType ) Return Null
|
|
|
|
|
|
Local data:=DataBuffer.Load( path )
|
|
Local data:=DataBuffer.Load( path )
|
|
- If Not data Return Null
|
|
|
|
|
|
+ If Not data
|
|
|
|
+ If Not ExtractRootDir( path ) data=DataBuffer.Load( "font::"+path )
|
|
|
|
+ If Not data Return Null
|
|
|
|
+ Endif
|
|
|
|
|
|
Local face:FT_Face
|
|
Local face:FT_Face
|
|
- If FT_New_Memory_Face( FreeType,data.Data,data.Length,0,Varptr face )
|
|
|
|
|
|
+ If FT_New_Memory_Face( FreeType,data.Data,data.Length,0,Varptr face )
|
|
data.Discard()
|
|
data.Discard()
|
|
Return Null
|
|
Return Null
|
|
Endif
|
|
Endif
|
|
|
|
|
|
- Local font:=New FreeTypeFont( data,face,fheight,shader,textureFlags )
|
|
|
|
|
|
+ Local font:=New FreeTypeFont( data,face,height,shader,textureFlags )
|
|
|
|
|
|
Return font
|
|
Return font
|
|
End
|
|
End
|
|
|
|
|
|
Protected
|
|
Protected
|
|
|
|
|
|
- Method OnLoadGlyphPage( page:Int,gpage:GlyphPage ) Override
|
|
|
|
|
|
+ Method OnDiscard() Override
|
|
|
|
+
|
|
|
|
+ FT_Done_Face( _face )
|
|
|
|
+
|
|
|
|
+ _data.Discard()
|
|
|
|
+
|
|
|
|
+ _data=Null
|
|
|
|
+ _face=Null
|
|
|
|
+ End
|
|
|
|
+
|
|
|
|
+ Private
|
|
|
|
+
|
|
|
|
+ Class GlyphPage
|
|
|
|
+ Field image:Image
|
|
|
|
+ Field glyphs:Glyph[]
|
|
|
|
+ End
|
|
|
|
+
|
|
|
|
+ Field _data:DataBuffer
|
|
|
|
+ Field _face:FT_Face
|
|
|
|
+ Field _shader:Shader
|
|
|
|
+ Field _textureFlags:TextureFlags
|
|
|
|
+
|
|
|
|
+ Field _height:Int
|
|
|
|
+ Field _ascent:Int
|
|
|
|
+
|
|
|
|
+ Field _pages:GlyphPage[]
|
|
|
|
+
|
|
|
|
+ Field _nullGlyph:Glyph
|
|
|
|
+
|
|
|
|
+ Method LoadGlyphPage( page:Int,gpage:GlyphPage )
|
|
|
|
|
|
Const MaxTexWidth:=1024
|
|
Const MaxTexWidth:=1024
|
|
|
|
|
|
@@ -94,7 +152,7 @@ Class FreeTypeFont Extends Font
|
|
Local pixmap:=New Pixmap( texw,texh,PixelFormat.A8 )
|
|
Local pixmap:=New Pixmap( texw,texh,PixelFormat.A8 )
|
|
pixmap.Clear( Color.None )
|
|
pixmap.Clear( Color.None )
|
|
|
|
|
|
- Local glyphs:=New Glyph[numChars],nullGlyph:Glyph
|
|
|
|
|
|
+ Local glyphs:=New Glyph[numChars],glyph:Glyph,nullGlyph:Glyph
|
|
|
|
|
|
tx=0;ty=0;maxh=0
|
|
tx=0;ty=0;maxh=0
|
|
|
|
|
|
@@ -103,7 +161,7 @@ Class FreeTypeFont Extends Font
|
|
If i<0
|
|
If i<0
|
|
If FT_Load_Char( _face,0,FT_LOAD_RENDER|FT_LOAD_FORCE_AUTOHINT ) FontError()
|
|
If FT_Load_Char( _face,0,FT_LOAD_RENDER|FT_LOAD_FORCE_AUTOHINT ) FontError()
|
|
Else
|
|
Else
|
|
- If Not FT_Get_Char_Index( _face,firstChar+i ) Or FT_Load_Char( _face,firstChar+i,FT_LOAD_RENDER|FT_LOAD_FORCE_AUTOHINT )
|
|
|
|
|
|
+ If Not FT_Get_Char_Index( _face,firstChar+i ) Or FT_Load_Char( _face,firstChar+i,FT_LOAD_RENDER|FT_LOAD_FORCE_AUTOHINT )
|
|
glyphs[i]=nullGlyph
|
|
glyphs[i]=nullGlyph
|
|
Continue
|
|
Continue
|
|
Endif
|
|
Endif
|
|
@@ -124,7 +182,9 @@ Class FreeTypeFont Extends Font
|
|
|
|
|
|
tmp.Discard()
|
|
tmp.Discard()
|
|
|
|
|
|
- Local glyph:=New Glyph( New Recti( tx,ty,tx+gw,ty+gh ),New Vec2f( slot->bitmap_left,_ascent-slot->bitmap_top ),slot->advance.x Shr 6 )
|
|
|
|
|
|
+ glyph.rect=New Recti( tx,ty,tx+gw,ty+gh )
|
|
|
|
+ glyph.offset=New Vec2f( slot->bitmap_left,_ascent-slot->bitmap_top )
|
|
|
|
+ glyph.advance=slot->advance.x Shr 6
|
|
|
|
|
|
If i>=0 glyphs[i]=glyph Else nullGlyph=glyph
|
|
If i>=0 glyphs[i]=glyph Else nullGlyph=glyph
|
|
|
|
|
|
@@ -138,28 +198,6 @@ Class FreeTypeFont Extends Font
|
|
' Print "Loading glyph page "+page+", image size="+gpage.image.Rect.Size
|
|
' Print "Loading glyph page "+page+", image size="+gpage.image.Rect.Size
|
|
End
|
|
End
|
|
|
|
|
|
- Method OnDiscard() Override
|
|
|
|
-
|
|
|
|
- FT_Done_Face( _face )
|
|
|
|
-
|
|
|
|
- _data.Discard()
|
|
|
|
-
|
|
|
|
- _data=Null
|
|
|
|
- _face=Null
|
|
|
|
- _shader=Null
|
|
|
|
-
|
|
|
|
- End
|
|
|
|
-
|
|
|
|
- Private
|
|
|
|
-
|
|
|
|
- Field _data:DataBuffer
|
|
|
|
- Field _face:FT_Face
|
|
|
|
- Field _shader:Shader
|
|
|
|
- Field _textureFlags:TextureFlags
|
|
|
|
-
|
|
|
|
- Field _height:Int
|
|
|
|
- Field _ascent:Int
|
|
|
|
-
|
|
|
|
Method New( data:DataBuffer,face:FT_Face,fheight:Float,shader:Shader,textureFlags:TextureFlags )
|
|
Method New( data:DataBuffer,face:FT_Face,fheight:Float,shader:Shader,textureFlags:TextureFlags )
|
|
|
|
|
|
_data=data
|
|
_data=data
|
|
@@ -180,30 +218,34 @@ Class FreeTypeFont Extends Font
|
|
_height=(face->size->metrics.height+32) Shr 6
|
|
_height=(face->size->metrics.height+32) Shr 6
|
|
_ascent=(face->size->metrics.ascender+32) Shr 6
|
|
_ascent=(face->size->metrics.ascender+32) Shr 6
|
|
|
|
|
|
- Local gindex:FT_UInt,nchars:=0
|
|
|
|
- Local charcode:=FT_Get_First_Char( face,Varptr gindex )
|
|
|
|
|
|
+ Local gindex:FT_UInt
|
|
|
|
+ Local char:=FT_Get_First_Char( face,Varptr gindex )
|
|
|
|
+ Local minChar:=char,maxChar:=char,maxPage:=0
|
|
|
|
|
|
- Local pages:=New GlyphPage[256]
|
|
|
|
-
|
|
|
|
- Local maxpage:=0
|
|
|
|
|
|
+ _pages=New GlyphPage[256]
|
|
|
|
|
|
While gindex
|
|
While gindex
|
|
|
|
|
|
- Local page:Int=charcode Shr 8
|
|
|
|
-
|
|
|
|
- If page>=0 And page<pages.Length
|
|
|
|
|
|
+ Local page:Int=char Shr 8
|
|
|
|
|
|
- maxpage=Max( maxpage,page )
|
|
|
|
-
|
|
|
|
- If Not pages[page] pages[page]=New GlyphPage
|
|
|
|
|
|
+ If page>=0 And page<_pages.Length And Not _pages[page]
|
|
|
|
+
|
|
|
|
+ maxPage=Max( page,maxPage )
|
|
|
|
+ _pages[page]=New GlyphPage
|
|
|
|
|
|
Endif
|
|
Endif
|
|
|
|
|
|
- charcode=FT_Get_Next_Char( face,charcode,Varptr gindex )
|
|
|
|
-
|
|
|
|
|
|
+ char=FT_Get_Next_Char( face,char,Varptr gindex )
|
|
|
|
+
|
|
|
|
+ minChar=Min( char,minChar )
|
|
|
|
+ maxChar=Max( char,maxChar )
|
|
Wend
|
|
Wend
|
|
|
|
|
|
- InitFont( _height,pages.Slice( 0,maxpage+1 ) )
|
|
|
|
|
|
+ _pages=_pages.Slice( 0,maxPage+1 )
|
|
|
|
+
|
|
|
|
+ Super.Init( _height,minChar,maxChar-minChar+1 )
|
|
|
|
+
|
|
|
|
+ _nullGlyph=GetGlyph( 0 )
|
|
End
|
|
End
|
|
|
|
|
|
End
|
|
End
|