Get a font filename based on Font Name and Style (Bold/Italic) - c++

This has been driving me crazy all day.
I need to get a font filename (eg. Arial.ttf) based on its name (Arial in this case) and whether it is bold, italic or both. Using those pieces of information, I need to find the font file so I can use it for rendering.
Some more examples:
Calibri, Bold would resolve to calibrib.ttf.
Calibri, Italic would resolve to calibrii.ttf.
Any ideas on how I could achieve this in C++ (Win32)

First, to my knowledge, there is no reliable way to do that.
The Windows API deals with font families and mappings, not with font files, which are dealt with at a lower level. Also note that even if you manage to get the file name of a font, no rendering function (that I know of) will accept it, so what will you do with it?
That said, you can look in the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts in order to obtain the file name of a font from its logical name. An implementation of that solution can be found here.

Related to the earlier posts, this seems to be a reliable way:
1) Read the registered Windows font list from
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts\
You will obtain file names and alternate file paths here.
The Font names are not useful as they can change with user's locale.
2) Load the TrueType files (.ttf, .ttc, .otf):
Use FreeType https://www.freetype.org/). Just initialize the freetype library and load face with FT_New_Face(library, path, 0, &face).
3) Obtain the font Family name using FreeType.
Use FT_Get_Sfnt_Name_Count() and FT_Get_Sfnt_Name() to obtain the string table.
You will need to check if the encoding is Ansi, UTF16 or other, as some strings will be in multiple different languages and encodings.
4) Obtain the OS2 TrueType properties.
Use (TT_OS2 *) FT_Get_Sfnt_Table (face, ft_sfnt_os2) to get the OS2 structure.
Interpret the structure using docs like https://www.microsoft.com/typography/otspec/os2.htm#fc
5) Now you have font file path, family name, style properties and other information. Build a list of these and function to search for a file based on font family and style.

This Code Project project does what you want. As-is it fails on Windows 7 because the GetWinVer function stops at XP. It is trivial to add the case for Windows 7.

You normally do this by calling CreateFontIndirect and then getting the system to render. Perhaps you could explain why you can't use this standard approach.

One solution would be to access the font files and extract the name from the name table to create your own lookup (an STL map would be a simple way of doing that). Details of the TTF file format can be found here.

Related

Substitute Wingding fonts for linux

I am using Java aspose.words and trying to build a pdf from docx/ppt in linux. The docx-document has an list with bulletpoints. These bulletpoints use the symbol font.
When i create the pdf with aspose these bulletpoints are shown in webdings font as a clapperboard.
I did not find any free font (for commercial use) that is an equivalent to the symbol font. Does anyone know a good solution to show correct bulletpoints in list?
I found the way to substitute fonts, but i don't know which font to use:
TableSubstitutionRule tableSubstitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
tableSubstitutionRule.addSubstitutes("Symbol", "?WHICH_FONT?");
It might be a known peculiarity. Windows “Symbol” font is a symbolic font (like “Webdings”, “Wingdings”, etc.) which uses Unicode PUA. Thus substitution of this font will cause different glyphs rendering. Provided Mac/Linux “Symbol” font on the other hand is a proper Unicode font (for example Greek characters are in the U+0370…U+03FF Greek and Coptic block). So these fonts are incompatible and Mac/Linux “Symbol” font cannot be used instead of Windows “Symbol” without additional actions. In this particular case you have to change the bullet codepoint from PUA U+F0B7 (or U+00B7 which also can be used in MS Word for symbolic fonts) to the U+2022 in the document to use the Mac “Symbol” font. See the following code for example:
Document doc = new Document("/Users/mac1/Downloads/in.docx");
for (com.aspose.words.List lst : doc.getLists())
{
for (com.aspose.words.ListLevel level : lst.getListLevels())
{
if (level.getFont().getName().equals("Symbol") && level.getNumberFormat().equals("\uF0B7"))
{
level.setNumberFormat("\u2022");
}
}
}
doc.save("/Users/mac1/Downloads/out.pdf");
If this does not help, please post your question in Aspose.Words support forum and attach your input and output document there.

Django - suing font awersome in unicode form

I want to use font awersome in my project in the way, that user can choose which icon he wants. I found django-fontawesome-5 but there is one problem - there is no access to icon's unicode and I need it for one javascript component (html doesn't work there, and unicode does). I was looking all over the internet, but I coundn't find anything that would allow me to add font-awersome icons, with their unicodes somhow stored. My question is do you know how to get this feature?
The unicode codes are stored in icons.json and in icons_semantic_ui.json
Since you've got the codes source you can define a custom templatetag or a model/mixin method or a function which just gets a code from one of those json files using icon name
You can see example in fontawesome_5/utils.py

Windows API To Access Font Tables (Kern, GPOS, etc)

Currently Apple provides functions to access data in font tables, like CTFontCopyTable. I'm using it to parse information (kerning, etc) out of a font when available. Is there any similar way of pulling the same data on Windows per font?
I can see a lot of documentation on the windows side when it comes to these font tables, but I can't seem to find a way to pull this data per font.
Here is how I'm pulling the data in osx:
CTFontRef lCTFont = CTFontCreateWithName((CFStringRef)lNSFontName, 800.0f, NULL);
CFDataRef lKernTable = CTFontCopyTable(lCTFont, kCTFontTableKern, kCTFontTableOptionNoOptions);
CFDataRef lGPOSTable = CTFontCopyTable(lCTFont, kCTFontTableGPOS, kCTFontTableOptionNoOptions);
GetFontData will get the raw table data, but as other suggestions advise, you will probably want to use the system-provided text layout methods rather than trying to roll your own.
You can use GetKerningPairs to get kerning data and GetCharacterPlacement to get GPOS data.
If your real intent is to simply render some text correctly though, you might want to use Uniscribe instead.

Path shortener with MFC

I need to display paths in the context menu in my app and need to find a way to shorted them. For instance, what Microsoft apps do in their "Recently Used" list, if the original path is c:\ClientName\ProjectName\ProgramName\ComponentName\SomeFileName.cpp I need it to be converted into something like c:\ClientName\...\SomeFileName.cpp.
So I'm curious if there's any built-in means to do this with C++/MFC or maybe a WinAPI?
Use PathCompacPathEx that will truncate a path by replacing path components with ellipses.
There is Win32 API to get short path name called GetShortPathName, read below. It may help.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx

How to get paper.print() to work?

I have been trying to get the following Raphael code to just write something on the screen with no luck!
paper.print(30, 15, "TEXT", paper.getFont("Arial"), 20).attr({fill: "black"});
Is there anything else you need to do to get the text in the paper?!?!!?
You need to cufonize a font, being sure to indicate that the cufonized font should register itself with Raphael, and include the resulting .js file before you can use getFont to retrieve it (there are no fonts available by default). If you check, I'm reasonably sure you'll find that paper.getFont("Arial") is returning undefined.
Cufon essentially converts every glyph in a provided font into its vector equivalent -- Raphael simply transforms and sequences those paths to produce output.