Fredrik Lundh avatar Fredrik Lundh committed 1fe24d3

Search for truetype fonts in well-known directories also on Linux.

Comments (0)

Files changed (1)

 # 2002-12-04 fl   skip non-directory entries in the system path
 # 2003-04-29 fl   add embedded default font
 # 2003-09-27 fl   added support for truetype charmap encodings
+# 2011-01-10 fl   search for truetype fonts in "well-known directories"
 #
 # Todo:
 # Adapt to PILFONT2 format (16-bit fonts, compressed, single file)
 #
-# Copyright (c) 1997-2003 by Secret Labs AB
-# Copyright (c) 1996-2003 by Fredrik Lundh
+# Copyright (c) 1997-2010 by Secret Labs AB
+# Copyright (c) 1996-2010 by Fredrik Lundh
 #
 # See the README file for information on usage and redistribution.
 #
 
 # FIXME: add support for pilfont2 format (see FontFile.py)
 
+#
+# look for "well-known" font directories
+
+DIRS = []
+
+if sys.platform == "win32":
+    # check the windows font repository
+    # NOTE: must use uppercase WINDIR, to work around bugs in
+    # 1.5.2's os.environ.get()
+    windir = os.environ.get("WINDIR")
+    if windir:
+        FONTDIRS.append(os.path.join(windir, "fonts"))
+else:
+    for dir in [
+        # some common linux font directories
+        "/usr/share/fonts/truetype",
+        "/usr/share/fonts/truetype/msttcorefonts",
+        "/usr/share/fonts/truetype/ttf-dejavu",
+        "/usr/share/fonts/truetype/ttf-liberation",
+        ]:
+        if os.path.isdir(dir):
+            DIRS.append(dir)
+
 # --------------------------------------------------------------------
 # Font metrics format:
 #       "PILfont" LF
     try:
         return FreeTypeFont(filename, size, index, encoding)
     except IOError:
-        if sys.platform == "win32":
-            # check the windows font repository
-            # NOTE: must use uppercase WINDIR, to work around bugs in
-            # 1.5.2's os.environ.get()
-            windir = os.environ.get("WINDIR")
-            if windir:
-                filename = os.path.join(windir, "fonts", filename)
-                return FreeTypeFont(filename, size, index, encoding)
+        for directory in DIRS:
+            # look for font in well-known directories
+            try:
+                path = os.path.join(directory, filename)
+                return FreeTypeFont(path, size, index, encoding)
+            except IOError:
+                pass
         raise
 
 ##
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.