imagefont.bmx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Strict
  2. Import BRL.Font
  3. Import "image.bmx"
  4. Incbin "blitzfont.bin"
  5. Type TImageGlyph
  6. Field _image:TImage
  7. Field _advance#,_x,_y,_w,_h
  8. Method Pixels:TImage()
  9. Return _image
  10. End Method
  11. Method Advance#()
  12. Return _advance
  13. End Method
  14. Method GetRect( x Var,y Var,w Var,h Var )
  15. x=_x
  16. y=_y
  17. w=_w
  18. h=_h
  19. End Method
  20. End Type
  21. Type TImageFont
  22. Field _src_font:TFont
  23. Field _glyphs:TImageGlyph[]
  24. Field _imageFlags
  25. Method Style()
  26. If _src_font Return _src_font.Style()
  27. Return 0
  28. End Method
  29. Method Height()
  30. If _src_font Return _src_font.Height()
  31. Return 16
  32. End Method
  33. Method CountGlyphs()
  34. Return _glyphs.length
  35. End Method
  36. Method CharToGlyph( char )
  37. If _src_font Return _src_font.CharToGlyph( char )
  38. If char>=32 And char<128 Return char-32
  39. Return -1
  40. End Method
  41. Method LoadGlyph:TImageGlyph( index )
  42. Assert index>=0 And index<_glyphs.length
  43. Local glyph:TImageGlyph=_glyphs[index]
  44. If glyph Return glyph
  45. glyph:TImageGlyph=New TImageGlyph
  46. _glyphs[index]=glyph
  47. Local src_glyph:TGlyph=_src_font.LoadGlyph( index )
  48. glyph._advance=src_glyph.Advance()
  49. src_glyph.GetRect glyph._x,glyph._y,glyph._w,glyph._h
  50. Local pixmap:TPixmap=TPixmap( src_glyph.Pixels() )
  51. If Not pixmap Return glyph
  52. glyph._image=TImage.Load( pixmap.Copy(),_imageFlags,0,0,0 )
  53. Return glyph
  54. End Method
  55. Method Draw( text$,x#,y#,ix#,iy#,jx#,jy# )
  56. For Local i=0 Until text.length
  57. Local n=CharToGlyph( text[i] )
  58. If n<0 Continue
  59. Local glyph:TImageGlyph=LoadGlyph(n)
  60. Local image:TImage=glyph._image
  61. If image
  62. Local frame:TImageFrame=image.Frame(0)
  63. If frame
  64. Local tx#=glyph._x*ix+glyph._y*iy
  65. Local ty#=glyph._x*jx+glyph._y*jy
  66. frame.Draw 0,0,image.width,image.height,x+tx,y+ty,0,0,image.width,image.height
  67. EndIf
  68. EndIf
  69. x:+glyph._advance*ix
  70. y:+glyph._advance*jx
  71. Next
  72. End Method
  73. Function Load:TImageFont( url:Object,size,style )
  74. Local src:TFont=LoadFont( url,size,style )
  75. If Not src Return
  76. Local font:TImageFont=New TImageFont
  77. font._src_font=src
  78. font._glyphs=New TImageGlyph[src.CountGlyphs()]
  79. If style & SMOOTHFONT font._imageFlags=FILTEREDIMAGE|MIPMAPPEDIMAGE
  80. Return font
  81. End Function
  82. Function CreateDefault:TImageFont()
  83. Local font:TImageFont=New TImageFont
  84. font._glyphs=New TImageGlyph[96]
  85. Local pixmap:TPixmap=TPixmap.Create( 96*8,16,PF_RGBA8888 )
  86. Local p:Byte Ptr=IncbinPtr( "blitzfont.bin" )
  87. For Local y=0 Until 16
  88. For Local x=0 Until 96
  89. Local b=p[x]
  90. For Local n=0 Until 8
  91. If b & (1 Shl n)
  92. pixmap.WritePixel x*8+n,y,~0
  93. Else
  94. pixmap.WritePixel x*8+n,y,0
  95. EndIf
  96. Next
  97. Next
  98. p:+96
  99. Next
  100. For Local n=0 Until 96
  101. Local glyph:TImageGlyph=New TImageGlyph
  102. font._glyphs[n]=glyph
  103. glyph._advance=8
  104. glyph._w=8
  105. glyph._h=16
  106. glyph._image=TImage.Load( pixmap.Window(n*8,0,8,16).Copy(),0,0,0,0 )
  107. Next
  108. Return font
  109. End Function
  110. End Type