Re: [wx-users] How to get font filename from wxFont or "face name"?

537 views
Skip to first unread message

Vadim Zeitlin

unread,
Mar 30, 2009, 6:47:40 AM3/30/09
to wx-u...@lists.wxwidgets.org
On Sun, 29 Mar 2009 16:44:08 -0700 (PDT) Catalin <cat...@yahoo.com> wrote:

C> Is there a way to retrieve the file
C> name and/or file location of a font

Not really. Fonts may not even come from a (local) file at all -- just
imagine you use a font server under X. And what are you going to do with
this information anyhow?

Regards,
VZ

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

Ulrich Telle

unread,
Mar 30, 2009, 7:46:52 AM3/30/09
to wx-u...@lists.wxwidgets.org
Hi Catalin,

> Is there a way to retrieve the file name and/or file location of a font,
> starting from a wxFont or a font face name (or something else..)?
> ..For any platform, but any "partial" answer is welcomed.

As Vadim already stated this is generally not possible, let alone in a
portable way. But on some platforms you may achieve it, at least "almost".

On Windows it is possible for TrueType fonts to access the raw font data
starting from a wxFont
(http://wxforum.shadonet.com/viewtopic.php?t=19804), but there is no way
to find out the file name or location in a reliable way, although you
might try to guess the right file name from the font information in the
Windows registry.

On a Linux system where FontConfig is used for font management it is
possible to deduce the file name from a wxFont by applying the same
match methods as Pango uses internally. (I already wrote code to perform
this for my wxPdfDocument component, but the code is still unreleased.)

On Mac I know it can be done, but I don't have access to a Mac system to
verify it.

I didn't investigate the issue for other platforms (yet).

Regards,

Ulrich

Catalin

unread,
Mar 29, 2009, 7:44:08 PM3/29/09
to wx-u...@lists.wxwidgets.org
Hi,


Is there a way to retrieve the file name and/or file location of a font, starting from a wxFont or a font face name (or something else..)?
..For any platform, but any "partial" answer is welcomed.

Thanks,
Catalin


Catalin

unread,
Mar 30, 2009, 6:06:55 PM3/30/09
to wx-u...@lists.wxwidgets.org
Hi,
Thanks for your answers.

C> Is there a way to retrieve the file
C> name and/or file location of a font

VZ> Not really. Fonts may not even come from a (local) file at all -- just imagine you use a font server under X. And what are you going to do with this information anyhow?

True; actually I'm only interested in doing this for ttf.
I'm trying to correctly identify a font by its name. Or at least by the name that the OS associates it with.
The issue is that I see there are two "names" of a font (well, even more...). But the two that I'm referring to I encountered in msw and I'll try to describe them:
- the "Font Name"; how to see it: using the FontView application, when viewing the font sample text, it is the first field from the top;
- the "Title"; how to see it: going at the ttf font file -> Properties -> Details, the first field in the description.

Most of the times the two fields contain the same things but sometimes (Square 721 ttf) they do not.

UT> On Windows it is possible for TrueType fonts to access the raw font data starting from a wxFont (http://wxforum.shadonet.com/viewtopic.php?t=19804), but there is no way to find out the file name or location in a reliable way, although you might try to guess the right file name from the font information in the Windows registry.

Thanks for the hints, it crossed my mind but...
AFAICS Msw registry contains the "Title" of the font, while wxFont::GetFaceName returns the "Font name".
When registering fonts in a java framework (I need to to that...), they will have the "Title" assigned, and will be searched by the "Title" value, so not passing the correct value will result in a match failure (the whole process will be done on the same machine so at least one OS will do).
That framework needs the actual files, so one way of solving that problem would be to have the actual file name of the font file - then I wouldn't care about the name or title anymore, that's why I asked. The alternative of not knowing the file names can be to register "all" font files and then search the font by the allegedly correct name.
Another way would be to ask something else: why is that difference between the registry value (the "Title") and the wxFont facename ("Font Name")? Is there a way to retrieve the "Title" from wxFont? wxNativeFontInfo? something else?

An example of font Name table from MS I found here: http://www.microsoft.com/typography/otspec/namesmp.htm
And it makes me think that the "Font Name" == "Name ID 1" and "Title" == "Name ID 4", although this is an ideal case if I may say so. The issues I saw were more like "Square721 BT" != "Square 721 BT" (see the extra space).

I hope I am explicit enough so that you have a better idea now.

Thanks again,
C


Ulrich Telle

unread,
Mar 31, 2009, 4:48:42 PM3/31/09
to wx-u...@lists.wxwidgets.org
> The issue is that I see there are two "names" of a font (well, even
> more...). But the two that I'm referring to I encountered in msw and
> I'll try to describe them:
> - the "Font Name"; how to see it: using the FontView application, when
> viewing the font sample text, it is the first field from the top;
> - the "Title"; how to see it: going at the ttf font file -> Properties
> -> Details, the first field in the description.

The first field shows the full font name, usually language dependent.
The second field shows the font family.

> AFAICS Msw registry contains the "Title" of the font,

Unfortunately "title" is not a technical term. The font names in the
Windows registry correspond usually to the full font name BUT if the
font file contains the name in the language of the Windows installation
language that name is used. And sometimes additional information is
appended enclosed in parentheses.

> while wxFont::GetFaceName returns the "Font name".

At least when I tested it with wxGTK 2.8.9 wxFont::GetFaceName returned
the font family and not the font name, while it returned the full font
name on wxMSW as far as I remember.

Maybe the behaviour in wxWidgets 3.x will be more consistent.

> That framework needs the actual files, so one way of solving that
> problem would be to have the actual file name of the font file - then I
> wouldn't care about the name or title anymore, that's why I asked. The
> alternative of not knowing the file names can be to register "all" font
> files and then search the font by the allegedly correct name.

On Windows the situation is quite complex: not all fonts used by an
application need to be registered (=> no entry in the registry) and not
all font files need to reside in the Windows font directory. So it's not
an easy task to register *all* font files.

> Another way would be to ask something else: why is that difference
> between the registry value (the "Title") and the wxFont facename ("Font
> Name")?

Maybe one of the wxWidgets developers is able to answer this question.

> Is there a way to retrieve the "Title" from wxFont?
> wxNativeFontInfo? something else?

As I said in my previous post on Windows you can access the raw font
data using the native font info contained in wxFont:

LOGFONT lf;
wxFillLogFont(&lf, &font);

Then you could access the "name" table to extract the font names.

> An example of font Name table from MS I found here:
> http://www.microsoft.com/typography/otspec/namesmp.htm
> And it makes me think that the "Font Name" == "Name ID 1" and "Title" ==
> "Name ID 4", although this is an ideal case if I may say so. The issues
> I saw were more like "Square721 BT" != "Square 721 BT" (see the extra
> space).

Square721 BT <=> font family (name id 1)
Square 721 BT <=> full font name (name id 4)

You could use the PostScript name (name id 6) for identifying a font,
since that name should be unique. At least iText (the popular PDF
library written in Java) uses this name as the base name of a font. But
beware, not all fonts have a name id 6 entry.

Regards,

Ulrich

Ulrich Telle

unread,
Apr 1, 2009, 3:38:26 PM4/1/09
to wx-u...@lists.wxwidgets.org
Hi Catalin,

>> The first field shows the full font name, usually language dependent. The second
>> field shows the font family.
>

> Actually I think you mean the other way around.

If I open the system font viewer of Windows XP, the first line shows the
same information as the registry entry, essentially the full font name.
The next block shows some font information like file size, copyright,
version etc. Among these fields I see the font family. In German the
field is named "Schriftart", but I can't t tell it's name in the English
version.

> The field that I refer to as "Title" is more detailed.
> I'll give you another example for Arial font (Open type), I have 4 files: Arial.ttf, Arial Bold.ttf, Arial Bold Italic.ttf, Arial Italic.ttf.
> All of them have "Font Name" == "Arial", while the "Title"-s are: "Arial", "Arial Bold", "Arial Bold Italic", "Arial Italic" respectively.

Those names are the full font names.

> Although I'm not sure that any of them are in fact the family name, which AFAIU should be something more generic.

The font family name is "Arial".

>> The font names in the Windows registry correspond usually to the full font name BUT if the font file contains
>> the name in the language of the Windows installation language that name is used.
>> And sometimes additional information is appended enclosed in parentheses.
>

> Sorry, I didn't understand your explanation here.

As I have the German version of Windows I see for example the font name
"Arial Fett" instead of "Arial Bold". A font may have full font names in
different languages in it's "name" table. If for example the name table
of a font contains a Chinese name, on a Chinese Windows system this
Chinese font name would be displayed.

> wxFont::GetFaceName, under msw, returns "Font name" == "Name ID 1" in ttf Names Table == "Arial" if applied to the previous example, in this mail == "Square721 BT" (the form without one space character) if applied to the example from my previous mail.

You are right. Maybe I confused the issue.

> Or is there a way (in MFC for example, if anyone happens to know) to get the complete Names Table of a font?

The Windows API function GetFontData may be used for that purpose. The
code I mentioned in a previous post
(http://wxforum.shadonet.com/viewtopic.php?t=19804) is used to get the
complete raw font data, but GetFontData could be restricted to return
only the "name" table.

>>> Is there a way to retrieve the "Title" from wxFont? wxNativeFontInfo? something else?
>> As I said in my previous post on Windows you can access the raw font data using
>> the native font info contained in wxFont:
>>
>> LOGFONT lf;
>> wxFillLogFont(&lf, &font);
>

> LOGFONT structure contains only one field for name (lfFaceName),
> and that is the "Font Name" referred in these posts ("Name ID 1" from
ttf Names Table),
> which is (if I did not miss anything) also the _directly_
> returned value of wxFont::GetFaceName.

I didn't say that the field lfFaceName is of any use, but a LOGFONT can
be used to get access to the raw font data (see link above).

>> Then you could access the "name" table to extract the font names.
>

> Well... sorry for asking in case it is obvious, but how to do that ?

Documentation of the GetFontData function can be found here:

http://msdn.microsoft.com/en-us/library/dd144885(VS.85).aspx

> Ok, so in case your interpretation of the fields is correct, then wxFont::GetFaceName returns the font family in msw too.

You are right.

> But then, what would wxFont::GetFamily return? :)

For your example font Square721 BT you would get wxSWISS.

>> You could use the PostScript name (name id 6) for identifying a font, since that
>> name should be unique.
>

> I don't know a way to get to it, but I don't think it will help much - see below.

Why not? Usually the PostScript name *is* available and *is* unique.

>> At least iText (the popular PDF library written in Java)
>> uses this name as the base name of a font.
>

> Sorry, but I'll have to disagree with you here - iText can retrieve the PostScript name of a font (even set it), but it will not use it to create a font.

If you use the font factory and register the system fonts (I don't
remember whether they are registered automatically or not at start up),
you can use the GetFont method of the font factory specifying the
PostScript name as the font name.

> To create a com.lowagie.text.pdf.BaseFont using createFont( String name, [...] ), the 'name' parameter == "Title" == Name ID 4 == "full font name" in your interpretation.
> As opposed to that,
> wxFont(int pointSize, wxFontFamily family, int style, wxFontWeight weight, const bool underline = false, const wxString& faceName = "", wxFontEncoding encoding = wxFONTENCODING_DEFAULT)
> will need that faceName == "Font Name" == Name ID 1.

So you may want to extract the family name (name id 1) and the subfamily
name (name id 2). The latter allows to identify the font style and weight.

> I'm not by any means saying that the implementation is wrong in any way, I'm just wandering if I can get any more info about the used font in wxW.

You could use wxFont method GetNativeFontInfoDesc or
GetNativeFontInfoUserDesc.

> IMO strange things happen in Windows :)

Definitely.

> for a font registered with the OS, the Registry will hold some value
as its "name", but the programatically used value (LOGFONT field) is a
different one, even if many time they are identical... If it matters,
the native widget wxFontDialog also gets font names from Name ID 1...

Yes, the fonts are located using the font family and the font style and
weight.

Regards,

Ulrich

Catalin

unread,
Apr 1, 2009, 9:56:42 AM4/1/09
to wx-u...@lists.wxwidgets.org

Hi,

Sorry for the formatting in the previous mail...

> Re: How to get font filename from wxFont or "face name"?


>
> > The issue is that I see there are two "names" of a font (well, even more...).
> > But the two that I'm referring to I encountered in msw and I'll try to describe
> > them:
> > - the "Font Name"; how to see it: using the FontView application, when viewing
> > the font sample text, it is the first field from the top;
> > - the "Title"; how to see it: going at the ttf font file -> Properties ->
> > Details, the first field in the description.
>

> The first field shows the full font name, usually language dependent. The second
> field shows the font family.

Actually I think you mean the other way around. The field that I refer to as "Title" is more detailed.


I'll give you another example for Arial font (Open type), I have 4 files: Arial.ttf, Arial Bold.ttf, Arial Bold Italic.ttf, Arial Italic.ttf.
All of them have "Font Name" == "Arial", while the "Title"-s are: "Arial", "Arial Bold", "Arial Bold Italic", "Arial Italic" respectively.

Although I'm not sure that any of them are in fact the family name, which AFAIU should be something more generic..

> > AFAICS Msw registry contains the "Title" of the font,
>
> Unfortunately "title" is not a technical term.

I didn't claim that I speak in technical terms here, I was just trying to make things clear for anybody who reads this, using the terms which seamed like the names of the corresponding fields. :-)
I've explained above how I got to the "Font Name" item, and "Title" item of a font (ttf...).

> The font names in the Windows registry correspond usually to the full font name BUT if the font file contains
> the name in the language of the Windows installation language that name is used.
> And sometimes additional information is appended enclosed in parentheses.

Sorry, I didn't understand your explanation here.

> > while wxFont::GetFaceName returns the "Font name".


>
> At least when I tested it with wxGTK 2.8.9 wxFont::GetFaceName returned the font
> family and not the font name, while it returned the full font name on wxMSW as
> far as I remember.
>
> Maybe the behaviour in wxWidgets 3.x will be more consistent.

wxFont::GetFaceName, under msw, returns "Font name" == "Name ID 1" in ttf Names Table == "Arial" if applied to the previous example, in this mail == "Square721 BT" (the form without one space character) if applied to the example from my previous mail.

> > The alternative of not knowing the file names can be to register "all" font files and then search the


> > font by the allegedly correct name.
>
> On Windows the situation is quite complex: not all fonts used by an application
> need to be registered (=> no entry in the registry) and not all font files need
> to reside in the Windows font directory. So it's not an easy task to register
> *all* font files.

Right, I meant "get *all* fonts that are registered with windows, so the ones that have Registry entries".

> > Another way would be to ask something else: why is that difference between the
> > registry value (the "Title") and the wxFont facename ("Font Name")?
>
> Maybe one of the wxWidgets developers is able to answer this question.

Or is there a way (in MFC for example, if anyone happens to know) to get the complete Names Table of a font?

> > Is there a way to retrieve the "Title" from wxFont? wxNativeFontInfo? something else?


>
> As I said in my previous post on Windows you can access the raw font data using
> the native font info contained in wxFont:
>
> LOGFONT lf;
> wxFillLogFont(&lf, &font);

LOGFONT structure contains only one field for name (lfFaceName), and that is the "Font Name" referred in these posts ("Name ID 1" from ttf Names Table), which is (if I did not miss anything) also the _directly_ returned value of wxFont::GetFaceName.

> Then you could access the "name" table to extract the font names.

Well... sorry for asking in case it is obvious, but how to do that ?

> > An example of font Name table from MS I found here:
> > http://www.microsoft.com/typography/otspec/namesmp.htm
> > And it makes me think that the "Font Name" == "Name ID 1" and "Title" == "Name


> > ID 4", although this is an ideal case if I may say so. The issues I saw were
> > more like "Square721 BT" != "Square 721 BT" (see the extra space).
>
> Square721 BT <=> font family (name id 1)
> Square 721 BT <=> full font name (name id 4)

Ok, so in case your interpretation of the fields is correct, then wxFont::GetFaceName returns the font family in msw too. But then, what would wxFont::GetFamily return? :)

> You could use the PostScript name (name id 6) for identifying a font, since that
> name should be unique.

I don't know a way to get to it, but I don't think it will help much - see below.

> At least iText (the popular PDF library written in Java)


> uses this name as the base name of a font.

Sorry, but I'll have to disagree with you here - iText can retrieve the PostScript name of a font (even set it), but it will not use it to create a font.

To create a com.lowagie.text.pdf.BaseFont using createFont( String name, [...] ), the 'name' parameter == "Title" == Name ID 4 == "full font name" in your interpretation.
As opposed to that,
wxFont(int pointSize, wxFontFamily family, int style, wxFontWeight weight, const bool underline = false, const wxString& faceName = "", wxFontEncoding encoding = wxFONTENCODING_DEFAULT)
will need that faceName == "Font Name" == Name ID 1.

I'm not by any means saying that the implementation is wrong in any way, I'm just wandering if I can get any more info about the used font in wxW.
IMO strange things happen in Windows :) for a font registered with the OS, the Registry will hold some value as its "name", but the programatically used value (LOGFONT field) is a different one, even if many time they are identical... If it matters, the native widget wxFontDialog also gets font names from Name ID 1...

> Regards,
>
> Ulrich

Thank you for your inputs. I really appreciate it!
Catalin



Reply all
Reply to author
Forward
0 new messages