'Copyright 2021 Richard D. Clark
'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
'to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
'and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
'IN THE SOFTWARE.
'========================================================================================================================================================
'This code displays a graphic font. You can access the font either by character or ascii code.
Option _Explicit
'Screen and image coords.
Type coords
x As Integer
y As Integer
End Type
'Working consts.
Const False = 0
Const True = Not False
Const fontsize = 16
'This is the location of each character in the font.
Dim fgrid(0 To 255) As coords
Dim fhandle As Long
Dim As Integer ret, cx, cy, ic
Dim As String mystring
'Using 32 bit screen.
Screen _NewImage(640, 480, 32)
_Title "Graphic Font Demo"
'Load the font.
ret = LoadFont%("16x16.png", fontsize, fgrid(), fhandle)
If ret = False Then
Print "Could not load font."
Else
mystring = "Graphic Font Demo"
cy = fontsize
cx = (_Width(0) / 2) - ((Len(mystring) / 2) * fontsize) 'Center the string.
PrintString mystring, cx, cy, fontsize, fgrid(), fhandle
'Print all the characters.
cx = 0
cy = cy + (fontsize) * 2
For ic = 0 To 255
PrintByCode ic, cx, cy, fontsize, fgrid(), fhandle
cx = cx + fontsize
If cx + fontsize > _Width(0) Then
cx = 0
cy = cy + fontsize
End If
Next
End If
Sub PrintString (c As String, x As Integer, y As Integer, fsize As Integer, grd() As coords, handle As Long)
Dim As Integer x1, y1, x2, y2, charw, charh, i, ch, nx, ny
'Calculate the character width and height.
charw = _Width(handle) / fsize
charh = _Height(handle) / fsize
nx = x
ny = y
For i = 1 To Len(c)
ch = Asc(Mid$(c, i, 1))
'Get the character coords.
x1 = grd(ch).x
x2 = grd(ch).x + charw - 1
y1 = grd(ch).y
y2 = grd(ch).y + charh - 1
If nx < _Width(0) + charw Then
_PutImage (nx, ny), handle, 0, (x1, y1)-(x2, y2)
nx = nx + charw
End If
Next
End Sub
Sub PrintByCode (c As Integer, x As Integer, y As Integer, fsize As Integer, grd() As coords, handle As Long)
Dim As Integer x1, y1, x2, y2, charw, charh
If c >= 0 And c <= 255 Then
'Calculate the character width and height.
charw = _Width(handle) / fsize
charh = _Height(handle) / fsize
'Get the character coords.
x1 = grd(c).x
x2 = grd(c).x + charw - 1
y1 = grd(c).y
y2 = grd(c).y + charh - 1
_PutImage (x, y), handle, 0, (x1, y1)-(x2, y2)
End If
End Sub
'Loads the font into memory. The fonts graphic has to be square such as 16x16 characters. This is the standard Dwarf Fortress format.
Function LoadFont% (FontName As String, fsize As Integer, grd() As coords, handle As Long)
Dim As Integer x, y, charw, charh, px, py
handle = _LoadImage(FontName, 32)
If handle > -2 Then
LoadFont% = False
Else
'Calculate the character width and height.
charw = _Width(handle) / fsize
charh = _Height(handle) / fsize
'Fill in the grid.
px = 0
py = 0
For y = 0 To 15
For x = 0 To 15
grd(x + y * fsize).x = px
grd(x + y * fsize).y = py
px = px + charw
Next
px = 0
py = py + charh
Next
LoadFont% = True
End If
End Function