How to Get a List of Installed True Type Fonts on Linux Using C or C++

Finding out what characters a given font supports

Here is a method using the fontTools Python library (which you can install with something like pip install fonttools):

#!/usr/bin/env python
from itertools import chain
import sys

from fontTools.ttLib import TTFont
from fontTools.unicode import Unicode

with TTFont(
sys.argv[1], 0, allowVID=0, ignoreDecompileErrors=True, fontNumber=-1
) as ttf:
chars = chain.from_iterable(
[y + (Unicode[y[0]],) for y in x.cmap.items()] for x in ttf["cmap"].tables
)
if len(sys.argv) == 2: # print all code points
for c in chars:
print(c)
elif len(sys.argv) >= 3: # search code points / characters
code_points = {c[0] for c in chars}
for i in sys.argv[2:]:
code_point = int(i) # search code point
#code_point = ord(i) # search character
print(Unicode[code_point])
print(code_point in code_points)

The script takes as arguments the font path and optionally code points / characters to search for:

$ python checkfont.py /usr/share/fonts/**/DejaVuSans.ttf
(32, 'space', 'SPACE')
(33, 'exclam', 'EXCLAMATION MARK')
(34, 'quotedbl', 'QUOTATION MARK')


$ python checkfont.py /usr/share/fonts/**/DejaVuSans.ttf 65 12622 # a ㅎ
LATIN CAPITAL LETTER A
True
HANGUL LETTER HIEUH
False

How can I get a list of installed fonts on Windows, using unmanaged C++?

You can do it something like this:

LOGFONT lf;
lf.lfFaceName[0] = '\0';
lf.lfCharSet = DEFAULT_CHARSET;
HDC hDC = ::GetDC();
EnumFontFamiliesEx(hDC, &lf, (FONTENUMPROC)&EnumFontFamExProc, 0, 0);
ReleaseDC(hDC);

Then define a callback function:

int CALLBACK EnumFontFamExProc(
ENUMLOGFONTEX *lpelfe,
NEWTEXTMETRICEX *lpntme,
DWORD FontType,
LPARAM lParam
)
{
AfxMessageBox(lpelfe->elfFullName);

//Return non--zero to continue enumeration
return 1;
}

Install fonts in Linux container for ASP.NET Core

Got it. Revise the start of your Dockerfile as follows:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

#Add these two lines
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig

WORKDIR /app
EXPOSE 80
[...]

The first line updates the default /etc/apt/sources.list file in the Linux OS to include the 'contrib' archive area (which is where ttf-mscorefonts-installer lives). That ensures apt-get can find it and install it as normal in the second line (along with fontconfig, which you'll also need.)

For the record, this page suggested using the "fonts-liberation" package instead of ttf-mscorefonts-installer, which you can also get working with two different lines at the start of the Dockerfile as follows:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

#Add these two lines for fonts-liberation instead
RUN apt-get update; apt-get install -y fontconfig fonts-liberation
RUN fc-cache -f -v

WORKDIR /app
EXPOSE 80

[...]

How to install a Font programmatically (C#)

As you mentioned, you can launch other executables to install TrueType Fonts for you. I don't know your specific use cases but I'll run down the methods I know of and maybe one will be of use to you.

Windows has a built-in utility called fontview.exe, which you can invoke simply by calling Process.Start("Path\to\file.ttf") on any valid TrueType Font... assuming default file associations. This is akin to launching it manually from Windows Explorer. The advantage here is it's really trivial, but it still requires user interaction per font to install. As far as I know there is no way to invoke the "Install" portion of this process as an argument, but even if there was you'd still have to elevate permissions and battle UAC.

The more intriguing option is a utility called FontReg that replaces the deprecated fontinst.exe that was included on older versions of Windows. FontReg enables you to programatically install an entire directory of Fonts by invoking the executable with the /copy switch:

    var info = new ProcessStartInfo()
{
FileName = "Path\to\FontReg.exe",
Arguments = "/copy",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden

};

Process.Start(info);

Note that the Fonts have to be in the root of wherever FontReg.exe is located. You'll also have to have administrator privileges. If you need your Font installations to be completely transparent, I would suggest launching your application with elevated permissions and approve of the UAC up front, that way when you spawn your child processes you wont need user approval Permissions stuff



Related Topics



Leave a reply



Submit