New Proportional Font system

Previous topic - Next topic

MrTAToad

Whilst Moru's proportional font system has done sterling with my previous games, it does have a few limitations, especially when it comes to Android devices.  Hence I decided to write a new proportional system.

With this routine, I convert a standard font bitmap into a compressed bitmap, and a separate file containing the X,Y, width and height of all characters.

Code (glbasic) Select
// --------------------------------- //
// Project: CreateSpriteFont
// Start: Friday, June 08, 2012
// IDE Version: 11.001

TYPE tFontInfo
x%
y%
width%
ENDTYPE

LOCAL chars[] AS tFontInfo; DIM chars[256]
LOCAL maxBitmapWidth%=256
LOCAL bitmapHeight%,inFile$,outFile$
LOCAL x%,loop%,length%,fW%,fH%,pos%

inFile$=DDgui_FileDialog$(TRUE,"*.*")
IF inFile$<>""
LOADFONT inFile$,1
SETFONT 1
GETFONTSIZE fW%,fH%
IF fW%=0 OR fH%=0
SETFONT 0
DDgui_msg("Not a font file",FALSE)
ELSE
// Calculate bitmap height
bitmapHeight%=0
x%=0
FOR loop%=0 TO 255
length%=LEN(CHR$(loop%),TRUE)
IF length%=0 THEN length%=fW%

IF x%+length%>maxBitmapWidth%
x%=0
INC bitmapHeight%,fH%+1
ENDIF

chars[loop%].x%=x%
chars[loop%].y%=bitmapHeight%
chars[loop%].width%=length%
DEBUG "Pos : "+loop%+" "+x%+" "+bitmapHeight%+"\n"

INC x%,length%
NEXT

INC bitmapHeight%,fH%+1

// Create bitmap
SMOOTHSHADING FALSE
ALPHAMODE 0.0
CREATESCREEN 1,1,maxBitmapWidth%,bitmapHeight%
USESCREEN 1

DRAWRECT 0,0,maxBitmapWidth%,bitmapHeight%,RGB(1,1,1)
FOR loop%=0 TO 255
PRINT CHR$(loop),chars[loop%].x%,chars[loop%].y%,TRUE
NEXT

USESCREEN -1

SETFONT 0
outFile$=DDgui_FileDialog$(FALSE,"*.png")
IF outFile$<>""
SAVESPRITE outFile$,1

// Remove the extension
outFile$=REPLACE$(outFile$,".png",".fif")
IF OPENFILE(1,outFile$,0)
WRITEUWORD 1,fW%
WRITEUWORD 1,fH%
FOR loop%=0 TO 255
WRITEUWORD 1,chars[loop%].x%
WRITEUWORD 1,chars[loop%].y%
WRITEUWORD 1,chars[loop%].width%
NEXT
CLOSEFILE 1
ENDIF
ENDIF
ENDIF
ENDIF

LOCAL scale=1.0

LOCAL test$="This is a test"
LOCAL one%
LOCAL x%=0
SMOOTHSHADING FALSE
STARTPOLY 1,2
FOR l%=0 TO LEN(test$)-1
one%=ASC(MID$(test$,l%,1))
DEBUG one%+"\n"
LOCAL a%=chars[one%].x%
LOCAL b%=chars[one%].y%
LOCAL c%=chars[one%].width%
LOCAL d%=fH%
DEBUG a%+" "+b%+" "+c%+" "+d%+"\n"

POLYVECTOR x%,0*scale,a%,b%,RGB(255,0,0)
POLYVECTOR x%,(0+d%)*scale,a%,b%+d%,RGB(255,255,255)
POLYVECTOR x%+(c%*scale),0*scale,a%+c%,b%,RGB(255,0,0)
POLYVECTOR x%+(c%*scale),(0+d%)*scale,a%+c%,b%+d%,RGB(255,255,255)
POLYNEWSTRIP

INC x%,c%*scale
NEXT
ENDPOLY
////
SHOWSCREEN
KEYWAIT
//


There is also a basic routine for using the new bitmap to display some text.  This version is somewhat based on Moru's proportional font code, although for my new TSetup routine it has been vastly expanded to allow sprites to be treated as text, as well as scaling.