font.monkey2 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #rem monkeydoc Creates a new glyph.
  11. #end
  12. Method New( rect:Recti,offset:Vec2f,advance:Float )
  13. Self.rect=rect
  14. Self.offset=offset
  15. Self.advance=advance
  16. End
  17. End
  18. #rem monkeydoc @hidden
  19. #end
  20. Class GlyphPage
  21. Field image:Image
  22. Field glyphs:Glyph[]
  23. End
  24. #rem monkeydoc The Font class.
  25. #end
  26. Class Font Extends Resource
  27. #rem monkeydoc The font height in pixels.
  28. #end
  29. Property Height:Float()
  30. Return _height
  31. End
  32. #rem monkeydoc Measures the width of some text when rendered by the font.
  33. #end
  34. Method TextWidth:Float( text:String )
  35. Local w:=0.0
  36. For Local char:=Eachin text
  37. w+=GetGlyph( char ).advance
  38. Next
  39. Return w
  40. End
  41. #rem monkeydoc @hidden
  42. Gets the glyph page for a given char.
  43. Returns null if char does not have a glyph.
  44. #end
  45. Method GetGlyphPage:GlyphPage( char:Int )
  46. Local page:=char Shr 8
  47. If page<0 Or page>=_pages.Length Return Null
  48. Local gpage:=_pages[page]
  49. If Not gpage Return Null
  50. If Not gpage.image LoadGlyphPage( page )
  51. Local index:=char & 255
  52. If index>=gpage.glyphs.Length Return Null
  53. Return gpage
  54. End
  55. #rem monkeydoc @hidden
  56. Gets the glyph for a given char.
  57. #end
  58. Method GetGlyph:Glyph( char:Int )
  59. Local page:=char Shr 8
  60. If page<0 Or page>=_pages.Length Return _nullGlyph
  61. Local gpage:=_pages[page]
  62. If Not gpage Return _nullGlyph
  63. If Not gpage.image LoadGlyphPage( page )
  64. Local index:=char & 255
  65. If index>=gpage.glyphs.Length Return _nullGlyph
  66. Return gpage.glyphs[index]
  67. End
  68. #rem monkeydoc Loads a font from a file.
  69. #end
  70. Function Load:Font( path:String,height:Float,shader:Shader=Null )
  71. If Not shader shader=Shader.GetShader( "font" )
  72. Local font:=FreeTypeFont.Load( path,height,shader )
  73. If Not font And Not ExtractRootDir( path ) font=FreeTypeFont.Load( "font::"+path,height,shader )
  74. Return font
  75. End
  76. Protected
  77. Method OnLoadGlyphPage( page:Int,gpage:GlyphPage ) Abstract
  78. Method InitFont( height:Float,pages:GlyphPage[] )
  79. _height=height
  80. _pages=pages
  81. LoadGlyphPage( 0 )
  82. _nullGlyph=GetGlyph( 0 )
  83. End
  84. Method OnDiscard() Override
  85. For Local page:=Eachin _pages
  86. If page And page.image page.image.Discard()
  87. Next
  88. _pages=Null
  89. End
  90. Private
  91. Field _height:Float
  92. Field _pages:GlyphPage[]
  93. Field _nullGlyph:Glyph
  94. Method LoadGlyphPage( page:Int )
  95. Local gpage:=_pages[page]
  96. If Not gpage.image OnLoadGlyphPage( page,gpage )
  97. End
  98. End
  99. Class ResourceManager Extension
  100. Method OpenFont:Font( path:String,height:Float,shader:Shader=Null )
  101. Local slug:="Font:name="+StripDir( StripExt( path ) )+"&height="+height+"&shader="+(shader ? shader.Name Else "")
  102. Local font:=Cast<Font>( OpenResource( slug ) )
  103. If font Return font
  104. font=Font.Load( path,height )
  105. AddResource( slug,font )
  106. Return font
  107. End
  108. End