freetypefont.bmx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. SuperStrict
  2. Module BRL.FreeTypeFont
  3. ModuleInfo "Version: 1.10"
  4. ModuleInfo "Author: Simon Armstrong, Mark Sibly"
  5. ModuleInfo "License: zlib/libpng"
  6. ModuleInfo "Copyright: Blitz Research Ltd"
  7. ModuleInfo "Modserver: BRL"
  8. ModuleInfo "History: 1.10"
  9. ModuleInfo "History: Module is now SuperStrict"
  10. ModuleInfo "History: 1.09 Release"
  11. ModuleInfo "History: Offset glyph rect to allow for smooth font border"
  12. ModuleInfo "History: 1.08 Release"
  13. ModuleInfo "History: Fixed freetypelib being reopened per font"
  14. ModuleInfo "History: 1.07 Release"
  15. ModuleInfo "History: Added one pixel blank border around SMOOTHFONT glyphs for ultra smooth subpixel positioning."
  16. ModuleInfo "History: 1.06 Release"
  17. ModuleInfo "History: Fixed memory (incbin::) fonts"
  18. ModuleInfo "History: 1.05 Release"
  19. ModuleInfo "History: Improved unicode support"
  20. ModuleInfo "History: Replaced stream hooks with New_Memory_Face"
  21. ModuleInfo "History: 1.04 Release"
  22. ModuleInfo "History: Added stream hooks"
  23. Import BRL.Font
  24. Import BRL.Pixmap
  25. Import Pub.FreeType
  26. Private
  27. Function PadPixmap:TPixmap( p:TPixmap )
  28. Local t:TPixmap=TPixmap.Create( p.width+2,p.height+2,p.format )
  29. MemClear t.pixels,Size_T(t.capacity)
  30. t.Paste p,1,1
  31. Return t
  32. End Function
  33. Public
  34. Type TFreeTypeGlyph Extends TGlyph
  35. Field _pixmap:TPixmap
  36. Field _advance#,_x:Int,_y:Int,_w:Int,_h:Int
  37. Method Pixels:TPixmap()
  38. If _pixmap Return _pixmap
  39. Return _pixmap
  40. End Method
  41. Method Advance#()
  42. Return _advance
  43. End Method
  44. Method GetRect( x:Int Var,y:Int Var,w:Int Var,h:Int Var )
  45. x=_x
  46. y=_y
  47. w=_w
  48. h=_h
  49. End Method
  50. End Type
  51. Type TFreeTypeFont Extends BRL.Font.TFont
  52. 'Field _face:FTFace
  53. Field _ft_face:Byte Ptr
  54. Field _style:Int,_height:Int
  55. Field _ascend:Int,_descend:Int
  56. Field _glyphs:TFreeTypeGlyph[]
  57. Field _buf:Byte Ptr,_buf_size:Int
  58. Method Delete()
  59. FT_Done_Face _ft_face
  60. MemFree _buf
  61. End Method
  62. Method Style:Int()
  63. Return _style
  64. End Method
  65. Method Height:Int()
  66. Return _height
  67. End Method
  68. Method CountGlyphs:Int()
  69. Return _glyphs.length
  70. End Method
  71. Method CharToGlyph:Int( char:Int )
  72. Return FT_Get_Char_Index( _ft_face,char )-1
  73. End Method
  74. Method LoadGlyph:TFreeTypeGlyph( index:Int )
  75. Local glyph:TFreeTypeGlyph=_glyphs[index]
  76. If glyph Return glyph
  77. glyph=New TFreeTypeGlyph
  78. _glyphs[index]=glyph
  79. If FT_Load_Glyph( _ft_face,index+1,FT_LOAD_RENDER ) Return glyph
  80. 'Local slot:FTGlyph=New FTGlyph
  81. 'MemCopy slot,bmx_freetype_Face_glyph(_ft_face),SizeOf slot
  82. Local _slot:Byte Ptr = bmx_freetype_Face_glyph(_ft_face)
  83. Local width:Int = bmx_freetype_Slot_bitmap_width(_slot)
  84. Local rows:Int = bmx_freetype_Slot_bitmap_rows(_slot)
  85. Local pitch:Int = bmx_freetype_Slot_bitmap_pitch(_slot)
  86. Local advancex:Int = bmx_freetype_Slot_advance_x(_slot)
  87. Local buffer:Byte Ptr = bmx_freetype_Slot_bitmap_buffer(_slot)
  88. glyph._x=bmx_freetype_Slot_bitmapleft(_slot)
  89. glyph._y=-bmx_freetype_Slot_bitmaptop(_slot)+_ascend
  90. glyph._w=width
  91. glyph._h=rows
  92. glyph._advance=advancex Sar 6
  93. If width=0 Return glyph
  94. Local pixmap:TPixmap
  95. If bmx_freetype_Slot_bitmap_numgreys(_slot)
  96. pixmap=TPixmap.CreateStatic( buffer,width,rows,pitch,PF_A8 ).Copy()
  97. Else
  98. pixmap=CreatePixmap( width,rows,PF_A8 )
  99. Local b:Int
  100. For Local y:Int=0 Until rows
  101. Local dst:Byte Ptr=pixmap.PixelPtr(0,y)
  102. Local src:Byte Ptr=buffer+y*pitch
  103. For Local x:Int=0 Until width
  104. If (x&7)=0 b=src[x/8]
  105. If b & $80 dst[x]=$ff Else dst[x]=0
  106. b:+b
  107. Next
  108. Next
  109. EndIf
  110. If _style & SMOOTHFONT
  111. glyph._x:-1
  112. glyph._y:-1
  113. glyph._w:+2
  114. glyph._h:+2
  115. pixmap=PadPixmap(pixmap)
  116. EndIf
  117. glyph._pixmap=pixmap
  118. Return glyph
  119. End Method
  120. Function Load:TFreeTypeFont( src$,size:Int,style:Int )
  121. Global ft_lib:Byte Ptr
  122. If Not ft_lib
  123. If FT_Init_FreeType( Varptr ft_lib ) Return Null
  124. EndIf
  125. Local buf:Byte Ptr,buf_size:Int
  126. Local ft_face:Byte Ptr
  127. If src.Find( "::" )>0
  128. Local tmp:Byte[]=LoadByteArray( src )
  129. buf_size=tmp.length
  130. If Not buf_size Return Null
  131. buf=MemAlloc( Size_T(buf_size) )
  132. MemCopy buf,tmp,Size_T(buf_size)
  133. If FT_New_Memory_Face( ft_lib,buf,buf_size,0,Varptr ft_face )
  134. MemFree buf
  135. Return Null
  136. EndIf
  137. Else
  138. If FT_New_Face( ft_lib,src$,0,Varptr ft_face ) Return Null
  139. EndIf
  140. While size
  141. If Not FT_Set_Pixel_Sizes( ft_face,0,size ) Exit
  142. size:-1
  143. Wend
  144. If Not size
  145. FT_Done_Face ft_face
  146. Return Null
  147. EndIf
  148. 'Local face:FTFace=New FTFace
  149. 'MemCopy face,ft_face,SizeOf face
  150. Local ft_size:Byte Ptr = bmx_freetype_Face_size(ft_face)
  151. 'Local metrics:FTMetrics=New FTMetrics
  152. 'MemCopy metrics,face.metrics,SizeOf metrics
  153. Local font:TFreeTypeFont=New TFreeTypeFont
  154. 'font._face=face
  155. font._ft_face=ft_face
  156. font._style=style
  157. font._height=bmx_freetype_Size_height(ft_size) Sar 6
  158. font._ascend=bmx_freetype_Size_ascend(ft_size) Sar 6
  159. font._descend=bmx_freetype_Size_descend(ft_size) Sar 6
  160. font._glyphs=New TFreeTypeGlyph[bmx_freetype_Face_numglyphs(ft_face)]
  161. font._buf=buf
  162. font._buf_size=buf_size
  163. Return font
  164. End Function
  165. End Type
  166. Type TFreeTypeFontLoader Extends TFontLoader
  167. Method LoadFont:TFreeTypeFont( url:Object,size:Int,style:Int )
  168. Local src$=String( url )
  169. If src Return TFreeTypeFont.Load( src,size,style )
  170. End Method
  171. End Type
  172. AddFontLoader New TFreeTypeFontLoader