Search code examples
c++linuxwindowsmacosfonts

Platform-independent font directory?


Where are fonts stored on Windows, macOS, and Linux?


Solution

  • This is one of those 'simple' problems that could have an over-the-top solution depending on the need. Linux distributions don't use any specific font management making fonts across Linux flavours inconsistent, very configurable, and influenced by many factors including desktop environment, remote services, and more. A Linux distribution may not have any font directories specified at all (e.g., on a headless RTOS installation or using an X Font Server that serves up fonts remotely).

    Font directory locations

    • Windows (newer than 3.1)
      • %WINDIR%\fonts
    • Mac OSX
      • /System/Library/Fonts - Fonts necessary for the system. Do not touch these.
      • /Library/Fonts - Additional fonts for all users. Fonts that are to be used by other applications, in general.
      • ~/Library/Fonts - User-specific fonts.
      • /Network/Library/Fonts - Fonts shared for users on a network.
    • Linux
      • /usr/share/fonts - Common location across many Linux distributions.
      • /usr/local/share/fonts - Common location across many Linux distributions.
      • ~/.fonts - User-specific fonts.

    Some Linux systems may have configured font directories in the following files:

    • /etc/fonts/fonts.conf
    • /etc/fonts/local.conf

    For example, run grep "<dir>" /etc/fonts/fonts.conf, which may show:

    <dir>/usr/share/fonts</dir>
    <dir>/usr/local/share/fonts</dir>
    <dir>~/.fonts</dir>
    

    Checking for environment

    You can check various platforms via the use of macros defined for specific environments.

    • Windows - #if defined(_WIN32)
      • _WIN32 is defined for both 32-bit and 64-bit Windows.
    • Mac OSX - #if defined(_APPLE_) && defined(_MACH_)
      • _APPLE_ is defined for all Apple computers, and _MACH_ is defined if the system supports Mach system calls, a la Mac OSX
    • Linux (generic) - #if defined(linux) || defined(__linux)

    Resources