font.monkey2 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Fonts are used when drawing text to a canvas using [[Canvas.DrawText]].
  26. To load a font, use the [[Font.Load]] function. Fonts should be in .otf, .ttf or .fon format.
  27. Once a font is loaded it can be used with a canvas via the [[Canvas.Font]] property.
  28. #end
  29. Class Font Extends Resource
  30. #rem monkeydoc The font height in pixels.
  31. #end
  32. Property Height:Float()
  33. Return _height
  34. End
  35. #rem monkeydoc Measures the width of some text when rendered by the font.
  36. #end
  37. Method TextWidth:Float( text:String )
  38. Local w:=0.0
  39. For Local char:=Eachin text
  40. w+=GetGlyph( char ).advance
  41. Next
  42. Return w
  43. End
  44. #rem monkeydoc @hidden
  45. Gets the glyph page for a given char.
  46. Returns null if char does not have a glyph.
  47. #end
  48. Method GetGlyphPage:GlyphPage( char:Int )
  49. Local page:=char Shr 8
  50. If page<0 Or page>=_pages.Length Return Null
  51. Local gpage:=_pages[page]
  52. If Not gpage Return Null
  53. If Not gpage.image LoadGlyphPage( page )
  54. Local index:=char & 255
  55. If index>=gpage.glyphs.Length Return Null
  56. Return gpage
  57. End
  58. #rem monkeydoc @hidden
  59. Gets the glyph for a given char.
  60. #end
  61. Method GetGlyph:Glyph( char:Int )
  62. Local page:=char Shr 8
  63. If page<0 Or page>=_pages.Length Return _nullGlyph
  64. Local gpage:=_pages[page]
  65. If Not gpage Return _nullGlyph
  66. If Not gpage.image LoadGlyphPage( page )
  67. Local index:=char & 255
  68. If index>=gpage.glyphs.Length Return _nullGlyph
  69. Return gpage.glyphs[index]
  70. End
  71. #rem monkeydoc Loads a font from a file.
  72. #end
  73. Function Load:Font( path:String,height:Float,shader:Shader=Null )
  74. If Not shader shader=Shader.GetShader( "font" )
  75. Local font:=FreeTypeFont.Load( path,height,shader )
  76. If Not font And Not ExtractRootDir( path ) font=FreeTypeFont.Load( "font::"+path,height,shader )
  77. Return font
  78. End
  79. Protected
  80. Method OnLoadGlyphPage( page:Int,gpage:GlyphPage ) Abstract
  81. Method InitFont( height:Float,pages:GlyphPage[] )
  82. _height=height
  83. _pages=pages
  84. LoadGlyphPage( 0 )
  85. _nullGlyph=GetGlyph( 0 )
  86. End
  87. Method OnDiscard() Override
  88. For Local page:=Eachin _pages
  89. If page And page.image page.image.Discard()
  90. Next
  91. _pages=Null
  92. End
  93. Private
  94. Field _height:Float
  95. Field _pages:GlyphPage[]
  96. Field _nullGlyph:Glyph
  97. Method LoadGlyphPage( page:Int )
  98. Local gpage:=_pages[page]
  99. If Not gpage.image OnLoadGlyphPage( page,gpage )
  100. End
  101. End
  102. Class ResourceManager Extension
  103. Method OpenFont:Font( path:String,height:Float,shader:Shader=Null )
  104. Local slug:="Font:name="+StripDir( StripExt( path ) )+"&height="+height+"&shader="+(shader ? shader.Name Else "")
  105. Local font:=Cast<Font>( OpenResource( slug ) )
  106. If font Return font
  107. font=Font.Load( path,height )
  108. AddResource( slug,font )
  109. Return font
  110. End
  111. End