fontloader.monkey2 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Namespace mojo.graphics.fontloader
  2. #Import "<freetype>"
  3. Private
  4. Using freetype
  5. Global FreeType:FT_Library
  6. Public
  7. #rem monkeydoc @hidden
  8. #end
  9. Function LoadFont:Font( path:String,fheight:Float,shader:Shader )
  10. If Not FreeType And FT_Init_FreeType( Varptr FreeType ) Return Null
  11. Local data:=DataBuffer.Load( path )
  12. If Not data Return Null
  13. Local face:FT_Face
  14. If FT_New_Memory_Face( FreeType,data.Data,data.Length,0,Varptr face )
  15. data.Discard()
  16. Return Null
  17. Endif
  18. Local size_req:FT_Size_RequestRec
  19. size_req.type=FT_SIZE_REQUEST_TYPE_REAL_DIM
  20. size_req.width=0
  21. size_req.height=fheight * 64
  22. size_req.horiResolution=0
  23. size_req.vertResolution=0
  24. If FT_Request_Size( face,Varptr size_req )
  25. data.Discard()
  26. Return Null
  27. Endif
  28. Local height:=(face->size->metrics.height+32) Shr 6
  29. Local ascent:=(face->size->metrics.ascender+32) Shr 6
  30. ' Print face->size->metrics.height/64.0
  31. ' Print face->size->metrics.ascender/64.0
  32. ' Print face->size->metrics.descender/64.0
  33. Local firstChar:=32,numChars:=96
  34. Local glyphs:=New Glyph[numChars]
  35. Local pixmap:=New Pixmap( 512,512,PixelFormat.A8 )
  36. pixmap.Clear( Color.None )
  37. Local slot:=face->glyph
  38. Local x:=0,y:=0,h:=0
  39. For Local i:=0 Until numChars
  40. 'What to do?!?
  41. '
  42. ' If FT_Load_Char( face,firstChar+i,FT_LOAD_RENDER )
  43. ' If FT_Load_Char( face,firstChar+i,FT_LOAD_RENDER|FT_LOAD_TARGET_LIGHT )
  44. ' If FT_Load_Char( face,firstChar+i,FT_LOAD_RENDER|FT_LOAD_NO_HINTING )
  45. If FT_Load_Char( face,firstChar+i,FT_LOAD_RENDER|FT_LOAD_FORCE_AUTOHINT )
  46. Continue
  47. Endif
  48. #rem
  49. If FT_Render_Glyph( slot,FT_RENDER_MODE_NORMAL )
  50. Continue
  51. Endif
  52. #end
  53. Local gw:=slot->bitmap.width
  54. Local gh:=slot->bitmap.rows
  55. If x+gw+1>pixmap.Width
  56. y+=h
  57. h=0
  58. x=0
  59. Endif
  60. Local tmp:=New Pixmap( gw,gh,PixelFormat.A8,slot->bitmap.buffer,slot->bitmap.pitch )
  61. pixmap.Paste( tmp,x,y )
  62. glyphs[i]=New Glyph( New Recti( x,y,x+gw,y+gh ),New Vec2f( slot->bitmap_left,ascent-slot->bitmap_top ),slot->advance.x Shr 6 )
  63. h=Max( Int(h),Int(gh)+1 )
  64. x+=gw+1
  65. Next
  66. FT_Done_Face( face )
  67. data.Discard()
  68. Local texture:=New Texture( pixmap,Null )
  69. Local image:=New Image( texture,shader )
  70. Local font:=New Font( image,height,firstChar,glyphs )
  71. Return font
  72. End