This plugin will allow any truetype font you have on your system to be rendered with antialiasing (smoothing) and kerning (correct spacing between letters).
<edit 25,2,07: All known issues fixed so far! Turned example code into functions to make it easier to use.>
Pretty nice - don't you think?! This is not a prerendered bitmap, but real time dbpro output. It's pretty fast, but obviously with bigger font sizes and longer strings of text, it will get a little slower. I know there is Cloggy's excellent d3d plugin which also renders antialiased text amongst other things, but unfortunately I have found it seems to have some issues on some pc's, and sometimes seems to struggle with correct kerning for 'script-like' fonts. I wrote this because I need something I can guarantee will work on all pc's, which is why I would be grateful to anyone who can help me test this.
The command is 'GUI_RENDERTEXT font$,size,text$'. The commented example code below shows you how to use it.
Setting up the plugin is very straight forward - just two downloads below and your away.
You will need 3 dll's to be included either in your project folder (where the .dbpro program is) or in your windows system folder. If you distribute your project, the end user will need these too, so it's best to include them in your project folder:
zlib1.dll
libpng13.dll
freetype6.dll
These can be downloaded here:
http://www.andromedus.com/misc/rendertextfiles.zip
The open source 'freetype6' dll has a license agreement which should go with it.
And here is the plugin, which needs to go into your 'user plugins' folder (Dark Basic Professional/Compiler/plugins-user):
http://www.andromedus.com/misc/GUI_rendertext.dll
Please let me know if there are any problems. Thanks,
Ric.
Here is some example code of it in action:
`set display to your maximum resolution for best appearance
set display mode 1024,768,32
color backdrop rgb(255,120,100)
`the GUI_RENDERTEXT command needs the full path of the font to be used, so use this function to get it from the font name.
fontpath$=get_font_path("times new roman")
`the size can be anything, unlike DBPro standard text size limit of 100. This will be the approximate height in pixels of the font.
size=100
`here is the command which renders the text to a dds image
GUI_RENDERTEXT fontpath$,size,"Hello there!"
`this loads the image and turns it into a sprite with an alpha layer
spritenumber=1
imagenumber=1
text_to_sprite(spritenumber,imagenumber)
`now you can position the text, colour it, or do whatever else you want with it
sprite spritenumber,100,100,imagenumber
set sprite diffuse spritenumber,200,50,200
set sprite alpha spritenumber,150
wait key
end
function text_to_sprite(spritenumber,imagenumber)
if sprite exist(spritenumber) then delete sprite spritenumber
if image exist(imagenumber) then delete image imagenumber
`give the command the required time to produce the png image before moving on
repeat until file exist("output.png")=1
`load the output image into dbpro
load image "output.png",imagenumber,1
delete file "output.png"
`save then reload as dds, to ensure dbpro recognises the image format
save image "output.dds",1
load image "output.dds",imagenumber,1
delete file "output.dds"
`add an alpha layer to the image to give transparency and for better blending with background
alpha(spritenumber,255,255,255)
`create a sprite from the image
sprite spritenumber,0,0,imagenumber
endfunction
function get_font_path(fontname$)
`find the directory fonts are stored
fontdirectory$=FontDirectory()
`get the file name (*.ttf) of the truetype fontname from the registry - registry address for fonts dependent upon operating system
if OS_Family()="NT"
file$=get registry$("SOFTWAREMicrosoftWindows NTCurrentVersionFonts",fontname$+" (TrueType)")
else
file$=get registry$("SOFTWAREMicrosoftWindowsCurrentVersionFonts",fontname$+" (TrueType)")
endif
`if truetype font found, add file name to font directory to get full path of font
if file$<>""
path$=FontDirectory$+""+file$
else
path$=""
endif
`exclude 'Microsoft Sans Serif' font - due to known problem with rendering this font
if lower$(path$)=lower$(fontdirectory())+lower$("micross.ttf") then path$=""
`if path to truetype font wasn't found, font is not truetype - so revert to default: arial
if path$="" or file exist(path$)=0 then path$=FontDirectory$+""+"arial.ttf"
endfunction path$
function alpha(image,red,green,blue)
`this is for converting an image with no alpha layer into a monochrome image with alpha layer created from rgb values.
`image: image number
`red, green, blue: the single colour the final image will be
memblockin=1
memblockout=2
make memblock from image memblockin,image
sx=memblock dword(memblockin,0)
sy=memblock dword(memblockin,4)
depth=memblock dword(memblockin,8)
`text 0,440,"Depth: "+str$(depth)
`sync
`wait key
if depth=16 then size=((get memblock size(memblockin)-12)*2)+12
if depth=32 then size=get memblock size(memblockin)
if depth=24 then size=roundtointeger((get memblock size(memblockin)-12)*(4/3.0))+12
`make output memblock and fill header
make memblock memblockout,size
write memblock dword memblockout,0,sx
write memblock dword memblockout,4,sy
write memblock dword memblockout,8,32
bpp=depth/8 `bytes per pixel
for x=0 to sx-1
for y=0 to sy-1
pos=(x * bpp) + ((y * bpp) * sx) + 12
pos32bit=(x * 4) + ((y * 4) * sx) + 12
if depth=32 or depth=24
b=memblock byte(memblockin,pos)
g=memblock byte(memblockin,pos+1)
r=memblock byte(memblockin,pos+2)
endif
if depth=16
colour=memblock word(memblockin,pos)
b=getbluefrom16bit(colour)
g=getgreenfrom16bit(colour)
r=getredfrom16bit(colour)
endif
a=int((b+g+r)/3.0)
b=blue
g=green
r=red
if a>255 then a=255
if a<0 then a=0
write memblock byte memblockout,pos32bit,b
write memblock byte memblockout,pos32bit+1,g
write memblock byte memblockout,pos32bit+2,r
write memblock byte memblockout,pos32bit+3,a
next y
next x
make image from memblock image,memblockout
delete memblock memblockout
endfunction
function GetRedFrom16bit(Colour as word)
Colour = (Colour >> 7) && %11111000
Colour = Colour || (Colour >> 5)
endfunction Colour
function GetGreenFrom16bit(Colour as word)
Colour = (Colour >> 2) && %11111000
Colour = Colour || (Colour >> 5)
endfunction Colour
function GetBlueFrom16bit(Colour as word)
Colour = (Colour << 3) && %11111000
Colour = Colour || (Colour >> 5)
endfunction Colour
function GetAlphaFrom16bit(Colour as word)
Colour = ((Colour >> 15) && 1) * 255
endfunction Colour
function FontDirectory()
local FontDir as string
` Prepare a string for the API to write to
FontDir = space$(260)
` Call the API to get the path
load dll "shell32.dll", 1
Result = call dll(1, "SHGetSpecialFolderPathA", 0, FontDir, 0x0014, 0)
delete dll 1
endfunction FontDir
function OS_Family()
local Family as string
Family = "9x"
load dll "kernel32.dll", 1
if dll call exist(1, "TryEnterCriticalSection") = 1 then Family = "NT"
delete dll 1
endfunction Family
function roundtointeger(value#)
value=int(value#+0.5)
endfunction value