Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform independent way to get font directory?

Tags:

c++

fonts

Is there a way I could find where fonts are stored on either Windows, OSX, or Linux? If not, is there a way I can guarantee certain paths (such as X:/Windows/Fonts/) for all 3 platforms? What ifdefs would I use for these?

like image 809
jmasterx Avatar asked Oct 17 '10 16:10

jmasterx


2 Answers

This is going to be one of those 'simple' problems could have an over-the-top solution depending on what you need this information for.

I will have to apologize for the vaguer Linux answers, as font management across Linux distributions are not consistent and can be very configurable, can be influenced by desktop environment, can be remotely served, etc.

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)

Font directory locations

  • Windows
    • On Windows newer than 3.1, the font directory is located in %WINDIR%\fonts.
  • Mac OS X
    • Mac OSX has multiple font directories
      • /System/Library/Fonts - Fonts necessary for the system. Do not touch these.
      • /Library/Fonts - Additional fonts that can be used by all users. This is generally where fonts go if they are to be used by other applications.
      • ~/Library/Fonts - Fonts specific to each user.
      • /Network/Library/Fonts - Fonts shared for users on a network.
  • Linux
    • As mentioned above, a Linux distribution may not have specified font directories at all. I remember dealing with this issue a while back since Linux distros don't use any specific font management.
    • There could be an XFS (X Font Server) serving up fonts remotely.
    • The most common locations for fonts across Linux distributions are /usr/share/fonts, /usr/local/share/fonts, and user-specific ~/.fonts
    • Some systems may have configured font directories in the file /etc/fonts/fonts.conf or /etc/fonts/local.conf.

Resources:

  • Mac OS X: Font Locations
  • How to install or remove a font in Windows
  • Predefined C/C++ Macros Project
  • Font Configuration: Arch Linux Wiki
  • Fonts: Ubuntu Linux Wiki
  • Fonts: GNU/Debian Linux Wiki
  • Fonts: CentOS 5 Documentation (Also applies to Red Hat Enterprise Linux 5)
  • X Font Server System
like image 176
逆さま Avatar answered Nov 10 '22 18:11

逆さま


That's assuming the target OS has a font folder. For example, it's quite feasible for a Linux installation to be console-only and not have a font directory at all.

Anyways, my best guess is that there is no platform independent way. You can write your own platform independent function, but within it will have to check the current OS (via some IFDEF's, I cannot say what) and then call the right function. But again - I wouldn't be so sure you can obtain it under Linux at all.

like image 23
Vilx- Avatar answered Nov 10 '22 18:11

Vilx-