As promised, here is my split BM font function.
Things to note in order for this to work:
There must be a blank space at the beginning of the image file which will be used for a space character. The width of the space will be determined as an average of all character widths
Each row of characters must be in ASCII order
The extracted characters are grabbed as fixed height but won't show as fixed height if you use transparency above and below.
There must be NO transparent columns inside any character, otherwise they will be determined as two separate characters. This is most noticeable in a double quote
". If you load my font sheet into an image editor and zoom in you will see that I have put two pixels between each quote and set their transparency to 10% so that they are not noticeable.
Test Program to show how to use it:
#constant TRUE 1
#constant FALSE 0
#include "SplitBMFont.agc"
SetWindowSize( 500, 120, 0 )
SetVirtualResolution( 500, 120 )
font$ = "e:\AGK\Projects\BMfont\media\TGC"
SplitBMfont(font$)
fntImg = LoadImage("raw:" + font$+".png")
txt = CreateText("Hello World!")
SetTextSize(txt, 100)
SetTextFontImage(txt, fntImg)
do
Sync()
loop
The include file that you can download below or copy/paste here:
/********** SplitBMfont(fn$) **************************
Description: Uses a text file to determine how to split a bitmap font sheet
The text file should have the same name as the font and be in the same folder
Parameters: fn$ - The full RAW PATH file name of the text file that holds the data for the split
NOT including any file extension
Returns: Returns nothing but CREATES a subimages.txt file to use for the bitmap font sheet
Notes: The text file should be in the following format
100
A:M
N:Z
0:9
And saved with the same filename as the bitmap font
The above example demonstrates how the file should be formatted
100 = The Height of each row of text
Each subsequent row is the range characters on each line which must be in correct ASCII order
*/
function SplitBMfont(fn$)
fn$ = "raw:" + fn$
OpenToRead(1, fn$+".txt")
height = val(ReadLine(1))
imgFont = LoadImage(fn$+".png")
mbFont = CreateMemblockFromImage(imgFont)
y = 0
OpenToWrite(2, fn$ + " subimages.txt")
avg = 0
avgCount = 0
repeat
ln$ = ReadLine(1)
startASC = asc(left(ln$, 1))
endASC = asc(right(ln$, 1))
ascii = startASC
startX = 0
while ascii <= endASC
inc avgCount
repeat
test = ColumnIsAllAlpha(mbFont, startX, y, y+height-1)
if test = TRUE
inc startX
endif
until test = FALSE
endX = startX+1
repeat
test = ColumnHasNonAlpha(mbFont, endX, y, y+height-1)
if test = TRUE
inc endX
endif
until test = FALSE
WriteLine(2, str(ascii) + ":" + str(startX) + ":" + str(y)+":"+str(endX-startX)+":"+str(height))
inc ascii
inc avg, endX-startX
startX = endX+1
endwhile
inc y, height
until FileEOF(1)
//Create Space that is half the width of the average of all characters
//And place it at the begining of the file
WriteLine(2, "32:0:0:"+str(avg/avgCount)+":"+str(height))
CloseFile(2)
CloseFile(1)
endfunction
function GetMemblockImageOffset(mb, x, y)
w = GetMemblockInt(mb, 0)
h = GetMemblockInt(mb, 4)
yy = 12 + (4*w*y)
xx = 4*x
ret = xx+yy
endfunction ret
function ColumnIsAllAlpha(mb, x, startY, endY)
for y = startY to endY
pix = GetMemblockImageOffset(mb, x, y)
if GetMemblockByte(mb, pix+3) > 0
exitfunction FALSE
endif
next y
endfunction TRUE
function ColumnHasNonAlpha(mb, x, startY, endY)
for y = startY to endY
pix = GetMemblockImageOffset(mb, x, y)
if GetMemblockByte(mb, pix+3) > 0
exitfunction TRUE
endif
next y
endfunction FALSE
The included text file is what is required to describe the layout of the font file and looks like this:
64
!)
*3
4=
>G
HQ
R[
\e
fo
py
z~
The Font file used:
Output:
I have also included a PSD file. You can change the font and style en masse and create a whole new bitmap font very easily.