imagefont.bmx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. SuperStrict
  2. Import BRL.Font
  3. Import "image.bmx"
  4. Incbin "blitzfont.bin"
  5. Type TImageGlyph
  6. Field _image:TImage
  7. Field _advance:Float,_x:Int,_y:Int,_w:Int,_h:Int
  8. Method Pixels:TImage()
  9. Return _image
  10. End Method
  11. Method Advance:Float()
  12. Return _advance
  13. End Method
  14. Method GetRect( x:Int Var,y:Int Var,w:Int Var,h:Int 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:Int
  25. Field _style:Int
  26. Method Style:Int()
  27. If _src_font Return _src_font.Style()
  28. Return 0
  29. End Method
  30. Method Height:Int()
  31. If _src_font Return _src_font.Height()
  32. Return 16
  33. End Method
  34. Method CountGlyphs:Int()
  35. Return _glyphs.length
  36. End Method
  37. Method CharToGlyph:Int( char:Int )
  38. If _src_font Return _src_font.CharToGlyph( char )
  39. If char>=32 And char<128 Return char-32
  40. Return -1
  41. End Method
  42. Method LoadGlyph:TImageGlyph( index:Int )
  43. Assert index>=0 And index<_glyphs.length
  44. Local glyph:TImageGlyph=_glyphs[index]
  45. If glyph Return glyph
  46. glyph:TImageGlyph=New TImageGlyph
  47. _glyphs[index]=glyph
  48. Local src_glyph:TGlyph=_src_font.LoadGlyph( index )
  49. glyph._advance=src_glyph.Advance()
  50. src_glyph.GetRect glyph._x,glyph._y,glyph._w,glyph._h
  51. Local pixmap:TPixmap=TPixmap( src_glyph.Pixels() )
  52. If Not pixmap Return glyph
  53. glyph._image=TImage.Load( pixmap.Copy(),_imageFlags,0,0,0 )
  54. Return glyph
  55. End Method
  56. Method LoadGlyphs:TImageGlyph[]( text:String )
  57. Local src_glyph:TGlyph[]=_src_font.LoadGlyphs( text )
  58. Local glyphs:TImageGlyph[]=New TImageGlyph[text.length]
  59. For Local i:Int=0 Until text.length
  60. Local src:TGlyph=src_glyph[i]
  61. Local glyph:TImageGlyph=New TImageGlyph
  62. glyphs[i]=glyph
  63. If src Then
  64. Local index:Int = src.Index()
  65. Local cachedGlyph:TImageGlyph = _glyphs[index]
  66. If cachedGlyph Then
  67. glyph._image = cachedGlyph._image
  68. End If
  69. glyph._advance=src.Advance()
  70. src.GetRect glyph._x,glyph._y,glyph._w,glyph._h
  71. If Not glyph._image
  72. Local pixmap:TPixmap=TPixmap( src.Pixels() )
  73. If pixmap Then
  74. glyph._image=TImage.Load( pixmap.Copy(),_imageFlags,0,0,0 )
  75. End If
  76. End If
  77. End If
  78. Next
  79. Return glyphs
  80. End Method
  81. Method Draw( text:String,x:Float,y:Float,ix:Float,iy:Float,jx:Float,jy:Float )
  82. If Not (_style & KERNFONT) Then
  83. For Local i:Int=0 Until text.length
  84. Local n:Int=CharToGlyph( text[i] )
  85. If n<0 Continue
  86. Local glyph:TImageGlyph=LoadGlyph(n)
  87. Local image:TImage=glyph._image
  88. If image
  89. Local frame:TImageFrame=image.Frame(0)
  90. If frame
  91. Local tx:Float=glyph._x*ix+glyph._y*iy
  92. Local ty:Float=glyph._x*jx+glyph._y*jy
  93. frame.Draw 0,0,image.width,image.height,x+tx,y+ty,0,0,image.width,image.height
  94. EndIf
  95. EndIf
  96. x:+glyph._advance*ix
  97. y:+glyph._advance*jx
  98. Next
  99. Else
  100. Local glyphs:TImageGlyph[] = LoadGlyphs( text )
  101. For Local i:Int=0 Until glyphs.length
  102. Local glyph:TImageGlyph=glyphs[i]
  103. Local image:TImage=glyph._image
  104. If image
  105. Local frame:TImageFrame=image.Frame(0)
  106. If frame
  107. Local tx:Float=glyph._x*ix+glyph._y*iy
  108. Local ty:Float=glyph._x*jx+glyph._y*jy
  109. frame.Draw 0,0,image.width,image.height,x+tx,y+ty,0,0,image.width,image.height
  110. EndIf
  111. EndIf
  112. x:+glyph._advance*ix
  113. y:+glyph._advance*jx
  114. Next
  115. End If
  116. End Method
  117. Function Load:TImageFont( url:Object,size:Int,style:Int )
  118. Local src:TFont=LoadFont( url,size,style )
  119. If Not src Return null
  120. Local font:TImageFont=New TImageFont
  121. font._src_font=src
  122. font._glyphs=New TImageGlyph[src.CountGlyphs()]
  123. font._style=style
  124. If style & SMOOTHFONT font._imageFlags=FILTEREDIMAGE|MIPMAPPEDIMAGE
  125. Return font
  126. End Function
  127. Function CreateDefault:TImageFont()
  128. Local font:TImageFont=New TImageFont
  129. font._glyphs=New TImageGlyph[96]
  130. Local pixmap:TPixmap=TPixmap.Create( 96*8,16,PF_RGBA8888 )
  131. Local p:Byte Ptr=IncbinPtr( "blitzfont.bin" )
  132. For Local y:Int=0 Until 16
  133. For Local x:Int=0 Until 96
  134. Local b:Int=p[x]
  135. For Local n:Int=0 Until 8
  136. If b & (1 Shl n)
  137. pixmap.WritePixel x*8+n,y,~0
  138. Else
  139. pixmap.WritePixel x*8+n,y,0
  140. EndIf
  141. Next
  142. Next
  143. p:+96
  144. Next
  145. For Local n:Int=0 Until 96
  146. Local glyph:TImageGlyph=New TImageGlyph
  147. font._glyphs[n]=glyph
  148. glyph._advance=8
  149. glyph._w=8
  150. glyph._h=16
  151. glyph._image=TImage.Load( pixmap.Window(n*8,0,8,16).Copy(),0,0,0,0 )
  152. Next
  153. Return font
  154. End Function
  155. End Type