font.monkey2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Namespace mojo.graphics
  2. Using std.resource
  3. #rem monkeydoc @hidden The Glyph struct.
  4. Glyph are used to store the individual character data for fonts.
  5. #end
  6. Struct Glyph
  7. Field rect:Recti
  8. Field offset:Vec2f
  9. Field advance:Float
  10. Field page:Int
  11. End
  12. #rem monkeydoc The Font class.
  13. Fonts are used when drawing text to a canvas using [[Canvas.DrawText]].
  14. To load a font, use the [[Font.Load]] function. Fonts should be in .otf, .ttf or .fon format.
  15. Once a font is loaded it can be used with a canvas via the [[Canvas.Font]] property.
  16. #end
  17. Class Font Extends Resource
  18. #rem monkeydoc The font height in pixels.
  19. #end
  20. Property Height:Float()
  21. Return _height
  22. End
  23. #rem monkeydoc The first character in the font.
  24. #end
  25. Property FirstChar:Int()
  26. Return _firstChar
  27. End
  28. #rem monkeydoc The number of characters in the font.
  29. #end
  30. Property NumChars:Int()
  31. Return _numChars
  32. End
  33. #rem monkeydoc Gets the glyph for a character.
  34. #end
  35. Method GetGlyph:Glyph( char:Int ) Abstract
  36. #rem monkeydoc Gets the glyph page for a character.
  37. #end
  38. Method GetGlyphPage:Image( char:Int ) Abstract
  39. #rem monkeydoc Gets the kerning between 2 characters.
  40. #end
  41. Method GetKerning:Float( firstChar:Int,secondChar:Int ) Virtual
  42. Return 0
  43. End
  44. #rem monkeydoc Measures the width of some text when rendered by the font.
  45. #end
  46. Method TextWidth:Float( text:String )
  47. Local w:=0.0,lastChar:=0
  48. For Local char:=Eachin text
  49. w+=GetKerning( lastChar,char )+GetGlyph( char ).advance
  50. lastChar=char
  51. Next
  52. Return w
  53. End
  54. #rem monkeydoc Loads a font from a file.
  55. #end
  56. Function Load:Font( path:String,size:Float,shader:Shader=Null,textureFlags:TextureFlags=TextureFlags.FilterMipmap )
  57. Local font:Font
  58. Select ExtractExt( path )
  59. Case ".ttf",".otf",".fon"
  60. font=FreeTypeFont.Load( path,size,shader,textureFlags )
  61. Case ".fnt"
  62. font=AngelFont.Load( path,shader,textureFlags )
  63. Case ""
  64. font=FreeTypeFont.Load( path+".ttf",size,shader,textureFlags )
  65. If Not font
  66. font=FreeTypeFont.Load( path+".otf",size,shader,textureFlags )
  67. If Not font font=FreeTypeFont.Load( path+".fon",size,shader,textureFlags )
  68. Endif
  69. End
  70. Return font
  71. End
  72. Protected
  73. Method Init( height:Float,firstChar:Int,numChars:Int )
  74. _height=height
  75. _firstChar=firstChar
  76. _numChars=numChars
  77. End
  78. Private
  79. Field _height:Float
  80. Field _firstChar:Int
  81. Field _numChars:Int
  82. End
  83. Class ResourceManager Extension
  84. Method OpenFont:Font( path:String,height:Float,shader:Shader=Null,textureFlags:TextureFlags=TextureFlags.FilterMipmap )
  85. Local slug:="Font:name="+StripDir( StripExt( path ) )+"&height="+height+"&shader="+(shader ? shader.Name Else "")+"&textureFlags="+Int(textureFlags)
  86. Local font:=Cast<Font>( OpenResource( slug ) )
  87. If font Return font
  88. font=Font.Load( path,height,shader,textureFlags )
  89. AddResource( slug,font )
  90. Return font
  91. End
  92. End