Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: AGSzabo on June 08, 2006, 07:56:18 PM

Title: how do i get a pointer to the default system font?
Post by: AGSzabo on June 08, 2006, 07:56:18 PM
at the moment i use
Code: [Select]
move.l intbase(pc),a0
move.l ib_FirstScreen(a0),a0
move.l sc_RastPort+rp_Font(a0),a5


but how do i get a pointer to the default system font?
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 08, 2006, 08:02:31 PM
And what if there is no screen (ib_FirstScreen == 0) or if the screen you read the rastport from closes? Or if the font in question is flushed while you use it?

Anyway, if you insist: graphics.library base gb_DefaultFont has the current default screen font. It still doesn't gurantee a lock on it, so you should copy parameters from it withint Forbid() and call OpenFont(). Only then you're guaranteed to have valid lock on it.

System Font is trickier, basically one bulletproof way to get it is to read ENV:Sys/Font.prefs using iffparse.library. The data is multiple struct FontPrefs with system font prefs having fp_Type FP_SYSFONT.
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 08, 2006, 08:09:01 PM
yes, thats why i ask. i need to know the font that a new window will get by default but prior to opening that window.
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 08, 2006, 08:32:16 PM
The official way is to Lock the public screen you want to open on and then use GetScreenDrawInfo(). dri_Font will then have pointer to font to be used for the window. This is the font that is used for titlebar, and window RPort default font.

Or do you really mean system font (used for console text)?
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 08, 2006, 09:00:51 PM
Quote
Or do you really mean system font (used for console text)?


no. thank you.
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 09, 2006, 08:52:32 PM
what if i want to know the default font prior to opening the screen for my app?
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 09, 2006, 10:12:01 PM
Quote
what if i want to know the default font prior to opening the screen for my app?

Several ways to handle that, but the easist would be to open temp screen with SA_Behind,TRUE, then get font specifics from screen rastport font (clone the name, ysize and style). CloseScreen the temp screen.

You now have the font info, which you can for example use to open the font with OpenDiskFont().
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 10, 2006, 01:10:48 PM
ok, i am curious about "the other methods"?
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 10, 2006, 04:47:46 PM
Method 1:
- use iffparse.library to read env:sys/fonts.prefs
- The entry with fp_Type of FP_SCREENFONT is the one defining the default screen font.

Method 2:
- LockPubScreen("Workbench")
- GetScreenDrawInfo and copy info from dri_Font
- FreeScreenDrawInfo
- UnLockPubScreen
This might not return any font though, in case there is no Workbench screen.

Method 3:
- Forbid
- copy font details from GfxBase gb_DefaultFont
- Permit
This is a hack. It really should not be used.
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 10, 2006, 04:56:33 PM
its really great that there IS response in the amiga scene! that amiga is alive! thank you for your very good answer!

i currently use LockPubScreen(NULL) and am curious if that works on systems with gfxcard, where there is no workbench but some other default screen that i will get by LockPubScreen(NULL) instead?
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 10, 2006, 05:09:32 PM
LockPubScreen(NULL) will get you the default public screen (99% of the time this is the Workbench screen). You can get some other screen aswell though, and in theory it could use some other font than the system default.
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 10, 2006, 07:35:47 PM
btw, why should i forbid while reading gfxbase?
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 10, 2006, 07:44:49 PM
Quote
btw, why should i forbid while reading gfxbase?

Because there is no way to arbitrate the access to it. Without Forbid(), and if the default system font was changed, you could end up with stale pointer when the OS CloseFont()'s the font.

With intuitionbase you can lock = LockIBase(0), read values and then UnlockIBase(lock).
Title: Re: how do i get a pointer to the default system font?
Post by: AGSzabo on June 10, 2006, 08:40:11 PM
i doubt that something wonderfull will happen just in the same time when i access gfxbase or an existing screen. its more likely that you win lotto.
Title: Re: how do i get a pointer to the default system font?
Post by: Piru on June 10, 2006, 09:03:47 PM
@AGSzabo
Quote

i doubt that something wonderfull will happen just in the same time when i access gfxbase or an existing screen.

It can, since this is a multitasking system. Once you've fetched the pointer, a reschedule can occur. Your task might be asleep for a very long time, and during this time the font might be closed.

Quote
its more likely that you win lotto.

Could be, but if such similar race conditions pile up, the probability for failure grows aswell. If it just takes Forbid/Permit to avoid them, better use it.