Where is AddPrivateFont() looking on Mac?

22 views
Skip to first unread message

Henry M

unread,
Jan 13, 2023, 2:14:39 PM1/13/23
to wx-users
I've been trying to add a font into my program for hours, but I can't figure out where wxWidgets is looking. I've setup my Info.plist file already:

<key>ATSApplicationFontsPath</key>
    <string>Fonts</string>

And I put the font file in AppName.app/Contents/Resources/Fonts, but the following line

wxFont::AddPrivateFont("font.ttf");

gives me this runtime error:

11:00:57: Font file "font.ttf" doesn't exist.

The only way I can get it to work is by hardcoding the entire path name:
wxFont::AddPrivateFont("/Users/user/Developer/AppName.app/Contents/Resources/Fonts/font.ttf");
And obviously, I don't want to do that.

What relative path can I give to AddPrivateFont so it finds the file?

Stefan Csomor

unread,
Jan 13, 2023, 2:48:13 PM1/13/23
to wx-u...@googlegroups.com

Hi

 

IMHO the code at wxFontBase::AddPrivateFont has a wrong check at the beginning, it does not search for the file at the right place, try just commenting that out, IIRC things should just work then ..

 

HTH

 

Stefan

 

Henry M

unread,
Jan 13, 2023, 3:19:40 PM1/13/23
to wx-users
Haha thanks! Wow, they should really fix that, seeing as the whole point of that function on Mac is to tell you if you've configured your font correctly.

Stefan Csomor

unread,
Jan 13, 2023, 4:43:29 PM1/13/23
to wx-u...@googlegroups.com

Hi

IMHO the code at wxFontBase::AddPrivateFont has a wrong check at the beginning, it does not search for the file at the right place, try just commenting that out, IIRC things should just work then ..

Haha thanks! Wow, they should really fix that, seeing as the whole point of that function on Mac is to tell you if you've configured your font correctly.

Sorry, I have to correct myself, apparently the API is meant to be using with the full path, not the filename only. So you’d have to use

 

wxStandardPaths::Get().GetResourcesDir() , append the ‘Fonts’ dir and your filename

 

when calling this function

 

Best,

 

Stefan

Henry M

unread,
Jan 13, 2023, 4:52:09 PM1/13/23
to wx-users
Ah gotcha. That wasn't clear to me. Thank you for the explanation.

Vadim Zeitlin

unread,
Jan 14, 2023, 8:55:56 AM1/14/23
to wx-u...@googlegroups.com
On Fri, 13 Jan 2023 12:19:40 -0800 (PST) Henry M wrote:

HM> Haha thanks! Wow, they should really fix that

Just a terminology note: "they" here is really the same as "you".
wxWidgets is a (traditional, as opposed to one controlled by some
corporation) open source project, meaning that it is developed by its users
and if you think something needs to be fixed, the best way to do it is to
fix it yourself.

On Fri, 13 Jan 2023 13:52:09 -0800 (PST) Henry M wrote:

HM> Ah gotcha. That wasn't clear to me. Thank you for the explanation.

I've amended the docs to make it hopefully more clear in 4a38b9ea34
(Document that wxFont::AddPrivateFont() needs an absolute path, 2023-01-14)
but this API remains somewhat clumsy. We might want to actually accept
relative paths in it, at least under Mac. But someone would need to make a
proposal about how exactly should the API behave and actually implement it
for this to happen.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Stefan Csomor

unread,
Jan 15, 2023, 7:17:11 AM1/15/23
to wx-u...@googlegroups.com
Hi

I've amended the docs to make it hopefully more clear in 4a38b9ea34
(Document that wxFont::AddPrivateFont() needs an absolute path, 2023-01-14)
but this API remains somewhat clumsy. We might want to actually accept
relative paths in it, at least under Mac. But someone would need to make a
proposal about how exactly should the API behave and actually implement it
for this to happen.

Thanks, Vadim.

Since on macOS the real behavior is done by the system and the info.plist entry, we'd just have to adjust the method when checking the filename, to behave the way I originally thought it would do anyway ... but is there some cross-platform way to embed fonts in an app so that this could be done everywhere ?

Best,

Stefan

Vadim Zeitlin

unread,
Jan 15, 2023, 3:08:17 PM1/15/23
to wx-u...@googlegroups.com
On Sun, 15 Jan 2023 12:17:04 +0000 Stefan Csomor wrote:

SC> Since on macOS the real behavior is done by the system and the
SC> info.plist entry, we'd just have to adjust the method when checking the
SC> filename, to behave the way I originally thought it would do anyway ...
SC> but is there some cross-platform way to embed fonts in an app so that
SC> this could be done everywhere ?

We could support embedding fonts as Win32 resources in wxMSW, I guess, and
then we could just use the font name (without path) to identify them, but
this wouldn't work under Linux and I don't see what else, i.e. other than
using files, can you do there.

As it is, AddPrivateFont() is clearly not the most straightforward API,
but by passing the full path to it, things do work under all platforms and
also allow you to use fonts not necessarily belonging to your application
(but, for example, selected by the user) under the platforms that support
it (i.e. non-Mac).

I don't see any other obvious improvements to make here, but I could, of
course, be missing something and any suggestions would be welcome.
Reply all
Reply to author
Forward
0 new messages