Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Freefont error
#1
Sad 
Inspired by @grymmjack's comment about the fonts, I looked at "_LoadFont" in the wiki. The first example works, but I want that the output to return to the standard font at the end. Unfortunately, I get an error message with "_FreeFont"; I looked up what the error number says, but I don't know where the error is.

Can anyone tell me where the error is in the program? Thanks!

Code: (Select All)

'Fontuebung mit _LoadFont aus dem Wiki - 27. Juni 2024

Option _Explicit

Dim As String rootpath, fontfile, style
Dim As Long f

rootpath = Environ$("SYSTEMROOT") 'normally "C:\WINDOWS"

fontfile = rootpath + "\Fonts\cour.ttf" 'TTF file in Windows
style = "monospace" 'font style is not case sensitive

f = _LoadFont(fontfile, 30, style)
_Font f

Print "Hello!"

_FreeFont f

End
Reply
#2
The issue is mentioned in the Wiki, but maybe isn't made that obvious:

> You cannot free a font which is in use. Change the font to a QB64 default font size before freeing the handle (see example below).

Basically, doing a `_FreeFont f` while `_Font f` is in use isn't allowed, it errors. To restore the default font you should use `_Font 16`, and after that is done you can do `_FreeFont f` with no issue.
Reply
#3
(06-27-2024, 08:02 PM)DSMan195276 Wrote: The issue is mentioned in the Wiki, but maybe isn't made that obvious:

> You cannot free a font which is in use. Change the font to a QB64 default font size before freeing the handle (see example below).

Basically, doing a `_FreeFont f` while `_Font f` is in use isn't allowed, it errors. To restore the default font you should use `_Font 16`, and after that is done you can do `_FreeFont f` with no issue.
Thanks for the quick reply, but the result looks like this: Before _ Font 16 and with _ Font 16. Why doesn't that work?

Code: (Select All)

f = _LoadFont(fontfile, 30, style)
_Font f

Print "Hello!"

_Font 16
_FreeFont f

[Image: Vor-Font16-danach.jpg]

I just remembered that I tried something about this and it worked.

Code: (Select All)

'Text im Grafikbildschirm positionieren - 8. April 2023

Option _Explicit

Screen _NewImage(600, 440, 32)

Dim As Integer schrift
Dim As String text

schrift = _LoadFont("C:\WINDOWS\Fonts\courbd.ttf", 24, "Bold")
_Font schrift

'Notwendig fuer Locate Zeile 22
Locate 3, 10
text = String$(20, "X")

'Positionierung in Grafikpixeln!
_PrintString (20, 20), text

'Die Positionierung durch Grafikpixel
'wird NICHT beruecksichtigt.
Locate CsrLin + 2, 20
Print "Screen width is 80 chars"

Locate CsrLin + 2, 297
Print "Middle"
'_PrintString (35, 300), "Mitte"

'Neue Schriftgroesse
'Eigenartiges Verhalten bei der Darstellung und Positionierung.
'Nach was richtet die sich?
_Font 16

'Damit geht es nach Pixel, und umgekehrt: Spalte - Zeile
'Locate ist: Zeile - Spalte
_PrintString (22, 220), "Middle"

'Das bedeutet: ", X mal 6 Buchstaben"
Locate CsrLin + 14, 38
Print "Middle"

_FreeFont schrift

End
Reply
#4
(06-27-2024, 08:27 PM)Kernelpanic Wrote:
(06-27-2024, 08:02 PM)DSMan195276 Wrote: The issue is mentioned in the Wiki, but maybe isn't made that obvious:

> You cannot free a font which is in use. Change the font to a QB64 default font size before freeing the handle (see example below).

Basically, doing a `_FreeFont f` while `_Font f` is in use isn't allowed, it errors. To restore the default font you should use `_Font 16`, and after that is done you can do `_FreeFont f` with no issue.
Thanks for the quick reply, but the result looks like this: Before _ Font 16 and with _ Font 16. Why doesn't that work?

Code: (Select All)

f = _LoadFont(fontfile, 30, style)
_Font f

Print "Hello!"

_Font 16
_FreeFont f

The above doesn't work as you aren't specifying the font name or style when loading it.

Code: (Select All)
fontfile$ = "C:\WINDOWS\Fonts\courbd.ttf"
style$ = "Bold,Monospace"
f = _LOADFONT(fontfile$, 30, style$)
_FONT f

PRINT "Hello!"

_FONT 16
_FREEFONT f

One thing to remember, any call to _FONT will reset your print cursor location, as different fonts are going to be different heights and such.

If you call _FONT, the next PRINT statement is always going to be at the top left of the screen, just as if you'd called CLS.
Reply
#5
Quote:One thing to remember, any call to _FONT will reset your print cursor location, as different fonts are going to be different heights and such.

If you call _FONT, the next PRINT statement is always going to be at the top left of the screen, just as if you'd called CLS.
I understand that, what I don't understand is why the font doesn't change. No matter what I try, it doesn't work.
Either it doesn't work or I don't understand how it works.

Errors at 16 and 25 -- _FreeFont f/myFont
Code: (Select All)

Option _Explicit

Dim As String rootpath, fontfile, fontfile2, style, style2
Dim As Long f, f2, myfont

rootpath = Environ$("SYSTEMROOT") 'normally "C:\WINDOWS"

fontfile = rootpath + "\Fonts\cour.ttf" 'TTF file in Windows
style = "monospace" 'font style is not case sensitive

f = _LoadFont(fontfile, 30, style)
_Font f

Print "Hello!"
_FreeFont f

'fontfile2 = "C:\WINDOWS\Fonts\courbd.ttf"
'style2 = "Bold,Monospace"

fontfile2 = "C:\Windows\Fonts\Dauphinn.ttf"
style2 = "Bold"

myfont = _LoadFont("C:\Windows\Fonts\Dauphinn.ttf", 25, "")
_Font myfont

'f2 = _LoadFont(fontfile2, 30, style2)
'_Font f2

Locate 3, 5
Print "Hello!"

_Font 16
_FreeFont myfont 'f2

End

[Image: Dauphin1.jpg]

[Image: Dauphin2.jpg]
Reply
#6
The reason why you're getting errors is because the font is still active.   

It's just like trying to turn the engine off on your car -- you have to be in park and not moving.  You certainly can't just turn if off while driving down the road and in drive!

Let me ask you this -- what Font is in use at line 16? 


As the line 24 issue, let me ask this:   What screen mode are you in?   Does SCREEN 0 have any limitations to the fonts that we can use with it?

Hint:  Screen 0 requires fonts to be MONOSPACED, as it doesn't work with variable-width fonts. 
Hinr2:  Screen 0 only uses ONE font for the whole screen.  When you change font on SCREEN 0, you change all the text to use that font.

Code: (Select All)

OPTION _EXPLICIT

DIM AS STRING rootpath, fontfile, fontfile2, style, style2
DIM AS LONG f, f2, myfont

rootpath = ENVIRON$("SYSTEMROOT") 'normally "C:\WINDOWS"

fontfile = rootpath + "\Fonts\cour.ttf" 'TTF file in Windows
style = "monospace" 'font style is not case sensitive

f = _LOADFONT(fontfile, 30, style)
_FONT f

PRINT "Hello!"
SLEEP
_FONT 16 'use a different font, such as the one built in to QB64PE, before trying to free a font in use.
SLEEP

_FREEFONT f

'fontfile2 = "C:\WINDOWS\Fonts\courbd.ttf"
'style2 = "Bold,Monospace"

fontfile2 = "C:\Windows\Fonts\Dauphinn.ttf"
style2 = "Bold"

myfont = _LOADFONT("C:\Windows\Fonts\courbd.ttf", 25, "Monospace") 'You're in SCREEN 0. MUST be monospaced.
_FONT myfont

'f2 = _LoadFont(fontfile2, 30, style2)
'_Font f2

LOCATE 3, 5
PRINT "Hello!"
SLEEP
_FONT 16
_FREEFONT myfont 'f2

END


See the above? No errors anymore, but since this is SCREEN 0, *all* the text on the screen changes fonts each time we change our font. The SLEEP statements here should allow you to slowly go through and see that.

If you want to use more than a single font for the whole screen, use a graphics screen such as SCREEN _NEWIMAGE(x, y, 32).
Reply
#7
I discovered I actually can turn my car off while still in drive. Dammit Steve, you owe me a new front end plus a garage door repair.

@KP. Stay with it. Properly freeing fonts takes a bit of getting used to but in larger apps it will save you from potential out of memory errors. 

Pete

- Critical Error 101 (Out of Beer).
Reply
#8
Quote:Hint:  Screen 0 requires fonts to be MONOSPACED, as it doesn't work with variable-width fonts. 
Hinr2:  Screen 0 only uses ONE font for the whole screen.  When you change font on SCREEN 0, you change all the text to use that font.
Ok, now I understand how it works; I think so.  Rolleyes  Thanks!
Reply
#9
You say you don't want limitations
Well you know
We all want to change our fonts
You say you have anticipations
Well you know
You have to work with what you've got
But if you start coding around in graphics screens
You ain't going to create an app that works worth beans
Don't you know it's gonna be
Alright...

Pete Big Grin (Follow me!)

- I'm the same bot I used to be.
Reply
#10
you say you want a revolution
well you know you can get out of screen 0
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)