Simple Font-Library

User projects written in or related to FreeBASIC.
Postmann
Posts: 2
Joined: Mar 14, 2017 15:18

Simple Font-Library

Postby Postmann » Mar 14, 2017 15:34

Hey :)

I wrote this for one of my projects. It loads and displays fonts loaded from bitmap-files. I added 5 fonts to the demo (The 3 default fonts from FreeBasic + 2 fonts I use in my project). No libraries needed, FB only.
Image
There are 3 commands/functions in it:

Code: Select all

DrawString (STRING, X, Y, FONT)     'draws a string with the specified font
ColorRGB (RED, GREEN, BLUE, FONT)   'works like "Color RGB()"
FontWidth (STRING, FONT)            'returns the length of a string in pixel

Download
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Simple Font-Library

Postby datwill310 » Apr 07, 2017 23:29

Looks interesting! May be very useful for my game (I quite like the fonts for screen modes like 20, but as they say, variety is the spice of life)!
xlucas
Posts: 302
Joined: May 09, 2014 21:19
Location: Argentina

Re: Simple Font-Library

Postby xlucas » May 09, 2017 4:24

That's very nice, mate. I was also thinking of a way to make fonts in FreeBasic that could change colour, like the default one, but that would also use different intensities and I came up with an idea I haven't tried yet.
Bad_Idea
Posts: 8
Joined: Sep 11, 2017 3:46

Re: Simple Font-Library

Postby Bad_Idea » Oct 01, 2017 10:47

Hey, I am "Postmann". I just forgot my password.

I've rewritten the library. It now supports nearly every latin/germanic based language and the €uro-sign. The fonts can be scaled, colored and rendered bold, italic and underlined. It is working on Windows, Linux and DOS.

Download from freebasic-portal.de
Image

The Download includes the fonts of the "GNU-FreeFonts" project. FreeSans, FreeSerif and FreeMono are saved in an own format.
When you want to include your own fonts, be sure to take a look at "TFont.Load()" in the file "font.bi". The first byte in the header contains the pixel-height of all characters. The next 101 bytes contain the width of every character. The rest is basically a compressed BMP-file, where I have rendered 101 characters (All printable Ascii-chars + the german letters "ÄÖÜäöüß" + €) into. Every bit is one pixel.
thebigh
Posts: 36
Joined: Dec 14, 2018 11:11

Re: Simple Font-Library

Postby thebigh » Dec 21, 2018 19:11

Hope it's OK to bump this, but I've got an issue that I can't understand. I'm testing this with the following code, compiling in Linux Mint 18.1 MATE 64 bit:

Code: Select all

const depth=8

#include once"gfx.bi"
#include once"font.bi"

screenres 640,480,depth

Dim As TFont Font
Font.Load("mono.fnt")

Font.DrawString(,"Hello, World!", 10, 10 )

sleep
end


This works fine with mono.fnt and serif.fnt. but when I try it with sans.fnt I get the following error:

Code: Select all

Error in `./hello': free(): invalid pointer: 0x0000000000d9a6a0


The demo.bas program that comes with the library works without issue. I can't see what my simpler code is doing differently. What am I doing wrong?
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Simple Font-Library

Postby sancho3 » Dec 22, 2018 6:29

I'm on Ubuntu and your code works fine.
Where is the error message getting "Free()" from? That function is not in any of the files.
Also make sure that the program is finding 'mono.fnt'. There is no error message or return value to indicate if load worked (BadIdea I recommend some kind of return value for the load procedure).
I had to use the full path to the file.
thebigh
Posts: 36
Joined: Dec 14, 2018 11:11

Re: Simple Font-Library

Postby thebigh » Dec 22, 2018 8:39

Yeah, mono and serif work fine, but sans gives me that weird error. The text gets displayed on the screen, but then it displays the error message in the terminal and crashes so that I have to Ctrl-C out of it.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Simple Font-Library

Postby sancho3 » Dec 22, 2018 8:56

Oh sorry, I didn't see that part of your post.
I just tried it with sans (on Ubuntu and it worked fine as does serif.
Perhaps sans.fnt is corrupt. Mine is 927 bytes.
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Re: Simple Font-Library

Postby Lachie Dazdarian » Dec 22, 2018 18:50

This is really cool.
badidea
Posts: 2184
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Simple Font-Library

Postby badidea » Dec 22, 2018 21:15

thebigh wrote:Hope it's OK to bump this, but I've got an issue that I can't understand. I'm testing this with the following code, compiling in Linux Mint 18.1 MATE 64 bit:
(code)
This works fine with mono.fnt and serif.fnt. but when I try it with sans.fnt I get the following error:

Code: Select all

Error in `./hello': free(): invalid pointer: 0x0000000000d9a6a0

The demo.bas program that comes with the library works without issue. I can't see what my simpler code is doing differently. What am I doing wrong?

Similar behaviour here (crash).
FBC32:
test: malloc.c:3722: _int_malloc: Assertion `(unsigned long) (size) >= (unsigned long) (nb)' failed.
FBC64:
*** Error in `./test': corrupted size vs. prev_size: 0x000000000110ac10 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f15737887e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x80dfb)[0x7f1573791dfb]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f157379553c]
./test[0x40fa09]
======= Memory map: ========
...


Changing depth gives different results.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Simple Font-Library

Postby sancho3 » Dec 23, 2018 0:13

I just want to add that I tried it with FBC32 on Ubuntu and FBC64 and no crashes.
However changing the depth to 16 or 32 does freeze the program on keypress to exit.

The problem narrows down to the line in font.bi and the load function:

Code: Select all

Get fntimg,(wholewide,0)-(wholewide+wide(i)-1,size-1),Char(i)

It looks like the get command can get from fntimg but often tries to write out of bounds in char(i).

In the font.load function where char(x) is imagecreated, it is done with height = size -1. That should actuallyl be size.
Making that change seems to prevent the freezing I see.

Code: Select all

         Char(i)=ImageCreate(wide(i),size)
         CharItalic(i)=ImageCreate(wide(i)+2,size)
         wholewide+=wide(i)
      Next
      Char(102)=ImageCreate(Wide(64),size)
      CharItalic(102)=ImageCreate(Wide(64)+2,size)
      Char(103)=ImageCreate(Wide(31),size)
      CharItalic(103)=ImageCreate(Wide(31)+2,size)
thebigh
Posts: 36
Joined: Dec 14, 2018 11:11

Re: Simple Font-Library

Postby thebigh » Dec 23, 2018 14:48

Nice! That is some impressive detective work.Now I know what the problem is, and that I am not going insane. Thanks a lot for the help.


EDIT: I believe I have found another small bug. Line 486, in the function TFont.Length, should read

Code: Select all

   For i As Integer=0 To Len(text)-1

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 4 guests