font.monkey2 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Namespace mojo.graphics
  2. #rem monkeydoc The Glyph struct.
  3. Glyph are used to store the individual character data for fonts.
  4. #end
  5. Struct Glyph
  6. Field rect:Recti
  7. Field offset:Vec2f
  8. Field advance:Float
  9. #rem monkeydoc Creates a new glyph.
  10. #end
  11. Method New( rect:Recti,offset:Vec2f,advance:Float )
  12. Self.rect=rect
  13. Self.offset=offset
  14. Self.advance=advance
  15. End
  16. End
  17. #rem monkeydoc The Font class.
  18. Mojo fonts use an image atlas system for storing font data.
  19. The glyph struct is used to store the location, size and advance for individual characters within a font.
  20. All character image data for a font must A font must occupy a single image,
  21. #end
  22. Class Font
  23. #rem monkeydoc Creates a new font.
  24. @param image The image atlas.
  25. @param height The height of the font in pixels.
  26. @param firstChar The first character contained in the font.
  27. @param glyphs An array of glyph structs describing the characters in the font.
  28. #end
  29. Method New( image:Image,height:Float,firstChar:Int,glyphs:Glyph[] )
  30. _image=image
  31. _height=height
  32. _firstChar=firstChar
  33. _glyphs=glyphs
  34. End
  35. #rem monkeydoc The font image atlas.
  36. #end
  37. Property Image:Image()
  38. Return _image
  39. End
  40. #rem monkeydoc The font height in pixels.
  41. #end
  42. Property Height:Float()
  43. Return _height
  44. End
  45. #rem monkeydoc The first character contained in the font.
  46. #end
  47. Property FirstChar:Int()
  48. Return _firstChar
  49. End
  50. #rem monkeydoc The number of characters in the font.
  51. #end
  52. Property NumChars:Int()
  53. Return _glyphs.Length
  54. End
  55. #rem monkeydoc @hidden
  56. #end
  57. Property Glyphs:Glyph[]()
  58. Return _glyphs
  59. End
  60. #rem monkeydoc Gets a glyph from the font.
  61. #end
  62. Method GetGlyph:Glyph( char:Int )
  63. If char>=_firstChar And char<_firstChar+_glyphs.Length Return _glyphs[ char-_firstChar ]
  64. Return _glyphs[0]
  65. End
  66. #rem monkedoc Measures the width of some text when rendered by the font.
  67. #end
  68. Method TextWidth:Float( text:String )
  69. Local w:=0.0
  70. For Local char:=Eachin text
  71. w+=GetGlyph( char ).advance
  72. Next
  73. Return w
  74. End
  75. #rem monkeydoc @hidden
  76. #end
  77. Function Open:Font( path:String,height:Float )
  78. Local tag:=RealPath( path )+":"+height
  79. Local font:=_openFonts[tag]
  80. If Not font
  81. font=Load( path,height )
  82. _openFonts[tag]=font
  83. Endif
  84. Return font
  85. End
  86. 'Make this ALWAYS work!
  87. #rem monkeydoc Loads a font from a ttf file.
  88. #end
  89. Function Load:Font( path:String,height:Float,textureFlags:TextureFlags=Null,shader:Shader=Null )
  90. If Not shader shader=Shader.GetShader( "font" )
  91. Local font:=fontloader.LoadFont( path,height,textureFlags,shader )
  92. Return font
  93. End
  94. Private
  95. Private
  96. Field _image:Image
  97. Field _height:Float
  98. Field _firstChar:Int
  99. Field _glyphs:Glyph[]
  100. Global _openFonts:=New StringMap<Font>
  101. End