9

I came across a situation where we needed to know the filename of a font currently in use by a QFont. Knowing that a QFont can give us the font family and Windows HFONT handle.

The font family is not enough, because manipulating styles like Bold or Italic can result in Windows selecting a different font file. (f.e. arial.ttf, arialbd.ttf, arialbi.ttf, ariali.ttf).

This code sample should give us <path>\arial.ttf:

QFont font("Arial", 12);
FindFontFileName(font.handle());

while this code sample should give us <path>\arialbi.ttf

QFont font("Arial", 12);
font.setStyle(QFont::StyleItalic);
font.setWeight(QFont::Bold);
FindFontFileName(font.handle());
7
  • There are a surprising number of people who have wanted to do this: Get a font filename based on Font Name and Style (Bold/Italic); Get a font filepath from name and style in C++/Windows. Weird, given I can't think of a situation where it would be required. Seems like bad design to me. May 27, 2013 at 9:13
  • @CodyGray: in our application, users add their own fonts to a library. The user creates content using these fonts (they only select font family and apply some styles like you would in your favorite text editor). At the time the user deploys the content to a remote destination, the application must know which fonts to include in deployment. May 27, 2013 at 9:16
  • 1
    Not sure that makes it not a duplicate. It's trivial to retrieve the font family when given an HFONT, just call GetObject to retrieve a LOGFONT. But don't interpret that as saying your advice is not useful or unwelcome—I upvoted both the question and answer as well. May 27, 2013 at 9:36
  • 1
    This is something I want to do also. My reason is for solving customers' font installation issues for our font-intensive software application product. Although fonts normally reside in the official Windows Fonts folder, they don't have to. Font management products can move them around. Also some products that install fonts don't do things right. Finally, people mess with their registry. BTW, Mac OSX has an API to do the equivalent. Dec 5, 2015 at 18:10
  • 1
    @CodeGray for embedding fonts in PDF files, for example, you need to know the name of the font file.
    – Pierre
    May 31, 2016 at 22:29

1 Answer 1

11

The Windows API Font and Text Functions doesn't contain a function that returns the filename of a font. So a more creative solution has to be worked out.

The solution is to use the GetFontData function, which will give us the exact copy of the original font file. The only thing that's left is comparing this data with the contents of all installed/known fonts.

Lookup table

We will first create a lookup table (FontList) of all installed/known fonts:

#define FONT_FINGERPRINT_SIZE    256
struct FontListItem
{
    std::string FileName;
    int FingerPrintOffset;
    char FingerPrint[FONT_FINGERPRINT_SIZE];
};

std::multimap< size_t, std::shared_ptr<FontListItem> > FontList;

The FingerPrint is a random part read from the font file in order to distinguish between fonts of the same filesize. You could also use a hash (f.e. SHA1) of the complete file to establish this.

Adding fonts

Method for adding a single font to this list is pretty straightforward:

void AddFontToList(const std::string& fontFileName)
{
    std::ifstream file(fontFileName, std::ios::binary | std::ios::ate);
    if (!file.is_open())
        return;

    size_t fileSize = file.tellg();
    if (fileSize < FONT_FINGERPRINT_SIZE)
        return;

    std::shared_ptr<FontListItem> fontListItem(new FontListItem());
    fontListItem->FileName = fontFileName;
    fontListItem->FingerPrintOffset = rand() % (fileSize - FONT_FINGERPRINT_SIZE);
    file.seekg(fontListItem->FingerPrintOffset);
    file.read(fontListItem->FingerPrint, FONT_FINGERPRINT_SIZE);
    FontList.insert(std::pair<size_t, std::shared_ptr<FontListItem> >(fileSize, fontListItem));
}

A Qt way to add all Windows fonts to the lookup table goes like this:

const QDir dir(QString(getenv("WINDIR")) + "\\fonts");
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
foreach (const QFileInfo fileInfo, dir.entryInfoList())
    AddFontToList(fileInfo.absoluteFilePath().toUtf8().constData());

File enumeration can also be done using FindFirstFile/FindNextFile Windows API functions, but would be less readable for the purpose of this answer.

GetFontData helper

Then we create a wrapper function for the GetFontData function that creates a DC, selects the font by the HFONT handle and returns the fonts data:

bool GetFontData(const HFONT fontHandle, std::vector<char>& data)
{
    bool result = false;
    HDC hdc = ::CreateCompatibleDC(NULL);
    if (hdc != NULL)
    {
        ::SelectObject(hdc, fontHandle);
        const size_t size = ::GetFontData(hdc, 0, 0, NULL, 0);
        if (size > 0)
        {
            char* buffer = new char[size];
            if (::GetFontData(hdc, 0, 0, buffer, size) == size)
            {
                data.resize(size);
                memcpy(&data[0], buffer, size);
                result = true;
            }
            delete[] buffer;
        }
        ::DeleteDC(hdc);
    }
    return result;
}

Font filename lookup

Now we're all set for looking up the exact filename of a font by only knowing the HFONT handle:

std::string FindFontFileName(const HFONT fontHandle)
{
    std::vector<char> data;
    if (GetFontData(fontHandle, data))
    {
        for (auto i = FontList.lower_bound(data.size()); i != FontList.upper_bound(data.size()); ++i)
        {
            if (memcmp(&data[i->second->FingerPrintOffset], i->second->FingerPrint, FONT_FINGERPRINT_SIZE) == 0)
                return i->second->FileName;
        }
    }
    return std::string();
}
1
  • Note that not all fonts are stored in %WINDIR%\Fonts. Font paths are stored in a registry setting, HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts -- see stackoverflow.com/a/39093093/1836776 for more details, including why font names stored in that registry key are not reliable. Dec 19, 2021 at 3:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.