1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
Rem
bbdoc: noddybox.bitmapfont
EndRem
Module noddybox.bitmapfont
ModuleInfo "Framework: Simple Bitmap Font Routines"
ModuleInfo "Copyright: Public Domain"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"
' $Id$
Import brl.Max2D
Import brl.Basic
Strict
Rem
bbdoc: Defines a bitmap font
about: The @TBitmapFont object works from BMF Files, which are created using a tool called BitmapFontEd available at
<a href="http://www.noddybox.demon.co.uk/free/index-csharp.html">http://www.noddybox.demon.co.uk/free/index-csharp.html</a><br>
Note that when colours are referenced they simply modify the colours in the font images as @SetColor() would do on a norml image.<br>
When drawing characters the current scale, alpha and rotation settings are applied.
EndRem
Type TBitmapFont
Const NOCHR=95
Field img:TImage[NOCHR]
Field height:Int[NOCHR]
Field width:Int[NOCHR]
Field is_fixed:Int
Field max_width:Int
Field max_height:Int
Rem
bbdoc: Loads a font from the supplied BMF file.
returns: The created font, or null if the BMF file couldn't be loaded.
about: The @image_flags are passed onto @CreateImage() when the images for the font are being created.
EndRem
Function Load:TBitmapFont(path:String, image_flags:Int)
Local fnt:TBitmapFont
Local str:TStream
Local magic:String
Local f,x,y
str=ReadStream(path)
If (Not str) Then Return Null
fnt=New TBitmapFont
magic=ReadString(str,4)
If (magic<>"BMF1")
CloseStream(str)
Return Null
EndIf
fnt.max_width=-1
fnt.max_height=-1
fnt.is_fixed=ReadInt(str)
For f=0 Until NOCHR
fnt.width[f]=ReadInt(str)
fnt.height[f]=ReadInt(str)
fnt.max_width=Max(fnt.max_width,fnt.width[f])
fnt.max_height=Max(fnt.max_height,fnt.height[f])
fnt.img[f]=CreateImage(fnt.width[f],fnt.height[f],1,image_flags)
Local pm:TPixmap=LockImage(fnt.img[f])
For x=0 Until fnt.width[f]
For y=0 Until fnt.height[f]
WritePixel(pm,x,y,ReadInt(str))
Next
Next
UnlockImage(fnt.img[f])
Next
CloseStream(str)
Return fnt
End Function
Rem
bbdoc: Draws text.
about: Draws @txt at the supplied @x, @y co-ordinates. @red, @green and @blue control the colour.
EndRem
Method Draw(txt:String, x:Int, y:Int, red:Int=255, green:Int=255, blue:Int=255)
Local f,r,g,b,c
Local xs#,ys#
GetScale(xs,ys)
GetColor(r,g,b)
SetColor(red,green,blue)
For f=0 Until Len(txt)
c=txt[f]-32
DrawImage(img[c],x,y)
x:+width[c]*xs
Next
SetColor(r,g,b)
End Method
Rem
bbdoc: Draws centred text.
about: Draws @txt centred at the supplied @y co-ordinate. @red, @green and @blue control the colour.
EndRem
Method Centre(txt:String, y:Int, red:Int=255, green:Int=255, blue:Int=255)
If is_fixed
Draw(txt,GraphicsWidth()/2-Len(txt)*width[0]/2,y,red,green,blue)
Else
Draw(txt,GraphicsWidth()/2-TextWidth(txt)/2,y,red,green,blue)
EndIf
End Method
Rem
bbdoc: Draws text centred on a position.
about: Draws @txt centred on the supplied @x and @y co-ordinate. @red, @green and @blue control the colour.
EndRem
Method CentreOn(txt:String, x:Int, y:Int, red:Int=255, green:Int=255, blue:Int=255)
If is_fixed
Draw(txt,x-Len(txt)*width[0]/2,y-TextHeight(txt),red,green,blue)
Else
Draw(txt,x-TextWidth(txt)/2,y-TextHeight(txt),red,green,blue)
EndIf
End Method
Rem
bbdoc: Draws text right justified.
about: Draws @txt right justified on the supplied @x and @y co-ordinate. @red, @green and @blue control the colour.
EndRem
Method DrawRight(txt:String, x:Int, y:Int, red:Int=255, green:Int=255, blue:Int=255)
If is_fixed
Draw(txt,x-Len(txt)*width[0],y,red,green,blue)
Else
Draw(txt,x-TextWidth(txt),y,red,green,blue)
EndIf
End Method
Rem
bbdoc: Width of the supplied string.
returns: The length of the string @txt in pixels.
about: The current scale settings are taken into account.
EndRem
Method TextWidth:Int(txt:String)
Local w
Local xs#,ys#
GetScale(xs,ys)
For Local f=0 Until Len(txt)
w:+width[txt[f]-32]*xs
Next
Return w
End Method
Rem
bbdoc: Height of the supplied string.
returns: The height of the string @txt in pixels.
about: The current scale settings are taken into account.
EndRem
Method TextHeight:Int(txt:String)
Local h:Int=0
Local xs#,ys#
GetScale(xs,ys)
For Local f=0 Until Len(txt)
h=Max(h,height[txt[f]-32]*ys)
Next
Return h
End Method
Rem
bbdoc: Width of the largest character.
returns: The width in pixels of the largest character in the font.
EndRem
Method MaxWidth:Int()
Return max_width
End Method
Rem
bbdoc: Height of the largest character.
returns: The height in pixels of the largest character in the font.
EndRem
Method MaxHeight:Int()
Return max_height
End Method
End Type
|